fops-zip
[platform/upstream/libwebsockets.git] / lib / lws-plat-esp32.c
1 #include "private-libwebsockets.h"
2 #include "freertos/timers.h"
3
4 /*
5  * included from libwebsockets.c for unix builds
6  */
7
8 unsigned long long time_in_microseconds(void)
9 {
10         struct timeval tv;
11         gettimeofday(&tv, NULL);
12         return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
13 }
14
15 LWS_VISIBLE int
16 lws_get_random(struct lws_context *context, void *buf, int len)
17 {
18         // !!!
19         return 0;
20 }
21
22 LWS_VISIBLE int
23 lws_send_pipe_choked(struct lws *wsi)
24 {
25         fd_set writefds;
26         struct timeval tv = { 0, 0 };
27
28         /* treat the fact we got a truncated send pending as if we're choked */
29         if (wsi->trunc_len)
30                 return 1;
31
32         FD_ZERO(&writefds);
33         FD_SET(wsi->desc.sockfd, &writefds);
34
35         if (select(wsi->desc.sockfd + 1, NULL, &writefds, NULL, &tv) < 1)
36                 return 1;
37
38         return 0;
39 }
40
41 LWS_VISIBLE int
42 lws_poll_listen_fd(struct lws_pollfd *fd)
43 {
44         fd_set readfds;
45         struct timeval tv = { 0, 0 };
46
47         FD_ZERO(&readfds);
48         FD_SET(fd->fd, &readfds);
49
50         return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
51 }
52
53 LWS_VISIBLE void
54 lws_cancel_service_pt(struct lws *wsi)
55 {
56 }
57
58 LWS_VISIBLE void
59 lws_cancel_service(struct lws_context *context)
60 {
61 }
62
63 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
64 {
65         printf("%d: %s", level, line);
66 }
67
68 LWS_VISIBLE LWS_EXTERN int
69 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
70 {
71         struct lws_context_per_thread *pt;
72         int n = -1, m, c;
73
74         /* stay dead once we are dead */
75
76         if (!context || !context->vhost_list)
77                 return 1;
78
79         pt = &context->pt[tsi];
80
81         if (timeout_ms < 0)
82                 goto faked_service;
83
84         if (!context->service_tid_detected) {
85                 struct lws _lws;
86
87                 memset(&_lws, 0, sizeof(_lws));
88                 _lws.context = context;
89
90                 context->service_tid_detected =
91                         context->vhost_list->protocols[0].callback(
92                         &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
93         }
94         context->service_tid = context->service_tid_detected;
95
96         /*
97          * is there anybody with pending stuff that needs service forcing?
98          */
99         if (!lws_service_adjust_timeout(context, 1, tsi)) {
100                 /* -1 timeout means just do forced service */
101                 _lws_plat_service_tsi(context, -1, pt->tid);
102                 /* still somebody left who wants forced service? */
103                 if (!lws_service_adjust_timeout(context, 1, pt->tid))
104                         /* yes... come back again quickly */
105                         timeout_ms = 0;
106         }
107
108 //      n = poll(pt->fds, pt->fds_count, timeout_ms);
109         {
110                 fd_set readfds, writefds, errfds;
111                 struct timeval tv = { timeout_ms / 1000,
112                                       (timeout_ms % 1000) * 1000 };
113                 int max_fd = 0;
114                 FD_ZERO(&readfds);
115                 FD_ZERO(&writefds);
116                 FD_ZERO(&errfds);
117
118                 for (n = 0; n < pt->fds_count; n++) {
119                         pt->fds[n].revents = 0;
120                         if (pt->fds[n].fd >= max_fd)
121                                 max_fd = pt->fds[n].fd;
122                         if (pt->fds[n].events & LWS_POLLIN)
123                                 FD_SET(pt->fds[n].fd, &readfds);
124                         if (pt->fds[n].events & LWS_POLLOUT)
125                                 FD_SET(pt->fds[n].fd, &writefds);
126                         FD_SET(pt->fds[n].fd, &errfds);
127                 }
128
129                 n = select(max_fd + 1, &readfds, &writefds, &errfds, &tv);
130                 for (n = 0; n < pt->fds_count; n++) {
131                         if (FD_ISSET(pt->fds[n].fd, &readfds))
132                                 pt->fds[n].revents |= LWS_POLLIN;
133                         if (FD_ISSET(pt->fds[n].fd, &writefds))
134                                 pt->fds[n].revents |= LWS_POLLOUT;
135                         if (FD_ISSET(pt->fds[n].fd, &errfds))
136                                 pt->fds[n].revents |= LWS_POLLHUP;
137                 }
138         }
139
140
141 #ifdef LWS_OPENSSL_SUPPORT
142         if (!pt->rx_draining_ext_list &&
143             !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
144 #else
145         if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
146 #endif
147                 lws_service_fd_tsi(context, NULL, tsi);
148                 return 0;
149         }
150
151 faked_service:
152         m = lws_service_flag_pending(context, tsi);
153         if (m)
154                 c = -1; /* unknown limit */
155         else
156                 if (n < 0) {
157                         if (LWS_ERRNO != LWS_EINTR)
158                                 return -1;
159                         return 0;
160                 } else
161                         c = n;
162
163         /* any socket with events to service? */
164         for (n = 0; n < pt->fds_count && c; n++) {
165                 if (!pt->fds[n].revents)
166                         continue;
167
168                 c--;
169
170                 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
171                 if (m < 0)
172                         return -1;
173                 /* if something closed, retry this slot */
174                 if (m)
175                         n--;
176         }
177
178         return 0;
179 }
180
181 LWS_VISIBLE int
182 lws_plat_check_connection_error(struct lws *wsi)
183 {
184         return 0;
185 }
186
187 LWS_VISIBLE int
188 lws_plat_service(struct lws_context *context, int timeout_ms)
189 {
190         return _lws_plat_service_tsi(context, timeout_ms, 0);
191 }
192
193 LWS_VISIBLE int
194 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
195 {
196         int optval = 1;
197         socklen_t optlen = sizeof(optval);
198
199 #if defined(__APPLE__) || \
200     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
201     defined(__NetBSD__) || \
202     defined(__OpenBSD__)
203         struct protoent *tcp_proto;
204 #endif
205
206         if (vhost->ka_time) {
207                 /* enable keepalive on this socket */
208                 optval = 1;
209                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
210                                (const void *)&optval, optlen) < 0)
211                         return 1;
212
213 #if defined(__APPLE__) || \
214     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
215     defined(__NetBSD__) || \
216         defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
217
218                 /*
219                  * didn't find a way to set these per-socket, need to
220                  * tune kernel systemwide values
221                  */
222 #else
223                 /* set the keepalive conditions we want on it too */
224                 optval = vhost->ka_time;
225                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
226                                (const void *)&optval, optlen) < 0)
227                         return 1;
228
229                 optval = vhost->ka_interval;
230                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
231                                (const void *)&optval, optlen) < 0)
232                         return 1;
233
234                 optval = vhost->ka_probes;
235                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
236                                (const void *)&optval, optlen) < 0)
237                         return 1;
238 #endif
239         }
240 #if 0
241         /* Disable Nagle */
242         optval = 1;
243 //      if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
244 //              return 1;
245         tcp_proto = getprotobyname("TCP");
246         if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
247                 return 1;
248 #endif
249         /* We are nonblocking... */
250         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
251                 return 1;
252
253         return 0;
254 }
255
256 LWS_VISIBLE void
257 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
258 {
259 }
260
261 LWS_VISIBLE int
262 lws_plat_context_early_init(void)
263 {
264         //signal(SIGPIPE, SIG_IGN);
265
266 //      signal(SIGABRT, sigabrt_handler);
267
268         return 0;
269 }
270
271 LWS_VISIBLE void
272 lws_plat_context_early_destroy(struct lws_context *context)
273 {
274 }
275
276 LWS_VISIBLE void
277 lws_plat_context_late_destroy(struct lws_context *context)
278 {
279 #ifdef LWS_WITH_PLUGINS
280         if (context->plugin_list)
281                 lws_plat_plugins_destroy(context);
282 #endif
283
284         if (context->lws_lookup)
285                 lws_free(context->lws_lookup);
286 }
287
288 /* cast a struct sockaddr_in6 * into addr for ipv6 */
289
290 LWS_VISIBLE int
291 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
292                     size_t addrlen)
293 {
294 #if 0
295         int rc = -1;
296
297         struct ifaddrs *ifr;
298         struct ifaddrs *ifc;
299 #ifdef LWS_USE_IPV6
300         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
301 #endif
302
303         getifaddrs(&ifr);
304         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
305                 if (!ifc->ifa_addr)
306                         continue;
307
308                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
309
310                 if (strcmp(ifc->ifa_name, ifname))
311                         continue;
312
313                 switch (ifc->ifa_addr->sa_family) {
314                 case AF_INET:
315 #ifdef LWS_USE_IPV6
316                         if (ipv6) {
317                                 /* map IPv4 to IPv6 */
318                                 bzero((char *)&addr6->sin6_addr,
319                                                 sizeof(struct in6_addr));
320                                 addr6->sin6_addr.s6_addr[10] = 0xff;
321                                 addr6->sin6_addr.s6_addr[11] = 0xff;
322                                 memcpy(&addr6->sin6_addr.s6_addr[12],
323                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
324                                                         sizeof(struct in_addr));
325                         } else
326 #endif
327                                 memcpy(addr,
328                                         (struct sockaddr_in *)ifc->ifa_addr,
329                                                     sizeof(struct sockaddr_in));
330                         break;
331 #ifdef LWS_USE_IPV6
332                 case AF_INET6:
333                         memcpy(&addr6->sin6_addr,
334                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
335                                                        sizeof(struct in6_addr));
336                         break;
337 #endif
338                 default:
339                         continue;
340                 }
341                 rc = 0;
342         }
343
344         freeifaddrs(ifr);
345
346         if (rc == -1) {
347                 /* check if bind to IP address */
348 #ifdef LWS_USE_IPV6
349                 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
350                         rc = 0;
351                 else
352 #endif
353                 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
354                         rc = 0;
355         }
356
357         return rc;
358 #endif
359
360         return -1;
361 }
362
363 LWS_VISIBLE void
364 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
365 {
366         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
367
368         pt->fds[pt->fds_count++].revents = 0;
369 }
370
371 LWS_VISIBLE void
372 lws_plat_delete_socket_from_fds(struct lws_context *context,
373                                                 struct lws *wsi, int m)
374 {
375         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
376
377         pt->fds_count--;
378 }
379
380 LWS_VISIBLE void
381 lws_plat_service_periodic(struct lws_context *context)
382 {
383 }
384
385 LWS_VISIBLE int
386 lws_plat_change_pollfd(struct lws_context *context,
387                       struct lws *wsi, struct lws_pollfd *pfd)
388 {
389         return 0;
390 }
391
392 LWS_VISIBLE const char *
393 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
394 {
395         return inet_ntop(af, src, dst, cnt);
396 }
397
398 LWS_VISIBLE lws_fop_fd_t
399 _lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
400                     const char *vpath, lws_fop_flags_t *flags)
401 {
402         struct stat stat_buf;
403         lws_fop_fd_t fop_fd;
404         int ret = open(filename, *flags, 0664);
405
406         if (ret < 0)
407                 return NULL;
408
409         if (fstat(ret, &stat_buf) < 0)
410
411         fop_fd = malloc(sizeof(*fop_fd));
412         if (!fop_fd)
413                 goto bail;
414
415         fop_fd->fops = fops;
416         fop_fd->fd = ret;
417         fop_fd->flags = *flags;
418         fop_fd->filesystem_priv = NULL; /* we don't use it */
419         fop_fd->pos = 0;
420         fop_fd->len = stat_buf.st_size;
421
422         return fop_fd;
423
424 bail:
425         close(ret);
426
427         return NULL;
428 }
429
430 LWS_VISIBLE int
431 _lws_plat_file_close(lws_fop_fd_t *fops_fd)
432 {
433         int fd = (*fops_fd)->fd;
434
435         free(*fops_fd);
436         *fops_fd = NULL;
437
438         return close(fd);
439 }
440
441 LWS_VISIBLE lws_fileofs_t
442 _lws_plat_file_seek_cur(lws_fop_fd_t fops_fd, lws_fileofs_t offset)
443 {
444         return lseek(fops_fd->fd, offset, SEEK_CUR);
445 }
446
447 LWS_VISIBLE int
448 _lws_plat_file_read(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
449                     uint8_t *buf, lws_filepos_t len)
450 {
451         long n;
452
453         n = read(fops_fd->fd, buf, len);
454         if (n == -1) {
455                 *amount = 0;
456                 return -1;
457         }
458         fop_fd->pos += n;
459         *amount = n;
460
461         return 0;
462 }
463
464 LWS_VISIBLE int
465 _lws_plat_file_write(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
466                      uint8_t *buf, lws_filepos_t len)
467 {
468         long n;
469
470         n = write(fops_fd->fd, buf, len);
471         if (n == -1) {
472                 *amount = 0;
473                 return -1;
474         }
475         fop_fd->pos += n;
476         *amount = n;
477
478         return 0;
479 }
480
481
482 LWS_VISIBLE int
483 lws_plat_init(struct lws_context *context,
484               struct lws_context_creation_info *info)
485 {
486         /* master context has the global fd lookup array */
487         context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
488                                          context->max_fds);
489         if (context->lws_lookup == NULL) {
490                 lwsl_err("OOM on lws_lookup array for %d connections\n",
491                          context->max_fds);
492                 return 1;
493         }
494
495         lwsl_notice(" mem: platform fd map: %5lu bytes\n",
496                     (unsigned long)(sizeof(struct lws *) * context->max_fds));
497
498 #ifdef LWS_WITH_PLUGINS
499         if (info->plugin_dirs)
500                 lws_plat_plugins_init(context, info->plugin_dirs);
501 #endif
502
503         return 0;
504 }
505
506
507 LWS_VISIBLE void esp32_uvtimer_cb(TimerHandle_t t)
508 {
509         struct timer_mapping *p = pvTimerGetTimerID(t);
510
511         p->cb(p->t);
512 }
513