Home | History | Annotate | Download | only in gnss
      1 /* Copyright (c) 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 #include "GnssAdapter.h"
     31 #include "location_interface.h"
     32 
     33 static GnssAdapter* gGnssAdapter = NULL;
     34 
     35 static void initialize();
     36 static void deinitialize();
     37 
     38 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks);
     39 static void removeClient(LocationAPI* client);
     40 static void requestCapabilities(LocationAPI* client);
     41 
     42 static uint32_t startTracking(LocationAPI* client, LocationOptions& options);
     43 static void updateTrackingOptions(LocationAPI* client, uint32_t id, LocationOptions& options);
     44 static void stopTracking(LocationAPI* client, uint32_t id);
     45 
     46 static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response);
     47 static uint32_t gnssDeleteAidingData(GnssAidingData& data);
     48 
     49 static void setControlCallbacks(LocationControlCallbacks& controlCallbacks);
     50 static uint32_t enable(LocationTechnologyType techType);
     51 static void disable(uint32_t id);
     52 static uint32_t* gnssUpdateConfig(GnssConfig config);
     53 
     54 static void injectLocation(double latitude, double longitude, float accuracy);
     55 static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty);
     56 
     57 static void agpsInit(const AgpsCbInfo& cbInfo);
     58 static void agpsDataConnOpen(AGpsExtType agpsType, const char* apnName, int apnLen, int ipType);
     59 static void agpsDataConnClosed(AGpsExtType agpsType);
     60 static void agpsDataConnFailed(AGpsExtType agpsType);
     61 static void getDebugReport(GnssDebugReport& report);
     62 static void updateConnectionStatus(bool connected, uint8_t type);
     63 
     64 static const GnssInterface gGnssInterface = {
     65     sizeof(GnssInterface),
     66     initialize,
     67     deinitialize,
     68     addClient,
     69     removeClient,
     70     requestCapabilities,
     71     startTracking,
     72     updateTrackingOptions,
     73     stopTracking,
     74     gnssNiResponse,
     75     setControlCallbacks,
     76     enable,
     77     disable,
     78     gnssUpdateConfig,
     79     gnssDeleteAidingData,
     80     injectLocation,
     81     injectTime,
     82     agpsInit,
     83     agpsDataConnOpen,
     84     agpsDataConnClosed,
     85     agpsDataConnFailed,
     86     getDebugReport,
     87     updateConnectionStatus,
     88 };
     89 
     90 #ifndef DEBUG_X86
     91 extern "C" const GnssInterface* getGnssInterface()
     92 #else
     93 const GnssInterface* getGnssInterface()
     94 #endif // DEBUG_X86
     95 {
     96    return &gGnssInterface;
     97 }
     98 
     99 static void initialize()
    100 {
    101     if (NULL == gGnssAdapter) {
    102         gGnssAdapter = new GnssAdapter();
    103     }
    104 }
    105 
    106 static void deinitialize()
    107 {
    108     if (NULL != gGnssAdapter) {
    109         delete gGnssAdapter;
    110         gGnssAdapter = NULL;
    111     }
    112 }
    113 
    114 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks)
    115 {
    116     if (NULL != gGnssAdapter) {
    117         gGnssAdapter->addClientCommand(client, callbacks);
    118     }
    119 }
    120 
    121 static void removeClient(LocationAPI* client)
    122 {
    123     if (NULL != gGnssAdapter) {
    124         gGnssAdapter->removeClientCommand(client);
    125     }
    126 }
    127 
    128 static void requestCapabilities(LocationAPI* client)
    129 {
    130     if (NULL != gGnssAdapter) {
    131         gGnssAdapter->requestCapabilitiesCommand(client);
    132     }
    133 }
    134 
    135 static uint32_t startTracking(LocationAPI* client, LocationOptions& options)
    136 {
    137     if (NULL != gGnssAdapter) {
    138         return gGnssAdapter->startTrackingCommand(client, options);
    139     } else {
    140         return 0;
    141     }
    142 }
    143 
    144 static void updateTrackingOptions(LocationAPI* client, uint32_t id, LocationOptions& options)
    145 {
    146     if (NULL != gGnssAdapter) {
    147         gGnssAdapter->updateTrackingOptionsCommand(client, id, options);
    148     }
    149 }
    150 
    151 static void stopTracking(LocationAPI* client, uint32_t id)
    152 {
    153     if (NULL != gGnssAdapter) {
    154         gGnssAdapter->stopTrackingCommand(client, id);
    155     }
    156 }
    157 
    158 static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response)
    159 {
    160     if (NULL != gGnssAdapter) {
    161         gGnssAdapter->gnssNiResponseCommand(client, id, response);
    162     }
    163 }
    164 
    165 static void setControlCallbacks(LocationControlCallbacks& controlCallbacks)
    166 {
    167     if (NULL != gGnssAdapter) {
    168         return gGnssAdapter->setControlCallbacksCommand(controlCallbacks);
    169     }
    170 }
    171 
    172 static uint32_t enable(LocationTechnologyType techType)
    173 {
    174     if (NULL != gGnssAdapter) {
    175         return gGnssAdapter->enableCommand(techType);
    176     } else {
    177         return 0;
    178     }
    179 }
    180 
    181 static void disable(uint32_t id)
    182 {
    183     if (NULL != gGnssAdapter) {
    184         return gGnssAdapter->disableCommand(id);
    185     }
    186 }
    187 
    188 static uint32_t* gnssUpdateConfig(GnssConfig config)
    189 {
    190     if (NULL != gGnssAdapter) {
    191         return gGnssAdapter->gnssUpdateConfigCommand(config);
    192     } else {
    193         return NULL;
    194     }
    195 }
    196 
    197 static uint32_t gnssDeleteAidingData(GnssAidingData& data)
    198 {
    199     if (NULL != gGnssAdapter) {
    200         return gGnssAdapter->gnssDeleteAidingDataCommand(data);
    201     } else {
    202         return 0;
    203     }
    204 }
    205 
    206 static void injectLocation(double latitude, double longitude, float accuracy)
    207 {
    208    if (NULL != gGnssAdapter) {
    209        gGnssAdapter->injectLocationCommand(latitude, longitude, accuracy);
    210    }
    211 }
    212 
    213 static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty)
    214 {
    215    if (NULL != gGnssAdapter) {
    216        gGnssAdapter->injectTimeCommand(time, timeReference, uncertainty);
    217    }
    218 }
    219 
    220 static void agpsInit(const AgpsCbInfo& cbInfo) {
    221 
    222     if (NULL != gGnssAdapter) {
    223         gGnssAdapter->initAgpsCommand(cbInfo);
    224     }
    225 }
    226 static void agpsDataConnOpen(
    227         AGpsExtType agpsType, const char* apnName, int apnLen, int ipType) {
    228 
    229     if (NULL != gGnssAdapter) {
    230         gGnssAdapter->dataConnOpenCommand(
    231                 agpsType, apnName, apnLen, ipType);
    232     }
    233 }
    234 static void agpsDataConnClosed(AGpsExtType agpsType) {
    235 
    236     if (NULL != gGnssAdapter) {
    237         gGnssAdapter->dataConnClosedCommand(agpsType);
    238     }
    239 }
    240 static void agpsDataConnFailed(AGpsExtType agpsType) {
    241 
    242     if (NULL != gGnssAdapter) {
    243         gGnssAdapter->dataConnFailedCommand(agpsType);
    244     }
    245 }
    246 
    247 static void getDebugReport(GnssDebugReport& report) {
    248 
    249     if (NULL != gGnssAdapter) {
    250         gGnssAdapter->getDebugReport(report);
    251     }
    252 }
    253 
    254 static void updateConnectionStatus(bool connected, uint8_t type) {
    255     if (NULL != gGnssAdapter) {
    256         gGnssAdapter->getSystemStatus()->eventConnectionStatus(connected, type);
    257     }
    258 }