1 /*
2 * lws-minimal-raw-file
3 *
4 * Written in 2010-2019 by Andy Green <[email protected]>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * This demonstrates dealing with a serial port
10 */
11
12 #include <libwebsockets.h>
13 #include <string.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18
19 #include <termios.h>
20 #include <sys/ioctl.h>
21
22 #if defined(__linux__)
23 #include <asm/ioctls.h>
24 #include <linux/serial.h>
25 #endif
26
27 struct raw_vhd {
28 lws_sorted_usec_list_t sul;
29 struct lws *wsi;
30 int filefd;
31 };
32
33 static char filepath[256];
34
35 static void
sul_cb(lws_sorted_usec_list_t * sul)36 sul_cb(lws_sorted_usec_list_t *sul)
37 {
38 struct raw_vhd *v = lws_container_of(sul, struct raw_vhd, sul);
39
40 lws_callback_on_writable(v->wsi);
41
42 lws_sul_schedule(lws_get_context(v->wsi), 0, &v->sul, sul_cb,
43 2 * LWS_USEC_PER_SEC);
44 }
45
46 static int
callback_raw_test(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)47 callback_raw_test(struct lws *wsi, enum lws_callback_reasons reason,
48 void *user, void *in, size_t len)
49 {
50 struct raw_vhd *vhd = (struct raw_vhd *)lws_protocol_vh_priv_get(
51 lws_get_vhost(wsi), lws_get_protocol(wsi));
52 #if defined(__linux__)
53 struct serial_struct s_s;
54 #endif
55 lws_sock_file_fd_type u;
56 struct termios tio;
57 uint8_t buf[1024];
58 int n;
59
60 switch (reason) {
61 case LWS_CALLBACK_PROTOCOL_INIT:
62 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
63 lws_get_protocol(wsi), sizeof(struct raw_vhd));
64 vhd->filefd = lws_open(filepath, O_RDWR);
65 if (vhd->filefd == -1) {
66 lwsl_err("Unable to open %s\n", filepath);
67
68 return 1;
69 }
70
71 tcflush(vhd->filefd, TCIOFLUSH);
72
73 #if defined(__linux__)
74 if (ioctl(vhd->filefd, TIOCGSERIAL, &s_s) == 0) {
75 s_s.closing_wait = ASYNC_CLOSING_WAIT_NONE;
76 ioctl(vhd->filefd, TIOCSSERIAL, &s_s);
77 }
78 #endif
79
80 /* enforce suitable tty state */
81
82 memset(&tio, 0, sizeof tio);
83 if (tcgetattr(vhd->filefd, &tio)) {
84 close(vhd->filefd);
85 vhd->filefd = -1;
86 return -1;
87 }
88
89 cfsetispeed(&tio, B115200);
90 cfsetospeed(&tio, B115200);
91
92 tio.c_lflag &= (tcflag_t)~(ISIG | ICANON | IEXTEN | ECHO |
93 #if defined(__linux__)
94 XCASE |
95 #endif
96 ECHOE | ECHOK | ECHONL | ECHOCTL | ECHOKE);
97 tio.c_iflag &= (tcflag_t)~(INLCR | IGNBRK | IGNPAR | IGNCR | ICRNL |
98 IMAXBEL | IXON | IXOFF | IXANY
99 #if defined(__linux__)
100 | IUCLC
101 #endif
102 | 0xff);
103 tio.c_oflag = 0;
104
105 tio.c_cc[VMIN] = 1;
106 tio.c_cc[VTIME] = 0;
107 tio.c_cc[VEOF] = 1;
108 tio.c_cflag = tio.c_cflag & (unsigned long) ~(
109 #if defined(__linux__)
110 CBAUD |
111 #endif
112 CSIZE | CSTOPB | PARENB | CRTSCTS);
113 tio.c_cflag |= 0x1412 | CS8 | CREAD | CLOCAL;
114
115 tcsetattr(vhd->filefd, TCSANOW, &tio);
116
117 u.filefd = (lws_filefd_type)(long long)vhd->filefd;
118 if (!lws_adopt_descriptor_vhost(lws_get_vhost(wsi),
119 LWS_ADOPT_RAW_FILE_DESC, u,
120 "raw-test", NULL)) {
121 lwsl_err("Failed to adopt fifo descriptor\n");
122 close(vhd->filefd);
123 vhd->filefd = -1;
124
125 return 1;
126 }
127
128 break;
129
130 case LWS_CALLBACK_PROTOCOL_DESTROY:
131 if (vhd && vhd->filefd != -1)
132 close(vhd->filefd);
133 break;
134
135 /* callbacks related to raw file descriptor */
136
137 case LWS_CALLBACK_RAW_ADOPT_FILE:
138 lwsl_notice("LWS_CALLBACK_RAW_ADOPT_FILE\n");
139 vhd->wsi = wsi;
140 lws_sul_schedule(lws_get_context(wsi), 0, &vhd->sul, sul_cb, 1);
141 break;
142
143 case LWS_CALLBACK_RAW_RX_FILE:
144 lwsl_notice("LWS_CALLBACK_RAW_RX_FILE\n");
145 n = (int)read(vhd->filefd, buf, sizeof(buf));
146 if (n < 0) {
147 lwsl_err("Reading from %s failed\n", filepath);
148
149 return 1;
150 }
151 lwsl_hexdump_level(LLL_NOTICE, buf, (unsigned int)n);
152 break;
153
154 case LWS_CALLBACK_RAW_CLOSE_FILE:
155 lwsl_notice("LWS_CALLBACK_RAW_CLOSE_FILE\n");
156 lws_sul_cancel(&vhd->sul);
157 break;
158
159 case LWS_CALLBACK_RAW_WRITEABLE_FILE:
160 lwsl_notice("LWS_CALLBACK_RAW_WRITEABLE_FILE\n");
161 if (lws_write(wsi, (uint8_t *)"hello-this-is-written-every-couple-of-seconds\r\n", 47, LWS_WRITE_RAW) != 47)
162 return -1;
163 break;
164
165 default:
166 break;
167 }
168
169 return 0;
170 }
171
172 static struct lws_protocols protocols[] = {
173 { "raw-test", callback_raw_test, 0, 0, 0, NULL, 0 },
174 LWS_PROTOCOL_LIST_TERM
175 };
176
177 static int interrupted;
178
sigint_handler(int sig)179 void sigint_handler(int sig)
180 {
181 interrupted = 1;
182 }
183
main(int argc,const char ** argv)184 int main(int argc, const char **argv)
185 {
186 struct lws_context_creation_info info;
187 struct lws_context *context;
188 const char *p;
189 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
190 /* for LLL_ verbosity above NOTICE to be built into lws,
191 * lws must have been configured and built with
192 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
193 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
194 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
195 /* | LLL_DEBUG */;
196
197 signal(SIGINT, sigint_handler);
198
199 if ((p = lws_cmdline_option(argc, argv, "-d")))
200 logs = atoi(p);
201
202 lws_set_log_level(logs, NULL);
203 lwsl_user("LWS minimal raw serial\n");
204 if (argc < 2) {
205 lwsl_user("Usage: %s <serial device> "
206 " eg, /dev/ttyUSB0\n", argv[0]);
207
208 return 1;
209 }
210
211 signal(SIGINT, sigint_handler);
212
213 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
214 info.port = CONTEXT_PORT_NO_LISTEN_SERVER; /* no listen socket for demo */
215 info.protocols = protocols;
216
217 lws_strncpy(filepath, argv[1], sizeof(filepath));
218
219 context = lws_create_context(&info);
220 if (!context) {
221 lwsl_err("lws init failed\n");
222 return 1;
223 }
224
225 while (n >= 0 && !interrupted)
226 n = lws_service(context, 0);
227
228 lws_context_destroy(context);
229
230 return 0;
231 }
232