summaryrefslogtreecommitdiff
path: root/loc_api/libloc_api_50001/loc.cpp
blob: 9fc5623569a4a14a35d68e7ab746da5d3f9db91a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation, nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#define LOG_NDDEBUG 0
#define LOG_TAG "LocSvc_afw"

#include <hardware/gps.h>
#include <gps_extended.h>
#include <loc_eng.h>
#include <loc_target.h>
#include <loc_log.h>
#include <fcntl.h>
#include <errno.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <LocDualContext.h>
#include <cutils/properties.h>

#ifdef MODEM_POWER_VOTE
#include <pm-service.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <mdm_detect.h>
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /*MODEM_POWER_VOTE*/

using namespace loc_core;

#define LOC_PM_CLIENT_NAME "GPS"

//Globals defns
static gps_location_callback gps_loc_cb = NULL;
static gps_sv_status_callback gps_sv_cb = NULL;

static void local_loc_cb(UlpLocation* location, void* locExt);
static void local_sv_cb(GpsSvStatus* sv_status, void* svExt);

static const GpsGeofencingInterface* get_geofence_interface(void);

// Function declarations for sLocEngInterface
static int  loc_init(GpsCallbacks* callbacks);
static int  loc_start();
static int  loc_stop();
static void loc_cleanup();
static int  loc_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty);
static int  loc_inject_location(double latitude, double longitude, float accuracy);
static void loc_delete_aiding_data(GpsAidingData f);
static int  loc_set_position_mode(GpsPositionMode mode, GpsPositionRecurrence recurrence,
                                  uint32_t min_interval, uint32_t preferred_accuracy,
                                  uint32_t preferred_time);
static const void* loc_get_extension(const char* name);
static void loc_close_mdm_node();
// Defines the GpsInterface in gps.h
static const GpsInterface sLocEngInterface =
{
   sizeof(GpsInterface),
   loc_init,
   loc_start,
   loc_stop,
   loc_cleanup,
   loc_inject_time,
   loc_inject_location,
   loc_delete_aiding_data,
   loc_set_position_mode,
   loc_get_extension
};

// Function declarations for sLocEngAGpsInterface
static void loc_agps_init(AGpsCallbacks* callbacks);
static int  loc_agps_open(const char* apn);
static int  loc_agps_closed();
static int  loc_agps_open_failed();
static int  loc_agps_set_server(AGpsType type, const char *hostname, int port);
static int  loc_agps_open_with_apniptype( const char* apn, ApnIpType apnIpType);

static const AGpsInterface sLocEngAGpsInterface =
{
   sizeof(AGpsInterface),
   loc_agps_init,
   loc_agps_open,
   loc_agps_closed,
   loc_agps_open_failed,
   loc_agps_set_server,
   loc_agps_open_with_apniptype
};

static int loc_xtra_init(GpsXtraCallbacks* callbacks);
static int loc_xtra_inject_data(char* data, int length);

static const GpsXtraInterface sLocEngXTRAInterface =
{
    sizeof(GpsXtraInterface),
    loc_xtra_init,
    loc_xtra_inject_data
};

static void loc_ni_init(GpsNiCallbacks *callbacks);
static void loc_ni_respond(int notif_id, GpsUserResponseType user_response);

static const GpsNiInterface sLocEngNiInterface =
{
   sizeof(GpsNiInterface),
   loc_ni_init,
   loc_ni_respond,
};

#ifdef MODEM_POWER_VOTE
typedef struct {
    //MAX_NAME_LEN defined in mdm_detect.h
    char modem_name[MAX_NAME_LEN];
    //MAX_PATH_LEN defined in mdm_detect.h
    char powerup_node[MAX_PATH_LEN];
    //this handle is used by peripheral mgr
    void *handle;
    int mdm_fd;
    MdmType mdm_type;
    bool peripheral_mgr_supported;
    bool peripheral_mgr_registered;
}s_loc_mdm_info;
static s_loc_mdm_info loc_mdm_info;
static void loc_pm_event_notifier(void *client_data, enum pm_event event);
#endif /*MODEM_POWER_VOTE*/
// For shutting down MDM in fusion devices
static int mdm_fd = -1;
static int loc_gps_measurement_init(GpsMeasurementCallbacks* callbacks);
static void loc_gps_measurement_close();

