1 #include "private-libwebsockets.h"
4 * included from libwebsockets.c for OPTEE builds
7 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen);
9 unsigned long long time_in_microseconds(void)
11 return ((unsigned long long)time(NULL)) * 1000000;
15 lws_get_random(struct lws_context *context, void *buf, int len)
17 TEE_GenerateRandom(buf, len);
23 lws_send_pipe_choked(struct lws *wsi)
26 struct lws_pollfd fds;
28 /* treat the fact we got a truncated send pending as if we're choked */
36 if (poll(&fds, 1, 0) != 1)
39 if ((fds.revents & POLLOUT) == 0)
42 /* okay to send another packet without blocking */
48 lws_poll_listen_fd(struct lws_pollfd *fd)
50 // return poll(fd, 1, 0);
56 lws_cancel_service_pt(struct lws *wsi)
59 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
62 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
63 lwsl_err("Cannot write to dummy pipe");
68 lws_cancel_service(struct lws_context *context)
71 struct lws_context_per_thread *pt = &context->pt[0];
72 char buf = 0, m = context->count_threads;
75 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
76 lwsl_err("Cannot write to dummy pipe");
82 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
84 IMSG("%d: %s\n", level, line);
88 LWS_VISIBLE LWS_EXTERN int
89 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
91 struct lws_context_per_thread *pt;
95 /* stay dead once we are dead */
97 if (!context || !context->vhost_list)
100 pt = &context->pt[tsi];
105 if (!context->service_tid_detected) {
108 memset(&_lws, 0, sizeof(_lws));
109 _lws.context = context;
111 context->service_tid_detected =
112 context->vhost_list->protocols[0].callback(
113 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
115 context->service_tid = context->service_tid_detected;
118 * is there anybody with pending stuff that needs service forcing?
120 if (!lws_service_adjust_timeout(context, 1, tsi)) {
121 lwsl_notice("%s: doing forced service\n", __func__);
122 /* -1 timeout means just do forced service */
123 _lws_plat_service_tsi(context, -1, pt->tid);
124 /* still somebody left who wants forced service? */
125 if (!lws_service_adjust_timeout(context, 1, pt->tid))
126 /* yes... come back again quickly */
130 n = poll(pt->fds, pt->fds_count, timeout_ms);
132 #ifdef LWS_OPENSSL_SUPPORT
133 if (!pt->rx_draining_ext_list &&
134 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
136 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
138 lws_service_fd_tsi(context, NULL, tsi);
143 m = lws_service_flag_pending(context, tsi);
145 c = -1; /* unknown limit */
148 if (LWS_ERRNO != LWS_EINTR)
154 /* any socket with events to service? */
155 for (n = 0; n < pt->fds_count && c; n++) {
156 if (!pt->fds[n].revents)
161 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
162 if (read(pt->fds[n].fd, &buf, 1) != 1)
163 lwsl_err("Cannot read from dummy pipe.");
167 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
170 /* if something closed, retry this slot */
179 lws_plat_check_connection_error(struct lws *wsi)
185 lws_plat_service(struct lws_context *context, int timeout_ms)
187 return _lws_plat_service_tsi(context, timeout_ms, 0);
191 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
197 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
202 lws_plat_context_early_init(void)
208 lws_plat_context_early_destroy(struct lws_context *context)
213 lws_plat_context_late_destroy(struct lws_context *context)
215 if (context->lws_lookup)
216 lws_free(context->lws_lookup);
219 /* cast a struct sockaddr_in6 * into addr for ipv6 */
222 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
229 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
231 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
233 pt->fds[pt->fds_count++].revents = 0;
237 lws_plat_delete_socket_from_fds(struct lws_context *context,
238 struct lws *wsi, int m)
240 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
246 lws_plat_service_periodic(struct lws_context *context)
251 lws_plat_change_pollfd(struct lws_context *context,
252 struct lws *wsi, struct lws_pollfd *pfd)
257 LWS_VISIBLE const char *
258 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
260 //return inet_ntop(af, src, dst, cnt);
261 return "lws_plat_inet_ntop";
264 static lws_filefd_type
265 _lws_plat_file_open(struct lws *wsi, const char *filename,
266 unsigned long *filelen, int flags)
272 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
278 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
284 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
285 unsigned char *buf, unsigned long len)
292 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
293 unsigned char *buf, unsigned long len)
301 lws_plat_init(struct lws_context *context,
302 struct lws_context_creation_info *info)
304 /* master context has the global fd lookup array */
305 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
307 if (context->lws_lookup == NULL) {
308 lwsl_err("OOM on lws_lookup array for %d connections\n",
313 lwsl_notice(" mem: platform fd map: %5u bytes\n",
314 sizeof(struct lws *) * context->max_fds);
316 context->fops.open = _lws_plat_file_open;
317 context->fops.close = _lws_plat_file_close;
318 context->fops.seek_cur = _lws_plat_file_seek_cur;
319 context->fops.read = _lws_plat_file_read;
320 context->fops.write = _lws_plat_file_write;
322 #ifdef LWS_WITH_PLUGINS
323 if (info->plugin_dirs)
324 lws_plat_plugins_init(context, info->plugin_dirs);