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