static const GpsMeasurementInterface sLocEngGpsMeasurementInterface =
{
    sizeof(GpsMeasurementInterface),
    loc_gps_measurement_init,
    loc_gps_measurement_close
};

static void loc_agps_ril_init( AGpsRilCallbacks* callbacks );
static void loc_agps_ril_set_ref_location(const AGpsRefLocation *agps_reflocation, size_t sz_struct);
static void loc_agps_ril_set_set_id(AGpsSetIDType type, const char* setid);
static void loc_agps_ril_ni_message(uint8_t *msg, size_t len);
static void loc_agps_ril_update_network_state(int connected, int type, int roaming, const char* extra_info);
static void loc_agps_ril_update_network_availability(int avaiable, const char* apn);

static const AGpsRilInterface sLocEngAGpsRilInterface =
{
   sizeof(AGpsRilInterface),
   loc_agps_ril_init,
   loc_agps_ril_set_ref_location,
   loc_agps_ril_set_set_id,
   loc_agps_ril_ni_message,
   loc_agps_ril_update_network_state,
   loc_agps_ril_update_network_availability
};

static int loc_agps_install_certificates(const DerEncodedCertificate* certificates,
                                         size_t length);
static int loc_agps_revoke_certificates(const Sha1CertificateFingerprint* fingerprints,
                                        size_t length);

static const SuplCertificateInterface sLocEngAGpsCertInterface =
{
    sizeof(SuplCertificateInterface),
    loc_agps_install_certificates,
    loc_agps_revoke_certificates
};

static void loc_configuration_update(const char* config_data, int32_t length);

static const GnssConfigurationInterface sLocEngConfigInterface =
{
    sizeof(GnssConfigurationInterface),
    loc_configuration_update
};

static loc_eng_data_s_type loc_afw_data;
static int gss_fd = -1;

