tizen beta release
[framework/location/libslp-lbs-plugin-replay.git] / replay-plugin / src / gps_plugin_replay.c
1 /*
2  * GPS manager replay plugin
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <errno.h>
27 #include <sys/time.h>
28
29 #include <gps_manager_plugin_intf.h>
30
31 #include "gps_plugin_debug.h"
32 #include "nmea_parser.h"
33 #include "setting.h"
34
35 #define REPLAY_NMEA_SET_SIZE            4096
36 #define REPLAY_NMEA_SENTENCE_SIZE       128
37
38 gps_event_cb g_gps_event_cb = NULL;
39
40 typedef struct {
41         FILE *fd;
42         int interval;
43         int replay_mode;
44
45         pos_data_t *pos_data;
46         sv_data_t *sv_data;
47         nmea_data_t *nmea_data;
48
49         GSource *timeout_src;
50         GMainContext *default_context;
51 } replay_timeout;
52
53 replay_timeout *g_replay_timer = NULL;
54
55 int gps_plugin_replay_gps_init(gps_event_cb gps_event_cb, gps_server_param_t * gps_params);
56 int gps_plugin_replay_gps_deinit(gps_failure_reason_t * reason_code);
57 int gps_plugin_replay_gps_request(gps_action_t gps_action, void *data, gps_failure_reason_t * reason_code);
58
59 static const gps_plugin_interface g_gps_plugin_replay_interface = {
60         gps_plugin_replay_gps_init,
61         gps_plugin_replay_gps_deinit,
62         gps_plugin_replay_gps_request
63 };
64
65 void gps_plugin_replay_pos_event(pos_data_t * data)
66 {
67         gps_event_info_t gps_event;
68         time_t timestamp;
69
70         memset(&gps_event, 0, sizeof(gps_event_info_t));
71         time(&timestamp);
72
73         gps_event.event_id = GPS_EVENT_REPORT_POSITION;
74
75         if (data == NULL) {
76                 LOG_PLUGIN(DBG_ERR, "NULL POS data.");
77                 gps_event.event_data.pos_ind.error = GPS_ERR_COMMUNICATION;
78         } else {
79                 gps_event.event_data.pos_ind.error = GPS_ERR_NONE;
80                 gps_event.event_data.pos_ind.pos.timestamp = timestamp;
81                 gps_event.event_data.pos_ind.pos.latitude = data->latitude;
82                 gps_event.event_data.pos_ind.pos.longitude = data->longitude;
83                 gps_event.event_data.pos_ind.pos.altitude = data->altitude;
84                 gps_event.event_data.pos_ind.pos.speed = data->speed;
85                 gps_event.event_data.pos_ind.pos.bearing = data->bearing;
86                 gps_event.event_data.pos_ind.pos.hor_accuracy = data->hor_accuracy;
87                 gps_event.event_data.pos_ind.pos.ver_accuracy = data->ver_accuracy;
88         }
89
90         //LOG_PLUGIN(DBG_LOW, "%d / %f / %f / %f", gps_event.event_data.pos_ind.pos.timestamp, gps_event.event_data.pos_ind.pos.latitude, gps_event.event_data.pos_ind.pos.longitude, gps_event.event_data.pos_ind.pos.altitude);
91
92         if (g_gps_event_cb != NULL) {
93                 g_gps_event_cb(&gps_event);
94         }
95 }
96
97 void gps_plugin_replay_sv_event(sv_data_t * data)
98 {
99         int i;
100         gps_event_info_t gps_event;
101         time_t timestamp;
102
103         memset(&gps_event, 0, sizeof(gps_event_info_t));
104         time(&timestamp);
105         gps_event.event_id = GPS_EVENT_REPORT_SATELLITE;
106
107         if (data == NULL) {
108                 LOG_PLUGIN(DBG_ERR, "NULL SV data.");
109                 gps_event.event_data.sv_ind.error = GPS_ERR_COMMUNICATION;
110         } else {
111                 gps_event.event_data.sv_ind.error = GPS_ERR_NONE;
112                 gps_event.event_data.sv_ind.sv.timestamp = timestamp;
113                 gps_event.event_data.sv_ind.sv.pos_valid = data->pos_valid;
114                 gps_event.event_data.sv_ind.sv.num_of_sat = data->num_of_sat;
115                 for (i = 0; i < data->num_of_sat; i++) {
116                         gps_event.event_data.sv_ind.sv.sat[i].used = data->sat[i].used;
117                         gps_event.event_data.sv_ind.sv.sat[i].prn = data->sat[i].prn;
118                         gps_event.event_data.sv_ind.sv.sat[i].snr = data->sat[i].snr;
119                         gps_event.event_data.sv_ind.sv.sat[i].elevation = data->sat[i].elevation;
120                         gps_event.event_data.sv_ind.sv.sat[i].azimuth = data->sat[i].azimuth;
121                 }
122         }
123
124         if (g_gps_event_cb != NULL) {
125                 g_gps_event_cb(&gps_event);
126         }
127 }
128
129 void gps_plugin_replay_nmea_event(nmea_data_t * data)
130 {
131         gps_event_info_t gps_event;
132         time_t timestamp;
133
134         memset(&gps_event, 0, sizeof(gps_event_info_t));
135         time(&timestamp);
136
137         gps_event.event_id = GPS_EVENT_REPORT_NMEA;
138
139         if (data == NULL) {
140                 LOG_PLUGIN(DBG_ERR, "NULL NMEA data.");
141                 gps_event.event_data.nmea_ind.error = GPS_ERR_COMMUNICATION;
142         } else {
143                 if (data->len > REPLAY_NMEA_SENTENCE_SIZE) {
144                         LOG_PLUGIN(DBG_WARN, "The Size of NMEA[ %d ] is larger then max ", data->len);
145                         data->len = REPLAY_NMEA_SENTENCE_SIZE;
146                         gps_event.event_data.nmea_ind.error = GPS_ERR_COMMUNICATION;
147                 } else {
148                         gps_event.event_data.nmea_ind.error = GPS_ERR_NONE;
149                 }
150                 gps_event.event_data.nmea_ind.nmea.timestamp = timestamp;
151                 gps_event.event_data.nmea_ind.nmea.len = data->len;
152                 gps_event.event_data.nmea_ind.nmea.data = (char *)malloc(data->len);
153                 memset(gps_event.event_data.nmea_ind.nmea.data, 0x00, data->len);
154                 memcpy(gps_event.event_data.nmea_ind.nmea.data, data->data, data->len);
155                 //LOG_PLUGIN(DBG_LOW, "NMEA[%d] : %s", gps_event.event_data.nmea_ind.nmea.len, gps_event.event_data.nmea_ind.nmea.data);
156         }
157
158         if (g_gps_event_cb != NULL) {
159                 g_gps_event_cb(&gps_event);
160         }
161
162         if (gps_event.event_data.nmea_ind.nmea.data != NULL) {
163                 free(gps_event.event_data.nmea_ind.nmea.data);
164                 gps_event.event_data.nmea_ind.nmea.data = NULL;
165         }
166 }
167
168 void gps_plugin_respond_start_session(gboolean ret)
169 {
170         gps_event_info_t gps_event;
171         gps_event.event_id = GPS_EVENT_START_SESSION;
172
173         if (ret == TRUE) {
174                 gps_event.event_data.start_session_rsp.error = GPS_ERR_NONE;
175         } else {
176                 gps_event.event_data.start_session_rsp.error = GPS_ERR_COMMUNICATION;
177         }
178
179         if (g_gps_event_cb != NULL) {
180                 g_gps_event_cb(&gps_event);
181         }
182 }
183
184 void gps_plugin_respond_stop_session(void)
185 {
186         gps_event_info_t gps_event;
187
188         gps_event.event_id = GPS_EVENT_STOP_SESSION;
189         gps_event.event_data.stop_session_rsp.error = GPS_ERR_NONE;
190
191         if (g_gps_event_cb != NULL) {
192                 g_gps_event_cb(&gps_event);
193         }
194 }
195
196 gboolean gps_plugin_replay_read_nmea(replay_timeout * timer, char *nmea_data)
197 {
198         gboolean ret = FALSE;
199         int ref = 0;
200         char buf[REPLAY_NMEA_SENTENCE_SIZE] = { 0, };
201
202         if (nmea_data == NULL) {
203                 LOG_PLUGIN(DBG_ERR, "nmea_data is NULL");
204                 ret = FALSE;
205         }
206
207         while (fgets(buf, REPLAY_NMEA_SENTENCE_SIZE, timer->fd) != NULL) {
208                 if (strncmp(buf, "$GPGGA", 6) == 0) {
209                         ref++;
210                         if (ref > 1) {
211                                 fseek(timer->fd, -strlen(buf), SEEK_CUR);
212                                 LOG_PLUGIN(DBG_LOW, "2nd GPGGA : stop to read nmea data");
213                                 ret = TRUE;
214                                 break;
215                         } else if (ref == 1) {
216                                 LOG_PLUGIN(DBG_LOW, "1st GPGGA : start to read nmea data");
217                                 strncpy(nmea_data, buf, strlen(buf));
218                         }
219                 } else {
220                         if (strlen(nmea_data) + strlen(buf) > REPLAY_NMEA_SET_SIZE) {
221                                 LOG_PLUGIN(DBG_ERR, "read nmea data size is too long");
222                                 break;
223                         } else {
224                                 strncat(nmea_data, buf, strlen(buf));
225                         }
226                 }
227                 timer->nmea_data->len = strlen(buf);
228                 timer->nmea_data->data = buf;
229                 gps_plugin_replay_nmea_event(timer->nmea_data);
230         }
231
232         if (feof(timer->fd)) {
233                 LOG_PLUGIN(DBG_ERR, "end of file");
234                 rewind(timer->fd);
235         } else {
236                 LOG_PLUGIN(DBG_LOW, "read nmea data [%s]", nmea_data);
237         }
238         return ret;
239 }
240
241 gboolean gps_plugin_replay_read_manual(pos_data_t * pos_data)
242 {
243         gboolean ret = TRUE;
244
245         if (setting_get_double(MANUAL_LATITUDE, &pos_data->latitude) == FALSE) {
246                 LOG_PLUGIN(DBG_ERR, "Fail to get latitude");
247                 ret = FALSE;
248         }
249         if (setting_get_double(MANUAL_LONGITUDE, &pos_data->longitude) == FALSE) {
250                 LOG_PLUGIN(DBG_ERR, "Fail to get longitude");
251                 ret = FALSE;
252         }
253         if (setting_get_double(MANUAL_ALTITUDE, &pos_data->altitude) == FALSE) {
254                 LOG_PLUGIN(DBG_ERR, "Fail to get altitude");
255                 ret = FALSE;
256         }
257
258         return ret;
259 }
260
261 gboolean gps_plugin_replay_timeout_cb(gpointer data)
262 {
263         gboolean ret = FALSE;
264         read_error_t err = READ_SUCCESS;
265         replay_timeout *timer = (replay_timeout *) data;
266         char nmea_data[REPLAY_NMEA_SET_SIZE] = { 0, };
267
268         memset(timer->pos_data, 0, sizeof(pos_data_t));
269         memset(timer->sv_data, 0, sizeof(sv_data_t));
270
271         if (timer->replay_mode == REPLAY_NMEA) {
272                 if (gps_plugin_replay_read_nmea(timer, nmea_data) == FALSE) {
273                         LOG_PLUGIN(DBG_ERR, "Fail to read nmea data from file");
274                         return FALSE;
275                 } else {
276                         err = nmea_parser(nmea_data, timer->pos_data, timer->sv_data);
277                         if (err == READ_ERROR) {
278                                 LOG_PLUGIN(DBG_ERR, "Fail to parser nmea data from file");
279                                 return FALSE;
280                         } else if (err == READ_NOT_FIXED) {
281                                 LOG_PLUGIN(DBG_LOW, "GPS position is not fixed");
282                                 timer->sv_data->pos_valid = FALSE;
283                         }
284                 }
285         } else if (timer->replay_mode == REPLAY_MANUAL) {
286                 if (gps_plugin_replay_read_manual(timer->pos_data) == FALSE) {
287                         LOG_PLUGIN(DBG_ERR, "Fail to read manual data");
288                         err = READ_ERROR;
289                         return FALSE;
290                 } else {
291                         timer->sv_data->pos_valid = TRUE;
292                         err = READ_SUCCESS;
293                 }
294         } else if (timer->replay_mode == REPLAY_OFF) {
295                 LOG_PLUGIN(DBG_WARN, "replay_mode is OFF");
296                 err = READ_NOT_FIXED;
297                 timer->sv_data->pos_valid = FALSE;
298         }
299
300         if (timer != NULL) {
301                 if (g_gps_event_cb != NULL) {
302                         if (err != READ_NOT_FIXED) {
303                                 gps_plugin_replay_pos_event(timer->pos_data);
304                         }
305                         gps_plugin_replay_sv_event(timer->sv_data);
306                 }
307                 ret = TRUE;
308         }
309         return ret;
310 }
311
312 void gps_plugin_stop_replay_mode(replay_timeout * timer)
313 {
314         if (timer->replay_mode == REPLAY_NMEA && fclose(timer->fd) != 0) {
315                 LOG_PLUGIN(DBG_ERR, "fclose failed");
316         }
317
318         if (timer->timeout_src != NULL && timer->default_context != NULL && !g_source_is_destroyed(timer->timeout_src)) {
319                 if (timer->default_context == g_source_get_context(timer->timeout_src)) {
320                         g_source_destroy(timer->timeout_src);
321                         LOG_PLUGIN(DBG_LOW, "g_source_destroy timeout_src");
322                 } else {
323                         LOG_PLUGIN(DBG_WARN, "timer->timeout_src is attatched to 0x%x (actual 0x%x)",
324                                    g_source_get_context(timer->timeout_src), timer->default_context);
325                 }
326                 timer->timeout_src = NULL;
327                 timer->default_context = NULL;
328         } else {
329                 LOG_PLUGIN(DBG_WARN, "timeout_src or default_context is NULL or timeout_src is already destroyed");
330         }
331         gps_plugin_respond_stop_session();
332 }
333
334 gboolean gps_plugin_start_replay_mode(replay_timeout * timer)
335 {
336         gboolean ret = FALSE;
337         char replay_file_path[256];
338
339         if (timer->replay_mode == REPLAY_NMEA) {
340                 snprintf(replay_file_path, sizeof(replay_file_path), NMEA_FILE_PATH "%s", setting_get_string(NMEA_FILE_NAME));
341                 LOG_PLUGIN(DBG_ERR, "replay file name : %s", replay_file_path);
342
343                 timer->fd = fopen(replay_file_path, "r");
344                 if (timer->fd == NULL) {
345                         LOG_PLUGIN(DBG_ERR, "fopen(%s) failed", replay_file_path);
346                         return FALSE;
347                 }
348         }
349
350         if (timer->default_context == NULL) {
351                 timer->default_context = g_main_context_default();
352                 if (timer->default_context == NULL) {
353                         return ret;
354                 }
355         }
356
357         if (timer->timeout_src != NULL) {
358                 LOG_PLUGIN(DBG_ERR, "timeout_src is already existed");
359                 ret = FALSE;
360         } else {
361                 timer->timeout_src = g_timeout_source_new_seconds(timer->interval);
362                 if (timer->timeout_src != NULL) {
363                         g_source_set_callback(timer->timeout_src, &gps_plugin_replay_timeout_cb, timer, NULL);
364                         if (g_source_attach(timer->timeout_src, timer->default_context) > 0) {
365                                 LOG_PLUGIN(DBG_LOW, "timeout_src(0x%x) is created & attatched to 0x%x", timer->timeout_src,
366                                            timer->default_context);
367                                 ret = TRUE;
368                         } else {
369                                 gps_plugin_stop_replay_mode(timer);
370                                 ret = FALSE;
371                         }
372                 }
373         }
374         gps_plugin_respond_start_session(ret);
375
376         return ret;
377 }
378
379 static void replay_mode_changed_cb(keynode_t * key, void *data)
380 {
381         if (setting_get_int(REPLAY_MODE, &g_replay_timer->replay_mode) == FALSE) {
382                 g_replay_timer->replay_mode = REPLAY_OFF;
383         }
384         return;
385 }
386
387 replay_timeout *gps_plugin_replay_timer_init()
388 {
389         replay_timeout *timer = NULL;
390
391         timer = (replay_timeout *) malloc(sizeof(replay_timeout));
392         if (timer == NULL) {
393                 LOG_PLUGIN(DBG_ERR, "replay_timeout allocation is failed.");
394                 return NULL;
395         }
396
397         timer->interval = 1;
398         if (setting_get_int(REPLAY_MODE, &timer->replay_mode) == FALSE) {
399                 timer->replay_mode = REPLAY_OFF;
400         }
401         setting_notify_key_changed(REPLAY_MODE, replay_mode_changed_cb);
402
403         timer->pos_data = (pos_data_t *) malloc(sizeof(pos_data_t));
404         timer->sv_data = (sv_data_t *) malloc(sizeof(sv_data_t));
405         timer->nmea_data = (nmea_data_t *) malloc(sizeof(nmea_data_t));
406
407         if (timer->pos_data == NULL || timer->sv_data == NULL || timer->nmea_data == NULL) {
408                 LOG_PLUGIN(DBG_ERR, "pos_data or sv_data or nmea_data allocation is failed.");
409                 return NULL;
410         }
411
412         timer->timeout_src = NULL;
413         timer->default_context = NULL;
414
415         return timer;
416 }
417
418 void gps_plugin_replay_timer_deinit(replay_timeout * timer)
419 {
420         if (timer == NULL) {
421                 return;
422         }
423
424         if (timer->pos_data != NULL) {
425                 free(timer->pos_data);
426                 timer->pos_data = NULL;
427         }
428         if (timer->sv_data != NULL) {
429                 free(timer->sv_data);
430                 timer->sv_data = NULL;
431         }
432         if (timer->nmea_data != NULL) {
433                 free(timer->nmea_data);
434                 timer->nmea_data = NULL;
435         }
436
437         setting_ignore_key_changed(REPLAY_MODE, replay_mode_changed_cb);
438
439         free(timer);
440         timer = NULL;
441 }
442
443 int gps_plugin_replay_gps_init(gps_event_cb gps_event_cb, gps_server_param_t * gps_params)
444 {
445         g_gps_event_cb = gps_event_cb;
446         g_replay_timer = gps_plugin_replay_timer_init();
447
448         return TRUE;
449 }
450
451 int gps_plugin_replay_gps_deinit(gps_failure_reason_t * reason_code)
452 {
453         gps_plugin_replay_timer_deinit(g_replay_timer);
454
455         return TRUE;
456 }
457
458 int gps_plugin_replay_gps_request(gps_action_t gps_action, void *data, gps_failure_reason_t * reason_code)
459 {
460         switch (gps_action) {
461         case GPS_ACTION_SEND_PARAMS:
462                 break;
463         case GPS_ACTION_START_SESSION:
464                 gps_plugin_start_replay_mode(g_replay_timer);
465                 break;
466         case GPS_ACTION_STOP_SESSION:
467                 gps_plugin_stop_replay_mode(g_replay_timer);
468                 break;
469         case GPS_INDI_SUPL_VERIFICATION:
470         case GPS_INDI_SUPL_DNSQUERY:
471         case GPS_ACTION_START_FACTTEST:
472         case GPS_ACTION_STOP_FACTTEST:
473         case GPS_ACTION_REQUEST_SUPL_NI:
474                 LOG_PLUGIN(DBG_LOW, "Don't use action type : [ %d ]", gps_action);
475                 break;
476         default:
477                 break;
478         }
479
480         return TRUE;
481 }
482
483 EXPORT_API const gps_plugin_interface *get_gps_plugin_interface()
484 {
485         return &g_gps_plugin_replay_interface;
486 }