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
37namespace 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
42int 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
60int 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
87struct LocSsrMsg : public LocMsg {
88    LocApiBase* mLocApi;
89    inline LocSsrMsg(LocApiBase* locApi) :
90        LocMsg(), mLocApi(locApi)
91    {
92        locallog();
93    }
94    inline virtual void proc() const {
95        mLocApi->close();
96        mLocApi->open(mLocApi->getEvtMask());
97    }
98    inline void locallog() {
99        LOC_LOGV("LocSsrMsg");
100    }
101    inline virtual void log() {
102        locallog();
103    }
104};
105
106LocApiBase::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
113LOC_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
122bool LocApiBase::isInSession()
123{
124    bool inSession = false;
125
126    TO_ALL_LOCADAPTERS(inSession = mLocAdapters[i]->isInSession());
127
128    return inSession;
129}
130
131void 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
142void 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
178void 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
187void LocApiBase::handleEngineDownEvent()
188{
189    // loop through adapters, and deliver to all adapters.
190    TO_ALL_LOCADAPTERS(mLocAdapters[i]->handleEngineDownEvent());
191}
192
193void 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
209void 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
221void LocApiBase::reportStatus(GpsStatusValue status)
222{
223    // loop through adapters, and deliver to all adapters.
224    TO_ALL_LOCADAPTERS(mLocAdapters[i]->reportStatus(status));
225}
226
227void 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
233void 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
241void LocApiBase::requestXtraData()
242{
243    // loop through adapters, and deliver to the first handling adapter.
244    TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestXtraData());
245}
246
247void LocApiBase::requestTime()
248{
249    // loop through adapters, and deliver to the first handling adapter.
250    TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestTime());
251}
252
253void LocApiBase::requestLocation()
254{
255    // loop through adapters, and deliver to the first handling adapter.
256    TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestLocation());
257}
258
259void 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
265void 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
271void 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
277void LocApiBase::reportDataCallOpened()
278{
279    // loop through adapters, and deliver to the first handling adapter.
280    TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportDataCallOpened());
281}
282
283void LocApiBase::reportDataCallClosed()
284{
285    // loop through adapters, and deliver to the first handling adapter.
286    TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportDataCallClosed());
287}
288
289void 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
295enum loc_api_adapter_err LocApiBase::
296   open(LOC_API_ADAPTER_EVENT_MASK_T mask)
297DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
298
299enum loc_api_adapter_err LocApiBase::
300    close()
301DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
302
303enum loc_api_adapter_err LocApiBase::
304    startFix(const LocPosMode& posMode)
305DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
306
307enum loc_api_adapter_err LocApiBase::
308    stopFix()
309DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
310
311enum loc_api_adapter_err LocApiBase::
312    deleteAidingData(GpsAidingData f)
313DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
314
315enum loc_api_adapter_err LocApiBase::
316    enableData(int enable)
317DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
318
319enum loc_api_adapter_err LocApiBase::
320    setAPN(char* apn, int len)
321DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
322
323enum loc_api_adapter_err LocApiBase::
324    injectPosition(double latitude, double longitude, float accuracy)
325DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
326
327enum loc_api_adapter_err LocApiBase::
328    setTime(GpsUtcTime time, int64_t timeReference, int uncertainty)
329DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
330
331enum loc_api_adapter_err LocApiBase::
332    setXtraData(char* data, int length)
333DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
334
335enum loc_api_adapter_err LocApiBase::
336    requestXtraServer()
337DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
338
339enum loc_api_adapter_err LocApiBase::
340   atlOpenStatus(int handle, int is_succ, char* apn,
341                 AGpsBearerType bear, AGpsType agpsType)
342DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
343
344enum loc_api_adapter_err LocApiBase::
345    atlCloseStatus(int handle, int is_succ)
346DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
347
348enum loc_api_adapter_err LocApiBase::
349    setPositionMode(const LocPosMode& posMode)
350DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
351
352enum loc_api_adapter_err LocApiBase::
353    setServer(const char* url, int len)
354DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
355
356enum loc_api_adapter_err LocApiBase::
357    setServer(unsigned int ip, int port,
358              LocServerType type)
359DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
360
361enum loc_api_adapter_err LocApiBase::
362    informNiResponse(GpsUserResponseType userResponse,
363                     const void* passThroughData)
364DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
365
366enum loc_api_adapter_err LocApiBase::
367    setSUPLVersion(uint32_t version)
368DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
369
370enum loc_api_adapter_err LocApiBase::
371    setLPPConfig(uint32_t profile)
372DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
373
374enum loc_api_adapter_err LocApiBase::
375    setSensorControlConfig(int sensorUsage)
376DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
377
378enum 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)
389DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
390
391enum 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)
402DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
403
404enum loc_api_adapter_err LocApiBase::
405    setExtPowerConfig(int isBatteryCharging)
406DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
407
408enum loc_api_adapter_err LocApiBase::
409    setAGLONASSProtocol(unsigned long aGlonassProtocol)
410DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS)
411
412int LocApiBase::
413    initDataServiceClient()
414DEFAULT_IMPL(-1)
415
416int LocApiBase::
417    openAndStartDataCall()
418DEFAULT_IMPL(-1)
419
420void LocApiBase::
421    stopDataCall()
422DEFAULT_IMPL()
423
424void LocApiBase::
425    closeDataCall()
426DEFAULT_IMPL()
427
428
429} // namespace loc_core
430