/*===========================================================================
FUNCTION    gps_get_hardware_interface

DESCRIPTION
   Returns the GPS hardware interaface based on LOC API
   if GPS is enabled.

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
const GpsInterface* gps_get_hardware_interface ()
{
    ENTRY_LOG_CALLFLOW();
    const GpsInterface* ret_val;

    char propBuf[PROPERTY_VALUE_MAX];

    loc_eng_read_config();

    // check to see if GPS should be disabled
    property_get("gps.disable", propBuf, "");
    if (propBuf[0] == '1')
    {
        LOC_LOGD("gps_get_interface returning NULL because gps.disable=1\n");
        ret_val = NULL;
    } else {
        ret_val = &sLocEngInterface;
    }

    loc_eng_read_config();

    EXIT_LOG(%p, ret_val);
    return ret_val;
}

// for gps.c
extern "C" const GpsInterface* get_gps_interface()
{
    unsigned int target = TARGET_DEFAULT;
    loc_eng_read_config();

    target = loc_get_target();
    LOC_LOGD("Target name check returned %s", loc_get_target_name(target));

    int gnssType = getTargetGnssType(target);
    switch (gnssType)
    {
    case GNSS_GSS:
    case GNSS_AUTO:
        //APQ8064
        gps_conf.CAPABILITIES &= ~(GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
        gss_fd = open("/dev/gss", O_RDONLY);
        if (gss_fd < 0) {
            LOC_LOGE("GSS open failed: %s\n", strerror(errno));
        }
        else {
            LOC_LOGD("GSS open success! CAPABILITIES %0lx\n",
                     gps_conf.CAPABILITIES);
        }
        break;
    case GNSS_NONE:
        //MPQ8064
        LOC_LOGE("No GPS HW on this target. Not returning interface.");
        return NULL;
    case GNSS_QCA1530:
        // qca1530 chip is present
        gps_conf.CAPABILITIES &= ~(GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
        LOC_LOGD("qca1530 present: CAPABILITIES %0lx\n", gps_conf.CAPABILITIES);
        break;
    }
    return &sLocEngInterface;
}

/*===========================================================================
FUNCTION    loc_init

DESCRIPTION
   Initialize the location engine, this include setting up global datas
   and registers location engien with loc api service.

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/Ax

===========================================================================*/
static int loc_init(GpsCallbacks* callbacks)
{
    int retVal = -1;
#ifdef MODEM_POWER_VOTE
    enum pm_event mdm_state;
    static int mdm_index = -1;
    int peripheral_mgr_ret = PM_RET_FAILED;
#endif /*MODEM_POWER_VOTE*/
    ENTRY_LOG();
    LOC_API_ADAPTER_EVENT_MASK_T event;

    if (NULL == callbacks) {
        LOC_LOGE("loc_init failed. cb = NULL\n");
        EXIT_LOG(%d, retVal);
        return retVal;
    }

    event = LOC_API_ADAPTER_BIT_PARSED_POSITION_REPORT |
            LOC_API_ADAPTER_BIT_SATELLITE_REPORT |
            LOC_API_ADAPTER_BIT_LOCATION_SERVER_REQUEST |
            LOC_API_ADAPTER_BIT_ASSISTANCE_DATA_REQUEST |
            LOC_API_ADAPTER_BIT_IOCTL_REPORT |
            LOC_API_ADAPTER_BIT_STATUS_REPORT |
            LOC_API_ADAPTER_BIT_NMEA_1HZ_REPORT |
            LOC_API_ADAPTER_BIT_NI_NOTIFY_VERIFY_REQUEST;

    LocCallbacks clientCallbacks = {local_loc_cb, /* location_cb */
                                    callbacks->status_cb, /* status_cb */
                                    local_sv_cb, /* sv_status_cb */
                                    callbacks->nmea_cb, /* nmea_cb */
                                    callbacks->set_capabilities_cb, /* set_capabilities_cb */
                                    callbacks->acquire_wakelock_cb, /* acquire_wakelock_cb */
                                    callbacks->release_wakelock_cb, /* release_wakelock_cb */
                                    callbacks->create_thread_cb, /* create_thread_cb */
                                    NULL, /* location_ext_parser */
                                    NULL, /* sv_ext_parser */
                                    callbacks->request_utc_time_cb, /* request_utc_time_cb */
                                    loc_close_mdm_node  /*loc_shutdown_cb*/};

    gps_loc_cb = callbacks->location_cb;
    gps_sv_cb = callbacks->sv_status_cb;

    retVal = loc_eng_init(loc_afw_data, &clientCallbacks, event, NULL);
    loc_afw_data.adapter->mSupportsAgpsRequests = !loc_afw_data.adapter->hasAgpsExtendedCapabilities();
    loc_afw_data.adapter->mSupportsPositionInjection = !loc_afw_data.adapter->hasCPIExtendedCapabilities();
    loc_afw_data.adapter->mSupportsTimeInjection = !loc_afw_data.adapter->hasCPIExtendedCapabilities();
    loc_afw_data.adapter->setGpsLockMsg(0);
    loc_afw_data.adapter->requestUlp(getCarrierCapabilities());

    if(retVal) {
        LOC_LOGE("loc_eng_init() fail!");
        goto err;
    }

    loc_afw_data.adapter->setPowerVoteRight(loc_get_target() == TARGET_QCA1530);
    loc_afw_data.adapter->setPowerVote(true);

    LOC_LOGD("loc_eng_init() success!");

#ifdef MODEM_POWER_VOTE
    //if index is 0 or more, then we've looked for mdm already
    LOC_LOGD("%s:%d]: mdm_index: %d", __func__, __LINE__,
             mdm_index);
    if (mdm_index < 0) {
        struct dev_info modem_info;
        memset(&modem_info, 0, sizeof(struct dev_info));
        if(get_system_info(&modem_info) != RET_SUCCESS) {
            LOC_LOGE("%s:%d]: Error: get_system_info returned error\n",
                     __func__, __LINE__);
            goto err;
        }

        for(mdm_index = 0;
            mdm_index < modem_info.num_modems;
            mdm_index++) {
            if(modem_info.mdm_list[mdm_index].mdm_name) {
                //Copy modem name to register with peripheral manager
                strlcpy(loc_mdm_info.modem_name,
                        modem_info.mdm_list[mdm_index].mdm_name,
                        sizeof(loc_mdm_info.modem_name));
                //copy powerup node name if we need to use mdmdetect method
                strlcpy(loc_mdm_info.powerup_node,
                        modem_info.mdm_list[mdm_index].powerup_node,
                        sizeof(loc_mdm_info.powerup_node));
                loc_mdm_info.mdm_type = modem_info.mdm_list[mdm_index].type;
                LOC_LOGD("%s:%d]: Found modem: %s, powerup node:%s at index: %d",
                         __func__, __LINE__, loc_mdm_info.modem_name, loc_mdm_info.powerup_node,
                         mdm_index);
                break;
            }
        }
    }

    if(loc_mdm_info.peripheral_mgr_registered != true) {
        peripheral_mgr_ret = pm_client_register(loc_pm_event_notifier,
                                                &loc_mdm_info,
                                                loc_mdm_info.modem_name,
                                                LOC_PM_CLIENT_NAME,
                                                &mdm_state,
                                                &loc_mdm_info.handle);
        if(peripheral_mgr_ret == PM_RET_SUCCESS) {
            loc_mdm_info.peripheral_mgr_supported = true;
            loc_mdm_info.peripheral_mgr_registered = true;
            LOC_LOGD("%s:%d]: registered with peripheral mgr for %s",
                     __func__, __LINE__, loc_mdm_info.modem_name);
        }
        else if(peripheral_mgr_ret == PM_RET_UNSUPPORTED) {
            loc_mdm_info.peripheral_mgr_registered = true;
            loc_mdm_info.peripheral_mgr_supported = false;
            LOC_LOGD("%s:%d]: peripheral mgr unsupported for: %s",
                     __func__, __LINE__, loc_mdm_info.modem_name);
        }
        else {
            //Not setting any flags here so that we can try again the next time around
            LOC_LOGE("%s:%d]: Error: pm_client_register returned: %d",
                     __func__, __LINE__, peripheral_mgr_ret);
        }
    }

    if(loc_mdm_info.peripheral_mgr_supported == false &&
       loc_mdm_info.peripheral_mgr_registered == true) {
        //Peripheral mgr is not supported
        //use legacy method to open the powerup node
        LOC_LOGD("%s:%d]: powerup_node: %s", __func__, __LINE__,
                 loc_mdm_info.powerup_node);
        loc_mdm_info.mdm_fd = open(loc_mdm_info.powerup_node, O_RDONLY);

        if (loc_mdm_info.mdm_fd < 0) {
            LOC_LOGE("Error: %s open failed: %s\n",
                     loc_mdm_info.powerup_node, strerror(errno));
        } else {
            LOC_LOGD("%s opens success!", loc_mdm_info.powerup_node);
        }
    }
    else if(loc_mdm_info.peripheral_mgr_supported == true &&
            loc_mdm_info.peripheral_mgr_registered == true) {
        LOC_LOGD("%s:%d]: Voting for modem power up", __func__, __LINE__);
        pm_client_connect(loc_mdm_info.handle);
    }
    else {
        LOC_LOGD("%s:%d]: Not voted for modem power up due to errors", __func__, __LINE__);
    }
#endif /*MODEM_POWER_VOTE*/
err:
    EXIT_LOG(%d, retVal);
    return retVal;
}

