Home | History | Annotate | Download | only in core
      1 /* Copyright (c) 2011-2014, 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 #define LOG_NDDEBUG 0
     30 #define LOG_TAG "LocSvc_LocApiBase"
     31 
     32 #include <dlfcn.h>
     33 #include <LocApiBase.h>
     34 #include <LocAdapterBase.h>
     35 #include <log_util.h>
     36 #include <LocDualContext.h>
     37 
     38 namespace loc_core {
     39 
     40 #define TO_ALL_LOCADAPTERS(call) TO_ALL_ADAPTERS(mLocAdapters, (call))
     41 #define TO_1ST_HANDLING_LOCADAPTERS(call) TO_1ST_HANDLING_ADAPTER(mLocAdapters, (call))
     42 
     43 int hexcode(char *hexstring, int string_size,
     44             const char *data, int data_size)
     45 {
     46    int i;
     47    for (i = 0; i < data_size; i++)
     48    {
     49       char ch = data[i];
     50       if (i*2 + 3 <= string_size)
     51       {
     52          snprintf(&hexstring[i*2], 3, "%02X", ch);
     53       }
     54       else {
     55          break;
     56       }
     57    }
     58    return i;
     59 }
     60 
     61 int decodeAddress(char *addr_string, int string_size,
     62                    const char *data, int data_size)
     63 {
     64     const char addr_prefix = 0x91;
     65     int i, idxOutput = 0;
     66 
     67     if (!data || !addr_string) { return 0; }
     68 
     69     if (data[0] != addr_prefix)
     70     {
     71         LOC_LOGW("decodeAddress: address prefix is not 0x%x but 0x%x", addr_prefix, data[0]);
     72         addr_string[0] = '\0';
     73         return 0; // prefix not correct
     74     }
     75 
     76     for (i = 1; i < data_size; i++)
     77     {
     78         unsigned char ch = data[i], low = ch & 0x0F, hi = ch >> 4;
     79         if (low <= 9 && idxOutput < string_size - 1) { addr_string[idxOutput++] = low + '0'; }
     80         if (hi <= 9 && idxOutput < string_size - 1) { addr_string[idxOutput++] = hi + '0'; }
     81     }
     82 
     83     addr_string[idxOutput] = '\0'; // Terminates the string
     84 
     85     return idxOutput;
     86 }
     87 
     88 struct LocSsrMsg : public LocMsg {
     89     LocApiBase* mLocApi;
     90     inline LocSsrMsg(LocApiBase* locApi) :
     91         LocMsg(), mLocApi(locApi)
     92     {
     93         locallog();
     94     }
     95     inline virtual void proc() const {
     96         mLocApi->close();
     97         mLocApi->open(mLocApi->getEvtMask());
     98     }
     99     inline void locallog() {
    100         LOC_LOGV("LocSsrMsg");
    101     }
    102     inline virtual void log() {
    103         locallog();
    104     }
    105 };
    106 
    107 struct LocOpenMsg : public LocMsg {
    108     LocApiBase* mLocApi;
    109     LOC_API_ADAPTER_EVENT_MASK_T mMask;
    110     inline LocOpenMsg(LocApiBase* locApi,
    111                       LOC_API_ADAPTER_EVENT_MASK_T mask) :
    112         LocMsg(), mLocApi(locApi), mMask(mask)
    113     {
    114         locallog();
    115     }
    116     inline virtual void proc() const {
    117         mLocApi->open(mMask);
    118     }
    119     inline void locallog() {
    120         LOC_LOGV("%s:%d]: LocOpen Mask: %x\n",
    121                  __func__, __LINE__, mMask);
    122     }
    123     inline virtual void log() {
    124         locallog();
    125     }
    126 };
    127 
    128 LocApiBase::LocApiBase(const MsgTask* msgTask,
    129                        LOC_API_ADAPTER_EVENT_MASK_T excludedMask,
    130                        ContextBase* context) :
    131     mExcludedMask(excludedMask), mMsgTask(msgTask), mMask(0), mContext(context)
    132 {
    133     memset(mLocAdapters, 0, sizeof(mLocAdapters));
    134 }
    135 
    136 LOC_API_ADAPTER_EVENT_MASK_T LocApiBase::getEvtMask()
    137 {
    138     LOC_API_ADAPTER_EVENT_MASK_T mask = 0;
    139 
    140     TO_ALL_LOCADAPTERS(mask |= mLocAdapters[i]->getEvtMask());
    141 
    142     return mask & ~mExcludedMask;
    143 }
    144 
    145 bool LocApiBase::isInSession()
    146 {
    147     bool inSession = false;
    148 
    149     for (int i = 0;
    150          !inSession && i < MAX_ADAPTERS && NULL != mLocAdapters[i];
    151          i++) {
    152         inSession = mLocAdapters[i]->isInSession();
    153     }
    154 
    155     return inSession;
    156 }
    157 
    158 void LocApiBase::addAdapter(LocAdapterBase* adapter)
    159 {
    160     for (int i = 0; i < MAX_ADAPTERS && mLocAdapters[i] != adapter; i++) {
    161         if (mLocAdapters[i] == NULL) {
    162             mLocAdapters[i] = adapter;
    163             mMsgTask->sendMsg(new LocOpenMsg(this,
    164                                              (adapter->getEvtMask())));
    165             break;
    166         }
    167     }
    168 }
    169 
    170 void LocApiBase::removeAdapter(LocAdapterBase* adapter)
    171 {
    172     for (int i = 0;
    173          i < MAX_ADAPTERS && NULL != mLocAdapters[i];
    174          i++) {
    175         if (mLocAdapters[i] == adapter) {
    176             mLocAdapters[i] = NULL;
    177 
    178             // shift the rest of the adapters up so that the pointers
    179             // in the array do not have holes.  This should be more
    180             // performant, because the array maintenance is much much
    181             // less frequent than event handlings, which need to linear
    182             // search all the adapters
    183             int j = i;
    184             while (++i < MAX_ADAPTERS && mLocAdapters[i] != NULL);
    185 
    186             // i would be MAX_ADAPTERS or point to a NULL
    187             i--;
    188             // i now should point to a none NULL adapter within valid
    189             // range although i could be equal to j, but it won't hurt.
    190             // No need to check it, as it gains nothing.
    191             mLocAdapters[j] = mLocAdapters[i];
    192             // this makes sure that we exit the for loop
    193             mLocAdapters[i] = NULL;
    194 
    195             // if we have an empty list of adapters
    196             if (0 == i) {
    197                 close();
    198             } else {
    199                 // else we need to remove the bit
    200                 mMsgTask->sendMsg(new LocOpenMsg(this, getEvtMask()));
    201             }
    202         }
    203     }
    204 }
    205 
    206 void LocApiBase::handleEngineUpEvent()
    207 {
    208     // This will take care of renegotiating the loc handle
    209     mMsgTask->sendMsg(new LocSsrMsg(this));
    210 
    211     LocDualContext::injectFeatureConfig(mContext);
    212 
    213     // loop through adapters, and deliver to all adapters.
    214     TO_ALL_LOCADAPTERS(mLocAdapters[i]->handleEngineUpEvent());
    215 }
    216 
    217 void LocApiBase::handleEngineDownEvent()
    218 {
    219     // loop through adapters, and deliver to all adapters.
    220     TO_ALL_LOCADAPTERS(mLocAdapters[i]->handleEngineDownEvent());
    221 }
    222 
    223 void LocApiBase::reportPosition(UlpLocation &location,
    224                                 GpsLocationExtended &locationExtended,
    225                                 void* locationExt,
    226                                 enum loc_sess_status status,
    227                                 LocPosTechMask loc_technology_mask)
    228 {
    229     // loop through adapters, and deliver to all adapters.
    230     TO_ALL_LOCADAPTERS(
    231         mLocAdapters[i]->reportPosition(location,
    232                                         locationExtended,
    233                                         locationExt,
    234                                         status,
    235                                         loc_technology_mask)
    236     );
    237 }
    238 
    239 void LocApiBase::reportSv(GpsSvStatus &svStatus,
    240                   GpsLocationExtended &locationExtended,
    241                   void* svExt)
    242 {
    243     // loop through adapters, and deliver to all adapters.
    244     TO_ALL_LOCADAPTERS(
    245         mLocAdapters[i]->reportSv(svStatus,
    246                                      locationExtended,
    247                                      svExt)
    248     );
    249 }
    250 
    251 void LocApiBase::reportStatus(GpsStatusValue status)
    252 {
    253     // loop through adapters, and deliver to all adapters.
    254     TO_ALL_LOCADAPTERS(mLocAdapters[i]->reportStatus(status));
    255 }
    256 
    257 void LocApiBase::reportNmea(const char* nmea, int length)
    258 {
    259     // loop through adapters, and deliver to all adapters.
    260     TO_ALL_LOCADAPTERS(mLocAdapters[i]->reportNmea(nmea, length));
    261 }
    262 
    263 void LocApiBase::reportXtraServer(const char* url1, const char* url2,
    264                                   const char* url3, const int maxlength)
    265 {
    266     // loop through adapters, and deliver to the first handling adapter.
    267     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportXtraServer(url1, url2, url3, maxlength));
    268 
    269 }
    270 
    271 void LocApiBase::requestXtraData()
    272 {
    273     // loop through adapters, and deliver to the first handling adapter.
    274     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestXtraData());
    275 }
    276 
    277 void LocApiBase::requestTime()
    278 {
    279     // loop through adapters, and deliver to the first handling adapter.
    280     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestTime());
    281 }
    282 
    283 void LocApiBase::requestLocation()
    284 {
    285     // loop through adapters, and deliver to the first handling adapter.
    286     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestLocation());
    287 }
    288 
    289 void LocApiBase::requestATL(int connHandle, AGpsType agps_type)
    290 {
    291     // loop through adapters, and deliver to the first handling adapter.
    292     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestATL(connHandle, agps_type));
    293 }
    294 
    295 void LocApiBase::releaseATL(int connHandle)
    296 {
    297     // loop through adapters, and deliver to the first handling adapter.
    298     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->releaseATL(connHandle));
    299 }
    300 
    301 void LocApiBase::requestSuplES(int connHandle)
    302 {
    303     // loop through adapters, and deliver to the first handling adapter.
    304     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestSuplES(connHandle));
    305 }
    306 
    307 void LocApiBase::reportDataCallOpened()
    308 {
    309     // loop through adapters, and deliver to the first handling adapter.
    310     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportDataCallOpened());
    311 }
    312 
    313 void LocApiBase::reportDataCallClosed()
    314 {
    315     // loop through adapters, and deliver to the first handling adapter.
    316     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportDataCallClosed());
    317 }
    318 
    319 void LocApiBase::requestNiNotify(GpsNiNotification &notify, const void* data)
    320 {
    321     // loop through adapters, and deliver to the first handling adapter.
    322     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestNiNotify(notify, data));
    323 }
    324 
    325 void* LocApiBase :: getSibling()
    326     DEFAULT_IMPL(NULL)
    327 
    328 LocApiProxyBase* LocApiBase :: getLocApiProxy()
    329     DEFAULT_IMPL(NULL)
    330 
    331 enum loc_api_adapter_err LocApiBase::
    332    open(LOC_API_ADAPTER_EVENT_MASK_T mask)
    333 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    334 
    335 enum loc_api_adapter_err LocApiBase::
    336     close()
    337 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    338 
    339 enum loc_api_adapter_err LocApiBase::
    340     startFix(const LocPosMode& posMode)
    341 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    342 
    343 enum loc_api_adapter_err LocApiBase::
    344     stopFix()
    345 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    346 
    347 enum loc_api_adapter_err LocApiBase::
    348     deleteAidingData(GpsAidingData f)
    349 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    350 
    351 enum loc_api_adapter_err LocApiBase::
    352     enableData(int enable)
    353 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    354 
    355 enum loc_api_adapter_err LocApiBase::
    356     setAPN(char* apn, int len)
    357 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    358 
    359 enum loc_api_adapter_err LocApiBase::
    360     injectPosition(double latitude, double longitude, float accuracy)
    361 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    362 
    363 enum loc_api_adapter_err LocApiBase::
    364     setTime(GpsUtcTime time, int64_t timeReference, int uncertainty)
    365 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    366 
    367 enum loc_api_adapter_err LocApiBase::
    368     setXtraData(char* data, int length)
    369 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    370 
    371 enum loc_api_adapter_err LocApiBase::
    372     requestXtraServer()
    373 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    374 
    375 enum loc_api_adapter_err LocApiBase::
    376    atlOpenStatus(int handle, int is_succ, char* apn,
    377                  AGpsBearerType bear, AGpsType agpsType)
    378 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    379 
    380 enum loc_api_adapter_err LocApiBase::
    381     atlCloseStatus(int handle, int is_succ)
    382 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    383 
    384 enum loc_api_adapter_err LocApiBase::
    385     setPositionMode(const LocPosMode& posMode)
    386 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    387 
    388 enum loc_api_adapter_err LocApiBase::
    389     setServer(const char* url, int len)
    390 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    391 
    392 enum loc_api_adapter_err LocApiBase::
    393     setServer(unsigned int ip, int port,
    394               LocServerType type)
    395 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    396 
    397 enum loc_api_adapter_err LocApiBase::
    398     informNiResponse(GpsUserResponseType userResponse,
    399                      const void* passThroughData)
    400 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    401 
    402 enum loc_api_adapter_err LocApiBase::
    403     setSUPLVersion(uint32_t version)
    404 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    405 
    406 enum loc_api_adapter_err LocApiBase::
    407     setLPPConfig(uint32_t profile)
    408 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    409 
    410 enum loc_api_adapter_err LocApiBase::
    411     setSensorControlConfig(int sensorUsage,
    412                            int sensorProvider)
    413 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    414 
    415 enum loc_api_adapter_err LocApiBase::
    416     setSensorProperties(bool gyroBiasVarianceRandomWalk_valid,
    417                         float gyroBiasVarianceRandomWalk,
    418                         bool accelBiasVarianceRandomWalk_valid,
    419                         float accelBiasVarianceRandomWalk,
    420                         bool angleBiasVarianceRandomWalk_valid,
    421                         float angleBiasVarianceRandomWalk,
    422                         bool rateBiasVarianceRandomWalk_valid,
    423                         float rateBiasVarianceRandomWalk,
    424                         bool velocityBiasVarianceRandomWalk_valid,
    425                         float velocityBiasVarianceRandomWalk)
    426 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    427 
    428 enum loc_api_adapter_err LocApiBase::
    429     setSensorPerfControlConfig(int controlMode,
    430                                int accelSamplesPerBatch,
    431                                int accelBatchesPerSec,
    432                                int gyroSamplesPerBatch,
    433                                int gyroBatchesPerSec,
    434                                int accelSamplesPerBatchHigh,
    435                                int accelBatchesPerSecHigh,
    436                                int gyroSamplesPerBatchHigh,
    437                                int gyroBatchesPerSecHigh,
    438                                int algorithmConfig)
    439 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    440 
    441 enum loc_api_adapter_err LocApiBase::
    442     setExtPowerConfig(int isBatteryCharging)
    443 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    444 
    445 enum loc_api_adapter_err LocApiBase::
    446     setAGLONASSProtocol(unsigned long aGlonassProtocol)
    447 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    448 
    449 enum loc_api_adapter_err LocApiBase::
    450    getWwanZppFix(GpsLocation & zppLoc)
    451 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    452 
    453 enum loc_api_adapter_err LocApiBase::
    454    getBestAvailableZppFix(GpsLocation & zppLoc)
    455 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    456 
    457 enum loc_api_adapter_err LocApiBase::
    458    getBestAvailableZppFix(GpsLocation & zppLoc, LocPosTechMask & tech_mask)
    459 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
    460 
    461 int LocApiBase::
    462     initDataServiceClient()
    463 DEFAULT_IMPL(-1)
    464 
    465 int LocApiBase::
    466     openAndStartDataCall()
    467 DEFAULT_IMPL(-1)
    468 
    469 void LocApiBase::
    470     stopDataCall()
    471 DEFAULT_IMPL()
    472 
    473 void LocApiBase::
    474     closeDataCall()
    475 DEFAULT_IMPL()
    476 
    477 int LocApiBase::
    478     setGpsLock(LOC_GPS_LOCK_MASK lock)
    479 DEFAULT_IMPL(-1)
    480 
    481 void LocApiBase::
    482     installAGpsCert(const DerEncodedCertificate* pData,
    483                     size_t length,
    484                     uint32_t slotBitMask)
    485 DEFAULT_IMPL()
    486 
    487 int LocApiBase::
    488     getGpsLock()
    489 DEFAULT_IMPL(-1)
    490 } // namespace loc_core
    491