From f5f230c665316320d98f8e2b3baa1adca9b193dd Mon Sep 17 00:00:00 2001 From: "kj7.sung" Date: Thu, 7 Jul 2016 14:35:32 +0900 Subject: [PATCH] Fixed batch behavior in emulator mode Change-Id: I38b226feea28e1e42fc91dea56782b6ff015e4e5 Signed-off-by: kj7.sung --- gps-plugin/include/gps_plugin_debug.h | 4 +- gps-plugin/src/gps_plugin_replay.c | 193 +++++++++--------------- gps-plugin/src/nmea_parser.c | 12 +- packaging/lbs-server-plugin-replay.spec | 2 +- 4 files changed, 79 insertions(+), 132 deletions(-) diff --git a/gps-plugin/include/gps_plugin_debug.h b/gps-plugin/include/gps_plugin_debug.h index d06956e..e74a02d 100644 --- a/gps-plugin/include/gps_plugin_debug.h +++ b/gps-plugin/include/gps_plugin_debug.h @@ -31,9 +31,9 @@ extern "C" { #ifdef GPS_DLOG #include -#define TAG_GPS_PLUGIN "gps-plugin" +#define TAG_GPS_PLUGIN "LBS_REPLAY" -#define DBG_LOW LOG_UNKNOWN /* LOG_DEBUG */ +#define DBG_LOW LOG_DEBUG #define DBG_INFO LOG_INFO #define DBG_WARN LOG_WARN #define DBG_ERR LOG_ERROR diff --git a/gps-plugin/src/gps_plugin_replay.c b/gps-plugin/src/gps_plugin_replay.c index 544ca63..851f00a 100644 --- a/gps-plugin/src/gps_plugin_replay.c +++ b/gps-plugin/src/gps_plugin_replay.c @@ -51,6 +51,7 @@ typedef struct { int lcd_mode; int batch_mode; + int batch_client_count; int batch_interval; int batch_period; int num_of_batch; @@ -109,51 +110,47 @@ void gps_plugin_replay_pos_event(pos_data_t *data) void gps_plugin_replay_batch_event(pos_data_t *data, replay_timeout *timer) { + char buf[256] = {0, }; time_t timestamp; time(×tamp); const char *batch_path = tzplatform_mkpath(TZ_SYS_MEDIA, "lbs-server/location_batch.log"); if (timer->batch_fd == NULL) { - if (timer->batch_mode == BATCH_MODE_ON) { - - struct stat st = {0}; - const char *lbs_server_path = tzplatform_mkpath(TZ_SYS_MEDIA, "lbs-server"); - if (stat(lbs_server_path, &st) == -1) { - if (mkdir(lbs_server_path, 0777) == -1) { - LOG_PLUGIN(DBG_ERR, "Fail to create lbs-server folder"); - return ; - } - } - timer->batch_fd = fopen(batch_path, "w+"); - if (timer->batch_fd == NULL) { - LOG_PLUGIN(DBG_ERR, "Fail to open file [Not available batch_fd]"); + struct stat st = {0}; + const char *lbs_server_path = tzplatform_mkpath(TZ_SYS_MEDIA, "lbs-server"); + if (stat(lbs_server_path, &st) == -1) { + if (mkdir(lbs_server_path, 0777) == -1) { + LOG_PLUGIN(DBG_ERR, "Fail to create lbs-server folder"); return ; } } + + timer->batch_fd = fopen(batch_path, "w+"); + if (timer->batch_fd == NULL) { + LOG_PLUGIN(DBG_ERR, "Fail to open file [Not available batch_fd]"); + return ; + } } if (data != NULL) { - if (timer->batch_mode == BATCH_MODE_ON) { - int ret = -1; - char buf[256] = {0, }; - - g_snprintf(buf, 256, "%ld;%.6lf;%.6lf;%.2lf;%.2lf;%.2lf;%.2lf;%.2lf;\n", timestamp, data->latitude, data->longitude, data->altitude, data->speed, data->bearing, data->hor_accuracy, data->ver_accuracy); - LOG_PLUGIN(DBG_LOW, "Add location info to batch file [%s]", buf); - ret = fwrite(buf, 1, strlen(buf), timer->batch_fd); - if (ret != strlen(buf)) - LOG_PLUGIN(DBG_ERR, "Fail to write file[%s]", batch_path); - - (timer->num_of_batch)++ ; - } + g_snprintf(buf, 256, "%ld;%.6lf;%.6lf;%.2lf;%.2lf;%.2lf;%.2lf;%.2lf;\n", + timestamp, data->latitude, data->longitude, data->altitude, + data->speed, data->bearing, data->hor_accuracy, data->ver_accuracy); + LOG_PLUGIN(DBG_LOW, "Add location info to batch file [%s]", buf); } if (timer->lcd_mode == VCONFKEY_PM_STATE_NORMAL) { - if ((timestamp - timer->batch_start_time) >= timer->batch_interval) - timer->is_flush = TRUE; + fwrite(buf, 1, strlen(buf), timer->batch_fd); + (timer->num_of_batch)++ ; + timer->is_flush = TRUE; } else { + if ((timestamp - timer->batch_start_time) % timer->batch_interval == 0) { + fwrite(buf, 1, strlen(buf), timer->batch_fd); + (timer->num_of_batch)++ ; + } if ((timestamp - timer->batch_start_time) >= timer->batch_period) timer->is_flush = TRUE; } @@ -306,11 +303,11 @@ gboolean gps_plugin_replay_read_nmea(replay_timeout *timer, char *nmea_data) ref++; if (ref > 1) { fseek(timer->fd, -strlen(buf), SEEK_CUR); - LOG_PLUGIN(DBG_LOW, "2nd GPGGA : stop to read nmea data"); + /* LOG_PLUGIN(DBG_LOW, "2nd GPGGA : stop to read nmea data"); */ ret = TRUE; break; } else if (ref == 1) { - LOG_PLUGIN(DBG_LOW, "1st GPGGA : start to read nmea data"); + /* LOG_PLUGIN(DBG_LOW, "1st GPGGA : start to read nmea data"); */ strncpy(nmea_data, buf, strlen(buf)); } } else { @@ -331,7 +328,7 @@ gboolean gps_plugin_replay_read_nmea(replay_timeout *timer, char *nmea_data) rewind(timer->fd); ret = TRUE; } else { - LOG_PLUGIN(DBG_LOW, "read nmea data [%s]", nmea_data); + /* LOG_PLUGIN(DBG_LOW, "read nmea data [%s]", nmea_data); */ } return ret; } @@ -402,62 +399,15 @@ gboolean gps_plugin_replay_timeout_cb(gpointer data) } if (g_gps_event_cb != NULL) { - if (err != READ_NOT_FIXED) - gps_plugin_replay_pos_event(timer->pos_data); - - gps_plugin_replay_sv_event(timer->sv_data); - } - ret = TRUE; - return ret; -} - -gboolean gps_plugin_batch_replay_timeout_cb(gpointer data) -{ - gboolean ret = FALSE; - read_error_t err = READ_SUCCESS; - char nmea_data[REPLAY_NMEA_SET_SIZE] = { 0, }; - replay_timeout *timer = (replay_timeout *) data; - - if (timer == NULL) { - LOG_PLUGIN(DBG_ERR, "replay handel[timer] is NULL"); - return FALSE; - } - - memset(timer->pos_data, 0, sizeof(pos_data_t)); - memset(timer->batch_data, 0, sizeof(batch_data_t)); - memset(timer->sv_data, 0, sizeof(sv_data_t)); - - if (timer->replay_mode == REPLAY_NMEA) { - if (gps_plugin_replay_read_nmea(timer, nmea_data) == FALSE) { - LOG_PLUGIN(DBG_ERR, "Fail to read nmea data from file"); - return FALSE; - } else { - err = nmea_parser(nmea_data, timer->pos_data, timer->sv_data); - if (err == READ_ERROR) { - LOG_PLUGIN(DBG_ERR, "Fail to parser nmea data from file"); - } else if (err == READ_NOT_FIXED) { - LOG_PLUGIN(DBG_LOW, "GPS position is not fixed"); - timer->sv_data->pos_valid = FALSE; + if (err != READ_NOT_FIXED) { + if (timer->batch_mode == BATCH_MODE_ON) { + gps_plugin_replay_batch_event(timer->pos_data, timer); } + + gps_plugin_replay_pos_event(timer->pos_data); } - } else if (timer->replay_mode == REPLAY_MANUAL) { - if (gps_plugin_replay_read_manual(timer->pos_data) == FALSE) { - LOG_PLUGIN(DBG_ERR, "Fail to read manual data"); - err = READ_ERROR; - return FALSE; - } else { - timer->sv_data->pos_valid = TRUE; - err = READ_SUCCESS; - } - } else if (timer->replay_mode == REPLAY_OFF) { - LOG_PLUGIN(DBG_WARN, "replay_mode is OFF"); - err = READ_NOT_FIXED; - timer->sv_data->pos_valid = FALSE; - } - if (g_gps_event_cb != NULL) { - if (timer->batch_mode == BATCH_MODE_ON) - gps_plugin_replay_batch_event(timer->pos_data, timer); + gps_plugin_replay_sv_event(timer->sv_data); } ret = TRUE; return ret; @@ -518,6 +468,7 @@ gboolean gps_plugin_get_nmea_fd(replay_timeout *timer) gboolean gps_plugin_start_replay_mode(replay_timeout *timer) { + LOG_PLUGIN(DBG_LOW, "gps_plugin_start replay mode"); gboolean ret = FALSE; if (timer->replay_mode == REPLAY_NMEA) { @@ -557,60 +508,48 @@ gboolean gps_plugin_start_replay_mode(replay_timeout *timer) return ret; } -gboolean gps_plugin_start_batch_mode(replay_timeout *timer, int batch_interval, int batch_period) +gboolean gps_plugin_update_batch_mode(replay_timeout *timer, int batch_interval, int batch_period) { gboolean ret = FALSE; time_t timestamp; time(×tamp); - if (timer->replay_mode == REPLAY_NMEA) { - if (gps_plugin_get_nmea_fd(timer) == FALSE) - return FALSE; - } - - if (timer->default_context == NULL) { - timer->default_context = g_main_context_default(); - - if (timer->default_context == NULL) - return ret; + if (timer->batch_mode == BATCH_MODE_OFF) { + timer->batch_mode = BATCH_MODE_ON; + timer->batch_client_count = 0; } + timer->batch_client_count++; - if (timer->timeout_src != NULL) { - LOG_PLUGIN(DBG_ERR, "timeout_src is already existed"); - ret = FALSE; - } else { - timer->timeout_src = g_timeout_source_new_seconds(batch_interval); - if (timer->timeout_src != NULL) { - g_source_set_callback(timer->timeout_src, &gps_plugin_batch_replay_timeout_cb, timer, NULL); - if (g_source_attach(timer->timeout_src, timer->default_context) > 0) { - LOG_PLUGIN(DBG_LOW, "timeout_src(0x%x) is created & attatched to 0x%x", timer->timeout_src, timer->default_context); - ret = TRUE; - } else { - gps_plugin_stop_replay_mode(timer); - ret = FALSE; - } - } - } - - gps_plugin_respond_start_session(ret); - - timer->batch_mode = BATCH_MODE_ON; timer->batch_interval = batch_interval; timer->batch_period = batch_period; timer->batch_start_time = timestamp; + LOG_PLUGIN(DBG_LOW, "batch_client_count = %d", timer->batch_client_count); return ret; } -void gps_plugin_stop_batch_mode(replay_timeout *timer) +void gps_plugin_stop_batch_mode(replay_timeout *timer, int batch_interval, int batch_period) { - if (timer->batch_mode == BATCH_MODE_ON) + if (timer->batch_client_count > 0) + timer->batch_client_count--; + + LOG_PLUGIN(DBG_ERR, "batch_client_count = %d", timer->batch_client_count); + if (timer->batch_client_count <= 0) { timer->batch_mode = BATCH_MODE_OFF; + timer->batch_interval = 0; + timer->batch_period = 0; - if (timer->batch_fd != NULL) { - fclose(timer->batch_fd); - timer->batch_fd = NULL; - timer->num_of_batch = 0; + if (timer->batch_fd != NULL) { + fclose(timer->batch_fd); + timer->batch_fd = NULL; + timer->num_of_batch = 0; + } + } else { + timer->batch_interval = batch_interval; + timer->batch_period = batch_period; + time_t timestamp; + time(×tamp); + timer->batch_start_time = timestamp; } } @@ -660,7 +599,10 @@ replay_timeout *gps_plugin_replay_timer_init() timer->batch_fd = NULL; timer->num_of_batch = 0; + timer->batch_client_count = 0; timer->batch_mode = BATCH_MODE_OFF; + timer->batch_interval = 0; + timer->batch_period = 0; timer->is_flush = FALSE; if (setting_get_int(VCONFKEY_LOCATION_REPLAY_MODE, &timer->replay_mode) == FALSE) @@ -769,11 +711,16 @@ int gps_plugin_replay_gps_request(gps_action_t gps_action, void *gps_action_data gps_plugin_stop_replay_mode(g_replay_timer); break; case GPS_ACTION_START_BATCH: - gps_plugin_start_batch_mode(g_replay_timer, gps_start_data->interval, gps_start_data->period); + if (!gps_start_data->session_status) /* need to start */ + gps_plugin_start_replay_mode(g_replay_timer); + + gps_plugin_update_batch_mode(g_replay_timer, gps_start_data->interval, gps_start_data->period); break; case GPS_ACTION_STOP_BATCH: - gps_plugin_stop_batch_mode(g_replay_timer); - gps_plugin_stop_replay_mode(g_replay_timer); + if (!gps_start_data->session_status) /* need to stop */ + gps_plugin_stop_replay_mode(g_replay_timer); + + gps_plugin_stop_batch_mode(g_replay_timer, gps_start_data->interval, gps_start_data->period); break; case GPS_INDI_SUPL_VERIFICATION: case GPS_INDI_SUPL_DNSQUERY: diff --git a/gps-plugin/src/nmea_parser.c b/gps-plugin/src/nmea_parser.c index d8bf178..945bd17 100644 --- a/gps-plugin/src/nmea_parser.c +++ b/gps-plugin/src/nmea_parser.c @@ -83,7 +83,7 @@ int nmea_parser_tokenize(char input[], char *token[]) token[num_tokens] = s; num_tokens++; state = 0; - LOG_PLUGIN(DBG_LOW, "input:%s \n", input); + /* LOG_PLUGIN(DBG_LOW, "input:%s \n", input); */ while ((*s != 0) && (num_tokens < MAX_TOEKNS)) { switch (state) { @@ -197,7 +197,7 @@ static int nmea_parser_gprmc(char *token[], pos_data_t *pos) status = token[2]; /*warn = *token[2]; */ if (strcmp(status, "V") == 0) { - LOG_PLUGIN(DBG_LOW, "Not fixed"); + /* LOG_PLUGIN(DBG_LOW, "Not fixed"); */ return READ_NOT_FIXED; } @@ -224,7 +224,7 @@ static int nmea_parser_gpgll(char *token[], pos_data_t *pos) status = token[6]; /*warn = *token[2]; */ if (strcmp(status, "V") == 0) { - LOG_PLUGIN(DBG_LOW, "Not fixed"); + /* LOG_PLUGIN(DBG_LOW, "Not fixed"); */ return READ_NOT_FIXED; } @@ -243,7 +243,7 @@ static int nmea_parser_gpgsa(char *token[], pos_data_t *pos) fix_type = atoi(token[2]); if (fix_type == 1) { - LOG_PLUGIN(DBG_LOW, "Not fixed"); + /* LOG_PLUGIN(DBG_LOW, "Not fixed"); */ return READ_NOT_FIXED; } @@ -352,14 +352,14 @@ int nmea_parser(char *data, pos_data_t *pos, sv_data_t *sv) num_sen++; nmea_sen[num_sen] = (char *)strtok_r(NULL, "$", &last); } - LOG_PLUGIN(DBG_LOW, "Number of NMEA sentences:%d \n", num_sen); + /* LOG_PLUGIN(DBG_LOW, "Number of NMEA sentences:%d \n", num_sen); */ while (num_sen > 0) { if (nmea_parser_verify_checksum(nmea_sen[count]) == 0) { nmea_parser_tokenize(nmea_sen[count], token); err = nmea_parser_sentence(token[0], token, pos, sv); if (err == READ_NOT_FIXED) { - LOG_PLUGIN(DBG_LOW, "NOT Fixed"); + /* LOG_PLUGIN(DBG_LOW, "NOT Fixed"); */ ret = err; } else if (err == READ_ERROR) { ret = err; diff --git a/packaging/lbs-server-plugin-replay.spec b/packaging/lbs-server-plugin-replay.spec index 3df890c..867b318 100644 --- a/packaging/lbs-server-plugin-replay.spec +++ b/packaging/lbs-server-plugin-replay.spec @@ -1,6 +1,6 @@ Name: lbs-server-plugin-replay Summary: LBS Server plugin library for replay mode -Version: 0.2.4 +Version: 0.2.5 Release: 1 Group: Location/Libraries License: Apache-2.0 -- 2.34.1