-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
73 lines (60 loc) · 1.34 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <time.h>
#define blocksize 255
#define DEBUG 1
int sfd;
char buf[blocksize];
int initWiFi();
int ATWiFi();
int getBlock();
int main(int argc, char** argv) {
initWiFi();
ATWiFi();
return (EXIT_SUCCESS);
}
int initWiFi() {
printf("Starting");
system("sudo systemctl stop [email protected]");
sfd = open("/dev/serial0", O_RDWR | O_NOCTTY);
if (sfd == -1) {
printf("Error no is : %d\n", errno);
printf("Error description is : %s\n", strerror(errno));
return -1;
}
struct termios options;
tcgetattr(sfd, &options);cfsetspeed(&options, B115200);
cfmakeraw(&options);
options.c_cflag &= ~CSTOPB;
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = blocksize;
tcsetattr(sfd, TCSANOW, &options);
};
int getBlock() {
int bytes;
struct timespec pause;
pause.tv_sec = 0;
pause.tv_nsec = 100000000;
nanosleep(&pause, NULL);
memset(buf, '\0', sizeof (buf));
ioctl(sfd, FIONREAD, &bytes);
if (bytes == 0)return 0;
int count = read(sfd, buf, blocksize - 1);
buf[count] = 0;
if (DEBUG) {
printf("%s", buf);
fflush(stdout);
}
return count;
}
int ATWiFi() {dprintf(sfd, "AT\r\n");
return getBlock();
}