summaryrefslogtreecommitdiffstats
path: root/gps/libloc_api_50001/LocApiAdapter.cpp
blob: cd356466785c5e171eb48ddc238fe86dcf31e3b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#define LOG_NDDEBUG 0
#define LOG_TAG "LocSvc_adapter"

#include <dlfcn.h>
#include <LocApiAdapter.h>
#include "loc_eng_msg.h"
#include "loc_log.h"
#include "loc_eng_ni.h"

static void* noProc(void* data)
{
    return NULL;
}

LocEng::LocEng(void* caller,
               LOC_API_ADAPTER_EVENT_MASK_T emask,
               gps_acquire_wakelock acqwl,
               gps_release_wakelock relwl,
               loc_msg_sender msgSender,
               loc_msg_sender msgUlpSender,
               loc_ext_parser posParser,
               loc_ext_parser svParser) :
        owner(caller),
        eventMask(emask), acquireWakelock(acqwl),
        releaseWakeLock(relwl), sendMsge(msgSender), sendUlpMsg(msgUlpSender),
        extPosInfo(NULL == posParser ? noProc : posParser),
        extSvInfo(NULL == svParser ? noProc : svParser)
{
    LOC_LOGV("LocEng constructor %p, %p", posParser, svParser);
}

LocApiAdapter::LocApiAdapter(LocEng &locEng) :
    locEngHandle(locEng), fixCriteria(), navigating(false)
{
    LOC_LOGD("LocApiAdapter created");
}

LocApiAdapter::~LocApiAdapter()
{
    LOC_LOGV("LocApiAdapter deleted");
}

LocApiAdapter* LocApiAdapter::getLocApiAdapter(LocEng &locEng)
{
    void* handle;
    LocApiAdapter* adapter = NULL;

    handle = dlopen ("libloc_api_v02.so", RTLD_NOW);

    if (!handle) {
        LOC_LOGI("%s: dlopen(libloc_api_v02.so) failed, trying to load libloc_api-rpc-qc.so", __FUNCTION__);
        handle = dlopen ("libloc_api-rpc-qc.so", RTLD_NOW);
    }
    else
        LOC_LOGE("%s: dlopen(libloc_api_v02.so) succeeded.", __FUNCTION__);

    if (!handle) {
        LOC_LOGI("%s: dlopen(libloc_api-rpc-qc.so) failed, constructing LocApiAdapter", __FUNCTION__);
        adapter = new LocApiAdapter(locEng);
    } else {
        getLocApiAdapter_t* getHandle = (getLocApiAdapter_t*)dlsym(handle, "_Z16getLocApiAdapterR6LocEng");
        if (!getHandle) {
            LOC_LOGE("%s: dlsym(getLocApiAdapter) failed", __FUNCTION__);
            return NULL;
        }
        adapter = (*getHandle)(locEng);
    }

    return adapter;
}

int LocApiAdapter::hexcode(char *hexstring, int string_size,
                        const char *data, int data_size)
{
   int i;
   for (i = 0; i < data_size; i++)
   {
      char ch = data[i];
      if (i*2 + 3 <= string_size)
      {
         snprintf(&hexstring[i*2], 3, "%02X", ch);
      }
      else {
         break;
      }
   }
   return i;
}

int LocApiAdapter::decodeAddress(char *addr_string, int string_size,
                               const char *data, int data_size)
{
    const char addr_prefix = 0x91;
    int i, idxOutput = 0;

    if (!data || !addr_string) { return 0; }

    if (data[0] != addr_prefix)
    {
        LOC_LOGW("decodeAddress: address prefix is not 0x%x but 0x%x", addr_prefix, data[0]);
        addr_string[0] = '\0';
        return 0; // prefix not correct
    }

    for (i = 1; i < data_size; i++)
    {
        unsigned char ch = data[i], low = ch & 0x0F, hi = ch >> 4;
        if (low <= 9 && idxOutput < string_size - 1) { addr_string[idxOutput++] = low + '0'; }
        if (hi <= 9 && idxOutput < string_size - 1) { addr_string[idxOutput++] = hi + '0'; }
    }

    addr_string[idxOutput] = '\0'; // Terminates the string

    return idxOutput;
}

