ソースを参照

add: hostname有默认值,也支持自由设置

Wendal Chen 2 年 前
コミット
9f1119fe59
2 ファイル変更25 行追加7 行削除
  1. 22 3
      app/network/luat_wlan_air101.c
  2. 3 4
      src/network/lwip2.1.3/netif/ethernetif.c

+ 22 - 3
app/network/luat_wlan_air101.c

@@ -30,6 +30,8 @@ void net_lwip_set_link_state(uint8_t adapter_index, uint8_t updown);
 static int wlan_init;
 static int wlan_state;
 
+char luat_sta_hostname[32];
+
 static int l_wlan_cb(lua_State*L, void* ptr) {
     u8 ssid[33]= {0};
     u8 pwd[65] = {0};
@@ -111,6 +113,7 @@ static void scan_event_cb(void) {
 
 int luat_wlan_init(luat_wlan_config_t *conf) {
     if (wlan_init == 0) {
+        luat_wlan_get_hostname(0); // 调用一下就行
         wlan_init = 1;
         tls_netif_add_status_event(netif_event_cb);
         tls_wifi_scan_result_cb_register(scan_event_cb);
@@ -292,12 +295,28 @@ int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo2) {
     // ----------------------------
     // 这部分有必要不?? 拿不准
     // ----------------------------
-    u8 mac[6] = {0};
-    tls_get_mac_addr(mac);
-    sprintf_(ipinfo.dnsname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+    // u8 mac[6] = {0};
+    // tls_get_mac_addr(mac);
+    // sprintf_(ipinfo.dnsname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
     //------------------------------
 
     ret = tls_wifi_softap_create(&apinfo, &ipinfo);
     LLOGD("tls_wifi_softap_create %d", ret);
     return ret;
 }
+
+const char* luat_wlan_get_hostname(int id) {
+    if (luat_sta_hostname[0] == 0) {
+        u8* mac_addr = wpa_supplicant_get_mac();
+        sprintf_(luat_sta_hostname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
+    }
+    return (const char*)luat_sta_hostname;
+}
+
+int luat_wlan_set_hostname(int id, char* hostname) {
+    if (hostname == NULL || hostname[0] == 0) {
+        return 0;
+    }
+    memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
+    return 0;
+}

+ 3 - 4
src/network/lwip2.1.3/netif/ethernetif.c

@@ -354,7 +354,7 @@ static err_t ethernetif_igmp_mac_filter(struct netif *netif,
 }
 #endif
 
-static char host_name[64] = {0};
+extern char luat_sta_hostname[];
 /**
  * Should be called at the beginning of the program to set up the
  * network interface. It calls the function low_level_init() to do the
@@ -371,11 +371,10 @@ err_t
 ethernetif_init(struct netif *netif)
 {
   LWIP_ASSERT("netif != NULL", (netif != NULL));
-    u8* mac_addr = wpa_supplicant_get_mac();
+    
 #if LWIP_NETIF_HOSTNAME
 	/* Initialize interface hostname */
-    sprintf(host_name, "LUATOS_%02X%02X%02X%02X%02X%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
-	netif->hostname = host_name;
+	netif->hostname = luat_sta_hostname;
 #endif /* LWIP_NETIF_HOSTNAME */
 
   /*