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