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