Tue, 23 Oct 2018 21:21:13 +0200
Added files for /spiffs internet updates.
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 | break; | |
578 | ||
579 | case FRAME_BUFFER_UPDATE_REQ: | |
580 | /* Get the remainder of message (9 bytes) */ | |
581 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 9) == 0) { | |
582 | goto close_connection; | |
583 | } | |
584 | ||
585 | if (!message_buffer[1]) { | |
586 | /* Non-incremental mode - mark the squares that need to be updated */ | |
587 | for (i = (message_buffer[2]*255 + message_buffer[3])/TILE_SIZE; | |
588 | i <= (message_buffer[2]*255 + message_buffer[3] + message_buffer[6]*255 + message_buffer[7])/TILE_SIZE; | |
589 | i++) { | |
590 | for (j = (message_buffer[4]*255 + message_buffer[5])/TILE_SIZE; | |
591 | j <= (message_buffer[4]*255 + message_buffer[5] + message_buffer[8]*255 + message_buffer[9])/TILE_SIZE; | |
592 | j++) { | |
593 | tile_updated[j][i] = 1; | |
594 | } | |
595 | } | |
596 | } | |
597 | ||
598 | /* Signal that there is now a pending update request */ | |
599 | xEventGroupSetBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
600 | break; | |
601 | ||
602 | case KEY_EVENT: | |
603 | /* Handle the key event, ignored for brewboard */ | |
604 | /* Get the remainder of message (7 bytes) */ | |
605 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 7) == 0) { | |
606 | goto close_connection; | |
607 | } | |
608 | break; | |
609 | ||
610 | case POINTER_EVENT: | |
611 | /* Handle the pointer event, simulate the touch screen. */ | |
612 | /* Get the remainder of message (5 bytes) */ | |
613 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 5) == 0) { | |
614 | goto close_connection; | |
615 | } | |
616 | /* Set global variables that will be read by another task. */ | |
617 | VNC_pointer_button = message_buffer[1]; | |
618 | VNC_pointer_x = message_buffer[2]*255 + message_buffer[3]; | |
619 | VNC_pointer_y = message_buffer[4]*255 + message_buffer[5]; | |
620 | break; | |
621 | ||
622 | case CLIENT_CUT_TEXT: | |
623 | /* Handle the client has cut text event */ | |
624 | /* Current we just get and discard the data */ | |
625 | /* Get the next 7 bytes of the message */ | |
626 | if (GetMessageData(vnc_client_sock, &(message_buffer[1]), 7) == 0) { | |
627 | goto close_connection; | |
628 | } | |
629 | ||
630 | ptr_to_uint32 = (uint32_t *)&(message_buffer[4]); | |
631 | temp_long = htonl(*ptr_to_uint32); | |
632 | ||
633 | while (temp_long > 0) { | |
634 | /* Get the text in chunks MESSAGE_BUFFER_SIZE-1 characters */ | |
635 | if (temp_long > MESSAGE_BUFFER_SIZE-2) { | |
636 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), MESSAGE_BUFFER_SIZE-1) == 0) { | |
637 | goto close_connection; | |
638 | } | |
639 | ||
640 | message_buffer[MESSAGE_BUFFER_SIZE-1] = 0; | |
641 | temp_long -= (MESSAGE_BUFFER_SIZE-1); | |
642 | } else { | |
643 | if (GetMessageData(vnc_client_sock, &(message_buffer[0]), temp_long) == 0) { | |
644 | goto close_connection; | |
645 | } | |
646 | ||
647 | message_buffer[temp_long] = 0; | |
648 | temp_long = 0; | |
649 | } | |
650 | } | |
651 | ||
652 | break; | |
653 | ||
654 | default: | |
655 | ESP_LOGI(TAG, "Unknown message %d from client", message_buffer[0]); | |
656 | } | |
657 | ||
658 | vTaskDelay( (TickType_t)1); | |
659 | } | |
660 | ||
661 | close_connection: | |
662 | ESP_LOGI(TAG, "VNC client disconnected"); | |
663 | ||
664 | close_connection_quietly: | |
665 | ||
666 | /* Cancel any outstanding update requests */ | |
667 | xEventGroupClearBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
668 | close(vnc_client_sock); | |
669 | } | |
670 | ||
671 | ||
672 | ||
673 | /** | |
674 | * @brief Frame update task. This thread handles the sending of all frame | |
675 | * update data to the client and sends the 'ring bell' message to | |
676 | * the client when required. | |
677 | * Works for VNC port connected clients and Websocket clients. | |
678 | * @param pvParameter Ignored | |
679 | */ | |
680 | void task_frame(void *pvParameter) | |
681 | { | |
682 | int i, j, x_pos, y_pos, packet_length, num_updated_tiles; | |
683 | uint16_t *ptr_to_uint16; | |
684 | /* These are declared static so they don't use thread stack memory */ | |
685 | static uint8_t FramebufferUpdate_msg[4 + 12 + TILE_SIZE*TILE_SIZE*BITS_PER_PIXEL/8 + 1460]; | |
686 | static int FrameBufferPtr; /* Pointer to next space in buffer */ | |
687 | static int tile_updated_local[NUM_TILES_Y_AXIS][NUM_TILES_X_AXIS]; | |
688 | ||
689 | ESP_LOGI(TAG, "Starting VNC frame updater"); | |
690 | ||
691 | while(1) { | |
692 | /* Wait until client sends a frame update request */ | |
693 | wait_for_client: | |
694 | xEventGroupWaitBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ, pdFALSE, pdFALSE, portMAX_DELAY); | |
695 | ||
696 | /* Copy tile_updated array to local version and clear copied tiles */ | |
697 | vTaskSuspendAll(); | |
698 | num_updated_tiles = 0; | |
699 | for (i = 0; i < NUM_TILES_Y_AXIS; i++) { | |
700 | for (j = 0; j < NUM_TILES_X_AXIS; j++) { | |
701 | if (tile_updated[i][j]) { | |
702 | tile_updated_local[i][j] = 1; | |
703 | tile_updated[i][j] = 0; | |
704 | num_updated_tiles++; /* Keep count of the updated tiles */ | |
705 | } | |
706 | } | |
707 | } | |
708 | xTaskResumeAll(); | |
709 | ||
710 | if (num_updated_tiles) { | |
711 | /* Cancel update request */ | |
712 | xEventGroupClearBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
713 | /* Fill in constant parts of FramebufferUpdate Message */ | |
714 | FramebufferUpdate_msg[0] = 0; /* Message-type */ | |
715 | FramebufferUpdate_msg[1] = 0; /* Padding */ | |
716 | ptr_to_uint16 = (uint16_t *) &(FramebufferUpdate_msg[2]); | |
717 | *ptr_to_uint16 = htons(num_updated_tiles); /* Number-of-rectangles */ | |
718 | FrameBufferPtr = 4; | |
719 | ||
720 | for (y_pos = 0; y_pos < NUM_TILES_Y_AXIS; y_pos++) { | |
721 | for (x_pos = 0; x_pos < NUM_TILES_X_AXIS; x_pos++) { | |
722 | if (tile_updated_local[y_pos][x_pos]) { | |
723 | /* Send current square data to client */ | |
724 | ||
725 | /* x-position */ | |
726 | FramebufferUpdate_msg[FrameBufferPtr+0] = (x_pos * TILE_SIZE) / 256; | |
727 | FramebufferUpdate_msg[FrameBufferPtr+1] = (x_pos * TILE_SIZE) % 256; | |
728 | ||
729 | /* y-position */ | |
730 | FramebufferUpdate_msg[FrameBufferPtr+2] = (y_pos * TILE_SIZE) / 256; | |
731 | FramebufferUpdate_msg[FrameBufferPtr+3] = (y_pos * TILE_SIZE) % 256; | |
732 | ||
733 | ||
734 | /* Set width of tile in packet */ | |
735 | if (x_pos == (NUM_TILES_X_AXIS -1)) { | |
736 | /* Last tile in X-axis */ | |
737 | FramebufferUpdate_msg[FrameBufferPtr+4] = LAST_TILE_WIDTH / 256; | |
738 | FramebufferUpdate_msg[FrameBufferPtr+5] = LAST_TILE_WIDTH % 256; | |
739 | } else { | |
740 | FramebufferUpdate_msg[FrameBufferPtr+4] = TILE_SIZE / 256; | |
741 | FramebufferUpdate_msg[FrameBufferPtr+5] = TILE_SIZE % 256; | |
742 | } | |
743 | ||
744 | if (y_pos == (NUM_TILES_Y_AXIS -1)) { | |
745 | /* Last tile in Y-axis */ | |
746 | FramebufferUpdate_msg[FrameBufferPtr+6] = LAST_TILE_HEIGHT / 256; | |
747 | FramebufferUpdate_msg[FrameBufferPtr+7] = LAST_TILE_HEIGHT % 256; | |
748 | } else { | |
749 | FramebufferUpdate_msg[FrameBufferPtr+6] = TILE_SIZE / 256; | |
750 | FramebufferUpdate_msg[FrameBufferPtr+7] = TILE_SIZE % 256; | |
751 | } | |
752 | ||
753 | /* Generate the packet data for this tile */ | |
754 | packet_length = GenTileUpdateData(&(FramebufferUpdate_msg[FrameBufferPtr])); | |
755 | ||
756 | /* Send the packet data for this tile to the client */ | |
757 | FrameBufferPtr += packet_length; | |
758 | ||
759 | if (FrameBufferPtr > 1460) { | |
760 | /* Send the data to the client */ | |
761 | if (VNC_WS_num != -1) { | |
762 | ws_server_send_bin_client(VNC_WS_num, (char *)FramebufferUpdate_msg, FrameBufferPtr); | |
763 | } | |
764 | if (vnc_client_connected) { | |
765 | if (send(vnc_client_sock, FramebufferUpdate_msg, FrameBufferPtr, 0) != FrameBufferPtr) { | |
766 | goto wait_for_client; | |
767 | } | |
768 | } | |
769 | FrameBufferPtr = 0; | |
770 | } | |
771 | ||
772 | tile_updated_local[y_pos][x_pos] = 0; /* Clear the update bit for this square */ | |
773 | } | |
774 | } | |
775 | } | |
776 | ||
777 | if (FrameBufferPtr > 0) { | |
778 | /* Last data for this update, send it to the client */ | |
779 | if (VNC_WS_num != -1) { | |
780 | ws_server_send_bin_client(VNC_WS_num, (char *)FramebufferUpdate_msg, FrameBufferPtr); | |
781 | } | |
782 | if (vnc_client_connected) { | |
783 | if (send(vnc_client_sock, FramebufferUpdate_msg, FrameBufferPtr, 0) != FrameBufferPtr) { | |
784 | goto wait_for_client; | |
785 | } | |
786 | } | |
787 | ||
788 | FrameBufferPtr = 0; | |
789 | } | |
790 | ||
791 | } else { /* if (num_updated_tiles) */ | |
792 | /* There was no new display data to send to the client */ | |
793 | /* Sleep for 1/20th second before checking again */ | |
794 | vTaskDelay(20 / portTICK_PERIOD_MS); | |
795 | } | |
796 | ||
797 | /* Check for sound bell event */ | |
798 | if (xSemaphoreTake(SoundBell_lock, 10) == pdTRUE) { | |
799 | if (SoundBellCount) { | |
800 | --SoundBellCount; | |
801 | xSemaphoreGive(SoundBell_lock); | |
802 | ||
803 | if (vnc_client_connected) { | |
804 | if (send(vnc_client_sock, sound_bell, 1, 0) != 1) { | |
805 | goto wait_for_client; | |
806 | } | |
807 | } | |
808 | if (VNC_WS_num != -1) { | |
809 | ws_server_send_bin_client(VNC_WS_num, sound_bell, 1); | |
810 | } | |
811 | } else { | |
812 | xSemaphoreGive(SoundBell_lock); | |
813 | } | |
814 | } | |
815 | } | |
816 | } | |
817 | ||
818 | ||
819 | ||
820 | /** | |
821 | * Convert a framebuffer pixel to BGR233 if needed. | |
822 | * @param pixel The 8 bit pixel value | |
823 | * @return Then unchanged or changed pixel. | |
824 | */ | |
825 | vnc_color_t IRAM_ATTR PixelConvert(vnc_color_t pixel) | |
826 | { | |
827 | if (!AltPixels) | |
828 | return pixel; | |
829 | ||
830 | return (((pixel >> RED_SHIFT) & RED_MAX) << Red_Shift) | | |
831 | (((pixel >> GREEN_SHIFT) & GREEN_MAX) << Green_Shift) | | |
832 | (((pixel >> BLUE_SHIFT) & BLUE_MAX) << Blue_Shift); | |
833 | } | |
834 | ||
835 | ||
836 | ||
837 | /** | |
838 | * @brief Generate tile update data function | |
839 | * | |
840 | * This function is called by the frame_update thread to generate the message | |
841 | * data for a tile to send to the client. This function expects the | |
842 | * x-position, y-position, width and height fields of the buffer to be filled | |
843 | * in before it is called. | |
844 | * | |
845 | * The format of the buffer is: | |
846 | * packet_buffer[0:1] - x-position of tile | |
847 | * packet_buffer[2:3] - y-position of tile | |
848 | * packet_buffer[4:5] - width of tile | |
849 | * packet_buffer[6:7] - height of tile | |
850 | * packet_buffer[8 onwards] - Pixel data for the tile | |
851 | * | |
852 | * The pixel data will be encoded with CoRRE encoding (if the CDL option is | |
853 | * enabled and the client can handle it) or RAW encoding if that is smaller | |
854 | * than CoRRE encoding for that particular tile. | |
855 | * | |
856 | * @param packet_buffer Buffer to store tile data | |
857 | * @return Length of generated data in bytes | |
858 | */ | |
859 | static int GenTileUpdateData(uint8_t *packet_buffer) | |
860 | { | |
861 | uint16_t x_pos, y_pos; | |
862 | int i, j; | |
863 | int tile_width, tile_height; | |
864 | ||
865 | /* Get the X and Y positions of this tile from the packet buffer */ | |
866 | x_pos = packet_buffer[0] * 256 + packet_buffer[1]; | |
867 | y_pos = packet_buffer[2] * 256 + packet_buffer[3]; | |
868 | ||
869 | /* Get the tile width and height from the packet buffer */ | |
870 | tile_width = packet_buffer[4] * 256 + packet_buffer[5]; | |
871 | tile_height = packet_buffer[6] * 256 + packet_buffer[7]; | |
872 | ||
873 | /* Create packet data using RAW encoding */ | |
874 | for (i = 0; i < tile_height; i++) { | |
875 | for (j = 0; j < tile_width; j++) { | |
876 | if (Bits_Per_Pixel == 8) { | |
877 | packet_buffer[12 + tile_width * i + j] = PixelConvert(frame_buffer[y_pos + i][x_pos + j]); | |
878 | } else { | |
879 | packet_buffer[12 + 2 * tile_width * i + 2*j] = COLOUR2BYTE0(frame_buffer[y_pos + i][x_pos + j]); | |
880 | packet_buffer[12 + 2 * tile_width * i + 2*j+ 1] = COLOUR2BYTE1(frame_buffer[y_pos + i][x_pos + j]); | |
881 | } | |
882 | } | |
883 | } | |
884 | ||
885 | /* Set the encoding type to raw */ | |
886 | packet_buffer[8+0] = 0; | |
887 | packet_buffer[8+1] = 0; | |
888 | packet_buffer[8+2] = 0; | |
889 | packet_buffer[8+3] = 0; | |
890 | ||
891 | return (12 + tile_width*tile_height*(Bits_Per_Pixel/8)); | |
892 | } | |
893 | ||
894 | ||
895 | ||
896 | /** | |
897 | * @brief Get message data function | |
898 | * This function is called by the client_handler thread to get data | |
899 | * from the client's socket. | |
900 | * | |
901 | * @param socket_fd File descriptor of the socket to get the data from. | |
902 | * @param *buffer Buffer to store received data in. | |
903 | * @param num_bytes Number of bytes to attempt to get. | |
904 | * | |
905 | * @return 1 on sucessfull completion - 0 on error. | |
906 | */ | |
907 | static int GetMessageData(int socket_fd, char *buffer, int num_bytes) | |
908 | { | |
909 | int bytes_rxd; | |
910 | int message_len = 0; | |
911 | ||
912 | while (message_len < num_bytes) { | |
913 | if ((bytes_rxd = recv(socket_fd, buffer, num_bytes, 0)) <= 0) { | |
914 | return 0; | |
915 | } | |
916 | message_len += bytes_rxd; | |
917 | } | |
918 | ||
919 | return 1; | |
920 | } | |
921 | ||
922 | ||
923 | ||
924 | void VncCls(vnc_color_t color) | |
925 | { | |
926 | /* Clear the frame buffer */ | |
927 | int i, j; | |
928 | ||
929 | for (i = 0; i < CONFIG_VNC_SERVER_FRAME_HEIGHT; i++) { | |
930 | for (j = 0; j < CONFIG_VNC_SERVER_FRAME_WIDTH; j++) { | |
931 | VncDrawPixel(j, i, color); | |
932 | } | |
933 | } | |
934 | } | |
935 | ||
936 | ||
937 | ||
938 | void IRAM_ATTR VncDrawPixel(uint16_t x, uint16_t y, vnc_color_t color) | |
939 | { | |
940 | if (x >= CONFIG_VNC_SERVER_FRAME_WIDTH || y >= CONFIG_VNC_SERVER_FRAME_HEIGHT) { | |
941 | printf("write_frame(%d, %d) tile %d/%d\n", x, y, x/TILE_SIZE, y/TILE_SIZE); | |
942 | return; | |
943 | } | |
944 | ||
945 | /* Set that pixel to 'colour' */ | |
946 | frame_buffer[y][x] = color; | |
947 | ||
948 | /* Mark the tile for update */ | |
949 | tile_updated[y/TILE_SIZE][x/TILE_SIZE] = 1; | |
950 | } | |
951 | ||
952 | ||
953 | ||
954 | void IRAM_ATTR VncDrawHorzLine(uint16_t x1, uint16_t x2, uint16_t y, vnc_color_t color) | |
955 | { | |
956 | int i; | |
957 | ||
958 | /* Draw the line */ | |
959 | for (i = x1; i <= x2; i++) { | |
960 | VncDrawPixel(i, y, color); | |
961 | } | |
962 | } | |
963 | ||
964 | ||
965 | ||
966 | void IRAM_ATTR VncDrawVertLine(uint16_t x, uint16_t y1, uint16_t y2, vnc_color_t color) | |
967 | { | |
968 | int i; | |
969 | ||
970 | /* Draw the line */ | |
971 | for (i = y1; i <= y2; i++) { | |
972 | VncDrawPixel(x, i, color); | |
973 | } | |
974 | } | |
975 | ||
976 | ||
977 | ||
978 | void IRAM_ATTR VncFillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, vnc_color_t color) | |
979 | { | |
980 | /* Draw a solid rectangle */ | |
981 | int i, j; | |
982 | ||
983 | for (i = y1; i <= y2; i++) { | |
984 | for (j = x1; j <= x2; j++) { | |
985 | VncDrawPixel(j, i, color); | |
986 | } | |
987 | } | |
988 | } | |
989 | ||
990 | ||
991 | ||
992 | void VncSoundBell(void) | |
993 | { | |
994 | if (xSemaphoreTake(SoundBell_lock, 10) == pdTRUE) { | |
995 | SoundBellCount++; | |
996 | xSemaphoreGive(SoundBell_lock); | |
997 | } | |
998 | } | |
999 | ||
1000 | ||
1001 | ||
1002 | /** | |
1003 | * @brief Websocket task. Created on a websocket connection. | |
1004 | */ | |
1005 | void task_WS(void *pvParameter) | |
1006 | { | |
1007 | int i, j, num_of_encodings, rest_time; | |
1008 | uint16_t *ptr_to_uint16; | |
1009 | struct strMessage _msg; | |
1010 | ||
1011 | // Do pin/pong. | |
1012 | ||
1013 | VNC_WS_run = true; | |
1014 | rest_time = 0; | |
1015 | ||
1016 | while (VNC_WS_run) { | |
1017 | ||
1018 | /* | |
1019 | * Wait until something is received from the client. Exit the queue receiver | |
1020 | * each 5 milliseconds to allow to evaluate the VNC_WS_run flag and to allow | |
1021 | * a clean exit of this task. | |
1022 | */ | |
1023 | if (xQueueReceive(message_queue, &_msg, 5 / portTICK_PERIOD_MS) == pdTRUE) { | |
1024 | // dump_msg((char *)_msg.message, _msg.length, "pull"); | |
1025 | ||
1026 | switch (_msg.message[0]) { | |
1027 | ||
1028 | case SET_PIXEL_FORMAT: | |
1029 | // printf("SET_PIXEL_FORMAT\n"); | |
1030 | /* Check total message length */ | |
1031 | if (_msg.length >= 20) { | |
1032 | /* Check pixel format is as expected */ | |
1033 | i = 0; | |
1034 | if (_msg.message[4] != BITS_PER_PIXEL) { | |
1035 | ESP_LOGI(TAG, "SetPixelFormat client wants %d bits-per-pixel", _msg.message[4]); | |
1036 | i++; | |
1037 | } | |
1038 | ||
1039 | if (_msg.message[5] != PIXEL_DEPTH) { | |
1040 | ESP_LOGI(TAG, "SetPixelFormat client wants %d pixel-depth", _msg.message[5]); | |
1041 | i++; | |
1042 | } | |
1043 | ||
1044 | if ((_msg.message[7] & 0x01) != TRUE_COLOUR_FLAG) { | |
1045 | ESP_LOGI(TAG, "SetPixelFormat client wants %d true-colour-flag", _msg.message[7]); | |
1046 | i++; | |
1047 | } | |
1048 | ||
1049 | ptr_to_uint16 = (uint16_t *)&(_msg.message[8]); | |
1050 | if (htons(*ptr_to_uint16) != Red_Max) { | |
1051 | Red_Max = htons(*ptr_to_uint16); | |
1052 | AltPixels = true; | |
1053 | ESP_LOGI(TAG, "SetPixelFormat client granted %d red-max", Red_Max); | |
1054 | } | |
1055 | ||
1056 | ptr_to_uint16 = (uint16_t *)&(_msg.message[10]); | |
1057 | if (htons(*ptr_to_uint16) != Green_Max) { | |
1058 | Green_Max = htons(*ptr_to_uint16); | |
1059 | AltPixels = true; | |
1060 | ESP_LOGI(TAG, "SetPixelFormat client granted %d green-max", Green_Max); | |
1061 | } | |
1062 | ||
1063 | ptr_to_uint16 = (uint16_t *)&(_msg.message[12]); | |
1064 | if (htons(*ptr_to_uint16) != Blue_Max) { | |
1065 | Blue_Max = htons(*ptr_to_uint16); | |
1066 | AltPixels = true; | |
1067 | ESP_LOGI(TAG, "SetPixelFormat client granted %d blue-max", Blue_Max); | |
1068 | } | |
1069 | ||
1070 | if (_msg.message[14] != Red_Shift) { | |
1071 | Red_Shift = _msg.message[14]; | |
1072 | AltPixels = true; | |
1073 | ESP_LOGI(TAG, "SetPixelFormat client granted %d red-shift", Red_Shift); | |
1074 | } | |
1075 | ||
1076 | if (_msg.message[15] != Green_Shift) { | |
1077 | Green_Shift = _msg.message[15]; | |
1078 | AltPixels = true; | |
1079 | ESP_LOGI(TAG, "SetPixelFormat client granted %d green-shift", Green_Shift); | |
1080 | } | |
1081 | ||
1082 | if (_msg.message[16] != Blue_Shift) { | |
1083 | Blue_Shift = _msg.message[16]; | |
1084 | AltPixels = true; | |
1085 | ESP_LOGI(TAG, "SetPixelFormat client granted %d blue-shift", Blue_Shift); | |
1086 | } | |
1087 | ||
1088 | if (i) { | |
1089 | ESP_LOGI(TAG, "SetPixelFormat %d errors, disconnect client", i); | |
1090 | ESP_LOGI(TAG, "Ensure the 'Auto select' is not enabled in your vncviewer options,"); | |
1091 | _msg.length = 0; | |
1092 | ws_server_remove_client(VNC_WS_num); | |
1093 | } | |
1094 | } | |
1095 | break; | |
1096 | ||
1097 | case FIX_COLOUR_MAP_ENTRIES: | |
1098 | printf("FIX_COLOUR_MAP_ENTRIES\n"); | |
1099 | break; | |
1100 | ||
1101 | case SET_ENCODINGS: | |
1102 | if (_msg.length >= 3) { | |
1103 | // Get the total encodings | |
1104 | num_of_encodings = _msg.message[2]*255 + _msg.message[3]; | |
1105 | ||
1106 | /* Clear the encoding type structure */ | |
1107 | encoding_type.raw = 0; | |
1108 | encoding_type.copy_rectangle = 0; | |
1109 | encoding_type.rre = 0; | |
1110 | encoding_type.corre = 0; | |
1111 | encoding_type.hextile = 0; | |
1112 | ||
1113 | for (i = 0; i < num_of_encodings; i++) { | |
1114 | switch(_msg.message[3 + (i*4)]) { | |
1115 | case 0: /* Raw encoding */ | |
1116 | encoding_type.raw = i + 1; | |
1117 | break; | |
1118 | case 1: /* Copy rectangle encoding */ | |
1119 | encoding_type.copy_rectangle = i + 1; | |
1120 | break; | |
1121 | case 2: /* RRE encoding */ | |
1122 | encoding_type.rre = i + 1; | |
1123 | break; | |
1124 | case 4: /* CoRRE encoding */ | |
1125 | encoding_type.corre = i + 1; | |
1126 | break; | |
1127 | case 5: /* Hextile encoding */ | |
1128 | encoding_type.hextile = i + 1; | |
1129 | break; | |
1130 | default: /* Unknown coding type - do nothing */ | |
1131 | break; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | if (encoding_type.corre) { | |
1136 | ESP_LOGI(TAG, "SetEncodings use CORRE"); | |
1137 | } else { | |
1138 | ESP_LOGI(TAG, "SetEncodings use RAW"); | |
1139 | } | |
1140 | } | |
1141 | break; | |
1142 | ||
1143 | case FRAME_BUFFER_UPDATE_REQ: | |
1144 | if (_msg.length == 10) { | |
1145 | if (!_msg.message[1]) { | |
1146 | /* Non-incremental mode - mark the squares that need to be updated */ | |
1147 | for (i = (_msg.message[2]*255 + _msg.message[3])/TILE_SIZE; | |
1148 | i <= (_msg.message[2]*255 + _msg.message[3] + _msg.message[6]*255 + _msg.message[7])/TILE_SIZE; | |
1149 | i++) { | |
1150 | for (j = (_msg.message[4]*255 + _msg.message[5])/TILE_SIZE; | |
1151 | j <= (_msg.message[4]*255 + _msg.message[5] + _msg.message[8]*255 + _msg.message[9])/TILE_SIZE; | |
1152 | j++) { | |
1153 | tile_updated[j][i] = 1; | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | /* Signal that there is now a pending update request */ | |
1159 | xEventGroupSetBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
1160 | } | |
1161 | break; | |
1162 | ||
1163 | case KEY_EVENT: | |
1164 | printf("KEY_EVENT\n"); | |
1165 | /* Handle the key event, ignored for brewboard */ | |
1166 | /* Get the remainder of message (7 bytes) */ | |
1167 | break; | |
1168 | ||
1169 | case POINTER_EVENT: | |
1170 | /* Handle the pointer event, simulate the touch screen. */ | |
1171 | if (_msg.length == 6) { | |
1172 | /* Set global variables that will be read by another task. */ | |
1173 | VNC_pointer_button = _msg.message[1]; | |
1174 | VNC_pointer_x = _msg.message[2]*255 + _msg.message[3]; | |
1175 | VNC_pointer_y = _msg.message[4]*255 + _msg.message[5]; | |
1176 | // printf("POINTER_EVENT button=%02x x=%d y=%d\n", VNC_pointer_button, VNC_pointer_x, VNC_pointer_y); | |
1177 | } | |
1178 | _msg.length = 0; | |
1179 | break; | |
1180 | ||
1181 | case CLIENT_CUT_TEXT: | |
1182 | printf("CLIENT_CUT_TEXT\n"); | |
1183 | break; | |
1184 | ||
1185 | default: | |
1186 | ESP_LOGI(TAG, "Unknown message %d from client", _msg.message[0]); | |
1187 | } | |
1188 | rest_time = 0; | |
1189 | } else { | |
1190 | rest_time++; // Each 5 milliseconds | |
1191 | if (rest_time == 2000) { | |
1192 | // 10 seconds silence, send a PING | |
1193 | rest_time = 0; | |
1194 | // int rc = ws_server_ping(VNC_WS_num); | |
1195 | // ESP_LOGI(TAG, "Send PING to %d, rc=%d", VNC_WS_num, rc); | |
1196 | } | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | VNC_WS_num = -1; | |
1201 | xEventGroupClearBits(xEventGroupVNC, VNC_CLIENT_UPDATE_REQ); | |
1202 | ESP_LOGI(TAG, "task_WS finished"); | |
1203 | vTaskDelete(NULL); | |
1204 | } | |
1205 | ||
1206 | ||
1207 | ||
1208 | int VncStartWS(int num) | |
1209 | { | |
1210 | char protocol_ver[8], message_buffer[MESSAGE_BUFFER_SIZE]; | |
1211 | int i, ProtocolOkay; | |
1212 | uint32_t *ptr_to_uint32; | |
1213 | uint16_t *ptr_to_uint16; | |
1214 | struct strMessage _msg; | |
1215 | ||
1216 | ESP_LOGI(TAG, "Start VNC WebSocket connection %d", num); | |
1217 | message_queue = xQueueCreate(message_queue_size, sizeof(struct strMessage)); | |
1218 | ||
1219 | /* | |
1220 | * Initial handshake | |
1221 | */ | |
1222 | ws_server_send_bin_client(num, "RFB 003.003\n", 12); | |
1223 | ProtocolOkay = 1; | |
1224 | if (xQueueReceive(message_queue, &_msg, 5000 / portTICK_PERIOD_MS) == pdTRUE) { | |
1225 | // dump_msg((char *)_msg.message, _msg.length, "pull"); | |
1226 | ||
1227 | for (i = 0; i < 8; i++) { | |
1228 | if (_msg.message[i] != server_ProtocolVersion[i]) { | |
1229 | ProtocolOkay = 0; | |
1230 | } | |
1231 | /* Store the protocol version - ignoring thr 'RFB ' part */ | |
1232 | protocol_ver[i] = _msg.message[i + 4]; | |
1233 | } | |
1234 | protocol_ver[7] = 0; | |
1235 | } else { | |
1236 | ESP_LOGE(TAG, "Client timeout after initial message"); | |
1237 | return -5; | |
1238 | } | |
1239 | ||
1240 | if (ProtocolOkay == 0) { | |
1241 | /* Generate the Bad ProtocolVerion message */ | |
1242 | ESP_LOGI(TAG, "Start VNC WebSocket task failed, bad protocol."); | |
1243 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
1244 | *ptr_to_uint32 = htonl(0); | |
1245 | ptr_to_uint32 = (uint32_t *) &(message_buffer[4]); | |
1246 | *ptr_to_uint32 = htonl(strlen(bad_protocol)); | |
1247 | strcpy(&(message_buffer[8]), bad_protocol); | |
1248 | ws_server_send_bin_client(num, message_buffer, 8 + strlen(bad_protocol)); | |
1249 | vTaskDelay(500 / portTICK_PERIOD_MS); | |
1250 | ws_server_remove_client(num); | |
1251 | return -2; | |
1252 | } | |
1253 | /* ProtocolVerion is okay - connect with no authentication*/ | |
1254 | ||
1255 | /* Server is busy with another client */ | |
1256 | if (VNC_WS_run || vnc_client_connected) { | |
1257 | ESP_LOGI(TAG, "Start VNC WebSocket task failed, server busy."); | |
1258 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
1259 | *ptr_to_uint32 = htonl(0); | |
1260 | ptr_to_uint32 = (uint32_t *) &(message_buffer[4]); | |
1261 | *ptr_to_uint32 = htonl(strlen(server_busy)); | |
1262 | strcpy(&(message_buffer[8]), server_busy); | |
1263 | ws_server_send_bin_client(num, message_buffer, 8 + strlen(server_busy)); | |
1264 | vTaskDelay(500 / portTICK_PERIOD_MS); | |
1265 | ws_server_remove_client(num); | |
1266 | return -1; | |
1267 | } | |
1268 | ||
1269 | /* Generate the No Authentication message */ | |
1270 | ptr_to_uint32 = (uint32_t *) &(message_buffer[0]); | |
1271 | *ptr_to_uint32 = htonl((uint32_t)1); | |
1272 | ws_server_send_bin_client(num, message_buffer, 4); | |
1273 | /* Authentication - end */ | |
1274 | ||
1275 | /* ClientInitialisation - begin */ | |
1276 | /* Receive initialisation message from client (1 byte) */ | |
1277 | if (xQueueReceive(message_queue, &_msg, 5000 / portTICK_PERIOD_MS) == pdFALSE) { | |
1278 | ESP_LOGE(TAG, "Client timeout after auth message"); | |
1279 | return -5; | |
1280 | } | |
1281 | /* Do nothing with this as we only support 1 Client at a time */ | |
1282 | /* ClientInitialisation - end */ | |
1283 | ||
1284 | /* Initial default RGB332 */ | |
1285 | Red_Max = RED_MAX; | |
1286 | Green_Max = GREEN_MAX; | |
1287 | Blue_Max = BLUE_MAX; | |
1288 | Red_Shift = RED_SHIFT; | |
1289 | Green_Shift = GREEN_SHIFT; | |
1290 | Blue_Shift = BLUE_SHIFT; | |
1291 | AltPixels = false; | |
1292 | ||
1293 | /* ServerInitialisation - begin */ | |
1294 | /* Create Initialisation message for client */ | |
1295 | ptr_to_uint16 = (uint16_t *) &(message_buffer[0]); | |
1296 | *ptr_to_uint16 = htons((uint16_t)CONFIG_VNC_SERVER_FRAME_WIDTH); | |
1297 | ||
1298 | ptr_to_uint16 = (uint16_t *) &(message_buffer[2]); | |
1299 | *ptr_to_uint16 = htons((uint16_t)CONFIG_VNC_SERVER_FRAME_HEIGHT); | |
1300 | ||
1301 | message_buffer[4] = (uint8_t)BITS_PER_PIXEL; | |
1302 | message_buffer[5] = (uint8_t)PIXEL_DEPTH; | |
1303 | message_buffer[6] = (uint8_t)BIG_ENDIAN_FLAG; | |
1304 | message_buffer[7] = (uint8_t)TRUE_COLOUR_FLAG; | |
1305 | ||
1306 | ptr_to_uint16 = (uint16_t *) &(message_buffer[8]); | |
1307 | *ptr_to_uint16 = htons(Red_Max); | |
1308 | ||
1309 | ptr_to_uint16 = (uint16_t *) &(message_buffer[10]); | |
1310 | *ptr_to_uint16 = htons(Green_Max); | |
1311 | ||
1312 | ptr_to_uint16 = (uint16_t *) &(message_buffer[12]); | |
1313 | *ptr_to_uint16 = htons(Blue_Max); | |
1314 | ||
1315 | message_buffer[14] = Red_Shift; | |
1316 | message_buffer[15] = Green_Shift; | |
1317 | message_buffer[16] = Blue_Shift; | |
1318 | ||
1319 | ptr_to_uint32 = (uint32_t *) &(message_buffer[20]); | |
1320 | *ptr_to_uint32 = htonl(strlen(desktop_name)); | |
1321 | strcpy(&(message_buffer[24]), desktop_name); | |
1322 | ||
1323 | // dump_msg(message_buffer, 24 + strlen(desktop_name), (char *)"send"); | |
1324 | ws_server_send_bin_client(num, message_buffer, 24 + strlen(desktop_name)); | |
1325 | ||
1326 | /* ServerInitialisation - end */ | |
1327 | ESP_LOGI(TAG, "VNC WebSocket client connected (RFB Protocol Ver: %s)", protocol_ver); | |
1328 | ||
1329 | VNC_WS_num = num; | |
1330 | xTaskCreate(&task_WS, "WSclient", MIN_STACK_SIZE, NULL, 5, &xTaskWSclient); | |
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | ||
1335 | ||
1336 | void VncStopWS(int num) | |
1337 | { | |
1338 | if (! VNC_WS_run) { | |
1339 | ESP_LOGI(TAG, "Stop VNC WebSocket, not running."); | |
1340 | return; | |
1341 | } | |
1342 | ||
1343 | if (num != VNC_WS_num) { | |
1344 | ESP_LOGI(TAG, "Stop VNC WebSocket, %d is running, requested %d", VNC_WS_num, num); | |
1345 | return; | |
1346 | } | |
1347 | ||
1348 | ESP_LOGI(TAG, "Stop VNC WebSocket task connection %d", num); | |
1349 | VNC_WS_run = false; | |
1350 | xQueueReset(message_queue); | |
1351 | } | |
1352 | ||
1353 | ||
1354 | ||
1355 | void VncGetWSmessage(char *msg, uint16_t len) | |
1356 | { | |
1357 | int max; | |
1358 | struct strMessage _msg; | |
1359 | ||
1360 | if (len == 0) | |
1361 | return; | |
1362 | ||
1363 | max = len; | |
1364 | if (max > MESSAGE_BUFFER_SIZE) { | |
1365 | ESP_LOGE(TAG, "VncGetWSmessage need %d bytes in a %d length buffer", len, MESSAGE_BUFFER_SIZE); | |
1366 | max = MESSAGE_BUFFER_SIZE; | |
1367 | } | |
1368 | ||
1369 | for (int i = 0; i < max; i++) { | |
1370 | _msg.message[i] = msg[i]; | |
1371 | } | |
1372 | _msg.length = max; | |
1373 | if (xQueueSendToBack(message_queue, &_msg, 200 / portTICK_PERIOD_MS) == pdFALSE) { | |
1374 | ESP_LOGE(TAG, "VncGetWSmessage() Queue full, message lost"); | |
1375 | } | |
1376 | } | |
1377 |