/*===========================================================================
FUNCTION    loc_close_mdm_node

DESCRIPTION
   closes loc_mdm_info.mdm_fd which is the modem powerup node obtained in loc_init

DEPENDENCIES
   None

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
static void loc_close_mdm_node()
{
    ENTRY_LOG();
#ifdef MODEM_POWER_VOTE
    if(loc_mdm_info.peripheral_mgr_supported == true) {
        LOC_LOGD("%s:%d]: Voting for modem power down", __func__, __LINE__);
        pm_client_disconnect(loc_mdm_info.handle);
    }
    else if (loc_mdm_info.mdm_fd >= 0) {
        LOC_LOGD("closing the powerup node");
        close(loc_mdm_info.mdm_fd);
        loc_mdm_info.mdm_fd = -1;
        LOC_LOGD("finished closing the powerup node");
    }
    else {
        LOC_LOGD("powerup node has not been opened yet.");
    }
#endif /*MODEM_POWER_VOTE*/
    EXIT_LOG(%s, VOID_RET);
}

/*===========================================================================
FUNCTION    loc_cleanup

DESCRIPTION
   Cleans location engine. The location client handle will be released.

DEPENDENCIES
   None

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
static void loc_cleanup()
{
    ENTRY_LOG();

    loc_afw_data.adapter->setPowerVote(false);
    loc_afw_data.adapter->setGpsLockMsg(gps_conf.GPS_LOCK);

    loc_eng_cleanup(loc_afw_data);
    loc_close_mdm_node();
    gps_loc_cb = NULL;
    gps_sv_cb = NULL;

    EXIT_LOG(%s, VOID_RET);
}

/*===========================================================================
FUNCTION    loc_start

DESCRIPTION
   Starts the tracking session

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_start()
{
    ENTRY_LOG();
    int ret_val = loc_eng_start(loc_afw_data);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_stop

DESCRIPTION
   Stops the tracking session

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_stop()
{
    ENTRY_LOG();
    int ret_val = -1;
    ret_val = loc_eng_stop(loc_afw_data);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_set_position_mode

DESCRIPTION
   Sets the mode and fix frequency for the tracking session.

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
static int  loc_set_position_mode(GpsPositionMode mode,
                                  GpsPositionRecurrence recurrence,
                                  uint32_t min_interval,
                                  uint32_t preferred_accuracy,
                                  uint32_t preferred_time)
{
    ENTRY_LOG();
    int ret_val = -1;
    LocPositionMode locMode;
    switch (mode) {
    case GPS_POSITION_MODE_MS_BASED:
        locMode = LOC_POSITION_MODE_MS_BASED;
        break;
    case GPS_POSITION_MODE_MS_ASSISTED:
        locMode = LOC_POSITION_MODE_MS_ASSISTED;
        break;
    default:
        locMode = LOC_POSITION_MODE_STANDALONE;
        break;
    }

    LocPosMode params(locMode, recurrence, min_interval,
                      preferred_accuracy, preferred_time, NULL, NULL);
    ret_val = loc_eng_set_position_mode(loc_afw_data, params);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_inject_time

DESCRIPTION
   This is used by Java native function to do time injection.

DEPENDENCIES
   None

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty)
{
    ENTRY_LOG();
    int ret_val = 0;

    ret_val = loc_eng_inject_time(loc_afw_data, time,
                                  timeReference, uncertainty);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}


/*===========================================================================
FUNCTION    loc_inject_location

DESCRIPTION
   This is used by Java native function to do location injection.

DEPENDENCIES
   None

RETURN VALUE
   0          : Successful
   error code : Failure

SIDE EFFECTS
   N/A
===========================================================================*/
static int loc_inject_location(double latitude, double longitude, float accuracy)
{
    ENTRY_LOG();

    int ret_val = 0;
    ret_val = loc_eng_inject_location(loc_afw_data, latitude, longitude, accuracy);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}


