diff --git a/eth_worker.c b/eth_worker.c index cb7932d8e4b..4768e14ed24 100644 --- a/eth_worker.c +++ b/eth_worker.c @@ -119,6 +119,43 @@ void eth_log(EthWorkerProcess process, const char* format, ...) { furi_string_free(fstring); } +static void eth_set_force_state(EthWorkerState state) { + EthWorker* worker = static_worker; + furi_assert(worker); + worker->next_state = EthWorkerStateDefaultNext; + if(worker->state == EthWorkerStateNotAllocated) { + return; + } + worker->state = state; +} + +void eth_set_state() { +} + +void eth_set_next_state(EthWorkerState state) { + EthWorker* worker = static_worker; + furi_assert(worker); + if(state == EthWorkerStateReset || state == EthWorkerStateStop) { + eth_set_force_state(state); + return; + } + if(worker->state == EthWorkerStateNotInited) { + if(state == EthWorkerStateInit) { + worker->next_state = state; + } + } + if(worker->state == EthWorkerStateInited) { + if(state == EthWorkerStateDHCP || state == EthWorkerStateStaticIp) { + worker->next_state = state; + } + } + if(worker->state == EthWorkerStateOnline) { + if(state == EthWorkerStatePing) { + worker->next_state = state; + } + } +} + void eth_run(EthWorker* worker, EthWorkerProcess process) { furi_assert(worker); switch(process) { @@ -130,45 +167,30 @@ void eth_run(EthWorker* worker, EthWorkerProcess process) { furi_thread_set_callback(worker->thread, eth_worker_task); furi_thread_set_context(worker->thread, worker); worker->state = EthWorkerStateNotInited; - worker->next_state = EthWorkerStateInit; furi_thread_start(worker->thread); } - worker->state = EthWorkerStateNotInited; - worker->next_state = EthWorkerStateInit; + eth_set_next_state(EthWorkerStateInit); break; case EthWorkerProcessDHCP: - if((uint8_t)worker->state < EthWorkerStateInited) { - eth_log(EthWorkerProcessDHCP, "[error] module not inited"); - break; - } - worker->next_state = EthWorkerStateDHCP; + eth_set_next_state(EthWorkerStateDHCP); break; case EthWorkerProcessStatic: - if((uint8_t)worker->state < EthWorkerStateInited) { - eth_log(EthWorkerProcessStatic, "[error] module not inited"); - break; - } - worker->next_state = EthWorkerStateStaticIp; - eth_log(EthWorkerProcessStatic, "Fuck you"); + eth_set_next_state(EthWorkerStateStaticIp); break; case EthWorkerProcessPing: - if((uint8_t)worker->state < EthWorkerStateInited) { - eth_log(EthWorkerProcessPing, "[error] module not inited"); - break; - } - worker->next_state = EthWorkerStatePing; + eth_set_next_state(EthWorkerStatePing); break; case EthWorkerProcessReset: - worker->next_state = EthWorkerStateNotInited; - if((uint8_t)worker->state > EthWorkerStateNotAllocated) { - worker->state = EthWorkerStateNotInited; - } + eth_set_next_state(EthWorkerStateReset); + eth_log(EthWorkerProcessInit, "reset module"); + eth_log(EthWorkerProcessDHCP, "reset module"); + eth_log(EthWorkerProcessStatic, "reset module"); + eth_log(EthWorkerProcessPing, "reset module"); eth_log(EthWorkerProcessReset, "reset module"); break; case EthWorkerProcessExit: if(worker->state != EthWorkerStateNotAllocated) { - worker->next_state = EthWorkerStateStop; - worker->state = EthWorkerStateStop; + eth_set_force_state(EthWorkerStateStop); furi_thread_join(worker->thread); furi_thread_free(worker->thread); worker->state = EthWorkerStateNotAllocated; @@ -296,177 +318,169 @@ int32_t eth_worker_task(void* context) { furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); - while(worker->next_state != EthWorkerStateStop && worker->state != EthWorkerStateStop) { + while(worker->state != EthWorkerStateStop) { if(worker->state == EthWorkerStateNotInited) { - if(worker->next_state != EthWorkerStateInit && - worker->next_state != EthWorkerStateNotInited) { - eth_log(EthWorkerProcessActive, "[error] try using not inited module"); - worker->next_state = EthWorkerStateNotInited; - } if(worker->next_state == EthWorkerStateInit) { - worker->state = EthWorkerStateInit; - furi_hal_power_enable_otg(); - furi_delay_ms(300); - furi_hal_gpio_write(&resetpin, false); - furi_delay_ms(50); - furi_hal_gpio_write(&resetpin, true); - if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) { - eth_log(EthWorkerProcessInit, "[error] W5500 init fail"); - worker->state = worker->next_state = EthWorkerStateNotInited; - continue; - } - eth_log(EthWorkerProcessInit, "W5500 inited"); - furi_delay_ms(50); - update_WIZNETINFO(false); - wizchip_setnetinfo(&gWIZNETINFO); - wiz_NetInfo readed_net_info; - wizchip_getnetinfo(&readed_net_info); - if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) { - eth_log(EthWorkerProcessInit, "[error] module not detected"); - worker->state = EthWorkerStateNotInited; - continue; - } - setSHAR(gWIZNETINFO.mac); - wiz_PhyConf conf; - wizphy_getphyconf(&conf); - eth_log( - EthWorkerProcessInit, - "conf %d %d %d %d", - conf.by, - conf.mode, - conf.speed, - conf.duplex); - eth_log(EthWorkerProcessInit, "net info setted"); - eth_log( - EthWorkerProcessInit, - "mac: %02X-%02X-%02X-%02X-%02X-%02X", - gWIZNETINFO.mac[0], - gWIZNETINFO.mac[1], - gWIZNETINFO.mac[2], - gWIZNETINFO.mac[3], - gWIZNETINFO.mac[4], - gWIZNETINFO.mac[5]); - worker->state = EthWorkerStateInited; - continue; + worker->state = worker->next_state; } } else if(worker->state == EthWorkerStateInited) { - if(worker->next_state == EthWorkerStateDHCP) { - worker->state = EthWorkerStateDHCP; - if(!check_phylink(worker, EthWorkerStateDHCP, EthWorkerProcessDHCP, 5000)) { - worker->state = EthWorkerStateInited; - continue; - } - reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict); - DHCP_init(DHCP_SOCKET, dhcp_buffer); - uint8_t next_cycle = 1; - uint8_t divider = 0; - while(next_cycle && worker->state == EthWorkerStateDHCP) { - uint8_t dhcp_ret = DHCP_run(); - switch(dhcp_ret) { - case DHCP_IP_ASSIGN: - case DHCP_IP_CHANGED: - case DHCP_IP_LEASED: - getIPfromDHCP(gWIZNETINFO.ip); - getGWfromDHCP(gWIZNETINFO.gw); - getSNfromDHCP(gWIZNETINFO.sn); - getDNSfromDHCP(gWIZNETINFO.dns); - gWIZNETINFO.dhcp = NETINFO_DHCP; - ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); - eth_log( - EthWorkerProcessDHCP, - "DHCP IP Leased Time : %ld Sec", - getDHCPLeasetime()); - break; - case DHCP_FAILED: - eth_log(EthWorkerProcessDHCP, "DHCP Failed"); - break; - } - furi_delay_ms(100); - if(divider++ % 10 == 0) { - eth_log(EthWorkerProcessDHCP, "DHCP process %d", divider / 10); - if(divider > 250) { - DHCP_stop(); - eth_log(EthWorkerProcessDHCP, "DHCP Stop by timer"); - worker->state = EthWorkerStateInited; - break; - } - } - next_cycle = (dhcp_ret == DHCP_RUNNING); - } - if(worker->state != EthWorkerStateDHCP) { - break; - } - //wizchip_getnetinfo(&gWIZNETINFO); - eth_log( - EthWorkerProcessDHCP, - "IP address:\n %d.%d.%d.%d", - gWIZNETINFO.ip[0], - gWIZNETINFO.ip[1], - gWIZNETINFO.ip[2], - gWIZNETINFO.ip[3]); - eth_log( - EthWorkerProcessDHCP, - "SM Mask:\n %d.%d.%d.%d", - gWIZNETINFO.sn[0], - gWIZNETINFO.sn[1], - gWIZNETINFO.sn[2], - gWIZNETINFO.sn[3]); - eth_log( - EthWorkerProcessDHCP, - "Gate way:\n %d.%d.%d.%d", - gWIZNETINFO.gw[0], - gWIZNETINFO.gw[1], - gWIZNETINFO.gw[2], - gWIZNETINFO.gw[3]); - eth_log( - EthWorkerProcessDHCP, - "DNS Server:\n %d.%d.%d.%d", - gWIZNETINFO.dns[0], - gWIZNETINFO.dns[1], - gWIZNETINFO.dns[2], - gWIZNETINFO.dns[3]); - worker->state = EthWorkerStateOnline; + if(worker->next_state == EthWorkerStateDHCP || + worker->next_state == EthWorkerStateStaticIp) { + worker->state = worker->next_state; } } else if(worker->state == EthWorkerStateOnline) { if(worker->next_state == EthWorkerStatePing) { - worker->state = EthWorkerStatePing; - uint8_t* adress = static_worker->config->ping_ip; - eth_log( - EthWorkerProcessPing, - "ping %d.%d.%d.%d", - adress[0], - adress[1], - adress[2], - adress[3]); - const uint8_t tryes = 4; - uint8_t try = 0; - while(try < tryes && worker->state == EthWorkerStatePing) { - try++; - uint32_t start_time = furi_get_tick(); - uint8_t res = ping_auto_interface(adress); - uint32_t res_time = furi_get_tick(); - if(res == 3) { - eth_log( - EthWorkerProcessPing, "%d success %d ms", try, res_time - start_time); - } else { - eth_log( - EthWorkerProcessPing, - "%d error %d, %d", - try, - res, - res_time - start_time); + worker->state = worker->next_state; + } + } else if(worker->state == EthWorkerStateReset) { + worker->state = EthWorkerStateNotInited; + } + + if(worker->state == EthWorkerStateInit) { + furi_hal_power_enable_otg(); + furi_delay_ms(300); + furi_hal_gpio_write(&resetpin, false); + furi_delay_ms(50); + furi_hal_gpio_write(&resetpin, true); + if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) { + eth_log(EthWorkerProcessInit, "[error] W5500 init fail"); + eth_set_force_state(EthWorkerStateNotInited); + continue; + } + eth_log(EthWorkerProcessInit, "W5500 inited"); + furi_delay_ms(50); + update_WIZNETINFO(false); + wizchip_setnetinfo(&gWIZNETINFO); + wiz_NetInfo readed_net_info; + wizchip_getnetinfo(&readed_net_info); + if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) { + eth_log(EthWorkerProcessInit, "[error] module not detected"); + eth_set_force_state(EthWorkerStateNotInited); + continue; + } + setSHAR(gWIZNETINFO.mac); + wiz_PhyConf conf; + wizphy_getphyconf(&conf); + eth_log( + EthWorkerProcessInit, + "conf %d %d %d %d", + conf.by, + conf.mode, + conf.speed, + conf.duplex); + eth_log(EthWorkerProcessInit, "net info setted"); + eth_log( + EthWorkerProcessInit, + "mac: %02X-%02X-%02X-%02X-%02X-%02X", + gWIZNETINFO.mac[0], + gWIZNETINFO.mac[1], + gWIZNETINFO.mac[2], + gWIZNETINFO.mac[3], + gWIZNETINFO.mac[4], + gWIZNETINFO.mac[5]); + eth_set_force_state(EthWorkerStateInited); + } else if(worker->state == EthWorkerStateDHCP) { + if(!check_phylink(worker, EthWorkerStateDHCP, EthWorkerProcessDHCP, 5000)) { + worker->state = EthWorkerStateInited; + continue; + } + reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict); + DHCP_init(DHCP_SOCKET, dhcp_buffer); + uint8_t next_cycle = 1; + uint8_t divider = 0; + while(next_cycle && worker->state == EthWorkerStateDHCP) { + uint8_t dhcp_ret = DHCP_run(); + switch(dhcp_ret) { + case DHCP_IP_ASSIGN: + case DHCP_IP_CHANGED: + case DHCP_IP_LEASED: + getIPfromDHCP(gWIZNETINFO.ip); + getGWfromDHCP(gWIZNETINFO.gw); + getSNfromDHCP(gWIZNETINFO.sn); + getDNSfromDHCP(gWIZNETINFO.dns); + gWIZNETINFO.dhcp = NETINFO_DHCP; + ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); + eth_log( + EthWorkerProcessDHCP, "DHCP IP Leased Time : %ld Sec", getDHCPLeasetime()); + break; + case DHCP_FAILED: + eth_log(EthWorkerProcessDHCP, "DHCP Failed"); + break; + } + furi_delay_ms(100); + if(divider++ % 10 == 0) { + eth_log(EthWorkerProcessDHCP, "DHCP process %d", divider / 10); + if(divider > 250) { + DHCP_stop(); + eth_log(EthWorkerProcessDHCP, "DHCP Stop by timer"); + eth_set_force_state(EthWorkerStateInited); break; } } - if(worker->state != EthWorkerStatePing) { + next_cycle = (dhcp_ret == DHCP_RUNNING); + } + if(worker->state != EthWorkerStateDHCP) { + continue; + } + eth_log( + EthWorkerProcessDHCP, + "IP address:\n %d.%d.%d.%d", + gWIZNETINFO.ip[0], + gWIZNETINFO.ip[1], + gWIZNETINFO.ip[2], + gWIZNETINFO.ip[3]); + eth_log( + EthWorkerProcessDHCP, + "SM Mask:\n %d.%d.%d.%d", + gWIZNETINFO.sn[0], + gWIZNETINFO.sn[1], + gWIZNETINFO.sn[2], + gWIZNETINFO.sn[3]); + eth_log( + EthWorkerProcessDHCP, + "Gate way:\n %d.%d.%d.%d", + gWIZNETINFO.gw[0], + gWIZNETINFO.gw[1], + gWIZNETINFO.gw[2], + gWIZNETINFO.gw[3]); + eth_log( + EthWorkerProcessDHCP, + "DNS Server:\n %d.%d.%d.%d", + gWIZNETINFO.dns[0], + gWIZNETINFO.dns[1], + gWIZNETINFO.dns[2], + gWIZNETINFO.dns[3]); + eth_set_force_state(EthWorkerStateOnline); + } else if(worker->state == EthWorkerStatePing) { + uint8_t* adress = worker->config->ping_ip; + eth_log( + EthWorkerProcessPing, + "ping %d.%d.%d.%d", + adress[0], + adress[1], + adress[2], + adress[3]); + const uint8_t tryes = 4; + uint8_t try = 0; + while(try < tryes && worker->state == EthWorkerStatePing) { + try++; + uint32_t start_time = furi_get_tick(); + uint8_t res = ping_auto_interface(adress); + uint32_t res_time = furi_get_tick(); + if(res == 3) { + eth_log(EthWorkerProcessPing, "%d success %d ms", try, res_time - start_time); + } else { + eth_log( + EthWorkerProcessPing, "%d error %d, %d", try, res, res_time - start_time); break; } - worker->state = EthWorkerStateOnline; - worker->next_state = EthWorkerStateDefaultNext; - } else { } + if(worker->state != EthWorkerStatePing) { + break; + } + eth_set_force_state(EthWorkerStateOnline); } - furi_delay_ms(50); + furi_delay_ms(5); } furi_hal_spi_release(&furi_hal_spi_bus_handle_external); @@ -475,105 +489,3 @@ int32_t eth_worker_task(void* context) { return 0; } - -static void w5500_init() { - furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external); - uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}}; - FURI_LOG_I(TAG, "Registering W5500 callbacks"); - - reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte); - reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff); - reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect); - - GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3}; - furi_hal_gpio_write(&resetpin, true); - furi_hal_gpio_write(&cspin, true); - furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); - furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); -} - -static void w5500_deinit() { - furi_hal_spi_release(&furi_hal_spi_bus_handle_external); -} - -void eth_worker_w5500(EthWorker* eth_worker) { - furi_assert(eth_worker); - - //uint8_t temp; - - FURI_LOG_I(TAG, "Ehtping_Init"); - furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external); - uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}}; - FURI_LOG_I(TAG, "Registering W5500 callbacks"); - - reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte); - reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff); - reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect); - - FURI_LOG_I(TAG, "Registered W5500 callbacks"); - - GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3}; - furi_hal_gpio_write(&resetpin, true); - furi_hal_gpio_write(&cspin, true); - furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); - furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh); - - FURI_LOG_I(TAG, "GPIO inited"); - - furi_hal_power_enable_otg(); - furi_delay_ms(1000); - //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context); - furi_delay_ms(2000); - furi_hal_gpio_write(&resetpin, false); - furi_delay_ms(10); - furi_hal_gpio_write(&resetpin, true); - - FURI_LOG_I(TAG, "GPIO used"); - - //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context); - - if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) { - FURI_LOG_I(TAG, "W5500 initialized fail.\r\n"); - //eth_worker->callback(EthCustomEventModuleError, eth_worker->context); - } - - FURI_LOG_I(TAG, "W5500 initialized success.\r\n"); - furi_delay_ms(2000); - - wizchip_setnetinfo(&gWIZNETINFO); - FURI_LOG_I(TAG, "W5500 info setted 1.\r\n"); - - setSHAR(gWIZNETINFO.mac); - FURI_LOG_I(TAG, "W5500 info setted 2.\r\n"); - - //check phy status - //do - //{ - // if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) - // { - // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n"); - // } - // furi_delay_ms(1); - //} while (temp == PHY_LINK_OFF); - - //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n"); - ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context); - - FURI_LOG_I(TAG, "W5500 before delay\r\n"); - - furi_delay_ms(2000); - - FURI_LOG_I(TAG, "W5500 after delay\r\n"); - - //furi_hal_power_disable_otg(); - - //FURI_LOG_I(TAG, "W5500 power off\r\n"); - - ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context); - - furi_delay_ms(2000); - - ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context); - - furi_hal_spi_release(&furi_hal_spi_bus_handle_external); -} diff --git a/eth_worker.h b/eth_worker.h index d1b7f04f53f..42c8738affd 100644 --- a/eth_worker.h +++ b/eth_worker.h @@ -19,6 +19,7 @@ typedef enum { EthWorkerStateOnline, EthWorkerStatePing, EthWorkerStateStop, + EthWorkerStateReset, } EthWorkerState; typedef enum { diff --git a/lib/ioLibrary_Driver/Internet/ICMP/ping.c b/lib/ioLibrary_Driver/Internet/ICMP/ping.c index e08e2a71a17..66227df4632 100755 --- a/lib/ioLibrary_Driver/Internet/ICMP/ping.c +++ b/lib/ioLibrary_Driver/Internet/ICMP/ping.c @@ -101,8 +101,7 @@ uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t* addr) { /* Check socket register */ while(getSn_SR(s) != SOCK_IPRAW) ; - ping_wait_ms(1000); // wait 1000ms - ping_wait_ms(1000); // wait 1000ms + ping_wait_ms(1000); break; case SOCK_IPRAW: ping_request(s, addr); @@ -120,7 +119,7 @@ uint8_t ping_count(uint8_t s, uint16_t pCount, uint8_t* addr) { break; } else { cnt++; - ping_wait_ms(50); // wait 50ms + ping_wait_ms(20); } } break;