Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
#include "lwip/opt.h"
#include "lwip/dhcp.h"
#include "lwip/netif.h"
#include "lwip/tcpip.h"
#include "lwip/sockets.h"
#include "vnc_app.h"
#include "ethernetif.h"
#include "GUI_VNC.h"
Private variables
#define MAX_DHCP_TRIES
VNC_State
VNC_LockState
gnetif
Netif_LinkSemaphore
link_arg
_Sock
iptxt
_Addr
_Context
is_initialized
Connection_accepted
VNC_ThreadId
Private function prototypes
_Send(const unsigned char *, int, void *)
_Recv(unsigned char *, int, void *)
_ListenAtTcpAddr(unsigned short)
VNC_Process()
Netif_Config()
HAL_GPIO_EXTI_Callback(uint16_t)
VNC_SERVER_Start()
VNC_SERVER_Stop()
ethernetif_notify_conn_changed(struct netif *)
VNC_GetState()
VNC_SetState(uint8_t)
VNC_GetLockState()
VNC_SetLockState(uint8_t)
VNC_Thread(const void *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesSTemWinModules/vnc_server/vnc_app.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file LwIP/LwIP_HTTP_Server_Netconn_RTOS/Src/app_ethernet.c * @author MCD Application Team * @brief Ethernet specific module ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "lwip/opt.h" #include "lwip/dhcp.h" #include "lwip/netif.h" #include "lwip/tcpip.h" #include "lwip/sockets.h" #include "vnc_app.h" #include "ethernetif.h" #include "GUI_VNC.h" 9 includes Includes/* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ #define MAX_DHCP_TRIES 4 __IO uint8_t VNC_State = VNC_IDLE; __IO uint8_t VNC_LockState = 0; struct netif gnetif; /* network interface structure */ /* Semaphore to signal Ethernet Link state update */ osSemaphoreId Netif_LinkSemaphore = NULL; /* Ethernet link thread Argument */ struct link_str link_arg; int _Sock; static uint8_t iptxt[128]; static struct sockaddr_in _Addr; static GUI_VNC_CONTEXT _Context; static int is_initialized = 0; static int Connection_accepted = 0; osThreadId VNC_ThreadId = 0;Private variables /* Private function prototypes -----------------------------------------------*/ void VNC_Thread(void const * argument); extern void VNC_SERVER_LogMessage (const char *message); extern void VNC_SERVER_StatusChanged (uint8_t status);Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Called by the server to send data * @param buf: buffer to be sent. * @param len: length of buf. * @param pConnectionInfo: Connection info * @retval transmit status. *//* ... */ static int _Send(const U8 * buf, int len, void * pConnectionInfo) { return ( send((long)pConnectionInfo, (const char *)buf, len, 0)); }{ ... } /** * @brief Called by the server when data is received * @param buf: buffer to get the received data. * @param len: length of received data. * @param pConnectionInfo: Connection info * @retval receive status. *//* ... */ static int _Recv(U8 * buf, int len, void * pConnectionInfo) { return recv((long)pConnectionInfo, (char *)buf, len, 0); }{ ... } /** * @brief Starts listening at a TCP port. * @param Port: TCP port to listen at * @retval listen status. *//* ... */ static int _ListenAtTcpAddr(U16 Port) { struct sockaddr_in addr; int sock; sock = socket(AF_INET, SOCK_STREAM, 0); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(Port); addr.sin_addr.s_addr = INADDR_ANY; bind(sock, (struct sockaddr *)&addr, sizeof(addr)); listen(sock, 1); return sock; }{ ... } /** * @brief VNC server thread process * @param argument: not used * @retval none *//* ... */ static void VNC_Process( void) { static int s; static uint8_t Is_SocketAssigned = 0; u32_t AddrLen; U16 Port; if(Is_SocketAssigned == 0) { Is_SocketAssigned = 1; /* Prepare socket (one time setup) Default port for VNC is is 590x, where x is the 0-based layer index *//* ... */ Port = 5900 + _Context.ServerIndex; /* Loop until we get a socket into listening state */ do { s = _ListenAtTcpAddr(Port); if (s != -1) { break; }if (s != -1) { ... } vTaskDelay(100); /* Try again */ ...} while (1); }if (Is_SocketAssigned == 0) { ... } VNC_SERVER_StatusChanged(VNC_CONN_ESTABLISHED); /* Loop once per client and create a thread for the actual server */ while (VNC_State == VNC_PROCESS) { /* Wait for an incoming connection */ AddrLen = sizeof(_Addr); Connection_accepted = 1; if ((_Sock = accept(s, (struct sockaddr*)&_Addr, &AddrLen)) < 1) { closesocket(_Sock); vTaskDelay(100); continue; /* Error */ }if ((_Sock = accept(s, (struct sockaddr*)&_Addr, &AddrLen)) < 1) { ... } Connection_accepted = 0; VNC_SERVER_LogMessage ("Connected to VNC Client"); GUI_VNC_Process(&_Context, _Send, _Recv, (void *)_Sock); VNC_SERVER_LogMessage ((char *)iptxt); /* Close the connection */ closesocket(_Sock); }while (VNC_State == VNC_PROCESS) { ... } }{ ... } /** * @brief Initializes the lwIP stack * @param None * @retval None *//* ... */ static void Netif_Config(void) { struct ip4_addr ipaddr; struct ip4_addr netmask; struct ip4_addr gw; /* IP address setting */ IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); /* - netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif), err_t (* input)(struct pbuf *p, struct netif *netif)) Adds your network interface to the netif_list. Allocate a struct netif and pass a pointer to this structure as the first argument. Give pointers to cleared ip_addr structures when using DHCP, or fill them with sane numbers otherwise. The state pointer may be NULL. The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use.*//* ... */ netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input); /* Registers the default network interface. */ netif_set_default(&gnetif); if (netif_is_link_up(&gnetif)) { /* When the netif is fully configured this function must be called.*/ netif_set_up(&gnetif); }if (netif_is_link_up(&gnetif)) { ... } else { /* When the netif link is down this function must be called */ netif_set_down(&gnetif); }else { ... } }{ ... } /** * @brief EXTI line detection callbacks * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None *//* ... */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == MFX_IRQOUT_PIN) { /* Get the IT status register value */ if(BSP_IO_ITGetStatus(MII_INT_PIN)) { /* ethernetif_notify_conn_changed(&gnetif);; */ }if (BSP_IO_ITGetStatus(MII_INT_PIN)) { ... } BSP_IO_ITClear(); }if (GPIO_Pin == MFX_IRQOUT_PIN) { ... } }{ ... } /** * @brief Initializes audio * @param None. * @retval Audio state. *//* ... */ void VNC_SERVER_Start (void) { if(is_initialized == 0) { is_initialized = 1; /* Create tcp_ip stack thread */ tcpip_init(NULL, NULL); /* Initialize the LwIP stack */ Netif_Config(); /* Check connection */ if (netif_is_up(&gnetif)) { /* Update VNC state machine */ VNC_State = VNC_LINK_UP; VNC_SERVER_StatusChanged(VNC_LINK_UP); }if (netif_is_up(&gnetif)) { ... } else { /* Update VNC state machine */ VNC_State = VNC_LINK_DOWN; VNC_SERVER_LogMessage ("Cable is not connected \n"); VNC_SERVER_StatusChanged(VNC_LINK_DOWN); }else { ... } }if (is_initialized == 0) { ... } else if(VNC_State == VNC_INIT) { /* Check connection */ if (netif_is_up(&gnetif)) { /* Update VNC state machine */ VNC_State = VNC_LINK_UP; VNC_SERVER_StatusChanged(VNC_LINK_UP); }if (netif_is_up(&gnetif)) { ... } else { /* Update VNC state machine */ VNC_State = VNC_LINK_DOWN; VNC_SERVER_LogMessage ("Cable is not connected \n"); VNC_SERVER_StatusChanged(VNC_LINK_DOWN); }else { ... } }else if (VNC_State == VNC_INIT) { ... } /* Start VNC Task */ if(VNC_ThreadId == 0) { osThreadDef(VNC, VNC_Thread, osPriorityAboveNormal, 0, 1024); VNC_ThreadId = osThreadCreate (osThread(VNC), NULL); }if (VNC_ThreadId == 0) { ... } }{ ... } /** * @brief Initializes audio * @param None. * @retval Audio state. *//* ... */ void VNC_SERVER_Stop (void) { VNC_State = VNC_IDLE; if(!Connection_accepted) closesocket(_Sock); VNC_SERVER_StatusChanged(VNC_IDLE); VNC_SERVER_LogMessage ("VNC Connection OFF"); if(VNC_ThreadId != 0) { osThreadTerminate(VNC_ThreadId); VNC_ThreadId = 0; }if (VNC_ThreadId != 0) { ... } }{ ... } /** * @brief This function notify user about link status changement. * @param netif: the network interface * @retval None *//* ... */ void ethernetif_notify_conn_changed(struct netif *netif) { struct ip4_addr ipaddr; struct ip4_addr netmask; struct ip4_addr gw; if(netif_is_link_up(netif)) { VNC_SERVER_LogMessage ("Cable is now connected."); /* Update DHCP state machine */ VNC_State = VNC_LINK_UP; VNC_SERVER_StatusChanged(VNC_LINK_UP); netif_set_addr(netif, &ipaddr , &netmask, &gw); /* When the netif is fully configured this function must be called.*/ netif_set_up(netif); }if (netif_is_link_up(netif)) { ... } else { /* Update DHCP state machine */ VNC_State = VNC_LINK_DOWN; /* When the netif link is down this function must be called.*/ netif_set_down(netif); VNC_SERVER_LogMessage ("Cable is not connected."); closesocket(_Sock); }else { ... } }{ ... } /** * @brief Get VNC state * @param None. * @retval VNC state. *//* ... */ uint8_t VNC_GetState(void) { return VNC_State; }{ ... } /** * @brief Get VNC state * @param None. * @retval VNC state. *//* ... */ void VNC_SetState(uint8_t state) { VNC_State = state; }{ ... } /** * @brief Get VNC state * @param None. * @retval VNC state. *//* ... */ uint8_t VNC_GetLockState(void) { return VNC_LockState; }{ ... } /** * @brief Get VNC state * @param None. * @retval VNC state. *//* ... */ void VNC_SetLockState(uint8_t LockState) { VNC_LockState = LockState; }{ ... } /** * @brief VNC Thread * @param argument: network interface * @retval None *//* ... */ void VNC_Thread(void const * argument) { struct dhcp *dhcp; for (;;) { switch (VNC_State) { case VNC_LINK_UP: { gnetif.ip_addr.addr = 0; gnetif.netmask.addr = 0; gnetif.gw.addr = 0; dhcp_start(&gnetif); VNC_State = VNC_WAIT_FOR_ADDRESS; VNC_SERVER_LogMessage ("Waiting for DHCP server...\n"); VNC_SERVER_StatusChanged(VNC_WAIT_FOR_ADDRESS); ...} break; case VNC_LINK_UP: case VNC_WAIT_FOR_ADDRESS: { if (dhcp_supplied_address(&gnetif)) { VNC_State = VNC_START; VNC_SERVER_StatusChanged(VNC_START); }if (dhcp_supplied_address(&gnetif)) { ... } else { dhcp = (struct dhcp *)netif_get_client_data(&gnetif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP); /* DHCP timeout */ if (dhcp->tries > MAX_DHCP_TRIES) { VNC_State = VNC_ERROR; dhcp_stop(&gnetif); VNC_SERVER_LogMessage ("No reply from DHCP Server!\n"); }if (dhcp->tries > MAX_DHCP_TRIES) { ... } }else { ... } ...} break; case VNC_WAIT_FOR_ADDRESS: case VNC_START: sprintf((char*)iptxt, "IP address : %d.%d.%d.%d\n", (uint8_t)(gnetif.ip_addr.addr), (uint8_t)((gnetif.ip_addr.addr) >> 8), (uint8_t)((gnetif.ip_addr.addr) >> 16), (uint8_t)((gnetif.ip_addr.addr) >> 24)); VNC_SERVER_LogMessage ((char *)iptxt); /* Init VNC context and attach to layer (so context is updated if the display-layer-contents change */ GUI_VNC_AttachToLayer(&_Context, 0); _Context.ServerIndex = 0; GUI_VNC_SetProgName ("STM32 VNC Server"); if(VNC_LockState) { GUI_VNC_SetPassword((U8 *)"STM32"); }if (VNC_LockState) { ... } else { GUI_VNC_SetAuthentication(NULL); }else { ... } VNC_State = VNC_PROCESS; break; case VNC_START: case VNC_PROCESS: VNC_Process(); break; case VNC_PROCESS: case VNC_IDLE: break; case VNC_IDLE: default: break;default }switch (VNC_State) { ... } osDelay(250); }for (;;) { ... } }{ ... }