blob: 30de33a99f2aae23c3398c3fc307ab8a9039f31c [file] [log] [blame]
b.liu8f231a12024-05-31 17:55:06 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <fcntl.h>
5#include <pthread.h>
6#include <stdarg.h>
7#include <unistd.h>
8#include <libubox/ustream.h>
9#include <libubus.h>
10
11#include "gnss_info.h"
12#include "mbtk_log.h"
13
14//static struct blob_buf b;
15static struct blob_buf gps_blob;
16
17int gnss_init(uint32 print_port);
18int gnss_deinit();
19int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len);
20
21const struct blobmsg_policy gnss_init_policy[] ={
22 [0] = {
23 .name = "gnss_init_param",
24 .type = BLOBMSG_TYPE_INT32,
25 },
26};
27
28
29const struct blobmsg_policy get_agps_policy[] ={
30 [0] = {
31 .name = "server_name",
32 .type = BLOBMSG_TYPE_STRING,
33 },
34 [1] = {
35 .name = "alam_flag",
36 .type = BLOBMSG_TYPE_INT32,
37 },
38};
39
40const struct blobmsg_policy gnss_sleep_policy[] ={
41 [0] = {
42 .name = "gnss_sleep_param",
43 .type = BLOBMSG_TYPE_INT32,
44 },
45};
46
47const struct blobmsg_policy gnss_setting_policy[] ={
48 [0] = {
49 .name = "gnss_setting_param",
50 .type = BLOBMSG_TYPE_STRING,
51 },
52};
53
54static int gps_ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
55{
56 blob_buf_init(&gps_blob, 0);
57 blobmsg_add_u32(&gps_blob, "event", ret);
58 ubus_send_reply(ctx, req, gps_blob.head);
59 return 0;
60}
61
62static int gps_ubus_string_reply(struct ubus_context *ctx, struct ubus_request_data *req, char *str)
63{
64 blob_buf_init(&gps_blob, 0);
65 blobmsg_add_string(&gps_blob, "gps_state_resp", str);
66 ubus_send_reply(ctx, req, gps_blob.head);
67 return 0;
68}
69
70static int ubus_gnss_init(struct ubus_context *ctx, struct ubus_object *obj,
71 struct ubus_request_data *req, const char *method,
72 struct blob_attr *msg)
73{
74 UNUSED(obj);
75 UNUSED(method);
76 struct blob_attr *tb[1];
77 struct blob_attr *cur;
78 int init_mode = 0, err = 0, ret = 0;
79 int status = 0;
80
81 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
82 err = blobmsg_parse(gnss_init_policy, 1, tb, blob_data(msg), blob_len(msg));
83 if (err < 0)
84 {
85 LOGE("blobmsg_parse fail");
86 return -1;
87 }
88 cur = tb[0];
89 if (!cur) {
90 LOGE("missing parameter");
91 return -2;
92 }
93
94 init_mode = blobmsg_get_u32(cur);
95 LOGD("init_mode=%d", init_mode);
96 if(init_mode == 0) { // Close gnss.
97 ret = gnss_deinit();
98 } else {
99 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) == init_mode) {
100 ret = gnss_init(init_mode);
101 } else { // ARG error, no print nmea.
102 ret = gnss_init(0);
103 }
104 }
105
106 LOGD("ubus_gnss_init() ret=%d", ret);
107 gps_ubus_send_reply(ctx, req, ret);
108
109 return 0;
110}
111
112static int ubus_gnss_deinit(struct ubus_context *ctx, struct ubus_object *obj,
113 struct ubus_request_data *req, const char *method,
114 struct blob_attr *msg)
115{
116 UNUSED(ctx);
117 UNUSED(obj);
118 UNUSED(req);
119 UNUSED(method);
120 UNUSED(msg);
121
122 int ret = 0;
123
124 ret = gnss_deinit();
125
126 gps_ubus_send_reply(ctx, req, ret);
127
128 return 0;
129}
130
131static int ubus_gnss_get_agps(struct ubus_context *ctx, struct ubus_object *obj,
132 struct ubus_request_data *req, const char *method,
133 struct blob_attr *msg)
134{
135 UNUSED(obj);
136 UNUSED(method);
137 struct blob_attr *tb[ARRAY_SIZE(get_agps_policy)];
138 struct blob_attr *cur;
139 char *server_name = NULL;
140 int err = 0, alm_flag = 0, ret = 0;
141
142 err = blobmsg_parse(get_agps_policy, ARRAY_SIZE(get_agps_policy), tb, blob_data(msg), blob_len(msg));
143 if (err < 0)
144 {
145 LOGE("blobmsg_parse table fail");
146 return -1;
147 }
148
149 cur = tb[0];
150 if (cur)
151 server_name = blobmsg_get_string(cur);
152 else
153 LOGE("missing parameter1");
154
155 cur = tb[1];
156 if (cur)
157 alm_flag = blobmsg_get_u32(cur);
158 else
159 LOGE("missing parameter2");
160
161 LOGD("server_name=%s, alm_flag=%d", server_name, alm_flag);
162
163 if (server_name)
164 {
165 ret = 3;
166 }
167
168 gps_ubus_send_reply(ctx, req, ret);
169
170 return 0;
171}
172
173static int ubus_gnss_set_agps(struct ubus_context *ctx, struct ubus_object *obj,
174 struct ubus_request_data *req, const char *method,
175 struct blob_attr *msg)
176{
177 UNUSED(ctx);
178 UNUSED(obj);
179 UNUSED(req);
180 UNUSED(method);
181 UNUSED(msg);
182
183 int err = 0, ret = 0;
184
185 ret = 4;
186
187
188 gps_ubus_send_reply(ctx, req, ret);
189
190 return 0;
191}
192
193static int ubus_gnss_sleep(struct ubus_context *ctx, struct ubus_object *obj,
194 struct ubus_request_data *req, const char *method,
195 struct blob_attr *msg)
196{
197 UNUSED(ctx);
198 UNUSED(obj);
199 UNUSED(req);
200 UNUSED(method);
201 struct blob_attr *tb[1];
202 struct blob_attr *cur;
203 int workmode = 0, err = 0;
204 int status = 0;
205
206 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
207 err = blobmsg_parse(gnss_sleep_policy, 1, tb, blob_data(msg), blob_len(msg));
208 if (err < 0)
209 {
210 LOGE("blobmsg_parse fail");
211 return -1;
212 }
213 cur = tb[0];
214 if (!cur) {
215 LOGE("missing parameter");
216 return -2;
217 }
218
219 workmode = blobmsg_get_u32(cur);
220 LOGD("workMode=%d", workmode);
221 /* Goto Sleeping.....*/
222
223 LOGD("ret=%d", status);
224 return 0;
225}
226
227static int ubus_gnss_setting(struct ubus_context *ctx, struct ubus_object *obj,
228 struct ubus_request_data *req, const char *method,
229 struct blob_attr *msg)
230{
231 UNUSED(ctx);
232 UNUSED(obj);
233 UNUSED(req);
234 UNUSED(method);
235 struct blob_attr *tb[1];
236 struct blob_attr *cur;
237 int err = 0, ret = 0;
238 char *gpsCfg = NULL;
239 int status = 0;
240
241 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
242 err = blobmsg_parse(gnss_setting_policy, 1, tb, blob_data(msg), blob_len(msg));
243 if (err < 0)
244 {
245 LOGE("blobmsg_parse fail");
246 return -1;
247 }
248 cur = tb[0];
249 if (!cur) {
250 LOGE("missing parameter");
251 return -2;
252 }
253
254 gpsCfg = blobmsg_get_string(cur);
255 LOGD("gpsCfg=%s", gpsCfg);
256
257 char rsp[1024];
258 ret = gnss_set(gpsCfg, strlen(gpsCfg), rsp, 1024);
259
260 LOGD("ret=%d", ret);
261 gps_ubus_send_reply(ctx, req, ret);
262
263 return 0;
264}
265
266#define ASR_GNSS_STATUS_LEN 128
267static int ubus_gnss_get_state(struct ubus_context *ctx, struct ubus_object *obj,
268 struct ubus_request_data *req, const char *method,
269 struct blob_attr *msg)
270{
271 UNUSED(obj);
272 UNUSED(req);
273 UNUSED(method);
274 UNUSED(msg);
275
276 char tmpBuf[ASR_GNSS_STATUS_LEN] = {0};
277 int i = 0;
278 int len = 0;
279 int ret = 0;
280 int num = 0;
281
282 //ret = asr_gnss_get_gps_info(&param);
283 if (0 == ret) {
284 len = snprintf(tmpBuf, sizeof(tmpBuf), "%d, %d, %d, %d, %d;", 0, 0, 0, 0, 0);
285 num = 6;
286 for(i=0; i<num; i++) {
287 len += sprintf(&tmpBuf[len], " %d, %d;", 1, 2);
288
289 if(len > ASR_GNSS_STATUS_LEN)
290 break;
291 }
292
293 LOGD("[%d]tmpBuf=%s", len, tmpBuf);
294 gps_ubus_string_reply(ctx, req, tmpBuf);
295 }
296
297 return 0;
298}
299
300static const struct ubus_method gps_ubus_methods[] = {
301 UBUS_METHOD("gnss_init", ubus_gnss_init, gnss_init_policy),
302 UBUS_METHOD_NOARG("gnss_deinit", ubus_gnss_deinit),
303 UBUS_METHOD("gnss_get_agps", ubus_gnss_get_agps, get_agps_policy),
304 UBUS_METHOD_NOARG("gnss_set_agps", ubus_gnss_set_agps),
305 UBUS_METHOD("gnss_sleep", ubus_gnss_sleep, gnss_sleep_policy),
306 UBUS_METHOD("gnss_setting", ubus_gnss_setting, gnss_setting_policy),
307 UBUS_METHOD_NOARG("gnss_get_state", ubus_gnss_get_state),
308};
309
310static struct ubus_object_type gps_object_type =
311 UBUS_OBJECT_TYPE("mbtk_gnss", gps_ubus_methods);
312
313static struct ubus_object gps_ubus_obj = {
314 .name = "mbtk_gnss",
315 .type = &gps_object_type,
316 .methods = gps_ubus_methods,
317 .n_methods = ARRAY_SIZE(gps_ubus_methods),
318};
319
320int gnss_ubus_exit(struct ubus_context *ctx)
321{
322 if(!ctx) {
323 return -1;
324 }
325
326 ubus_remove_object(ctx, &gps_ubus_obj);
327 ubus_free(ctx);
328 uloop_done();
329
330 LOGD("ubus exit done");
331 return 0;
332}
333
334struct ubus_context *gnss_ubus_init(void)
335{
336 struct ubus_context *ctx;
337
338 uloop_init();
339
340 ctx = ubus_connect(NULL);
341 if (!ctx) {
342 LOGE("Failed to connect to ubus");
343 return NULL;
344 }
345
346 ubus_add_uloop(ctx);
347 if (ubus_add_object(ctx, &gps_ubus_obj)) {
348 LOGE("Failed to add server");
349 ubus_free(ctx);
350 uloop_done();
351 return NULL;
352 }
353
354 LOGD("gps ubus init done!");
355
356 return ctx;
357}
358