Fix internal bug and change the design of ico-app-samplenavi
[profile/ivi/ico-uxf-homescreen-sample-apps.git] / ico-app-samplenavi / src / samplenavi.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   sample navigation application main
11  *
12  * @date    Apr-25-2013
13  */
14
15 #include "define.h"
16 #include <stdio.h>
17 #include <stdbool.h>
18 #include <time.h>
19 #include <stddef.h>
20 #include <getopt.h>
21 #include <dbus/dbus.h>
22 #include <libwebsockets.h>
23 #include <EWebKit2.h>
24 #include <Eina.h>
25 #include <Ecore.h>
26 #include <Ecore_Evas.h>
27 #include <Elementary.h>
28 #include "samplenavi.h"
29 #include "e3d.h"
30 #include "ico_apf.h"
31 #include "ico_apf_ecore.h"
32 #include "ico_apf_log.h"
33
34 #ifdef _USE_OPENCV_
35 #include <highgui.h>
36 #endif
37
38 /* DEBUG.s */
39 //#define TEST_TIME 0.001
40 //#define TEST_TIME 0.066
41 #define TEST_TIME 0.5
42
43 //#define DEBUG_METER_TEXT_DSP
44
45 //#define DEBUG_VIC_INFO_SET
46 #ifdef DEBUG_VIC_INFO_SET
47 #include <math.h>
48 extern int route_data_count;
49 extern CsvRoute csv_route[MAX_ROUTE_POINTS];
50 static double test_lat = 35.47945;
51 static double test_lon = 139.40026;
52 static int test_dir = 0;
53 static int test_cnt = 0;
54 #endif
55 /* DEBUG.e */
56
57 /*============================================================================*/
58 /* Define fixed parameters                                                    */
59 /*============================================================================*/
60 /* AMB */
61 #define CONFIG_FILE    "/opt/apps/org.tizen.ico.app-samplenavi/res/config.txt"
62 #define DBUS_SERVICE   "org.automotive.message.broker"
63 #define DBUS_INTERFACE "org.freedesktop.DBus.Properties"
64 #define DBUS_METHOD    "Get"
65 #define MAX_PARA_NUM   4        /* Lat,Lon,Dir,VicSpeed */
66 #define LATITUDE       0
67 #define LONGITUDE      1
68 #define DIRECTION      2
69 #define VEHICLESPEED   3
70
71 /* Setting screen */
72 #define ICO_APP_BUF_SIZE    (1024)
73
74 /*============================================================================*/
75 /* Define data types                                                          */
76 /*============================================================================*/
77 /* AMB */
78 union VicVal_t
79 {
80     dbus_int32_t i32_val;
81     dbus_int32_t i16_val;
82     dbus_uint32_t u32_val;
83     dbus_uint16_t u16_val;
84     uint8_t byte_val;
85     dbus_bool_t b_val;
86     double d_val;
87     char *s_val;
88 };
89
90 struct vic_data_t
91 {
92     int property;
93     char name[32];
94     char path_name[64];
95     char interface_name[64];
96     char property_name[32];
97 };
98
99 struct vic_key_data_t
100 {
101     int id;
102     char name[32];
103 };
104
105 /*============================================================================*/
106 /* Function prototype for static(internal) functions                          */
107 /*============================================================================*/
108 static double get_time();
109 static void set_default_data();
110 static void load_config_file();
111 static void e_ui_draw();
112 static void e_ui_init();
113 static void e_map_init();
114 static void e_map_draw();
115 #ifdef _USE_OPENCV_
116 static void convert_data_evas_cv(Evas_Object *eo, IplImage *iplimage);
117 static void evas_object_image_from_cv(Evas_Object *eo, const char *filepath);
118 #endif
119 static Eina_Bool _time_interval_navi_cb(void *cam);
120 static Eina_Bool _time_interval_map_renew_cb(void *data);
121 static Eina_Bool callback_listener(void *data);
122 static int callback_http(struct libwebsocket_context *context,
123                          struct libwebsocket *wsi,
124                          enum libwebsocket_callback_reasons reason,
125                          void *user, void *in, size_t len);
126 static bool parse_elementary_value(union VicVal_t *vic_val_p,
127                                    DBusMessageIter *iter);
128 static bool parse_value(union VicVal_t *vic_val_p, DBusMessageIter *iter);
129 static int getAmbDbus(int key, union VicVal_t *vic_val_p);
130 static void getLocationFromAmb();
131 static int get_config();
132 static char *edje_parse_str(void *in, int arg_num);
133 static void event_message(struct libwebsocket *wsi, char *format, ...);
134 static void _touch_up_edje(void *data, Evas *evas, Evas_Object *obj,
135                            void *event_info);
136 static int loading_edje_file(const char *edje_file);
137 static void res_callback(ico_apf_resource_notify_info_t *info,
138                          void *user_data);
139
140 /*============================================================================*/
141 /* Tables and Valiables                                                       */
142 /*============================================================================*/
143 int conf_data[CONF_DATA_MAX];
144 static const char *config_path = RESOURCE_DIR "/samplenavi.conf";
145 static const char *setting_button_path = IMAGES_DIR "/na_bt3_off.png";
146 static const char *goal_text_img_path = IMAGES_DIR "/na_goal_text.png";
147 static const char *default_url =
148     "file:///opt/apps/org.tizen.ico.app-samplenavi/res/Map/map.html";
149
150 /* Screen right */
151 static char sscrntype[32];
152
153 /* Setting screen */
154 static Evas *g_evas;
155 static Evas_Object *g_edje;     /* loaded edje objects */
156 static char edje_str[ICO_APP_BUF_SIZE];
157
158 /* Meter Display */
159 static int ui_vicspeed = 0;
160 static Evas_Object *ui_vicspeed_text;
161 static Evas_Object *ui_meter_l;
162 static Evas_Object *ui_meter_c[10];
163 static Evas_Object *ui_meter_r[10];
164 static int fig_l = 0;
165 static int fig_c = 0;
166 static int fig_r = 0;
167
168 const static char *meter_l_path = {
169     IMAGES_DIR "/Meter/Meters_SpeedNum_1l.png"
170 };
171
172 const static char *meter_c_path[10] = {
173     IMAGES_DIR "/Meter/Meters_SpeedNum_0c.png",
174     IMAGES_DIR "/Meter/Meters_SpeedNum_1c.png",
175     IMAGES_DIR "/Meter/Meters_SpeedNum_2c.png",
176     IMAGES_DIR "/Meter/Meters_SpeedNum_3c.png",
177     IMAGES_DIR "/Meter/Meters_SpeedNum_4c.png",
178     IMAGES_DIR "/Meter/Meters_SpeedNum_5c.png",
179     IMAGES_DIR "/Meter/Meters_SpeedNum_6c.png",
180     IMAGES_DIR "/Meter/Meters_SpeedNum_7c.png",
181     IMAGES_DIR "/Meter/Meters_SpeedNum_8c.png",
182     IMAGES_DIR "/Meter/Meters_SpeedNum_9c.png"
183 };
184
185 const static char *meter_r_path[10] = {
186     IMAGES_DIR "/Meter/Meters_SpeedNum_0r.png",
187     IMAGES_DIR "/Meter/Meters_SpeedNum_1r.png",
188     IMAGES_DIR "/Meter/Meters_SpeedNum_2r.png",
189     IMAGES_DIR "/Meter/Meters_SpeedNum_3r.png",
190     IMAGES_DIR "/Meter/Meters_SpeedNum_4r.png",
191     IMAGES_DIR "/Meter/Meters_SpeedNum_5r.png",
192     IMAGES_DIR "/Meter/Meters_SpeedNum_6r.png",
193     IMAGES_DIR "/Meter/Meters_SpeedNum_7r.png",
194     IMAGES_DIR "/Meter/Meters_SpeedNum_8r.png",
195     IMAGES_DIR "/Meter/Meters_SpeedNum_9r.png"
196 };
197
198 /* AMB */
199 static DBusConnection *g_connection = NULL;     /* D-Bus Connection */
200 static int property_num = 0;
201 const static char Bus_name[] = DBUS_SERVICE;
202 static struct vic_data_t vic_data[MAX_PARA_NUM];
203
204 const struct vic_key_data_t vic_key_data[] = {
205     {LATITUDE    , "Latitude"    },
206     {LONGITUDE   , "Longitude"   },
207     {DIRECTION   , "Direction"   },
208     {VEHICLESPEED, "VehicleSpeed"},
209     {-1          , "END"         }
210 };
211
212 /* BASE */
213 static Ecore_Evas *window;
214 static Evas *e_cam;
215 static Evas *e_3d;
216 static Evas *e_ui;
217 static Evas *e_map;
218
219 #ifdef _USE_OPENCV_
220 IplImage *captureImage;
221 static CvCapture *capture;
222 #endif
223
224 Evas_Object *cam;
225
226 Evas_Object *ui_bg;
227 Evas_Object *ui_btn;
228 Evas_Object *ui_goal_text;
229 Evas_Object *ui_address;
230
231 #ifdef _SHOW_FPS_
232 Evas_Object *ui_fps;
233 Evas_Object *ui_fps_bg;
234 Evas_Object *ui_polygon_count;
235 Evas_Object *ui_polygon_count_bg;
236 double fps;
237 #endif
238
239 Evas_Object *browser;
240
241 /* CAMERA */
242 static const char *test_camera_path = IMAGES_DIR "/test_cameraview.png";
243
244 /* WEBSOCKET */
245 static int connected = 0;
246 static struct libwebsocket *socket_val = NULL;
247 static struct libwebsocket_context *context;
248 static struct libwebsocket_protocols protocols[] = {
249     {
250      "http-only",          /* name */
251      callback_http,        /* callback */
252      0                     /* per_session_data_size */
253      },
254     {
255      NULL, NULL, 0}
256 };
257
258 static int port = 50414;
259 static char addr[16] = "127.0.0.1";
260 static char proxy_uri[512] = "";
261
262 /*============================================================================*/
263 /* functions                                                                  */
264 /*============================================================================*/
265 /*--------------------------------------------------------------------------*/
266 /*
267  * @brief   get_time
268  *          Time acquisition(second).
269  *
270  * @param       none
271  * @return      time(second)
272  */
273 /*--------------------------------------------------------------------------*/
274 static double get_time()
275 {
276     struct timeval sec_timeofday;
277     gettimeofday(&sec_timeofday, NULL);
278     return ((sec_timeofday.tv_sec) + (sec_timeofday.tv_usec / 1000000.0));
279 }
280
281 /*--------------------------------------------------------------------------*/
282 /*
283  * @brief   set_default_data
284  *          Set Default data without define in conf file.
285  *
286  * @param       none
287  * @return      none
288  */
289 /*--------------------------------------------------------------------------*/
290 static void set_default_data()
291 {
292     int i;
293
294     for (i = 0; i < CONF_DATA_MAX; i++) {
295         if (conf_data[i] == 0) {
296             switch (i) {
297             case USB_CAMERA_ID:
298                 conf_data[USB_CAMERA_ID] = DEF_USB_CAMERA_ID;
299                 break;
300             case LANDMARK_POSITION:
301                 conf_data[LANDMARK_POSITION] = DEF_LANDMARK_POSITION;
302                 break;
303             case LANDMARK_ROTATION:
304                 conf_data[LANDMARK_ROTATION] = DEF_LANDMARK_ROTATION;
305                 break;
306             }
307         }
308     }
309 }
310
311 /*--------------------------------------------------------------------------*/
312 /*
313  * @brief   load_config_file
314  *          A config file is loaded.
315  *
316  * @param       none
317  * @return      none
318  */
319 /*--------------------------------------------------------------------------*/
320 static void load_config_file()
321 {
322     FILE *fp;
323     char str[512];
324     char *valuestr;
325     int i = 0;
326
327     for (i = 0; i < CONF_DATA_MAX; i++) {
328         conf_data[i] = 0;
329     }
330
331     if ((fp = fopen(config_path, "r")) == NULL) {
332         fprintf(stderr, "%s\n",
333                 "Error : can't open file.(samplenavi.conf)\n");
334         set_default_data();
335
336         uim_debug("USB_CAMERA_ID = %d", conf_data[USB_CAMERA_ID]);
337         uim_debug("LANDMARK_POSITION = %d", conf_data[LANDMARK_POSITION]);
338         uim_debug("LANDMARK_ROTATION = %d", conf_data[LANDMARK_ROTATION]);
339         return;
340     }
341
342     while (fscanf(fp, "%s", str) != EOF) {
343         if (strncmp(str, "USBCAMERAID", 11) == 0) {
344             valuestr = strtok((char *) (str) + 12, "=");
345             if (valuestr != NULL) {
346                 conf_data[USB_CAMERA_ID] = atoi(valuestr);
347             }
348         }
349         else if (strncmp(str, "LANDMARKPOSITION", 16) == 0) {
350             valuestr = strtok((char *) (str) + 17, "=");
351             if (valuestr != NULL) {
352                 conf_data[LANDMARK_POSITION] = atoi(valuestr);
353             }
354         }
355         else if (strncmp(str, "LANDMARKROTATION", 16) == 0) {
356             valuestr = strtok((char *) (str) + 17, "=");
357             if (valuestr != NULL) {
358                 conf_data[LANDMARK_ROTATION] = atoi(valuestr);
359             }
360         }
361         else if (strncmp(str, "WEBSOCKETPORT", 13) == 0) {
362             valuestr = strtok((char *) (str) + 14, "=");
363             if (valuestr != NULL) {
364                 port = atoi(valuestr);
365             }
366         }
367         else if (strncmp(str, "IPADDR", 6) == 0) {
368             valuestr = strtok((char *) (str) + 7, "=");
369             if (valuestr != NULL) {
370                 strcpy(addr, valuestr);
371             }
372         }
373         else if (strncmp(str, "PROXYURI", 8) == 0) {
374             valuestr = strtok((char *) (str) + 9, "=");
375             if (valuestr != NULL) {
376                 strcpy(proxy_uri, valuestr);
377             }
378         }
379     }
380
381     fclose(fp);
382
383     set_default_data();
384
385     uim_debug("USB_CAMERA_ID = %d", conf_data[USB_CAMERA_ID]);
386     uim_debug("LANDMARK_POSITION = %d", conf_data[LANDMARK_POSITION]);
387     uim_debug("LANDMARK_ROTATION = %d", conf_data[LANDMARK_ROTATION]);
388     uim_debug("WEBSOKET_PORT  = %d", port);
389     uim_debug("IP_ADDR = %s", addr);
390     uim_debug("PROXY_URI = %s", proxy_uri);
391
392     return;
393 }
394
395 /*--------------------------------------------------------------------------*/
396 /*
397  * @brief   e_ui_draw
398  *          Draw UI Layer.
399  *
400  * @param       none
401  * @return      none
402  */
403 /*--------------------------------------------------------------------------*/
404 static void e_ui_draw()
405 {
406 #ifdef _SHOW_FPS_
407     char str[15];
408 #endif
409
410     int value = 0;
411     int w_fig_r = 0;
412     int w_fig_c = 0;
413     int w_fig_l = 0;
414
415     evas_object_show(ui_bg);
416     evas_object_show(ui_btn);
417
418     /* Meter */
419 #ifdef DEBUG_METER_TEXT_DSP
420     sprintf(str, "[kph]  %d", ui_vicspeed);
421     evas_object_text_text_set(ui_vicspeed_text, str);
422     evas_object_show(ui_vicspeed_text);
423 #else
424     evas_object_show(ui_vicspeed_text);
425     value = ui_vicspeed;
426     if (value > 199) {
427         uim_debug("MAX SPEED OVER [%d]", value);
428         value = 199;
429     }
430
431     w_fig_r = (value % 10);
432     value /= 10;
433     w_fig_c = (value % 10);
434     value /= 10;
435     w_fig_l = (value % 10);
436     value /= 10;
437
438     if (w_fig_r != fig_r) {
439         evas_object_hide(ui_meter_r[fig_r]);
440         evas_object_show(ui_meter_r[w_fig_r]);
441         fig_r = w_fig_r;
442     }
443
444     if (w_fig_c != fig_c || fig_c == 0) {
445         evas_object_hide(ui_meter_c[fig_c]);
446
447         if (w_fig_c != 0 || w_fig_l == 1) {
448             evas_object_show(ui_meter_c[w_fig_c]);
449         }
450
451         fig_c = w_fig_c;
452     }
453
454     if (w_fig_l != fig_l) {
455         evas_object_hide(ui_meter_l);
456
457         if (w_fig_l != 0) {
458             evas_object_show(ui_meter_l);
459         }
460
461         fig_l = w_fig_l;
462     }
463 #endif
464
465     if (enable_navi == TRUE && set_route == TRUE) {
466         if (goal_square_length <= (GOAL_MESSAGE_LENGTH * GOAL_MESSAGE_LENGTH)) {
467             evas_object_show(ui_goal_text);
468         }
469         else {
470             evas_object_hide(ui_goal_text);
471         }
472     }
473
474 #ifdef _SHOW_FPS_
475     sprintf(str, "FPS : %.2f", fps);
476     evas_object_text_text_set(ui_fps, str);
477     sprintf(str, "Polygon : %d", polygon_count);
478     evas_object_text_text_set(ui_polygon_count, str);
479 #endif
480 }
481
482 /*--------------------------------------------------------------------------*/
483 /*
484  * @brief   e_ui_init
485  *          Initialize UI Layer.
486  *
487  * @param       none
488  * @return      none
489  */
490 /*--------------------------------------------------------------------------*/
491 static void e_ui_init()
492 {
493 #ifdef _SHOW_FPS_
494     char str[15];
495 #endif
496     int i = 0;
497
498     ui_bg = evas_object_rectangle_add(e_ui);
499     evas_object_color_set(ui_bg, 0, 34, 119, 255);
500     evas_object_move(ui_bg, W_TEXTAREA_X, W_TEXTAREA_Y);
501     evas_object_resize(ui_bg, W_TEXTAREA_WIDTH, W_TEXTAREA_HEIGHT);
502     evas_object_show(ui_bg);
503     evas_object_layer_set(ui_bg, LAYER_UI);
504
505     ui_btn = evas_object_image_add(e_ui);
506     evas_object_image_file_set(ui_btn, setting_button_path, NULL);
507     evas_object_image_fill_set(ui_btn, 0, 0, W_NAVIBUTTON_WIDTH,
508                                W_NAVIBUTTON_HEIGHT);
509     evas_object_move(ui_btn, W_NAVIBUTTON_X, W_NAVIBUTTON_Y);
510     evas_object_resize(ui_btn, W_NAVIBUTTON_WIDTH, W_NAVIBUTTON_HEIGHT);
511     evas_object_show(ui_btn);
512     evas_object_layer_set(ui_btn, LAYER_UI);
513
514     ui_goal_text = evas_object_image_add(e_ui);
515     evas_object_image_file_set(ui_goal_text, goal_text_img_path, NULL);
516     evas_object_image_fill_set(ui_goal_text, 0, 0, W_GOALMESSAGE_WIDTH,
517                                W_GOALMESSAGE_HEIGHT);
518     evas_object_move(ui_goal_text, W_GOALMESSAGE_X, W_GOALMESSAGE_Y);
519     evas_object_resize(ui_goal_text, W_GOALMESSAGE_WIDTH,
520                        W_GOALMESSAGE_HEIGHT);
521     evas_object_hide(ui_goal_text);
522     evas_object_layer_set(ui_goal_text, LAYER_UI);
523
524     /* Meter */
525 #ifdef DEBUG_METER_TEXT_DSP
526     ui_vicspeed_text = evas_object_text_add(e_ui);
527     evas_object_text_style_set(ui_vicspeed_text, EVAS_TEXT_STYLE_PLAIN);
528     evas_object_color_set(ui_vicspeed_text, 255, 255, 255, 255);
529     evas_object_text_font_set(ui_vicspeed_text, "Utopia", 28);
530     sprintf(str, "[kph]  %d", ui_vicspeed);
531     evas_object_text_text_set(ui_vicspeed_text, str);
532     evas_object_move(ui_vicspeed_text, 0, 430);
533     evas_object_resize(ui_vicspeed_text, W_METER_UNIT_WIDTH,
534                        W_METER_UNIT_HEIGHT);
535     evas_object_show(ui_vicspeed_text);
536     evas_object_layer_set(ui_vicspeed_text, LAYER_UI);
537 #else
538     ui_meter_l = evas_object_image_add(e_ui);
539     evas_object_image_file_set(ui_meter_l, meter_l_path, NULL);
540     evas_object_image_fill_set(ui_meter_l, 0, 0, W_METER_HEIGHT,
541                                W_METER_WIDTH);
542     evas_object_move(ui_meter_l, W_METER_X, W_METER_Y);
543     evas_object_resize(ui_meter_l, W_METER_HEIGHT, W_METER_WIDTH);
544     evas_object_hide(ui_meter_l);
545     evas_object_layer_set(ui_meter_l, LAYER_UI);
546
547     for (i = 0; i < 10; i++) {
548         ui_meter_c[i] = evas_object_image_add(e_ui);
549         evas_object_image_file_set(ui_meter_c[i], meter_c_path[i], NULL);
550         evas_object_image_fill_set(ui_meter_c[i], 0, 0, W_METER_HEIGHT,
551                                    W_METER_WIDTH);
552         evas_object_move(ui_meter_c[i], W_METER_X, W_METER_Y);
553         evas_object_resize(ui_meter_c[i], W_METER_HEIGHT, W_METER_WIDTH);
554         evas_object_hide(ui_meter_c[i]);
555         evas_object_layer_set(ui_meter_c[i], LAYER_UI);
556
557         ui_meter_r[i] = evas_object_image_add(e_ui);
558         evas_object_image_file_set(ui_meter_r[i], meter_r_path[i], NULL);
559         evas_object_image_fill_set(ui_meter_r[i], 0, 0, W_METER_HEIGHT,
560                                    W_METER_WIDTH);
561         evas_object_move(ui_meter_r[i], W_METER_X, W_METER_Y);
562         evas_object_resize(ui_meter_r[i], W_METER_HEIGHT, W_METER_WIDTH);
563         evas_object_hide(ui_meter_r[i]);
564         evas_object_layer_set(ui_meter_r[i], LAYER_UI);
565     }
566     evas_object_show(ui_meter_r[0]);
567
568     ui_vicspeed_text = evas_object_text_add(e_ui);
569     evas_object_text_style_set(ui_vicspeed_text, EVAS_TEXT_STYLE_PLAIN);
570     evas_object_color_set(ui_vicspeed_text, 255, 255, 255, 255);
571     evas_object_text_font_set(ui_vicspeed_text, "Utopia", 24);
572     evas_object_text_text_set(ui_vicspeed_text, "kph");
573     evas_object_move(ui_vicspeed_text, W_METER_UNIT_X, W_METER_UNIT_Y);
574     evas_object_resize(ui_vicspeed_text, W_METER_UNIT_WIDTH,
575                        W_METER_UNIT_HEIGHT);
576     evas_object_show(ui_vicspeed_text);
577
578     evas_object_layer_set(ui_vicspeed_text, LAYER_UI);
579 #endif
580
581 #ifdef _SHOW_FPS_
582     sprintf(str, "FPS : %.2f", fps);
583
584     ui_fps_bg = evas_object_rectangle_add(e_ui);
585     evas_object_color_set(ui_fps_bg, 255, 255, 255, 255);
586     evas_object_move(ui_fps_bg, 0, 0);
587     evas_object_resize(ui_fps_bg, 150, 20);
588     evas_object_show(ui_fps_bg);
589     evas_object_layer_set(ui_fps_bg, LAYER_UI);
590
591     ui_fps = evas_object_text_add(e_ui);
592     evas_object_text_style_set(ui_fps, EVAS_TEXT_STYLE_PLAIN);
593     evas_object_color_set(ui_fps, 0, 0, 0, 255);
594     evas_object_text_font_set(ui_fps, "Utopia", 14);
595     evas_object_text_text_set(ui_fps, str);
596     evas_object_move(ui_fps, 0, 0);
597     evas_object_resize(ui_fps, 250, 250);
598     evas_object_show(ui_fps);
599     evas_object_layer_set(ui_fps, LAYER_UI);
600
601     sprintf(str, "Polygon : %d", polygon_count);
602
603     ui_polygon_count_bg = evas_object_rectangle_add(e_ui);
604     evas_object_color_set(ui_polygon_count_bg, 255, 255, 255, 255);
605     evas_object_move(ui_polygon_count_bg, 0, 20);
606     evas_object_resize(ui_polygon_count_bg, 150, 20);
607     evas_object_show(ui_polygon_count_bg);
608     evas_object_layer_set(ui_polygon_count_bg, LAYER_UI);
609
610     ui_polygon_count = evas_object_text_add(e_ui);
611     evas_object_text_style_set(ui_polygon_count, EVAS_TEXT_STYLE_PLAIN);
612     evas_object_color_set(ui_polygon_count, 0, 0, 0, 255);
613     evas_object_text_font_set(ui_polygon_count, "Utopia", 14);
614     evas_object_text_text_set(ui_polygon_count, str);
615     evas_object_move(ui_polygon_count, 0, 20);
616     evas_object_resize(ui_polygon_count, 250, 250);
617     evas_object_show(ui_polygon_count);
618     evas_object_layer_set(ui_polygon_count, LAYER_UI);
619 #endif
620 }
621
622 /*--------------------------------------------------------------------------*/
623 /*
624  * @brief   e_map_init
625  *          Initialize MAP Layer.
626  *
627  * @param       none
628  * @return      none
629  */
630 /*--------------------------------------------------------------------------*/
631 static void e_map_init()
632 {
633     e_map = ecore_evas_get(window);
634     browser = ewk_view_add(e_map);
635
636     if(strlen(proxy_uri)) {
637         ewk_context_proxy_uri_set(ewk_view_context_get(browser), proxy_uri);
638     }
639
640     evas_object_move(browser, W_MAP_ORIGINE_X, W_MAP_ORIGINE_Y);
641     evas_object_resize(browser, W_MAP_WIDTH, W_MAP_HEIGHT);
642     evas_object_show(browser);
643     ewk_view_uri_set(browser, default_url);
644     evas_object_layer_set(browser, LAYER_MAP);
645 }
646
647 /*--------------------------------------------------------------------------*/
648 /*
649  * @brief   e_map_draw
650  *          Draw Map Layer(EWebKit2)
651  *
652  * @param       none
653  * @return      none
654  */
655 /*--------------------------------------------------------------------------*/
656 static void e_map_draw()
657 {
658     evas_object_show(browser);
659 }
660
661 #ifdef _USE_OPENCV_
662 /*--------------------------------------------------------------------------*/
663 /*
664  * @brief   convert_data_evas_cv
665  *          Convert IplImage object to Evas_Object.
666  *
667  * @param[out]  eo              Evas_Object 
668  * @param[in]   iplimagenone    IplImage object
669  * @return      none
670  */
671 /*--------------------------------------------------------------------------*/
672 static void convert_data_evas_cv(Evas_Object *eo, IplImage *iplimage)
673 {
674     int image_data_size = (iplimage->width) * iplimage->height * 4;
675     unsigned char image[image_data_size];
676     int i = 0;
677
678     if (image_data_size == 0) {
679         uim_debug("convert_data_evas_cv: IplImage empty error.");
680         return;
681     }
682
683     for (i = 0; i < (iplimage->width * iplimage->height); i++) {
684         image[i * 4] = iplimage->imageData[(3 * i)];
685         image[(i * 4) + 1] = iplimage->imageData[(3 * i) + 1];
686         image[(i * 4) + 2] = iplimage->imageData[(3 * i) + 2];
687         image[(i * 4) + 3] = 255;
688     }
689
690     /* convert to Evas_Object */
691     evas_object_image_colorspace_set(eo, EVAS_COLORSPACE_ARGB8888);
692     evas_object_image_size_set(eo, iplimage->width, iplimage->height);
693     evas_object_image_data_set(eo, image);
694 }
695
696 /*--------------------------------------------------------------------------*/
697 /*
698  * @brief   evas_object_image_from_cv
699  *          Load the picture to an IplImage object. 
700  *
701  * @param[out]  eo          Evas_Object 
702  * @param[in]   filepath    path of the file
703  * @return      none
704  */
705 /*--------------------------------------------------------------------------*/
706 static void evas_object_image_from_cv(Evas_Object *eo, const char *filepath)
707 {
708     IplImage *iplimage;
709
710     /* load picture */
711     iplimage = cvLoadImage(filepath, CV_LOAD_IMAGE_COLOR);
712     if (iplimage == 0) {
713         uim_debug("cvLoadImageError.");
714     }
715
716     convert_data_evas_cv(eo, iplimage);
717 }
718 #endif
719
720 /*--------------------------------------------------------------------------*/
721 /*
722  * @brief   _time_interval_navi_cb
723  *          main timer for navigation. 
724  *
725  * @param[out]  cam    Evas_Object 
726  * @return      result
727  * @retval      ECORE_CALLBACK_RENEW    Return value to keep a callback
728  * @retval      ECORE_CALLBACK_CANCEL   Return value to remove a callback
729  */
730 /*--------------------------------------------------------------------------*/
731 static Eina_Bool _time_interval_navi_cb(void *cam)
732 {
733     static double webcam_timer = 0;
734
735     /* for calc fps */
736 #ifdef _SHOW_FPS_
737     static double total_frame_time = 0;
738     static int frame_count = 0;
739     static double t0;
740     double t1;
741 #endif
742
743 #ifdef _USE_CAMERA_
744     webcam_timer += TIME_INTERVAL_AR;
745
746     if (webcam_timer >= TIME_INTERVAL_CAMERA) {
747         webcam_timer = 0;
748
749         /* Capture from Camera */
750         cam = evas_object_image_add(e_cam);
751
752         captureImage = cvQueryFrame(capture);
753         convert_data_evas_cv(cam, captureImage);
754
755         evas_object_image_fill_set(cam, 0, 0, W_WIDTH, W_NAVI_HEIGHT);
756         evas_object_move(cam, W_NAVI_ORIGINE_X, W_NAVI_ORIGINE_Y);
757         evas_object_resize(cam, W_WIDTH, W_NAVI_HEIGHT);
758         evas_object_show(cam);
759     }
760 #else
761     evas_object_image_fill_set(cam, 0, 0, W_NAVI_WIDTH, W_NAVI_HEIGHT);
762     evas_object_move(cam, W_NAVI_ORIGINE_X, W_NAVI_ORIGINE_Y);
763     evas_object_resize(cam, W_NAVI_WIDTH, W_NAVI_HEIGHT);
764     evas_object_show(cam);
765 #endif
766
767     getLocationFromAmb();
768
769     calc_camera_coord();
770
771     /* render 3D view */
772     draw_route(e_3d);
773     draw_landmark(e_3d);
774
775     /* render UI view */
776     e_ui_draw();
777
778     /* render Map View */
779     e_map_draw();
780
781 #ifdef _SHOW_FPS_
782     // Moving Average
783     if (frame_count == 0) {
784         t0 = get_time();
785         frame_count++;
786     }
787     else if (frame_count < 5) {
788         t1 = get_time();
789         total_frame_time += t1 - t0;
790         t0 = t1;
791         frame_count++;
792     }
793     else {
794         t1 = get_time();
795         total_frame_time -= total_frame_time / 4;
796         total_frame_time += t1 - t0;
797         t0 = t1;
798
799         fps = 4.0 * 1.0 / total_frame_time;
800
801     }
802 #endif
803
804     return ECORE_CALLBACK_RENEW;
805 }
806
807 /*--------------------------------------------------------------------------*/
808 /*
809  * @brief   _time_interval_map_renew_cb
810  *          timer for renewal of a map.
811  *
812  * @param[out]  data    user data(UNUSED) 
813  * @return      result
814  * @retval      ECORE_CALLBACK_RENEW    Return value to keep a callback
815  * @retval      ECORE_CALLBACK_CANCEL   Return value to remove a callback
816  */
817 /*--------------------------------------------------------------------------*/
818 static Eina_Bool _time_interval_map_renew_cb(void *data)
819 {
820 /* DEBUG.s */
821 #ifdef DEBUG_VIC_INFO_SET
822 double lat_tmp = 0;
823 double lon_tmp = 0;
824 int add_angle = 0;
825
826 if (test_cnt < route_data_count) {
827     test_lat = csv_route[test_cnt].lat;
828     test_lon = csv_route[test_cnt].lon;
829
830     if ((test_cnt + 1) != route_data_count) {
831         lat_tmp = csv_route[test_cnt + 1].lat - test_lat;
832         lon_tmp = csv_route[test_cnt + 1].lon - test_lon;
833
834         if (lat_tmp >= 0 && lon_tmp >= 0) {
835             add_angle = 0;
836             test_dir =
837                 atan((fabs(lon_tmp) / fabs(lat_tmp))) * 180.0 / (atan(1.0) *
838                                                                  4.0);
839         }
840         else if (lat_tmp < 0 && lon_tmp >= 0) {
841             add_angle = 90;
842             test_dir =
843                 atan((fabs(lat_tmp) / fabs(lon_tmp))) * 180.0 / (atan(1.0) *
844                                                                  4.0);
845         }
846         else if (lat_tmp < 0 && lon_tmp < 0) {
847             add_angle = 180;
848             test_dir =
849                 atan((fabs(lon_tmp) / fabs(lat_tmp))) * 180.0 / (atan(1.0) *
850                                                                  4.0);
851         }
852         else if (lat_tmp >= 0 && lon_tmp < 0) {
853             add_angle = 270;
854             test_dir =
855                 atan((fabs(lat_tmp) / fabs(lon_tmp))) * 180.0 / (atan(1.0) *
856                                                                  4.0);
857         }
858         test_dir = test_dir + add_angle;
859     }
860     camera_geocode.lat = test_lat;
861     camera_geocode.lon = test_lon;
862     camera_geocode.dir = test_dir;
863     test_cnt++;
864
865     uim_debug("test_lat = [%f]", test_lat);
866     uim_debug("test_lon = [%f]", test_lon);
867     uim_debug("test_dir = [%d]", test_dir);
868 }
869 #endif
870 /* DEBUG.e */
871
872     if ((socket_val)
873         && ((map_pos.lat != camera_geocode.lat)
874             || (map_pos.lon != camera_geocode.lon))) {
875         map_pos.lat = camera_geocode.lat;
876         map_pos.lon = camera_geocode.lon;
877
878         uim_debug("map_pos.lat = [%f]", map_pos.lat);
879         uim_debug("map_pos.lon = [%f]", map_pos.lon);
880
881         event_message(socket_val, "CHG VAL VIC_INFO LAT %f LON %f",
882                       map_pos.lat, map_pos.lon);
883         uim_debug("SEND LAT LON TO SINARIO(samplenavi)");
884     }
885
886     return ECORE_CALLBACK_RENEW;
887 }
888
889 /*--------------------------------------------------------------------------*/
890 /*
891  * @brief   callback_listener
892  *          timer for libwebsocket connection surveillance.
893  *
894  * @param[out]  data    libwebsocket context 
895  * @return      result
896  * @retval      ECORE_CALLBACK_RENEW    Return value to keep a callback
897  * @retval      ECORE_CALLBACK_CANCEL   Return value to remove a callback
898  */
899 /*--------------------------------------------------------------------------*/
900 static Eina_Bool callback_listener(void *data)
901 {
902     if (connected) {
903         libwebsocket_service((struct libwebsocket_context *) data, 0);
904     }
905     else {
906         uim_debug("WEBSOCKET CONNECTED ERROR");
907         if (context != NULL) {
908             libwebsocket_context_destroy(context);
909         }
910         /* Server */
911         context = libwebsocket_create_context(port, NULL,
912                                               protocols,
913                                               libwebsocket_internal_extensions,
914                                               NULL, NULL, -1, -1, 0);
915
916         if (context == NULL) {
917             uim_debug("libwebsocket_create_context failed. (line:%d)",
918                       __LINE__);
919             sleep(1);
920             return 0;
921         }
922
923         connected = 1;
924     }
925
926     return ECORE_CALLBACK_RENEW;
927 }
928
929 /*--------------------------------------------------------------------------*/
930 /*
931  * @brief   callback_http
932  *          this callback function is notified from libwebsockets
933  *
934  * @param[in]   context    libwebsocket context 
935  * @param[in]   wsi        libwebsockets management table
936  * @param[in]   reason     event type 
937  * @param[in]   user       intact 
938  * @param[in]   in         receive message 
939  * @param[in]   len        message size[BYTE]
940  * @return      result
941  * @retval      =0         success
942  * @retval      =1         error
943  */
944 /*--------------------------------------------------------------------------*/
945 static int
946 callback_http(struct libwebsocket_context *context, struct libwebsocket *wsi,
947               enum libwebsocket_callback_reasons reason, void *user, void *in,
948               size_t len)
949 {
950     int n = 0;
951     unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
952                       LWS_SEND_BUFFER_POST_PADDING];
953     unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
954     char data[512];
955
956     uim_debug("call callback_http! size = %d", len);
957     uim_debug("callback_http REASON %d", reason);
958
959     switch (reason) {
960     case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
961         uim_debug("CONNECTION %x", wsi);
962         break;
963     case LWS_CALLBACK_ESTABLISHED:
964         uim_debug("ESTABLISHED %x", wsi);
965         socket_val = wsi;
966         n = sprintf((char *) p, "%s", "ANS HELLO");
967         break;
968     case LWS_CALLBACK_ADD_POLL_FD:
969         uim_debug("RECIEVE REASON LWS_CALLBACK_ADD_POLL_FD");
970         break;
971     case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
972         uim_debug
973             ("RECIEVE REASON LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED");
974         break;
975     case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
976         uim_debug
977             ("RECIEVE REASON LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER");
978         break;
979     case LWS_CALLBACK_CLIENT_ESTABLISHED:
980         uim_debug("RECIEVE REASON LWS_CALLBACK_CLIENT_ESTABLISHED");
981         break;
982     case LWS_CALLBACK_SET_MODE_POLL_FD:
983         uim_debug("RECIEVE REASON LWS_CALLBACK_SET_MODE_POLL_FD");
984         break;
985     case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
986         uim_debug("RECIEVE REASON LWS_CALLBACK_CLEAR_MODE_POLL_FD");
987         break;
988     case LWS_CALLBACK_CLIENT_WRITEABLE:
989         uim_debug("RECIEVE REASON LWS_CALLBACK_CLIENT_WRITEABLE");
990         break;
991     case LWS_CALLBACK_RECEIVE:
992
993         if ((in != NULL)
994             && (strncmp((char *) in, "OPEN", strlen("OPEN")) == 0)) {
995
996             uim_debug("OPEN loading_edje_file [%s]", (char *) in);
997             strncpy(edje_str, edje_parse_str(in, 1), sizeof(edje_str));
998             loading_edje_file(&edje_str[0]);
999             if (socket_val) {
1000                 event_message(socket_val, "RESULT SUCCESS");
1001                 uim_debug("SEND RESULT SUCCESS");
1002             }
1003         }
1004         else if ((in != NULL) && (strncmp("CLOSE", in, strlen("CLOSE")) == 0)) {
1005             evas_object_hide(g_edje);
1006             evas_object_del(g_edje);
1007             uim_debug("ONSCREEN CLOSE");
1008         }
1009
1010         /* Get route from navi.js */
1011         if (strncmp((char *) in, "<route>", strlen("<route>")) == 0) {
1012             uim_debug("<route> recv");
1013             init_e3d(e_3d, in, len);
1014             set_route = TRUE;
1015             break;
1016         }
1017
1018         memset(data, 0, sizeof(data));
1019         strncpy(data, (char *) in, len);
1020         uim_debug("RECIEVE[%d] %s", len, data);
1021
1022         if (len == 0) {
1023             uim_debug("ERROR data=null (line:%d)", __LINE__);
1024             break;
1025         }
1026
1027         if (strncmp((char *) data, "CHG SEQ REQ_NAV", 15) == 0) {
1028             uim_debug("RECIEVE COMMAND CHG SEQ REQ_NAV");
1029
1030             enable_navi = TRUE;
1031         }
1032         else if (strncmp((char *) data, "CHG SEQ END_NAV", 15) == 0) {
1033             uim_debug("RECIEVE COMMAND CHG SEQ END_NAV");
1034
1035             enable_navi = FALSE;
1036         }
1037         else if (strncmp((char *) data, "ERR", 3) == 0) {
1038         }
1039         break;
1040     case LWS_CALLBACK_CLIENT_RECEIVE:
1041         memset(data, 0, sizeof(data));
1042         strncpy(data, (char *) in, len);
1043         uim_debug("CLIENT_RECIEVE[%d] %s", len, data);
1044         break;
1045     case LWS_CALLBACK_CLOSED:
1046         uim_debug("RECIEVE REASON LWS_CALLBACK_CLOSED");
1047         connected = 0;
1048         break;
1049     default:
1050         uim_debug("REASON %d", reason);
1051         break;
1052     }
1053
1054     return 0;
1055 }
1056
1057 /*--------------------------------------------------------------------------*/
1058 /*
1059  * @brief   parse_elementary_value
1060  *          The message content received from DBus is acquired. 
1061  *
1062  * @param[out]   vic_val_p    Vehicles information data
1063  * @param[in]    iter         message received from DBus
1064  * @return       result
1065  * @retval       TRUE     success
1066  * @retval       FALSE    error
1067  */
1068 /*--------------------------------------------------------------------------*/
1069 static bool
1070 parse_elementary_value(union VicVal_t *vic_val_p, DBusMessageIter *iter)
1071 {
1072     dbus_int32_t i32_val;
1073     dbus_int32_t i16_val;
1074     dbus_uint32_t u32_val;
1075     dbus_uint16_t u16_val;
1076     uint8_t byte_val;
1077     dbus_bool_t b_val;
1078     double d_val;
1079     char *s_val;
1080     char *w_s_val;
1081
1082     char sig;
1083
1084     if (vic_val_p == NULL || iter == NULL) {
1085         uim_debug("Err Parameter NG ");
1086         return FALSE;
1087     }
1088
1089     sig = dbus_message_iter_get_arg_type(iter);
1090
1091     switch (sig) {
1092     case DBUS_TYPE_INT32:
1093         dbus_message_iter_get_basic(iter, &i32_val);
1094         vic_val_p->i32_val = i32_val;
1095         vic_val_p++;
1096         break;
1097     case DBUS_TYPE_INT16:
1098         dbus_message_iter_get_basic(iter, &i16_val);
1099         vic_val_p->i16_val = i16_val;
1100         vic_val_p++;
1101         break;
1102     case DBUS_TYPE_UINT32:
1103         dbus_message_iter_get_basic(iter, &u32_val);
1104         vic_val_p->u32_val = u32_val;
1105         vic_val_p++;
1106         break;
1107     case DBUS_TYPE_UINT16:
1108         dbus_message_iter_get_basic(iter, &u16_val);
1109         vic_val_p->u16_val = u16_val;
1110         vic_val_p++;
1111         break;
1112     case DBUS_TYPE_BOOLEAN:
1113         dbus_message_iter_get_basic(iter, &b_val);
1114         vic_val_p->b_val = b_val;
1115         vic_val_p++;
1116         break;
1117     case DBUS_TYPE_BYTE:
1118         dbus_message_iter_get_basic(iter, &byte_val);
1119         vic_val_p->byte_val = byte_val;
1120         vic_val_p++;
1121         break;
1122     case DBUS_TYPE_DOUBLE:
1123         dbus_message_iter_get_basic(iter, &d_val);
1124         vic_val_p->d_val = d_val;
1125         vic_val_p++;
1126         break;
1127     case DBUS_TYPE_STRING:
1128 #if 0
1129         dbus_message_iter_get_basic(iter, &s_val);
1130         w_s_val = (char *) malloc(strlen(s_val) + 1);
1131         if (w_s_val == NULL) {
1132             uim_debug("Err malloc");
1133             return FALSE;
1134         }
1135         strncpy(w_s_val, s_val, strlen(s_val));
1136         vic_val_p->s_val = w_s_val;
1137         vic_val_p++;
1138         break;
1139 #endif
1140     default:
1141         uim_debug("Err parse_elementary_value: unknown type");
1142         return FALSE;
1143     }
1144
1145     return TRUE;
1146 }
1147
1148 /*--------------------------------------------------------------------------*/
1149 /*
1150  * @brief   parse_value
1151  *          parse message received from DBus
1152  *
1153  * @param[out]   vic_val_p    Vehicles information data
1154  * @param[in]    iter         message received from DBus
1155  * @return       result
1156  * @retval       TRUE     success
1157  * @retval       FALSE    error
1158  */
1159 /*--------------------------------------------------------------------------*/
1160 static bool parse_value(union VicVal_t *vic_val_p, DBusMessageIter *iter)
1161 {
1162     char curr;
1163
1164     if (vic_val_p == NULL || iter == NULL) {
1165         uim_debug("Err Parameter NG ");
1166         return FALSE;
1167     }
1168
1169     curr = dbus_message_iter_get_arg_type(iter);
1170
1171     switch (curr) {
1172     case DBUS_TYPE_BYTE:
1173     case DBUS_TYPE_BOOLEAN:
1174     case DBUS_TYPE_INT16:
1175     case DBUS_TYPE_INT32:
1176     case DBUS_TYPE_UINT16:
1177     case DBUS_TYPE_UINT32:
1178     case DBUS_TYPE_DOUBLE:
1179     case DBUS_TYPE_STRING:
1180         return parse_elementary_value(vic_val_p, iter);
1181     case DBUS_TYPE_ARRAY:
1182     case DBUS_TYPE_STRUCT:
1183     case DBUS_TYPE_DICT_ENTRY:
1184         return FALSE;
1185     case DBUS_TYPE_INVALID:
1186         return TRUE;
1187     default:
1188         break;
1189     }
1190     return FALSE;
1191 }
1192
1193 /*--------------------------------------------------------------------------*/
1194 /*
1195  * @brief   getAmbDbus
1196  *          Vehicle information is acquired from AMB.(D-Bus)
1197  *
1198  * @param[in]    key          vic_data index
1199  * @param[out]   vic_val_p    Vehicles information data
1200  * @return       result
1201  * @retval       = 0      success
1202  * @retval       =-1      error
1203  */
1204 /*--------------------------------------------------------------------------*/
1205 static int getAmbDbus(int key, union VicVal_t *vic_val_p)
1206 {
1207     /* local variable */
1208     DBusMessage *message;
1209     DBusError error;
1210     int result = 0;
1211     const char *v_string[] = { vic_data[key].interface_name,
1212         vic_data[key].property_name
1213     };
1214     const char *dest = Bus_name;
1215     DBusMessage *reply;
1216     int reply_timeout = 1000;   /* Millisecond */
1217     DBusMessageIter iter;
1218     DBusMessageIter iter_array;
1219     union VicVal_t *tmp_vic_val_p = vic_val_p;
1220
1221     /* initialize */
1222     dbus_error_init(&error);
1223
1224     if (NULL == g_connection) {
1225         /* obtain the right to use dbus */
1226         g_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1227
1228         if (g_connection == NULL) {
1229             uim_debug("Err dbus_bus_get");
1230
1231             /* Release err parameter */
1232             dbus_error_free(&error);
1233             return -1;
1234         }
1235     }
1236
1237     /* Constructs a new message */
1238     message = dbus_message_new_method_call(DBUS_SERVICE,
1239                                            vic_data[key].path_name,
1240                                            DBUS_INTERFACE, DBUS_METHOD);
1241     if (message == NULL) {
1242         uim_debug("Err dbus_message_new_method_call");
1243
1244         /* Release the connection */
1245         dbus_connection_unref(g_connection);
1246         g_connection = NULL;
1247         return -1;
1248     }
1249
1250     /* Gets the type of a message */
1251     result = dbus_message_append_args(message,
1252                                       DBUS_TYPE_STRING,
1253                                       &v_string[0],
1254                                       DBUS_TYPE_STRING,
1255                                       &v_string[1], DBUS_TYPE_INVALID);
1256
1257     if (!result) {
1258         uim_debug("Err dbus_message_append_args");
1259
1260         /* Release the connection */
1261         dbus_connection_unref(g_connection);
1262         g_connection = NULL;
1263
1264         /* Release the message */
1265         dbus_message_unref(message);
1266
1267         return -1;
1268     }
1269
1270     /* Gets the error name */
1271     if (dest && !dbus_message_set_destination(message, dest)) {
1272         uim_debug("Err dbus_message_new_method_call");
1273
1274         /* Release the connection */
1275         dbus_connection_unref(g_connection);
1276         g_connection = NULL;
1277
1278         /* Release the message */
1279         dbus_message_unref(message);
1280
1281         return -1;
1282     }
1283
1284     /* Queues a message to send */
1285     reply = dbus_connection_send_with_reply_and_block(g_connection,
1286                                                       message,
1287                                                       reply_timeout, &error);
1288     if (reply == NULL) {
1289         uim_debug( "Err dbus_connection_send_with_reply_and_block" );
1290
1291         /* Release the connection */
1292         dbus_connection_unref(g_connection);
1293         g_connection = NULL;
1294
1295         /* Release the message */
1296         dbus_message_unref(message);
1297
1298         /* Release err parameter */
1299         dbus_error_free(&error);
1300
1301         return -1;
1302     }
1303
1304     /* Gets the result */
1305     dbus_message_iter_init(reply, &iter);
1306     dbus_message_iter_recurse(&iter, &iter_array);
1307
1308     /* Type conversion of the resulting value */
1309     result = parse_value(tmp_vic_val_p, &iter_array);
1310     if (result != TRUE) {
1311         uim_debug("Err parse_elementary_value");
1312
1313         /* Release the connection */
1314         dbus_connection_unref(g_connection);
1315         g_connection = NULL;
1316
1317         /* Release the message */
1318         dbus_message_unref(message);
1319         dbus_message_unref(reply);
1320
1321         return -1;
1322     }
1323
1324     /* Release the message */
1325     dbus_message_unref(message);
1326     dbus_message_unref(reply);
1327
1328     return 0;
1329 }
1330
1331 /*--------------------------------------------------------------------------*/
1332 /*
1333  * @brief   getLocationFromAmb
1334  *          Location information acquisition from AMB.
1335  *
1336  * @param    none
1337  * @return   none
1338  */
1339 /*--------------------------------------------------------------------------*/
1340 static void getLocationFromAmb()
1341 {
1342     union VicVal_t vic_val[32];
1343     int result = 0;
1344     int i;
1345
1346     for (i = 0; i < property_num; i++) {
1347         result = getAmbDbus(i, vic_val);
1348
1349         if (result != 0) {
1350             uim_debug("Err getAmbDbus : Unacquired [%s]",vic_data[i].name);
1351             continue;
1352         }
1353
1354         switch (vic_data[i].property) {
1355         case LATITUDE:
1356             if ((camera_geocode.lat != vic_val[0].d_val) &&
1357                 (vic_val[0].d_val != 0)) {
1358                 uim_debug("%s(%f)", vic_data[i].name, vic_val[0].d_val);
1359                 camera_geocode.lat = vic_val[0].d_val;
1360             }
1361             break;
1362         case LONGITUDE:
1363             if ((camera_geocode.lon != vic_val[0].d_val) &&
1364                 (vic_val[0].d_val != 0)) {
1365                 uim_debug("%s(%f)", vic_data[i].name, vic_val[0].d_val);
1366                 camera_geocode.lon = vic_val[0].d_val;
1367             }
1368             break;
1369         case DIRECTION:
1370             if (camera_geocode.dir != vic_val[0].i32_val) {
1371                 uim_debug("%s(%d)", vic_data[i].name, vic_val[0].i32_val);
1372                 camera_geocode.dir = vic_val[0].i32_val;
1373             }
1374             break;
1375         case VEHICLESPEED:
1376             if (ui_vicspeed != vic_val[0].i32_val) {
1377                 uim_debug("%s(%d)", vic_data[i].name, vic_val[0].i32_val);
1378                 ui_vicspeed = vic_val[0].i32_val;
1379             }
1380             break;
1381         default:
1382             uim_debug("ERROR no property : %s", vic_data[i].name);
1383             break;
1384         }
1385     }
1386     return;
1387 }
1388
1389 /*--------------------------------------------------------------------------*/
1390 /*
1391  * @brief   get_config
1392  *          The config file for vehicle information acquisition is read.
1393  *
1394  * @param    none
1395  * @return   result
1396  * @retval   = 0      success
1397  * @retval   =-1      error
1398  */
1399 /*--------------------------------------------------------------------------*/
1400 static int get_config()
1401 {
1402     FILE *fp;
1403     int k = 0;
1404     int j, m;
1405     char buff[512];
1406     char *tp;
1407     char *clm = " \n";
1408
1409     fp = fopen(CONFIG_FILE, "r");
1410     if (fp == NULL) {
1411         uim_debug("File open error");
1412         return -1;
1413     }
1414
1415     for (m = 0; k < MAX_PARA_NUM; m++) {
1416         if (fgets(buff, sizeof(buff) - 2, fp) != NULL) {
1417             tp = strtok(buff, clm);
1418             if (tp != NULL) {
1419                 if (tp[0] != '#') {
1420                     for (j = 0; vic_key_data[j].id != -1; j++) {
1421                         if (strcmp(tp, vic_key_data[j].name) == 0) {
1422                             vic_data[k].property = vic_key_data[j].id;
1423                             strcpy(vic_data[k].name, tp);
1424                             strcpy(vic_data[k].path_name, strtok(NULL, clm));
1425                             strcpy(vic_data[k].interface_name,
1426                                    strtok(NULL, clm));
1427                             strcpy(vic_data[k].property_name,
1428                                    strtok(NULL, clm));
1429
1430                             uim_debug("vic_data[%d].property=%d", k,
1431                                       vic_data[k].property);
1432                             uim_debug("vic_data[%d].name=%s", k,
1433                                       vic_data[k].name);
1434                             uim_debug("vic_data[%d].path_name=%s", k,
1435                                       vic_data[k].path_name);
1436                             uim_debug("vic_data[%d].interface_name=%s", k,
1437                                       vic_data[k].interface_name);
1438                             uim_debug("vic_data[%d].property_name=%s", k,
1439                                       vic_data[k].property_name);
1440                             k++;
1441                             break;
1442                         }
1443                     }
1444                     if (vic_key_data[j].id == -1) {
1445                         uim_debug
1446                             ("Err config.txt Line:%d Unregistered parameter name",
1447                              m + 1);
1448                     }
1449
1450                 }
1451                 else {
1452                     uim_debug("config.txt Line:%d Comment out  '#'Discovery",
1453                               m + 1);
1454                 }
1455             }
1456             else {
1457                 uim_debug("config.txt Line:%d Comment out  Null line", m + 1);
1458             }
1459         }
1460         else {
1461             uim_debug("config.txt The end of data reading");
1462             break;
1463         }
1464     }
1465     fclose(fp);
1466
1467     property_num = k;
1468     if (property_num == 0) {
1469         uim_debug("config.txt No valid data");
1470         return -1;
1471     }
1472
1473     return 0;
1474 }
1475
1476 /*--------------------------------------------------------------------------*/
1477 /*
1478  * @brief   edje_parse_str
1479  *          A character string is decomposed by a delimiter.
1480  *
1481  * @param[in]    key          vic_data index
1482  * @param[in]    vic_val_p    Vehicles information data
1483  * @return       The pointer address to a lexical token
1484  * @retval       > 0      success
1485  * @retval       NULL     error
1486  */
1487 /*--------------------------------------------------------------------------*/
1488 static char *edje_parse_str(void *in, int arg_num)
1489 {
1490     int i;
1491     char *data;
1492
1493     uim_debug("edje_parse_str %s, arg = %d", in, arg_num);
1494     data = strtok(in, " ");
1495     /* arg_num : 0 to n */
1496     for (i = 0; i < arg_num; i++) {
1497         data = strtok(NULL, " ");
1498     }
1499     uim_debug("edje_parse_str data: %s", data);
1500     return data;
1501 }
1502
1503 /*--------------------------------------------------------------------------*/
1504 /**
1505  * @brief   event_message
1506  *          send message
1507  *
1508  * @param[in]   wsi                 libwebsockets management table to send
1509  * @param[in]   fromat              message to send
1510  * @return      none
1511  */
1512 /*--------------------------------------------------------------------------*/
1513 static void event_message(struct libwebsocket *wsi, char *format, ...)
1514 {
1515     va_list list;
1516     char message[256];
1517
1518     va_start(list, format);
1519     vsnprintf(message, sizeof(message), format, list);
1520     va_end(list);
1521
1522     uim_debug("Setting screen: event_message wsi = %p, %s", wsi, message);
1523     if (wsi) {
1524         int n = 0;
1525         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
1526                           LWS_SEND_BUFFER_POST_PADDING];
1527         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
1528
1529         n = sprintf((char *) p, "%s", message);
1530         n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
1531         uim_debug("Setting screen: libwebsocket_write return = %d", n);
1532     }
1533     else {
1534         uim_debug("Setting screen: wsi is not initialized");
1535     }
1536
1537     return;
1538 }
1539
1540 /*--------------------------------------------------------------------------*/
1541 /**
1542  * @brief   _touch_up_edje
1543  *          Touch-up event processing.
1544  *
1545  * @param[in]   data                user data
1546  * @param[in]   obj                 evas object of the button
1547  * @param[in]   event_info          evas event infomation
1548  * @return      none
1549  */
1550 /*--------------------------------------------------------------------------*/
1551 static void
1552 _touch_up_edje(void *data, Evas *evas, Evas_Object *obj, void *event_info)
1553 {
1554     /* get name from userdata */
1555     if (data != NULL) {
1556         uim_debug("Setting screen: user data is %s", (const char *) data);
1557
1558         if (socket_val) {
1559             event_message(socket_val, "TOUCH %s %s", edje_str, data);
1560             uim_debug("SEND TOUCH %s ", data);
1561         }
1562     }
1563     else {
1564         uim_debug("Setting screen: user data is NULL");
1565     }
1566 }
1567
1568 /*--------------------------------------------------------------------------*/
1569 /**
1570  * @brief   loading_edje_file
1571  *          Loading of edje file.
1572  *
1573  * @param[in]   edje_file    edje file
1574  * @return      result
1575  * @retval      = 0      success
1576  * @retval      =-1      error
1577  */
1578 /*--------------------------------------------------------------------------*/
1579 static int loading_edje_file(const char *edje_file)
1580 {
1581     Evas_Object *part;          /* part handle */
1582     Eina_List *group;           /* edje group list */
1583     Eina_List *list;            /* part list in edje */
1584     int group_count = 0;        /* group counter */
1585     int name_count = 0;         /* name counter */
1586
1587     g_evas = ecore_evas_get(window);
1588     if (!g_evas) {
1589         uim_debug("Setting screen: could not create evas.");
1590         return -1;
1591     }
1592
1593     /* delete edge */
1594     evas_object_del(g_edje);
1595
1596     /* create and add object in canvas from edje */
1597     g_edje = edje_object_add(g_evas);
1598     if (!g_edje) {
1599         uim_debug("Setting screen: could not create edje object!");
1600         return -1;
1601     }
1602
1603     /* get group list */
1604     group = edje_file_collection_list(edje_file);
1605     while (group != NULL) {
1606         /* Set the edj file */
1607         if (!edje_object_file_set
1608             (g_edje, edje_file, (const char *) group->data)) {
1609             int err = edje_object_load_error_get(g_edje);
1610             const char *errmsg = edje_load_error_str(err);
1611             uim_debug("Setting screen: could not load %s: %s", edje_file,
1612                       errmsg);
1613
1614             edje_file_collection_list_free(group);
1615             evas_object_del(g_edje);
1616             return -1;
1617         }
1618         uim_debug("Setting screen: group[%d] data : %s", group_count,
1619                   (const char *) group->data);
1620
1621         /* get list */
1622         list = edje_object_access_part_list_get(g_edje);
1623         while (list != NULL) {
1624             uim_debug("Setting screen: list[%d] data : %s", name_count,
1625                       (const char *) list->data);
1626
1627             /* set callback for part name */
1628             part =
1629                 edje_object_part_object_get((const Evas_Object *) g_edje,
1630                                             (const char *) list->data);
1631             if (part != NULL) {
1632                 uim_debug("Setting screen: list[%d] name : %s", name_count,
1633                           (const char *) list->data);
1634                 evas_object_event_callback_add(part, EVAS_CALLBACK_MOUSE_UP,
1635                                                _touch_up_edje, list->data);
1636             }
1637             else {
1638                 uim_debug("Setting screen: list[%d] is NULL", name_count);
1639             }
1640
1641             /* to next list */
1642             list = list->next;
1643             name_count++;
1644         }
1645
1646         /* to next group */
1647         group = group->next;
1648         group_count++;
1649     }
1650     uim_debug("Setting screen: group num is %d", group_count);
1651     uim_debug("Setting screen: name num is %d", name_count);
1652
1653     /* Put in the image */
1654     evas_object_move(g_edje, 0, 0);
1655     /* Resize the image */
1656     evas_object_resize(g_edje, W_WIDTH, W_HEIGHT);
1657     /* Show the image */
1658     evas_object_show(g_edje);
1659
1660     evas_object_layer_set(g_edje, LAYER_UI);
1661
1662     return 0;
1663 }
1664
1665 /*--------------------------------------------------------------------------*/
1666 /**
1667  * @brief   res_callback
1668  *          Notice event processing of a resource state.
1669  *
1670  * @param[in]   info                rsource information
1671  * @param[in]   user_data           user data(UNUSED)
1672  * @return      none
1673  */
1674 /*--------------------------------------------------------------------------*/
1675 static void
1676 res_callback(ico_apf_resource_notify_info_t *info, void *user_data)
1677 {
1678     int ret;
1679
1680     uim_debug("##==> Callbacked evt=%d res=%d id=%d bid=%d appid=%s dev=%s"
1681               " user_data=%d", info->state, info->resid, info->id, info->bid,
1682               info->appid, info->device, (int) user_data);
1683
1684     switch (info->state) {
1685     case ICO_APF_RESOURCE_STATE_ACQUIRED:
1686     case ICO_APF_RESOURCE_STATE_DEPRIVED:
1687     case ICO_APF_RESOURCE_STATE_WAITTING:
1688     case ICO_APF_RESOURCE_STATE_RELEASED:
1689         if (info->resid == ICO_APF_RESID_INT_SCREEN) {
1690             ret =
1691                 ico_apf_resource_reply_int_screen_mode(info->device,
1692                                                        info->bid, info->id,
1693                                                        1);
1694             uim_debug("##==> callback reply int_screen(%s,%d,%d,1) = %d",
1695                       info->device, info->bid, info->id, ret);
1696         }
1697         else if (info->resid == ICO_APF_RESID_ON_SCREEN) {
1698             ret =
1699                 ico_apf_resource_reply_int_screen_mode_disp(info->device,
1700                                                             info->id, 1);
1701             uim_debug("##==> callback reply on_screen(%s,%d,1) = %d",
1702                       info->device, info->id, ret);
1703         }
1704         else {
1705             ret =
1706                 ico_apf_resource_reply_screen_mode(info->device, info->id, 1);
1707             uim_debug("##==> callback reply screen(%s,%d,1) = %d",
1708                       info->device, info->id, ret);
1709         }
1710         break;
1711     default:
1712         /* NOP  */
1713         break;
1714     }
1715 }
1716
1717 /*--------------------------------------------------------------------------*/
1718 /*
1719  * @brief   sample navigation application
1720  *          main routine
1721  *
1722  * @param   main() finction's standard parameter (argc,argv)
1723  * @return  result
1724  * @retval  =0       success
1725  * @retval  =-1      failed
1726  */
1727 /*--------------------------------------------------------------------------*/
1728 int main(int argc, char *argv[])
1729 {
1730     int i;
1731     int getscreen;
1732     char appid[ICO_UXF_MAX_PROCESS_NAME + 1];
1733     int ret = 0;
1734     connected = 0;
1735
1736     /* Setting the log output */
1737     if (ico_apf_get_app_id(0, appid) == ICO_APP_CTL_E_NONE) {
1738         ico_apf_log_open(appid);
1739     }
1740
1741     load_config_file();
1742
1743     elm_init(argc, argv);
1744
1745     enable_navi = FALSE;
1746     set_route = FALSE;
1747
1748     /* initialize */
1749     if (0 != get_config()) {
1750         uim_debug("ERROR get_config()");
1751         return -1;
1752     }
1753
1754     getscreen = 0;
1755     sscrntype[0] = 0;
1756     for (i = 1; i < argc; i++) {
1757         if (argv[i][0] == '-') {
1758             if (strcasecmp(argv[i], "-basescreen") == 0) {
1759                 getscreen = 1;  /* get base screen */
1760                 strcpy(sscrntype, "BasicScreen");
1761                 uim_debug("BasicScreen");
1762             }
1763             else if (strcasecmp(argv[i], "-intscreen") == 0) {
1764                 getscreen = 2;  /* get interrupt screen */
1765                 strcpy(sscrntype, "IntScreen");
1766             }
1767             else if (strcasecmp(argv[i], "-onscreen") == 0) {
1768                 getscreen = 3;  /* get on screen */
1769                 strcpy(sscrntype, "OnScreen");
1770             }
1771         }
1772     }
1773
1774     ecore_evas_init();
1775
1776     if (getscreen > 0) {
1777         /* initialize resource control for Ecore */
1778         if (ico_apf_ecore_init(NULL) != ICO_APF_E_NONE) {
1779             uim_debug("ico_apf_ecore_init() Error");
1780             ecore_evas_shutdown();
1781             return -1;
1782         }
1783
1784         /* set resource request callback */
1785         ico_apf_resource_set_event_cb(res_callback, NULL);
1786
1787         /* acquire a right to display a screen */
1788         if (getscreen == 1) {
1789             ret = ico_apf_resource_get_screen_mode(NULL, 0);
1790         }
1791         else if (getscreen == 2) {
1792             ret = ico_apf_resource_get_int_screen_mode(NULL, 0, 0);
1793         }
1794         else {
1795             ret = ico_apf_resource_get_int_screen_mode_disp(NULL, 0);
1796         }
1797     }
1798     uim_debug("getscreen = %d, ret = %d", getscreen, ret);
1799
1800     /* window setup */
1801     window = ecore_evas_new(NULL, 0, 0, W_WIDTH, W_HEIGHT, "frame=0");
1802     if (!window)
1803         goto error;
1804
1805     ecore_evas_show(window);
1806
1807     /* Camera Layer */
1808     e_cam = ecore_evas_get(window);
1809     cam = evas_object_image_add(e_cam);
1810 #ifdef _USE_CAMERA_
1811     capture = cvCreateCameraCapture(conf_data[USB_CAMERA_ID]);
1812     if (!capture) {
1813         uim_debug("cvCaptureFromCAM failed.");
1814         return -1;
1815     }
1816
1817     captureImage = cvQueryFrame(capture);
1818     uim_debug("camera width = %d, height = %d", captureImage->width,
1819               captureImage->height);
1820     convert_data_evas_cv(cam, captureImage);
1821 #else
1822
1823 #ifdef _USE_OPENCV_
1824     evas_object_image_from_cv(cam, test_camera_path);
1825 #else /* _USE_OPENCV_ */
1826     evas_object_image_file_set(cam, test_camera_path, NULL);
1827 #endif /* _USE_OPENCV_ */
1828
1829 #endif
1830
1831     evas_object_image_fill_set(cam, 0, 0, W_NAVI_WIDTH, W_NAVI_HEIGHT);
1832     evas_object_move(cam, W_NAVI_ORIGINE_X, W_NAVI_ORIGINE_Y);
1833     evas_object_resize(cam, W_NAVI_WIDTH, W_NAVI_HEIGHT);
1834     evas_object_show(cam);
1835
1836     /* 3D Layer */
1837     e_3d = ecore_evas_get(window);
1838
1839     /* UI Layer */
1840     e_ui = ecore_evas_get(window);
1841     e_ui_init();
1842
1843     /* Server */
1844     context =
1845         libwebsocket_create_context(port, NULL, protocols,
1846                                     libwebsocket_internal_extensions, NULL,
1847                                     NULL, -1, -1, 0);
1848     if (context == NULL) {
1849         fprintf(stderr, "libwebsocket_create_context failed.");
1850         goto error;
1851     }
1852
1853     connected = 1;
1854     ecore_timer_add(0.05, callback_listener, context);
1855
1856     /* Browser(Map) Layer */
1857     e_map_init();
1858
1859     edje_init();
1860
1861     /* timer */
1862     ecore_timer_add(TIME_INTERVAL_AR, _time_interval_navi_cb, cam);
1863
1864     ecore_timer_add(TEST_TIME, _time_interval_map_renew_cb, NULL);
1865
1866     /* main loop */
1867     ecore_main_loop_begin();
1868
1869     /* cleanup */
1870     e3d_cleanup();
1871
1872 #ifdef _USE_CAMERA_
1873     cvReleaseCapture(&capture);
1874 #endif
1875
1876     if (NULL != g_connection) {
1877         dbus_connection_unref(g_connection);
1878         g_connection = NULL;
1879     }
1880
1881     ico_apf_ecore_term();
1882
1883     ecore_evas_free(window);
1884     ecore_evas_shutdown();
1885
1886     return 0;
1887
1888   error:
1889     fprintf(stderr, "Evas engine error.");
1890     ecore_evas_shutdown();
1891     return -1;
1892 }