1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 *
4 *
5 */
6 
7 
8 #include "gps_parse.h"
9 #include "sensor_hal.h"
10 
11 
12 #ifdef AOS_ATCMD
13 #include <at/at.h>
14 #endif
15 
16 #define GPS_EV_UDATA                        (0x40)
17 #define GPS_DEV_READ                        (1)
18 #define GPS_DEV_PROC                        (2)
19 #define GPS_DEV_SEND                        (3)
20 
21 
22 
23 #define SIM868_AT_CMD_SUCCESS_RSP           "OK"
24 #define SIM868_AT_CMD_FAIL_RSP              "ERROR"
25 
26 #define SIM868_AT_CMD_GPS_POWER_OFF         "AT+CGNSPWR=0"
27 #define SIM868_AT_CMD_GPS_POWER_ON          "AT+CGNSPWR=1"
28 #define SIM868_AT_CMD_GPS_POWER_CHECK       "AT+CGNSPWR?"
29 
30 #define SIM868_AT_CMD_GPS_LASTPARSE_SET     "AT+CGNSSEQ=\"RMC\""
31 #define SIM868_AT_CMD_GPS_LASTPARSE_CHECK   "AT+CGNSSEQ?"
32 
33 #define SIM868_AT_CMD_GPS_POSITION_GET      "AT+CGNSINF"
34 
35 #define SIM868_AT_CMD_GPS_INTERVAL_SET      "AT+CGNSURC=100"
36 #define SIM868_AT_CMD_GPS_INTERVAL_CLOSE    "AT+CGNSURC=0"
37 #define SIM868_AT_CMD_GPS_INTERVAL_CHECK    "AT+CGNSURC?"
38 
39 #define SIM868_AT_CMD_GPS_SEND_MODE_SET     "AT+CGNSTST=0"
40 #define SIM868_AT_CMD_GPS_SEND_MODE_CHECK   "AT+CGNSTST?"
41 
42 #define SIM868_GPS_HEAD_LOOP                "\n+UGNSINF:"
43 #define SIM868_GPS_TAIL_LOOP                "\r\n"
44 
45 #define SIM868_GPS_HEAD_POLL                "\n+CGNSINF:"
46 #define SIM868_GPS_TAIL_POLL                "\r\n"
47 
48 #define SIM868_GPS_DEFAULT_CMD_LEN          (64)
49 #define SIM868_GPS_DEFAULT_RSP_LEN          (256)
50 
51 typedef enum{
52     AT_CB_ON,
53     AT_CB_OFF,
54     AT_CB_INVLID,
55 }at_cb_mode_e;
56 
57 
58 typedef enum{
59     NONINF   = 0x0000,
60     UGNSINF  = 0x0001,
61     CGNSINF  = 0x0002
62 }en_gnsinf_type;
63 
64 
65 typedef struct _gps_sim868
66 {
67     char        name[GPS_TYPE_NAME_LEN];
68     int         run_stat;
69     int         fix_stat;
70     gps_time_t  utc;
71     float       lat;
72     float       lon;
73     float       elv;
74     float       speed;
75     float       course;
76     int         fix_mode;
77     int         rev1;
78 
79     float       HDOP;
80     float       PDOP;
81     float       VDOP;
82     int         rev2;
83 
84     int         gps_sat_num;
85     int         gnss_sat_num;
86     int         glo_sat_num;
87     int         rev3;
88 
89     int         cn0_max;
90     float       HPA;
91     float       VPA;
92 
93 } gps_sim868_t;
94 
95 #define GPS_SIM868_T_PARA_NUM     (22)
96 
97 typedef struct sim868_inernel_data_stu{
98     gps_sim868_t  data_sim868;
99 
100 }sim868_inernel_data_t;
101 
102 
103 static sim868_inernel_data_t  g_sim868value;
104 static int  g_sim868typebitmap = CGNSINF;
105 
106 static at_cb_mode_e g_sim868_mode = AT_CB_OFF;
107 
108 static char    g_gps_sim868_addr[GPS_RCV_DATA_LEN];
109 static gps_data_t g_gps_sim868_data;
110 static char g_gps_recv_buf[GPS_RCV_DATA_LEN];
111 extern int at_dev_fd;
112 
113 
drv_gps_simcom_sim868_para_set(test_gps_data_t sim868_index[],gps_sim868_t * sim868_para)114 static void  drv_gps_simcom_sim868_para_set(test_gps_data_t sim868_index[], gps_sim868_t* sim868_para)
115 {
116     sim868_index[0].type = GPS_TYPE_STR;
117     sim868_index[0].addr = &(sim868_para->name[0]);
118 
119     sim868_index[1].type = GPS_TYPE_INT32;
120     sim868_index[1].addr = &(sim868_para->run_stat);
121 
122     sim868_index[2].type = GPS_TYPE_INT32;
123     sim868_index[2].addr = &(sim868_para->fix_stat);
124 
125     sim868_index[3].type = GPS_TYPE_UTC;
126     sim868_index[3].addr = &(sim868_para->utc);
127 
128     sim868_index[4].type = GPS_TYPE_FLOAT;
129     sim868_index[4].addr = &(sim868_para->lat);
130     sim868_index[5].type = GPS_TYPE_FLOAT;
131     sim868_index[5].addr = &(sim868_para->lon);
132     sim868_index[6].type = GPS_TYPE_FLOAT;
133     sim868_index[6].addr = &(sim868_para->elv);
134 
135 
136     sim868_index[7].type = GPS_TYPE_FLOAT;
137     sim868_index[7].addr = &(sim868_para->speed);
138     sim868_index[8].type = GPS_TYPE_FLOAT;
139     sim868_index[8].addr = &(sim868_para->course);
140     sim868_index[9].type = GPS_TYPE_INT32;
141     sim868_index[9].addr = &(sim868_para->fix_mode);
142 
143     sim868_index[10].type = GPS_TYPE_INT32;
144     sim868_index[10].addr = &(sim868_para->rev1);
145 
146 
147     sim868_index[11].type = GPS_TYPE_FLOAT;
148     sim868_index[11].addr = &(sim868_para->HDOP);
149     sim868_index[12].type = GPS_TYPE_FLOAT;
150     sim868_index[12].addr = &(sim868_para->PDOP);
151     sim868_index[13].type = GPS_TYPE_FLOAT;
152     sim868_index[13].addr = &(sim868_para->VDOP);
153 
154 
155     sim868_index[14].type = GPS_TYPE_INT32;
156     sim868_index[14].addr = &(sim868_para->rev2);
157 
158 
159     sim868_index[15].type = GPS_TYPE_INT32;
160     sim868_index[15].addr = &(sim868_para->gps_sat_num);
161     sim868_index[16].type = GPS_TYPE_INT32;
162     sim868_index[16].addr = &(sim868_para->gnss_sat_num);
163     sim868_index[17].type = GPS_TYPE_INT32;
164     sim868_index[17].addr = &(sim868_para->glo_sat_num);
165 
166     sim868_index[18].type = GPS_TYPE_INT32;
167     sim868_index[18].addr = &(sim868_para->rev3);
168 
169     sim868_index[19].type = GPS_TYPE_INT32;
170     sim868_index[19].addr = &(sim868_para->cn0_max);
171 
172     sim868_index[20].type = GPS_TYPE_FLOAT;
173     sim868_index[20].addr = &(sim868_para->HPA);
174     sim868_index[21].type = GPS_TYPE_FLOAT;
175     sim868_index[21].addr = &(sim868_para->VPA);
176 
177 }
178 
drv_gps_simcom_sim868_data_parse(char * str,int len,gps_sim868_t * result)179 static int drv_gps_simcom_sim868_data_parse(char *str, int len, gps_sim868_t *result)
180 {
181     int i = 0;
182     char data[GPS_RCV_DATA_LEN];
183     char* prt0;
184     char* prt1 = &data[0];
185     test_gps_data_t    index[GPS_SIM868_T_PARA_NUM];
186 
187     if((NULL == str) || (0 == result)){
188         return -1;
189     }
190 
191     memcpy(data,str,len);
192     data[len] = '\0';
193 
194     memset(result, 0, sizeof(gps_sim868_t));
195     drv_gps_simcom_sim868_para_set(index,result);
196 
197     prt0 = gps_strtok(prt1,&prt1,':',strlen(prt1));
198     gps_data_conv(prt0,strlen(prt0),index[i].addr,index[i].type);
199     i++;
200 
201     for(; (i < 22) &&(NULL!=prt0); i++){
202         prt0 = gps_strtok(prt1,&prt1,',',strlen(prt1));
203         gps_data_conv(prt0,strlen(prt0),index[i].addr,index[i].type);
204     }
205 
206     return 0;
207 }
208 
209 
210 
drv_gps_simcom_sim868_data_get(gps_sim868_t * result,gps_data_t * pgpsdata)211 static int drv_gps_simcom_sim868_data_get(gps_sim868_t *result,gps_data_t* pgpsdata)
212 {
213 
214     if((NULL == result) || (0 == pgpsdata)){
215         return -1;
216     }
217 
218     pgpsdata->utc.year = result->utc.year;
219     pgpsdata->utc.mon = result->utc.mon;
220     pgpsdata->utc.day = result->utc.day;
221     pgpsdata->utc.hour = result->utc.hour;
222     pgpsdata->utc.min = result->utc.min;
223     pgpsdata->utc.sec = result->utc.sec;
224     pgpsdata->utc.hsec = result->utc.hsec;
225 
226     pgpsdata->lat = result->lat;
227     pgpsdata->lon = result->lon;
228     pgpsdata->elv = result->elv;
229 
230     return 0;
231 }
232 
233 
234 
drv_gps_simcom_sim868_type_get(const char * buf,int size)235 static int drv_gps_simcom_sim868_type_get(const char *buf, int size)
236 {
237     static const char *pheads[] = {
238         "\n+UGNSINF",
239         "\n+CGNSINF"
240     };
241 
242     if(0 == buf){
243         return -1;
244     }
245 
246     if(size < 8)
247         return NONINF;
248     else if(0 == memcmp(buf, pheads[0], 5))
249         return UGNSINF;
250     else if(0 == memcmp(buf, pheads[1], 5))
251         return CGNSINF;
252 
253     return NONINF;
254 }
255 
drv_gps_simcom_sim868_type_filter(const char * buf,int size)256 UNUSED static bool drv_gps_simcom_sim868_type_filter(const char *buf, int size)
257 {
258     int ret = drv_gps_simcom_sim868_type_get(buf,size);
259 
260     return (g_sim868typebitmap&ret) ? 1:0;
261 }
262 
263 
drv_gps_simcom_sim868_parse(char * str,int len,gps_data_t * pgpsdata)264 static int drv_gps_simcom_sim868_parse(char* str, int len, gps_data_t* pgpsdata)
265 {
266     int ret;
267     int ptype;
268 
269     ptype = drv_gps_simcom_sim868_type_get(str,len);
270 
271     switch(ptype){
272         case UGNSINF:
273         case CGNSINF:{
274             ret = drv_gps_simcom_sim868_data_parse(str, len,&(g_sim868value.data_sim868));
275             if(0 != ret){
276                 return -1;
277             }
278 
279             ret = drv_gps_simcom_sim868_data_get(&(g_sim868value.data_sim868), pgpsdata);
280             if(0 != ret){
281                 return -1;
282             }
283             break;
284         }
285 
286         default:
287             break;
288     }
289 
290     return 0;
291 }
292 
drv_gps_simcom_sim868_proc(const char * str,gps_data_t * pgpsdata)293 static int drv_gps_simcom_sim868_proc(const char* str, gps_data_t* pgpsdata)
294 {
295     int ret;
296     int len;
297 
298     if(0 == str){
299         return -1;
300     }
301 
302     if(0 == pgpsdata){
303         return -1;
304     }
305 
306     len = strlen(str);
307     if(len >= GPS_RCV_DATA_LEN){
308         return -1;
309     }
310     ret = drv_gps_simcom_sim868_parse((char*)str,len, pgpsdata);
311     if(0 != ret){
312         return -1;
313     }
314 
315     return 0;
316 }
317 
318 
drv_gps_simcom_sim868_power_on(at_cb_mode_e mode)319 static int drv_gps_simcom_sim868_power_on(at_cb_mode_e mode)
320 {
321     int ret = 0;
322     char rsp[SIM868_GPS_DEFAULT_RSP_LEN] = {0};
323 
324     if(mode >= AT_CB_INVLID){
325         LOG("func:%s line: %d  para: %d error\n",__func__, __LINE__,mode);
326         return -1;
327     }
328 
329     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
330     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_POWER_ON, strlen(SIM868_AT_CMD_GPS_POWER_ON),
331                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
332     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL)) {
333         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
334         return -1;
335     }
336 
337     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
338     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_LASTPARSE_SET, strlen(SIM868_AT_CMD_GPS_LASTPARSE_SET),
339                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
340     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL)) {
341         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
342         return -1;
343     }
344 
345     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
346     if(AT_CB_OFF == mode){
347         ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_INTERVAL_CLOSE, strlen(SIM868_AT_CMD_GPS_INTERVAL_CLOSE),
348                                  true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
349     }
350     else{
351         ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_INTERVAL_SET, strlen(SIM868_AT_CMD_GPS_INTERVAL_SET),
352                                  true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
353     }
354 
355     if ((0 != ret) ||  (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL)) {
356         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
357         return -1;
358     }
359 
360     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
361     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_SEND_MODE_SET, strlen(SIM868_AT_CMD_GPS_SEND_MODE_SET),
362                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
363     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL)) {
364         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
365         return -1;
366     }
367 
368     return 0;
369 }
370 
drv_gps_simcom_sim868_power_off()371 static int drv_gps_simcom_sim868_power_off()
372 {
373     int ret = 0;
374     char rsp[SIM868_GPS_DEFAULT_RSP_LEN] = {0};
375 
376     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
377     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_POWER_OFF, strlen(SIM868_AT_CMD_GPS_POWER_OFF),
378                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
379     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL)) {
380         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
381         return -1;
382     }
383 
384     return 0;
385 }
386 
drv_gps_simcom_sim868_check(void)387 static int drv_gps_simcom_sim868_check(void)
388 {
389     int ret = 0;
390     char rsp[SIM868_GPS_DEFAULT_RSP_LEN] = {0};
391 
392     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
393     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_POWER_CHECK, strlen(SIM868_AT_CMD_GPS_POWER_CHECK),
394                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
395     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL)) {
396         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
397         return -1;
398     }
399     LOG("GPS power check %s \r\n", rsp);
400 
401     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
402     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_LASTPARSE_CHECK, strlen(SIM868_AT_CMD_GPS_LASTPARSE_CHECK),
403                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
404     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL))  {
405         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
406         return -1;
407     }
408     LOG("GPS LAST PARSE %s \r\n", rsp);
409 
410     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
411     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_POSITION_GET, strlen(SIM868_AT_CMD_GPS_POSITION_GET),
412                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
413     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL))  {
414         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
415         return -1;
416     }
417     LOG("GPS POSITION %s \r\n", rsp);
418 
419     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
420     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_INTERVAL_CHECK, strlen(SIM868_AT_CMD_GPS_INTERVAL_CHECK),
421                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
422     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL))  {
423         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
424         return -1;
425     }
426     LOG("GPS INVERVAL %s \r\n", rsp);
427 
428 
429     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
430     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_SEND_MODE_CHECK, strlen(SIM868_AT_CMD_GPS_SEND_MODE_CHECK),
431                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
432     if ((0 != ret) || (strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL))  {
433         LOG("%s %d failed rsp %s errno %d\r\n", __func__, __LINE__, rsp,ret);
434         return -1;
435     }
436     LOG("GPS send mode %s \r\n", rsp);
437 
438     return 0;
439 }
440 
441 
drv_gps_simcom_sim868_config(at_cb_mode_e mode)442 static int drv_gps_simcom_sim868_config(at_cb_mode_e mode)
443 {
444     int ret;
445 
446     ret = drv_gps_simcom_sim868_power_off();
447     if(0 != ret)
448     {
449         return ret;
450     }
451 
452     ret = drv_gps_simcom_sim868_power_on(mode);
453     if(0 != ret)
454     {
455         return ret;
456     }
457 
458     return drv_gps_simcom_sim868_check();
459 }
460 
461 
drv_gps_simcom_sim868_poll_read(char * rsp)462 static int drv_gps_simcom_sim868_poll_read(char* rsp)
463 {
464     int ret = 0;
465     memset(rsp, 0, SIM868_GPS_DEFAULT_RSP_LEN);
466     ret = at_send_wait_reply(at_dev_fd, SIM868_AT_CMD_GPS_POSITION_GET, strlen(SIM868_AT_CMD_GPS_POSITION_GET),
467                              true, NULL, 0, rsp, SIM868_GPS_DEFAULT_RSP_LEN, NULL);
468     if ((strstr(rsp, SIM868_AT_CMD_SUCCESS_RSP) == NULL) || (ret != 0)) {
469         LOG("%s %d failed rsp %s\r\n", __func__, __LINE__, rsp);
470         return -1;
471     }
472 
473     return (int)strlen(rsp);
474 }
475 
drv_gps_simcom_sim868_ood_cb(void * arg,char * buf,int size)476 static void drv_gps_simcom_sim868_ood_cb(void *arg, char* buf, int size)
477 {
478     int ret;
479     char* str;
480     if((NULL == buf) || (0 == size) || (size >= GPS_RCV_DATA_LEN-1)){
481         return;
482     }
483 
484     str = &g_gps_sim868_addr[0];
485     memcpy(str,(void*)buf,size);
486     str[size] = '\0';
487 
488     ret = drv_gps_simcom_sim868_proc(str,&g_gps_sim868_data);
489     if(unlikely(ret)){
490         return;
491     }
492 }
493 
drv_gps_simcom_sim868_ood_cb_reg(at_cb_mode_e mode)494 static int drv_gps_simcom_sim868_ood_cb_reg(at_cb_mode_e mode)
495 {
496 
497     if(AT_CB_ON == mode){
498         at_register_callback(at_dev_fd, SIM868_GPS_HEAD_LOOP, SIM868_GPS_TAIL_LOOP, g_gps_recv_buf, GPS_RCV_DATA_LEN-1, drv_gps_simcom_sim868_ood_cb, NULL);
499     }
500     else{
501         LOG("func : %s no need\n",__func__);
502     }
503 
504     return 0;
505 }
506 
drv_gps_simcom_sim868_cb_unreg(void)507 static int drv_gps_simcom_sim868_cb_unreg(void)
508 {
509     /*unreg uart callback, ref to at.oob*/
510     return 0;
511 }
512 
drv_gps_simcom_sim868_open(void)513 static int drv_gps_simcom_sim868_open(void)
514 {
515     int ret;
516 
517     ret = drv_gps_simcom_sim868_ood_cb_reg(g_sim868_mode);
518     if(0 != ret){
519         return -1;
520     }
521 
522     ret = drv_gps_simcom_sim868_config(g_sim868_mode);
523     if(0 != ret){
524         return -1;
525     }
526 
527     return 0;
528 }
529 
drv_gps_simcom_sim868_close(void)530 static int drv_gps_simcom_sim868_close(void)
531 {
532     int ret;
533 
534     ret = drv_gps_simcom_sim868_power_off();
535     if(0 != ret){
536         return -1;
537     }
538 
539     ret = drv_gps_simcom_sim868_cb_unreg();
540     if(0 != ret){
541         return -1;
542     }
543 
544     return 0;
545 }
546 
drv_gps_simcom_sim868_data_read()547 static int drv_gps_simcom_sim868_data_read()
548 {
549     int ret = 0;
550     char rsp[SIM868_GPS_DEFAULT_RSP_LEN] ={0};
551 
552     ret = drv_gps_simcom_sim868_poll_read(rsp);
553     if((ret <= 0) || (ret >= SIM868_GPS_DEFAULT_RSP_LEN))
554     {
555         return -1;
556     }
557     ret = drv_gps_simcom_sim868_proc(rsp,&g_gps_sim868_data);
558     if(0 != ret){
559         return -1;
560     }
561     return 0;
562 }
563 
drv_gps_simcom_sim868_read(void * buf,size_t len)564 static int drv_gps_simcom_sim868_read(void *buf, size_t len)
565 {
566     int ret;
567     size_t size = 0;
568     gps_data_t* gps = buf;
569 
570     if(0 == buf){
571         return -1;
572     }
573 
574     size = sizeof(gps_data_t);
575     if(len < size){
576         return -1;
577     }
578 
579     if(AT_CB_OFF == g_sim868_mode){
580         ret = drv_gps_simcom_sim868_data_read();
581         if(0 != ret){
582             return -1;
583         }
584     }
585 
586     memcpy(&(gps->utc),&(g_gps_sim868_data.utc),sizeof(g_gps_sim868_data.utc));
587 
588     gps->lat = g_gps_sim868_data.lat;
589     gps->lon = g_gps_sim868_data.lon;
590     gps->elv = g_gps_sim868_data.elv;
591     gps->timestamp = aos_now_ms();
592 
593     return (int)size;
594 }
595 
drv_gps_simcom_sim868_irq_handle(void)596 static void drv_gps_simcom_sim868_irq_handle(void)
597 {
598     /* no handle so far */
599 }
600 
drv_gps_simcom_sim868_ioctl(int cmd,unsigned long arg)601 static int drv_gps_simcom_sim868_ioctl(int cmd, unsigned long arg)
602 {
603     return 0;
604 }
605 
606 
drv_gps_simcom_sim868_init(void)607 int drv_gps_simcom_sim868_init(void)
608 {
609     int ret = 0;
610     sensor_obj_t gpsobj;
611 
612     /* fill the gps obj parameters here */
613     gpsobj.io_port    = UART_PORT;
614     gpsobj.tag        = TAG_DEV_GPS;
615     gpsobj.instance   = 0;
616     gpsobj.path       = dev_gps_path;
617     gpsobj.mode       = DEV_POLLING;
618     gpsobj.open       = drv_gps_simcom_sim868_open;
619     gpsobj.close      = drv_gps_simcom_sim868_close;
620     gpsobj.read       = drv_gps_simcom_sim868_read;
621     gpsobj.write      = NULL;
622     gpsobj.ioctl      = drv_gps_simcom_sim868_ioctl;
623     gpsobj.irq_handle = drv_gps_simcom_sim868_irq_handle;
624 
625     ret = sensor_create_obj(&gpsobj);
626     if(unlikely(ret)){
627         return -1;
628     }
629 
630     LOG("%s %s successfully \n", GPS_STR, __func__);
631 
632     return 0;
633 }
634 
635 SENSOR_DRV_ADD(drv_gps_simcom_sim868_init);
636 
637