1 /* Copyright (c) 2011-2017, 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_GNSS_MEASUREMENT |
293             LOC_API_ADAPTER_BIT_SATELLITE_REPORT |
294             LOC_API_ADAPTER_BIT_LOCATION_SERVER_REQUEST |
295             LOC_API_ADAPTER_BIT_ASSISTANCE_DATA_REQUEST |
296             LOC_API_ADAPTER_BIT_IOCTL_REPORT |
297             LOC_API_ADAPTER_BIT_STATUS_REPORT |
298             LOC_API_ADAPTER_BIT_NMEA_1HZ_REPORT |
299             LOC_API_ADAPTER_BIT_NI_NOTIFY_VERIFY_REQUEST;
300 
301     LocCallbacks clientCallbacks = {local_loc_cb, /* location_cb */
302                                     callbacks->status_cb, /* status_cb */
303                                     local_sv_cb, /* sv_status_cb */
304                                     callbacks->nmea_cb, /* nmea_cb */
305                                     callbacks->set_capabilities_cb, /* set_capabilities_cb */
306                                     callbacks->acquire_wakelock_cb, /* acquire_wakelock_cb */
307                                     callbacks->release_wakelock_cb, /* release_wakelock_cb */
308                                     callbacks->create_thread_cb, /* create_thread_cb */
309                                     NULL, /* location_ext_parser */
310                                     NULL, /* sv_ext_parser */
311                                     callbacks->request_utc_time_cb, /* request_utc_time_cb */
312                                     callbacks->set_system_info_cb, /* set_system_info_cb */
313                                     callbacks->gnss_sv_status_cb, /* gnss_sv_status_cb */
314                                     };
315 
316     gps_loc_cb = callbacks->location_cb;
317     gps_sv_cb = callbacks->sv_status_cb;
318 
319     retVal = loc_eng_init(loc_afw_data, &clientCallbacks, event, NULL);
320     loc_afw_data.adapter->mSupportsAgpsRequests = !loc_afw_data.adapter->hasAgpsExtendedCapabilities();
321     loc_afw_data.adapter->mSupportsPositionInjection = !loc_afw_data.adapter->hasCPIExtendedCapabilities();
322     loc_afw_data.adapter->mSupportsTimeInjection = !loc_afw_data.adapter->hasCPIExtendedCapabilities()
323                                                    && !loc_afw_data.adapter->hasNativeXtraClient();
324     loc_afw_data.adapter->setGpsLockMsg(0);
325     loc_afw_data.adapter->requestUlp(ContextBase::getCarrierCapabilities());
326 
327     if(retVal) {
328         LOC_LOGE("loc_eng_init() fail!");
329         goto err;
330     }
331 
332     loc_afw_data.adapter->setPowerVoteRight(loc_get_target() == TARGET_QCA1530);
333     loc_afw_data.adapter->setPowerVote(true);
334 
335     LOC_LOGD("loc_eng_init() success!");
336 
337 err:
338     EXIT_LOG(%d, retVal);
339     return retVal;
340 }
341 
342 /*===========================================================================
343 FUNCTION    loc_cleanup
344 
345 DESCRIPTION
346    Cleans location engine. The location client handle will be released.
347 
348 DEPENDENCIES
349    None
350 
351 RETURN VALUE
352    None
353 
354 SIDE EFFECTS
355    N/A
356 
357 ===========================================================================*/
loc_cleanup()358 static void loc_cleanup()
359 {
360     ENTRY_LOG();
361 
362     loc_afw_data.adapter->setPowerVote(false);
363     loc_afw_data.adapter->setGpsLockMsg(gps_conf.GPS_LOCK);
364 
365     loc_eng_cleanup(loc_afw_data);
366     gps_loc_cb = NULL;
367     gps_sv_cb = NULL;
368 
369     EXIT_LOG(%s, VOID_RET);
370 }
371 
372 /*===========================================================================
373 FUNCTION    loc_start
374 
375 DESCRIPTION
376    Starts the tracking session
377 
378 DEPENDENCIES
379    None
380 
381 RETURN VALUE
382    0: success
383 
384 SIDE EFFECTS
385    N/A
386 
387 ===========================================================================*/
loc_start()388 static int loc_start()
389 {
390     ENTRY_LOG();
391     int ret_val = loc_eng_start(loc_afw_data);
392 
393     EXIT_LOG(%d, ret_val);
394     return ret_val;
395 }
396 
397 /*===========================================================================
398 FUNCTION    loc_stop
399 
400 DESCRIPTION
401    Stops the tracking session
402 
403 DEPENDENCIES
404    None
405 
406 RETURN VALUE
407    0: success
408 
409 SIDE EFFECTS
410    N/A
411 
412 ===========================================================================*/
loc_stop()413 static int loc_stop()
414 {
415     ENTRY_LOG();
416     int ret_val = -1;
417     ret_val = loc_eng_stop(loc_afw_data);
418 
419     EXIT_LOG(%d, ret_val);
420     return ret_val;
421 }
422 
423 /*===========================================================================
424 FUNCTION    loc_set_position_mode
425 
426 DESCRIPTION
427    Sets the mode and fix frequency for the tracking session.
428 
429 DEPENDENCIES
430    None
431 
432 RETURN VALUE
433    0: success
434 
435 SIDE EFFECTS
436    N/A
437 
438 ===========================================================================*/
loc_set_position_mode(GpsPositionMode mode,GpsPositionRecurrence recurrence,uint32_t min_interval,uint32_t preferred_accuracy,uint32_t preferred_time)439 static int  loc_set_position_mode(GpsPositionMode mode,
440                                   GpsPositionRecurrence recurrence,
441                                   uint32_t min_interval,
442                                   uint32_t preferred_accuracy,
443                                   uint32_t preferred_time)
444 {
445     ENTRY_LOG();
446     int ret_val = -1;
447     LocPositionMode locMode;
448     switch (mode) {
449     case GPS_POSITION_MODE_MS_BASED:
450         locMode = LOC_POSITION_MODE_MS_BASED;
451         break;
452     case GPS_POSITION_MODE_MS_ASSISTED:
453         locMode = LOC_POSITION_MODE_MS_ASSISTED;
454         break;
455     default:
456         locMode = LOC_POSITION_MODE_STANDALONE;
457         break;
458     }
459 
460     LocPosMode params(locMode, recurrence, min_interval,
461                       preferred_accuracy, preferred_time, NULL, NULL);
462     ret_val = loc_eng_set_position_mode(loc_afw_data, params);
463 
464     EXIT_LOG(%d, ret_val);
465     return ret_val;
466 }
467 
468 /*===========================================================================
469 FUNCTION    loc_inject_time
470 
471 DESCRIPTION
472    This is used by Java native function to do time injection.
473 
474 DEPENDENCIES
475    None
476 
477 RETURN VALUE
478    0
479 
480 SIDE EFFECTS
481    N/A
482 
483 ===========================================================================*/
loc_inject_time(GpsUtcTime time,int64_t timeReference,int uncertainty)484 static int loc_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty)
485 {
486     ENTRY_LOG();
487     int ret_val = 0;
488 
489     ret_val = loc_eng_inject_time(loc_afw_data, time,
490                                   timeReference, uncertainty);
491 
492     EXIT_LOG(%d, ret_val);
493     return ret_val;
494 }
495 
496 
497 /*===========================================================================
498 FUNCTION    loc_inject_location
499 
500 DESCRIPTION
501    This is used by Java native function to do location injection.
502 
503 DEPENDENCIES
504    None
505 
506 RETURN VALUE
507    0          : Successful
508    error code : Failure
509 
510 SIDE EFFECTS
511    N/A
512 ===========================================================================*/
loc_inject_location(double latitude,double longitude,float accuracy)513 static int loc_inject_location(double latitude, double longitude, float accuracy)
514 {
515     ENTRY_LOG();
516 
517     int ret_val = 0;
518     ret_val = loc_eng_inject_location(loc_afw_data, latitude, longitude, accuracy);
519 
520     EXIT_LOG(%d, ret_val);
521     return ret_val;
522 }
523 
524 
525 /*===========================================================================
526 FUNCTION    loc_delete_aiding_data
527 
528 DESCRIPTION
529    This is used by Java native function to delete the aiding data. The function
530    updates the global variable for the aiding data to be deleted. If the GPS
531    engine is off, the aiding data will be deleted. Otherwise, the actual action
532    will happen when gps engine is turned off.
533 
534 DEPENDENCIES
535    Assumes the aiding data type specified in GpsAidingData matches with
536    LOC API specification.
537 
538 RETURN VALUE
539    None
540 
541 SIDE EFFECTS
542    N/A
543 
544 ===========================================================================*/
loc_delete_aiding_data(GpsAidingData f)545 static void loc_delete_aiding_data(GpsAidingData f)
546 {
547     ENTRY_LOG();
548 
549     loc_eng_delete_aiding_data(loc_afw_data, f);
550 
551     EXIT_LOG(%s, VOID_RET);
552 }
553 
get_geofence_interface(void)554 const GpsGeofencingInterface* get_geofence_interface(void)
555 {
556     ENTRY_LOG();
557     void *handle;
558     const char *error;
559     typedef const GpsGeofencingInterface* (*get_gps_geofence_interface_function) (void);
560     get_gps_geofence_interface_function get_gps_geofence_interface;
561     static const GpsGeofencingInterface* geofence_interface = NULL;
562 
563     dlerror();    /* Clear any existing error */
564 
565     handle = dlopen ("libgeofence.so", RTLD_NOW);
566 
567     if (!handle)
568     {
569         if ((error = dlerror()) != NULL)  {
570             LOC_LOGE ("%s, dlopen for libgeofence.so failed, error = %s\n", __func__, error);
571            }
572         goto exit;
573     }
574     dlerror();    /* Clear any existing error */
575     get_gps_geofence_interface = (get_gps_geofence_interface_function)dlsym(handle, "gps_geofence_get_interface");
576     if ((error = dlerror()) != NULL || NULL == get_gps_geofence_interface)  {
577         LOC_LOGE ("%s, dlsym for get_gps_geofence_interface failed, error = %s\n", __func__, error);
578         goto exit;
579      }
580 
581     geofence_interface = get_gps_geofence_interface();
582 
583 exit:
584     EXIT_LOG(%d, geofence_interface == NULL);
585     return geofence_interface;
586 }
587 /*===========================================================================
588 FUNCTION    loc_get_extension
589 
590 DESCRIPTION
591    Get the gps extension to support XTRA.
592 
593 DEPENDENCIES
594    N/A
595 
596 RETURN VALUE
597    The GPS extension interface.
598 
599 SIDE EFFECTS
600    N/A
601 
602 ===========================================================================*/
loc_get_extension(const char * name)603 const void* loc_get_extension(const char* name)
604 {
605     ENTRY_LOG();
606     const void* ret_val = NULL;
607 
608    LOC_LOGD("%s:%d] For Interface = %s\n",__func__, __LINE__, name);
609    if (strcmp(name, GPS_XTRA_INTERFACE) == 0)
610    {
611        ret_val = &sLocEngXTRAInterface;
612    }
613    else if (strcmp(name, AGPS_INTERFACE) == 0)
614    {
615        ret_val = &sLocEngAGpsInterface;
616    }
617    else if (strcmp(name, GPS_NI_INTERFACE) == 0)
618    {
619        ret_val = &sLocEngNiInterface;
620    }
621    else if (strcmp(name, AGPS_RIL_INTERFACE) == 0)
622    {
623        ret_val = &sLocEngAGpsRilInterface;
624    }
625    else if (strcmp(name, GPS_GEOFENCING_INTERFACE) == 0)
626    {
627        if ((gps_conf.CAPABILITIES | GPS_CAPABILITY_GEOFENCING) == gps_conf.CAPABILITIES ){
628            ret_val = get_geofence_interface();
629        }
630    }
631    else if (strcmp(name, SUPL_CERTIFICATE_INTERFACE) == 0)
632    {
633        ret_val = &sLocEngAGpsCertInterface;
634    }
635    else if (strcmp(name, GNSS_CONFIGURATION_INTERFACE) == 0)
636    {
637        ret_val = &sLocEngConfigInterface;
638    }
639    else if (strcmp(name, GPS_MEASUREMENT_INTERFACE) == 0)
640    {
641        ret_val = &sLocEngGpsMeasurementInterface;
642    }
643    else
644    {
645       LOC_LOGE ("get_extension: Invalid interface passed in\n");
646    }
647     EXIT_LOG(%p, ret_val);
648     return ret_val;
649 }
650 
651 /*===========================================================================
652 FUNCTION    loc_agps_init
653 
654 DESCRIPTION
655    Initialize the AGps interface.
656 
657 DEPENDENCIES
658    NONE
659 
660 RETURN VALUE
661    0
662 
663 SIDE EFFECTS
664    N/A
665 
666 ===========================================================================*/
loc_agps_init(AGpsCallbacks * callbacks)667 static void loc_agps_init(AGpsCallbacks* callbacks)
668 {
669     ENTRY_LOG();
670     loc_eng_agps_init(loc_afw_data, (AGpsExtCallbacks*)callbacks);
671     EXIT_LOG(%s, VOID_RET);
672 }
673 
674 /*===========================================================================
675 FUNCTION    loc_agps_open
676 
677 DESCRIPTION
678    This function is called when on-demand data connection opening is successful.
679 It should inform ARM 9 about the data open result.
680 
681 DEPENDENCIES
682    NONE
683 
684 RETURN VALUE
685    0
686 
687 SIDE EFFECTS
688    N/A
689 
690 ===========================================================================*/
loc_agps_open(const char * apn)691 static int loc_agps_open(const char* apn)
692 {
693     ENTRY_LOG();
694     AGpsType agpsType = AGPS_TYPE_SUPL;
695     AGpsBearerType bearerType = AGPS_APN_BEARER_IPV4;
696     int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);
697 
698     EXIT_LOG(%d, ret_val);
699     return ret_val;
700 }
701 
702 /*===========================================================================
703 FUNCTION    loc_agps_open_with_apniptype
704 
705 DESCRIPTION
706    This function is called when on-demand data connection opening is successful.
707 It should inform ARM 9 about the data open result.
708 
709 DEPENDENCIES
710    NONE
711 
712 RETURN VALUE
713    0
714 
715 SIDE EFFECTS
716    N/A
717 
718 ===========================================================================*/
loc_agps_open_with_apniptype(const char * apn,ApnIpType apnIpType)719 static int  loc_agps_open_with_apniptype(const char* apn, ApnIpType apnIpType)
720 {
721     ENTRY_LOG();
722     AGpsType agpsType = AGPS_TYPE_SUPL;
723     AGpsBearerType bearerType;
724 
725     switch (apnIpType) {
726         case APN_IP_IPV4:
727             bearerType = AGPS_APN_BEARER_IPV4;
728             break;
729         case APN_IP_IPV6:
730             bearerType = AGPS_APN_BEARER_IPV6;
731             break;
732         case APN_IP_IPV4V6:
733             bearerType = AGPS_APN_BEARER_IPV4V6;
734             break;
735         default:
736             bearerType = AGPS_APN_BEARER_IPV4;
737             break;
738     }
739 
740     int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);
741 
742     EXIT_LOG(%d, ret_val);
743     return ret_val;
744 }
745 
746 /*===========================================================================
747 FUNCTION    loc_agps_closed
748 
749 DESCRIPTION
750    This function is called when on-demand data connection closing is done.
751 It should inform ARM 9 about the data close result.
752 
753 DEPENDENCIES
754    NONE
755 
756 RETURN VALUE
757    0
758 
759 SIDE EFFECTS
760    N/A
761 
762 ===========================================================================*/
loc_agps_closed()763 static int loc_agps_closed()
764 {
765     ENTRY_LOG();
766     AGpsType agpsType = AGPS_TYPE_SUPL;
767     int ret_val = loc_eng_agps_closed(loc_afw_data, agpsType);
768 
769     EXIT_LOG(%d, ret_val);
770     return ret_val;
771 }
772 
773 /*===========================================================================
774 FUNCTION    loc_agps_open_failed
775 
776 DESCRIPTION
777    This function is called when on-demand data connection opening has failed.
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_failed()790 int loc_agps_open_failed()
791 {
792     ENTRY_LOG();
793     AGpsType agpsType = AGPS_TYPE_SUPL;
794     int ret_val = loc_eng_agps_open_failed(loc_afw_data, agpsType);
795 
796     EXIT_LOG(%d, ret_val);
797     return ret_val;
798 }
799 
800 /*===========================================================================
801 FUNCTION    loc_agps_set_server
802 
803 DESCRIPTION
804    If loc_eng_set_server is called before loc_eng_init, it doesn't work. This
805    proxy buffers server settings and calls loc_eng_set_server when the client is
806    open.
807 
808 DEPENDENCIES
809    NONE
810 
811 RETURN VALUE
812    0
813 
814 SIDE EFFECTS
815    N/A
816 
817 ===========================================================================*/
loc_agps_set_server(AGpsType type,const char * hostname,int port)818 static int loc_agps_set_server(AGpsType type, const char* hostname, int port)
819 {
820     ENTRY_LOG();
821     LocServerType serverType;
822     switch (type) {
823     case AGPS_TYPE_SUPL:
824         serverType = LOC_AGPS_SUPL_SERVER;
825         break;
826     case AGPS_TYPE_C2K:
827         serverType = LOC_AGPS_CDMA_PDE_SERVER;
828         break;
829     default:
830         serverType = LOC_AGPS_SUPL_SERVER;
831     }
832     int ret_val = loc_eng_set_server_proxy(loc_afw_data, serverType, hostname, port);
833 
834     EXIT_LOG(%d, ret_val);
835     return ret_val;
836 }
837 
838 /*===========================================================================
839 FUNCTIONf571
840     loc_xtra_init
841 
842 DESCRIPTION
843    Initialize XTRA module.
844 
845 DEPENDENCIES
846    None
847 
848 RETURN VALUE
849    0: success
850 
851 SIDE EFFECTS
852    N/A
853 
854 ===========================================================================*/
loc_xtra_init(GpsXtraCallbacks * callbacks)855 static int loc_xtra_init(GpsXtraCallbacks* callbacks)
856 {
857     ENTRY_LOG();
858     GpsXtraExtCallbacks extCallbacks;
859     memset(&extCallbacks, 0, sizeof(extCallbacks));
860     extCallbacks.download_request_cb = callbacks->download_request_cb;
861     int ret_val = loc_eng_xtra_init(loc_afw_data, &extCallbacks);
862 
863     EXIT_LOG(%d, ret_val);
864     return ret_val;
865 }
866 
867 
868 /*===========================================================================
869 FUNCTION    loc_xtra_inject_data
870 
871 DESCRIPTION
872    Initialize XTRA module.
873 
874 DEPENDENCIES
875    None
876 
877 RETURN VALUE
878    0: success
879 
880 SIDE EFFECTS
881    N/A
882 
883 ===========================================================================*/
loc_xtra_inject_data(char * data,int length)884 static int loc_xtra_inject_data(char* data, int length)
885 {
886     ENTRY_LOG();
887     int ret_val = -1;
888     if( (data != NULL) && ((unsigned int)length <= XTRA_DATA_MAX_SIZE))
889         ret_val = loc_eng_xtra_inject_data(loc_afw_data, data, length);
890     else
891         LOC_LOGE("%s, Could not inject XTRA data. Buffer address: %p, length: %d",
892                  __func__, data, length);
893     EXIT_LOG(%d, ret_val);
894     return ret_val;
895 }
896 
897 /*===========================================================================
898 FUNCTION    loc_gps_measurement_init
899 
900 DESCRIPTION
901    This function initializes the gps measurement interface
902 
903 DEPENDENCIES
904    NONE
905 
906 RETURN VALUE
907    None
908 
909 SIDE EFFECTS
910    N/A
911 
912 ===========================================================================*/
loc_gps_measurement_init(GpsMeasurementCallbacks * callbacks)913 static int loc_gps_measurement_init(GpsMeasurementCallbacks* callbacks)
914 {
915     ENTRY_LOG();
916     int ret_val = loc_eng_gps_measurement_init(loc_afw_data,
917                                                callbacks);
918 
919     EXIT_LOG(%d, ret_val);
920     return ret_val;
921 }
922 
923 /*===========================================================================
924 FUNCTION    loc_gps_measurement_close
925 
926 DESCRIPTION
927    This function closes the gps measurement interface
928 
929 DEPENDENCIES
930    NONE
931 
932 RETURN VALUE
933    None
934 
935 SIDE EFFECTS
936    N/A
937 
938 ===========================================================================*/
loc_gps_measurement_close()939 static void loc_gps_measurement_close()
940 {
941     ENTRY_LOG();
942     loc_eng_gps_measurement_close(loc_afw_data);
943 
944     EXIT_LOG(%s, VOID_RET);
945 }
946 
947 /*===========================================================================
948 FUNCTION    loc_ni_init
949 
950 DESCRIPTION
951    This function initializes the NI interface
952 
953 DEPENDENCIES
954    NONE
955 
956 RETURN VALUE
957    None
958 
959 SIDE EFFECTS
960    N/A
961 
962 ===========================================================================*/
loc_ni_init(GpsNiCallbacks * callbacks)963 void loc_ni_init(GpsNiCallbacks *callbacks)
964 {
965     ENTRY_LOG();
966     loc_eng_ni_init(loc_afw_data,(GpsNiExtCallbacks*) callbacks);
967     EXIT_LOG(%s, VOID_RET);
968 }
969 
970 /*===========================================================================
971 FUNCTION    loc_ni_respond
972 
973 DESCRIPTION
974    This function sends an NI respond to the modem processor
975 
976 DEPENDENCIES
977    NONE
978 
979 RETURN VALUE
980    None
981 
982 SIDE EFFECTS
983    N/A
984 
985 ===========================================================================*/
loc_ni_respond(int notif_id,GpsUserResponseType user_response)986 void loc_ni_respond(int notif_id, GpsUserResponseType user_response)
987 {
988     ENTRY_LOG();
989     loc_eng_ni_respond(loc_afw_data, notif_id, user_response);
990     EXIT_LOG(%s, VOID_RET);
991 }
992 
993 // for XTRA
createSocket()994 static inline int createSocket() {
995     int socketFd = -1;
996 
997     if ((socketFd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
998         LOC_LOGE("create socket error. reason:%s", strerror(errno));
999 
1000      } else {
1001         const char* socketPath = "/data/vendor/location/xtra/socket_hal_xtra";
1002         struct sockaddr_un addr = { .sun_family = AF_UNIX };
1003         snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socketPath);
1004 
1005         if (::connect(socketFd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
1006             LOC_LOGE("cannot connect to XTRA. reason:%s", strerror(errno));
1007             if (::close(socketFd)) {
1008                 LOC_LOGE("close socket error. reason:%s", strerror(errno));
1009             }
1010             socketFd = -1;
1011         }
1012     }
1013 
1014     return socketFd;
1015 }
1016 
closeSocket(const int socketFd)1017 static inline void closeSocket(const int socketFd) {
1018     if (socketFd >= 0) {
1019         if(::close(socketFd)) {
1020             LOC_LOGE("close socket error. reason:%s", strerror(errno));
1021         }
1022     }
1023 }
1024 
sendConnectionEvent(const bool connected,const int32_t type)1025 static inline bool sendConnectionEvent(const bool connected, const int32_t type) {
1026     int socketFd = createSocket();
1027     if (socketFd < 0) {
1028         LOC_LOGE("XTRA unreachable. sending failed.");
1029         return false;
1030     }
1031 
1032     std::stringstream ss;
1033     ss <<  "connection";
1034     ss << " " << (connected ? "1" : "0");
1035     ss << " " << (int)type;
1036     ss << "\n"; // append seperator
1037 
1038     const std::string& data = ss.str();
1039     int remain = data.length();
1040     ssize_t sent = 0;
1041 
1042     while (remain > 0 &&
1043           (sent = ::send(socketFd, data.c_str() + (data.length() - remain),
1044                        remain, MSG_NOSIGNAL)) > 0) {
1045         remain -= sent;
1046     }
1047 
1048     if (sent < 0) {
1049         LOC_LOGE("sending error. reason:%s", strerror(errno));
1050     }
1051 
1052     closeSocket(socketFd);
1053 
1054     return (remain == 0);
1055 }
1056 
1057 // Below stub functions are members of sLocEngAGpsRilInterface
loc_agps_ril_init(AGpsRilCallbacks * callbacks)1058 static void loc_agps_ril_init( AGpsRilCallbacks* callbacks ) {}
loc_agps_ril_set_ref_location(const AGpsRefLocation * agps_reflocation,size_t sz_struct)1059 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)1060 static void loc_agps_ril_set_set_id(AGpsSetIDType type, const char* setid) {}
loc_agps_ril_ni_message(uint8_t * msg,size_t len)1061 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)1062 static void loc_agps_ril_update_network_state(int connected, int type, int roaming, const char* extra_info) {
1063     ENTRY_LOG();
1064     // for XTRA
1065     sendConnectionEvent((connected != 0) ? true : false, type);
1066     EXIT_LOG(%s, VOID_RET);
1067 }
1068 
1069 /*===========================================================================
1070 FUNCTION    loc_agps_ril_update_network_availability
1071 
1072 DESCRIPTION
1073    Sets data call allow vs disallow flag to modem
1074    This is the only member of sLocEngAGpsRilInterface implemented.
1075 
1076 DEPENDENCIES
1077    None
1078 
1079 RETURN VALUE
1080    0: success
1081 
1082 SIDE EFFECTS
1083    N/A
1084 
1085 ===========================================================================*/
loc_agps_ril_update_network_availability(int available,const char * apn)1086 static void loc_agps_ril_update_network_availability(int available, const char* apn)
1087 {
1088     ENTRY_LOG();
1089     char baseband[PROPERTY_VALUE_MAX];
1090     property_get("ro.baseband", baseband, "msm");
1091     if (strcmp(baseband, "csfb") == 0)
1092     {
1093         loc_eng_agps_ril_update_network_availability(loc_afw_data, available, apn);
1094     }
1095     EXIT_LOG(%s, VOID_RET);
1096 }
1097 
loc_agps_install_certificates(const DerEncodedCertificate * certificates,size_t length)1098 static int loc_agps_install_certificates(const DerEncodedCertificate* certificates,
1099                                          size_t length)
1100 {
1101     ENTRY_LOG();
1102     int ret_val = loc_eng_agps_install_certificates(loc_afw_data, certificates, length);
1103     EXIT_LOG(%d, ret_val);
1104     return ret_val;
1105 }
loc_agps_revoke_certificates(const Sha1CertificateFingerprint * fingerprints,size_t length)1106 static int loc_agps_revoke_certificates(const Sha1CertificateFingerprint* fingerprints,
1107                                         size_t length)
1108 {
1109     ENTRY_LOG();
1110     LOC_LOGE("%s:%d]: agps_revoke_certificates not supported",__func__, __LINE__);
1111     int ret_val = AGPS_CERTIFICATE_ERROR_GENERIC;
1112     EXIT_LOG(%d, ret_val);
1113     return ret_val;
1114 }
1115 
loc_configuration_update(const char * config_data,int32_t length)1116 static void loc_configuration_update(const char* config_data, int32_t length)
1117 {
1118     ENTRY_LOG();
1119     loc_eng_configuration_update(loc_afw_data, config_data, length);
1120     switch (sGnssType)
1121     {
1122     case GNSS_GSS:
1123     case GNSS_AUTO:
1124     case GNSS_QCA1530:
1125         //APQ
1126         gps_conf.CAPABILITIES &= ~(GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
1127         break;
1128     }
1129     EXIT_LOG(%s, VOID_RET);
1130 }
1131 
local_loc_cb(UlpLocation * location,void * locExt)1132 static void local_loc_cb(UlpLocation* location, void* locExt)
1133 {
1134     ENTRY_LOG();
1135     if (NULL != location) {
1136         CALLBACK_LOG_CALLFLOW("location_cb - from", %d, location->position_source);
1137 
1138         if (NULL != gps_loc_cb) {
1139             gps_loc_cb(&location->gpsLocation);
1140         }
1141     }
1142     EXIT_LOG(%s, VOID_RET);
1143 }
1144 
local_sv_cb(GpsSvStatus * sv_status,void * svExt)1145 static void local_sv_cb(GpsSvStatus* sv_status, void* svExt)
1146 {
1147     ENTRY_LOG();
1148     if (NULL != gps_sv_cb) {
1149         CALLBACK_LOG_CALLFLOW("sv_status_cb -", %d, sv_status->num_svs);
1150         gps_sv_cb(sv_status);
1151     }
1152     EXIT_LOG(%s, VOID_RET);
1153 }
1154 
1155