karasevIA 2 лет назад
Родитель
Сommit
d5516e394e
1 измененных файлов с 178 добавлено и 203 удалено
  1. 178 203
      eth_worker.c

+ 178 - 203
eth_worker.c

@@ -228,14 +228,14 @@ static void wait_ms(int ms) {
 }
 
 static wiz_NetInfo gWIZNETINFO;
-void update_WIZNETINFO() {
+void update_WIZNETINFO(uint8_t is_dhcp) {
     furi_assert(static_worker);
     memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
     memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
     memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
     memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
     memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
-    gWIZNETINFO.dhcp = NETINFO_STATIC;
+    gWIZNETINFO.dhcp = is_dhcp ? NETINFO_DHCP : NETINFO_STATIC;
 }
 
 int32_t eth_worker_task(void* context) {
@@ -277,8 +277,14 @@ int32_t eth_worker_task(void* context) {
                 }
                 eth_log(EthWorkerProcessInit, "W5500 inited");
                 furi_delay_ms(50);
-                update_WIZNETINFO();
+                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");
+                    continue;
+                }
                 setSHAR(gWIZNETINFO.mac);
                 wiz_PhyConf conf;
                 wizphy_getphyconf(&conf);
@@ -303,6 +309,8 @@ int32_t eth_worker_task(void* context) {
                 continue;
             }
         } else if(worker->state == EthWorkerStateInited) {
+            if(worker->next_state == EthWorkerStateDHCP) {
+            }
         }
         furi_delay_ms(50);
     }
@@ -314,206 +322,173 @@ int32_t eth_worker_task(void* context) {
     return 0;
 }
 
