mbed3 plat
[platform/upstream/libwebsockets.git] / lib / lws-plat-mbed3.c
1 #include "private-libwebsockets.h"
2
3 /*
4  * included from libwebsockets.c for MBED3 builds
5  * MBED3 is an "OS" for very small embedded systems.
6  * He doesn't have Posix semantics or apis.
7  * But he has things like TCP sockets.
8  */
9
10 unsigned long long time_in_microseconds(void)
11 {
12         return 0;
13 }
14
15 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
16                                                              void *buf, int len)
17 {
18         (void)context;
19         int n = len;
20         unsigned char *b = (unsigned char *)buf;
21         while (n--)
22                 b[n]= rand();
23         return len;
24 }
25
26 /*
27  * MBED3 does not have a 'kernel' which takes copies of what userland wants
28  * to send.  The user application must hold the tx buffer until it is informed
29  * that send of the user buffer was complete.
30  * 
31  * So as soon as you send something the pipe is globally choked.
32  * 
33  * There is no concept of additional sent things being maybe acceptable.
34  * You can send one thing up to 64KB at a time and may not try to send
35  * anything else until that is completed.
36  * 
37  * You can send things on other sockets, but they cannot complete until they
38  * get their turn at the network device.
39  */
40
41 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
42 {
43 #if 0
44         struct libwebsocket_pollfd fds;
45
46         /* treat the fact we got a truncated send pending as if we're choked */
47         if (wsi->truncated_send_len)
48                 return 1;
49
50         fds.fd = wsi->sock;
51         fds.events = POLLOUT;
52         fds.revents = 0;
53
54         if (poll(&fds, 1, 0) != 1)
55                 return 1;
56
57         if ((fds.revents & POLLOUT) == 0)
58                 return 1;
59
60         /* okay to send another packet without blocking */
61 #endif
62         (void)wsi;
63         return 0;
64 }
65
66 LWS_VISIBLE int
67 lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
68 {
69         (void)fd;
70         return -1;
71 }
72
73 /**
74  * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
75  * @context:    Websocket context
76  *
77  *      This function let a call to libwebsocket_service() waiting for a timeout
78  *      immediately return.
79  * 
80  *      There is no poll() in MBED3, he will fire callbacks when he feels like
81  *      it.
82  */
83 LWS_VISIBLE void
84 libwebsocket_cancel_service(struct libwebsocket_context *context)
85 {
86         (void)context;
87 }
88
89 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
90 {
91         printf("%d: %s", level, line);
92 }
93
94 LWS_VISIBLE int
95 lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
96 {
97         (void)context;
98         (void)timeout_ms;
99 #if 0
100         int n;
101         int m;
102         char buf;
103 #ifdef LWS_OPENSSL_SUPPORT
104         struct libwebsocket *wsi, *wsi_next;
105 #endif
106
107         /* stay dead once we are dead */
108
109         if (!context)
110                 return 1;
111
112         lws_libev_run(context);
113
114         context->service_tid = context->protocols[0].callback(context, NULL,
115                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
116
117 #ifdef LWS_OPENSSL_SUPPORT
118         /* if we know we have non-network pending data, do not wait in poll */
119         if (lws_ssl_anybody_has_buffered_read(context))
120                 timeout_ms = 0;
121 #endif
122         n = poll(context->fds, context->fds_count, timeout_ms);
123         context->service_tid = 0;
124
125 #ifdef LWS_OPENSSL_SUPPORT
126         if (!lws_ssl_anybody_has_buffered_read(context) && n == 0) {
127 #else
128         if (n == 0) /* poll timeout */ {
129 #endif
130                 libwebsocket_service_fd(context, NULL);
131                 return 0;
132         }
133
134         if (n < 0) {
135                 if (LWS_ERRNO != LWS_EINTR)
136                         return -1;
137                 return 0;
138         }
139
140         /* any socket with events to service? */
141
142         for (n = 0; n < context->fds_count; n++) {
143
144                 if (!context->fds[n].revents)
145                         continue;
146
147                 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
148                         if (read(context->fds[n].fd, &buf, 1) != 1)
149                                 lwsl_err("Cannot read from dummy pipe.");
150                         continue;
151                 }
152
153                 m = libwebsocket_service_fd(context, &context->fds[n]);
154                 if (m < 0)
155                         return -1;
156                 /* if something closed, retry this slot */
157                 if (m)
158                         n--;
159         }
160 #endif
161         return 0;
162 }
163
164 LWS_VISIBLE int
165 lws_plat_set_socket_options(struct libwebsocket_context *context, lws_sockfd_type fd)
166 {
167         (void)context;
168         (void)fd;
169         return 0;
170 }
171
172 LWS_VISIBLE void
173 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
174 {
175         (void)info;
176 }
177
178 LWS_VISIBLE int
179 lws_plat_init_lookup(struct libwebsocket_context *context)
180 {
181         (void)context;
182         return 0;
183 }
184
185 LWS_VISIBLE int
186 lws_plat_init_fd_tables(struct libwebsocket_context *context)
187 {
188         (void)context;
189         return 0;
190 }
191
192
193 LWS_VISIBLE int
194 lws_plat_context_early_init(void)
195 {
196         return 0;
197 }
198
199 LWS_VISIBLE void
200 lws_plat_context_early_destroy(struct libwebsocket_context *context)
201 {
202         (void)context;
203 }
204
205 LWS_VISIBLE void
206 lws_plat_context_late_destroy(struct libwebsocket_context *context)
207 {
208         (void)context;
209 }
210
211
212 LWS_VISIBLE void
213 lws_plat_service_periodic(struct libwebsocket_context *context)
214 {
215         (void)context;
216 }
217
218 LWS_VISIBLE int
219 lws_plat_change_pollfd(struct libwebsocket_context *context,
220                       struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
221 {
222         (void)context;
223         (void)wsi;
224         (void)pfd;
225         
226         return 0;
227 }
228
229 LWS_VISIBLE int
230 lws_plat_open_file(const char* filename, unsigned long* filelen)
231 {
232         (void)filename;
233         (void)filelen;
234         return LWS_INVALID_FILE;
235 }
236
237 LWS_VISIBLE const char *
238 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
239 {
240         (void)af;
241         (void)src;
242         (void)dst;
243         (void)cnt;
244         return "unsupported";
245 }
246
247 LWS_VISIBLE int
248 insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi)
249 {
250         (void)context;
251         (void)wsi;
252
253         return 0;
254 }
255
256 LWS_VISIBLE int
257 delete_from_fd(struct libwebsocket_context *context, lws_sockfd_type fd)
258 {
259         (void)context;
260         (void)fd;
261
262         return 1;
263 }