luat_wlan_air101.c 21 KB

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