-#define DHCP_SOCKET 0
-#define PING_SOCKET 1
-
-// MAC-àäðåñ
-#define ETHADDR0 0x00 // The first octet of the Ethernet address
-#define ETHADDR1 0x08 // The second octet of the Ethernet address
-#define ETHADDR2 0xDC // The third octet of the Ethernet address
-#define ETHADDR3 0x47 // The fourth octet of the Ethernet address
-#define ETHADDR4 0x47 // The fifth octet of the Ethernet address
-#define ETHADDR5 0x54 // The sixth octet of the Ethernet address
-// Ïàðàìåòðû IP (åñëè DHCP îòêëþ÷åí)
-#define IPADDR0 192 // The first octet of the IP address of this uIP node
-#define IPADDR1 168 // The second octet of the IP address of this uIP node
-#define IPADDR2 1 // The third octet of the IP address of this uIP node
-#define IPADDR3 137 // The fourth octet of the IP address of this uIP node
-#define NETMASK0 255 // The first octet of the netmask of this uIP node
-#define NETMASK1 255 // The second octet of the netmask of this uIP node
-#define NETMASK2 255 // The third octet of the netmask of this uIP node
-#define NETMASK3 0 // The fourth octet of the netmask of this uIP node
-#define DRIPADDR0 192 // The first octet of the IP address of the default router
-#define DRIPADDR1 168 // The second octet of the IP address of the default router
-#define DRIPADDR2 1 // The third octet of the IP address of the default router
-#define DRIPADDR3 1 // The fourth octet of the IP address of the default router
-
-// wiz_NetInfo gWIZNETINFO = {
-//     .mac = {ETHADDR0, ETHADDR1, ETHADDR2, ETHADDR3, ETHADDR4, ETHADDR5},
-//     .ip = {IPADDR0, IPADDR1, IPADDR2, IPADDR3},
-//     .sn = {NETMASK0, NETMASK1, NETMASK2, NETMASK3},
-//     .gw = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
-//     .dns = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
-//     .dhcp = NETINFO_DHCP // NETINFO_STATIC
-// };
-
-void eth_worker_dhcp(EthWorker* eth_worker) {
-    furi_assert(eth_worker);
-    furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
-
-    uint8_t temp;
-    uint8_t W5500FifoSize[2][8] = {
-        {
-            2,
-            2,
-            2,
-            2,
-            2,
-            2,
-            2,
-            2,
-        },
-        {2, 2, 2, 2, 2, 2, 2, 2}};
-
-    uint8_t dhcp_buffer[2000];
-
-    FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
-    FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
-
-    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_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_hal_power_enable_otg();
-    //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
-    furi_delay_ms(1000);
-    furi_hal_gpio_write(&resetpin, false);
-    furi_delay_ms(10);
-    furi_hal_gpio_write(&resetpin, true);
-
-    //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
-
-    if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
-        FURI_LOG_I(TAG, "W5500 initialized fail");
-        //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
-        while(1)
-            ;
-        //break;
-    }
-
-    FURI_LOG_I(TAG, "W5500 initialized success");
-    furi_delay_ms(200);
-
-    wizchip_setnetinfo(&gWIZNETINFO);
-    FURI_LOG_I(TAG, "W5500 info setted 1");
-
-    setSHAR(gWIZNETINFO.mac);
-    FURI_LOG_I(TAG, "W5500 info setted 2");
-
-    //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_delay_ms(1000);
-
-    FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
-    reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
-
-    ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
-    if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
-        DHCP_init(DHCP_SOCKET, dhcp_buffer);
-
-        uint8_t dhcp_ret = DHCP_STOPPED;
-
-        while(
-            !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
-              (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
-            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);
-                FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
-                break;
-            case DHCP_FAILED:
-                FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
-                gWIZNETINFO.dhcp = NETINFO_STATIC;
-                break;
-            }
-            furi_delay_ms(1);
-        }
-
-        wizchip_getnetinfo(&gWIZNETINFO);
-        FURI_LOG_I(
-            TAG,
-            "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
-            gWIZNETINFO.mac[0],
-            gWIZNETINFO.mac[1],
-            gWIZNETINFO.mac[2],
-            gWIZNETINFO.mac[3],
-            gWIZNETINFO.mac[4],
-            gWIZNETINFO.mac[5]);
-        if(gWIZNETINFO.dhcp == NETINFO_DHCP)
-            FURI_LOG_I(TAG, "DHCP\n\r");
-        else
-            FURI_LOG_I(TAG, "Static IP\n\r");
-        FURI_LOG_I(
-            TAG,
-            "IP address : %d.%d.%d.%d\n\r",
-            gWIZNETINFO.ip[0],
-            gWIZNETINFO.ip[1],
-            gWIZNETINFO.ip[2],
-            gWIZNETINFO.ip[3]);
-        FURI_LOG_I(
-            TAG,
-            "SM Mask    : %d.%d.%d.%d\n\r",
-            gWIZNETINFO.sn[0],
-            gWIZNETINFO.sn[1],
-            gWIZNETINFO.sn[2],
-            gWIZNETINFO.sn[3]);
-        FURI_LOG_I(
-            TAG,
-            "Gate way   : %d.%d.%d.%d\n\r",
-            gWIZNETINFO.gw[0],
-            gWIZNETINFO.gw[1],
-            gWIZNETINFO.gw[2],
-            gWIZNETINFO.gw[3]);
-        FURI_LOG_I(
-            TAG,
-            "DNS Server : %d.%d.%d.%d\n\r",
-            gWIZNETINFO.dns[0],
-            gWIZNETINFO.dns[1],
-            gWIZNETINFO.dns[2],
-            gWIZNETINFO.dns[3]);
-        ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
-        furi_delay_ms(20000);
-
-        uint8_t pDestaddr[4] = {8, 8, 8, 8};
-        uint8_t tmp = ping_auto(1, pDestaddr);
-        //tmp = ping_count(0,3,pDestaddr);
-        if(tmp == SUCCESS) {
-            ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
-            FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
-        } else {
-            ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
-            FURI_LOG_I(TAG, "----------ERROR  = %d----------\r\n", tmp);
-        }
-        furi_delay_ms(3000);
-
-        furi_delay_ms(2000);
-        ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
-    }
-
-    furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
-}
+// void eth_worker_dhcp(EthWorker* eth_worker) {
+//     furi_assert(eth_worker);
+//     furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
+
+//     uint8_t temp;
+//     uint8_t W5500FifoSize[2][8] = {
+//         {
+//             2,
+//             2,
+//             2,
+//             2,
+//             2,
+//             2,
+//             2,
+//             2,
+//         },
+//         {2, 2, 2, 2, 2, 2, 2, 2}};
+
+//     uint8_t dhcp_buffer[2000];
+
+//     FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
+//     FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
+
+//     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_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_hal_power_enable_otg();
+//     //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
+//     furi_delay_ms(1000);
+//     furi_hal_gpio_write(&resetpin, false);
+//     furi_delay_ms(10);
+//     furi_hal_gpio_write(&resetpin, true);
+
+//     //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
+
+//     if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
+//         FURI_LOG_I(TAG, "W5500 initialized fail");
+//         //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
+//         while(1)
+//             ;
+//         //break;
+//     }
+
+//     FURI_LOG_I(TAG, "W5500 initialized success");
+//     furi_delay_ms(200);
+
+//     wizchip_setnetinfo(&gWIZNETINFO);
+//     FURI_LOG_I(TAG, "W5500 info setted 1");
+
+//     setSHAR(gWIZNETINFO.mac);
+//     FURI_LOG_I(TAG, "W5500 info setted 2");
+
+//     //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_delay_ms(1000);
+
+//     FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
+//     reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
+
+//     ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
+//     if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
+//         DHCP_init(DHCP_SOCKET, dhcp_buffer);
+
+//         uint8_t dhcp_ret = DHCP_STOPPED;
+
+//         while(
+//             !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
+//               (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
+//             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);
+//                 FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
+//                 break;
+//             case DHCP_FAILED:
+//                 FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
+//                 gWIZNETINFO.dhcp = NETINFO_STATIC;
+//                 break;
+//             }
+//             furi_delay_ms(1);
+//         }
+
+//         wizchip_getnetinfo(&gWIZNETINFO);
+//         FURI_LOG_I(
+//             TAG,
+//             "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
+//             gWIZNETINFO.mac[0],
+//             gWIZNETINFO.mac[1],
+//             gWIZNETINFO.mac[2],
+//             gWIZNETINFO.mac[3],
+//             gWIZNETINFO.mac[4],
+//             gWIZNETINFO.mac[5]);
+//         if(gWIZNETINFO.dhcp == NETINFO_DHCP)
+//             FURI_LOG_I(TAG, "DHCP\n\r");
+//         else
+//             FURI_LOG_I(TAG, "Static IP\n\r");
+//         FURI_LOG_I(
+//             TAG,
+//             "IP address : %d.%d.%d.%d\n\r",
+//             gWIZNETINFO.ip[0],
+//             gWIZNETINFO.ip[1],
+//             gWIZNETINFO.ip[2],
+//             gWIZNETINFO.ip[3]);
+//         FURI_LOG_I(
+//             TAG,
+//             "SM Mask    : %d.%d.%d.%d\n\r",
+//             gWIZNETINFO.sn[0],
+//             gWIZNETINFO.sn[1],
+//             gWIZNETINFO.sn[2],
+//             gWIZNETINFO.sn[3]);
+//         FURI_LOG_I(
+//             TAG,
+//             "Gate way   : %d.%d.%d.%d\n\r",
+//             gWIZNETINFO.gw[0],
+//             gWIZNETINFO.gw[1],
+//             gWIZNETINFO.gw[2],
+//             gWIZNETINFO.gw[3]);
+//         FURI_LOG_I(
+//             TAG,
+//             "DNS Server : %d.%d.%d.%d\n\r",
+//             gWIZNETINFO.dns[0],
+//             gWIZNETINFO.dns[1],
+//             gWIZNETINFO.dns[2],
+//             gWIZNETINFO.dns[3]);
+//         ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
+//         furi_delay_ms(20000);
+
+//         uint8_t pDestaddr[4] = {8, 8, 8, 8};
+//         uint8_t tmp = ping_auto(1, pDestaddr);
+//         //tmp = ping_count(0,3,pDestaddr);
+//         if(tmp == SUCCESS) {
+//             ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
+//             FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
+//         } else {
+//             ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
+//             FURI_LOG_I(TAG, "----------ERROR  = %d----------\r\n", tmp);
+//         }
+//         furi_delay_ms(3000);
+
+//         furi_delay_ms(2000);
+//         ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
+//     }
+
+//     furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
+// }
 
 static void w5500_init() {
     furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);