void LocApiAdapter::reportPosition(GpsLocation &location,
                                   GpsLocationExtended &locationExtended,
                                   void* locationExt,
                                   enum loc_sess_status status,
                                   LocPosTechMask loc_technology_mask )
{
    loc_eng_msg_report_position *msg(new loc_eng_msg_report_position(locEngHandle.owner,
                                                                     location,
                                                                     locationExtended,
                                                                     locationExt,
                                                                     status,
                                                                     loc_technology_mask));
    if (locEngHandle.sendUlpMsg) {
        locEngHandle.sendUlpMsg(locEngHandle.owner, msg);
    } else {
        locEngHandle.sendMsge(locEngHandle.owner, msg);
    }
}

void LocApiAdapter::reportSv(GpsSvStatus &svStatus, GpsLocationExtended &locationExtended, void* svExt)
{
    loc_eng_msg_report_sv *msg(new loc_eng_msg_report_sv(locEngHandle.owner, svStatus, locationExtended, svExt));

    //We want to send SV info to ULP to help it in determining GNSS signal strength
    //ULP will forward the SV reports to HAL without any modifications
    if (locEngHandle.sendUlpMsg) {
        locEngHandle.sendUlpMsg(locEngHandle.owner, msg);
    } else {
        locEngHandle.sendMsge(locEngHandle.owner, msg);
    }
}

void LocApiAdapter::reportStatus(GpsStatusValue status)
{
    loc_eng_msg_report_status *msg(new loc_eng_msg_report_status(locEngHandle.owner, status));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::reportNmea(const char* nmea, int length)
{
    loc_eng_msg_report_nmea *msg(new loc_eng_msg_report_nmea(locEngHandle.owner, nmea, length));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::requestATL(int connHandle, AGpsType agps_type)
{
    loc_eng_msg_request_atl *msg(new loc_eng_msg_request_atl(locEngHandle.owner, connHandle, agps_type));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::releaseATL(int connHandle)
{
    loc_eng_msg_release_atl *msg(new loc_eng_msg_release_atl(locEngHandle.owner, connHandle));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::requestXtraData()
{
    LOC_LOGD("XTRA download request");

    loc_eng_msg *msg(new loc_eng_msg(locEngHandle.owner, LOC_ENG_MSG_REQUEST_XTRA_DATA));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::requestTime()
{
    LOC_LOGD("loc_event_cb: XTRA time download request");
    loc_eng_msg *msg(new loc_eng_msg(locEngHandle.owner, LOC_ENG_MSG_REQUEST_TIME));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::requestLocation()
{
    LOC_LOGD("loc_event_cb: XTRA time download request... not supported");
    // loc_eng_msg *msg(new loc_eng_msg(locEngHandle.owner, LOC_ENG_MSG_REQUEST_POSITION));
    // locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::requestNiNotify(GpsNiNotification &notif, const void* data)
{
    notif.size = sizeof(notif);
    notif.timeout     = LOC_NI_NO_RESPONSE_TIME;

    loc_eng_msg_request_ni *msg(new loc_eng_msg_request_ni(locEngHandle.owner, notif, data));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::handleEngineDownEvent()
{
    loc_eng_msg *msg(new loc_eng_msg(locEngHandle.owner, LOC_ENG_MSG_ENGINE_DOWN));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}

void LocApiAdapter::handleEngineUpEvent()
{
    loc_eng_msg *msg(new loc_eng_msg(locEngHandle.owner, LOC_ENG_MSG_ENGINE_UP));
    locEngHandle.sendMsge(locEngHandle.owner, msg);
}