wlan_cmd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * File : wlan_cmd.c
  3. * Wi-Fi common commands
  4. * This file is part of RT-Thread RTOS
  5. * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Change Logs:
  22. * Date Author Notes
  23. * 2016-03-12 Bernard first version
  24. */
  25. #include <rtthread.h>
  26. #include <wlan_dev.h>
  27. #include <finsh.h>
  28. #include <lwip/dhcp.h>
  29. #include "wlan_cmd.h"
  30. #ifdef LWIP_USING_DHCPD
  31. #include <dhcp_server.h>
  32. #endif
  33. struct rt_wlan_info info;
  34. static char wifi_ssid[32] = {0};
  35. static char wifi_key[32] = {0};
  36. static int network_mode = WIFI_STATION;
  37. #ifndef WIFI_SETTING_FN
  38. #define WIFI_SETTING_FN "/appfs/setting.json"
  39. #endif
  40. #ifndef WIFI_DEVICE_STA_NAME
  41. #define WIFI_DEVICE_STA_NAME "w0"
  42. #endif
  43. #ifndef WIFI_DEVICE_AP_NAME
  44. #define WIFI_DEVICE_AP_NAME "ap"
  45. #endif
  46. #ifdef RT_USING_DFS
  47. #include <dfs_posix.h>
  48. #ifdef PKG_USING_CJSON
  49. #include <cJSON_util.h>
  50. #endif
  51. int wifi_get_mode(void)
  52. {
  53. return network_mode;
  54. }
  55. int wifi_set_mode(int mode)
  56. {
  57. network_mode = mode;
  58. return network_mode;
  59. }
  60. int wifi_set_setting(const char* ssid, const char* pwd)
  61. {
  62. if (!ssid) return -1;
  63. strncpy(wifi_ssid, ssid, sizeof(wifi_ssid));
  64. wifi_ssid[sizeof(wifi_ssid) - 1] = '\0';
  65. if (pwd)
  66. {
  67. strncpy(wifi_key, pwd, sizeof(wifi_key));
  68. wifi_key[sizeof(wifi_key) - 1] = '\0';
  69. }
  70. else wifi_key[0] = '\0';
  71. return 0;
  72. }
  73. #ifdef PKG_USING_CJSON
  74. int wifi_read_cfg(const char* filename)
  75. {
  76. int fd;
  77. cJSON *json = RT_NULL;
  78. fd = open(filename,O_RDONLY, 0);
  79. if(fd < 0)
  80. {
  81. /* no setting file */
  82. return -1;
  83. }
  84. if (fd >= 0)
  85. {
  86. int length;
  87. length = lseek(fd, 0, SEEK_END);
  88. if (length)
  89. {
  90. char *json_str = (char *) rt_malloc (length);
  91. if (json_str)
  92. {
  93. lseek(fd, 0, SEEK_SET);
  94. read(fd, json_str, length);
  95. json = cJSON_Parse(json_str);
  96. rt_free(json_str);
  97. }
  98. }
  99. close(fd);
  100. }
  101. if (json)
  102. {
  103. cJSON *wifi = cJSON_GetObjectItem(json, "wifi");
  104. cJSON *ssid = cJSON_GetObjectItem(wifi, "SSID");
  105. cJSON *key = cJSON_GetObjectItem(wifi, "Key");
  106. cJSON *mode = cJSON_GetObjectItem(wifi, "Mode");
  107. if (ssid)
  108. {
  109. memset(wifi_ssid, 0x0, sizeof(wifi_ssid));
  110. rt_strncpy(wifi_ssid, ssid->valuestring, sizeof(wifi_ssid) - 1);
  111. }
  112. if (key)
  113. {
  114. memset(wifi_key, 0x0, sizeof(wifi_key));
  115. rt_strncpy(wifi_key, key->valuestring, sizeof(wifi_key) - 1);
  116. }
  117. if (mode)
  118. {
  119. network_mode = mode->valueint;
  120. }
  121. cJSON_Delete(json);
  122. }
  123. return 0;
  124. }
  125. int wifi_save_cfg(const char* filename)
  126. {
  127. int fd;
  128. cJSON *json = RT_NULL;
  129. fd = open(filename, O_RDONLY, 0);
  130. if (fd >= 0)
  131. {
  132. int length;
  133. length = lseek(fd, 0, SEEK_END);
  134. if (length)
  135. {
  136. char *json_str = (char *) rt_malloc (length);
  137. if (json_str)
  138. {
  139. lseek(fd, 0, SEEK_SET);
  140. read(fd, json_str, length);
  141. json = cJSON_Parse(json_str);
  142. rt_free(json_str);
  143. }
  144. }
  145. close(fd);
  146. }
  147. else
  148. {
  149. /* create a new setting.json */
  150. fd = open(filename, O_WRONLY | O_TRUNC, 0);
  151. if (fd >= 0)
  152. {
  153. json = cJSON_CreateObject();
  154. if (json)
  155. {
  156. cJSON *wifi = cJSON_CreateObject();
  157. if (wifi)
  158. {
  159. char *json_str;
  160. cJSON_AddItemToObject(json, "wifi", wifi);
  161. cJSON_AddStringToObject(wifi, "SSID", wifi_ssid);
  162. cJSON_AddStringToObject(wifi, "Key", wifi_key);
  163. cJSON_AddNumberToObject(wifi, "Mode", network_mode);
  164. json_str = cJSON_Print(json);
  165. if (json_str)
  166. {
  167. write(fd, json_str, rt_strlen(json_str));
  168. cJSON_free(json_str);
  169. }
  170. }
  171. }
  172. }
  173. close(fd);
  174. return 0;
  175. }
  176. if (json)
  177. {
  178. cJSON *wifi = cJSON_GetObjectItem(json, "wifi");
  179. if (!wifi)
  180. {
  181. wifi = cJSON_CreateObject();
  182. cJSON_AddItemToObject(json, "wifi", wifi);
  183. }
  184. if (cJSON_GetObjectItem(wifi, "SSID"))cJSON_ReplaceItemInObject(wifi, "SSID", cJSON_CreateString(wifi_ssid));
  185. else cJSON_AddStringToObject(wifi, "SSID", wifi_ssid);
  186. if (cJSON_GetObjectItem(wifi, "Key")) cJSON_ReplaceItemInObject(wifi, "Key", cJSON_CreateString(wifi_key));
  187. else cJSON_AddStringToObject(wifi, "Key", wifi_key);
  188. if (cJSON_GetObjectItem(wifi, "Mode")) cJSON_ReplaceItemInObject(wifi, "Mode", cJSON_CreateNumber(network_mode));
  189. else cJSON_AddNumberToObject(wifi, "Mode", network_mode);
  190. fd = open(filename, O_WRONLY | O_TRUNC, 0);
  191. if (fd >= 0)
  192. {
  193. char *json_str = cJSON_Print(json);
  194. if (json_str)
  195. {
  196. write(fd, json_str, rt_strlen(json_str));
  197. cJSON_free(json_str);
  198. }
  199. close(fd);
  200. }
  201. cJSON_Delete(json);
  202. }
  203. return 0;
  204. }
  205. #endif
  206. int wifi_save_setting(void)
  207. {
  208. #ifdef PKG_USING_CJSON
  209. wifi_save_cfg(WIFI_SETTING_FN);
  210. #endif
  211. return 0;
  212. }
  213. #endif
  214. int wifi_softap_setup_netif(struct netif *netif)
  215. {
  216. if (netif)
  217. {
  218. #ifdef RT_LWIP_DHCP
  219. /* Stop DHCP Client */
  220. dhcp_stop(netif);
  221. #endif
  222. #ifdef LWIP_USING_DHCPD
  223. {
  224. char name[8];
  225. memset(name, 0, sizeof(name));
  226. strncpy(name, netif->name, sizeof(name)>sizeof(netif->name)? sizeof(netif->name) : sizeof(name));
  227. dhcpd_start(name);
  228. }
  229. #endif
  230. }
  231. return 0;
  232. }
  233. int wifi_default(void)
  234. {
  235. int result = 0;
  236. struct rt_wlan_device *wlan;
  237. #ifdef PKG_USING_CJSON
  238. /* read default setting for wifi */
  239. wifi_read_cfg(WIFI_SETTING_FN);
  240. #endif
  241. if (network_mode == WIFI_STATION)
  242. {
  243. /* get wlan device */
  244. wlan = (struct rt_wlan_device*)rt_device_find(WIFI_DEVICE_STA_NAME);
  245. if (!wlan)
  246. {
  247. rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_STA_NAME);
  248. return -1;
  249. }
  250. /* wifi station */
  251. rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, wifi_ssid);
  252. result =rt_wlan_init(wlan, WIFI_STATION);
  253. if (result == RT_EOK)
  254. {
  255. result = rt_wlan_connect(wlan, &info, wifi_key);
  256. }
  257. }
  258. else
  259. {
  260. /* wifi AP */
  261. /* get wlan device */
  262. wlan = (struct rt_wlan_device*)rt_device_find(WIFI_DEVICE_AP_NAME);
  263. if (!wlan)
  264. {
  265. rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_AP_NAME);
  266. return -1;
  267. }
  268. rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, wifi_ssid);
  269. info.channel = 11;
  270. /* wifi soft-AP */
  271. result =rt_wlan_init(wlan, WIFI_AP);
  272. if (result == RT_EOK)
  273. {
  274. result = rt_wlan_softap(wlan, &info, wifi_key);
  275. }
  276. }
  277. return result;
  278. }
  279. static void wifi_usage(void)
  280. {
  281. rt_kprintf("wifi help - Help information\n");
  282. rt_kprintf("wifi cfg SSID PASSWORD - Setting your router AP ssid and pwd\n");
  283. rt_kprintf("wifi - Do the default wifi action\n");
  284. rt_kprintf("wifi wlan_dev scan\n");
  285. rt_kprintf("wifi wlan_dev join SSID PASSWORD\n");
  286. rt_kprintf("wifi wlan_dev ap SSID [PASSWORD]\n");
  287. rt_kprintf("wifi wlan_dev up\n");
  288. rt_kprintf("wifi wlan_dev down\n");
  289. rt_kprintf("wifi wlan_dev rssi\n");
  290. rt_kprintf("wifi wlan_dev status\n");
  291. }
  292. int wifi(int argc, char** argv)
  293. {
  294. struct rt_wlan_device *wlan;
  295. if (argc == 1)
  296. {
  297. wifi_default();
  298. return 0;
  299. }
  300. if (strcmp(argv[1], "help") == 0)
  301. {
  302. wifi_usage();
  303. return 0;
  304. }
  305. if (strcmp(argv[1], "cfg") == 0)
  306. {
  307. /* configure wifi setting */
  308. memset(wifi_ssid, 0x0, sizeof(wifi_ssid));
  309. rt_strncpy(wifi_ssid, argv[2], sizeof(wifi_ssid) - 1);
  310. memset(wifi_key, 0x0, sizeof(wifi_key));
  311. rt_strncpy(wifi_key, argv[3], sizeof(wifi_key) - 1);
  312. network_mode = WIFI_STATION;
  313. #ifdef PKG_USING_CJSON
  314. wifi_save_cfg(WIFI_SETTING_FN);
  315. #endif
  316. return 0;
  317. }
  318. /* get wlan device */
  319. wlan = (struct rt_wlan_device*)rt_device_find(argv[1]);
  320. if (!wlan)
  321. {
  322. rt_kprintf("no wlan:%s device\n", argv[1]);
  323. return 0;
  324. }
  325. if (argc < 3)
  326. {
  327. wifi_usage();
  328. return 0;
  329. }
  330. if (strcmp(argv[2], "join") == 0)
  331. {
  332. rt_wlan_init(wlan, WIFI_STATION);
  333. network_mode = WIFI_STATION;
  334. /* TODO: use easy-join to replace */
  335. rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, argv[3]);
  336. rt_wlan_connect(wlan, &info, argv[4]);
  337. }
  338. else if (strcmp(argv[2], "up") == 0)
  339. {
  340. /* the key was saved in wlan device */
  341. rt_wlan_connect(wlan, RT_NULL, wlan->key);
  342. }
  343. else if (strcmp(argv[2], "down") == 0)
  344. {
  345. rt_wlan_disconnect(wlan);
  346. }
  347. else if (strcmp(argv[2], "scan") == 0)
  348. {
  349. struct rt_wlan_info *infos;
  350. infos = (struct rt_wlan_info*)rt_malloc(sizeof(struct rt_wlan_info) * 12);
  351. if (infos)
  352. {
  353. int index, num;
  354. memset(infos, 0x0, sizeof(struct rt_wlan_info) * 12);
  355. num = rt_wlan_scan(wlan, infos, 12);
  356. for (index = 0; index < num; index ++)
  357. {
  358. rt_kprintf("----Wi-Fi AP[%d] Information----\n", index);
  359. rt_kprintf("SSID: %-.32s\n", infos[index].ssid);
  360. rt_kprintf("rssi: %d\n", infos[index].rssi);
  361. rt_kprintf(" chn: %d\n", infos[index].channel);
  362. rt_kprintf("rate: %d\n", infos[index].datarate);
  363. rt_kprintf("\n");
  364. }
  365. /* de-initialize info */
  366. for (index = 0; index < num; index ++)
  367. {
  368. rt_wlan_info_deinit(&infos[index]);
  369. }
  370. rt_free(infos);
  371. }
  372. }
  373. else if (strcmp(argv[2], "rssi") == 0)
  374. {
  375. int rssi;
  376. rssi = rt_wlan_get_rssi(wlan);
  377. rt_kprintf("rssi=%d\n", rssi);
  378. }
  379. else if (strcmp(argv[2], "ap") == 0)
  380. {
  381. rt_err_t result = RT_EOK;
  382. if (argc == 4)
  383. {
  384. // open soft-AP
  385. rt_wlan_info_init(&info, WIFI_AP, SECURITY_OPEN, argv[3]);
  386. info.channel = 11;
  387. result =rt_wlan_init(wlan, WIFI_AP);
  388. /* start soft ap */
  389. result = rt_wlan_softap(wlan, &info, NULL);
  390. if (result == RT_EOK)
  391. {
  392. network_mode = WIFI_AP;
  393. }
  394. }
  395. else if (argc == 5)
  396. {
  397. // WPA2 with password
  398. rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, argv[3]);
  399. info.channel = 11;
  400. result =rt_wlan_init(wlan, WIFI_AP);
  401. /* start soft ap */
  402. result = rt_wlan_softap(wlan, &info, argv[4]);
  403. if (result == RT_EOK)
  404. {
  405. network_mode = WIFI_AP;
  406. }
  407. }
  408. else
  409. {
  410. wifi_usage();
  411. }
  412. if (result != RT_EOK)
  413. {
  414. rt_kprintf("wifi start failed! result=%d\n", result);
  415. }
  416. }
  417. else if (strcmp(argv[2], "status") == 0)
  418. {
  419. int rssi;
  420. if (netif_is_link_up(wlan->parent.netif))
  421. {
  422. rssi = rt_wlan_get_rssi(wlan);
  423. rt_kprintf("Wi-Fi AP: %-.32s\n", wlan->info->ssid);
  424. rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", wlan->info->bssid[0],
  425. wlan->info->bssid[1],
  426. wlan->info->bssid[2],
  427. wlan->info->bssid[3],
  428. wlan->info->bssid[4],
  429. wlan->info->bssid[5]);
  430. rt_kprintf(" Channel: %d\n", wlan->info->channel);
  431. rt_kprintf("DataRate: %dMbps\n", wlan->info->datarate/1000);
  432. rt_kprintf(" RSSI: %d\n", rssi);
  433. }
  434. else
  435. {
  436. rt_kprintf("wifi disconnected!\n");
  437. }
  438. return 0;
  439. }
  440. return 0;
  441. }
  442. MSH_CMD_EXPORT(wifi, wifi command);