luat_wlan_air101.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_wlan.h"
  5. #include <string.h>
  6. #include "wm_irq.h"
  7. #include "tls_sys.h"
  8. #include "wm_ram_config.h"
  9. #include "wm_efuse.h"
  10. #include "wm_regs.h"
  11. #include "wm_wifi.h"
  12. #include "wm_netif.h"
  13. #include "wm_debug.h"
  14. #define LUAT_LOG_TAG "wlan"
  15. #include "luat_log.h"
  16. #include "lwip/netif.h"
  17. #include "luat_network_adapter.h"
  18. #include "luat_timer.h"
  19. #include "net_lwip2.h"
  20. #include "lwip/tcp.h"
  21. void net_lwip2_set_link_state(uint8_t adapter_index, uint8_t updown);
  22. int luat_wlan_raw_in(const u8 *bssid, u8 *buf, u32 buf_len);
  23. #define SCAN_DONE (0x73)
  24. #define ONESHOT_RESULT (0x74)
  25. static int wlan_init;
  26. static int wlan_state;
  27. char luat_sta_hostname[32];
  28. extern const char WiFiVer[];
  29. extern u8 tx_gain_group[];
  30. extern void *tls_wl_init(u8 *tx_gain, u8 *mac_addr, u8 *hwver);
  31. extern int wpa_supplicant_init(u8 *mac_addr);
  32. extern void tls_pmu_chipsleep_callback(int sleeptime);
  33. static int l_wlan_cb(lua_State*L, void* ptr) {
  34. (void)ptr;
  35. u8 ssid[33]= {0};
  36. u8 pwd[65] = {0};
  37. char sta_ip[16] = {0};
  38. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  39. lua_getglobal(L, "sys_pub");
  40. switch (msg->arg1)
  41. {
  42. case NETIF_IP_NET_UP:
  43. #ifdef LUAT_USE_NETWORK
  44. net_lwip2_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 1);
  45. luat_wlan_get_ip(0, sta_ip);
  46. LLOGD("sta ip %s", sta_ip);
  47. lua_pushstring(L, "IP_READY");
  48. lua_pushstring(L, sta_ip);
  49. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  50. lua_call(L, 3, 0);
  51. #endif
  52. break;
  53. case NETIF_WIFI_DISCONNECTED:
  54. #ifdef LUAT_USE_NETWORK
  55. net_lwip2_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 0);
  56. lua_pushstring(L, "IP_LOSE");
  57. lua_call(L, 1, 0); // 暂时只发个IP_LOSE
  58. #endif
  59. break;
  60. case SCAN_DONE :
  61. lua_pushstring(L, "WLAN_SCAN_DONE");
  62. lua_call(L, 1, 0);
  63. break;
  64. case ONESHOT_RESULT:
  65. #ifdef LUAT_USE_NETWORK
  66. lua_pushstring(L, "SC_RESULT");
  67. tls_wifi_get_oneshot_ssidpwd(ssid, pwd);
  68. LLOGD("oneshot %s %s", ssid, pwd);
  69. lua_pushstring(L, (const char*)ssid);
  70. lua_pushstring(L, (const char*)pwd);
  71. lua_call(L, 3, 0);
  72. #endif
  73. break;
  74. default:
  75. break;
  76. }
  77. #ifndef LUAT_USE_NETWORK
  78. switch (msg->arg1)
  79. {
  80. case NETIF_WIFI_DISCONNECTED:
  81. case NETIF_WIFI_JOIN_SUCCESS:
  82. lua_getglobal(L, "sys_pub");
  83. lua_pushstring(L, "WLAN_STATUS");
  84. lua_pushinteger(L, wlan_state);
  85. lua_call(L, 2, 0);
  86. break;
  87. }
  88. #endif
  89. return 0;
  90. }
  91. static void netif_event_cb(u8 status) {
  92. rtos_msg_t msg = {0};
  93. struct tls_param_ip ip_param;
  94. // LLOGD("netif_event %d", status);
  95. msg.handler = l_wlan_cb;
  96. msg.arg1 = status;
  97. switch (status)
  98. {
  99. case NETIF_WIFI_JOIN_FAILED:
  100. LLOGI("join failed");
  101. #ifndef LUAT_USE_NETWORK
  102. tls_auto_reconnect(0);
  103. #endif
  104. break;
  105. case NETIF_WIFI_DISCONNECTED:
  106. wlan_state = 0;
  107. LLOGI("disconnected");
  108. msg.arg1 = status;
  109. luat_msgbus_put(&msg, 0);
  110. #ifndef LUAT_USE_NETWORK
  111. tls_auto_reconnect(0);
  112. #endif
  113. break;
  114. case NETIF_WIFI_JOIN_SUCCESS :
  115. wlan_state = 1;
  116. LLOGI("join success");
  117. #ifdef LUAT_USE_NETWORK
  118. tls_param_get(TLS_PARAM_ID_IP, (void *)&ip_param, false);
  119. if (!ip_param.dhcp_enable) {
  120. LLOGI("dhcp is disable, so 'join success' as 'IP_READY'");
  121. luat_msgbus_put(&msg, 0);
  122. }
  123. #else
  124. luat_msgbus_put(&msg, 0);
  125. #endif
  126. break;
  127. case NETIF_IP_NET_UP :
  128. #ifdef LUAT_USE_NETWORK
  129. LLOGI("IP READY");
  130. msg.arg1 = status;
  131. luat_msgbus_put(&msg, 0);
  132. #endif
  133. break;
  134. #if TLS_CONFIG_AP
  135. case NETIF_WIFI_SOFTAP_SUCCESS :
  136. LLOGI("softap create success");
  137. #ifdef LUAT_USE_NETWORK
  138. net_lwip2_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 1);
  139. #endif
  140. break;
  141. case NETIF_WIFI_SOFTAP_FAILED:
  142. LLOGI("softap create failed");
  143. #ifdef LUAT_USE_NETWORK
  144. net_lwip2_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  145. #endif
  146. break;
  147. case NETIF_WIFI_SOFTAP_CLOSED:
  148. LLOGI("softap create closed");
  149. #ifdef LUAT_USE_NETWORK
  150. net_lwip2_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  151. #endif
  152. break;
  153. case NETIF_IP_NET2_UP :
  154. LLOGI("softap netif up");
  155. break;
  156. #endif
  157. default:
  158. break;
  159. }
  160. }
  161. int luat_wlan_init(luat_wlan_config_t *conf) {
  162. (void)conf;
  163. if (wlan_init == 0) {
  164. //------------------------------------
  165. //u8 enable = 0;
  166. u8 mac_addr[6];
  167. tls_get_tx_gain(&tx_gain_group[0]);
  168. TLS_DBGPRT_INFO("tx gain ");
  169. TLS_DBGPRT_DUMP((char *)(&tx_gain_group[0]), 27);
  170. // 计算wifi内存大小的公式 (TX+RX)*2*1638+4096
  171. // 例如 TX=6, RX=4, (6+4)*2*1638+4096=36856, 向上取整, 36k
  172. // 然后sram的最后位置是 0x20047FFF
  173. if (tls_wifi_mem_cfg((0x20048000u - (36*1024)), 6, 4)) /*wifi tx&rx mem customized interface*/
  174. {
  175. TLS_DBGPRT_INFO("wl mem initial failured\n");
  176. }
  177. tls_ft_param_get(CMD_WIFI_MAC, mac_addr, 6);
  178. TLS_DBGPRT_INFO("mac addr ");
  179. TLS_DBGPRT_DUMP((char *)(&mac_addr[0]), 6);
  180. if(tls_wl_init(NULL, &mac_addr[0], NULL) == NULL)
  181. {
  182. TLS_DBGPRT_INFO("wl driver initial failured\n");
  183. }
  184. if (wpa_supplicant_init(mac_addr))
  185. {
  186. TLS_DBGPRT_INFO("supplicant initial failured\n");
  187. }
  188. /*wifi-temperature compensation,default:open*/
  189. if (tls_wifi_get_tempcomp_flag() != 0)
  190. tls_wifi_set_tempcomp_flag(0);
  191. if (tls_wifi_get_psm_chipsleep_flag() != 0)
  192. tls_wifi_set_psm_chipsleep_flag(0);
  193. tls_wifi_psm_chipsleep_cb_register((tls_wifi_psm_chipsleep_callback)tls_pmu_chipsleep_callback, NULL, NULL);
  194. u8 wireless_protocol = 0;
  195. tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, TRUE);
  196. // LLOGD("wireless_protocol %d", wireless_protocol);
  197. if (TLS_PARAM_IEEE80211_INFRA != wireless_protocol)
  198. {
  199. wireless_protocol = TLS_PARAM_IEEE80211_INFRA;
  200. tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, FALSE);
  201. }
  202. // tls_wifi_enable_log(1);
  203. luat_wlan_get_hostname(0); // 调用一下就行
  204. wlan_init = 1;
  205. #ifdef LUAT_USE_ZLINK_WLAN
  206. extern void luat_zlink_wlan_init(void);
  207. luat_zlink_wlan_init();
  208. #else
  209. #ifdef LUAT_USE_NETWORK
  210. tls_ethernet_init();
  211. tls_sys_init();
  212. tls_netif_add_status_event(netif_event_cb);
  213. #else
  214. tls_wifi_status_change_cb_register(netif_event_cb);
  215. #ifdef LUAT_USE_WLAN_RAW
  216. tls_ethernet_data_rx_callback(luat_wlan_raw_in);
  217. #endif
  218. #endif
  219. tls_wifi_scan_result_cb_register(NULL);
  220. #endif
  221. //-----------------------------------
  222. #ifdef LUAT_USE_NETWORK
  223. struct netif *et0 = tls_get_netif();
  224. net_lwip2_set_netif(NW_ADAPTER_INDEX_LWIP_WIFI_STA, et0);
  225. #if TLS_CONFIG_AP
  226. extern struct netif *nif4apsta;
  227. if (nif4apsta) {
  228. net_lwip2_set_netif(NW_ADAPTER_INDEX_LWIP_WIFI_AP, nif4apsta);
  229. net_lwip2_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  230. }
  231. #endif
  232. net_lwip2_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  233. // 确保DHCP是默认开启
  234. struct tls_param_ip ip_param;
  235. tls_param_get(TLS_PARAM_ID_IP, (void *)&ip_param, false);
  236. if (!ip_param.dhcp_enable) {
  237. ip_param.dhcp_enable = 1;
  238. tls_param_set(TLS_PARAM_ID_IP, (void *)&ip_param, false);
  239. }
  240. #endif
  241. }
  242. tls_wifi_set_psflag(FALSE, FALSE);
  243. return 0;
  244. }
  245. int luat_wlan_mode(luat_wlan_config_t *conf) {
  246. (void)conf;
  247. #if TLS_CONFIG_AP
  248. // 不需要设置, 反正都能用
  249. if (conf->mode == LUAT_WLAN_MODE_STA) {
  250. tls_wifi_softap_destroy();
  251. }
  252. // else if (conf->mode == LUAT_WLAN_MODE_AP) {
  253. // tls_wifi_disconnect();
  254. // }
  255. #endif
  256. return 0;
  257. }
  258. int luat_wlan_ready(void) {
  259. return wlan_state;
  260. }
  261. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  262. tls_wifi_connect((u8*)info->ssid, strlen(info->ssid), (u8*)info->password, strlen(info->password));
  263. u8 opt = WIFI_AUTO_CNT_FLAG_SET;
  264. u8 mode = WIFI_AUTO_CNT_ON;
  265. tls_wifi_auto_connect_flag(opt, &mode);
  266. return 0;
  267. }
  268. int luat_wlan_disconnect(void) {
  269. u8 opt = WIFI_AUTO_CNT_FLAG_SET;
  270. u8 mode = WIFI_AUTO_CNT_OFF;
  271. tls_wifi_auto_connect_flag(opt, &mode);
  272. tls_wifi_disconnect();
  273. return 0;
  274. }
  275. static void scan_event_cb(void *ptmr, void *parg) {
  276. (void)ptmr;
  277. (void)parg;
  278. rtos_msg_t msg = {0};
  279. msg.handler = l_wlan_cb;
  280. msg.arg1 = SCAN_DONE;
  281. luat_msgbus_put(&msg, 0);
  282. }
  283. int luat_wlan_scan(void) {
  284. int ret = tls_wifi_scan();
  285. if (ret)
  286. LLOGD("tls_wifi_scan %d", ret);
  287. static tls_os_timer_t *scan_timer = NULL;
  288. tls_os_timer_create(&scan_timer, scan_event_cb, NULL, 3000, 0, NULL);
  289. tls_os_timer_start(scan_timer);
  290. return ret;
  291. }
  292. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
  293. size_t buffsize = ap_limit * 48 + 8;
  294. u8* buff = luat_heap_malloc(buffsize);
  295. if (buff == NULL)
  296. return 0;
  297. memset(buff, 0, buffsize);
  298. tls_wifi_get_scan_rslt((u8*)buff, buffsize);
  299. struct tls_scan_bss_t *wsr = (struct tls_scan_bss_t*)buff;
  300. struct tls_bss_info_t *bss_info;
  301. if (ap_limit > wsr->count)
  302. ap_limit = wsr->count;
  303. bss_info = wsr->bss;
  304. for (size_t i = 0; i < ap_limit; i++)
  305. {
  306. memset(results[i].ssid, 0, 33);
  307. memcpy(results[i].ssid, bss_info[i].ssid, bss_info[i].ssid_len);
  308. memcpy(results[i].bssid, bss_info[i].bssid, ETH_ALEN);
  309. results[i].rssi = bss_info[i].rssi;
  310. results[i].ch = bss_info[i].channel;
  311. // break;
  312. }
  313. luat_heap_free(buff);
  314. return ap_limit;
  315. }
  316. void luat_sc_callback(enum tls_wifi_oneshot_result_type type) {
  317. if (type == WM_WIFI_ONESHOT_TYPE_SSIDPWD) {
  318. LLOGD("oneshot Got!!");
  319. rtos_msg_t msg = {.handler = l_wlan_cb, .arg1=ONESHOT_RESULT};
  320. luat_msgbus_put(&msg, 0);
  321. }
  322. }
  323. extern u8 gucssidData[];
  324. extern u8 gucpwdData[];
  325. int luat_wlan_smartconfig_start(int tp) {
  326. (void)tp;
  327. #ifdef LUAT_USE_NETWORK
  328. gucssidData[0] = 0;
  329. gucpwdData[0] = 0;
  330. tls_wifi_oneshot_result_cb_register(luat_sc_callback);
  331. return tls_wifi_set_oneshot_flag(1);
  332. #else
  333. return -1;
  334. #endif
  335. }
  336. int luat_wlan_smartconfig_stop(void) {
  337. #ifdef LUAT_USE_NETWORK
  338. return tls_wifi_set_oneshot_flag(0);
  339. #else
  340. return -1;
  341. #endif
  342. }
  343. // 数据类
  344. int luat_wlan_get_mac(int id, char* mac) {
  345. if (id == 0)
  346. tls_get_mac_addr((u8*)mac);
  347. else
  348. tls_ft_param_get(CMD_WIFI_MACAP, mac, 6);
  349. return 0;
  350. }
  351. int luat_wlan_set_mac(int id, const char* mac_addr) {
  352. u8 mac[8] = {0};
  353. int ret = 0;
  354. if (id == 0) {
  355. tls_ft_param_get(CMD_WIFI_MAC, mac, 6);
  356. }
  357. else {
  358. tls_ft_param_get(CMD_WIFI_MACAP, mac, 6);
  359. }
  360. if (memcmp(mac_addr, mac, 6) == 0) {
  361. // 完全相同, 不需要设置
  362. return 0;
  363. }
  364. if (id == 0) {
  365. ret = tls_ft_param_set(CMD_WIFI_MAC, (void*)mac_addr, 6);
  366. }
  367. else {
  368. ret = tls_ft_param_set(CMD_WIFI_MACAP, (void*)mac_addr, 6);
  369. }
  370. return ret;
  371. }
  372. int luat_wlan_get_ip(int type, char* data) {
  373. (void)type;
  374. #ifdef LUAT_USE_NETWORK
  375. struct netif *et0 = tls_get_netif();
  376. if (et0 == NULL || ip_addr_isany(&et0->ip_addr))
  377. return -1;
  378. ipaddr_ntoa_r(&et0->ip_addr, data, 16);
  379. return 0;
  380. #else
  381. return -1;
  382. #endif
  383. }
  384. // 设置和获取省电模式
  385. int luat_wlan_set_ps(int mode) {
  386. tls_wifi_set_psm_chipsleep_flag(mode == 0 ? 0 : 1);
  387. return 0;
  388. }
  389. int luat_wlan_get_ps(void) {
  390. return tls_wifi_get_psm_chipsleep_flag();
  391. }
  392. int luat_wlan_get_ap_bssid(char* buff) {
  393. struct tls_curr_bss_t bss = {0};
  394. tls_wifi_get_current_bss(&bss);
  395. memcpy(buff, bss.bssid, ETH_ALEN);
  396. return 0;
  397. }
  398. int luat_wlan_get_ap_rssi(void) {
  399. struct tls_curr_bss_t bss = {0};
  400. tls_wifi_get_current_bss(&bss);
  401. return bss.rssi;
  402. }
  403. int luat_wlan_get_ap_gateway(char* buff) {
  404. #ifdef LUAT_USE_NETWORK
  405. struct netif *et0 = tls_get_netif();
  406. if (et0 == NULL || ip_addr_isany(&et0->gw))
  407. return -1;
  408. ipaddr_ntoa_r(&et0->gw, buff, 16);
  409. #else
  410. buff[0] = 0;
  411. #endif
  412. return 0;
  413. }
  414. // AP类
  415. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo2) {
  416. #if TLS_CONFIG_AP
  417. int ret = 0;
  418. struct tls_softap_info_t apinfo = {0};
  419. struct tls_ip_info_t ipinfo = {
  420. .ip_addr = {192, 168, 4, 1},
  421. .netmask = {255, 255, 255, 0}
  422. };
  423. if (apinfo2->gateway[0]) {
  424. memcpy(ipinfo.ip_addr, apinfo2->gateway, 4);
  425. }
  426. if (apinfo2->netmask[0]) {
  427. memcpy(ipinfo.netmask, apinfo2->netmask, 4);
  428. }
  429. memcpy(apinfo.ssid, apinfo2->ssid, strlen(apinfo2->ssid) + 1);
  430. if (strlen(apinfo2->password)) {
  431. apinfo.keyinfo.format = 1;
  432. apinfo.keyinfo.key_len = strlen(apinfo2->password);
  433. apinfo.keyinfo.index = 1;
  434. memcpy(apinfo.keyinfo.key, apinfo2->password, strlen(apinfo2->password)+1);
  435. apinfo.encrypt = IEEE80211_ENCRYT_CCMP_WPA2;
  436. }
  437. else {
  438. apinfo.encrypt = IEEE80211_ENCRYT_NONE;
  439. }
  440. if (apinfo2->channel > 0)
  441. apinfo.channel = apinfo2->channel;
  442. else {
  443. apinfo.channel = 6;
  444. }
  445. LLOGD("AP GW %d.%d.%d.%d MASK %d.%d.%d.%d",
  446. ipinfo.ip_addr[0],ipinfo.ip_addr[1],ipinfo.ip_addr[2],ipinfo.ip_addr[3],
  447. ipinfo.netmask[0],ipinfo.netmask[1],ipinfo.netmask[2],ipinfo.netmask[3]
  448. );
  449. // ----------------------------
  450. u8 mac[6] = {0};
  451. tls_get_mac_addr(mac);
  452. sprintf_((char*)ipinfo.dnsname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  453. //------------------------------
  454. u8 value = 0;
  455. tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void *) &value, TRUE);
  456. // LLOGD("wireless_protocol %d", wireless_protocol);
  457. if (TLS_PARAM_IEEE80211_SOFTAP != value)
  458. {
  459. value = TLS_PARAM_IEEE80211_SOFTAP;
  460. tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &value, FALSE);
  461. }
  462. if (apinfo2->hidden) {
  463. value = 0;
  464. tls_param_set(TLS_PARAM_ID_BRDSSID, (void *) &value, FALSE);
  465. }
  466. else {
  467. value = 1;
  468. tls_param_set(TLS_PARAM_ID_BRDSSID, (void *) &value, FALSE);
  469. }
  470. ret = tls_wifi_softap_create(&apinfo, &ipinfo);
  471. if (ret)
  472. LLOGD("tls_wifi_softap_create %d", ret);
  473. return ret;
  474. #else
  475. return -1;
  476. #endif
  477. }
  478. extern u8 *wpa_supplicant_get_mac(void);
  479. const char* luat_wlan_get_hostname(int id) {
  480. (void)id;
  481. if (luat_sta_hostname[0] == 0) {
  482. u8* mac_addr = wpa_supplicant_get_mac();
  483. 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]);
  484. }
  485. return (const char*)luat_sta_hostname;
  486. }
  487. int luat_wlan_set_hostname(int id, const char* hostname) {
  488. (void)id;
  489. if (hostname == NULL || hostname[0] == 0) {
  490. return 0;
  491. }
  492. memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
  493. return 0;
  494. }
  495. int luat_wlan_ap_stop(void) {
  496. #if TLS_CONFIG_AP
  497. tls_wifi_softap_destroy();
  498. #endif
  499. return 0;
  500. }
  501. int luat_wlan_set_station_ip(luat_wlan_station_info_t *info) {
  502. #ifdef LUAT_USE_NETWORK
  503. struct tls_ethif *ethif;
  504. struct tls_param_ip param_ip;
  505. if (info == NULL) {
  506. return -1;
  507. }
  508. ethif=tls_netif_get_ethif();
  509. if (ethif == NULL)
  510. {
  511. LLOGE("call wlan.init() first!!");
  512. return -1;
  513. }
  514. if (info->dhcp_enable) {
  515. LLOGD("dhcp enable");
  516. }
  517. else {
  518. LLOGD("dhcp disable");
  519. LLOGD("Sta IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d",
  520. info->ipv4_addr[0], info->ipv4_addr[1], info->ipv4_addr[2], info->ipv4_addr[3],
  521. info->ipv4_netmask[0],info->ipv4_netmask[1],info->ipv4_netmask[2],info->ipv4_netmask[3],
  522. info->ipv4_gateway[0],info->ipv4_gateway[1],info->ipv4_gateway[2],info->ipv4_gateway[3]
  523. );
  524. }
  525. if (WM_WIFI_JOINED == tls_wifi_get_state())
  526. {
  527. if (info->dhcp_enable) {
  528. /* enable dhcp */
  529. tls_dhcp_start();
  530. } else {
  531. tls_dhcp_stop();
  532. MEMCPY((char *)ip_2_ip4(&ethif->ip_addr) , info->ipv4_addr, 4);
  533. // MEMCPY((char *)ip_2_ip4(&ethif->dns1), &params->dns1, 4);
  534. MEMCPY((char *)ip_2_ip4(&ethif->netmask), info->ipv4_netmask, 4);
  535. MEMCPY((char *)ip_2_ip4(&ethif->gw), info->ipv4_gateway, 4);
  536. tls_netif_set_addr(&ethif->ip_addr, &ethif->netmask, &ethif->gw);
  537. }
  538. }
  539. /* update flash params */
  540. param_ip.dhcp_enable = info->dhcp_enable;
  541. MEMCPY((char *)param_ip.gateway, info->ipv4_gateway, 4);
  542. MEMCPY((char *)param_ip.ip, info->ipv4_addr, 4);
  543. MEMCPY((char *)param_ip.netmask, info->ipv4_netmask, 4);
  544. tls_param_set(TLS_PARAM_ID_IP, (void *)&param_ip, false);
  545. #endif
  546. return 0;
  547. }