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