b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1 | #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; |
| 15 | static struct blob_buf gps_blob; |
| 16 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 17 | const struct blobmsg_policy gnss_init_policy[] ={ |
| 18 | [0] = { |
| 19 | .name = "gnss_init_param", |
| 20 | .type = BLOBMSG_TYPE_INT32, |
| 21 | }, |
| 22 | }; |
| 23 | |
| 24 | |
| 25 | const 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 | |
| 36 | const struct blobmsg_policy gnss_sleep_policy[] ={ |
| 37 | [0] = { |
| 38 | .name = "gnss_sleep_param", |
| 39 | .type = BLOBMSG_TYPE_INT32, |
| 40 | }, |
| 41 | }; |
| 42 | |
| 43 | const struct blobmsg_policy gnss_setting_policy[] ={ |
| 44 | [0] = { |
| 45 | .name = "gnss_setting_param", |
| 46 | .type = BLOBMSG_TYPE_STRING, |
| 47 | }, |
| 48 | }; |
| 49 | |
| 50 | static 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 | |
| 58 | static 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 | |
| 66 | static 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; |
| 74 | int init_mode = 0, err = 0, ret = 0; |
| 75 | int status = 0; |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 76 | gnss_result_enum gnss_result = GNSS_RESULT_STATE_OPEN_SUCCESS; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 77 | |
| 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. |
| 94 | ret = gnss_deinit(); |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 95 | if(ret < 0) |
| 96 | { |
| 97 | gnss_result = GNSS_RESULT_STATE_CLOSE_FAIL; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | gnss_result = GNSS_RESULT_STATE_CLOSE_SUCCESS; |
| 102 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 103 | } else { |
| 104 | if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) == init_mode) { |
| 105 | ret = gnss_init(init_mode); |
| 106 | } else { // ARG error, no print nmea. |
| 107 | ret = gnss_init(0); |
| 108 | } |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 109 | |
| 110 | if(ret < 0) |
| 111 | { |
| 112 | gnss_result = GNSS_RESULT_STATE_OPEN_FAIL; |
| 113 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | LOGD("ubus_gnss_init() ret=%d", ret); |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 117 | gps_ubus_send_reply(ctx, req, (int)gnss_result); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int ubus_gnss_deinit(struct ubus_context *ctx, struct ubus_object *obj, |
| 123 | struct ubus_request_data *req, const char *method, |
| 124 | struct blob_attr *msg) |
| 125 | { |
| 126 | UNUSED(ctx); |
| 127 | UNUSED(obj); |
| 128 | UNUSED(req); |
| 129 | UNUSED(method); |
| 130 | UNUSED(msg); |
| 131 | |
| 132 | int ret = 0; |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 133 | gnss_result_enum gnss_result = GNSS_RESULT_STATE_OPEN_SUCCESS; |
| 134 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 135 | ret = gnss_deinit(); |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 136 | if(ret < 0) |
| 137 | { |
| 138 | gnss_result = GNSS_RESULT_STATE_CLOSE_FAIL; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | gnss_result = GNSS_RESULT_STATE_CLOSE_SUCCESS; |
| 143 | } |
| 144 | gps_ubus_send_reply(ctx, req, (int)gnss_result); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int ubus_gnss_get_agps(struct ubus_context *ctx, struct ubus_object *obj, |
| 150 | struct ubus_request_data *req, const char *method, |
| 151 | struct blob_attr *msg) |
| 152 | { |
| 153 | UNUSED(obj); |
| 154 | UNUSED(method); |
| 155 | struct blob_attr *tb[ARRAY_SIZE(get_agps_policy)]; |
| 156 | struct blob_attr *cur; |
| 157 | char *server_name = NULL; |
| 158 | int err = 0, alm_flag = 0, ret = 0; |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 159 | gnss_result_enum gnss_result = GNSS_RESULT_STATE_OPEN_SUCCESS; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 160 | |
| 161 | err = blobmsg_parse(get_agps_policy, ARRAY_SIZE(get_agps_policy), tb, blob_data(msg), blob_len(msg)); |
| 162 | if (err < 0) |
| 163 | { |
| 164 | LOGE("blobmsg_parse table fail"); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | cur = tb[0]; |
| 169 | if (cur) |
| 170 | server_name = blobmsg_get_string(cur); |
| 171 | else |
| 172 | LOGE("missing parameter1"); |
| 173 | |
| 174 | cur = tb[1]; |
| 175 | if (cur) |
| 176 | alm_flag = blobmsg_get_u32(cur); |
| 177 | else |
| 178 | LOGE("missing parameter2"); |
| 179 | |
| 180 | LOGD("server_name=%s, alm_flag=%d", server_name, alm_flag); |
| 181 | |
| 182 | if (server_name) |
| 183 | { |
| 184 | ret = 3; |
| 185 | } |
| 186 | |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 187 | gnss_result = GNSS_RESULT_STATE_UNSUPPORT; |
| 188 | gps_ubus_send_reply(ctx, req, (int)gnss_result); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int ubus_gnss_set_agps(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 | UNUSED(msg); |
| 202 | |
| 203 | int err = 0, ret = 0; |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 204 | gnss_result_enum gnss_result = GNSS_RESULT_STATE_OPEN_SUCCESS; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 205 | ret = 4; |
| 206 | |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 207 | gnss_result = GNSS_RESULT_STATE_UNSUPPORT; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 208 | gps_ubus_send_reply(ctx, req, ret); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int ubus_gnss_sleep(struct ubus_context *ctx, struct ubus_object *obj, |
| 214 | struct ubus_request_data *req, const char *method, |
| 215 | struct blob_attr *msg) |
| 216 | { |
| 217 | UNUSED(ctx); |
| 218 | UNUSED(obj); |
| 219 | UNUSED(req); |
| 220 | UNUSED(method); |
| 221 | struct blob_attr *tb[1]; |
| 222 | struct blob_attr *cur; |
| 223 | int workmode = 0, err = 0; |
| 224 | int status = 0; |
| 225 | |
| 226 | /*parsing blob to be accessed easily with tb array - parse "1" argument*/ |
| 227 | err = blobmsg_parse(gnss_sleep_policy, 1, tb, blob_data(msg), blob_len(msg)); |
| 228 | if (err < 0) |
| 229 | { |
| 230 | LOGE("blobmsg_parse fail"); |
| 231 | return -1; |
| 232 | } |
| 233 | cur = tb[0]; |
| 234 | if (!cur) { |
| 235 | LOGE("missing parameter"); |
| 236 | return -2; |
| 237 | } |
| 238 | |
| 239 | workmode = blobmsg_get_u32(cur); |
| 240 | LOGD("workMode=%d", workmode); |
| 241 | /* Goto Sleeping.....*/ |
| 242 | |
| 243 | LOGD("ret=%d", status); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int ubus_gnss_setting(struct ubus_context *ctx, struct ubus_object *obj, |
| 248 | struct ubus_request_data *req, const char *method, |
| 249 | struct blob_attr *msg) |
| 250 | { |
| 251 | UNUSED(ctx); |
| 252 | UNUSED(obj); |
| 253 | UNUSED(req); |
| 254 | UNUSED(method); |
| 255 | struct blob_attr *tb[1]; |
| 256 | struct blob_attr *cur; |
| 257 | int err = 0, ret = 0; |
| 258 | char *gpsCfg = NULL; |
| 259 | int status = 0; |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 260 | gnss_result_enum gnss_result = GNSS_RESULT_STATE_OPEN_SUCCESS; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 261 | |
| 262 | /*parsing blob to be accessed easily with tb array - parse "1" argument*/ |
| 263 | err = blobmsg_parse(gnss_setting_policy, 1, tb, blob_data(msg), blob_len(msg)); |
| 264 | if (err < 0) |
| 265 | { |
| 266 | LOGE("blobmsg_parse fail"); |
| 267 | return -1; |
| 268 | } |
| 269 | cur = tb[0]; |
| 270 | if (!cur) { |
| 271 | LOGE("missing parameter"); |
| 272 | return -2; |
| 273 | } |
| 274 | |
| 275 | gpsCfg = blobmsg_get_string(cur); |
| 276 | LOGD("gpsCfg=%s", gpsCfg); |
| 277 | |
| 278 | char rsp[1024]; |
| 279 | ret = gnss_set(gpsCfg, strlen(gpsCfg), rsp, 1024); |
| 280 | |
| 281 | LOGD("ret=%d", ret); |
wangyouqiang | 9ee6eaa | 2024-06-17 13:43:45 +0800 | [diff] [blame^] | 282 | if(ret == 0) |
| 283 | { |
| 284 | gnss_result = GNSS_RESULT_STATE_SET_SUCCESS; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | gnss_result = GNSS_RESULT_STATE_SET_FAIL; |
| 289 | } |
| 290 | gps_ubus_send_reply(ctx, req, (int)gnss_result); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | #define ASR_GNSS_STATUS_LEN 128 |
| 296 | static int ubus_gnss_get_state(struct ubus_context *ctx, struct ubus_object *obj, |
| 297 | struct ubus_request_data *req, const char *method, |
| 298 | struct blob_attr *msg) |
| 299 | { |
| 300 | UNUSED(obj); |
| 301 | UNUSED(req); |
| 302 | UNUSED(method); |
| 303 | UNUSED(msg); |
| 304 | |
| 305 | char tmpBuf[ASR_GNSS_STATUS_LEN] = {0}; |
| 306 | int i = 0; |
| 307 | int len = 0; |
| 308 | int ret = 0; |
| 309 | int num = 0; |
| 310 | |
| 311 | //ret = asr_gnss_get_gps_info(¶m); |
| 312 | if (0 == ret) { |
| 313 | len = snprintf(tmpBuf, sizeof(tmpBuf), "%d, %d, %d, %d, %d;", 0, 0, 0, 0, 0); |
| 314 | num = 6; |
| 315 | for(i=0; i<num; i++) { |
| 316 | len += sprintf(&tmpBuf[len], " %d, %d;", 1, 2); |
| 317 | |
| 318 | if(len > ASR_GNSS_STATUS_LEN) |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | LOGD("[%d]tmpBuf=%s", len, tmpBuf); |
| 323 | gps_ubus_string_reply(ctx, req, tmpBuf); |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static const struct ubus_method gps_ubus_methods[] = { |
| 330 | UBUS_METHOD("gnss_init", ubus_gnss_init, gnss_init_policy), |
| 331 | UBUS_METHOD_NOARG("gnss_deinit", ubus_gnss_deinit), |
| 332 | UBUS_METHOD("gnss_get_agps", ubus_gnss_get_agps, get_agps_policy), |
| 333 | UBUS_METHOD_NOARG("gnss_set_agps", ubus_gnss_set_agps), |
| 334 | UBUS_METHOD("gnss_sleep", ubus_gnss_sleep, gnss_sleep_policy), |
| 335 | UBUS_METHOD("gnss_setting", ubus_gnss_setting, gnss_setting_policy), |
| 336 | UBUS_METHOD_NOARG("gnss_get_state", ubus_gnss_get_state), |
| 337 | }; |
| 338 | |
| 339 | static struct ubus_object_type gps_object_type = |
| 340 | UBUS_OBJECT_TYPE("mbtk_gnss", gps_ubus_methods); |
| 341 | |
| 342 | static struct ubus_object gps_ubus_obj = { |
| 343 | .name = "mbtk_gnss", |
| 344 | .type = &gps_object_type, |
| 345 | .methods = gps_ubus_methods, |
| 346 | .n_methods = ARRAY_SIZE(gps_ubus_methods), |
| 347 | }; |
| 348 | |
| 349 | int gnss_ubus_exit(struct ubus_context *ctx) |
| 350 | { |
| 351 | if(!ctx) { |
| 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | ubus_remove_object(ctx, &gps_ubus_obj); |
| 356 | ubus_free(ctx); |
| 357 | uloop_done(); |
| 358 | |
| 359 | LOGD("ubus exit done"); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | struct ubus_context *gnss_ubus_init(void) |
| 364 | { |
| 365 | struct ubus_context *ctx; |
| 366 | |
| 367 | uloop_init(); |
| 368 | |
| 369 | ctx = ubus_connect(NULL); |
| 370 | if (!ctx) { |
| 371 | LOGE("Failed to connect to ubus"); |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | ubus_add_uloop(ctx); |
| 376 | if (ubus_add_object(ctx, &gps_ubus_obj)) { |
| 377 | LOGE("Failed to add server"); |
| 378 | ubus_free(ctx); |
| 379 | uloop_done(); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | LOGD("gps ubus init done!"); |
| 384 | |
| 385 | return ctx; |
| 386 | } |
| 387 | |