4 * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
35 #include "gps_manager_data_types.h"
36 #include "gps_manager_plugin_intf.h"
37 #include "nmea_logger.h"
38 #include "data_connection.h"
40 #include "plugin_module.h"
41 #include "debug_util.h"
42 #include "last_position.h"
45 #include <msg_transport.h>
49 #include <vconf-keys.h>
55 #include <glib-object.h>
56 #if !GLIB_CHECK_VERSION (2, 31, 0)
57 #include <glib/gthread.h>
59 #include <dbus/dbus-glib.h>
61 #define GPS_NI_POPUP "/usr/bin/gps_popup"
62 #define GPS_NI_BTN "BTN"
63 #define GPS_NI_WITHOUT_BTN "NONE"
64 #define YES_EXIT_STATUS 174
65 #define NO_EXIT_STATUS 175
67 #define REPLAY_MODULE "replay"
72 } gps_plugin_handler_t;
74 gps_plugin_handler_t g_gps_plugin;
75 gps_server_param_t g_gps_params;
77 pos_data_t *gps_pos_data = NULL;
78 sv_data_t *gps_sv_data = NULL;
79 nmea_data_t *gps_nmea_data = NULL;
80 pos_data_t *gps_last_pos = NULL;
84 gboolean logging_enabled = FALSE;
85 gboolean replay_enabled = FALSE;
87 static pthread_t msg_thread = 0; /* Register SUPL NI cb to msg server Thread */
88 static pthread_t popup_thread = 0; /* Register SUPL NI cb to msg server Thread */
89 static int msg_thread_status;
91 gps_session_state_t gps_session_state = GPS_SESSION_STOPPED;
93 struct gps_callbacks g_update_cb;
96 static int _gps_server_gps_event_cb(gps_event_info_t * gps_event_info);
98 static void _gps_mode_changed_cb(keynode_t * key, void *data)
102 if (setting_get_int(VCONFKEY_LOCATION_GPS_SESSION, &int_val) == FALSE) {
103 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_GPS_SESSION setting");
104 int_val = GPS_SESSION_TRACKING_MODE;
106 g_gps_params.session_type = int_val;
108 if (setting_get_int(VCONFKEY_LOCATION_GPS_OPERATION, &int_val) == FALSE) {
109 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_GPS_OPERATION setting");
110 int_val = GPS_OPERATION_STANDALONE;
112 g_gps_params.operation_mode = int_val;
114 if (setting_get_int(VCONFKEY_LOCATION_GPS_STARTING, &int_val) == FALSE) {
115 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_GPS_STARTING setting");
116 int_val = GPS_STARTING_HOT_;
118 g_gps_params.starting_type = int_val;
123 static void _gps_supl_changed_cb(keynode_t * key, void *data)
128 str = setting_get_string(VCONFKEY_LOCATION_SUPL_SERVER);
131 snprintf(g_gps_params.supl_url, MAX_SUPL_URL_LEN, "%s", SUPL_SERVER_URL_DEFAULT);
132 LOG_GPS(DBG_ERR, "vconf fail to get Server URL [Default URL]");
134 snprintf(g_gps_params.supl_url, MAX_SUPL_URL_LEN, "%s", str);
137 if (setting_get_int(VCONFKEY_LOCATION_SUPL_PORT, &int_val) == FALSE) {
138 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_SUPL_PORT setting");
139 int_val = SUPL_SERVER_PORT_DEFAULT;
141 g_gps_params.supl_port = int_val;
146 static void _gps_setting_changed_cb(keynode_t * key, void *data)
150 if (setting_get_int(VCONFKEY_LOCATION_SUPL_SSL, &int_val) == FALSE) {
151 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_SUPL_SSL setting");
154 g_gps_params.ssl_mode = int_val;
158 static void _gps_nmea_changed_cb(keynode_t * key, void *data)
161 if (setting_get_int(VCONFKEY_LOCATION_NMEA_LOGGING, &int_val) == FALSE) {
162 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_NMEA_LOGGING setting");
165 logging_enabled = (int_val == 1) ? TRUE : FALSE;
169 static gboolean get_replay_enabled()
174 if (setting_get_int(VCONFKEY_LOCATION_REPLAY_ENABLED, &int_val) == FALSE) {
175 LOG_GPS(DBG_ERR, "//ERROR get VCONFKEY_LOCATION_REPLAY_ENABLED setting");
179 ret = (int_val == 1) ? TRUE : FALSE;
184 static void reload_plugin_module()
187 gps_failure_reason_t ReasonCode = GPS_FAILURE_CAUSE_NORMAL;
189 if (replay_enabled == TRUE) {
190 module_name = REPLAY_MODULE;
192 module_name = g_gps_plugin.name;
195 LOG_GPS(DBG_LOW, "Loading plugin.name : %s", module_name);
197 if (!get_plugin_module()->deinit(&ReasonCode)) {
198 LOG_GPS(DBG_ERR, "Fail to GPS plugin deinit");
200 unload_plugin_module(&g_gps_plugin.handle);
201 if (!load_plugin_module(module_name, &g_gps_plugin.handle)) {
202 LOG_GPS(DBG_ERR, "Fail to load %s plugin", module_name);
204 if (!get_plugin_module()->init(_gps_server_gps_event_cb, &g_gps_params)) {
205 LOG_GPS(DBG_ERR, "Fail to %s plugin init", module_name);
212 static void _gps_replay_changed_cb(keynode_t * key, void *data)
214 replay_enabled = get_replay_enabled();
215 reload_plugin_module();
220 static void _position_state_changed_cb(keynode_t * key, void *data)
226 setting_get_int(VCONFKEY_LOCATION_GPS_STATE, &gps_state);
227 setting_get_int(VCONFKEY_LOCATION_WPS_STATE, &wps_state);
228 LOG_GPS(DBG_LOW, "gps state : [%d], wps state : [%d]", gps_state, wps_state);
230 if (gps_state == POSITION_CONNECTED || wps_state == POSITION_CONNECTED) {
231 ret = setting_set_int(VCONFKEY_LOCATION_POSITION_STATE, POSITION_CONNECTED);
233 if (gps_state == POSITION_OFF && wps_state == POSITION_OFF) {
234 ret = setting_set_int(VCONFKEY_LOCATION_POSITION_STATE, POSITION_OFF);
236 ret = setting_set_int(VCONFKEY_LOCATION_POSITION_STATE, POSITION_SEARCHING);
243 static void _gps_server_set_gps_state(int gps_state)
248 case POSITION_CONNECTED:
249 ret = setting_set_int(VCONFKEY_LOCATION_GPS_STATE, POSITION_CONNECTED);
251 case POSITION_SEARCHING:
252 ret = setting_set_int(VCONFKEY_LOCATION_GPS_STATE, POSITION_SEARCHING);
255 ret = setting_set_int(VCONFKEY_LOCATION_GPS_STATE, POSITION_OFF);
258 ret = setting_set_int(VCONFKEY_LOCATION_GPS_STATE, POSITION_OFF);
263 LOG_GPS(DBG_LOW, "Succesee to set indicator");
265 LOG_GPS(DBG_ERR, "Fail to setting_set_int(VCONF_GPS_STATE, ...)");
269 static gboolean _initialize_data()
271 gboolean result = TRUE;
272 if (gps_pos_data == NULL) {
273 gps_pos_data = (pos_data_t *) malloc(sizeof(pos_data_t));
274 if (gps_pos_data == NULL) {
275 LOG_GPS(DBG_ERR, "Failed to alloc gps_pos_data");
278 memset(gps_pos_data, 0x00, sizeof(pos_data_t));
282 if (gps_sv_data == NULL) {
283 gps_sv_data = (sv_data_t *) malloc(sizeof(sv_data_t));
284 if (gps_sv_data == NULL) {
285 LOG_GPS(DBG_ERR, "Failed to alloc gps_sv_data");
288 memset(gps_sv_data, 0x00, sizeof(sv_data_t));
292 if (gps_nmea_data == NULL) {
293 gps_nmea_data = (nmea_data_t *) malloc(sizeof(nmea_data_t));
294 if (gps_nmea_data == NULL) {
295 LOG_GPS(DBG_ERR, "Failed to alloc gps_nmea_data");
298 memset(gps_nmea_data, 0x00, sizeof(nmea_data_t));
302 if (gps_last_pos == NULL) {
303 gps_last_pos = (pos_data_t *) malloc(sizeof(pos_data_t));
304 if (gps_last_pos == NULL) {
305 LOG_GPS(DBG_ERR, "Failed to alloc gps_last_pos");
308 memset(gps_last_pos, 0x00, sizeof(pos_data_t));
314 int request_start_session()
316 gboolean status = TRUE;
317 gps_failure_reason_t reason_code = GPS_FAILURE_CAUSE_NORMAL;
319 if (gps_session_state != GPS_SESSION_STOPPED && gps_session_state != GPS_SESSION_STOPPING) {
320 LOG_GPS(DBG_LOW, "Main: GPS Session Already Started!");
324 gps_session_state = GPS_SESSION_STARTING;
325 LOG_GPS(DBG_LOW, "==GPSSessionState[%d]", gps_session_state);
326 status = get_plugin_module()->request(GPS_ACTION_START_SESSION, &g_gps_params, &reason_code);
328 if (status == FALSE) {
329 LOG_GPS(DBG_ERR, "Main: sending GPS_ACTION_START_SESSION Fail !");
333 LOG_GPS(DBG_LOW, "Main: sending GPS_ACTION_START_SESSION OK !");
336 setting_ignore_key_changed(VCONFKEY_LOCATION_REPLAY_ENABLED, _gps_replay_changed_cb);
341 int request_stop_session()
343 gboolean status = TRUE;
344 gboolean cur_replay_enabled = FALSE;
345 gps_failure_reason_t reason_code = GPS_FAILURE_CAUSE_NORMAL;
347 LOG_GPS(DBG_LOW, "Main: Stop GPS Session, ==GPSSessionState[%d]", gps_session_state);
348 if (gps_session_state == GPS_SESSION_STARTED || gps_session_state == GPS_SESSION_STARTING) {
349 status = get_plugin_module()->request(GPS_ACTION_STOP_SESSION, NULL, &reason_code);
351 gps_session_state = GPS_SESSION_STOPPING;
352 LOG_GPS(DBG_LOW, "==GPSSessionState[%d]", gps_session_state);
353 cur_replay_enabled = get_replay_enabled();
354 if (replay_enabled != cur_replay_enabled) {
355 replay_enabled = cur_replay_enabled;
356 reload_plugin_module();
358 setting_notify_key_changed(VCONFKEY_LOCATION_REPLAY_ENABLED, _gps_replay_changed_cb);
360 LOG_GPS(DBG_LOW, " plugin->request to LBS_GPS_STOP_SESSION Failed, reasonCode =%d", reason_code);
363 // If request is not sent, keep the client registed
365 " LBS_GPS_STOP_SESSION is not sent because the GPS state is not started, keep the client registed ");
370 static void _gps_plugin_handler_init(char *module_name)
372 g_gps_plugin.handle = NULL;
373 g_gps_plugin.name = (char *)malloc(strlen(module_name) + 1);
374 snprintf(g_gps_plugin.name, strlen(module_name) + 1, "%s", module_name);
375 if (strcmp(REPLAY_MODULE, g_gps_plugin.name) == 0) {
376 setting_set_int(VCONFKEY_LOCATION_REPLAY_ENABLED, 1);
378 setting_set_int(VCONFKEY_LOCATION_REPLAY_ENABLED, 0);
382 static void _gps_plugin_handler_deinit()
384 setting_set_int(VCONFKEY_LOCATION_REPLAY_ENABLED, 0);
386 if (g_gps_plugin.handle != NULL) {
387 g_gps_plugin.handle = NULL;
390 if (g_gps_plugin.name != NULL) {
391 free(g_gps_plugin.name);
392 g_gps_plugin.name = NULL;
396 static void _gps_read_params()
401 if (setting_get_int(VCONFKEY_LOCATION_GPS_SESSION, &int_val) == FALSE) {
402 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_GPS_SESSION setting");
403 int_val = GPS_SESSION_TRACKING_MODE;
405 g_gps_params.session_type = int_val;
407 if (setting_get_int(VCONFKEY_LOCATION_GPS_OPERATION, &int_val) == FALSE) {
408 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_GPS_OPERATION setting");
409 int_val = GPS_OPERATION_STANDALONE;
411 g_gps_params.operation_mode = int_val;
413 if (setting_get_int(VCONFKEY_LOCATION_GPS_STARTING, &int_val) == FALSE) {
414 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_GPS_STATING setting");
415 int_val = GPS_STARTING_HOT_;
417 g_gps_params.starting_type = int_val;
419 g_gps_params.time_bn_fixes = 1;
421 str = setting_get_string(VCONFKEY_LOCATION_SUPL_SERVER);
423 snprintf(g_gps_params.supl_url, MAX_SUPL_URL_LEN, "%s", SUPL_SERVER_URL_DEFAULT);
424 LOG_GPS(DBG_ERR, "vconf fail to get Server URL [Default URL]");
426 snprintf(g_gps_params.supl_url, MAX_SUPL_URL_LEN, "%s", str);
429 if (setting_get_int(VCONFKEY_LOCATION_SUPL_PORT, &int_val) == FALSE) {
430 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_SUPL_PORT setting");
431 int_val = SUPL_SERVER_PORT_DEFAULT;
433 g_gps_params.supl_port = int_val;
435 LOG_GPS(DBG_LOW, "First Read!! SUPL server:%s, port:%d", g_gps_params.supl_url, g_gps_params.supl_port);
437 if (setting_get_int(VCONFKEY_LOCATION_SUPL_SSL, &int_val) == FALSE) {
438 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_SUPL_SSL setting");
441 g_gps_params.ssl_mode = int_val;
443 if (setting_get_int(VCONFKEY_LOCATION_NMEA_LOGGING, &int_val) == FALSE) {
444 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_NMEA_LOGGING setting");
447 logging_enabled = (int_val == 1) ? TRUE : FALSE;
449 if (setting_get_int(VCONFKEY_LOCATION_REPLAY_ENABLED, &int_val) == FALSE) {
450 LOG_GPS(DBG_ERR, "//ERROR!! get VCONFKEY_LOCATION_REPLAY_ENABLED setting");
453 replay_enabled = (int_val == 1) ? TRUE : FALSE;
456 static void _gps_notify_params()
458 setting_notify_key_changed(VCONFKEY_LOCATION_GPS_SESSION, _gps_mode_changed_cb);
459 setting_notify_key_changed(VCONFKEY_LOCATION_GPS_OPERATION, _gps_mode_changed_cb);
460 setting_notify_key_changed(VCONFKEY_LOCATION_GPS_STARTING, _gps_mode_changed_cb);
461 setting_notify_key_changed(VCONFKEY_LOCATION_SUPL_SERVER, _gps_supl_changed_cb);
462 setting_notify_key_changed(VCONFKEY_LOCATION_SUPL_PORT, _gps_supl_changed_cb);
463 setting_notify_key_changed(VCONFKEY_LOCATION_SUPL_SSL, _gps_setting_changed_cb);
464 setting_notify_key_changed(VCONFKEY_LOCATION_NMEA_LOGGING, _gps_nmea_changed_cb);
465 setting_notify_key_changed(VCONFKEY_LOCATION_REPLAY_ENABLED, _gps_replay_changed_cb);
466 setting_notify_key_changed(VCONFKEY_LOCATION_GPS_STATE, _position_state_changed_cb);
467 setting_notify_key_changed(VCONFKEY_LOCATION_WPS_STATE, _position_state_changed_cb);
470 static void _gps_ignore_params()
472 setting_ignore_key_changed(VCONFKEY_LOCATION_GPS_SESSION, _gps_mode_changed_cb);
473 setting_ignore_key_changed(VCONFKEY_LOCATION_GPS_OPERATION, _gps_mode_changed_cb);
474 setting_ignore_key_changed(VCONFKEY_LOCATION_GPS_STARTING, _gps_mode_changed_cb);
475 setting_ignore_key_changed(VCONFKEY_LOCATION_SUPL_SERVER, _gps_supl_changed_cb);
476 setting_ignore_key_changed(VCONFKEY_LOCATION_SUPL_PORT, _gps_supl_changed_cb);
477 setting_ignore_key_changed(VCONFKEY_LOCATION_SUPL_SSL, _gps_setting_changed_cb);
478 setting_ignore_key_changed(VCONFKEY_LOCATION_NMEA_LOGGING, _gps_nmea_changed_cb);
479 setting_ignore_key_changed(VCONFKEY_LOCATION_REPLAY_ENABLED, _gps_replay_changed_cb);
480 setting_ignore_key_changed(VCONFKEY_LOCATION_GPS_STATE, _position_state_changed_cb);
481 setting_ignore_key_changed(VCONFKEY_LOCATION_WPS_STATE, _position_state_changed_cb);
484 static void _gps_server_start_event()
486 LOG_GPS(DBG_LOW, "==GPSSessionState [%d] -> [%d]", gps_session_state, GPS_SESSION_STARTED);
487 gps_session_state = GPS_SESSION_STARTED;
489 if (logging_enabled) {
490 LOG_GPS(DBG_LOW, "NMEA STARTED");
494 if (gps_pos_data == NULL) {
495 gps_pos_data = (pos_data_t *) malloc(sizeof(pos_data_t));
496 if (gps_pos_data == NULL) {
497 LOG_GPS(DBG_WARN, "//callback: gps_pos_data re-malloc Failed!!");
499 memset(gps_pos_data, 0x00, sizeof(pos_data_t));
502 if (gps_sv_data == NULL) {
503 gps_sv_data = (sv_data_t *) malloc(sizeof(sv_data_t));
504 if (gps_sv_data == NULL) {
505 LOG_GPS(DBG_WARN, "//callback: gps_sv_data re-malloc Failed!!");
507 memset(gps_sv_data, 0x00, sizeof(sv_data_t));
510 if (gps_nmea_data == NULL) {
511 gps_nmea_data = (nmea_data_t *) malloc(sizeof(nmea_data_t));
512 if (gps_nmea_data == NULL) {
513 LOG_GPS(DBG_WARN, "//callback: gps_nmea_data re-malloc Failed!!");
515 memset(gps_nmea_data, 0x00, sizeof(nmea_data_t));
519 if (gps_last_pos == NULL) {
520 gps_last_pos = (pos_data_t *) malloc(sizeof(pos_data_t));
521 if (gps_last_pos == NULL) {
522 LOG_GPS(DBG_WARN, "//callback: gps_last_pos re-malloc Failed!!");
524 memset(gps_last_pos, 0x00, sizeof(pos_data_t));
525 gps_manager_get_last_position(gps_last_pos);
529 _gps_server_set_gps_state(POSITION_SEARCHING);
530 pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
533 static void _gps_server_stop_event()
535 LOG_GPS(DBG_LOW, "==GPSSessionState [%d] -> [%d]", gps_session_state, GPS_SESSION_STOPPED);
536 gps_session_state = GPS_SESSION_STOPPED;
537 LOG_GPS(DBG_LOW, "==GPSSessionState[%d]", gps_session_state);
539 _gps_server_set_gps_state(POSITION_OFF);
540 pm_unlock_state(LCD_NORMAL, PM_RESET_TIMER);
542 if (logging_enabled) {
543 LOG_GPS(DBG_LOW, "NMEA STOPPED");
547 if (gps_pos_data != NULL) {
551 if (gps_sv_data != NULL) {
555 if (gps_nmea_data != NULL) {
557 gps_nmea_data = NULL;
559 if (gps_last_pos != NULL) {
565 static void _report_pos_event(gps_event_info_t * gps_event)
567 if (gps_pos_data != NULL) {
568 memcpy(gps_pos_data, &(gps_event->event_data.pos_ind.pos), sizeof(pos_data_t));
569 memcpy(gps_last_pos, gps_pos_data, sizeof(pos_data_t));
570 g_update_cb.pos_cb(gps_pos_data, gps_event->event_data.pos_ind.error, g_user_data);
571 gps_manager_set_last_position(gps_last_pos);
572 memset(gps_pos_data, 0x00, sizeof(pos_data_t));
574 LOG_GPS(DBG_ERR, "gps_pos_data is NULL");
578 static void _report_sv_event(gps_event_info_t * gps_event)
580 if (gps_sv_data != NULL) {
581 memcpy(gps_sv_data, &(gps_event->event_data.sv_ind.sv), sizeof(sv_data_t));
582 g_update_cb.sv_cb(gps_sv_data, g_user_data);
583 memset(gps_sv_data, 0x00, sizeof(sv_data_t));
585 LOG_GPS(DBG_ERR, "gps_sv_data is NULL");
589 static void _report_nmea_event(gps_event_info_t * gps_event)
591 if (gps_nmea_data == NULL) {
592 LOG_GPS(DBG_ERR, "gps_nmea_data is NULL");
596 gps_nmea_data->len = gps_event->event_data.nmea_ind.nmea.len;
597 LOG_GPS(DBG_LOW, "gps_nmea_data->len : [%d]", gps_nmea_data->len);
598 gps_nmea_data->data = (char *)malloc(gps_nmea_data->len);
599 memset(gps_nmea_data->data, 0x00, gps_nmea_data->len);
600 memcpy(gps_nmea_data->data, gps_event->event_data.nmea_ind.nmea.data, gps_nmea_data->len);
601 g_update_cb.nmea_cb(gps_nmea_data, g_user_data);
602 free(gps_nmea_data->data);
603 memset(gps_nmea_data, 0x00, sizeof(nmea_data_t));
605 if (logging_enabled) {
608 nmea_len = gps_event->event_data.nmea_ind.nmea.len;
609 p_nmea_data = gps_event->event_data.nmea_ind.nmea.data;
610 write_nmea_log(p_nmea_data, nmea_len);
614 static int _gps_server_open_data_connection()
616 LOG_GPS(DBG_LOW, "Enter _gps_server_open_data_connection");
620 if (g_dnet_used > 1) {
621 LOG_GPS(DBG_LOW, "g_dnet_used : [ %d ]", g_dnet_used);
624 LOG_GPS(DBG_LOW, "First open the data connection");
627 unsigned char result;
629 result = start_pdp_connection();
631 if (result == TRUE) {
632 LOG_GPS(DBG_LOW, "//Open PDP Conn for SUPL Success.");
634 LOG_GPS(DBG_ERR, "//Open PDP Conn for SUPL Fail.");
640 static int _gps_server_close_data_connection()
642 LOG_GPS(DBG_LOW, "Enter _gps_server_close_data_connection");
644 if (g_dnet_used > 0) {
648 if (g_dnet_used != 0) {
649 LOG_GPS(DBG_LOW, "Cannot stop the data connection! [ g_dnet_used : %d ]", g_dnet_used);
652 LOG_GPS(DBG_LOW, "Close the data connection");
655 unsigned char result;
657 result = stop_pdp_connection();
658 if (result == TRUE) {
659 LOG_GPS(DBG_LOW, "Close PDP Conn for SUPL Success.");
661 LOG_GPS(DBG_ERR, "//Close PDP Conn for SUPL Fail.");
668 static int _gps_server_resolve_dns(char *domain)
670 LOG_GPS(DBG_LOW, "Enter _gps_server_resolve_dns");
672 unsigned char result;
673 gps_failure_reason_t reason_code = GPS_FAILURE_CAUSE_NORMAL;
677 result = query_dns(domain, &ipaddr, &port);
678 if (result == TRUE) {
679 LOG_GPS(DBG_LOW, "Success to resolve domain name [ %s ] / ipaddr [ %u ]", domain, ipaddr);
680 get_plugin_module()->request(GPS_INDI_SUPL_DNSQUERY, (void *)(&ipaddr), &reason_code);
683 LOG_GPS(DBG_ERR, "Fail to resolve domain name [ %s ] / ipaddr [ %u ]", domain, ipaddr);
684 get_plugin_module()->request(GPS_INDI_SUPL_DNSQUERY, (void *)(&ipaddr), &reason_code);
691 static void _gps_server_send_facttest_result(double snr_data, int prn, unsigned short result)
695 static int _gps_server_gps_event_cb(gps_event_info_t * gps_event_info)
697 FUNC_ENTRANCE_SERVER;
699 if (gps_event_info == NULL) {
700 LOG_GPS(DBG_WARN, "//NULL pointer variable passed");
704 switch (gps_event_info->event_id) {
705 case GPS_EVENT_START_SESSION:
706 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_START_SESSION :::::::::::::::");
707 if (gps_event_info->event_data.start_session_rsp.error == GPS_ERR_NONE) {
708 _gps_server_start_event();
710 LOG_GPS(DBG_ERR, "//Start Session Failed, error : %d",
711 gps_event_info->event_data.start_session_rsp.error);
714 case GPS_EVENT_SET_OPTION:
716 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_SET_OPTION :::::::::::::::");
717 if (gps_event_info->event_data.set_option_rsp.error != GPS_ERR_NONE) {
718 LOG_GPS(DBG_ERR, "//Set Option Failed, error : %d",
719 gps_event_info->event_data.set_option_rsp.error);
724 case GPS_EVENT_STOP_SESSION:
725 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_STOP_SESSION :::::::::::::::");
726 if (gps_event_info->event_data.stop_session_rsp.error == GPS_ERR_NONE) {
727 _gps_server_close_data_connection();
728 _gps_server_stop_event();
730 LOG_GPS(DBG_ERR, "//Stop Session Failed, error : %d",
731 gps_event_info->event_data.stop_session_rsp.error);
735 case GPS_EVENT_REPORT_POSITION:
736 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_POSITION :::::::::::::::");
737 if (gps_event_info->event_data.pos_ind.error == GPS_ERR_NONE) {
738 _report_pos_event(gps_event_info);
740 LOG_GPS(DBG_ERR, "GPS_EVENT_POSITION Failed, error : %d", gps_event_info->event_data.pos_ind.error);
743 case GPS_EVENT_REPORT_SATELLITE:
744 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_SATELLITE :::::::::::::::");
745 if (gps_event_info->event_data.sv_ind.error == GPS_ERR_NONE) {
746 if (gps_event_info->event_data.sv_ind.sv.pos_valid) {
747 _gps_server_set_gps_state(POSITION_CONNECTED);
749 _gps_server_set_gps_state(POSITION_SEARCHING);
751 _report_sv_event(gps_event_info);
753 LOG_GPS(DBG_ERR, "GPS_EVENT_SATELLITE Failed, error : %d", gps_event_info->event_data.sv_ind.error);
756 case GPS_EVENT_REPORT_NMEA:
757 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_NMEA :::::::::::::::");
758 if (gps_event_info->event_data.nmea_ind.error == GPS_ERR_NONE) {
759 _report_nmea_event(gps_event_info);
761 LOG_GPS(DBG_ERR, "GPS_EVENT_NMEA Failed, error : %d", gps_event_info->event_data.nmea_ind.error);
764 case GPS_EVENT_ERR_CAUSE:
766 case GPS_EVENT_AGPS_VERIFICATION_INDI:
768 case GPS_EVENT_GET_IMSI:
770 case GPS_EVENT_GET_REF_LOCATION:
772 case GPS_EVENT_OPEN_DATA_CONNECTION:
774 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_OPEN_DATA_CONNECTION :::::::::::::::");
775 result = _gps_server_open_data_connection();
778 case GPS_EVENT_CLOSE_DATA_CONNECTION:
780 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_CLOSE_DATA_CONNECTION :::::::::::::::");
781 result = _gps_server_close_data_connection();
784 case GPS_EVENT_DNS_LOOKUP_IND:
785 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_DNS_LOOKUP_IND :::::::::::::::");
786 if (gps_event_info->event_data.dns_query_ind.error == GPS_ERR_NONE) {
787 result = _gps_server_resolve_dns(gps_event_info->event_data.dns_query_ind.domain_name);
791 if (result == TRUE) {
792 LOG_GPS(DBG_LOW, "Success to get the DNS Query about [ %s ]",
793 gps_event_info->event_data.dns_query_ind.domain_name);
796 case GPS_EVENT_FACTORY_TEST:
797 LOG_GPS(DBG_LOW, "<<::::::::::: GPS_EVENT_FACTORY_TEST :::::::::::::::");
798 if (gps_event_info->event_data.factory_test_rsp.error == GPS_ERR_NONE) {
799 LOG_GPS(DBG_LOW, "[LBS server] Response Factory test result success");
800 _gps_server_send_facttest_result(gps_event_info->event_data.factory_test_rsp.snr,
801 gps_event_info->event_data.factory_test_rsp.prn, TRUE);
803 LOG_GPS(DBG_ERR, "//[LBS server] Response Factory test result ERROR");
804 _gps_server_send_facttest_result(gps_event_info->event_data.factory_test_rsp.snr,
805 gps_event_info->event_data.factory_test_rsp.prn, FALSE);
809 LOG_GPS(DBG_WARN, "//Error: Isettingalid Event Type %d", gps_event_info->event_id);
815 static void *_gps_launch_popup(void *data)
817 gps_ni_popup_data_t *msgData = (gps_ni_popup_data_t *) data;
822 gps_failure_reason_t reason_code;
824 // Max size of SMS message is 255
825 // Max size of WAP PUSH is ??????
826 snprintf(temp, sizeof(temp), "%d", msgData->msg_size);
828 argv[0] = GPS_NI_POPUP;
829 if (msgData->num_btn == 2)
830 argv[1] = GPS_NI_BTN;
832 argv[1] = GPS_NI_WITHOUT_BTN;
834 argv[3] = msgData->msg_body;
837 if (msgData->num_btn == 2) {
839 if (status != YES_EXIT_STATUS && status != NO_EXIT_STATUS)
840 status = YES_EXIT_STATUS;
841 status = status / 256;
843 // Popup application Timer is 2 sec
845 status = YES_EXIT_STATUS;
848 status = NO_EXIT_STATUS - status;
849 LOG_GPS(DBG_LOW, "EXIT_STATUS from the LBS Popup is [ %d ]", status);
851 agps_supl_ni_info_t info;
852 info.msg_body = (char *)msgData->msg_body;
853 info.msg_size = msgData->msg_size;
854 info.status = status;
856 if (!get_plugin_module()->request(GPS_ACTION_REQUEST_SUPL_NI, &info, &reason_code)) {
857 LOG_GPS(DBG_ERR, "Failed to request SUPL NI (code:%d)", reason_code);
864 static void _gps_supl_networkinit_smscb(msg_handle_t hMsgHandle, msg_struct_t msg, void *user_param)
866 LOG_GPS(DBG_ERR, "_gps_supl_networkinit_smscb is called");
868 gps_ni_popup_data_t new_message;
869 memset(&new_message, 0x00, sizeof(new_message));
871 msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &new_message.msg_size);
872 msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, new_message.msg_body, new_message.msg_size);
874 // TODO: Button number of LBS Popup
875 new_message.num_btn = 2;
878 pthread_attr_init(&attr);
879 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
881 if (pthread_create(&popup_thread, &attr, _gps_launch_popup, &new_message) != 0) {
882 LOG_GPS(DBG_WARN, "Can not make pthread......");
887 static void _gps_supl_networkinit_wappushcb(msg_handle_t hMsgHandle, const char *pPushHeader, const char *pPushBody,
888 int pushBodyLen, void *user_param)
890 LOG_GPS(DBG_ERR, "_gps_supl_networkinit_wappushcb is called");
891 LOG_GPS(DBG_ERR, "SUPLNI WAPPush MSG size is [ %d ]", pushBodyLen);
893 gps_ni_popup_data_t new_message;
894 memset(&new_message, 0x00, sizeof(new_message));
896 new_message.msg_body = (char *)pPushBody;
897 new_message.msg_size = pushBodyLen;
898 // TODO: Button number of LBS Popup
899 new_message.num_btn = 2;
902 pthread_attr_init(&attr);
903 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
905 if (pthread_create(&popup_thread, &attr, _gps_launch_popup, &new_message) != 0) {
906 LOG_GPS(DBG_WARN, "Can not make pthread......");
911 static void *_gps_register_msgfwcb()
913 msg_handle_t msgHandle = NULL;
914 msg_error_t err = MSG_SUCCESS;
922 ret = vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &setValue);
925 LOG_GPS(DBG_WARN, "Fail to get VCONFKEY_MSG_SERVER_READY");
930 LOG_GPS(DBG_LOW, "MSG server is READY!!");
936 LOG_GPS(DBG_WARN, "Never connect to MSG Server for 300 secs.");
942 err = msg_open_msg_handle(&msgHandle);
944 if (err != MSG_SUCCESS) {
945 LOG_GPS(DBG_WARN, "Fail to MsgOpenMsgHandle. Error type = [ %d ]", err);
949 err = msg_reg_sms_message_callback(msgHandle, &_gps_supl_networkinit_smscb, 7275, NULL);
951 if (err != MSG_SUCCESS) {
952 LOG_GPS(DBG_WARN, "Fail to MsgRegSmsMessageCallback. Error type = [ %d ]", err);
956 err = msg_reg_lbs_message_callback(msgHandle, &_gps_supl_networkinit_wappushcb, NULL);
958 if (err != MSG_SUCCESS) {
959 LOG_GPS(DBG_WARN, "Fail to MsgRegLBSMessageCallback. Error type = [ %d ]", err);
963 LOG_GPS(DBG_LOW, "Success to lbs_register_msgfwcb");
968 static void _gps_hibernation_enter_callback(void *data)
970 LOG_GPS(DBG_LOW, "Enter the _gps_hibernation_enter_callback");
972 if (msg_thread != 0) {
973 pthread_cancel(msg_thread);
975 pthread_join(msg_thread, (void *)&msg_thread_status);
980 static void _gps_hibernation_leave_callback(void *data)
982 LOG_GPS(DBG_LOW, "Enter the _gps_hibernation_leave_callback");
984 if (msg_thread != 0) {
985 pthread_cancel(msg_thread);
987 pthread_join(msg_thread, (void *)&msg_thread_status);
990 if (pthread_create(&msg_thread, NULL, _gps_register_msgfwcb, NULL) != 0) {
991 LOG_GPS(DBG_WARN, "Can not make pthread......");
995 static void _print_help()
997 printf("Usage: gps-manager [OPTION] [NAME]\n"
999 " -l [NAME] Load a specific plugin module\n");
1003 static int _parse_argument(const int argc, char **argv, const int buffer_size, char *out_buffer)
1007 memset(out_buffer, 0x00, buffer_size);
1008 while ((opt = getopt(argc, argv, "hl:")) != -1) {
1011 snprintf(out_buffer, buffer_size, "%s", optarg);
1023 int initialize_server(int argc, char **argv)
1025 char module_name[16];
1027 _parse_argument(argc, argv, sizeof(module_name), module_name);
1029 FUNC_ENTRANCE_SERVER;
1031 _gps_plugin_handler_init(module_name);
1033 _gps_notify_params();
1035 if (!load_plugin_module(g_gps_plugin.name, &g_gps_plugin.handle)) {
1036 LOG_GPS(DBG_ERR, "Fail to load plugin module.");
1040 if (!get_plugin_module()->init(_gps_server_gps_event_cb, &g_gps_params)) {
1041 LOG_GPS(DBG_WARN, "Fail to gps-manager module initialization");
1045 // Register SUPL NI cb to MSG server
1046 if (pthread_create(&msg_thread, NULL, _gps_register_msgfwcb, NULL) != 0) {
1047 LOG_GPS(DBG_WARN, "Can not make pthread......");
1049 // Register Hibernation cb to re-connect msg-server
1051 notifd = heynoti_init();
1052 heynoti_subscribe(notifd, "HIBERNATION_ENTER", _gps_hibernation_enter_callback, NULL);
1053 heynoti_subscribe(notifd, "HIBERNATION_LEAVE", _gps_hibernation_leave_callback, NULL);
1054 heynoti_attach_handler(notifd);
1056 LOG_GPS(DBG_LOW, "Initialization is completed.");
1061 int deinitialize_server()
1063 gps_failure_reason_t ReasonCode = GPS_FAILURE_CAUSE_NORMAL;
1065 // Wait termination of msg thread
1066 pthread_join(msg_thread, (void *)&msg_thread_status);
1068 _gps_ignore_params();
1070 if (!get_plugin_module()->deinit(&ReasonCode)) {
1071 LOG_GPS(DBG_WARN, "Fail to gps-manager module de-initialization");
1073 unload_plugin_module(g_gps_plugin.handle);
1075 _gps_plugin_handler_deinit();
1080 int register_update_callbacks(struct gps_callbacks *gps_callback, void *user_data)
1082 g_update_cb = *gps_callback;
1083 g_user_data = user_data;