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