1/* Copyright (c) 2009-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
30#define LOG_NDDEBUG 0
31#define LOG_TAG "LocSvc_eng"
32
33#include <loc_eng.h>
34#include <MsgTask.h>
35#include "log_util.h"
36#include "platform_lib_includes.h"
37
38using namespace loc_core;
39
40struct LocEngRequestXtraServer : public LocMsg {
41    LocEngAdapter* mAdapter;
42    inline LocEngRequestXtraServer(LocEngAdapter* adapter) :
43        LocMsg(), mAdapter(adapter)
44    {
45        locallog();
46    }
47    inline virtual void proc() const {
48        mAdapter->requestXtraServer();
49    }
50    inline void locallog() const {
51        LOC_LOGV("LocEngRequestXtraServer");
52    }
53    inline virtual void log() const {
54        locallog();
55    }
56};
57
58struct LocEngInjectXtraData : public LocMsg {
59    LocEngAdapter* mAdapter;
60    char* mData;
61    const int mLen;
62    inline LocEngInjectXtraData(LocEngAdapter* adapter,
63                                char* data, int len):
64        LocMsg(), mAdapter(adapter),
65        mData(new char[len]), mLen(len)
66    {
67        memcpy((void*)mData, (void*)data, len);
68        locallog();
69    }
70    inline ~LocEngInjectXtraData()
71    {
72        delete[] mData;
73    }
74    inline virtual void proc() const {
75        mAdapter->setXtraData(mData, mLen);
76    }
77    inline  void locallog() const {
78        LOC_LOGV("length: %d\n  data: %p", mLen, mData);
79    }
80    inline virtual void log() const {
81        locallog();
82    }
83};
84
85struct LocEngSetXtraVersionCheck : public LocMsg {
86    LocEngAdapter *mAdapter;
87    int mCheck;
88    inline LocEngSetXtraVersionCheck(LocEngAdapter* adapter,
89                                        int check):
90        mAdapter(adapter), mCheck(check) {}
91    inline virtual void proc() const {
92        locallog();
93        mAdapter->setXtraVersionCheck(mCheck);
94    }
95    inline void locallog() const {
96        LOC_LOGD("%s:%d]: mCheck: %d",
97                 __func__, __LINE__, mCheck);
98    }
99    inline virtual void log() const {
100        locallog();
101    }
102};
103
104/*===========================================================================
105FUNCTION    loc_eng_xtra_init
106
107DESCRIPTION
108   Initialize XTRA module.
109
110DEPENDENCIES
111   N/A
112
113RETURN VALUE
114   0: success
115
116SIDE EFFECTS
117   N/A
118
119===========================================================================*/
120int loc_eng_xtra_init (loc_eng_data_s_type &loc_eng_data,
121                       GpsXtraExtCallbacks* callbacks)
122{
123    int ret_val = -1;
124    loc_eng_xtra_data_s_type *xtra_module_data_ptr;
125    ENTRY_LOG();
126
127    if(callbacks == NULL) {
128        LOC_LOGE("loc_eng_xtra_init: failed, cb is NULL");
129    } else {
130        xtra_module_data_ptr = &loc_eng_data.xtra_module_data;
131        xtra_module_data_ptr->download_request_cb = callbacks->download_request_cb;
132        xtra_module_data_ptr->report_xtra_server_cb = callbacks->report_xtra_server_cb;
133
134        ret_val = 0;
135    }
136    EXIT_LOG(%d, ret_val);
137    return ret_val;
138}
139
140/*===========================================================================
141FUNCTION    loc_eng_xtra_inject_data
142
143DESCRIPTION
144   Injects XTRA file into the engine but buffers the data if engine is busy.
145
146DEPENDENCIES
147   N/A
148
149RETURN VALUE
150   0
151
152SIDE EFFECTS
153   N/A
154
155===========================================================================*/
156int loc_eng_xtra_inject_data(loc_eng_data_s_type &loc_eng_data,
157                             char* data, int length)
158{
159    ENTRY_LOG();
160    LocEngAdapter* adapter = loc_eng_data.adapter;
161    adapter->sendMsg(new LocEngInjectXtraData(adapter, data, length));
162    EXIT_LOG(%d, 0);
163    return 0;
164}
165/*===========================================================================
166FUNCTION    loc_eng_xtra_request_server
167
168DESCRIPTION
169   Request the Xtra server url from the modem
170
171DEPENDENCIES
172   N/A
173
174RETURN VALUE
175   0
176
177SIDE EFFECTS
178   N/A
179
180===========================================================================*/
181int loc_eng_xtra_request_server(loc_eng_data_s_type &loc_eng_data)
182{
183    ENTRY_LOG();
184    LocEngAdapter* adapter = loc_eng_data.adapter;
185    adapter->sendMsg(new LocEngRequestXtraServer(adapter));
186    EXIT_LOG(%d, 0);
187    return 0;
188}
189/*===========================================================================
190FUNCTION    loc_eng_xtra_version_check
191
192DESCRIPTION
193   Injects the enable/disable value for checking XTRA version
194   that is specified in gps.conf
195
196DEPENDENCIES
197   N/A
198
199RETURN VALUE
200   none
201
202SIDE EFFECTS
203   N/A
204
205===========================================================================*/
206void loc_eng_xtra_version_check(loc_eng_data_s_type &loc_eng_data,
207                                int check)
208{
209    ENTRY_LOG();
210    LocEngAdapter *adapter = loc_eng_data.adapter;
211    adapter->sendMsg(new LocEngSetXtraVersionCheck(adapter, check));
212    EXIT_LOG(%d, 0);
213}
214