1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 *
4 *
5 */
6 
7 #ifndef GPS_PARSE_H
8 #define GPS_PARSE_H
9 
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <limits.h>
17 #include "ulog/ulog.h"
18 #include "aos/kernel.h"
19 #include "sensor/sensor.h"
20 
21 #define GPS_RCV_DATA_LEN        (256)
22 #define GPS_CONV_DATA_LEN       (64)
23 #define GPS_CALC_BUF_LEN        (256)
24 #define GPS_CALC_STR_LEN        (GPS_CALC_BUF_LEN-1)
25 #define GPS_TYPE_NAME_LEN       (16)
26 
27 #define GPS_UTC_YEAR_LEN        (4)
28 #define GPS_UTC_MON_LEN         (2)
29 #define GPS_UTC_DAY_LEN         (2)
30 
31 #define GPS_UTC_HOUR_LEN        (2)
32 #define GPS_UTC_MIN_LEN         (2)
33 #define GPS_UTC_SEC_LEN         (2)
34 #define GPS_UTC_HSEC_LEN        (3)
35 #define GPS_UTC_POINT_LEN       (1)
36 
37 
38 typedef enum{
39     GPS_TYPE_UINT8 = 0,
40     GPS_TYPE_INT32,
41     GPS_TYPE_FLOAT,
42     GPS_TYPE_STR,
43     GPS_TYPE_UTC
44 }gps_data_type;
45 
46 
47 typedef struct test_gps_data_t{
48     int     type;
49     void*   addr;
50 }test_gps_data_t;
51 
gps_atoi(const char * str,int size,int radix)52 static inline int gps_atoi(const char *str, int size, int radix)
53 {
54     int ret = 0;
55     char *str_tmp = NULL;
56     char buff[GPS_CONV_DATA_LEN];
57 
58     if(size < GPS_CONV_DATA_LEN){
59         memcpy(&buff[0], str, size);
60         buff[size] = '\0';
61         ret = strtol(&buff[0], &str_tmp, radix);
62     }
63     return ret;
64 }
65 
gps_atof(const char * str,int len)66 static inline float gps_atof(const char *str, int len)
67 {
68     double ret = 0;
69     char *str_tmp = NULL;
70     char buff[GPS_CONV_DATA_LEN];
71 
72     if(len < GPS_CONV_DATA_LEN){
73         memcpy(&buff[0], str, len);
74         buff[len] = '\0';
75         ret = strtod(&buff[0], &str_tmp);
76     }
77 
78     return (float)ret;
79 }
80 
gps_strtok(char * src,char ** ret,char c,int len)81 static inline char* gps_strtok(char* src, char** ret, char c,int len)
82 {
83     int i;
84     char* str_tmp = src;
85 
86     for(i = 0; i < len; i++,str_tmp++){
87         if(c == *str_tmp){
88             str_tmp[0] = '\0';
89             *ret = str_tmp+1;
90             return src;
91         }
92     }
93 
94     *ret = str_tmp;
95     return src;
96 }
97 
gps_utc_get(char * str,int len,gps_time_t * res)98 static inline int gps_utc_get(char *str, int len, gps_time_t* res)
99 {
100     char* str_tmp = NULL;
101 
102     switch(len){
103         case (sizeof("YYYYMMDDhhmmss.sss") - 1):
104             str_tmp = str;
105             res->year = gps_atoi(str_tmp,GPS_UTC_YEAR_LEN,10);
106             str_tmp += GPS_UTC_YEAR_LEN;
107             res->mon = gps_atoi(str_tmp,GPS_UTC_MON_LEN,10);
108             str_tmp += GPS_UTC_MON_LEN;
109             res->day = gps_atoi(str_tmp,GPS_UTC_DAY_LEN,10);
110             str_tmp += GPS_UTC_DAY_LEN;
111 
112             res->hour = gps_atoi(str_tmp,GPS_UTC_HOUR_LEN,10);
113             str_tmp += GPS_UTC_HOUR_LEN;
114             res->min = gps_atoi(str_tmp,GPS_UTC_MIN_LEN,10);
115             str_tmp += GPS_UTC_MIN_LEN;
116             res->sec = gps_atoi(str_tmp,GPS_UTC_SEC_LEN,10);
117             str_tmp += (GPS_UTC_SEC_LEN + GPS_UTC_POINT_LEN);
118             res->hsec = gps_atoi(str_tmp,3,10);
119             break;
120 
121         case (sizeof("hhmmss.sss") - 1):
122 
123             str_tmp = str;
124             res->hour = gps_atoi(str_tmp,GPS_UTC_HOUR_LEN,10);
125             str_tmp += GPS_UTC_HOUR_LEN;
126             res->min = gps_atoi(str_tmp,GPS_UTC_MIN_LEN,10);
127             str_tmp += GPS_UTC_MIN_LEN;
128             res->sec = gps_atoi(str_tmp,GPS_UTC_SEC_LEN,10);
129             str_tmp += (GPS_UTC_SEC_LEN + GPS_UTC_POINT_LEN);
130             res->hsec = gps_atoi(str_tmp,3,10);
131             break;
132 
133         default:
134             LOG("Parse of UTC fail (format error)!");
135             return -1;
136 
137     }
138 
139     return 0;
140 }
141 
142 
gps_data_conv(void * str,int len,void * addr,int type)143 static inline void gps_data_conv(void* str, int len,void* addr,int type)
144 {
145     switch(type){
146         case GPS_TYPE_FLOAT:
147             *(float*)(addr) = (float)gps_atof(str,len);
148             break;
149         case GPS_TYPE_INT32:
150             *(int*)(addr) = (int)gps_atoi(str,len,10);
151             break;
152 
153         case GPS_TYPE_UINT8:
154             *(char*)(addr) = *(char*)str;
155             break;
156 
157         case GPS_TYPE_STR:
158             memcpy(addr, str, len);
159             ((char *)addr)[len] = '\0';
160             break;
161 
162         case GPS_TYPE_UTC:
163             gps_utc_get(str,len,addr);
164             break;
165         default: break;
166     }
167 
168 }
169 
170 
171 #endif /* GPS_PARSE_H */
172 
173