Revert "Corresponding to TizenIVI3.0 M14.3, GENIVI-LayerManagement was used instead...
[profile/ivi/ico-uxf-homescreen.git] / tests / homescreen / ico_send_hscommand.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   applicatoin that send a command to homescreen
11  *
12  * @date    Feb-15-2013
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <pthread.h>
20 #include <unistd.h>
21
22 #include <ico_uws.h>
23
24 #include "home_screen.h"
25 #include "home_screen_res.h"
26
27
28 /*============================================================================*/
29 /* Define data types                                                          */
30 /*============================================================================*/
31 #define ICO_HSCMD_WS_TIMEOUT 0.05
32 #define ICO_HSCMD_WS_ADDRESS "127.0.0.1"
33 #define ICO_HSCMD_WS_PROTOCOL_NAME ICO_HS_PROTOCOL_CM
34
35 //#define hscmd_trace(...)
36 #define hscmd_trace(fmt, arg...) fprintf(stderr, ""fmt"\n",##arg)
37
38 /*============================================================================*/
39 /* static(internal) functions prototype                                       */
40 /*============================================================================*/
41 static void hscmd_callback_uws(const struct ico_uws_context *context,
42                             const ico_uws_evt_e event, const void *id,
43                             const ico_uws_detail *detail, void *data);
44
45 static void hscmd_create_ws_context(void);
46 static void hscmd_destroy_ws_context(void);
47 static void hscmd_ws_service_loop(void);
48 static void hscmd_usage(const char *prog);
49
50 /*============================================================================*/
51 /* variabe & table                                                            */
52 /*============================================================================*/
53 static int hscmd_ws_port = ICO_HS_WS_PORT;
54 static int hscmd_ws_connected = 0;
55 static struct ico_uws_context *hscmd_uws_context = NULL;
56 static void *hscmd_uws_id = NULL;
57 static FILE *hscmd_fp;
58 static int hscnd_send_end = 0;
59
60 /*============================================================================*/
61 /* functions                                                                  */
62 /*============================================================================*/
63 /*--------------------------------------------------------------------------*/
64 /*
65  * @brief   hscmd_callback_uws
66  *          callback function from UWS
67  *      
68  * @param[in]   context             context
69  * @param[in]   event               event kinds
70  * @param[in]   id                  client id
71  * @param[in]   detail              event detail
72  * @param[in]   data                user data
73  * @return      none
74  */
75 /*--------------------------------------------------------------------------*/
76 static void 
77 hscmd_callback_uws(const struct ico_uws_context *context,
78                 const ico_uws_evt_e event, const void *id,
79                 const ico_uws_detail *detail, void *data)
80 {
81     unsigned char msg[ICO_HS_TEMP_BUF_SIZE];
82     unsigned char *send;
83     char *in;
84     long fsize;
85     int len;
86
87     hscmd_trace("hscmd_callback_uws %p", context);
88
89     switch (event) {
90     case ICO_UWS_EVT_OPEN:
91         hscmd_trace("hscmd_callback_uws: ICO_UWS_EVT_OPEN(id=%d)", (int)id);
92         hscmd_uws_id = (void *)id;
93         len = sprintf((char *)msg, "%s %s", ICO_HS_MSG_HEAD_CM, HS_REQ_ANS_HELLO);
94         ico_uws_send((struct ico_uws_context *)context, (void *)id, msg, len);
95         break;
96
97     case ICO_UWS_EVT_CLOSE:
98         hscmd_trace("hscmd_callback_uws: ICO_UWS_EVT_CLOSE(id=%d)", (int)id);
99         hscmd_uws_context = NULL;
100         hscmd_uws_id = NULL;
101         break;
102
103     case ICO_UWS_EVT_RECEIVE:
104         hscmd_trace("hscmd_callback_uws: ICO_UWS_EVT_RECEIVE(id=%d, msg=%s, len=%d)",
105                    (int)id, (char *)detail->_ico_uws_message.recv_data,
106                    detail->_ico_uws_message.recv_len);
107         in = (char *)detail->_ico_uws_message.recv_data;
108
109         if(strncmp("ANS HELLO", in, 9) == 0) {
110             fseek(hscmd_fp, 0, SEEK_END);
111             fsize = ftell(hscmd_fp);
112             fseek(hscmd_fp,  0L, SEEK_SET);
113
114             len = (int)fsize + 4;
115             send = (void *)malloc(len);
116
117             memset(send, 0, len);
118
119             sprintf((char *)send, "%s ", ICO_HS_MSG_HEAD_CM);
120
121             fread(send + 4, 1, fsize, hscmd_fp);
122             hscmd_trace("hscmd_callback_uws: send (%s)", send);
123
124             ico_uws_send((struct ico_uws_context *)context, (void *)id, send, len);
125
126             hscnd_send_end = 1;
127         }
128         break;
129
130     case ICO_UWS_EVT_ERROR:
131         hscmd_trace("hscmd_callback_uws: ICO_UWS_EVT_ERROR(id=%d, err=%d)",
132                    (int)id, detail->_ico_uws_error.code);
133         break;
134
135     case ICO_UWS_EVT_ADD_FD:
136         hscmd_trace("hscmd_callback_uws: ICO_UWS_EVT_ADD_FD(id=%d, fd=%d)",
137                    (int)id, detail->_ico_uws_fd.fd);
138         break;
139     
140     case ICO_UWS_EVT_DEL_FD:
141         hscmd_trace("hscmd_callback_uws: ICO_UWS_EVT_DEL_FD(id=%d, fd=%d)",
142                    (int)id, detail->_ico_uws_fd.fd);
143         break;
144
145     default:
146         break;
147     }
148
149     return;
150 }
151
152 /*--------------------------------------------------------------------------*/
153 /**
154  * @brief   hscmd_create_ws_context
155  *          connect to the homescreen using websocket.
156  *
157  * @param[in]   none
158  * @return      none
159  */
160 /*--------------------------------------------------------------------------*/
161 static void
162 hscmd_create_ws_context(void)
163 {
164     int ret;
165     char uri[ICO_HS_TEMP_BUF_SIZE];
166
167     /* set up URI "ws://HOST:PORT" */
168     sprintf(uri, "ws://%s:%d", ICO_HS_WS_HOST, hscmd_ws_port);
169
170     hscmd_uws_context = ico_uws_create_context(uri, ICO_HS_PROTOCOL);
171
172     hscmd_ws_connected = 0;
173     if (hscmd_uws_context == NULL) {
174         fprintf(stderr, "libwebsocket_create_context failed.\n");
175     } else {
176         /* set callback */
177         ret = ico_uws_set_event_cb(hscmd_uws_context, hscmd_callback_uws, NULL);
178         if (ret != ICO_UWS_ERR_NONE) {
179             hscmd_trace("hscmd_create_ws_context: cannnot set callback");
180         }
181         hscmd_ws_connected = 1;
182     }
183 }
184
185 /*--------------------------------------------------------------------------*/
186 /**
187  * @brief   hscmd_destroy_ws_context
188  *          destroy websokets connection.
189  *
190  * @param[in]   none
191  * @return      none
192  */
193 /*--------------------------------------------------------------------------*/
194 static void
195 hscmd_destroy_ws_context(void)
196 {
197     if (hscmd_uws_context) {
198         ico_uws_service(hscmd_uws_context);
199         usleep(50 * 1000);
200         ico_uws_unset_event_cb(hscmd_uws_context);
201         ico_uws_close(hscmd_uws_context);
202         hscmd_uws_context = NULL;
203         hscmd_ws_connected = 0;
204     }
205 }
206
207 static void
208 hscmd_ws_service_loop(void)
209 {
210     while (hscmd_ws_connected) {
211         ico_uws_service(hscmd_uws_context);
212         usleep(50 * 1000);
213         if (hscnd_send_end == 1) {
214             hscmd_destroy_ws_context();
215         }
216     }
217 }
218
219 static void
220 hscmd_usage(const char *prog)
221 {
222     fprintf(stderr, "Usage: %s filename\n", prog);
223     exit(0);
224 }
225
226 /*--------------------------------------------------------------------------*/
227 /*
228  * @brief   test command tools
229  *          send json format command to
230  *          main routine
231  *
232  * @param   main() finction's standard parameter (argc,argv)
233  * @return  result
234  * @retval  0       success
235  * @retval  1       failed
236  */
237 /*--------------------------------------------------------------------------*/
238 int
239 main(int argc, char *argv[])
240 {
241
242     /* read json file */
243     if (argc < 1) {
244         hscmd_usage(argv[0]);
245     }
246
247     hscmd_fp = fopen(argv[1], "rb");
248     if (hscmd_fp == NULL) {
249         hscmd_usage(argv[0]);
250     }
251
252     /* Init websockets */
253     hscmd_create_ws_context();
254
255     hscmd_ws_service_loop();
256
257     hscmd_destroy_ws_context();
258
259     fclose(hscmd_fp);
260
261     return 0;
262 }
263