Sat, 20 Oct 2018 17:09:48 +0200
Updated lots of doxygen comments
0 | 1 | /** |
2 | * @file vnc-server.c | |
3 | * @brief VNC server original written for eCos. | |
4 | * | |
5 | * The VNC server runs on the target platform and waits for a client | |
6 | * (vncviewer program running on a PC) to connect via an ethernet connection | |
7 | * (port 5900). Once a client has connected, the target platform has a | |
8 | * virtual screen, keyboard and mouse. | |
9 | * | |
10 | * This port of the VNC server has been designed with some limitations in | |
11 | * order to keep memory and processor power requirements to a minimum. | |
12 | * | |
13 | * The VNC client must accept the settings that the VNC server suggests (bits-per-pixel, | |
14 | * number-of-colours and so on as specified at build time with the eCos configuration | |
15 | * tool) or the VNC server will just disconnect. | |
16 | * | |
17 | * The VNC server only supports CoRRE encoding and RAW encoding for sending | |
18 | * display data to the client. | |
19 | * | |
20 | * The VNC server does not support password authentication, the client is able | |
21 | * to connect without the user typing in a password. | |
22 | * | |
23 | * Only one VNC client may connect to the VNC server at a time. | |
24 | * | |
25 | * In reality these limitations are not a problem. | |
26 | * | |
27 | * @author Chris Garry <cgarry@sweeneydesign.co.uk> | |
28 | * @author Michiel Broek. | |
29 | */ | |
30 | ||
31 | #include "vnc-server.h" | |
32 | #include "websocket_server.h" | |
33 | ||
34 | static const char *TAG = "vnc-server"; | |
35 | ||
36 | #define BACKLOG 5 /* Number of pending connections queue will hold */ | |
37 | #define MESSAGE_BUFFER_SIZE 60 /* Was 50, but I have seen 52 bytes length */ | |
38 | #define TILE_SIZE 16 | |
39 | #define TRUE_COLOUR_FLAG 1 /* True colour is set */ | |
40 | #define BIG_ENDIAN_FLAG 1 /* Always send colour data big endian */ | |
41 | ||
42 | /* Default display parameters */ | |
43 | #define BITS_PER_PIXEL 8 /* Bits per pixel */ | |
44 | #define PIXEL_DEPTH 8 /* Usefull bits per pixel */ | |
45 | #define RED_MAX 7 | |
46 | #define GREEN_MAX 7 | |
47 | #define BLUE_MAX 3 | |
48 | #define RED_SHIFT 5 | |
49 | #define GREEN_SHIFT 2 | |
50 | #define BLUE_SHIFT 0 | |
51 | ||
52 | ||
53 | /* Initial default RGB332 */ | |
54 | uint8_t Bits_Per_Pixel = 8; | |
55 | ||
56 | uint16_t Red_Max = RED_MAX; | |
57 | uint16_t Green_Max = GREEN_MAX; | |
58 | uint16_t Blue_Max = BLUE_MAX; | |
59 | uint8_t Red_Shift = RED_SHIFT; | |
60 | uint8_t Green_Shift = GREEN_SHIFT; | |
61 | uint8_t Blue_Shift = BLUE_SHIFT; | |
62 | bool AltPixels = false; | |
63 | ||
64 | /* Client to Server message types */ | |
65 | #define SET_PIXEL_FORMAT 0 | |
66 | #define FIX_COLOUR_MAP_ENTRIES 1 | |
67 | #define SET_ENCODINGS 2 | |
68 | #define FRAME_BUFFER_UPDATE_REQ 3 | |
69 | #define KEY_EVENT 4 | |
70 | #define POINTER_EVENT 5 | |
71 | #define CLIENT_CUT_TEXT 6 | |
72 | ||
73 | /* Macros to split colour to bytes */ | |
74 | #define COLOUR2BYTE1(col) ((col>>8)&0xFF) | |
75 | #define COLOUR2BYTE0(col) (col&0xFF) | |
76 | ||
77 | ||
78 | /* Thread function prototypes */ | |
79 | static TaskHandle_t xTaskClientHandler = NULL; ///< Task for VNC clients | |
80 | static TaskHandle_t xTaskFrameUpdate = NULL; ///< Task for framebuffer updates | |
81 | static TaskHandle_t xTaskWSclient = NULL; ///< Task for websocket clients | |
82 | ||
83 | SemaphoreHandle_t SoundBell_lock = NULL; ///< Lock for the bell sound. | |
84 | ||
85 | static QueueHandle_t message_queue; ///< Websockets message queue | |
86 | const static int message_queue_size = 5; ///< Message queue size | |
87 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
88 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
89 | * @brief VNC messages. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
90 | */ |
0 | 91 | struct strMessage { |
92 | int length; ///< Length of the message | |
93 | uint8_t message[MESSAGE_BUFFER_SIZE]; ///< The message | |
94 | }; | |
95 | ||
96 | ||
97 | uint8_t VNC_pointer_button = 0; | |
98 | uint16_t VNC_pointer_x = 0; | |
99 | uint16_t VNC_pointer_y = 0; | |
100 | ||
101 | ||
102 | /* Define size of each thread's stack */ | |
103 | #define MIN_STACK_SIZE 3072 | |
104 | ||
105 | ||
106 | /* Messages */ | |
107 | char server_ProtocolVersion[] = "RFB 003.003\n"; | |
108 | char bad_protocol[] = "Unsupported ProtocolVersion"; | |
109 | char server_busy[] = "Server is Busy"; | |
110 | char sound_bell[] = "\2"; | |
111 | char desktop_name[] = "MBSE BrewBoard"; // Hardcoded, don't change or the VNC webclient breaks. | |
112 | ||
113 | /* Frame Buffer */ | |
114 | vnc_color_t frame_buffer[CONFIG_VNC_SERVER_FRAME_HEIGHT+1][CONFIG_VNC_SERVER_FRAME_WIDTH+1]; | |
115 | ||
116 | /* Calculate the number of tiles in the X and Y directions */ | |
117 | #if (CONFIG_VNC_SERVER_FRAME_HEIGHT % TILE_SIZE) != 0 | |
118 | #define NUM_TILES_Y_AXIS (CONFIG_VNC_SERVER_FRAME_HEIGHT/TILE_SIZE + 1) | |
119 | #define LAST_TILE_HEIGHT (CONFIG_VNC_SERVER_FRAME_HEIGHT % TILE_SIZE) | |
120 | #else | |
121 | #define NUM_TILES_Y_AXIS (CONFIG_VNC_SERVER_FRAME_HEIGHT/TILE_SIZE) | |
122 | #define LAST_TILE_HEIGHT TILE_SIZE | |
123 | #endif | |
124 | ||
125 | #if (CONFIG_VNC_SERVER_FRAME_WIDTH % TILE_SIZE) != 0 | |
126 | #define NUM_TILES_X_AXIS (CONFIG_VNC_SERVER_FRAME_WIDTH/TILE_SIZE + 1) | |
127 | #define LAST_TILE_WIDTH (CONFIG_VNC_SERVER_FRAME_WIDTH % TILE_SIZE) | |
128 | #else | |
129 | #define NUM_TILES_X_AXIS (CONFIG_VNC_SERVER_FRAME_WIDTH/TILE_SIZE) | |
130 | #define LAST_TILE_WIDTH TILE_SIZE | |
131 | #endif | |
132 | ||
133 | /* Array for marking tiles that have been updated */ | |
134 | int tile_updated[NUM_TILES_Y_AXIS+1][NUM_TILES_X_AXIS+1]; | |
135 | ||
136 | /* Conditional variable to signal that a client is connected and initialised */ | |
137 | EventGroupHandle_t xEventGroupVNC; | |
138 | const int VNC_CLIENT_UPDATE_REQ = BIT0; | |
139 | int vnc_client_sock = -1; | |
140 | bool vnc_client_connected = false; | |
141 | ||
142 | /* Variable for sounding the client's bell */ | |
143 | int SoundBellCount; | |
144 | ||
145 | /* Variables for the Websocket client */ | |
146 | bool VNC_WS_run = false; | |
147 | int VNC_WS_num = -1; | |
148 | ||
149 | ||
150 | ||
151 | /* Variable to hold the frame format details */ | |
152 | vnc_frame_format_t frame_format = {CONFIG_VNC_SERVER_FRAME_WIDTH, CONFIG_VNC_SERVER_FRAME_HEIGHT, frame_buffer, | |
153 | 1, // RGB332 server native. | |
154 | 0, // RGB555 | |
155 | 0, // RGB565 | |
156 | 0, // BGR233 | |
157 | 0, // TRUECOLOR0888 | |
158 | }; | |
159 | ||
160 | ||
161 | ||
162 | #if 0 | |
163 | void dump_msg(char *buf, uint16_t buflen, char *mode) | |
164 | { | |
165 | int i, l = 0; | |
166 | ||
167 | printf("%s %d", mode, buflen); | |
168 | for (i = 0; i < buflen; i++) { | |
169 | if ((i % 16) == 0) { | |
170 | printf("\n%02d: ", l); | |
171 | l++; | |
172 | } | |
173 | printf("%02x ", buf[i]); | |
174 | } | |
175 | printf("\n"); | |
176 | } | |
177 | #endif | |
178 | ||
179 | ||
180 | /* Structure to hold the encoding type details */ | |
181 | volatile struct encoding_type_struct | |
182 | { | |
183 | uint8_t raw; | |
184 | uint8_t copy_rectangle; | |
185 | uint8_t rre; | |
186 | uint8_t corre; | |
187 | uint8_t hextile; | |
188 | } encoding_type; | |
189 | ||
190 | ||
191 | static int GetMessageData(int, char *, int); | |
192 | static int GenTileUpdateData(uint8_t *); | |
193 | void task_VNCserver(void *); | |
194 | void task_frame(void *); | |
195 | void task_WS(void *); | |
196 | ||
197 | ||
198 | ||
199 | /** | |
200 | * @brief Serve a new VNC client. | |
201 | */ | |
202 | void vnc_netconn_serve(void); | |
203 | ||
204 | ||
205 | /* VNC startup. */ | |
206 | void VncStartup(void) | |
207 | { | |
208 | /* Initialise mutex & cond vars */ | |
209 | xEventGroupVNC = xEventGroupCreate(); | |
210 | SoundBell_lock = xSemaphoreCreateMutex(); | |
211 | ||
212 | xTaskCreate(&task_VNCserver, "VNCserver", MIN_STACK_SIZE, NULL, 5, &xTaskClientHandler); | |
213 | xTaskCreate(&task_frame, "frame_update", MIN_STACK_SIZE, NULL, 6, &xTaskFrameUpdate); | |
214 | } | |
215 | ||
216 | ||
217 | /** | |
218 | * @brief Client Handler Task. | |
219 | * | |
220 | * This task handles the client initialisation sequence. Once the client | |
221 | * is initialised this task handles all received messages from the client, | |
222 | * but does not send any data to the client. | |
223 | */ | |
224 | void task_VNCserver(void *pvParameter) | |
225 | { | |
226 | int server_sock; | |
227 | struct sockaddr_in server_addr; | |
228 | struct sockaddr_in client_addr; | |
229 | socklen_t client_addr_size; | |
230 | ||
231 | ESP_LOGI(TAG, "Starting VNC server"); | |
232 | ||
233 | /* Clear the encoding type structure */ | |
234 | encoding_type.raw = 0; | |
235 | encoding_type.copy_rectangle = 0; | |
236 | encoding_type.rre = 0; | |
237 | encoding_type.corre = 0; | |
238 | encoding_type.hextile = 0; | |
239 | ||
240 | /* Clear the sound bell counter */ | |
241 | SoundBellCount = 0; | |
242 | VNC_pointer_button = 0; | |
243 | VNC_pointer_x = VNC_pointer_y = 0; | |
244 | ||
245 | /* Create socket for incomming connections */ | |
246 | if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { | |
247 | ESP_LOGE(TAG, "socket() VNC function failed"); | |
248 | exit(1); | |
249 | } | |
250 | ||
251 | /* Construct the server address structure */ | |
252 | memset(&server_addr, 0, sizeof(server_addr)); /* Fill entire structure with 0's */ | |
253 | server_addr.sin_family = AF_INET; /* Internet address family */ | |
254 | server_addr.sin_addr.s_addr = INADDR_ANY; /* Autofill with my IP address */ | |
255 | server_addr.sin_port = htons(CONFIG_VNC_SERVER_PORT); | |
256 | ||
257 | /* Bind socket to local address */ | |
258 | if (bind(server_sock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { | |
259 | ESP_LOGE(TAG, "bind() VNC function failed"); | |
260 | exit(1); | |
261 | } | |
262 | ||
263 | /* Set the socket to listen for incoming connections */ | |
264 | if (listen(server_sock, BACKLOG) < 0) { | |
265 | ESP_LOGE(TAG, "listen() VNC function failed"); | |
266 | exit(1); | |
267 | } | |
268 | ||
269 | do { | |
270 | vnc_client_sock = (accept(server_sock, (struct sockaddr *) &client_addr, &client_addr_size)); | |
271 | if (vnc_client_sock >= 0) { | |
272 | vnc_client_connected = true; | |
273 | vnc_netconn_serve(); | |
274 | vnc_client_connected = false; | |
275 | } | |
276 | vTaskDelay((TickType_t)10); /* allows the freeRTOS scheduler to take over if needed */ | |
277 | } while (vnc_client_sock >= 0); | |
278 | } | |
279 | ||
280 | ||
281 | ||
282 | /** | |
283 | * @brief Serve a new VNC client and handle the whole session. | |
284 | */ | |
285 | void vnc_netconn_serve(void) | |
286 | { | |
287 | long int temp_long; | |
288 | char protocol_ver[8], message_buffer[MESSAGE_BUFFER_SIZE]; | |
289 | int i, j, message_len, ProtocolOkay; | |
290 | uint32_t *ptr_to_uint32; | |
291 | uint16_t *ptr_to_uint16; | |
292 | struct sockaddr_storage addr; | |
293 | socklen_t len = sizeof addr; | |
294 | char ipstr[INET6_ADDRSTRLEN]; | |
295 | ||
296 | getpeername(vnc_client_sock, (struct sockaddr*)&addr, &len); | |
297 | if (addr.ss_family == AF_INET) { | |
298 | struct sockaddr_in *s = (struct sockaddr_in *)&addr; | |
299 | inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr); | |
300 | } | |
301 | ESP_LOGI(TAG, "VNC new client %s socket %d", ipstr, vnc_client_sock); | |
302 | ||
303 | /* ProtocolVersion Handshake - begin */ | |
304 | /* Send ProtocolVersion we want to use to client */ | |
305 | message_len = sprintf(message_buffer, "RFB 003.003\n"); | |
306 | if (send(vnc_client_sock, message_buffer, message_len, 0) != message_len) { | |
307 | goto close_connection; | |
308 | } | |
309 | ||
310 | /* Receive ProtocolVersion the client wants to use */ | |
311 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), 12) == 0) { | |
312 | goto close_connection; | |
313 | } | |
314 | ||
315 | /* Check this is acceptable (RFB 003.xxx is okay) */ | |
316 | ProtocolOkay = 1; | |
317 | for (i = 0; i < 8; i++) { | |
318 | if (message_buffer[i] != server_ProtocolVersion[i]) { | |
319 | ProtocolOkay = 0; | |
320 | } | |
321 | ||
322 | /* Store the protocol version - ignoring thr 'RFB ' part */ | |
323 | protocol_ver[i] = message_buffer[i + 4]; | |
324 | } | |
325 | protocol_ver[7] = 0; | |
326 | /* ProtocolVersion Handshake - end */ | |
327 | ||
328 | /* Authentication - begin */ | |
329 | /* Send Authentication scheme to be used to client */ | |
330 | if (ProtocolOkay == 0) { | |
331 | /* ProtocolVerion is not okay */ | |
332 | ||
333 | /* Generate the Bad ProtocolVerion message */ | |
334 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
335 | *ptr_to_uint32 = htonl(0); | |
336 | ptr_to_uint32 = (uint32_t *) &(message_buffer[4]); | |
337 | *ptr_to_uint32 = htonl(strlen(bad_protocol)); | |
338 | strcpy(&(message_buffer[8]), bad_protocol); | |
339 | ||
340 | if (send(vnc_client_sock, message_buffer, 8 + strlen(bad_protocol), 0) != (8 + strlen(bad_protocol))) { | |
341 | printf("Call to send() 1 failed\n"); | |
342 | } | |
343 | goto close_connection; | |
344 | ||
345 | } else if (VNC_WS_num != -1) { | |
346 | /* Busy with a websocket client */ | |
347 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
348 | *ptr_to_uint32 = htonl(0); | |
349 | ptr_to_uint32 = (uint32_t *) &(message_buffer[4]); | |
350 | *ptr_to_uint32 = htonl(strlen(server_busy)); | |
351 | strcpy(&(message_buffer[8]), server_busy); | |
352 | ||
353 | if (send(vnc_client_sock, message_buffer, 8 + strlen(server_busy), 0) != (8 + strlen(server_busy))) { | |
354 | printf("Call to send() 1 failed\n"); | |
355 | } | |
356 | goto close_connection; | |
357 | ||
358 | } else { | |
359 | /* ProtocolVerion is okay - connect with no authentication*/ | |
360 | ||
361 | /* Generate the No Authentication message */ | |
362 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
363 | *ptr_to_uint32 = htonl((uint32_t)1); | |
364 | ||
365 | if (send(vnc_client_sock, message_buffer, 4, 0) != 4) { | |
366 | goto close_connection; | |
367 | } | |
368 | } | |
369 | /* Authentication - end */ | |
370 | ||
371 | /* ClientInitialisation - begin */ | |
372 | /* Receive initialisation message from client (1 byte) */ | |
373 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), 1) == 0) { | |
374 | goto close_connection; | |
375 | } | |
376 | /* Do nothing with this as we only support 1 Client at a time */ | |
377 | /* ClientInitialisation - end */ | |
378 | ||
379 | /* ServerInitialisation - begin */ | |
380 | ||
381 | /* Initial default RGB332 */ | |
382 | Red_Max = RED_MAX; | |
383 | Green_Max = GREEN_MAX; | |
384 | Blue_Max = BLUE_MAX; | |
385 | Red_Shift = RED_SHIFT; | |
386 | Green_Shift = GREEN_SHIFT; | |
387 | Blue_Shift = BLUE_SHIFT; | |
388 | AltPixels = false; | |
389 | ||
390 | /* Create Initialisation message for client */ | |
391 | ptr_to_uint16 = (uint16_t *) &(message_buffer[0]); | |
392 | *ptr_to_uint16 = htons((uint16_t)CONFIG_VNC_SERVER_FRAME_WIDTH); | |
393 | ||
394 | ptr_to_uint16 = (uint16_t *) &(message_buffer[2]); | |
395 | *ptr_to_uint16 = htons((uint16_t)CONFIG_VNC_SERVER_FRAME_HEIGHT); | |
396 | ||
397 | message_buffer[4] = (uint8_t)BITS_PER_PIXEL; | |
398 | message_buffer[5] = (uint8_t)PIXEL_DEPTH; | |
399 | message_buffer[6] = (uint8_t)BIG_ENDIAN_FLAG; | |
400 | message_buffer[7] = (uint8_t)TRUE_COLOUR_FLAG; | |
401 | ||
402 | ptr_to_uint16 = (uint16_t *) &(message_buffer[8]); | |
403 | *ptr_to_uint16 = htons(Red_Max); | |
404 | ||
405 | ptr_to_uint16 = (uint16_t *) &(message_buffer[10]); | |
406 | *ptr_to_uint16 = htons(Green_Max); | |
407 | ||
408 | ptr_to_uint16 = (uint16_t *) &(message_buffer[12]); | |
409 | *ptr_to_uint16 = htons(Blue_Max); | |
410 | ||
411 | message_buffer[14] = Red_Shift; | |
412 | message_buffer[15] = Green_Shift; | |
413 | message_buffer[16] = Blue_Shift; | |
414 | ||
415 | ptr_to_uint32 = (uint32_t *) &(message_buffer[20]); | |
416 | *ptr_to_uint32 = htonl(strlen(desktop_name)); | |
417 | strcpy(&(message_buffer[24]), desktop_name); | |
418 | ||
419 | if (send(vnc_client_sock, message_buffer, 24 + strlen(desktop_name), 0) != (24 + strlen(desktop_name))) { | |
420 | printf("Call to send() 3 failed\n"); | |
421 | } | |
422 | /* ServerInitialisation - end */ | |
423 | ||
424 | /* Cancel any outstanding Sound Bell requests */ | |
425 | if (xSemaphoreTake(SoundBell_lock, 10) == pdTRUE) { | |
426 | SoundBellCount = 0; | |
427 | xSemaphoreGive(SoundBell_lock); | |
428 | } | |
429 | ||
430 | ESP_LOGI(TAG, "VNC client connected (RFB Protocol Ver: %s)", protocol_ver); | |
431 | ||
432 | /* Main message handling loop */ | |
433 | while(1) { | |
434 | int num_of_encodings; | |
435 | ||
436 | /* Receive 1st byte of message from client */ | |
437 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), 1) == 0) { | |
438 | goto close_connection; /* Connection lost */ | |
439 | } | |
440 | ||
441 | switch(message_buffer[0]) { | |
442 | case SET_PIXEL_FORMAT: | |
443 | ||
444 | /* Get the remainder (19 bytes) of message */ | |
445 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 19) == 0) { | |
446 | goto close_connection; | |
447 | } | |
448 | ||
449 | /* Check pixel format is as expected */ | |
450 | i = 0; | |
451 | if (message_buffer[4] != BITS_PER_PIXEL) { | |
452 | ESP_LOGI(TAG, "SetPixelFormat client wants %d bits-per-pixel", message_buffer[4]); | |
453 | i++; | |
454 | } | |
455 | ||
456 | if (message_buffer[5] != PIXEL_DEPTH) { | |
457 | ESP_LOGI(TAG, "SetPixelFormat client wants %d pixel-depth", message_buffer[5]); | |
458 | i++; | |
459 | } | |
460 | ||
461 | if ((message_buffer[7] & 0x01) != TRUE_COLOUR_FLAG) { | |
462 | ESP_LOGI(TAG, "SetPixelFormat client wants %d true-colour-flag", message_buffer[7]); | |
463 | i++; | |
464 | } | |
465 | ||
466 | ptr_to_uint16 = (uint16_t *)&(message_buffer[8]); | |
467 | if (htons(*ptr_to_uint16) != Red_Max) { | |
468 | Red_Max = htons(*ptr_to_uint16); | |
469 | AltPixels = true; | |
470 | ESP_LOGI(TAG, "SetPixelFormat client granted %d red-max", Red_Max); | |
471 | } | |
472 | ||
473 | ptr_to_uint16 = (uint16_t *)&(message_buffer[10]); | |
474 | if (htons(*ptr_to_uint16) != Green_Max) { | |
475 | Green_Max = htons(*ptr_to_uint16); | |
476 | AltPixels = true; | |
477 | ESP_LOGI(TAG, "SetPixelFormat client granted %d green-max", Green_Max); | |
478 | } | |
479 | ||
480 | ptr_to_uint16 = (uint16_t *)&(message_buffer[12]); | |
481 | if (htons(*ptr_to_uint16) != Blue_Max) { | |
482 | Blue_Max = htons(*ptr_to_uint16); | |
483 | AltPixels = true; | |
484 | ESP_LOGI(TAG, "SetPixelFormat client granted %d blue-max", Blue_Max); | |
485 | } | |
486 | ||
487 | if (message_buffer[14] != Red_Shift) { | |
488 | Red_Shift = message_buffer[14]; | |
489 | AltPixels = true; | |
490 | ESP_LOGI(TAG, "SetPixelFormat client granted %d red-shift", Red_Shift); | |
491 | } | |
492 | ||
493 | if (message_buffer[15] != Green_Shift) { | |
494 | Green_Shift = message_buffer[15]; | |
495 | AltPixels = true; | |
496 | ESP_LOGI(TAG, "SetPixelFormat client granted %d green-shift", Green_Shift); | |
497 | } | |
498 | ||
499 | if (message_buffer[16] != Blue_Shift) { | |
500 | Blue_Shift = message_buffer[16]; | |
501 | AltPixels = true; | |
502 | ESP_LOGI(TAG, "SetPixelFormat client granted %d blue-shift", Blue_Shift); | |
503 | } | |
504 | ||
505 | if (i) { | |
506 | ESP_LOGI(TAG, "SetPixelFormat %d errors, disconnect client", i); | |
507 | ESP_LOGI(TAG, "Ensure the 'Auto select' is not enabled in your vncviewer options,"); | |
508 | goto close_connection_quietly; | |
509 | } | |
510 | ||
511 | break; | |
512 | ||
513 | case FIX_COLOUR_MAP_ENTRIES: | |
514 | printf("fix colormap entries\n"); | |
515 | /* Not supported, just get the data from the buffer and ignore it */ | |
516 | ||
517 | /* Get the next 5 bytes of message */ | |
518 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 5) == 0) { | |
519 | goto close_connection; | |
520 | } | |
521 | ||
522 | /* Calculate how many colour entries are in the buffer */ | |
523 | i = message_buffer[4]*255 + message_buffer[5]; | |
524 | i *= 6; | |
525 | ||
526 | /* Get this amount of data from the buffer */ | |
527 | for (j = 0; j < i; j++) { | |
528 | if (GetMessageData(vnc_client_sock, &(message_buffer[6]), 6) == 0) { | |
529 | goto close_connection; | |
530 | } | |
531 | } | |
532 | ||
533 | break; | |
534 | ||
535 | case SET_ENCODINGS: | |
536 | ||
537 | /* Get the next 3 bytes of message */ | |
538 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 3) == 0) { | |
539 | goto close_connection; | |
540 | } | |
541 | ||
542 | num_of_encodings = message_buffer[2]*255 + message_buffer[3]; | |
543 | ||
544 | /* Get the remainder of message */ | |
545 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), 4 * num_of_encodings) == 0) { | |
546 | goto close_connection; | |
547 | } | |
548 | ||
549 | /* Clear the encoding type structure */ | |
550 | encoding_type.raw = 0; | |
551 | encoding_type.copy_rectangle = 0; | |
552 | encoding_type.rre = 0; | |
553 | encoding_type.corre = 0; | |
554 | encoding_type.hextile = 0; | |
555 | ||
556 | for (i = 0; i < num_of_encodings; i++) { | |
557 | switch(message_buffer[3 + (i*4)]) { | |
558 | case 0: /* Raw encoding */ | |
559 | encoding_type.raw = i + 1; | |
560 | break; | |
561 | case 1: /* Copy rectangle encoding */ | |
562 | encoding_type.copy_rectangle = i + 1; | |
563 | break; | |
564 | case 2: /* RRE encoding */ | |
565 | encoding_type.rre = i + 1; | |
566 | break; | |
567 | case 4: /* CoRRE encoding */ | |
568 | encoding_type.corre = i + 1; | |
569 | break; | |
570 | case 5: /* Hextile encoding */ | |
571 | encoding_type.hextile = i + 1; | |
572 | break; | |
573 | default: /* Unknown coding type - do nothing */ | |
574 | break; | |
575 | } | |
576 | } | |
577 | ||
578 | if (encoding_type.corre) { | |
579 | ESP_LOGI(TAG, "SetEncodings use CORRE"); | |
580 | } else { | |
581 | ESP_LOGI(TAG, "SetEncodings use RAW"); | |
582 | } | |
583 | break; | |
584 | ||
585 | case FRAME_BUFFER_UPDATE_REQ: | |
586 | /* Get the remainder of message (9 bytes) */ | |
587 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 9) == 0) { | |
588 | goto close_connection; | |
589 | } | |
590 | ||
591 | if (!message_buffer[1]) { | |
592 | /* Non-incremental mode - mark the squares that need to be updated */ | |
593 | for (i = (message_buffer[2]*255 + message_buffer[3])/TILE_SIZE; | |
594 | i <= (message_buffer[2]*255 + message_buffer[3] + message_buffer[6]*255 + message_buffer[7])/TILE_SIZE; | |
595 | i++) { | |
596 | for (j = (message_buffer[4]*255 + message_buffer[5])/TILE_SIZE; | |
597 | j <= (message_buffer[4]*255 + message_buffer[5] + message_buffer[8]*255 + message_buffer[9])/TILE_SIZE; | |
598 | j++) { | |
599 | tile_updated[j][i] = 1; | |
600 | } | |
601 | } | |
602 | } | |
603 | ||
604 | /* Signal that there is now a pending update request */ | |
605 | xEventGroupSetBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
606 | break; | |
607 | ||
608 | case KEY_EVENT: | |
609 | /* Handle the key event, ignored for brewboard */ | |
610 | /* Get the remainder of message (7 bytes) */ | |
611 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 7) == 0) { | |
612 | goto close_connection; | |
613 | } | |
614 | break; | |
615 | ||
616 | case POINTER_EVENT: | |
617 | /* Handle the pointer event, simulate the touch screen. */ | |
618 | /* Get the remainder of message (5 bytes) */ | |
619 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 5) == 0) { | |
620 | goto close_connection; | |
621 | } | |
622 | /* Set global variables that will be read by another task. */ | |
623 | VNC_pointer_button = message_buffer[1]; | |
624 | VNC_pointer_x = message_buffer[2]*255 + message_buffer[3]; | |
625 | VNC_pointer_y = message_buffer[4]*255 + message_buffer[5]; | |
626 | break; | |
627 | ||
628 | case CLIENT_CUT_TEXT: | |
629 | /* Handle the client has cut text event */ | |
630 | /* Current we just get and discard the data */ | |
631 | /* Get the next 7 bytes of the message */ | |
632 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 7) == 0) { | |
633 | goto close_connection; | |
634 | } | |
635 | ||
636 | ptr_to_uint32 = (uint32_t *)&(message_buffer[4]); | |
637 | temp_long = htonl(*ptr_to_uint32); | |
638 | ||
639 | while (temp_long > 0) { | |
640 | /* Get the text in chunks MESSAGE_BUFFER_SIZE-1 characters */ | |
641 | if (temp_long > MESSAGE_BUFFER_SIZE-2) { | |
642 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), MESSAGE_BUFFER_SIZE-1) == 0) { | |
643 | goto close_connection; | |
644 | } | |
645 | ||
646 | message_buffer[MESSAGE_BUFFER_SIZE-1] = 0; | |
647 | temp_long -= (MESSAGE_BUFFER_SIZE-1); | |
648 | } else { | |
649 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), temp_long) == 0) { | |
650 | goto close_connection; | |
651 | } | |
652 | ||
653 | message_buffer[temp_long] = 0; | |
654 | temp_long = 0; | |
655 | } | |
656 | } | |
657 | ||
658 | break; | |
659 | ||
660 | default: | |
661 | ESP_LOGI(TAG, "Unknown message %d from client", message_buffer[0]); | |
662 | } | |
663 | ||
664 | vTaskDelay( (TickType_t)1); | |
665 | } | |
666 | ||
667 | close_connection: | |
668 | ESP_LOGI(TAG, "VNC client disconnected"); | |
669 | ||
670 | close_connection_quietly: | |
671 | ||
672 | /* Cancel any outstanding update requests */ | |
673 | xEventGroupClearBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
674 | close(vnc_client_sock); | |
675 | } | |
676 | ||
677 | ||
678 | ||
679 | /** | |
680 | * @brief Frame update task. This thread handles the sending of all frame | |
681 | * update data to the client and sends the 'ring bell' message to | |
682 | * the client when required. | |
683 | * Works for VNC port connected clients and Websocket clients. | |
684 | * @param pvParameter Ignored | |
685 | */ | |
686 | void task_frame(void *pvParameter) | |
687 | { | |
688 | int i, j, x_pos, y_pos, packet_length, num_updated_tiles; | |
689 | uint16_t *ptr_to_uint16; | |
690 | /* These are declared static so they don't use thread stack memory */ | |
691 | static uint8_t FramebufferUpdate_msg[4 + 12 + TILE_SIZE*TILE_SIZE*BITS_PER_PIXEL/8 + 1460]; | |
692 | static int FrameBufferPtr; /* Pointer to next space in buffer */ | |
693 | static int tile_updated_local[NUM_TILES_Y_AXIS][NUM_TILES_X_AXIS]; | |
694 | ||
695 | ESP_LOGI(TAG, "Starting VNC frame updater"); | |
696 | ||
697 | while(1) { | |
698 | /* Wait until client sends a frame update request */ | |
699 | wait_for_client: | |
700 | xEventGroupWaitBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ, pdFALSE, pdFALSE, portMAX_DELAY); | |
701 | ||
702 | /* Copy tile_updated array to local version and clear copied tiles */ | |
703 | vTaskSuspendAll(); | |
704 | num_updated_tiles = 0; | |
705 | for (i = 0; i < NUM_TILES_Y_AXIS; i++) { | |
706 | for (j = 0; j < NUM_TILES_X_AXIS; j++) { | |
707 | if (tile_updated[i][j]) { | |
708 | tile_updated_local[i][j] = 1; | |
709 | tile_updated[i][j] = 0; | |
710 | num_updated_tiles++; /* Keep count of the updated tiles */ | |
711 | } | |
712 | } | |
713 | } | |
714 | xTaskResumeAll(); | |
715 | ||
716 | if (num_updated_tiles) { | |
717 | /* Cancel update request */ | |
718 | xEventGroupClearBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
719 | /* Fill in constant parts of FramebufferUpdate Message */ | |
720 | FramebufferUpdate_msg[0] = 0; /* Message-type */ | |
721 | FramebufferUpdate_msg[1] = 0; /* Padding */ | |
722 | ptr_to_uint16 = (uint16_t *) &(FramebufferUpdate_msg[2]); | |
723 | *ptr_to_uint16 = htons(num_updated_tiles); /* Number-of-rectangles */ | |
724 | FrameBufferPtr = 4; | |
725 | ||
726 | for (y_pos = 0; y_pos < NUM_TILES_Y_AXIS; y_pos++) { | |
727 | for (x_pos = 0; x_pos < NUM_TILES_X_AXIS; x_pos++) { | |
728 | if (tile_updated_local[y_pos][x_pos]) { | |
729 | /* Send current square data to client */ | |
730 | ||
731 | /* x-position */ | |
732 | FramebufferUpdate_msg[FrameBufferPtr+0] = (x_pos * TILE_SIZE) / 256; | |
733 | FramebufferUpdate_msg[FrameBufferPtr+1] = (x_pos * TILE_SIZE) % 256; | |
734 | ||
735 | /* y-position */ | |
736 | FramebufferUpdate_msg[FrameBufferPtr+2] = (y_pos * TILE_SIZE) / 256; | |
737 | FramebufferUpdate_msg[FrameBufferPtr+3] = (y_pos * TILE_SIZE) % 256; | |
738 | ||
739 | ||
740 | /* Set width of tile in packet */ | |
741 | if (x_pos == (NUM_TILES_X_AXIS -1)) { | |
742 | /* Last tile in X-axis */ | |
743 | FramebufferUpdate_msg[FrameBufferPtr+4] = LAST_TILE_WIDTH / 256; | |
744 | FramebufferUpdate_msg[FrameBufferPtr+5] = LAST_TILE_WIDTH % 256; | |
745 | } else { | |
746 | FramebufferUpdate_msg[FrameBufferPtr+4] = TILE_SIZE / 256; | |
747 | FramebufferUpdate_msg[FrameBufferPtr+5] = TILE_SIZE % 256; | |
748 | } | |
749 | ||
750 | if (y_pos == (NUM_TILES_Y_AXIS -1)) { | |
751 | /* Last tile in Y-axis */ | |
752 | FramebufferUpdate_msg[FrameBufferPtr+6] = LAST_TILE_HEIGHT / 256; | |
753 | FramebufferUpdate_msg[FrameBufferPtr+7] = LAST_TILE_HEIGHT % 256; | |
754 | } else { | |
755 | FramebufferUpdate_msg[FrameBufferPtr+6] = TILE_SIZE / 256; | |
756 | FramebufferUpdate_msg[FrameBufferPtr+7] = TILE_SIZE % 256; | |
757 | } | |
758 | ||
759 | /* Generate the packet data for this tile */ | |
760 | packet_length = GenTileUpdateData(&(FramebufferUpdate_msg[FrameBufferPtr])); | |
761 | ||
762 | /* Send the packet data for this tile to the client */ | |
763 | FrameBufferPtr += packet_length; | |
764 | ||
765 | if (FrameBufferPtr > 1460) { | |
766 | /* Send the data to the client */ | |
767 | if (VNC_WS_num != -1) { | |
768 | ws_server_send_bin_client(VNC_WS_num, (char *)FramebufferUpdate_msg, FrameBufferPtr); | |
769 | } | |
770 | if (vnc_client_connected) { | |
771 | if (send(vnc_client_sock, FramebufferUpdate_msg, FrameBufferPtr, 0) != FrameBufferPtr) { | |
772 | goto wait_for_client; | |
773 | } | |
774 | } | |
775 | FrameBufferPtr = 0; | |
776 | } | |
777 | ||
778 | tile_updated_local[y_pos][x_pos] = 0; /* Clear the update bit for this square */ | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
783 | if (FrameBufferPtr > 0) { | |
784 | /* Last data for this update, send it to the client */ | |
785 | if (VNC_WS_num != -1) { | |
786 | ws_server_send_bin_client(VNC_WS_num, (char *)FramebufferUpdate_msg, FrameBufferPtr); | |
787 | } | |
788 | if (vnc_client_connected) { | |
789 | if (send(vnc_client_sock, FramebufferUpdate_msg, FrameBufferPtr, 0) != FrameBufferPtr) { | |
790 | goto wait_for_client; | |
791 | } | |
792 | } | |
793 | ||
794 | FrameBufferPtr = 0; | |
795 | } | |
796 | ||
797 | } else { /* if (num_updated_tiles) */ | |
798 | /* There was no new display data to send to the client */ | |
799 | /* Sleep for 1/20th second before checking again */ | |
800 | vTaskDelay(20 / portTICK_PERIOD_MS); | |
801 | } | |
802 | ||
803 | /* Check for sound bell event */ | |
804 | if (xSemaphoreTake(SoundBell_lock, 10) == pdTRUE) { | |
805 | if (SoundBellCount) { | |
806 | --SoundBellCount; | |
807 | xSemaphoreGive(SoundBell_lock); | |
808 | ||
809 | if (vnc_client_connected) { | |
810 | if (send(vnc_client_sock, sound_bell, 1, 0) != 1) { | |
811 | goto wait_for_client; | |
812 | } | |
813 | } | |
814 | if (VNC_WS_num != -1) { | |
815 | ws_server_send_bin_client(VNC_WS_num, sound_bell, 1); | |
816 | } | |
817 | } else { | |
818 | xSemaphoreGive(SoundBell_lock); | |
819 | } | |
820 | } | |
821 | } | |
822 | } | |
823 | ||
824 | ||
825 | ||
826 | /** | |
827 | * Convert a framebuffer pixel to BGR233 if needed. | |
828 | * @param pixel The 8 bit pixel value | |
829 | * @return Then unchanged or changed pixel. | |
830 | */ | |
831 | vnc_color_t IRAM_ATTR PixelConvert(vnc_color_t pixel) | |
832 | { | |
833 | if (!AltPixels) | |
834 | return pixel; | |
835 | ||
836 | return (((pixel >> RED_SHIFT) & RED_MAX) << Red_Shift) | | |
837 | (((pixel >> GREEN_SHIFT) & GREEN_MAX) << Green_Shift) | | |
838 | (((pixel >> BLUE_SHIFT) & BLUE_MAX) << Blue_Shift); | |
839 | } | |
840 | ||
841 | ||
842 | ||
843 | /** | |
844 | * @brief Generate tile update data function | |
845 | * | |
846 | * This function is called by the frame_update thread to generate the message | |
847 | * data for a tile to send to the client. This function expects the | |
848 | * x-position, y-position, width and height fields of the buffer to be filled | |
849 | * in before it is called. | |
850 | * | |
851 | * The format of the buffer is: | |
852 | * packet_buffer[0:1] - x-position of tile | |
853 | * packet_buffer[2:3] - y-position of tile | |
854 | * packet_buffer[4:5] - width of tile | |
855 | * packet_buffer[6:7] - height of tile | |
856 | * packet_buffer[8 onwards] - Pixel data for the tile | |
857 | * | |
858 | * The pixel data will be encoded with CoRRE encoding (if the CDL option is | |
859 | * enabled and the client can handle it) or RAW encoding if that is smaller | |
860 | * than CoRRE encoding for that particular tile. | |
861 | * | |
862 | * @param packet_buffer Buffer to store tile data | |
863 | * @return Length of generated data in bytes | |
864 | */ | |
865 | static int GenTileUpdateData(uint8_t *packet_buffer) | |
866 | { | |
867 | uint16_t x_pos, y_pos; | |
868 | int i, j; | |
869 | int tile_width, tile_height; | |
870 | int packet_length; | |
871 | static vnc_color_t tile_buffer[TILE_SIZE][TILE_SIZE]; /* Buffer to hold tile to be encoded */ | |
872 | vnc_color_t pixel_colour; | |
873 | vnc_color_t bg_colour; | |
874 | int no_of_subrects, subrect_width, subrect_height; | |
875 | int k, l; | |
876 | ||
877 | no_of_subrects = 0; /* Set to no sub-rectangles to start with */ | |
878 | packet_length = 20-4+(Bits_Per_Pixel/8); /* Set to minimum packet length to start with */ | |
879 | ||
880 | /* Get the X and Y positions of this tile from the packet buffer */ | |
881 | x_pos = packet_buffer[0] * 256 + packet_buffer[1]; | |
882 | y_pos = packet_buffer[2] * 256 + packet_buffer[3]; | |
883 | ||
884 | /* Get the tile width and height from the packet buffer */ | |
885 | tile_width = packet_buffer[4] * 256 + packet_buffer[5]; | |
886 | tile_height = packet_buffer[6] * 256 + packet_buffer[7]; | |
887 | ||
888 | /* Set the encoding type to RRE */ | |
889 | if (!encoding_type.corre) { | |
890 | /* CoRRE encoding is not supported - just use raw encoding */ | |
891 | goto use_raw_encoding; | |
892 | } | |
893 | ||
894 | /* Set encoding type to CoRRE encoding in packet buffer */ | |
895 | packet_buffer[8+0] = 0; | |
896 | packet_buffer[8+1] = 0; | |
897 | packet_buffer[8+2] = 0; | |
898 | packet_buffer[8+3] = 4; | |
899 | ||
900 | /* Copy tile from the main frame buffer to the local tile buffer */ | |
901 | for (i = 0; i < tile_height; i++) { | |
902 | for (j = 0; j < tile_width; j++) { | |
903 | tile_buffer[i][j] = frame_buffer[y_pos + i][x_pos + j]; | |
904 | } | |
905 | } | |
906 | ||
907 | /* Find the background colour */ | |
908 | /* We just assume the (0, 0) pixel in the tile is the bgcolour */ | |
909 | /* Its quick!!! */ | |
910 | bg_colour = frame_buffer[y_pos][x_pos]; | |
911 | ||
912 | /* Set the background colour in the packet buffer */ | |
913 | if (Bits_Per_Pixel == 8) { | |
914 | packet_buffer[16] = PixelConvert(bg_colour); /* (vnc_color_t) bg_colour; */ | |
915 | } else { | |
916 | packet_buffer[16] = COLOUR2BYTE0(bg_colour); | |
917 | packet_buffer[16+1] = COLOUR2BYTE1(bg_colour); | |
918 | } | |
919 | ||
920 | #ifdef CYGNUM_VNC_SERVER_CORRE_ENCODING_HACK | |
921 | /* Add an initial sub-rectangle to paint the background the background colour */ | |
922 | /* This is required because of a known bug in the VNC viewer (x86 version) */ | |
923 | //#if BITS_PER_PIXEL == 8 | |
924 | packet_buffer[packet_length] = (vnc_color_t) bg_colour; | |
925 | packet_length++; | |
926 | //#endif | |
927 | //#if BITS_PER_PIXEL == 16 | |
928 | // packet_buffer[packet_length] = packet_buffer[16]; | |
929 | // packet_buffer[packet_length+1] = packet_buffer[16+1]; | |
930 | // packet_length += 2; | |
931 | //#endif | |
932 | packet_buffer[packet_length] = (uint8_t) 0; /* Sub-rect x-pos */ | |
933 | packet_buffer[packet_length+1] = (uint8_t) 0; /* Sub-rect y-pos*/ | |
934 | packet_buffer[packet_length+2] = (uint8_t) tile_width; /* Sub-rect width*/ | |
935 | packet_buffer[packet_length+3] = (uint8_t) tile_height; /* Sub-rect height*/ | |
936 | packet_length += 4; | |
937 | no_of_subrects++; /* Increment sub-rectangle count */ | |
938 | #endif | |
939 | ||
940 | /* Scan trough tile and find sub-rectangles */ | |
941 | for (i = 0; i < tile_height; i++) { | |
942 | for (j = 0; j < tile_width; j++) { | |
943 | if (tile_buffer[i][j] != bg_colour) { | |
944 | /* This is a non-background pixel */ | |
945 | subrect_width = 1; | |
946 | pixel_colour = tile_buffer[i][j]; | |
947 | ||
948 | /* Extend the sub-rectangle to its maximum width */ | |
949 | for (subrect_width = 1; subrect_width <= tile_width-j-1; subrect_width++) { | |
950 | if (tile_buffer[i][j+subrect_width] != pixel_colour) { | |
951 | goto got_subrect_width; | |
952 | } | |
953 | } | |
954 | ||
955 | got_subrect_width: | |
956 | ||
957 | /* Extend the sub-rectangle to its maximum height */ | |
958 | for (subrect_height=1; subrect_height <= tile_height-i-1; subrect_height++) { | |
959 | for (k = j; k < j+subrect_width; k++) { | |
960 | if (tile_buffer[i+subrect_height][k] != pixel_colour) { | |
961 | goto got_subrect_height; | |
962 | } | |
963 | } | |
964 | } | |
965 | ||
966 | got_subrect_height: | |
967 | ||
968 | /* Delete the pixels for the sub-rectangle from the sub-rectangle */ | |
969 | for (k = i; k < i+subrect_height; k++) { | |
970 | for (l = j; l < j+subrect_width; l++) { | |
971 | tile_buffer[k][l] = bg_colour; | |
972 | } | |
973 | } | |
974 | ||
975 | /* Append new sub-rectangle data to the packet buffer */ | |
976 | if (Bits_Per_Pixel == 8) { | |
977 | packet_buffer[packet_length] = PixelConvert(pixel_colour); // (vnc_color_t) pixel_colour; | |
978 | packet_length++; | |
979 | } else { | |
980 | packet_buffer[packet_length] = COLOUR2BYTE0(pixel_colour); | |
981 | packet_buffer[packet_length+1] = COLOUR2BYTE1(pixel_colour); | |
982 | packet_length += 2; | |
983 | } | |
984 | ||
985 | packet_buffer[packet_length] = (uint8_t) j; /* Sub-rect x-pos */ | |
986 | packet_length++; | |
987 | ||
988 | packet_buffer[packet_length] = (uint8_t) i; /* Sub-rect y-pos*/ | |
989 | packet_length++; | |
990 | ||
991 | packet_buffer[packet_length] = (uint8_t) subrect_width; /* Sub-rect width*/ | |
992 | packet_length++; | |
993 | ||
994 | packet_buffer[packet_length] = (uint8_t) subrect_height; /* Sub-rect height*/ | |
995 | packet_length++; | |
996 | ||
997 | no_of_subrects++; /* Increment sub-rectangle count */ | |
998 | ||
999 | if (packet_length >= 12 + tile_height*tile_width*(BITS_PER_PIXEL/8) - 6) { | |
1000 | /* The next sub-rectangle will make the packet size */ | |
1001 | /* larger than a rew encoded packet - so just use raw */ | |
1002 | goto use_raw_encoding; | |
1003 | } | |
1004 | } | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | /* Fill in no_of_sub-rectangles field in packet buffer */ | |
1009 | packet_buffer[12+0] = 0; | |
1010 | packet_buffer[12+1] = 0; | |
1011 | packet_buffer[12+2] = no_of_subrects / 256; | |
1012 | packet_buffer[12+3] = no_of_subrects % 256; | |
1013 | ||
1014 | /* CoRRE data encoding for tile complete */ | |
1015 | return packet_length; | |
1016 | ||
1017 | use_raw_encoding: | |
1018 | ||
1019 | /* Create packet data using RAW encoding */ | |
1020 | for (i = 0; i < tile_height; i++) { | |
1021 | for (j = 0; j < tile_width; j++) { | |
1022 | if (Bits_Per_Pixel == 8) { | |
1023 | packet_buffer[12 + tile_width * i + j] = PixelConvert(frame_buffer[y_pos + i][x_pos + j]); | |
1024 | } else { | |
1025 | packet_buffer[12 + 2 * tile_width * i + 2*j] = COLOUR2BYTE0(frame_buffer[y_pos + i][x_pos + j]); | |
1026 | packet_buffer[12 + 2 * tile_width * i + 2*j+ 1] = COLOUR2BYTE1(frame_buffer[y_pos + i][x_pos + j]); | |
1027 | } | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | /* Set the encoding type to raw */ | |
1032 | packet_buffer[8+0] = 0; | |
1033 | packet_buffer[8+1] = 0; | |
1034 | packet_buffer[8+2] = 0; | |
1035 | packet_buffer[8+3] = 0; | |
1036 | ||
1037 | return (12 + tile_width*tile_height*(Bits_Per_Pixel/8)); | |
1038 | } | |
1039 | ||
1040 | ||
1041 | ||
1042 | /** | |
1043 | * @brief Get message data function | |
1044 | * This function is called by the client_handler thread to get data | |
1045 | * from the client's socket. | |
1046 | * | |
1047 | * @param socket_fd File descriptor of the socket to get the data from. | |
1048 | * @param *buffer Buffer to store received data in. | |
1049 | * @param num_bytes Number of bytes to attempt to get. | |
1050 | * | |
1051 | * @return 1 on sucessfull completion - 0 on error. | |
1052 | */ | |
1053 | static int GetMessageData(int socket_fd, char *buffer, int num_bytes) | |
1054 | { | |
1055 | int bytes_rxd; | |
1056 | int message_len = 0; | |
1057 | ||
1058 | while (message_len < num_bytes) { | |
1059 | if ((bytes_rxd = recv(socket_fd, buffer, num_bytes, 0)) <= 0) { | |
1060 | return 0; | |
1061 | } | |
1062 | message_len += bytes_rxd; | |
1063 | } | |
1064 | ||
1065 | return 1; | |
1066 | } | |
1067 | ||
1068 | ||
1069 | ||
1070 | void VncCls(vnc_color_t color) | |
1071 | { | |
1072 | /* Clear the frame buffer */ | |
1073 | int i, j; | |
1074 | ||
1075 | for (i = 0; i < CONFIG_VNC_SERVER_FRAME_HEIGHT; i++) { | |
1076 | for (j = 0; j < CONFIG_VNC_SERVER_FRAME_WIDTH; j++) { | |
1077 | VncDrawPixel(j, i, color); | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | ||
1083 | ||
1084 | void IRAM_ATTR VncDrawPixel(uint16_t x, uint16_t y, vnc_color_t color) | |
1085 | { | |
1086 | if (x >= CONFIG_VNC_SERVER_FRAME_WIDTH || y >= CONFIG_VNC_SERVER_FRAME_HEIGHT) { | |
1087 | printf("write_frame(%d, %d) tile %d/%d\n", x, y, x/TILE_SIZE, y/TILE_SIZE); | |
1088 | return; | |
1089 | } | |
1090 | ||
1091 | /* Set that pixel to 'colour' */ | |
1092 | frame_buffer[y][x] = color; | |
1093 | ||
1094 | /* Mark the tile for update */ | |
1095 | tile_updated[y/TILE_SIZE][x/TILE_SIZE] = 1; | |
1096 | } | |
1097 | ||
1098 | ||
1099 | ||
1100 | void IRAM_ATTR VncDrawHorzLine(uint16_t x1, uint16_t x2, uint16_t y, vnc_color_t color) | |
1101 | { | |
1102 | int i; | |
1103 | ||
1104 | /* Draw the line */ | |
1105 | for (i = x1; i <= x2; i++) { | |
1106 | VncDrawPixel(i, y, color); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | ||
1111 | ||
1112 | void IRAM_ATTR VncDrawVertLine(uint16_t x, uint16_t y1, uint16_t y2, vnc_color_t color) | |
1113 | { | |
1114 | int i; | |
1115 | ||
1116 | /* Draw the line */ | |
1117 | for (i = y1; i <= y2; i++) { | |
1118 | VncDrawPixel(x, i, color); | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | ||
1123 | ||
1124 | void IRAM_ATTR VncFillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, vnc_color_t color) | |
1125 | { | |
1126 | /* Draw a solid rectangle */ | |
1127 | int i, j; | |
1128 | ||
1129 | for (i = y1; i <= y2; i++) { | |
1130 | for (j = x1; j <= x2; j++) { | |
1131 | VncDrawPixel(j, i, color); | |
1132 | } | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | ||
1137 | ||
1138 | void VncSoundBell(void) | |
1139 | { | |
1140 | if (xSemaphoreTake(SoundBell_lock, 10) == pdTRUE) { | |
1141 | SoundBellCount++; | |
1142 | xSemaphoreGive(SoundBell_lock); | |
1143 | } | |
1144 | } | |
1145 | ||
1146 | ||
1147 | ||
1148 | /** | |
1149 | * @brief Websocket task. Created on a websocket connection. | |
1150 | */ | |
1151 | void task_WS(void *pvParameter) | |
1152 | { | |
1153 | int i, j, num_of_encodings, rest_time; | |
1154 | uint16_t *ptr_to_uint16; | |
1155 | struct strMessage _msg; | |
1156 | ||
1157 | // Do pin/pong. | |
1158 | ||
1159 | VNC_WS_run = true; | |
1160 | rest_time = 0; | |
1161 | ||
1162 | while (VNC_WS_run) { | |
1163 | ||
1164 | /* | |
1165 | * Wait until something is received from the client. Exit the queue receiver | |
1166 | * each 5 milliseconds to allow to evaluate the VNC_WS_run flag and to allow | |
1167 | * a clean exit of this task. | |
1168 | */ | |
1169 | if (xQueueReceive(message_queue, &_msg, 5 / portTICK_PERIOD_MS) == pdTRUE) { | |
1170 | // dump_msg((char *)_msg.message, _msg.length, "pull"); | |
1171 | ||
1172 | switch (_msg.message[0]) { | |
1173 | ||
1174 | case SET_PIXEL_FORMAT: | |
1175 | // printf("SET_PIXEL_FORMAT\n"); | |
1176 | /* Check total message length */ | |
1177 | if (_msg.length >= 20) { | |
1178 | /* Check pixel format is as expected */ | |
1179 | i = 0; | |
1180 | if (_msg.message[4] != BITS_PER_PIXEL) { | |
1181 | ESP_LOGI(TAG, "SetPixelFormat client wants %d bits-per-pixel", _msg.message[4]); | |
1182 | i++; | |
1183 | } | |
1184 | ||
1185 | if (_msg.message[5] != PIXEL_DEPTH) { | |
1186 | ESP_LOGI(TAG, "SetPixelFormat client wants %d pixel-depth", _msg.message[5]); | |
1187 | i++; | |
1188 | } | |
1189 | ||
1190 | if ((_msg.message[7] & 0x01) != TRUE_COLOUR_FLAG) { | |
1191 | ESP_LOGI(TAG, "SetPixelFormat client wants %d true-colour-flag", _msg.message[7]); | |
1192 | i++; | |
1193 | } | |
1194 | ||
1195 | ptr_to_uint16 = (uint16_t *)&(_msg.message[8]); | |
1196 | if (htons(*ptr_to_uint16) != Red_Max) { | |
1197 | Red_Max = htons(*ptr_to_uint16); | |
1198 | AltPixels = true; | |
1199 | ESP_LOGI(TAG, "SetPixelFormat client granted %d red-max", Red_Max); | |
1200 | } | |
1201 | ||
1202 | ptr_to_uint16 = (uint16_t *)&(_msg.message[10]); | |
1203 | if (htons(*ptr_to_uint16) != Green_Max) { | |
1204 | Green_Max = htons(*ptr_to_uint16); | |
1205 | AltPixels = true; | |
1206 | ESP_LOGI(TAG, "SetPixelFormat client granted %d green-max", Green_Max); | |
1207 | } | |
1208 | ||
1209 | ptr_to_uint16 = (uint16_t *)&(_msg.message[12]); | |
1210 | if (htons(*ptr_to_uint16) != Blue_Max) { | |
1211 | Blue_Max = htons(*ptr_to_uint16); | |
1212 | AltPixels = true; | |
1213 | ESP_LOGI(TAG, "SetPixelFormat client granted %d blue-max", Blue_Max); | |
1214 | } | |
1215 | ||
1216 | if (_msg.message[14] != Red_Shift) { | |
1217 | Red_Shift = _msg.message[14]; | |
1218 | AltPixels = true; | |
1219 | ESP_LOGI(TAG, "SetPixelFormat client granted %d red-shift", Red_Shift); | |
1220 | } | |
1221 | ||
1222 | if (_msg.message[15] != Green_Shift) { | |
1223 | Green_Shift = _msg.message[15]; | |
1224 | AltPixels = true; | |
1225 | ESP_LOGI(TAG, "SetPixelFormat client granted %d green-shift", Green_Shift); | |
1226 | } | |
1227 | ||
1228 | if (_msg.message[16] != Blue_Shift) { | |
1229 | Blue_Shift = _msg.message[16]; | |
1230 | AltPixels = true; | |
1231 | ESP_LOGI(TAG, "SetPixelFormat client granted %d blue-shift", Blue_Shift); | |
1232 | } | |
1233 | ||
1234 | if (i) { | |
1235 | ESP_LOGI(TAG, "SetPixelFormat %d errors, disconnect client", i); | |
1236 | ESP_LOGI(TAG, "Ensure the 'Auto select' is not enabled in your vncviewer options,"); | |
1237 | _msg.length = 0; | |
1238 | ws_server_remove_client(VNC_WS_num); | |
1239 | } | |
1240 | } | |
1241 | break; | |
1242 | ||
1243 | case FIX_COLOUR_MAP_ENTRIES: | |
1244 | printf("FIX_COLOUR_MAP_ENTRIES\n"); | |
1245 | break; | |
1246 | ||
1247 | case SET_ENCODINGS: | |
1248 | if (_msg.length >= 3) { | |
1249 | // Get the total encodings | |
1250 | num_of_encodings = _msg.message[2]*255 + _msg.message[3]; | |
1251 | ||
1252 | /* Clear the encoding type structure */ | |
1253 | encoding_type.raw = 0; | |
1254 | encoding_type.copy_rectangle = 0; | |
1255 | encoding_type.rre = 0; | |
1256 | encoding_type.corre = 0; | |
1257 | encoding_type.hextile = 0; | |
1258 | ||
1259 | for (i = 0; i < num_of_encodings; i++) { | |
1260 | switch(_msg.message[3 + (i*4)]) { | |
1261 | case 0: /* Raw encoding */ | |
1262 | encoding_type.raw = i + 1; | |
1263 | break; | |
1264 | case 1: /* Copy rectangle encoding */ | |
1265 | encoding_type.copy_rectangle = i + 1; | |
1266 | break; | |
1267 | case 2: /* RRE encoding */ | |
1268 | encoding_type.rre = i + 1; | |
1269 | break; | |
1270 | case 4: /* CoRRE encoding */ | |
1271 | encoding_type.corre = i + 1; | |
1272 | break; | |
1273 | case 5: /* Hextile encoding */ | |
1274 | encoding_type.hextile = i + 1; | |
1275 | break; | |
1276 | default: /* Unknown coding type - do nothing */ | |
1277 | break; | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | if (encoding_type.corre) { | |
1282 | ESP_LOGI(TAG, "SetEncodings use CORRE"); | |
1283 | } else { | |
1284 | ESP_LOGI(TAG, "SetEncodings use RAW"); | |
1285 | } | |
1286 | } | |
1287 | break; | |
1288 | ||
1289 | case FRAME_BUFFER_UPDATE_REQ: | |
1290 | if (_msg.length == 10) { | |
1291 | if (!_msg.message[1]) { | |
1292 | /* Non-incremental mode - mark the squares that need to be updated */ | |
1293 | for (i = (_msg.message[2]*255 + _msg.message[3])/TILE_SIZE; | |
1294 | i <= (_msg.message[2]*255 + _msg.message[3] + _msg.message[6]*255 + _msg.message[7])/TILE_SIZE; | |
1295 | i++) { | |
1296 | for (j = (_msg.message[4]*255 + _msg.message[5])/TILE_SIZE; | |
1297 | j <= (_msg.message[4]*255 + _msg.message[5] + _msg.message[8]*255 + _msg.message[9])/TILE_SIZE; | |
1298 | j++) { | |
1299 | tile_updated[j][i] = 1; | |
1300 | } | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | /* Signal that there is now a pending update request */ | |
1305 | xEventGroupSetBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
1306 | } | |
1307 | break; | |
1308 | ||
1309 | case KEY_EVENT: | |
1310 | printf("KEY_EVENT\n"); | |
1311 | /* Handle the key event, ignored for brewboard */ | |
1312 | /* Get the remainder of message (7 bytes) */ | |
1313 | break; | |
1314 | ||
1315 | case POINTER_EVENT: | |
1316 | /* Handle the pointer event, simulate the touch screen. */ | |
1317 | if (_msg.length == 6) { | |
1318 | /* Set global variables that will be read by another task. */ | |
1319 | VNC_pointer_button = _msg.message[1]; | |
1320 | VNC_pointer_x = _msg.message[2]*255 + _msg.message[3]; | |
1321 | VNC_pointer_y = _msg.message[4]*255 + _msg.message[5]; | |
1322 | // printf("POINTER_EVENT button=%02x x=%d y=%d\n", VNC_pointer_button, VNC_pointer_x, VNC_pointer_y); | |
1323 | } | |
1324 | _msg.length = 0; | |
1325 | break; | |
1326 | ||
1327 | case CLIENT_CUT_TEXT: | |
1328 | printf("CLIENT_CUT_TEXT\n"); | |
1329 | break; | |
1330 | ||
1331 | default: | |
1332 | ESP_LOGI(TAG, "Unknown message %d from client", _msg.message[0]); | |
1333 | } | |
1334 | rest_time = 0; | |
1335 | } else { | |
1336 | rest_time++; // Each 5 milliseconds | |
1337 | if (rest_time == 2000) { | |
1338 | // 10 seconds silence, send a PING | |
1339 | rest_time = 0; | |
1340 | // int rc = ws_server_ping(VNC_WS_num); | |
1341 | // ESP_LOGI(TAG, "Send PING to %d, rc=%d", VNC_WS_num, rc); | |
1342 | } | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | VNC_WS_num = -1; | |
1347 | xEventGroupClearBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
1348 | ESP_LOGI(TAG, "task_WS finished"); | |
1349 | vTaskDelete(NULL); | |
1350 | } | |
1351 | ||
1352 | ||
1353 | ||
1354 | int VncStartWS(int num) | |
1355 | { | |
1356 | char protocol_ver[8], message_buffer[MESSAGE_BUFFER_SIZE]; | |
1357 | int i, ProtocolOkay; | |
1358 | uint32_t *ptr_to_uint32; | |
1359 | uint16_t *ptr_to_uint16; | |
1360 | struct strMessage _msg; | |
1361 | ||
1362 | ESP_LOGI(TAG, "Start VNC WebSocket connection %d", num); | |
1363 | message_queue = xQueueCreate(message_queue_size, sizeof(struct strMessage)); | |
1364 | ||
1365 | /* | |
1366 | * Initial handshake | |
1367 | */ | |
1368 | ws_server_send_bin_client(num, "RFB 003.003\n", 12); | |
1369 | ProtocolOkay = 1; | |
1370 | if (xQueueReceive(message_queue, &_msg, 5000 / portTICK_PERIOD_MS) == pdTRUE) { | |
1371 | // dump_msg((char *)_msg.message, _msg.length, "pull"); | |
1372 | ||
1373 | for (i = 0; i < 8; i++) { | |
1374 | if (_msg.message[i] != server_ProtocolVersion[i]) { | |
1375 | ProtocolOkay = 0; | |
1376 | } | |
1377 | /* Store the protocol version - ignoring thr 'RFB ' part */ | |
1378 | protocol_ver[i] = _msg.message[i + 4]; | |
1379 | } | |
1380 | protocol_ver[7] = 0; | |
1381 | } else { | |
1382 | ESP_LOGE(TAG, "Client timeout after initial message"); | |
1383 | return -5; | |
1384 | } | |
1385 | ||
1386 | if (ProtocolOkay == 0) { | |
1387 | /* Generate the Bad ProtocolVerion message */ | |
1388 | ESP_LOGI(TAG, "Start VNC WebSocket task failed, bad protocol."); | |
1389 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
1390 | *ptr_to_uint32 = htonl(0); | |
1391 | ptr_to_uint32 = (uint32_t *) &(message_buffer[4]); | |
1392 | *ptr_to_uint32 = htonl(strlen(bad_protocol)); | |
1393 | strcpy(&(message_buffer[8]), bad_protocol); | |
1394 | ws_server_send_bin_client(num, message_buffer, 8 + strlen(bad_protocol)); | |
1395 | vTaskDelay(500 / portTICK_PERIOD_MS); | |
1396 | ws_server_remove_client(num); | |
1397 | return -2; | |
1398 | } | |
1399 | /* ProtocolVerion is okay - connect with no authentication*/ | |
1400 | ||
1401 | /* Server is busy with another client */ | |
1402 | if (VNC_WS_run || vnc_client_connected) { | |
1403 | ESP_LOGI(TAG, "Start VNC WebSocket task failed, server busy."); | |
1404 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
1405 | *ptr_to_uint32 = htonl(0); | |
1406 | ptr_to_uint32 = (uint32_t *) &(message_buffer[4]); | |
1407 | *ptr_to_uint32 = htonl(strlen(server_busy)); | |
1408 | strcpy(&(message_buffer[8]), server_busy); | |
1409 | ws_server_send_bin_client(num, message_buffer, 8 + strlen(server_busy)); | |
1410 | vTaskDelay(500 / portTICK_PERIOD_MS); | |
1411 | ws_server_remove_client(num); | |
1412 | return -1; | |
1413 | } | |
1414 | ||
1415 | /* Generate the No Authentication message */ | |
1416 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
1417 | *ptr_to_uint32 = htonl((uint32_t)1); | |
1418 | ws_server_send_bin_client(num, message_buffer, 4); | |
1419 | /* Authentication - end */ | |
1420 | ||
1421 | /* ClientInitialisation - begin */ | |
1422 | /* Receive initialisation message from client (1 byte) */ | |
1423 | if (xQueueReceive(message_queue, &_msg, 5000 / portTICK_PERIOD_MS) == pdFALSE) { | |
1424 | ESP_LOGE(TAG, "Client timeout after auth message"); | |
1425 | return -5; | |
1426 | } | |
1427 | /* Do nothing with this as we only support 1 Client at a time */ | |
1428 | /* ClientInitialisation - end */ | |
1429 | ||
1430 | /* Initial default RGB332 */ | |
1431 | Red_Max = RED_MAX; | |
1432 | Green_Max = GREEN_MAX; | |
1433 | Blue_Max = BLUE_MAX; | |
1434 | Red_Shift = RED_SHIFT; | |
1435 | Green_Shift = GREEN_SHIFT; | |
1436 | Blue_Shift = BLUE_SHIFT; | |
1437 | AltPixels = false; | |
1438 | ||
1439 | /* ServerInitialisation - begin */ | |
1440 | /* Create Initialisation message for client */ | |
1441 | ptr_to_uint16 = (uint16_t *) &(message_buffer[0]); | |
1442 | *ptr_to_uint16 = htons((uint16_t)CONFIG_VNC_SERVER_FRAME_WIDTH); | |
1443 | ||
1444 | ptr_to_uint16 = (uint16_t *) &(message_buffer[2]); | |
1445 | *ptr_to_uint16 = htons((uint16_t)CONFIG_VNC_SERVER_FRAME_HEIGHT); | |
1446 | ||
1447 | message_buffer[4] = (uint8_t)BITS_PER_PIXEL; | |
1448 | message_buffer[5] = (uint8_t)PIXEL_DEPTH; | |
1449 | message_buffer[6] = (uint8_t)BIG_ENDIAN_FLAG; | |
1450 | message_buffer[7] = (uint8_t)TRUE_COLOUR_FLAG; | |
1451 | ||
1452 | ptr_to_uint16 = (uint16_t *) &(message_buffer[8]); | |
1453 | *ptr_to_uint16 = htons(Red_Max); | |
1454 | ||
1455 | ptr_to_uint16 = (uint16_t *) &(message_buffer[10]); | |
1456 | *ptr_to_uint16 = htons(Green_Max); | |
1457 | ||
1458 | ptr_to_uint16 = (uint16_t *) &(message_buffer[12]); | |
1459 | *ptr_to_uint16 = htons(Blue_Max); | |
1460 | ||
1461 | message_buffer[14] = Red_Shift; | |
1462 | message_buffer[15] = Green_Shift; | |
1463 | message_buffer[16] = Blue_Shift; | |
1464 | ||
1465 | ptr_to_uint32 = (uint32_t *) &(message_buffer[20]); | |
1466 | *ptr_to_uint32 = htonl(strlen(desktop_name)); | |
1467 | strcpy(&(message_buffer[24]), desktop_name); | |
1468 | ||
1469 | // dump_msg(message_buffer, 24 + strlen(desktop_name), (char *)"send"); | |
1470 | ws_server_send_bin_client(num, message_buffer, 24 + strlen(desktop_name)); | |
1471 | ||
1472 | /* ServerInitialisation - end */ | |
1473 | ESP_LOGI(TAG, "VNC WebSocket client connected (RFB Protocol Ver: %s)", protocol_ver); | |
1474 | ||
1475 | VNC_WS_num = num; | |
1476 | xTaskCreate(&task_WS, "WSclient", MIN_STACK_SIZE, NULL, 5, &xTaskWSclient); | |
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | ||
1481 | ||
1482 | void VncStopWS(int num) | |
1483 | { | |
1484 | if (! VNC_WS_run) { | |
1485 | ESP_LOGI(TAG, "Stop VNC WebSocket, not running."); | |
1486 | return; | |
1487 | } | |
1488 | ||
1489 | if (num != VNC_WS_num) { | |
1490 | ESP_LOGI(TAG, "Stop VNC WebSocket, %d is running, requested %d", VNC_WS_num, num); | |
1491 | return; | |
1492 | } | |
1493 | ||
1494 | ESP_LOGI(TAG, "Stop VNC WebSocket task connection %d", num); | |
1495 | VNC_WS_run = false; | |
1496 | xQueueReset(message_queue); | |
1497 | } | |
1498 | ||
1499 | ||
1500 | ||
1501 | void VncGetWSmessage(char *msg, uint16_t len) | |
1502 | { | |
1503 | int max; | |
1504 | struct strMessage _msg; | |
1505 | ||
1506 | if (len == 0) | |
1507 | return; | |
1508 | ||
1509 | max = len; | |
1510 | if (max > MESSAGE_BUFFER_SIZE) { | |
1511 | ESP_LOGE(TAG, "VncGetWSmessage need %d bytes in a %d length buffer", len, MESSAGE_BUFFER_SIZE); | |
1512 | max = MESSAGE_BUFFER_SIZE; | |
1513 | } | |
1514 | ||
1515 | for (int i = 0; i < max; i++) { | |
1516 | _msg.message[i] = msg[i]; | |
1517 | } | |
1518 | _msg.length = max; | |
1519 | if (xQueueSendToBack(message_queue, &_msg, 200 / portTICK_PERIOD_MS) == pdFALSE) { | |
1520 | ESP_LOGE(TAG, "VncGetWSmessage() Queue full, message lost"); | |
1521 | } | |
1522 | } | |
1523 |