wlan_cfg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-06 tyx the first version
  9. */
  10. #include <rtthread.h>
  11. #include <wlan_cfg.h>
  12. #ifdef RT_WLAN_CFG_DEBUG
  13. #define DBG_LEVEL DBG_LOG
  14. #else
  15. #define DBG_LEVEL DBG_INFO
  16. #endif
  17. #define DBG_SECTION_NAME "WLAN.cfg"
  18. #include <rtdbg.h>
  19. #define WLAN_CFG_LOCK() (rt_mutex_take(&cfg_mutex, RT_WAITING_FOREVER))
  20. #define WLAN_CFG_UNLOCK() (rt_mutex_release(&cfg_mutex))
  21. #if RT_WLAN_CFG_INFO_MAX < 1
  22. #error "The minimum configuration is 1"
  23. #endif
  24. struct cfg_save_info_head
  25. {
  26. rt_uint32_t magic;
  27. rt_uint32_t len;
  28. rt_uint32_t num;
  29. rt_uint32_t crc;
  30. };
  31. struct rt_wlan_cfg_des
  32. {
  33. rt_uint32_t num;
  34. struct rt_wlan_cfg_info *cfg_info;
  35. };
  36. static struct rt_wlan_cfg_des *cfg_cache;
  37. static const struct rt_wlan_cfg_ops *cfg_ops;
  38. static struct rt_mutex cfg_mutex;
  39. /*
  40. * CRC16_CCITT
  41. */
  42. static rt_uint16_t rt_wlan_cal_crc(rt_uint8_t *buff, int len)
  43. {
  44. rt_uint16_t wCRCin = 0x0000;
  45. rt_uint16_t wCPoly = 0x1021;
  46. rt_uint8_t wChar = 0;
  47. while (len--)
  48. {
  49. wChar = *(buff++);
  50. wCRCin ^= (wChar << 8);
  51. for (int i = 0; i < 8; i++)
  52. {
  53. if (wCRCin & 0x8000)
  54. wCRCin = (wCRCin << 1) ^ wCPoly;
  55. else
  56. wCRCin = wCRCin << 1;
  57. }
  58. }
  59. return wCRCin;
  60. }
  61. void rt_wlan_cfg_init(void)
  62. {
  63. /* init cache memory */
  64. if (cfg_cache == RT_NULL)
  65. {
  66. cfg_cache = rt_malloc(sizeof(struct rt_wlan_cfg_des));
  67. if (cfg_cache != RT_NULL)
  68. {
  69. rt_memset(cfg_cache, 0, sizeof(struct rt_wlan_cfg_des));
  70. }
  71. /* init mutex lock */
  72. rt_mutex_init(&cfg_mutex, "wlan_cfg", RT_IPC_FLAG_FIFO);
  73. }
  74. }
  75. void rt_wlan_cfg_set_ops(const struct rt_wlan_cfg_ops *ops)
  76. {
  77. rt_wlan_cfg_init();
  78. WLAN_CFG_LOCK();
  79. /* save ops pointer */
  80. cfg_ops = ops;
  81. WLAN_CFG_UNLOCK();
  82. }
  83. /* save config data */
  84. rt_err_t rt_wlan_cfg_cache_save(void)
  85. {
  86. rt_err_t err = RT_EOK;
  87. struct cfg_save_info_head *info_pkg;
  88. int len = 0;
  89. if ((cfg_ops == RT_NULL) || (cfg_ops->write_cfg == RT_NULL))
  90. return RT_EOK;
  91. WLAN_CFG_LOCK();
  92. len = sizeof(struct cfg_save_info_head) + sizeof(struct rt_wlan_cfg_info) * cfg_cache->num;
  93. info_pkg = rt_malloc(len);
  94. if (info_pkg == RT_NULL)
  95. {
  96. WLAN_CFG_UNLOCK();
  97. return -RT_ENOMEM;
  98. }
  99. info_pkg->magic = RT_WLAN_CFG_MAGIC;
  100. info_pkg->len = len;
  101. info_pkg->num = cfg_cache->num;
  102. /* CRC */
  103. info_pkg->crc = rt_wlan_cal_crc((rt_uint8_t *)cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * cfg_cache->num);
  104. rt_memcpy(((rt_uint8_t *)info_pkg) + sizeof(struct cfg_save_info_head),
  105. cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * cfg_cache->num);
  106. if (cfg_ops->write_cfg(info_pkg, len) != len)
  107. err = -RT_ERROR;
  108. rt_free(info_pkg);
  109. WLAN_CFG_UNLOCK();
  110. return err;
  111. }
  112. rt_err_t rt_wlan_cfg_cache_refresh(void)
  113. {
  114. int len = 0, i, j;
  115. struct cfg_save_info_head *head;
  116. void *data;
  117. struct rt_wlan_cfg_info *t_info, *cfg_info;
  118. rt_uint32_t crc;
  119. rt_bool_t equal_flag;
  120. /* cache is full! exit */
  121. if (cfg_cache == RT_NULL || cfg_cache->num >= RT_WLAN_CFG_INFO_MAX)
  122. return -RT_ERROR;
  123. /* check callback */
  124. if ((cfg_ops == RT_NULL) ||
  125. (cfg_ops->get_len == RT_NULL) ||
  126. (cfg_ops->read_cfg == RT_NULL))
  127. return -RT_ERROR;
  128. WLAN_CFG_LOCK();
  129. /* get data len */
  130. if ((len = cfg_ops->get_len()) <= 0)
  131. {
  132. WLAN_CFG_UNLOCK();
  133. return -RT_ERROR;
  134. }
  135. head = rt_malloc(len);
  136. if (head == RT_NULL)
  137. {
  138. WLAN_CFG_UNLOCK();
  139. return -RT_ERROR;
  140. }
  141. /* get data */
  142. if (cfg_ops->read_cfg(head, len) != len)
  143. {
  144. rt_free(head);
  145. WLAN_CFG_UNLOCK();
  146. return -RT_ERROR;
  147. }
  148. /* get config */
  149. data = ((rt_uint8_t *)head) + sizeof(struct cfg_save_info_head);
  150. crc = rt_wlan_cal_crc((rt_uint8_t *)data, len - sizeof(struct cfg_save_info_head));
  151. LOG_D("head->magic:0x%08x RT_WLAN_CFG_MAGIC:0x%08x", head->magic, RT_WLAN_CFG_MAGIC);
  152. LOG_D("head->len:%d len:%d", head->len, len);
  153. LOG_D("head->num:%d num:%d", head->num, (len - sizeof(struct cfg_save_info_head)) / sizeof(struct rt_wlan_cfg_info));
  154. LOG_D("hred->crc:0x%04x crc:0x%04x", head->crc, crc);
  155. /* check */
  156. if ((head->magic != RT_WLAN_CFG_MAGIC) ||
  157. (head->len != len) ||
  158. (head->num != (len - sizeof(struct cfg_save_info_head)) / sizeof(struct rt_wlan_cfg_info)) ||
  159. (head->crc != crc))
  160. {
  161. rt_free(head);
  162. WLAN_CFG_UNLOCK();
  163. return -RT_ERROR;
  164. }
  165. /* remove duplicate config */
  166. cfg_info = (struct rt_wlan_cfg_info *)data;
  167. for (i = 0; i < head->num; i++)
  168. {
  169. equal_flag = RT_FALSE;
  170. for (j = 0; j < cfg_cache->num; j++)
  171. {
  172. if ((cfg_cache->cfg_info[j].info.ssid.len == cfg_info[i].info.ssid.len) &&
  173. (rt_memcmp(&cfg_cache->cfg_info[j].info.ssid.val[0], &cfg_info[i].info.ssid.val[0],
  174. cfg_cache->cfg_info[j].info.ssid.len) == 0) &&
  175. (rt_memcmp(&cfg_cache->cfg_info[j].info.bssid[0], &cfg_info[i].info.bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0))
  176. {
  177. equal_flag = RT_TRUE;
  178. break;
  179. }
  180. }
  181. if (cfg_cache->num >= RT_WLAN_CFG_INFO_MAX)
  182. {
  183. break;
  184. }
  185. if (equal_flag == RT_FALSE)
  186. {
  187. t_info = rt_realloc(cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num + 1));
  188. if (t_info == RT_NULL)
  189. {
  190. rt_free(head);
  191. WLAN_CFG_UNLOCK();
  192. return -RT_ERROR;
  193. }
  194. cfg_cache->cfg_info = t_info;
  195. cfg_cache->cfg_info[cfg_cache->num] = cfg_info[i];
  196. cfg_cache->num ++;
  197. }
  198. }
  199. rt_free(head);
  200. WLAN_CFG_UNLOCK();
  201. return RT_EOK;
  202. }
  203. int rt_wlan_cfg_get_num(void)
  204. {
  205. rt_wlan_cfg_init();
  206. return cfg_cache->num;
  207. }
  208. int rt_wlan_cfg_read(struct rt_wlan_cfg_info *cfg_info, int num)
  209. {
  210. rt_wlan_cfg_init();
  211. if ((cfg_info == RT_NULL) || (num <= 0))
  212. return 0;
  213. /* copy data */
  214. WLAN_CFG_LOCK();
  215. num = cfg_cache->num > num ? num : cfg_cache->num;
  216. rt_memcpy(&cfg_cache->cfg_info[0], cfg_info, sizeof(struct rt_wlan_cfg_info) * num);
  217. WLAN_CFG_UNLOCK();
  218. return num;
  219. }
  220. rt_err_t rt_wlan_cfg_save(struct rt_wlan_cfg_info *cfg_info)
  221. {
  222. rt_err_t err = RT_EOK;
  223. struct rt_wlan_cfg_info *t_info;
  224. int idx = -1, i = 0;
  225. rt_wlan_cfg_init();
  226. /* parameter check */
  227. if ((cfg_info == RT_NULL) || (cfg_info->info.ssid.len == 0))
  228. {
  229. return -RT_EINVAL;
  230. }
  231. /* if (iteam == cache) exit */
  232. WLAN_CFG_LOCK();
  233. for (i = 0; i < cfg_cache->num; i++)
  234. {
  235. if ((cfg_cache->cfg_info[i].info.ssid.len == cfg_info->info.ssid.len) &&
  236. (rt_memcmp(&cfg_cache->cfg_info[i].info.ssid.val[0], &cfg_info->info.ssid.val[0],
  237. cfg_cache->cfg_info[i].info.ssid.len) == 0) &&
  238. (rt_memcmp(&cfg_cache->cfg_info[i].info.bssid[0], &cfg_info->info.bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0))
  239. {
  240. idx = i;
  241. break;
  242. }
  243. }
  244. if ((idx == 0) && (cfg_cache->cfg_info[i].key.len == cfg_info->key.len) &&
  245. (rt_memcmp(&cfg_cache->cfg_info[i].key.val[0], &cfg_info->key.val[0], cfg_info->key.len) == 0))
  246. {
  247. WLAN_CFG_UNLOCK();
  248. return RT_EOK;
  249. }
  250. /* not find iteam with cache, Add iteam to the head */
  251. if ((idx == -1) && (cfg_cache->num < RT_WLAN_CFG_INFO_MAX))
  252. {
  253. t_info = rt_realloc(cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num + 1));
  254. if (t_info == RT_NULL)
  255. {
  256. WLAN_CFG_UNLOCK();
  257. return -RT_ENOMEM;
  258. }
  259. cfg_cache->cfg_info = t_info;
  260. cfg_cache->num ++;
  261. }
  262. /* move cache info */
  263. i = (i >= RT_WLAN_CFG_INFO_MAX ? RT_WLAN_CFG_INFO_MAX - 1 : i);
  264. for (; i; i--)
  265. {
  266. cfg_cache->cfg_info[i] = cfg_cache->cfg_info[i - 1];
  267. }
  268. /* add iteam to head */
  269. cfg_cache->cfg_info[i] = *cfg_info;
  270. WLAN_CFG_UNLOCK();
  271. /* save info to flash */
  272. err = rt_wlan_cfg_cache_save();
  273. return err;
  274. }
  275. int rt_wlan_cfg_read_index(struct rt_wlan_cfg_info *cfg_info, int index)
  276. {
  277. rt_wlan_cfg_init();
  278. if ((cfg_info == RT_NULL) || (index < 0))
  279. return 0;
  280. WLAN_CFG_LOCK();
  281. if (index >= cfg_cache->num)
  282. {
  283. WLAN_CFG_UNLOCK();
  284. return 0;
  285. }
  286. /* copy data */
  287. *cfg_info = cfg_cache->cfg_info[index];
  288. WLAN_CFG_UNLOCK();
  289. return 1;
  290. }
  291. int rt_wlan_cfg_delete_index(int index)
  292. {
  293. struct rt_wlan_cfg_info *cfg_info;
  294. int i;
  295. rt_wlan_cfg_init();
  296. if (index < 0)
  297. return -1;
  298. WLAN_CFG_LOCK();
  299. if (index >= cfg_cache->num)
  300. {
  301. WLAN_CFG_UNLOCK();
  302. return -1;
  303. }
  304. /* malloc new mem */
  305. cfg_info = rt_malloc(sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num - 1));
  306. if (cfg_info == RT_NULL)
  307. {
  308. WLAN_CFG_UNLOCK();
  309. return -1;
  310. }
  311. /* copy data to new mem */
  312. for (i = 0; i < cfg_cache->num; i++)
  313. {
  314. if (i < index)
  315. {
  316. cfg_info[i] = cfg_cache->cfg_info[i];
  317. }
  318. else if (i > index)
  319. {
  320. cfg_info[i - 1] = cfg_cache->cfg_info[i];
  321. }
  322. }
  323. rt_free(cfg_cache->cfg_info);
  324. cfg_cache->cfg_info = cfg_info;
  325. cfg_cache->num --;
  326. WLAN_CFG_UNLOCK();
  327. return 0;
  328. }
  329. void rt_wlan_cfg_delete_all(void)
  330. {
  331. rt_wlan_cfg_init();
  332. /* delete all iteam */
  333. WLAN_CFG_LOCK();
  334. cfg_cache->num = 0;
  335. rt_free(cfg_cache->cfg_info);
  336. cfg_cache->cfg_info = RT_NULL;
  337. WLAN_CFG_UNLOCK();
  338. }
  339. void rt_wlan_cfg_dump(void)
  340. {
  341. int index = 0;
  342. struct rt_wlan_info *info;
  343. struct rt_wlan_key *key;
  344. char *security;
  345. rt_wlan_cfg_init();
  346. rt_kprintf(" SSID PASSWORD MAC security chn\n");
  347. rt_kprintf("------------------------------- ------------------------------- ----------------- -------------- ---\n");
  348. for (index = 0; index < cfg_cache->num; index ++)
  349. {
  350. info = &cfg_cache->cfg_info[index].info;
  351. key = &cfg_cache->cfg_info[index].key;
  352. if (info->ssid.len)
  353. rt_kprintf("%-32.32s", &info->ssid.val[0]);
  354. else
  355. rt_kprintf("%-32.32s", " ");
  356. if (key->len)
  357. rt_kprintf("%-32.32s", &key->val[0]);
  358. else
  359. rt_kprintf("%-32.32s", " ");
  360. rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ",
  361. info->bssid[0],
  362. info->bssid[1],
  363. info->bssid[2],
  364. info->bssid[3],
  365. info->bssid[4],
  366. info->bssid[5]
  367. );
  368. switch (info->security)
  369. {
  370. case SECURITY_OPEN:
  371. security = "OPEN";
  372. break;
  373. case SECURITY_WEP_PSK:
  374. security = "WEP_PSK";
  375. break;
  376. case SECURITY_WEP_SHARED:
  377. security = "WEP_SHARED";
  378. break;
  379. case SECURITY_WPA_TKIP_PSK:
  380. security = "WPA_TKIP_PSK";
  381. break;
  382. case SECURITY_WPA_AES_PSK:
  383. security = "WPA_AES_PSK";
  384. break;
  385. case SECURITY_WPA2_AES_PSK:
  386. security = "WPA2_AES_PSK";
  387. break;
  388. case SECURITY_WPA2_TKIP_PSK:
  389. security = "WPA2_TKIP_PSK";
  390. break;
  391. case SECURITY_WPA2_MIXED_PSK:
  392. security = "WPA2_MIXED_PSK";
  393. break;
  394. case SECURITY_WPS_OPEN:
  395. security = "WPS_OPEN";
  396. break;
  397. case SECURITY_WPS_SECURE:
  398. security = "WPS_SECURE";
  399. break;
  400. default:
  401. security = "UNKNOWN";
  402. break;
  403. }
  404. rt_kprintf("%-14.14s ", security);
  405. rt_kprintf("%3d \n", info->channel);
  406. }
  407. }