lws-plat-optee.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "private-libwebsockets.h"
  2. /*
  3. * included from libwebsockets.c for OPTEE builds
  4. */
  5. void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen);
  6. unsigned long long time_in_microseconds(void)
  7. {
  8. return ((unsigned long long)time(NULL)) * 1000000;
  9. }
  10. #if 0
  11. LWS_VISIBLE int
  12. lws_get_random(struct lws_context *context, void *buf, int len)
  13. {
  14. TEE_GenerateRandom(buf, len);
  15. return len;
  16. }
  17. #endif
  18. LWS_VISIBLE int
  19. lws_send_pipe_choked(struct lws *wsi)
  20. {
  21. #if 0
  22. struct lws_pollfd fds;
  23. /* treat the fact we got a truncated send pending as if we're choked */
  24. if (wsi->trunc_len)
  25. return 1;
  26. fds.fd = wsi->desc.sockfd;
  27. fds.events = POLLOUT;
  28. fds.revents = 0;
  29. if (poll(&fds, 1, 0) != 1)
  30. return 1;
  31. if ((fds.revents & POLLOUT) == 0)
  32. return 1;
  33. #endif
  34. /* okay to send another packet without blocking */
  35. return 0;
  36. }
  37. LWS_VISIBLE int
  38. lws_poll_listen_fd(struct lws_pollfd *fd)
  39. {
  40. // return poll(fd, 1, 0);
  41. return 0;
  42. }
  43. LWS_VISIBLE void
  44. lws_cancel_service_pt(struct lws *wsi)
  45. {
  46. #if 0
  47. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  48. char buf = 0;
  49. if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
  50. lwsl_err("Cannot write to dummy pipe");
  51. #endif
  52. }
  53. LWS_VISIBLE void
  54. lws_cancel_service(struct lws_context *context)
  55. {
  56. #if 0
  57. struct lws_context_per_thread *pt = &context->pt[0];
  58. char buf = 0, m = context->count_threads;
  59. while (m--) {
  60. if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
  61. lwsl_err("Cannot write to dummy pipe");
  62. pt++;
  63. }
  64. #endif
  65. }
  66. #if 0
  67. LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
  68. {
  69. IMSG("%d: %s\n", level, line);
  70. }
  71. #endif
  72. LWS_VISIBLE LWS_EXTERN int
  73. _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
  74. {
  75. struct lws_context_per_thread *pt;
  76. int n = -1, m, c;
  77. //char buf;
  78. /* stay dead once we are dead */
  79. if (!context || !context->vhost_list)
  80. return 1;
  81. pt = &context->pt[tsi];
  82. if (timeout_ms < 0)
  83. goto faked_service;
  84. if (!context->service_tid_detected) {
  85. struct lws _lws;
  86. memset(&_lws, 0, sizeof(_lws));
  87. _lws.context = context;
  88. context->service_tid_detected =
  89. context->vhost_list->protocols[0].callback(
  90. &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
  91. }
  92. context->service_tid = context->service_tid_detected;
  93. /*
  94. * is there anybody with pending stuff that needs service forcing?
  95. */
  96. if (!lws_service_adjust_timeout(context, 1, tsi)) {
  97. lwsl_notice("%s: doing forced service\n", __func__);
  98. /* -1 timeout means just do forced service */
  99. _lws_plat_service_tsi(context, -1, pt->tid);
  100. /* still somebody left who wants forced service? */
  101. if (!lws_service_adjust_timeout(context, 1, pt->tid))
  102. /* yes... come back again quickly */
  103. timeout_ms = 0;
  104. }
  105. #if 1
  106. n = poll(pt->fds, pt->fds_count, timeout_ms);
  107. #ifdef LWS_OPENSSL_SUPPORT
  108. if (!pt->rx_draining_ext_list &&
  109. !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
  110. #else
  111. if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
  112. #endif
  113. lws_service_fd_tsi(context, NULL, tsi);
  114. return 0;
  115. }
  116. #endif
  117. faked_service:
  118. m = lws_service_flag_pending(context, tsi);
  119. if (m)
  120. c = -1; /* unknown limit */
  121. else
  122. if (n < 0) {
  123. if (LWS_ERRNO != LWS_EINTR)
  124. return -1;
  125. return 0;
  126. } else
  127. c = n;
  128. /* any socket with events to service? */
  129. for (n = 0; n < pt->fds_count && c; n++) {
  130. if (!pt->fds[n].revents)
  131. continue;
  132. c--;
  133. #if 0
  134. if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
  135. if (read(pt->fds[n].fd, &buf, 1) != 1)
  136. lwsl_err("Cannot read from dummy pipe.");
  137. continue;
  138. }
  139. #endif
  140. m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
  141. if (m < 0)
  142. return -1;
  143. /* if something closed, retry this slot */
  144. if (m)
  145. n--;
  146. }
  147. return 0;
  148. }
  149. LWS_VISIBLE int
  150. lws_plat_check_connection_error(struct lws *wsi)
  151. {
  152. return 0;
  153. }
  154. LWS_VISIBLE int
  155. lws_plat_service(struct lws_context *context, int timeout_ms)
  156. {
  157. return _lws_plat_service_tsi(context, timeout_ms, 0);
  158. }
  159. LWS_VISIBLE int
  160. lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
  161. {
  162. return 0;
  163. }
  164. LWS_VISIBLE void
  165. lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
  166. {
  167. }
  168. LWS_VISIBLE int
  169. lws_plat_context_early_init(void)
  170. {
  171. return 0;
  172. }
  173. LWS_VISIBLE void
  174. lws_plat_context_early_destroy(struct lws_context *context)
  175. {
  176. }
  177. LWS_VISIBLE void
  178. lws_plat_context_late_destroy(struct lws_context *context)
  179. {
  180. if (context->lws_lookup)
  181. lws_free(context->lws_lookup);
  182. }
  183. /* cast a struct sockaddr_in6 * into addr for ipv6 */
  184. LWS_VISIBLE int
  185. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  186. size_t addrlen)
  187. {
  188. return -1;
  189. }
  190. LWS_VISIBLE void
  191. lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
  192. {
  193. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  194. pt->fds[pt->fds_count++].revents = 0;
  195. }
  196. LWS_VISIBLE void
  197. lws_plat_delete_socket_from_fds(struct lws_context *context,
  198. struct lws *wsi, int m)
  199. {
  200. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  201. pt->fds_count--;
  202. }
  203. LWS_VISIBLE void
  204. lws_plat_service_periodic(struct lws_context *context)
  205. {
  206. }
  207. LWS_VISIBLE int
  208. lws_plat_change_pollfd(struct lws_context *context,
  209. struct lws *wsi, struct lws_pollfd *pfd)
  210. {
  211. return 0;
  212. }
  213. LWS_VISIBLE const char *
  214. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
  215. {
  216. //return inet_ntop(af, src, dst, cnt);
  217. return "lws_plat_inet_ntop";
  218. }
  219. LWS_VISIBLE lws_fop_fd_t
  220. _lws_plat_file_open(lws_plat_file_open(struct lws_plat_file_ops *fops,
  221. const char *filename, lws_fop_flags_t *flags)
  222. {
  223. return NULL;
  224. }
  225. LWS_VISIBLE int
  226. _lws_plat_file_close(lws_fop_fd_t *fop_fd)
  227. {
  228. return 0;
  229. }
  230. LWS_VISIBLE lws_fileofs_t
  231. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  232. {
  233. return 0;
  234. }
  235. LWS_VISIBLE int
  236. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  237. uint8_t *buf, lws_filepos_t len)
  238. {
  239. return 0;
  240. }
  241. LWS_VISIBLE int
  242. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  243. uint8_t *buf, lws_filepos_t len)
  244. {
  245. return 0;
  246. }
  247. LWS_VISIBLE int
  248. lws_plat_init(struct lws_context *context,
  249. struct lws_context_creation_info *info)
  250. {
  251. /* master context has the global fd lookup array */
  252. context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
  253. context->max_fds);
  254. if (context->lws_lookup == NULL) {
  255. lwsl_err("OOM on lws_lookup array for %d connections\n",
  256. context->max_fds);
  257. return 1;
  258. }
  259. lwsl_notice(" mem: platform fd map: %5lu bytes\n",
  260. (long)sizeof(struct lws *) * context->max_fds);
  261. #ifdef LWS_WITH_PLUGINS
  262. if (info->plugin_dirs)
  263. lws_plat_plugins_init(context, info->plugin_dirs);
  264. #endif
  265. return 0;
  266. }