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
52
53
54
61
64
65
72
75
76
81
82
83
84
85
86
87
88
89
90
91
92
93
94
99
100
101
102
103
104
105
106
107
108
109
111
112
113
114
121
122
123
124
125
126
127
128
129
130
135
136
137
138
139
140
141
142
143
144
145
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
184
189
190
191
192
197
209
210
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
235
242
243
260
261
262
267
268
269
274
275
276
277
278
279
280
281
282
283
288
289
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
332
336
337
338
343
347
352
356
357
362
366
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
432
436
437
438
439
440
444
447
449
450
451
452
/* ... */
#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
#define MAX_DHCP_TRIES 4
__IO uint8_t VNC_State = VNC_IDLE;
__IO uint8_t VNC_LockState = 0;
struct netif gnetif;
osSemaphoreId Netif_LinkSemaphore = NULL;
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
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
/* ... */
static int _Send(const U8 * buf, int len, void * pConnectionInfo) {
return ( send((long)pConnectionInfo, (const char *)buf, len, 0));
}{ ... }
/* ... */
static int _Recv(U8 * buf, int len, void * pConnectionInfo) {
return recv((long)pConnectionInfo, (char *)buf, len, 0);
}{ ... }
/* ... */
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;
}{ ... }
/* ... */
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;
/* ... */
Port = 5900 + _Context.ServerIndex;
do {
s = _ListenAtTcpAddr(Port);
if (s != -1) {
break;
}if (s != -1) { ... }
vTaskDelay(100);
...} while (1);
}if (Is_SocketAssigned == 0) { ... }
VNC_SERVER_StatusChanged(VNC_CONN_ESTABLISHED);
while (VNC_State == VNC_PROCESS)
{
AddrLen = sizeof(_Addr);
Connection_accepted = 1;
if ((_Sock = accept(s, (struct sockaddr*)&_Addr, &AddrLen)) < 1) {
closesocket(_Sock);
vTaskDelay(100);
continue;
}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);
closesocket(_Sock);
}while (VNC_State == VNC_PROCESS) { ... }
}{ ... }
/* ... */
static void Netif_Config(void)
{
struct ip4_addr ipaddr;
struct ip4_addr netmask;
struct ip4_addr gw;
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(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input);
netif_set_default(&gnetif);
if (netif_is_link_up(&gnetif))
{
netif_set_up(&gnetif);
}if (netif_is_link_up(&gnetif)) { ... }
else
{
netif_set_down(&gnetif);
}else { ... }
}{ ... }
/* ... */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == MFX_IRQOUT_PIN)
{
if(BSP_IO_ITGetStatus(MII_INT_PIN))
{
}if (BSP_IO_ITGetStatus(MII_INT_PIN)) { ... }
BSP_IO_ITClear();
}if (GPIO_Pin == MFX_IRQOUT_PIN) { ... }
}{ ... }
/* ... */
void VNC_SERVER_Start (void)
{
if(is_initialized == 0)
{
is_initialized = 1;
tcpip_init(NULL, NULL);
Netif_Config();
if (netif_is_up(&gnetif))
{
VNC_State = VNC_LINK_UP;
VNC_SERVER_StatusChanged(VNC_LINK_UP);
}if (netif_is_up(&gnetif)) { ... }
else
{
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)
{
if (netif_is_up(&gnetif))
{
VNC_State = VNC_LINK_UP;
VNC_SERVER_StatusChanged(VNC_LINK_UP);
}if (netif_is_up(&gnetif)) { ... }
else
{
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) { ... }
if(VNC_ThreadId == 0)
{
osThreadDef(VNC, VNC_Thread, osPriorityAboveNormal, 0, 1024);
VNC_ThreadId = osThreadCreate (osThread(VNC), NULL);
}if (VNC_ThreadId == 0) { ... }
}{ ... }
/* ... */
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) { ... }
}{ ... }
/* ... */
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.");
VNC_State = VNC_LINK_UP;
VNC_SERVER_StatusChanged(VNC_LINK_UP);
netif_set_addr(netif, &ipaddr , &netmask, &gw);
netif_set_up(netif);
}if (netif_is_link_up(netif)) { ... }
else
{
VNC_State = VNC_LINK_DOWN;
netif_set_down(netif);
VNC_SERVER_LogMessage ("Cable is not connected.");
closesocket(_Sock);
}else { ... }
}{ ... }
/* ... */
uint8_t VNC_GetState(void)
{
return VNC_State;
}{ ... }
/* ... */
void VNC_SetState(uint8_t state)
{
VNC_State = state;
}{ ... }
/* ... */
uint8_t VNC_GetLockState(void)
{
return VNC_LockState;
}{ ... }
/* ... */
void VNC_SetLockState(uint8_t LockState)
{
VNC_LockState = LockState;
}{ ... }
/* ... */
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);
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);
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 (;;) { ... }
}{ ... }