Home | History | Annotate | Download | only in libloc_api_50001
      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 ===========================================================================*/
    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
    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 ===========================================================================*/
    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 ===========================================================================*/
    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 ===========================================================================*/
    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 ===========================================================================*/
    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 ===========================================================================*/
    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 ===========================================================================*/
    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 ===========================================================================*/
    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 ===========================================================================*/
    545 static void loc_delete_aiding_data(GpsAidingData f)
    546 {
    547     ENTRY_LOG();
    548 
    549 #ifndef TARGET_BUILD_VARIANT_USER
    550     loc_eng_delete_aiding_data(loc_afw_data, f);
    551 #endif
    552 
    553     EXIT_LOG(%s, VOID_RET);
    554 }
    555 
    556 const GpsGeofencingInterface* get_geofence_interface(void)
    557 {
    558     ENTRY_LOG();
    559     void *handle;
    560     const char *error;
    561     typedef const GpsGeofencingInterface* (*get_gps_geofence_interface_function) (void);
    562     get_gps_geofence_interface_function get_gps_geofence_interface;
    563     static const GpsGeofencingInterface* geofence_interface = NULL;
    564 
    565     dlerror();    /* Clear any existing error */
    566 
    567     handle = dlopen ("libgeofence.so", RTLD_NOW);
    568 
    569     if (!handle)
    570     {
    571         if ((error = dlerror()) != NULL)  {
    572             LOC_LOGE ("%s, dlopen for libgeofence.so failed, error = %s\n", __func__, error);
    573            }
    574         goto exit;
    575     }
    576     dlerror();    /* Clear any existing error */
    577     get_gps_geofence_interface = (get_gps_geofence_interface_function)dlsym(handle, "gps_geofence_get_interface");
    578     if ((error = dlerror()) != NULL || NULL == get_gps_geofence_interface)  {
    579         LOC_LOGE ("%s, dlsym for get_gps_geofence_interface failed, error = %s\n", __func__, error);
    580         goto exit;
    581      }
    582 
    583     geofence_interface = get_gps_geofence_interface();
    584 
    585 exit:
    586     EXIT_LOG(%d, geofence_interface == NULL);
    587     return geofence_interface;
    588 }
    589 /*===========================================================================
    590 FUNCTION    loc_get_extension
    591 
    592 DESCRIPTION
    593    Get the gps extension to support XTRA.
    594 
    595 DEPENDENCIES
    596    N/A
    597 
    598 RETURN VALUE
    599    The GPS extension interface.
    600 
    601 SIDE EFFECTS
    602    N/A
    603 
    604 ===========================================================================*/
    605 const void* loc_get_extension(const char* name)
    606 {
    607     ENTRY_LOG();
    608     const void* ret_val = NULL;
    609 
    610    LOC_LOGD("%s:%d] For Interface = %s\n",__func__, __LINE__, name);
    611    if (strcmp(name, GPS_XTRA_INTERFACE) == 0)
    612    {
    613        ret_val = &sLocEngXTRAInterface;
    614    }
    615    else if (strcmp(name, AGPS_INTERFACE) == 0)
    616    {
    617        ret_val = &sLocEngAGpsInterface;
    618    }
    619    else if (strcmp(name, GPS_NI_INTERFACE) == 0)
    620    {
    621        ret_val = &sLocEngNiInterface;
    622    }
    623    else if (strcmp(name, AGPS_RIL_INTERFACE) == 0)
    624    {
    625        ret_val = &sLocEngAGpsRilInterface;
    626    }
    627    else if (strcmp(name, GPS_GEOFENCING_INTERFACE) == 0)
    628    {
    629        if ((gps_conf.CAPABILITIES | GPS_CAPABILITY_GEOFENCING) == gps_conf.CAPABILITIES ){
    630            ret_val = get_geofence_interface();
    631        }
    632    }
    633    else if (strcmp(name, SUPL_CERTIFICATE_INTERFACE) == 0)
    634    {
    635        ret_val = &sLocEngAGpsCertInterface;
    636    }
    637    else if (strcmp(name, GNSS_CONFIGURATION_INTERFACE) == 0)
    638    {
    639        ret_val = &sLocEngConfigInterface;
    640    }
    641    else if (strcmp(name, GPS_MEASUREMENT_INTERFACE) == 0)
    642    {
    643        ret_val = &sLocEngGpsMeasurementInterface;
    644    }
    645    else
    646    {
    647       LOC_LOGE ("get_extension: Invalid interface passed in\n");
    648    }
    649     EXIT_LOG(%p, ret_val);
    650     return ret_val;
    651 }
    652 
    653 /*===========================================================================
    654 FUNCTION    loc_agps_init
    655 
    656 DESCRIPTION
    657    Initialize the AGps interface.
    658 
    659 DEPENDENCIES
    660    NONE
    661 
    662 RETURN VALUE
    663    0
    664 
    665 SIDE EFFECTS
    666    N/A
    667 
    668 ===========================================================================*/
    669 static void loc_agps_init(AGpsCallbacks* callbacks)
    670 {
    671     ENTRY_LOG();
    672     loc_eng_agps_init(loc_afw_data, (AGpsExtCallbacks*)callbacks);
    673     EXIT_LOG(%s, VOID_RET);
    674 }
    675 
    676 /*===========================================================================
    677 FUNCTION    loc_agps_open
    678 
    679 DESCRIPTION
    680    This function is called when on-demand data connection opening is successful.
    681 It should inform ARM 9 about the data open result.
    682 
    683 DEPENDENCIES
    684    NONE
    685 
    686 RETURN VALUE
    687    0
    688 
    689 SIDE EFFECTS
    690    N/A
    691 
    692 ===========================================================================*/
    693 static int loc_agps_open(const char* apn)
    694 {
    695     ENTRY_LOG();
    696     AGpsType agpsType = AGPS_TYPE_SUPL;
    697     AGpsBearerType bearerType = AGPS_APN_BEARER_IPV4;
    698     int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);
    699 
    700     EXIT_LOG(%d, ret_val);
    701     return ret_val;
    702 }
    703 
    704 /*===========================================================================
    705 FUNCTION    loc_agps_open_with_apniptype
    706 
    707 DESCRIPTION
    708    This function is called when on-demand data connection opening is successful.
    709 It should inform ARM 9 about the data open result.
    710 
    711 DEPENDENCIES
    712    NONE
    713 
    714 RETURN VALUE
    715    0
    716 
    717 SIDE EFFECTS
    718    N/A
    719 
    720 ===========================================================================*/
    721 static int  loc_agps_open_with_apniptype(const char* apn, ApnIpType apnIpType)
    722 {
    723     ENTRY_LOG();
    724     AGpsType agpsType = AGPS_TYPE_SUPL;
    725     AGpsBearerType bearerType;
    726 
    727     switch (apnIpType) {
    728         case APN_IP_IPV4:
    729             bearerType = AGPS_APN_BEARER_IPV4;
    730             break;
    731         case APN_IP_IPV6:
    732             bearerType = AGPS_APN_BEARER_IPV6;
    733             break;
    734         case APN_IP_IPV4V6:
    735             bearerType = AGPS_APN_BEARER_IPV4V6;
    736             break;
    737         default:
    738             bearerType = AGPS_APN_BEARER_IPV4;
    739             break;
    740     }
    741 
    742     int ret_val = loc_eng_agps_open(loc_afw_data, agpsType, apn, bearerType);
    743 
    744     EXIT_LOG(%d, ret_val);
    745     return ret_val;
    746 }
    747 
    748 /*===========================================================================
    749 FUNCTION    loc_agps_closed
    750 
    751 DESCRIPTION
    752    This function is called when on-demand data connection closing is done.
    753 It should inform ARM 9 about the data close result.
    754 
    755 DEPENDENCIES
    756    NONE
    757 
    758 RETURN VALUE
    759    0
    760 
    761 SIDE EFFECTS
    762    N/A
    763 
    764 ===========================================================================*/
    765 static int loc_agps_closed()
    766 {
    767     ENTRY_LOG();
    768     AGpsType agpsType = AGPS_TYPE_SUPL;
    769     int ret_val = loc_eng_agps_closed(loc_afw_data, agpsType);
    770 
    771     EXIT_LOG(%d, ret_val);
    772     return ret_val;
    773 }
    774 
    775 /*===========================================================================
    776 FUNCTION    loc_agps_open_failed
    777 
    778 DESCRIPTION
    779    This function is called when on-demand data connection opening has failed.
    780 It should inform ARM 9 about the data open result.
    781 
    782 DEPENDENCIES
    783    NONE
    784 
    785 RETURN VALUE
    786    0
    787 
    788 SIDE EFFECTS
    789    N/A
    790 
    791 ===========================================================================*/
    792 int loc_agps_open_failed()
    793 {
    794     ENTRY_LOG();
    795     AGpsType agpsType = AGPS_TYPE_SUPL;
    796     int ret_val = loc_eng_agps_open_failed(loc_afw_data, agpsType);
    797 
    798     EXIT_LOG(%d, ret_val);
    799     return ret_val;
    800 }
    801 
    802 /*===========================================================================
    803 FUNCTION    loc_agps_set_server
    804 
    805 DESCRIPTION
    806    If loc_eng_set_server is called before loc_eng_init, it doesn't work. This
    807    proxy buffers server settings and calls loc_eng_set_server when the client is
    808    open.
    809 
    810 DEPENDENCIES
    811    NONE
    812 
    813 RETURN VALUE
    814    0
    815 
    816 SIDE EFFECTS
    817    N/A
    818 
    819 ===========================================================================*/
    820 static int loc_agps_set_server(AGpsType type, const char* hostname, int port)
    821 {
    822     ENTRY_LOG();
    823     LocServerType serverType;
    824     switch (type) {
    825     case AGPS_TYPE_SUPL:
    826         serverType = LOC_AGPS_SUPL_SERVER;
    827         break;
    828     case AGPS_TYPE_C2K:
    829         serverType = LOC_AGPS_CDMA_PDE_SERVER;
    830         break;
    831     default:
    832         serverType = LOC_AGPS_SUPL_SERVER;
    833     }
    834     int ret_val = loc_eng_set_server_proxy(loc_afw_data, serverType, hostname, port);
    835 
    836     EXIT_LOG(%d, ret_val);
    837     return ret_val;
    838 }
    839 
    840 /*===========================================================================
    841 FUNCTIONf571
    842     loc_xtra_init
    843 
    844 DESCRIPTION
    845    Initialize XTRA module.
    846 
    847 DEPENDENCIES
    848    None
    849 
    850 RETURN VALUE
    851    0: success
    852 
    853 SIDE EFFECTS
    854    N/A
    855 
    856 ===========================================================================*/
    857 static int loc_xtra_init(GpsXtraCallbacks* callbacks)
    858 {
    859     ENTRY_LOG();
    860     GpsXtraExtCallbacks extCallbacks;
    861     memset(&extCallbacks, 0, sizeof(extCallbacks));
    862     extCallbacks.download_request_cb = callbacks->download_request_cb;
    863     int ret_val = loc_eng_xtra_init(loc_afw_data, &extCallbacks);
    864 
    865     EXIT_LOG(%d, ret_val);
    866     return ret_val;
    867 }
    868 
    869 
    870 /*===========================================================================
    871 FUNCTION    loc_xtra_inject_data
    872 
    873 DESCRIPTION
    874    Initialize XTRA module.
    875 
    876 DEPENDENCIES
    877    None
    878 
    879 RETURN VALUE
    880    0: success
    881 
    882 SIDE EFFECTS
    883    N/A
    884 
    885 ===========================================================================*/
    886 static int loc_xtra_inject_data(char* data, int length)
    887 {
    888     ENTRY_LOG();
    889     int ret_val = -1;
    890     if( (data != NULL) && ((unsigned int)length <= XTRA_DATA_MAX_SIZE))
    891         ret_val = loc_eng_xtra_inject_data(loc_afw_data, data, length);
    892     else
    893         LOC_LOGE("%s, Could not inject XTRA data. Buffer address: %p, length: %d",
    894                  __func__, data, length);
    895     EXIT_LOG(%d, ret_val);
    896     return ret_val;
    897 }
    898 
    899 /*===========================================================================
    900 FUNCTION    loc_gps_measurement_init
    901 
    902 DESCRIPTION
    903    This function initializes the gps measurement interface
    904 
    905 DEPENDENCIES
    906    NONE
    907 
    908 RETURN VALUE
    909    None
    910 
    911 SIDE EFFECTS
    912    N/A
    913 
    914 ===========================================================================*/
    915 static int loc_gps_measurement_init(GpsMeasurementCallbacks* callbacks)
    916 {
    917     ENTRY_LOG();
    918     int ret_val = loc_eng_gps_measurement_init(loc_afw_data,
    919                                                callbacks);
    920 
    921     EXIT_LOG(%d, ret_val);
    922     return ret_val;
    923 }
    924 
    925 /*===========================================================================
    926 FUNCTION    loc_gps_measurement_close
    927 
    928 DESCRIPTION
    929    This function closes the gps measurement interface
    930 
    931 DEPENDENCIES
    932    NONE
    933 
    934 RETURN VALUE
    935    None
    936 
    937 SIDE EFFECTS
    938    N/A
    939 
    940 ===========================================================================*/
    941 static void loc_gps_measurement_close()
    942 {
    943     ENTRY_LOG();
    944     loc_eng_gps_measurement_close(loc_afw_data);
    945 
    946     EXIT_LOG(%s, VOID_RET);
    947 }
    948 
    949 /*===========================================================================
    950 FUNCTION    loc_ni_init
    951 
    952 DESCRIPTION
    953    This function initializes the NI interface
    954 
    955 DEPENDENCIES
    956    NONE
    957 
    958 RETURN VALUE
    959    None
    960 
    961 SIDE EFFECTS
    962    N/A
    963 
    964 ===========================================================================*/
    965 void loc_ni_init(GpsNiCallbacks *callbacks)
    966 {
    967     ENTRY_LOG();
    968     loc_eng_ni_init(loc_afw_data,(GpsNiExtCallbacks*) callbacks);
    969     EXIT_LOG(%s, VOID_RET);
    970 }
    971 
    972 /*===========================================================================
    973 FUNCTION    loc_ni_respond
    974 
    975 DESCRIPTION
    976    This function sends an NI respond to the modem processor
    977 
    978 DEPENDENCIES
    979    NONE
    980 
    981 RETURN VALUE
    982    None
    983 
    984 SIDE EFFECTS
    985    N/A
    986 
    987 ===========================================================================*/
    988 void loc_ni_respond(int notif_id, GpsUserResponseType user_response)
    989 {
    990     ENTRY_LOG();
    991     loc_eng_ni_respond(loc_afw_data, notif_id, user_response);
    992     EXIT_LOG(%s, VOID_RET);
    993 }
    994 
    995 // for XTRA
    996 static inline int createSocket() {
    997     int socketFd = -1;
    998 
    999     if ((socketFd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
   1000         LOC_LOGE("create socket error. reason:%s", strerror(errno));
   1001 
   1002      } else {
   1003         const char* socketPath = "/data/misc/location/xtra/socket_hal_xtra";
   1004         struct sockaddr_un addr = { .sun_family = AF_UNIX };
   1005         snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socketPath);
   1006 
   1007         if (::connect(socketFd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
   1008             LOC_LOGE("cannot connect to XTRA. reason:%s", strerror(errno));
   1009             if (::close(socketFd)) {
   1010                 LOC_LOGE("close socket error. reason:%s", strerror(errno));
   1011             }
   1012             socketFd = -1;
   1013         }
   1014     }
   1015 
   1016     return socketFd;
   1017 }
   1018 
   1019 static inline void closeSocket(const int socketFd) {
   1020     if (socketFd >= 0) {
   1021         if(::close(socketFd)) {
   1022             LOC_LOGE("close socket error. reason:%s", strerror(errno));
   1023         }
   1024     }
   1025 }
   1026 
   1027 static inline bool sendConnectionEvent(const bool connected, const int32_t type) {
   1028     int socketFd = createSocket();
   1029     if (socketFd < 0) {
   1030         LOC_LOGE("XTRA unreachable. sending failed.");
   1031         return false;
   1032     }
   1033 
   1034     std::stringstream ss;
   1035     ss <<  "connection";
   1036     ss << " " << (connected ? "1" : "0");
   1037     ss << " " << (int)type;
   1038     ss << "\n"; // append seperator
   1039 
   1040     const std::string& data = ss.str();
   1041     int remain = data.length();
   1042     ssize_t sent = 0;
   1043 
   1044     while (remain > 0 &&
   1045           (sent = ::send(socketFd, data.c_str() + (data.length() - remain),
   1046                        remain, MSG_NOSIGNAL)) > 0) {
   1047         remain -= sent;
   1048     }
   1049 
   1050     if (sent < 0) {
   1051         LOC_LOGE("sending error. reason:%s", strerror(errno));
   1052     }
   1053 
   1054     closeSocket(socketFd);
   1055 
   1056     return (remain == 0);
   1057 }
   1058 
   1059 // Below stub functions are members of sLocEngAGpsRilInterface
   1060 static void loc_agps_ril_init( AGpsRilCallbacks* callbacks ) {}
   1061 static void loc_agps_ril_set_ref_location(const AGpsRefLocation *agps_reflocation, size_t sz_struct) {}
   1062 static void loc_agps_ril_set_set_id(AGpsSetIDType type, const char* setid) {}
   1063 static void loc_agps_ril_ni_message(uint8_t *msg, size_t len) {}
   1064 static void loc_agps_ril_update_network_state(int connected, int type, int roaming, const char* extra_info) {
   1065     ENTRY_LOG();
   1066     // for XTRA
   1067     sendConnectionEvent((connected != 0) ? true : false, type);
   1068     EXIT_LOG(%s, VOID_RET);
   1069 }
   1070 
   1071 /*===========================================================================
   1072 FUNCTION    loc_agps_ril_update_network_availability
   1073 
   1074 DESCRIPTION
   1075    Sets data call allow vs disallow flag to modem
   1076    This is the only member of sLocEngAGpsRilInterface implemented.
   1077 
   1078 DEPENDENCIES
   1079    None
   1080 
   1081 RETURN VALUE
   1082    0: success
   1083 
   1084 SIDE EFFECTS
   1085    N/A
   1086 
   1087 ===========================================================================*/
   1088 static void loc_agps_ril_update_network_availability(int available, const char* apn)
   1089 {
   1090     ENTRY_LOG();
   1091     char baseband[PROPERTY_VALUE_MAX];
   1092     property_get("ro.baseband", baseband, "msm");
   1093     if (strcmp(baseband, "csfb") == 0)
   1094     {
   1095         loc_eng_agps_ril_update_network_availability(loc_afw_data, available, apn);
   1096     }
   1097     EXIT_LOG(%s, VOID_RET);
   1098 }
   1099 
   1100 static int loc_agps_install_certificates(const DerEncodedCertificate* certificates,
   1101                                          size_t length)
   1102 {
   1103     ENTRY_LOG();
   1104     int ret_val = loc_eng_agps_install_certificates(loc_afw_data, certificates, length);
   1105     EXIT_LOG(%d, ret_val);
   1106     return ret_val;
   1107 }
   1108 static int loc_agps_revoke_certificates(const Sha1CertificateFingerprint* fingerprints,
   1109                                         size_t length)
   1110 {
   1111     ENTRY_LOG();
   1112     LOC_LOGE("%s:%d]: agps_revoke_certificates not supported",__func__, __LINE__);
   1113     int ret_val = AGPS_CERTIFICATE_ERROR_GENERIC;
   1114     EXIT_LOG(%d, ret_val);
   1115     return ret_val;
   1116 }
   1117 
   1118 static void loc_configuration_update(const char* config_data, int32_t length)
   1119 {
   1120     ENTRY_LOG();
   1121     loc_eng_configuration_update(loc_afw_data, config_data, length);
   1122     switch (sGnssType)
   1123     {
   1124     case GNSS_GSS:
   1125     case GNSS_AUTO:
   1126     case GNSS_QCA1530:
   1127         //APQ
   1128         gps_conf.CAPABILITIES &= ~(GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
   1129         break;
   1130     }
   1131     EXIT_LOG(%s, VOID_RET);
   1132 }
   1133 
   1134 static void local_loc_cb(UlpLocation* location, void* locExt)
   1135 {
   1136     ENTRY_LOG();
   1137     if (NULL != location) {
   1138         CALLBACK_LOG_CALLFLOW("location_cb - from", %d, location->position_source);
   1139 
   1140         if (NULL != gps_loc_cb) {
   1141             gps_loc_cb(&location->gpsLocation);
   1142         }
   1143     }
   1144     EXIT_LOG(%s, VOID_RET);
   1145 }
   1146 
   1147 static void local_sv_cb(GpsSvStatus* sv_status, void* svExt)
   1148 {
   1149     ENTRY_LOG();
   1150     if (NULL != gps_sv_cb) {
   1151         CALLBACK_LOG_CALLFLOW("sv_status_cb -", %d, sv_status->num_svs);
   1152         gps_sv_cb(sv_status);
   1153     }
   1154     EXIT_LOG(%s, VOID_RET);
   1155 }
   1156 
   1157