1 /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation, nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #define LOG_NDDEBUG 0
31 #define LOG_TAG "LocSvc_afw"
32 
33 #include <hardware/gps.h>
34 #include <gps_extended.h>
35 #include <loc_eng.h>
36 #include <loc_target.h>
37 #include <loc_log.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <dlfcn.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <LocDualContext.h>
46 #include <cutils/properties.h>
47 
48 using namespace loc_core;
49 
50 //Globals defns
51 static gps_location_callback gps_loc_cb = NULL;
52 static gps_sv_status_callback gps_sv_cb = NULL;
53 static agps_status_callback agps_status_cb = NULL;
54 
55 static void local_loc_cb(UlpLocation* location, void* locExt);
56 static void local_sv_cb(GpsSvStatus* sv_status, void* svExt);
57 static void loc_agps_status_cb(AGpsStatus* status);
58 
59 static const GpsGeofencingInterface* get_geofence_interface(void);
60 
61 // Function declarations for sLocEngInterface
62 static int  loc_init(GpsCallbacks* callbacks);
63 static int  loc_start();
64 static int  loc_stop();
65 static void loc_cleanup();
66 static int  loc_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty);
67 static int  loc_inject_location(double latitude, double longitude, float accuracy);
68 static void loc_delete_aiding_data(GpsAidingData f);
69 static int  loc_set_position_mode(GpsPositionMode mode, GpsPositionRecurrence recurrence,
70                                   uint32_t min_interval, uint32_t preferred_accuracy,
71                                   uint32_t preferred_time);
72 static const void* loc_get_extension(const char* name);
73 static void loc_close_mdm_node();
74 // Defines the GpsInterface in gps.h
75 static const GpsInterface sLocEngInterface =
76 {
77    sizeof(GpsInterface),
78    loc_init,
79    loc_start,
80    loc_stop,
81    loc_cleanup,
82    loc_inject_time,
83    loc_inject_location,
84    loc_delete_aiding_data,
85    loc_set_position_mode,
86    loc_get_extension
87 };
88 
89 // Function declarations for sLocEngAGpsInterface
90 static void loc_agps_init(AGpsCallbacks* callbacks);
91 static int  loc_agps_open(const char* apn);
92 static int  loc_agps_closed();
93 static int  loc_agps_open_failed();
94 static int  loc_agps_set_server(AGpsType type, const char *hostname, int port);
95 static int  loc_agps_open_with_apniptype( const char* apn, ApnIpType apnIpType);
96 
97 static const AGpsInterface sLocEngAGpsInterface =
98 {
99    sizeof(AGpsInterface),
100    loc_agps_init,
101    loc_agps_open,
102    loc_agps_closed,
103    loc_agps_open_failed,
104    loc_agps_set_server,
105    loc_agps_open_with_apniptype
106 };
107 
108 static int loc_xtra_init(GpsXtraCallbacks* callbacks);
109 static int loc_xtra_inject_data(char* data, int length);
110 
111 static const GpsXtraInterface sLocEngXTRAInterface =
112 {
113     sizeof(GpsXtraInterface),
114     loc_xtra_init,
115     loc_xtra_inject_data
116 };
117 
118 static void loc_ni_init(GpsNiCallbacks *callbacks);
119 static void loc_ni_respond(int notif_id, GpsUserResponseType user_response);
120 
121 static const GpsNiInterface sLocEngNiInterface =
122 {
123    sizeof(GpsNiInterface),
124    loc_ni_init,
125    loc_ni_respond,
126 };
127 
128 // For shutting down MDM in fusion devices
129 static int mdm_fd = -1;
130 static int loc_gps_measurement_init(GpsMeasurementCallbacks* callbacks);
131 static void loc_gps_measurement_close();
132 
133 static const GpsMeasurementInterface sLocEngGpsMeasurementInterface =
134 {
135     sizeof(GpsMeasurementInterface),
136     loc_gps_measurement_init,
137     loc_gps_measurement_close
138 };
139 
140 static void loc_agps_ril_init( AGpsRilCallbacks* callbacks );
141 static void loc_agps_ril_set_ref_location(const AGpsRefLocation *agps_reflocation, size_t sz_struct);
142 static void loc_agps_ril_set_set_id(AGpsSetIDType type, const char* setid);
143 static void loc_agps_ril_ni_message(uint8_t *msg, size_t len);
144 static void loc_agps_ril_update_network_state(int connected, int type, int roaming, const char* extra_info);
145 static void loc_agps_ril_update_network_availability(int avaiable, const char* apn);
146 
147 static const AGpsRilInterface sLocEngAGpsRilInterface =
148 {
149    sizeof(AGpsRilInterface),
150    loc_agps_ril_init,
151    loc_agps_ril_set_ref_location,
152    loc_agps_ril_set_set_id,
153    loc_agps_ril_ni_message,
154    loc_agps_ril_update_network_state,
155    loc_agps_ril_update_network_availability
156 };
157 
158 static int loc_agps_install_certificates(const DerEncodedCertificate* certificates,
159                                          size_t length);
160 static int loc_agps_revoke_certificates(const Sha1CertificateFingerprint* fingerprints,
161                                         size_t length);
162 
163 static const SuplCertificateInterface sLocEngAGpsCertInterface =
164 {
165     sizeof(SuplCertificateInterface),
166     loc_agps_install_certificates,
167     loc_agps_revoke_certificates
168 };
169 
170 static void loc_configuration_update(const char* config_data, int32_t length);
171 
172 static const GnssConfigurationInterface sLocEngConfigInterface =
173 {
174     sizeof(GnssConfigurationInterface),
175     loc_configuration_update
176 };
177 
178 static loc_eng_data_s_type loc_afw_data;
179 static int gss_fd = -1;
180 
181 /*===========================================================================
182 FUNCTION    gps_get_hardware_interface
183 
184 DESCRIPTION
185    Returns the GPS hardware interaface based on LOC API
186    if GPS is enabled.
187 
188 DEPENDENCIES
189    None
190 
191 RETURN VALUE
192    0: success
193 
194 SIDE EFFECTS
195    N/A
196 
197 ===========================================================================*/
gps_get_hardware_interface()198 const GpsInterface* gps_get_hardware_interface ()
199 {
200     ENTRY_LOG_CALLFLOW();
201     const GpsInterface* ret_val;
202 
203     char propBuf[PROPERTY_VALUE_MAX];
204 
205     loc_eng_read_config();
206 
207     // check to see if GPS should be disabled
208     property_get("gps.disable", propBuf, "");
209     if (propBuf[0] == '1')
210     {
211         LOC_LOGD("gps_get_interface returning NULL because gps.disable=1\n");
212         ret_val = NULL;
213     } else {
214         ret_val = &sLocEngInterface;
215     }
216 
217     loc_eng_read_config();
218 
219     EXIT_LOG(%p, ret_val);
220     return ret_val;
221 }
222 
223 // for gps.c
get_gps_interface()224 extern "C" const GpsInterface* get_gps_interface()
225 {
226     unsigned int target = TARGET_DEFAULT;
227     loc_eng_read_config();
228 
229     target = loc_get_target();
230     LOC_LOGD("Target name check returned %s", loc_get_target_name(target));
231 
232     int gnssType = getTargetGnssType(target);
233     switch (gnssType)
234     {
235     case GNSS_GSS:
236         //APQ8064
237         gps_conf.CAPABILITIES &= ~(GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
238         gss_fd = open("/dev/gss", O_RDONLY);
239         if (gss_fd < 0) {
240             LOC_LOGE("GSS open failed: %s\n", strerror(errno));
241         }
242         else {
243             LOC_LOGD("GSS open success! CAPABILITIES %0lx\n",
244                      gps_conf.CAPABILITIES);
245         }
246         break;
247     case GNSS_NONE:
248         //MPQ8064
249         LOC_LOGE("No GPS HW on this target. Not returning interface.");
250         return NULL;
251     case GNSS_QCA1530:
252         // qca1530 chip is present
253         gps_conf.CAPABILITIES &= ~(GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
254         LOC_LOGD("qca1530 present: CAPABILITIES %0lx\n", gps_conf.CAPABILITIES);
255         break;
256     }
257     return &sLocEngInterface;
258 }
259 
260 /*===========================================================================
261 FUNCTION    loc_init
262 
263 DESCRIPTION
264    Initialize the location engine, this include setting up global datas
265    and registers location engien with loc api service.
266 
267 DEPENDENCIES
268    None
269 
270 RETURN VALUE
271    0: success
272 
273 SIDE EFFECTS
274    N/Ax
275 
276 ===========================================================================*/
loc_init(GpsCallbacks * callbacks)277 static int loc_init(GpsCallbacks* callbacks)
278 {
279     int retVal = -1;
280     int i = 0;
281     ENTRY_LOG();
282     LOC_API_ADAPTER_EVENT_MASK_T event;
283 
284     if (NULL == callbacks) {
285         LOC_LOGE("loc_init failed. cb = NULL\n");
286         EXIT_LOG(%d, retVal);
287         return retVal;
288     }
289 
290     event = LOC_API_ADAPTER_BIT_PARSED_POSITION_REPORT |
291             LOC_API_ADAPTER_BIT_SATELLITE_REPORT |
292             LOC_API_ADAPTER_BIT_LOCATION_SERVER_REQUEST |
293             LOC_API_ADAPTER_BIT_ASSISTANCE_DATA_REQUEST |
294             LOC_API_ADAPTER_BIT_IOCTL_REPORT |
295             LOC_API_ADAPTER_BIT_STATUS_REPORT |
296             LOC_API_ADAPTER_BIT_NMEA_1HZ_REPORT |
297             LOC_API_ADAPTER_BIT_NI_NOTIFY_VERIFY_REQUEST;
298 
299     LocCallbacks clientCallbacks = {local_loc_cb, /* location_cb */
300                                     callbacks->status_cb, /* status_cb */
301                                     local_sv_cb, /* sv_status_cb */
302                                     callbacks->nmea_cb, /* nmea_cb */
303                                     callbacks->set_capabilities_cb, /* set_capabilities_cb */
304                                     callbacks->acquire_wakelock_cb, /* acquire_wakelock_cb */
305                                     callbacks->release_wakelock_cb, /* release_wakelock_cb */
306                                     callbacks->create_thread_cb, /* create_thread_cb */
307                                     NULL, /* location_ext_parser */
308                                     NULL, /* sv_ext_parser */
309                                     callbacks->request_utc_time_cb, /* request_utc_time_cb */
310                                     loc_close_mdm_node  /*loc_shutdown_cb*/};
311 
312     gps_loc_cb = callbacks->location_cb;
313     gps_sv_cb = callbacks->sv_status_cb;
314 
315     retVal = loc_eng_init(loc_afw_data, &clientCallbacks, event, NULL);
316     loc_afw_data.adapter->mSupportsAgpsRequests = !loc_afw_data.adapter->hasAgpsExtendedCapabilities();
317     loc_afw_data.adapter->mSupportsPositionInjection = !loc_afw_data.adapter->hasCPIExtendedCapabilities();
318     loc_afw_data.adapter->setGpsLockMsg(0);
319     loc_afw_data.adapter->requestUlp(gps_conf.CAPABILITIES);
320 
321     if(retVal) {
322         LOC_LOGE("loc_eng_init() fail!");
323         goto err;
324     }
325 
326     loc_afw_data.adapter->setPowerVoteRight(loc_get_target() == TARGET_QCA1530);
327     loc_afw_data.adapter->setPowerVote(true);
328 
329     LOC_LOGD("loc_eng_init() success!");
330 
331 #ifdef PLATFORM_MSM8084
332     if (mdm_fd < 0) {
333         int (*open_first_external_modem)(void);
334         const char *name = "libdetectmodem.so";
335         const char *func = "open_first_external_modem";
336         const char *error;
337 
338         void *lib = ::dlopen(name, RTLD_NOW);
339         error = ::dlerror();
340         if (!lib) {
341             LOC_LOGE("%s: could not find %s: %s", __func__, name, error);
342             goto err;
343         }
344 
345         open_first_external_modem = NULL;
346         *(void **)(&open_first_external_modem) = ::dlsym(lib, func);
347         error = ::dlerror();
348 
349         if (!open_first_external_modem) {
350             LOC_LOGE("%s: could not find symbol %s in %s: %s",
351                      __func__, func, name, error);
352         }
353         else {
354             errno = 0;
355             mdm_fd = open_first_external_modem();
356             if (mdm_fd < 0) {
357                 LOC_LOGE("%s: %s failed: %s\n", __func__, func, strerror(errno));
358             }
359             else {
360                 LOC_LOGD("%s: external power up modem opened successfully\n", __func__);
361             }
362         }
363         dlclose(lib);
364     } else {
365         LOC_LOGD("powerup_node has been opened before");
366     }
367 #endif //PLATFORM_MSM8084
368 err:
369     EXIT_LOG(%d, retVal);
370     return retVal;
371 }
372 
373 /*===========================================================================
374 FUNCTION    loc_close_mdm_node
375 
376 DESCRIPTION
377    closes mdm_fd which is the modem powerup node obtained in loc_init
378 
379 DEPENDENCIES
380    None
381 
382 RETURN VALUE
383    None
384 
385 SIDE EFFECTS
386    N/A
387 
388 ===========================================================================*/
loc_close_mdm_node()389 static void loc_close_mdm_node()
390 {
391     ENTRY_LOG();
392     if (mdm_fd >= 0) {
393         LOC_LOGD("closing the powerup node");
394         close(mdm_fd);
395         mdm_fd = -1;
396         LOC_LOGD("finished closing the powerup node");
397     } else {
398         LOC_LOGD("powerup node has not been opened yet.");
399     }
400 
401     EXIT_LOG(%s, VOID_RET);
402 }
403 
404 /*===========================================================================
405 FUNCTION    loc_cleanup
406 
407 DESCRIPTION
408    Cleans location engine. The location client handle will be released.
409 
410 DEPENDENCIES
411    None
412 
413 RETURN VALUE
414    None
415 
416 SIDE EFFECTS
417    N/A
418 
419 ===========================================================================*/
loc_cleanup()420 static void loc_cleanup()
421 {
422     ENTRY_LOG();
423 
424     loc_afw_data.adapter->setPowerVote(false);
425     loc_afw_data.adapter->setGpsLockMsg(gps_conf.GPS_LOCK);
426 
427     loc_eng_cleanup(loc_afw_data);
428     loc_close_mdm_node();
429     gps_loc_cb = NULL;
430     gps_sv_cb = NULL;
431 
432     EXIT_LOG(%s, VOID_RET);
433 }
434 
435 /*===========================================================================
436 FUNCTION    loc_start
437 
438 DESCRIPTION
439    Starts the tracking session
440 
441 DEPENDENCIES
442    None
443 
444 RETURN VALUE
445    0: success
446 
447 SIDE EFFECTS
448    N/A
449 
450 ===========================================================================*/
loc_start()451 static int loc_start()
452 {
453     ENTRY_LOG();
454     int ret_val = loc_eng_start(loc_afw_data);
455 
456     EXIT_LOG(%d, ret_val);
457     return ret_val;
458 }
459 
460 /*===========================================================================
461 FUNCTION    loc_stop
462 
463 DESCRIPTION
464    Stops the tracking session
465 
466 DEPENDENCIES
467    None
468 
469 RETURN VALUE
470    0: success
471 
472 SIDE EFFECTS
473    N/A
474 
475 ===========================================================================*/
loc_stop()476 static int loc_stop()
477 {
478     ENTRY_LOG();
479     int ret_val = -1;
480     ret_val = loc_eng_stop(loc_afw_data);
481 
482     EXIT_LOG(%d, ret_val);
483     return ret_val;
484 }
485 
486 /*===========================================================================
487 FUNCTION    loc_set_position_mode
488 
489 DESCRIPTION
490    Sets the mode and fix frequency for the tracking session.
491 
492 DEPENDENCIES
493    None
494 
495 RETURN VALUE
496    0: success
497 
498 SIDE EFFECTS
499    N/A
500 
501 ===========================================================================*/
loc_set_position_mode(GpsPositionMode mode,GpsPositionRecurrence recurrence,uint32_t min_interval,uint32_t preferred_accuracy,uint32_t preferred_time)502 static int  loc_set_position_mode(GpsPositionMode mode,
503                                   GpsPositionRecurrence recurrence,
504                                   uint32_t min_interval,
505                                   uint32_t preferred_accuracy,
506                                   uint32_t preferred_time)
507 {
508     ENTRY_LOG();
509     int ret_val = -1;
510     LocPositionMode locMode;
511     switch (mode) {
512     case GPS_POSITION_MODE_MS_BASED:
513         locMode = LOC_POSITION_MODE_MS_BASED;
514         break;
515     case GPS_POSITION_MODE_MS_ASSISTED:
516         locMode = LOC_POSITION_MODE_MS_ASSISTED;
517         break;
518     default:
519         locMode = LOC_POSITION_MODE_STANDALONE;
520         break;
521     }
522 
523     LocPosMode params(locMode, recurrence, min_interval,
524                       preferred_accuracy, preferred_time, NULL, NULL);
525     ret_val = loc_eng_set_position_mode(loc_afw_data, params);
526 
527     EXIT_LOG(%d, ret_val);
528     return ret_val;
529 }
530 
531 /*===========================================================================
532 FUNCTION    loc_inject_time
533 
534 DESCRIPTION
535    This is used by Java native function to do time injection.
536 
537 DEPENDENCIES
538    None
539 
540 RETURN VALUE
541    0
542 
543 SIDE EFFECTS
544    N/A
545 
546 ===========================================================================*/
loc_inject_time(GpsUtcTime time,int64_t timeReference,int uncertainty)547 static int loc_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty)
548 {
549     ENTRY_LOG();
550     int ret_val = 0;
551 
552     ret_val = loc_eng_inject_time(loc_afw_data, time,
553                                   timeReference, uncertainty);
554 
555     EXIT_LOG(%d, ret_val);
556     return ret_val;
557 }
558 
559 
560 /*===========================================================================
561 FUNCTION    loc_inject_location
562 
563 DESCRIPTION
564    This is used by Java native function to do location injection.
565 
566 DEPENDENCIES
567    None
568 
569 RETURN VALUE
570    0          : Successful
571    error code : Failure
572 
573 SIDE EFFECTS
574    N/A
575 ===========================================================================*/
loc_inject_location(double latitude,double longitude,float accuracy)576 static int loc_inject_location(double latitude, double longitude, float accuracy)
577 {
578     static bool initialized = false;
579     static bool enable_cpi = true;
580     ENTRY_LOG();
581 
582     if (accuracy < 1000)
583     {
584       accuracy = 1000;
585     }
586 
587     int ret_val = 0;
588     ret_val = loc_eng_inject_location(loc_afw_data, latitude, longitude, accuracy);
589 
590     if(!initialized)
591     {
592         char value[PROPERTY_VALUE_MAX];
593         memset(value, 0, sizeof(value));
594         (void)property_get("persist.gps.qc_nlp_in_use", value, "0");
595         if(0 == strcmp(value, "1"))
596         {
597             enable_cpi = false;
598             LOC_LOGI("GPS HAL coarse position injection disabled");
599         }
600         else
601         {
602             LOC_LOGI("GPS HAL coarse position injection enabled");
603         }
604         initialized = true;
605     }
606 
607     if(enable_cpi)
608     {
609       ret_val = loc_eng_inject_location(loc_afw_data, latitude, longitude, accuracy);
610     }
611     EXIT_LOG(%d, ret_val);
612     return ret_val;
613 }
614 
615 
616 /*===========================================================================
617 FUNCTION    loc_delete_aiding_data
618 
619 DESCRIPTION
620    This is used by Java native function to delete the aiding data. The function
621    updates the global variable for the aiding data to be deleted. If the GPS
622    engine is off, the aiding data will be deleted. Otherwise, the actual action
623    will happen when gps engine is turned off.
624 
625 DEPENDENCIES
626    Assumes the aiding data type specified in GpsAidingData matches with
627    LOC API specification.
628 
629 RETURN VALUE
630    None
631 
632 SIDE EFFECTS
633    N/A
634 
635 ===========================================================================*/
loc_delete_aiding_data(GpsAidingData f)636 static void loc_delete_aiding_data(GpsAidingData f)
637 {
638     ENTRY_LOG();
639     loc_eng_delete_aiding_data(loc_afw_data, f);
640 
641     EXIT_LOG(%s, VOID_RET);
642 }
643 
get_geofence_interface(void)644 const GpsGeofencingInterface* get_geofence_interface(void)
645 {
646     ENTRY_LOG();
647     void *handle;
648     const char *error;
649     typedef const GpsGeofencingInterface* (*get_gps_geofence_interface_function) (void);
650     get_gps_geofence_interface_function get_gps_geofence_interface;
651     static const GpsGeofencingInterface* geofence_interface = NULL;
652 
653     dlerror();    /* Clear any existing error */
654 
655     handle = dlopen ("libgeofence.so", RTLD_NOW);
656 
657     if (!handle)
658     {
659         if ((error = dlerror()) != NULL)  {
660             LOC_LOGE ("%s, dlopen for libgeofence.so failed, error = %s\n", __func__, error);
661            }
662         goto exit;
663     }
664     dlerror();    /* Clear any existing error */
665     get_gps_geofence_interface = (get_gps_geofence_interface_function)dlsym(handle, "gps_geofence_get_interface");
666     if ((error = dlerror()) != NULL)  {
667         LOC_LOGE ("%s, dlsym for get_gps_geofence_interface failed, error = %s\n", __func__, error);
668         goto exit;
669      }
670 
671     geofence_interface = get_gps_geofence_interface();
672 
673 exit:
674     EXIT_LOG(%d, geofence_interface == NULL);
675     return geofence_interface;
676 }
677 /*===========================================================================
678 FUNCTION    loc_get_extension
679 
680 DESCRIPTION
681    Get the gps extension to support XTRA.
682 
683 DEPENDENCIES
684    N/A
685 
686 RETURN VALUE
687    The GPS extension interface.
688 
689 SIDE EFFECTS
690    N/A
691 
692 ===========================================================================*/
loc_get_extension(const char * name)693 const void* loc_get_extension(const char* name)
694 {
695     ENTRY_LOG();
696     const void* ret_val = NULL;
697 
698    LOC_LOGD("%s:%d] For Interface = %s\n",__func__, __LINE__, name);
699    if (strcmp(name, GPS_XTRA_INTERFACE) == 0)
700    {
701        ret_val = &sLocEngXTRAInterface;
702    }
703    else if (strcmp(name, AGPS_INTERFACE) == 0)
704    {
705        ret_val = &sLocEngAGpsInterface;
706    }
707    else if (strcmp(name, GPS_NI_INTERFACE) == 0)
708    {
709        ret_val = &sLocEngNiInterface;
710    }
711    else if (strcmp(name, AGPS_RIL_INTERFACE) == 0)
712    {
713        char baseband[PROPERTY_VALUE_MAX];
714        property_get("ro.baseband", baseband, "msm");
715        if (strcmp(baseband, "csfb") == 0)
716        {
717            ret_val = &sLocEngAGpsRilInterface;
718        }
719    }
720    else if (strcmp(name, GPS_GEOFENCING_INTERFACE) == 0)
721    {
722        if ((gps_conf.CAPABILITIES | GPS_CAPABILITY_GEOFENCING) == gps_conf.CAPABILITIES ){
723            ret_val = get_geofence_interface();
724        }
725    }
726    else if (strcmp(name, SUPL_CERTIFICATE_INTERFACE) == 0)
727    {
728        ret_val = &sLocEngAGpsCertInterface;
729    }
730    else if (strcmp(name, GNSS_CONFIGURATION_INTERFACE) == 0)
731    {
732        ret_val = &sLocEngConfigInterface;
733    }
734    else if (strcmp(name, GPS_MEASUREMENT_INTERFACE) == 0)
735    {
736        ret_val = &sLocEngGpsMeasurementInterface;
737    }
738    else
739    {
740       LOC_LOGE ("get_extension: Invalid interface passed in\n");
741    }
742     EXIT_LOG(%p, ret_val);
743     return ret_val;
744 }
745 
746 /*===========================================================================
747 FUNCTION    loc_agps_init
748 
749 DESCRIPTION
750    Initialize the AGps interface.
751 
752 DEPENDENCIES
753    NONE
754 
755 RETURN VALUE
756    0
757 
758 SIDE EFFECTS
759    N/A
760 
761 ===========================================================================*/
loc_agps_init(AGpsCallbacks * callbacks)762 static void loc_agps_init(AGpsCallbacks* callbacks)
763 {
764     ENTRY_LOG();
765     if (agps_status_cb == NULL) {
766         agps_status_cb = callbacks->status_cb;
767         callbacks->status_cb = loc_agps_status_cb;
768     }
769     loc_eng_agps_init(loc_afw_data, (AGpsExtCallbacks*)callbacks);
770     EXIT_LOG(%s, VOID_RET);
771 }
772 
773 /*===========================================================================
774 FUNCTION    loc_agps_open
775 
776 DESCRIPTION
777    This function is called when on-demand data connection opening is successful.
778 It should inform ARM 9 about the data open result.
779 
780 DEPENDENCIES
781    NONE
782 
783 RETURN VALUE
784    0
785 
786 SIDE EFFECTS
787    N/A
788 
789 ===========================================================================*/
loc_agps_open(const char * apn)790 static int loc_agps_open(const char* apn)
791 {
792     ENTRY_LOG();
793     AGpsType agpsType = AGPS_TYPE_SUPL;
794     AGpsBearerType bearerType = AGPS_APN_BEARER_IPV4;
795     int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);
796 
797     EXIT_LOG(%d, ret_val);
798     return ret_val;
799 }
800 
801 /*===========================================================================
802 FUNCTION    loc_agps_open_with_apniptype
803 
804 DESCRIPTION
805    This function is called when on-demand data connection opening is successful.
806 It should inform ARM 9 about the data open result.
807 
808 DEPENDENCIES
809    NONE
810 
811 RETURN VALUE
812    0
813 
814 SIDE EFFECTS
815    N/A
816 
817 ===========================================================================*/
loc_agps_open_with_apniptype(const char * apn,ApnIpType apnIpType)818 static int  loc_agps_open_with_apniptype(const char* apn, ApnIpType apnIpType)
819 {
820     ENTRY_LOG();
821     AGpsType agpsType = AGPS_TYPE_SUPL;
822     AGpsBearerType bearerType;
823 
824     switch (apnIpType) {
825         case APN_IP_IPV4:
826             bearerType = AGPS_APN_BEARER_IPV4;
827             break;
828         case APN_IP_IPV6:
829             bearerType = AGPS_APN_BEARER_IPV6;
830             break;
831         case APN_IP_IPV4V6:
832             bearerType = AGPS_APN_BEARER_IPV4V6;
833             break;
834         default:
835             bearerType = AGPS_APN_BEARER_IPV4;
836             break;
837     }
838 
839     int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);
840 
841     EXIT_LOG(%d, ret_val);
842     return ret_val;
843 }
844 
845 /*===========================================================================
846 FUNCTION    loc_agps_closed
847 
848 DESCRIPTION
849    This function is called when on-demand data connection closing is done.
850 It should inform ARM 9 about the data close result.
851 
852 DEPENDENCIES
853    NONE
854 
855 RETURN VALUE
856    0
857 
858 SIDE EFFECTS
859    N/A
860 
861 ===========================================================================*/
loc_agps_closed()862 static int loc_agps_closed()
863 {
864     ENTRY_LOG();
865     AGpsType agpsType = AGPS_TYPE_SUPL;
866     int ret_val = loc_eng_agps_closed(loc_afw_data, agpsType);
867 
868     EXIT_LOG(%d, ret_val);
869     return ret_val;
870 }
871 
872 /*===========================================================================
873 FUNCTION    loc_agps_open_failed
874 
875 DESCRIPTION
876    This function is called when on-demand data connection opening has failed.
877 It should inform ARM 9 about the data open result.
878 
879 DEPENDENCIES
880    NONE
881 
882 RETURN VALUE
883    0
884 
885 SIDE EFFECTS
886    N/A
887 
888 ===========================================================================*/
loc_agps_open_failed()889 int loc_agps_open_failed()
890 {
891     ENTRY_LOG();
892     AGpsType agpsType = AGPS_TYPE_SUPL;
893     int ret_val = loc_eng_agps_open_failed(loc_afw_data, agpsType);
894 
895     EXIT_LOG(%d, ret_val);
896     return ret_val;
897 }
898 
899 /*===========================================================================
900 FUNCTION    loc_agps_set_server
901 
902 DESCRIPTION
903    If loc_eng_set_server is called before loc_eng_init, it doesn't work. This
904    proxy buffers server settings and calls loc_eng_set_server when the client is
905    open.
906 
907 DEPENDENCIES
908    NONE
909 
910 RETURN VALUE
911    0
912 
913 SIDE EFFECTS
914    N/A
915 
916 ===========================================================================*/
loc_agps_set_server(AGpsType type,const char * hostname,int port)917 static int loc_agps_set_server(AGpsType type, const char* hostname, int port)
918 {
919     ENTRY_LOG();
920     LocServerType serverType;
921     switch (type) {
922     case AGPS_TYPE_SUPL:
923         serverType = LOC_AGPS_SUPL_SERVER;
924         break;
925     case AGPS_TYPE_C2K:
926         serverType = LOC_AGPS_CDMA_PDE_SERVER;
927         break;
928     default:
929         serverType = LOC_AGPS_SUPL_SERVER;
930     }
931     int ret_val = loc_eng_set_server_proxy(loc_afw_data, serverType, hostname, port);
932 
933     EXIT_LOG(%d, ret_val);
934     return ret_val;
935 }
936 
937 /*===========================================================================
938 FUNCTIONf571
939     loc_xtra_init
940 
941 DESCRIPTION
942    Initialize XTRA module.
943 
944 DEPENDENCIES
945    None
946 
947 RETURN VALUE
948    0: success
949 
950 SIDE EFFECTS
951    N/A
952 
953 ===========================================================================*/
loc_xtra_init(GpsXtraCallbacks * callbacks)954 static int loc_xtra_init(GpsXtraCallbacks* callbacks)
955 {
956     ENTRY_LOG();
957     int ret_val = loc_eng_xtra_init(loc_afw_data, (GpsXtraExtCallbacks*)callbacks);
958 
959     EXIT_LOG(%d, ret_val);
960     return ret_val;
961 }
962 
963 
964 /*===========================================================================
965 FUNCTION    loc_xtra_inject_data
966 
967 DESCRIPTION
968    Initialize XTRA module.
969 
970 DEPENDENCIES
971    None
972 
973 RETURN VALUE
974    0: success
975 
976 SIDE EFFECTS
977    N/A
978 
979 ===========================================================================*/
loc_xtra_inject_data(char * data,int length)980 static int loc_xtra_inject_data(char* data, int length)
981 {
982     ENTRY_LOG();
983     int ret_val = -1;
984     if( (data != NULL) && ((unsigned int)length <= XTRA_DATA_MAX_SIZE))
985         ret_val = loc_eng_xtra_inject_data(loc_afw_data, data, length);
986     else
987         LOC_LOGE("%s, Could not inject XTRA data. Buffer address: %p, length: %d",
988                 __func__, data, length);
989     EXIT_LOG(%d, ret_val);
990     return ret_val;
991 }
992 
993 /*===========================================================================
994 FUNCTION    loc_gps_measurement_init
995 
996 DESCRIPTION
997    This function initializes the gps measurement interface
998 
999 DEPENDENCIES
1000    NONE
1001 
1002 RETURN VALUE
1003    None
1004 
1005 SIDE EFFECTS
1006    N/A
1007 
1008 ===========================================================================*/
loc_gps_measurement_init(GpsMeasurementCallbacks * callbacks)1009 static int loc_gps_measurement_init(GpsMeasurementCallbacks* callbacks)
1010 {
1011     ENTRY_LOG();
1012     int ret_val = loc_eng_gps_measurement_init(loc_afw_data,
1013                                                callbacks);
1014 
1015     EXIT_LOG(%d, ret_val);
1016     return ret_val;
1017 }
1018 
1019 /*===========================================================================
1020 FUNCTION    loc_gps_measurement_close
1021 
1022 DESCRIPTION
1023    This function closes the gps measurement interface
1024 
1025 DEPENDENCIES
1026    NONE
1027 
1028 RETURN VALUE
1029    None
1030 
1031 SIDE EFFECTS
1032    N/A
1033 
1034 ===========================================================================*/
loc_gps_measurement_close()1035 static void loc_gps_measurement_close()
1036 {
1037     ENTRY_LOG();
1038     loc_eng_gps_measurement_close(loc_afw_data);
1039 
1040     EXIT_LOG(%s, VOID_RET);
1041 }
1042 
1043 /*===========================================================================
1044 FUNCTION    loc_ni_init
1045 
1046 DESCRIPTION
1047    This function initializes the NI interface
1048 
1049 DEPENDENCIES
1050    NONE
1051 
1052 RETURN VALUE
1053    None
1054 
1055 SIDE EFFECTS
1056    N/A
1057 
1058 ===========================================================================*/
loc_ni_init(GpsNiCallbacks * callbacks)1059 void loc_ni_init(GpsNiCallbacks *callbacks)
1060 {
1061     ENTRY_LOG();
1062     loc_eng_ni_init(loc_afw_data,(GpsNiExtCallbacks*) callbacks);
1063     EXIT_LOG(%s, VOID_RET);
1064 }
1065 
1066 /*===========================================================================
1067 FUNCTION    loc_ni_respond
1068 
1069 DESCRIPTION
1070    This function sends an NI respond to the modem processor
1071 
1072 DEPENDENCIES
1073    NONE
1074 
1075 RETURN VALUE
1076    None
1077 
1078 SIDE EFFECTS
1079    N/A
1080 
1081 ===========================================================================*/
loc_ni_respond(int notif_id,GpsUserResponseType user_response)1082 void loc_ni_respond(int notif_id, GpsUserResponseType user_response)
1083 {
1084     ENTRY_LOG();
1085     loc_eng_ni_respond(loc_afw_data, notif_id, user_response);
1086     EXIT_LOG(%s, VOID_RET);
1087 }
1088 
1089 // Below stub functions are members of sLocEngAGpsRilInterface
loc_agps_ril_init(AGpsRilCallbacks * callbacks)1090 static void loc_agps_ril_init( AGpsRilCallbacks* callbacks ) {}
loc_agps_ril_set_ref_location(const AGpsRefLocation * agps_reflocation,size_t sz_struct)1091 static void loc_agps_ril_set_ref_location(const AGpsRefLocation *agps_reflocation, size_t sz_struct) {}
loc_agps_ril_set_set_id(AGpsSetIDType type,const char * setid)1092 static void loc_agps_ril_set_set_id(AGpsSetIDType type, const char* setid) {}
loc_agps_ril_ni_message(uint8_t * msg,size_t len)1093 static void loc_agps_ril_ni_message(uint8_t *msg, size_t len) {}
loc_agps_ril_update_network_state(int connected,int type,int roaming,const char * extra_info)1094 static void loc_agps_ril_update_network_state(int connected, int type, int roaming, const char* extra_info) {}
1095 
1096 /*===========================================================================
1097 FUNCTION    loc_agps_ril_update_network_availability
1098 
1099 DESCRIPTION
1100    Sets data call allow vs disallow flag to modem
1101    This is the only member of sLocEngAGpsRilInterface implemented.
1102 
1103 DEPENDENCIES
1104    None
1105 
1106 RETURN VALUE
1107    0: success
1108 
1109 SIDE EFFECTS
1110    N/A
1111 
1112 ===========================================================================*/
loc_agps_ril_update_network_availability(int available,const char * apn)1113 static void loc_agps_ril_update_network_availability(int available, const char* apn)
1114 {
1115     ENTRY_LOG();
1116     loc_eng_agps_ril_update_network_availability(loc_afw_data, available, apn);
1117     EXIT_LOG(%s, VOID_RET);
1118 }
1119 
loc_agps_install_certificates(const DerEncodedCertificate * certificates,size_t length)1120 static int loc_agps_install_certificates(const DerEncodedCertificate* certificates,
1121                                          size_t length)
1122 {
1123     ENTRY_LOG();
1124     int ret_val = loc_eng_agps_install_certificates(loc_afw_data, certificates, length);
1125     EXIT_LOG(%d, ret_val);
1126     return ret_val;
1127 }
loc_agps_revoke_certificates(const Sha1CertificateFingerprint * fingerprints,size_t length)1128 static int loc_agps_revoke_certificates(const Sha1CertificateFingerprint* fingerprints,
1129                                         size_t length)
1130 {
1131     ENTRY_LOG();
1132     LOC_LOGE("agps_revoke_certificates not supported");
1133     int ret_val = AGPS_CERTIFICATE_ERROR_GENERIC;
1134     EXIT_LOG(%d, ret_val);
1135     return ret_val;
1136 }
1137 
loc_configuration_update(const char * config_data,int32_t length)1138 static void loc_configuration_update(const char* config_data, int32_t length)
1139 {
1140     ENTRY_LOG();
1141     loc_eng_configuration_update(loc_afw_data, config_data, length);
1142     EXIT_LOG(%s, VOID_RET);
1143 }
1144 
local_loc_cb(UlpLocation * location,void * locExt)1145 static void local_loc_cb(UlpLocation* location, void* locExt)
1146 {
1147     ENTRY_LOG();
1148     if (NULL != location) {
1149         CALLBACK_LOG_CALLFLOW("location_cb - from", %d, location->position_source);
1150 
1151         if (NULL != gps_loc_cb) {
1152             gps_loc_cb(&location->gpsLocation);
1153         }
1154     }
1155     EXIT_LOG(%s, VOID_RET);
1156 }
1157 
local_sv_cb(GpsSvStatus * sv_status,void * svExt)1158 static void local_sv_cb(GpsSvStatus* sv_status, void* svExt)
1159 {
1160     ENTRY_LOG();
1161     if (NULL != gps_sv_cb) {
1162         CALLBACK_LOG_CALLFLOW("sv_status_cb -", %d, sv_status->num_svs);
1163         gps_sv_cb(sv_status);
1164     }
1165     EXIT_LOG(%s, VOID_RET);
1166 }
1167 
loc_agps_status_cb(AGpsStatus * status)1168 static void loc_agps_status_cb(AGpsStatus* status)
1169 {
1170     ENTRY_LOG();
1171 
1172     if (NULL != agps_status_cb) {
1173         size_t realSize = sizeof(AGpsStatus);
1174         LOC_LOGD("agps_status size=%d real-size=%d", status->size, realSize);
1175         status->size = realSize;
1176         agps_status_cb(status);
1177     }
1178     EXIT_LOG(%s, VOID_RET);
1179 }
1180 
1181