blob: 75a65fe3f4e62ebba50348e7c0ddf5a43f54b400 [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
b.liu8f231a12024-05-31 17:55:06 +080017const struct blobmsg_policy gnss_init_policy[] ={
18 [0] = {
19 .name = "gnss_init_param",
20 .type = BLOBMSG_TYPE_INT32,
21 },
22};
23
24
25const struct blobmsg_policy get_agps_policy[] ={
26 [0] = {
27 .name = "server_name",
28 .type = BLOBMSG_TYPE_STRING,
29 },
30 [1] = {
31 .name = "alam_flag",
32 .type = BLOBMSG_TYPE_INT32,
33 },
34};
35
36const struct blobmsg_policy gnss_sleep_policy[] ={
37 [0] = {
38 .name = "gnss_sleep_param",
39 .type = BLOBMSG_TYPE_INT32,
40 },
41};
42
43const struct blobmsg_policy gnss_setting_policy[] ={
44 [0] = {
45 .name = "gnss_setting_param",
46 .type = BLOBMSG_TYPE_STRING,
47 },
48};
49
50static int gps_ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
51{
52 blob_buf_init(&gps_blob, 0);
53 blobmsg_add_u32(&gps_blob, "event", ret);
54 ubus_send_reply(ctx, req, gps_blob.head);
55 return 0;
56}
57
58static int gps_ubus_string_reply(struct ubus_context *ctx, struct ubus_request_data *req, char *str)
59{
60 blob_buf_init(&gps_blob, 0);
61 blobmsg_add_string(&gps_blob, "gps_state_resp", str);
62 ubus_send_reply(ctx, req, gps_blob.head);
63 return 0;
64}
65
66static int ubus_gnss_init(struct ubus_context *ctx, struct ubus_object *obj,
67 struct ubus_request_data *req, const char *method,
68 struct blob_attr *msg)
69{
70 UNUSED(obj);
71 UNUSED(method);
72 struct blob_attr *tb[1];
73 struct blob_attr *cur;
b.liubed54282024-06-28 13:57:32 +080074 int init_mode = 0, err = 0;
b.liu8f231a12024-05-31 17:55:06 +080075 int status = 0;
b.liubed54282024-06-28 13:57:32 +080076 gnss_err_enum gnss_result = GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +080077
78 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
79 err = blobmsg_parse(gnss_init_policy, 1, tb, blob_data(msg), blob_len(msg));
80 if (err < 0)
81 {
82 LOGE("blobmsg_parse fail");
83 return -1;
84 }
85 cur = tb[0];
86 if (!cur) {
87 LOGE("missing parameter");
88 return -2;
89 }
90
91 init_mode = blobmsg_get_u32(cur);
92 LOGD("init_mode=%d", init_mode);
93 if(init_mode == 0) { // Close gnss.
b.liubed54282024-06-28 13:57:32 +080094 gnss_result = gnss_deinit();
b.liu8f231a12024-05-31 17:55:06 +080095 } else {
96 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) == init_mode) {
b.liubed54282024-06-28 13:57:32 +080097 gnss_result = gnss_init(init_mode);
b.liu8f231a12024-05-31 17:55:06 +080098 } else { // ARG error, no print nmea.
b.liubed54282024-06-28 13:57:32 +080099 gnss_result = GNSS_ERR_UNSUPPORT;
wangyouqiang9ee6eaa2024-06-17 13:43:45 +0800100 }
b.liu8f231a12024-05-31 17:55:06 +0800101 }
102
b.liubed54282024-06-28 13:57:32 +0800103 LOGD("ubus_gnss_init() ret=%d", gnss_result);
wangyouqiang9ee6eaa2024-06-17 13:43:45 +0800104 gps_ubus_send_reply(ctx, req, (int)gnss_result);
b.liu8f231a12024-05-31 17:55:06 +0800105
106 return 0;
107}
108
109static int ubus_gnss_deinit(struct ubus_context *ctx, struct ubus_object *obj,
110 struct ubus_request_data *req, const char *method,
111 struct blob_attr *msg)
112{
113 UNUSED(ctx);
114 UNUSED(obj);
115 UNUSED(req);
116 UNUSED(method);
117 UNUSED(msg);
118
b.liubed54282024-06-28 13:57:32 +0800119 gnss_err_enum gnss_result = GNSS_ERR_OK;
b.liuced8dd02024-06-28 13:28:29 +0800120
b.liubed54282024-06-28 13:57:32 +0800121 gnss_result = gnss_deinit();
wangyouqiang9ee6eaa2024-06-17 13:43:45 +0800122 gps_ubus_send_reply(ctx, req, (int)gnss_result);
b.liu8f231a12024-05-31 17:55:06 +0800123
124 return 0;
125}
126
127static int ubus_gnss_get_agps(struct ubus_context *ctx, struct ubus_object *obj,
128 struct ubus_request_data *req, const char *method,
129 struct blob_attr *msg)
130{
131 UNUSED(obj);
132 UNUSED(method);
133 struct blob_attr *tb[ARRAY_SIZE(get_agps_policy)];
134 struct blob_attr *cur;
135 char *server_name = NULL;
b.liubed54282024-06-28 13:57:32 +0800136 int err = 0, alm_flag = 0;
137 gnss_err_enum gnss_result = GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800138
139 err = blobmsg_parse(get_agps_policy, ARRAY_SIZE(get_agps_policy), tb, blob_data(msg), blob_len(msg));
140 if (err < 0)
141 {
142 LOGE("blobmsg_parse table fail");
143 return -1;
144 }
145
146 cur = tb[0];
147 if (cur)
148 server_name = blobmsg_get_string(cur);
149 else
150 LOGE("missing parameter1");
151
152 cur = tb[1];
153 if (cur)
154 alm_flag = blobmsg_get_u32(cur);
155 else
156 LOGE("missing parameter2");
157
158 LOGD("server_name=%s, alm_flag=%d", server_name, alm_flag);
159
b.liubed54282024-06-28 13:57:32 +0800160 gnss_result = GNSS_ERR_UNSUPPORT;
wangyouqiang9ee6eaa2024-06-17 13:43:45 +0800161 gps_ubus_send_reply(ctx, req, (int)gnss_result);
b.liu8f231a12024-05-31 17:55:06 +0800162
163 return 0;
164}
165
166static int ubus_gnss_set_agps(struct ubus_context *ctx, struct ubus_object *obj,
167 struct ubus_request_data *req, const char *method,
168 struct blob_attr *msg)
169{
170 UNUSED(ctx);
171 UNUSED(obj);
172 UNUSED(req);
173 UNUSED(method);
174 UNUSED(msg);
175
b.liubed54282024-06-28 13:57:32 +0800176 gnss_err_enum gnss_result = GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800177
b.liubed54282024-06-28 13:57:32 +0800178
179 gnss_result = GNSS_ERR_UNSUPPORT;
180 gps_ubus_send_reply(ctx, req, gnss_result);
b.liu8f231a12024-05-31 17:55:06 +0800181
182 return 0;
183}
184
185static int ubus_gnss_sleep(struct ubus_context *ctx, struct ubus_object *obj,
186 struct ubus_request_data *req, const char *method,
187 struct blob_attr *msg)
188{
189 UNUSED(ctx);
190 UNUSED(obj);
191 UNUSED(req);
192 UNUSED(method);
193 struct blob_attr *tb[1];
194 struct blob_attr *cur;
195 int workmode = 0, err = 0;
196 int status = 0;
b.liubed54282024-06-28 13:57:32 +0800197 gnss_err_enum gnss_result = GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800198
199 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
200 err = blobmsg_parse(gnss_sleep_policy, 1, tb, blob_data(msg), blob_len(msg));
201 if (err < 0)
202 {
203 LOGE("blobmsg_parse fail");
204 return -1;
205 }
206 cur = tb[0];
207 if (!cur) {
208 LOGE("missing parameter");
209 return -2;
210 }
211
212 workmode = blobmsg_get_u32(cur);
213 LOGD("workMode=%d", workmode);
214 /* Goto Sleeping.....*/
215
216 LOGD("ret=%d", status);
b.liubed54282024-06-28 13:57:32 +0800217
218
219
220
221 gnss_result = GNSS_ERR_UNSUPPORT;
222
223 gps_ubus_send_reply(ctx, req, gnss_result);
224
b.liu8f231a12024-05-31 17:55:06 +0800225 return 0;
226}
227
228static int ubus_gnss_setting(struct ubus_context *ctx, struct ubus_object *obj,
229 struct ubus_request_data *req, const char *method,
230 struct blob_attr *msg)
231{
232 UNUSED(ctx);
233 UNUSED(obj);
234 UNUSED(req);
235 UNUSED(method);
236 struct blob_attr *tb[1];
237 struct blob_attr *cur;
b.liubed54282024-06-28 13:57:32 +0800238 int err = 0;
b.liu8f231a12024-05-31 17:55:06 +0800239 char *gpsCfg = NULL;
240 int status = 0;
b.liubed54282024-06-28 13:57:32 +0800241 gnss_err_enum gnss_result = GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800242
243 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
244 err = blobmsg_parse(gnss_setting_policy, 1, tb, blob_data(msg), blob_len(msg));
245 if (err < 0)
246 {
247 LOGE("blobmsg_parse fail");
248 return -1;
249 }
250 cur = tb[0];
251 if (!cur) {
252 LOGE("missing parameter");
253 return -2;
254 }
255
256 gpsCfg = blobmsg_get_string(cur);
257 LOGD("gpsCfg=%s", gpsCfg);
258
259 char rsp[1024];
b.liubed54282024-06-28 13:57:32 +0800260 gnss_result = gnss_set(gpsCfg, strlen(gpsCfg), rsp, 1024);
b.liu8f231a12024-05-31 17:55:06 +0800261
b.liubed54282024-06-28 13:57:32 +0800262 LOGD("gnss_result = %d", gnss_result);
263
wangyouqiang9ee6eaa2024-06-17 13:43:45 +0800264 gps_ubus_send_reply(ctx, req, (int)gnss_result);
b.liu8f231a12024-05-31 17:55:06 +0800265
266 return 0;
267}
268
269#define ASR_GNSS_STATUS_LEN 128
270static int ubus_gnss_get_state(struct ubus_context *ctx, struct ubus_object *obj,
271 struct ubus_request_data *req, const char *method,
272 struct blob_attr *msg)
273{
274 UNUSED(obj);
275 UNUSED(req);
276 UNUSED(method);
277 UNUSED(msg);
278
279 char tmpBuf[ASR_GNSS_STATUS_LEN] = {0};
280 int i = 0;
281 int len = 0;
282 int ret = 0;
283 int num = 0;
284
285 //ret = asr_gnss_get_gps_info(&param);
286 if (0 == ret) {
287 len = snprintf(tmpBuf, sizeof(tmpBuf), "%d, %d, %d, %d, %d;", 0, 0, 0, 0, 0);
288 num = 6;
289 for(i=0; i<num; i++) {
290 len += sprintf(&tmpBuf[len], " %d, %d;", 1, 2);
291
292 if(len > ASR_GNSS_STATUS_LEN)
293 break;
294 }
295
296 LOGD("[%d]tmpBuf=%s", len, tmpBuf);
297 gps_ubus_string_reply(ctx, req, tmpBuf);
298 }
299
300 return 0;
301}
302
303static const struct ubus_method gps_ubus_methods[] = {
304 UBUS_METHOD("gnss_init", ubus_gnss_init, gnss_init_policy),
305 UBUS_METHOD_NOARG("gnss_deinit", ubus_gnss_deinit),
306 UBUS_METHOD("gnss_get_agps", ubus_gnss_get_agps, get_agps_policy),
307 UBUS_METHOD_NOARG("gnss_set_agps", ubus_gnss_set_agps),
308 UBUS_METHOD("gnss_sleep", ubus_gnss_sleep, gnss_sleep_policy),
309 UBUS_METHOD("gnss_setting", ubus_gnss_setting, gnss_setting_policy),
310 UBUS_METHOD_NOARG("gnss_get_state", ubus_gnss_get_state),
311};
312
313static struct ubus_object_type gps_object_type =
314 UBUS_OBJECT_TYPE("mbtk_gnss", gps_ubus_methods);
315
316static struct ubus_object gps_ubus_obj = {
317 .name = "mbtk_gnss",
318 .type = &gps_object_type,
319 .methods = gps_ubus_methods,
320 .n_methods = ARRAY_SIZE(gps_ubus_methods),
321};
322
323int gnss_ubus_exit(struct ubus_context *ctx)
324{
325 if(!ctx) {
326 return -1;
327 }
328
329 ubus_remove_object(ctx, &gps_ubus_obj);
330 ubus_free(ctx);
331 uloop_done();
332
333 LOGD("ubus exit done");
334 return 0;
335}
336
337struct ubus_context *gnss_ubus_init(void)
338{
339 struct ubus_context *ctx;
340
341 uloop_init();
342
343 ctx = ubus_connect(NULL);
344 if (!ctx) {
345 LOGE("Failed to connect to ubus");
346 return NULL;
347 }
348
349 ubus_add_uloop(ctx);
350 if (ubus_add_object(ctx, &gps_ubus_obj)) {
351 LOGE("Failed to add server");
352 ubus_free(ctx);
353 uloop_done();
354 return NULL;
355 }
356
357 LOGD("gps ubus init done!");
358
359 return ctx;
360}
361