/*===========================================================================
FUNCTION    loc_delete_aiding_data

DESCRIPTION
   This is used by Java native function to delete the aiding data. The function
   updates the global variable for the aiding data to be deleted. If the GPS
   engine is off, the aiding data will be deleted. Otherwise, the actual action
   will happen when gps engine is turned off.

DEPENDENCIES
   Assumes the aiding data type specified in GpsAidingData matches with
   LOC API specification.

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
static void loc_delete_aiding_data(GpsAidingData f)
{
    ENTRY_LOG();
    loc_eng_delete_aiding_data(loc_afw_data, f);

    EXIT_LOG(%s, VOID_RET);
}

const GpsGeofencingInterface* get_geofence_interface(void)
{
    ENTRY_LOG();
    void *handle;
    const char *error;
    typedef const GpsGeofencingInterface* (*get_gps_geofence_interface_function) (void);
    get_gps_geofence_interface_function get_gps_geofence_interface;
    static const GpsGeofencingInterface* geofence_interface = NULL;

    dlerror();    /* Clear any existing error */

    handle = dlopen ("libgeofence.so", RTLD_NOW);

    if (!handle)
    {
        if ((error = dlerror()) != NULL)  {
            LOC_LOGE ("%s, dlopen for libgeofence.so failed, error = %s\n", __func__, error);
           }
        goto exit;
    }
    dlerror();    /* Clear any existing error */
    get_gps_geofence_interface = (get_gps_geofence_interface_function)dlsym(handle, "gps_geofence_get_interface");
    if ((error = dlerror()) != NULL && NULL != get_gps_geofence_interface)  {
        LOC_LOGE ("%s, dlsym for get_gps_geofence_interface failed, error = %s\n", __func__, error);
        goto exit;
     }

    geofence_interface = get_gps_geofence_interface();

