Skip to content

Commit 6e8ba51

Browse files
committed
dhcp work
1 parent d5516e3 commit 6e8ba51

File tree

2 files changed

+134
-176
lines changed

2 files changed

+134
-176
lines changed

eth_worker.c

+133-176
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include "eth_save_process.h"
44

55
#include <furi_hal.h>
6-
#include "socket.h"
76
#include "dhcp.h"
87
#include "ping.h"
8+
#include "socket.h"
99
#include "stm32wbxx_hal_gpio.h"
1010
#include "wizchip_conf.h"
1111

@@ -143,7 +143,6 @@ void eth_run(EthWorker* worker, EthWorkerProcess process) {
143143
break;
144144
}
145145
worker->next_state = EthWorkerStateDHCP;
146-
eth_log(EthWorkerProcessDHCP, "Fuck you");
147146
break;
148147
case EthWorkerProcessStatic:
149148
if((uint8_t)worker->state < EthWorkerStateInited) {
@@ -231,13 +230,24 @@ static wiz_NetInfo gWIZNETINFO;
231230
void update_WIZNETINFO(uint8_t is_dhcp) {
232231
furi_assert(static_worker);
233232
memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
234-
memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
235-
memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
236-
memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
237-
memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
238-
gWIZNETINFO.dhcp = is_dhcp ? NETINFO_DHCP : NETINFO_STATIC;
233+
if(is_dhcp) {
234+
memset(gWIZNETINFO.ip, 0, 4);
235+
memset(gWIZNETINFO.sn, 0, 4);
236+
memset(gWIZNETINFO.gw, 0, 4);
237+
memset(gWIZNETINFO.dns, 0, 4);
238+
gWIZNETINFO.dhcp = NETINFO_DHCP;
239+
} else {
240+
memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
241+
memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
242+
memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
243+
memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
244+
gWIZNETINFO.dhcp = NETINFO_STATIC;
245+
}
239246
}
240247

248+
#define DHCP_SOCKET 0
249+
uint8_t ping_auto(uint8_t s, uint8_t* addr);
250+
241251
int32_t eth_worker_task(void* context) {
242252
furi_assert(context);
243253
EthWorker* worker = (EthWorker*)context;
@@ -256,7 +266,7 @@ int32_t eth_worker_task(void* context) {
256266
furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
257267
furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
258268

259-
while(worker->next_state != EthWorkerStateStop) {
269+
while(worker->next_state != EthWorkerStateStop && worker->state != EthWorkerStateStop) {
260270
if(worker->state == EthWorkerStateNotInited) {
261271
if(worker->next_state != EthWorkerStateInit &&
262272
worker->next_state != EthWorkerStateNotInited) {
@@ -283,6 +293,7 @@ int32_t eth_worker_task(void* context) {
283293
wizchip_getnetinfo(&readed_net_info);
284294
if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) {
285295
eth_log(EthWorkerProcessInit, "[error] module not detected");
296+
worker->state = EthWorkerStateNotInited;
286297
continue;
287298
}
288299
setSHAR(gWIZNETINFO.mac);
@@ -310,6 +321,120 @@ int32_t eth_worker_task(void* context) {
310321
}
311322
} else if(worker->state == EthWorkerStateInited) {
312323
if(worker->next_state == EthWorkerStateDHCP) {
324+
worker->state = EthWorkerStateDHCP;
325+
uint8_t temp = PHY_LINK_OFF;
326+
while(temp == PHY_LINK_OFF && worker->state == EthWorkerStateDHCP) {
327+
if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
328+
eth_log(EthWorkerProcessDHCP, "Unknown PHY link status");
329+
}
330+
furi_delay_ms(1);
331+
}
332+
if(worker->state != EthWorkerStateDHCP) {
333+
break;
334+
}
335+
reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
336+
DHCP_init(DHCP_SOCKET, dhcp_buffer);
337+
uint8_t dhcp_ret = DHCP_STOPPED;
338+
uint8_t next_cycle = 1;
339+
while(next_cycle && worker->state == EthWorkerStateDHCP) {
340+
dhcp_ret = DHCP_run();
341+
switch(dhcp_ret) {
342+
case DHCP_IP_ASSIGN:
343+
case DHCP_IP_CHANGED:
344+
case DHCP_IP_LEASED:
345+
getIPfromDHCP(gWIZNETINFO.ip);
346+
getGWfromDHCP(gWIZNETINFO.gw);
347+
getSNfromDHCP(gWIZNETINFO.sn);
348+
getDNSfromDHCP(gWIZNETINFO.dns);
349+
gWIZNETINFO.dhcp = NETINFO_DHCP;
350+
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
351+
eth_log(
352+
EthWorkerProcessDHCP,
353+
"DHCP IP Leased Time : %ld Sec",
354+
getDHCPLeasetime());
355+
break;
356+
case DHCP_FAILED:
357+
eth_log(EthWorkerProcessDHCP, "DHCP Failed");
358+
break;
359+
}
360+
furi_delay_ms(1);
361+
next_cycle = 0;
362+
next_cycle |= dhcp_ret == DHCP_IP_ASSIGN;
363+
next_cycle |= dhcp_ret == DHCP_IP_CHANGED;
364+
next_cycle |= dhcp_ret == DHCP_FAILED;
365+
next_cycle |= dhcp_ret == DHCP_IP_LEASED;
366+
next_cycle != next_cycle;
367+
}
368+
if(worker->state != EthWorkerStateDHCP) {
369+
break;
370+
}
371+
//wizchip_getnetinfo(&gWIZNETINFO);
372+
eth_log(
373+
EthWorkerProcessDHCP,
374+
"IP address : %d.%d.%d.%d",
375+
gWIZNETINFO.ip[0],
376+
gWIZNETINFO.ip[1],
377+
gWIZNETINFO.ip[2],
378+
gWIZNETINFO.ip[3]);
379+
eth_log(
380+
EthWorkerProcessDHCP,
381+
"SM Mask : %d.%d.%d.%d",
382+
gWIZNETINFO.sn[0],
383+
gWIZNETINFO.sn[1],
384+
gWIZNETINFO.sn[2],
385+
gWIZNETINFO.sn[3]);
386+
eth_log(
387+
EthWorkerProcessDHCP,
388+
"Gate way : %d.%d.%d.%d",
389+
gWIZNETINFO.gw[0],
390+
gWIZNETINFO.gw[1],
391+
gWIZNETINFO.gw[2],
392+
gWIZNETINFO.gw[3]);
393+
eth_log(
394+
EthWorkerProcessDHCP,
395+
"DNS Server : %d.%d.%d.%d",
396+
gWIZNETINFO.dns[0],
397+
gWIZNETINFO.dns[1],
398+
gWIZNETINFO.dns[2],
399+
gWIZNETINFO.dns[3]);
400+
worker->state = EthWorkerStateOnline;
401+
}
402+
} else if(worker->state == EthWorkerStateOnline) {
403+
if(worker->next_state == EthWorkerStatePing) {
404+
worker->state = EthWorkerStatePing;
405+
uint8_t* adress = static_worker->config->ping_ip;
406+
eth_log(
407+
EthWorkerProcessDHCP,
408+
"ping %d.%d.%d.%d",
409+
adress[0],
410+
adress[1],
411+
adress[2],
412+
adress[3]);
413+
const uint8_t tryes = 4;
414+
uint8_t try = 0;
415+
while(try < tryes && worker->state == EthWorkerStatePing) {
416+
try++;
417+
uint32_t start_time = furi_get_tick();
418+
uint8_t res = 3; //ping_auto(1, adress);
419+
uint32_t res_time = furi_get_tick();
420+
if(res == 3) {
421+
eth_log(
422+
EthWorkerProcessDHCP, "%d success %d ms", try, res_time - start_time);
423+
} else {
424+
eth_log(
425+
EthWorkerProcessDHCP,
426+
"%d error %d, %d",
427+
try,
428+
res,
429+
res_time - start_time);
430+
break;
431+
}
432+
}
433+
if(worker->state != EthWorkerStatePing) {
434+
break;
435+
}
436+
worker->state = EthWorkerStateOnline;
437+
} else {
313438
}
314439
}
315440
furi_delay_ms(50);
@@ -322,174 +447,6 @@ int32_t eth_worker_task(void* context) {
322447
return 0;
323448
}
324449

325-
// void eth_worker_dhcp(EthWorker* eth_worker) {
326-
// furi_assert(eth_worker);
327-
// furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
328-
329-
// uint8_t temp;
330-
// uint8_t W5500FifoSize[2][8] = {
331-
// {
332-
// 2,
333-
// 2,
334-
// 2,
335-
// 2,
336-
// 2,
337-
// 2,
338-
// 2,
339-
// 2,
340-
// },
341-
// {2, 2, 2, 2, 2, 2, 2, 2}};
342-
343-
// uint8_t dhcp_buffer[2000];
344-
345-
// FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
346-
// FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
347-
348-
// reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
349-
// reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
350-
// reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
351-
352-
// furi_hal_gpio_write(&resetpin, true);
353-
// furi_hal_gpio_write(&cspin, true);
354-
// furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
355-
// furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
356-
357-
// furi_hal_power_enable_otg();
358-
// //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
359-
// furi_delay_ms(1000);
360-
// furi_hal_gpio_write(&resetpin, false);
361-
// furi_delay_ms(10);
362-
// furi_hal_gpio_write(&resetpin, true);
363-
364-
// //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
365-
366-
// if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
367-
// FURI_LOG_I(TAG, "W5500 initialized fail");
368-
// //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
369-
// while(1)
370-
// ;
371-
// //break;
372-
// }
373-
374-
// FURI_LOG_I(TAG, "W5500 initialized success");
375-
// furi_delay_ms(200);
376-
377-
// wizchip_setnetinfo(&gWIZNETINFO);
378-
// FURI_LOG_I(TAG, "W5500 info setted 1");
379-
380-
// setSHAR(gWIZNETINFO.mac);
381-
// FURI_LOG_I(TAG, "W5500 info setted 2");
382-
383-
// //check phy status
384-
// do {
385-
// if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
386-
// FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
387-
// }
388-
// furi_delay_ms(1);
389-
// } while(temp == PHY_LINK_OFF);
390-
391-
// FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
392-
// ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
393-
394-
// furi_delay_ms(1000);
395-
396-
// FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
397-
// reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
398-
399-
// ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
400-
// if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
401-
// DHCP_init(DHCP_SOCKET, dhcp_buffer);
402-
403-
// uint8_t dhcp_ret = DHCP_STOPPED;
404-
405-
// while(
406-
// !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
407-
// (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
408-
// dhcp_ret = DHCP_run();
409-
// switch(dhcp_ret) {
410-
// case DHCP_IP_ASSIGN:
411-
// case DHCP_IP_CHANGED:
412-
// case DHCP_IP_LEASED:
413-
// getIPfromDHCP(gWIZNETINFO.ip);
414-
// getGWfromDHCP(gWIZNETINFO.gw);
415-
// getSNfromDHCP(gWIZNETINFO.sn);
416-
// getDNSfromDHCP(gWIZNETINFO.dns);
417-
// gWIZNETINFO.dhcp = NETINFO_DHCP;
418-
// ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
419-
// FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
420-
// break;
421-
// case DHCP_FAILED:
422-
// FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
423-
// gWIZNETINFO.dhcp = NETINFO_STATIC;
424-
// break;
425-
// }
426-
// furi_delay_ms(1);
427-
// }
428-
429-
// wizchip_getnetinfo(&gWIZNETINFO);
430-
// FURI_LOG_I(
431-
// TAG,
432-
// "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
433-
// gWIZNETINFO.mac[0],
434-
// gWIZNETINFO.mac[1],
435-
// gWIZNETINFO.mac[2],
436-
// gWIZNETINFO.mac[3],
437-
// gWIZNETINFO.mac[4],
438-
// gWIZNETINFO.mac[5]);
439-
// if(gWIZNETINFO.dhcp == NETINFO_DHCP)
440-
// FURI_LOG_I(TAG, "DHCP\n\r");
441-
// else
442-
// FURI_LOG_I(TAG, "Static IP\n\r");
443-
// FURI_LOG_I(
444-
// TAG,
445-
// "IP address : %d.%d.%d.%d\n\r",
446-
// gWIZNETINFO.ip[0],
447-
// gWIZNETINFO.ip[1],
448-
// gWIZNETINFO.ip[2],
449-
// gWIZNETINFO.ip[3]);
450-
// FURI_LOG_I(
451-
// TAG,
452-
// "SM Mask : %d.%d.%d.%d\n\r",
453-
// gWIZNETINFO.sn[0],
454-
// gWIZNETINFO.sn[1],
455-
// gWIZNETINFO.sn[2],
456-
// gWIZNETINFO.sn[3]);
457-
// FURI_LOG_I(
458-
// TAG,
459-
// "Gate way : %d.%d.%d.%d\n\r",
460-
// gWIZNETINFO.gw[0],
461-
// gWIZNETINFO.gw[1],
462-
// gWIZNETINFO.gw[2],
463-
// gWIZNETINFO.gw[3]);
464-
// FURI_LOG_I(
465-
// TAG,
466-
// "DNS Server : %d.%d.%d.%d\n\r",
467-
// gWIZNETINFO.dns[0],
468-
// gWIZNETINFO.dns[1],
469-
// gWIZNETINFO.dns[2],
470-
// gWIZNETINFO.dns[3]);
471-
// ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
472-
// furi_delay_ms(20000);
473-
474-
// uint8_t pDestaddr[4] = {8, 8, 8, 8};
475-
// uint8_t tmp = ping_auto(1, pDestaddr);
476-
// //tmp = ping_count(0,3,pDestaddr);
477-
// if(tmp == SUCCESS) {
478-
// ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
479-
// FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
480-
// } else {
481-
// ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
482-
// FURI_LOG_I(TAG, "----------ERROR = %d----------\r\n", tmp);
483-
// }
484-
// furi_delay_ms(3000);
485-
486-
// furi_delay_ms(2000);
487-
// ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
488-
// }
489-
490-
// furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
491-
// }
492-
493450
static void w5500_init() {
494451
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
495452
uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};

eth_worker.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef enum {
1515
EthWorkerStateMACInit,
1616
EthWorkerStateStaticIp,
1717
EthWorkerStateDHCP,
18+
EthWorkerStateOnline,
1819
EthWorkerStatePing,
1920
EthWorkerStateStop,
2021
} EthWorkerState;

0 commit comments

Comments
 (0)