luat_wlan_air101.c 17 KB

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