exit:
    EXIT_LOG(%d, geofence_interface == NULL);
    return geofence_interface;
}
/*===========================================================================
FUNCTION    loc_get_extension

DESCRIPTION
   Get the gps extension to support XTRA.

DEPENDENCIES
   N/A

RETURN VALUE
   The GPS extension interface.

SIDE EFFECTS
   N/A

===========================================================================*/
const void* loc_get_extension(const char* name)
{
    ENTRY_LOG();
    const void* ret_val = NULL;

   LOC_LOGD("%s:%d] For Interface = %s\n",__func__, __LINE__, name);
   if (strcmp(name, GPS_XTRA_INTERFACE) == 0)
   {
       ret_val = &sLocEngXTRAInterface;
   }
   else if (strcmp(name, AGPS_INTERFACE) == 0)
   {
       ret_val = &sLocEngAGpsInterface;
   }
   else if (strcmp(name, GPS_NI_INTERFACE) == 0)
   {
       ret_val = &sLocEngNiInterface;
   }
   else if (strcmp(name, AGPS_RIL_INTERFACE) == 0)
   {
       char baseband[PROPERTY_VALUE_MAX];
       property_get("ro.baseband", baseband, "msm");
       if (strcmp(baseband, "csfb") == 0)
       {
           ret_val = &sLocEngAGpsRilInterface;
       }
   }
   else if (strcmp(name, GPS_GEOFENCING_INTERFACE) == 0)
   {
       if ((gps_conf.CAPABILITIES | GPS_CAPABILITY_GEOFENCING) == gps_conf.CAPABILITIES ){
           ret_val = get_geofence_interface();
       }
   }
   else if (strcmp(name, SUPL_CERTIFICATE_INTERFACE) == 0)
   {
       ret_val = &sLocEngAGpsCertInterface;
   }
   else if (strcmp(name, GNSS_CONFIGURATION_INTERFACE) == 0)
   {
       ret_val = &sLocEngConfigInterface;
   }
   else if (strcmp(name, GPS_MEASUREMENT_INTERFACE) == 0)
   {
       ret_val = &sLocEngGpsMeasurementInterface;
   }
   else
   {
      LOC_LOGE ("get_extension: Invalid interface passed in\n");
   }
    EXIT_LOG(%p, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_agps_init

DESCRIPTION
   Initialize the AGps interface.

DEPENDENCIES
   NONE

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
static void loc_agps_init(AGpsCallbacks* callbacks)
{
    ENTRY_LOG();
    loc_eng_agps_init(loc_afw_data, (AGpsExtCallbacks*)callbacks);
    EXIT_LOG(%s, VOID_RET);
}

/*===========================================================================
FUNCTION    loc_agps_open

DESCRIPTION
   This function is called when on-demand data connection opening is successful.
It should inform ARM 9 about the data open result.

DEPENDENCIES
   NONE

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_agps_open(const char* apn)
{
    ENTRY_LOG();
    AGpsType agpsType = AGPS_TYPE_SUPL;
    AGpsBearerType bearerType = AGPS_APN_BEARER_IPV4;
    int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_agps_open_with_apniptype

DESCRIPTION
   This function is called when on-demand data connection opening is successful.
It should inform ARM 9 about the data open result.

DEPENDENCIES
   NONE

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
static int  loc_agps_open_with_apniptype(const char* apn, ApnIpType apnIpType)
{
    ENTRY_LOG();
    AGpsType agpsType = AGPS_TYPE_SUPL;
    AGpsBearerType bearerType;

    switch (apnIpType) {
        case APN_IP_IPV4:
            bearerType = AGPS_APN_BEARER_IPV4;
            break;
        case APN_IP_IPV6:
            bearerType = AGPS_APN_BEARER_IPV6;
            break;
        case APN_IP_IPV4V6:
            bearerType = AGPS_APN_BEARER_IPV4V6;
            break;
        default:
            bearerType = AGPS_APN_BEARER_INVALID;
            break;
    }

    int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_agps_closed

DESCRIPTION
   This function is called when on-demand data connection closing is done.
It should inform ARM 9 about the data close result.

DEPENDENCIES
   NONE

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_agps_closed()
{
    ENTRY_LOG();
    AGpsType agpsType = AGPS_TYPE_SUPL;
    int ret_val = loc_eng_agps_closed(loc_afw_data, agpsType);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_agps_open_failed

DESCRIPTION
   This function is called when on-demand data connection opening has failed.
It should inform ARM 9 about the data open result.

DEPENDENCIES
   NONE

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
int loc_agps_open_failed()
{
    ENTRY_LOG();
    AGpsType agpsType = AGPS_TYPE_SUPL;
    int ret_val = loc_eng_agps_open_failed(loc_afw_data, agpsType);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_agps_set_server

DESCRIPTION
   If loc_eng_set_server is called before loc_eng_init, it doesn't work. This
   proxy buffers server settings and calls loc_eng_set_server when the client is
   open.

DEPENDENCIES
   NONE

RETURN VALUE
   0

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_agps_set_server(AGpsType type, const char* hostname, int port)
{
    ENTRY_LOG();
    LocServerType serverType;
    switch (type) {
    case AGPS_TYPE_SUPL:
        serverType = LOC_AGPS_SUPL_SERVER;
        break;
    case AGPS_TYPE_C2K:
        serverType = LOC_AGPS_CDMA_PDE_SERVER;
        break;
    default:
        serverType = LOC_AGPS_SUPL_SERVER;
    }
    int ret_val = loc_eng_set_server_proxy(loc_afw_data, serverType, hostname, port);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTIONf571
    loc_xtra_init

DESCRIPTION
   Initialize XTRA module.

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_xtra_init(GpsXtraCallbacks* callbacks)
{
    ENTRY_LOG();
    int ret_val = loc_eng_xtra_init(loc_afw_data, (GpsXtraExtCallbacks*)callbacks);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}


/*===========================================================================
FUNCTION    loc_xtra_inject_data

DESCRIPTION
   Initialize XTRA module.

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_xtra_inject_data(char* data, int length)
{
    ENTRY_LOG();
    int ret_val = -1;
    if( (data != NULL) && ((unsigned int)length <= XTRA_DATA_MAX_SIZE))
        ret_val = loc_eng_xtra_inject_data(loc_afw_data, data, length);
    else
        LOC_LOGE("%s, Could not inject XTRA data. Buffer address: %p, length: %d",
                 __func__, data, length);
    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_gps_measurement_init

DESCRIPTION
   This function initializes the gps measurement interface

DEPENDENCIES
   NONE

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
static int loc_gps_measurement_init(GpsMeasurementCallbacks* callbacks)
{
    ENTRY_LOG();
    int ret_val = loc_eng_gps_measurement_init(loc_afw_data,
                                               callbacks);

    EXIT_LOG(%d, ret_val);
    return ret_val;
}

/*===========================================================================
FUNCTION    loc_gps_measurement_close

DESCRIPTION
   This function closes the gps measurement interface

DEPENDENCIES
   NONE

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
static void loc_gps_measurement_close()
{
    ENTRY_LOG();
    loc_eng_gps_measurement_close(loc_afw_data);

    EXIT_LOG(%s, VOID_RET);
}

/*===========================================================================
FUNCTION    loc_ni_init

DESCRIPTION
   This function initializes the NI interface

DEPENDENCIES
   NONE

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
void loc_ni_init(GpsNiCallbacks *callbacks)
{
    ENTRY_LOG();
    loc_eng_ni_init(loc_afw_data,(GpsNiExtCallbacks*) callbacks);
    EXIT_LOG(%s, VOID_RET);
}

/*===========================================================================
FUNCTION    loc_ni_respond

DESCRIPTION
   This function sends an NI respond to the modem processor

DEPENDENCIES
   NONE

RETURN VALUE
   None

SIDE EFFECTS
   N/A

===========================================================================*/
void loc_ni_respond(int notif_id, GpsUserResponseType user_response)
{
    ENTRY_LOG();
    loc_eng_ni_respond(loc_afw_data, notif_id, user_response);
    EXIT_LOG(%s, VOID_RET);
}

// Below stub functions are members of sLocEngAGpsRilInterface
static void loc_agps_ril_init( AGpsRilCallbacks* callbacks ) {}
static void loc_agps_ril_set_ref_location(const AGpsRefLocation *agps_reflocation, size_t sz_struct) {}
static void loc_agps_ril_set_set_id(AGpsSetIDType type, const char* setid) {}
static void loc_agps_ril_ni_message(uint8_t *msg, size_t len) {}
static void loc_agps_ril_update_network_state(int connected, int type, int roaming, const char* extra_info) {}

/*===========================================================================
FUNCTION    loc_agps_ril_update_network_availability

DESCRIPTION
   Sets data call allow vs disallow flag to modem
   This is the only member of sLocEngAGpsRilInterface implemented.

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
static void loc_agps_ril_update_network_availability(int available, const char* apn)
{
    ENTRY_LOG();
    loc_eng_agps_ril_update_network_availability(loc_afw_data, available, apn);
    EXIT_LOG(%s, VOID_RET);
}

static int loc_agps_install_certificates(const DerEncodedCertificate* certificates,
                                         size_t length)
{
    ENTRY_LOG();
    int ret_val = loc_eng_agps_install_certificates(loc_afw_data, certificates, length);
    EXIT_LOG(%d, ret_val);
    return ret_val;
}
static int loc_agps_revoke_certificates(const Sha1CertificateFingerprint* fingerprints,
                                        size_t length)
{
    ENTRY_LOG();
    LOC_LOGE("%s:%d]: agps_revoke_certificates not supported");
    int ret_val = AGPS_CERTIFICATE_ERROR_GENERIC;
    EXIT_LOG(%d, ret_val);
    return ret_val;
}

static void loc_configuration_update(const char* config_data, int32_t length)
{
    ENTRY_LOG();
    loc_eng_configuration_update(loc_afw_data, config_data, length);
    EXIT_LOG(%s, VOID_RET);
}

static void local_loc_cb(UlpLocation* location, void* locExt)
{
    ENTRY_LOG();
    if (NULL != location) {
        CALLBACK_LOG_CALLFLOW("location_cb - from", %d, location->position_source);

        if (NULL != gps_loc_cb) {
            gps_loc_cb(&location->gpsLocation);
        }
    }
    EXIT_LOG(%s, VOID_RET);
}

static void local_sv_cb(GpsSvStatus* sv_status, void* svExt)
{
    ENTRY_LOG();
    if (NULL != gps_sv_cb) {
        CALLBACK_LOG_CALLFLOW("sv_status_cb -", %d, sv_status->num_svs);
        gps_sv_cb(sv_status);
    }
    EXIT_LOG(%s, VOID_RET);
}

#ifdef MODEM_POWER_VOTE
static void loc_pm_event_notifier(void *client_data, enum pm_event event)
{
    ENTRY_LOG();
    LOC_LOGD("%s:%d]: event: %d", __func__, __LINE__, (int)event);
    pm_client_event_acknowledge(loc_mdm_info.handle, event);
    EXIT_LOG(%s, VOID_RET);
}
#endif /*MODEM_POWER_VOTE*/