blob: e0fa746ccc24f016a6fc398d7fd830ab56f691c3 [file] [log] [blame]
b.liu8f231a12024-05-31 17:55:06 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <termios.h>
7#include <string.h>
8#include <stdarg.h>
9
10#include "mbtk_type.h"
11#include "mbtk_log.h"
12
13#define DATABITS CS8
14#define STOPBITS 0
15#define PARITYON 0
16#define PARITY 0
17#define MBTK_SLAVE_DEV_NAME_MAX_LEN 24
18
19int uart_baud_get(int baud)
20{
21 int rate = 0;
22 switch(baud)
23 {
24 case 300:
25 rate = B300;
26 break;
27 case 600:
28 rate = B600;
29 break;
30 case 1200:
31 rate = B1200;
32 break;
33 case 2400:
34 rate = B2400;
35 break;
36 case 4800:
37 rate = B4800;
38 break;
39 case 9600:
40 rate = B9600;
41 break;
42 case 19200:
43 rate = B19200;
44 break;
45 case 38400:
46 rate = B38400;
47 break;
48 case 57600:
49 rate = B57600;
50 break;
51 case 115200:
52 rate = B115200;
53 break;
54 case 230400:
55 rate = B230400;
56 break;
57 case 460800:
58 rate = B460800;
59 break;
60 case 921600:
61 rate = B921600;
62 break;
63 case 1500000:
64 rate = B1500000;
65 break;
66 case 2000000:
67 rate = B2000000;
68 break;
69 case 3000000:
70 rate = B3000000;
71 break;
72 case 4000000:
73 rate = B4000000;
74 break;
75 default:
76 rate = B115200;
77 break;
78 }
79
80 return rate;
81}
82
83int gnss_port_open(const char *dev, int flag, int baud, bool tty)
84{
85
86 int fd = -1;
87 if((fd = open(dev, flag)) < 0)
88 {
89 LOGE("Open %s fail errno = [%d].", dev, errno);
90 return -1;
91 }
92
93 LOGD("Open %s success.", dev);
94 if (tty)
95 {
96 int rate = uart_baud_get(baud);
97 /* set newtio */
98 struct termios newtio;
99 memset(&newtio, 0, sizeof(newtio));
100 //(void)fcntl(fd, F_SETFL, 0);
101 /* no flow control for uart by default */
102 newtio.c_cflag = rate | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
103 newtio.c_iflag = IGNPAR;
104 //newtio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
105 newtio.c_oflag = 0;
106 newtio.c_lflag = 0; /* disable ECHO, ICANON, etc... */
107
108 newtio.c_cc[VERASE] = 0x8; /* del */
109 newtio.c_cc[VEOF] = 4; /* Ctrl-d */
110 newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
111 newtio.c_cc[VEOL] = 0xD; /* '\0' */
112
113 tcflush(fd, TCIOFLUSH);
114 tcsetattr(fd, TCSANOW, &newtio);
115 }
116
117 return fd;
118}
119
120int gnss_port_close(int fd)
121{
122 if(fd > 0)
123 {
124 close(fd);
125 }
126 return 0;
127}
128
129int gnss_set_baudrate(int fd, int baudrate)
130{
131 struct termios options, oldtio;
132
133 if(fcntl(fd, F_SETFL, 0) < 0) {
134 LOGE("fcntl failed!");
135 return -1;
136 }
137
138 if(tcgetattr(fd, &oldtio) != 0) {
139 LOGE("setup serial error!");
140 return -1;
141 }
142
143 /* Get the current options for the port... */
144 tcgetattr(fd, &options);
145
146 /* Set the baud rates to baudrate... */
147 cfsetispeed(&options,baudrate);
148 cfsetospeed(&options,baudrate);
149 tcsetattr(fd, TCSANOW, &options);
150
151 if (0 != tcgetattr(fd, &options))
152 {
153 LOGE("get options error!");
154 return -1;
155 }
156
157 /*
158 * 8bit Data,no partity,1 stop bit...
159 */
160 options.c_cflag &= ~PARENB;//无奇偶校验
161 options.c_cflag &= ~CSTOPB;//停止位,1位
162 options.c_cflag &= ~CSIZE; //数据位的位掩码
163 options.c_cflag |= CS8; //数据位,8位
164
165 cfmakeraw(&options);
166
167 /*
168 * Set the new options for the port...
169 */
170 if (tcsetattr(fd, TCSANOW, &options) != 0)
171 {
172 LOGE("setup serial error!");
173 return -1 ;
174 }
175
176 return 0 ;
177}
178
179uint16 get_crc16(const char *ptr, uint16 count)
180{
181 uint16 crc, i;
182
183 crc = 0;
184 while(count--)
185 {
186 crc = crc ^ (int) *ptr++ << 8;
187
188 for(i = 0; i < 8; i++)
189 {
190 if(crc & 0x8000)
191 crc = crc << 1 ^ 0x1021;
192 else
193 crc = crc << 1;
194 }
195 }
196
197 return (crc & 0xFFFF);
198}
199
200int gnss_pty_open(int *master_fd, int *slave_fd, const char *dev)
201{
202 int flags = -1;
203 int ret = -1;
204 if(*master_fd > 0) {
205 LOGD("PTY has inited.");
206 return 0;
207 }
208 char spty_name[MBTK_SLAVE_DEV_NAME_MAX_LEN] = {0};
209 int result = openpty(master_fd, slave_fd, spty_name, NULL, NULL);
210 if (-1 == result) {
211 LOGE("Failed to get a pty.");
212 return -1;
213 }
214
215 LOGD("Get a pty pair, FD -- master[%d] slave[%d]", *master_fd, *slave_fd);
216 LOGD("Slave name is:%s", spty_name);
217
218 if(access(dev, F_OK) == -1)
219 {
220 LOGD("symlink %s -> %s", spty_name, dev);
221 result = symlink(spty_name, dev);
222 if (-1 == result) {
223 LOGE("symlink error.");
224 goto ERROR;
225 }
226 }
227
228 flags = fcntl(*master_fd, F_GETFL);
229 if (flags == -1)
230 {
231 LOGE("fcntl get error.");
232 goto ERROR;
233 }
234 flags |= O_NONBLOCK;
235 flags |= O_NOCTTY;
236 ret = fcntl(*master_fd, F_SETFL, flags);
237 if(ret == -1)
238 {
239 LOGE("fcntl set error.");
240 goto ERROR;
241 }
242
243 if (1) {
244 /* set newtio */
245 struct termios newtio;
246 memset(&newtio, 0, sizeof(newtio));
247 /* no flow control for uart by default */
248 newtio.c_cflag = B115200 | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
249 newtio.c_iflag = IGNPAR;
250 //newtio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
251 newtio.c_oflag = 0;
252 newtio.c_lflag = 0; /* disable ECHO, ICANON, etc... */
253
254 newtio.c_cc[VERASE] = 0x8; /* del */
255 newtio.c_cc[VEOF] = 4; /* Ctrl-d */
256 newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
257 newtio.c_cc[VEOL] = 0xD; /* '\0' */
258
259 tcflush(*master_fd, TCIFLUSH);
260 tcsetattr(*master_fd, TCSANOW, &newtio);
261 }
262 return 0;
263
264ERROR:
265 if (0 < *master_fd) {
266 close(*master_fd);
267 *master_fd = -1;
268 }
269
270 if (0 < *slave_fd) {
271 close(*slave_fd);
272 *slave_fd = -1;
273 }
274
275 return -1;
276}
277
278int gnss_nmea_sscanf(const char *str, char *ret,...)
279{
280 const char *ptr = str;
281 char *argv[16];
282 int argc;
283 va_list ap;
284 int i = 0;
285
286 va_start(ap, ret);
287 argc = 0;
288 argv[argc] = ret; // First arg.
289
290 do {
291 i = 0;
292 while(*ptr && *ptr != ',' && *ptr != '*') {
293 argv[argc][i++] = *ptr++;
294 }
295 ptr++; // Jump ',' or '*'
296 argc++;
297 } while((argv[argc] = va_arg(ap, char*)) != 0);
298
299 va_end(ap);
300
301 return argc;
302}
303
304void gnssStartTimer(struct uloop_timeout *timeout, int timeVal)
305{
306 //UNUSED(timeout);
307 LOGD("%s: timeVal=%lu.", __FUNCTION__, timeVal);
308 uloop_timeout_set(timeout, timeVal);
309 return;
310}
311
312void gnssStopTimer(struct uloop_timeout *timeout)
313{
314 //UNUSED(timeout);
315 uloop_timeout_cancel(timeout);
316 return;
317}
318