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