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