espnow_controller.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_main.c
  5. *
  6. * Description: entry file of user application
  7. *
  8. * Modification history:
  9. * 2015/7/3, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "osapi.h"
  12. #include "user_interface.h"
  13. #include "driver/uart.h"
  14. #include "driver/key.h"
  15. #include "user_json.h"
  16. #include "espnow.h"
  17. #include "mem.h"
  18. #include "simple_pair.h"
  19. #if ESPNOW_CONTROLLER
  20. /*******************************************************************************
  21. * open AS_STA to compile sta test code
  22. * open AS_AP to compile ap test code
  23. * don't open both
  24. * ****************************************************************************/
  25. #define AS_STA
  26. #define PWLDEV_ESP_NOW_CHANNEL_NUM 6
  27. // based on spadev board
  28. #define KEY_NUM 1
  29. #define KEY_0_IO_MUX PERIPHS_IO_MUX_MTDI_U
  30. #define KEY_0_IO_NUM 12
  31. #define KEY_0_IO_FUNC FUNC_GPIO12
  32. #define PWM_CHANNEL 1
  33. #define LED_0_OUT_IO_MUX PERIPHS_IO_MUX_GPIO5_U
  34. #define LED_0_OUT_IO_NUM 5
  35. #define LED_0_OUT_IO_FUNC FUNC_GPIO5
  36. LOCAL struct keys_param keys;
  37. LOCAL struct single_key_param *single_key[KEY_NUM];
  38. LOCAL os_timer_t dev_espnow_timer;
  39. static u8 simple_pair_stat = 0;
  40. static u8 esp_now_stat = 0;
  41. LOCAL uint8 ap_channel = 1;
  42. /* STA & AP use the same tmpkey to encrypt Simple Pair communication */
  43. static u8 tmpkey[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  44. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
  45. #ifdef AS_STA
  46. /* since the ex_key transfer from AP to STA, so STA's ex_key don't care */
  47. static u8 ex_key[16] = {0x00};
  48. #endif /* AS_STA */
  49. void ICACHE_FLASH_ATTR
  50. show_key(u8 *buf, u8 len)
  51. {
  52. u8 i;
  53. for (i = 0; i < len; i++)
  54. os_printf("%02x,%s", buf[i], (i%16 == 15?"\n":" "));
  55. }
  56. void ICACHE_FLASH_ATTR
  57. show_espnow_cnt(void)
  58. {
  59. u8 all_cnt, encrypt_cnt;
  60. if (esp_now_get_cnt_info(&all_cnt, &encrypt_cnt))
  61. os_printf("get_cnt_info failed\r\n");
  62. os_printf("client:%d, encrypted client:%d\r\n", all_cnt, encrypt_cnt);
  63. }
  64. #ifdef AS_STA
  65. static void ICACHE_FLASH_ATTR
  66. scan_done(void *arg, STATUS status)
  67. {
  68. int ret;
  69. if (status == OK) {
  70. struct bss_info *bss_link = (struct bss_info *)arg;
  71. while (bss_link != NULL) {
  72. if (bss_link->simple_pair) {
  73. os_printf("Simple Pair: bssid %02x:%02x:%02x:%02x:%02x:%02x Ready!\n",
  74. bss_link->bssid[0], bss_link->bssid[1], bss_link->bssid[2],
  75. bss_link->bssid[3], bss_link->bssid[4], bss_link->bssid[5]);
  76. os_printf("peer chn %d\n", bss_link->channel);
  77. simple_pair_set_peer_ref(bss_link->bssid, tmpkey, NULL);
  78. ret = simple_pair_sta_start_negotiate();
  79. if (ret)
  80. os_printf("Simple Pair: STA start NEG Failed\n");
  81. else
  82. os_printf("Simple Pair: STA start NEG OK\n");
  83. break;
  84. }
  85. bss_link = bss_link->next.stqe_next;
  86. }
  87. } else {
  88. os_printf("err, scan status %d\n", status);
  89. }
  90. }
  91. #endif
  92. void ICACHE_FLASH_ATTR
  93. sp_status(u8 *sa, u8 status)
  94. {
  95. #ifdef AS_STA
  96. if(sa)
  97. os_printf("Simple Pair: sta " MACSTR "\n", MAC2STR(sa));
  98. switch (status) {
  99. case SP_ST_STA_FINISH:
  100. simple_pair_get_peer_ref(NULL, NULL, ex_key);
  101. os_printf("Simple Pair: STA FINISH, Ex_key ");
  102. show_key(ex_key, 16);
  103. /* TODO: Try to use the ex-key communicate with AP, for example use ESP-NOW */
  104. os_printf("SP_ST_STA_FINISH @t %ld\n", system_get_time());
  105. if(esp_now_stat)
  106. {
  107. if(esp_now_add_peer(sa, ESP_NOW_ROLE_SLAVE, ap_channel, ex_key, 16) == 0)
  108. {
  109. os_printf("add remote peer addr success\n");
  110. char *pbuf = NULL;
  111. pbuf = (char *)os_zalloc(jsonSize);
  112. os_sprintf(pbuf, "hello slave.");
  113. // json_ws_send((struct jsontree_value *)&set_pwl_tree, "light", pbuf);
  114. if (esp_now_send(sa, pbuf, os_strlen(pbuf)))
  115. os_printf("fail\r\n");
  116. else
  117. os_printf("ok\r\n");
  118. // EPIT_DBG("ctrl:%s\n", pbuf);
  119. os_free(pbuf);
  120. }
  121. show_espnow_cnt();
  122. }
  123. /* if test ok , deinit simple pair */
  124. simple_pair_deinit();
  125. simple_pair_stat = 0;
  126. os_timer_disarm(&dev_espnow_timer);
  127. break;
  128. case SP_ST_STA_AP_REFUSE_NEG:
  129. /* AP refuse , so try simple pair again or scan other ap*/
  130. os_printf("Simple Pair: Recv AP Refuse\n");
  131. simple_pair_state_reset();
  132. simple_pair_sta_enter_scan_mode();
  133. wifi_station_scan(NULL, scan_done);
  134. break;
  135. case SP_ST_WAIT_TIMEOUT:
  136. /* In negotiate, timeout , so try simple pair again */
  137. os_printf("Simple Pair: Neg Timeout\n");
  138. os_printf(" @t %d\n", system_get_time());
  139. simple_pair_state_reset();
  140. simple_pair_sta_enter_scan_mode();
  141. wifi_station_scan(NULL, scan_done);
  142. break;
  143. case SP_ST_SEND_ERROR:
  144. os_printf("Simple Pair: Send Error\n");
  145. /* maybe the simple_pair_set_peer_ref() haven't called, it send to a wrong mac address */
  146. break;
  147. case SP_ST_KEY_INSTALL_ERR:
  148. os_printf("Simple Pair: Key Install Error\n");
  149. /* 1. maybe something argument error.
  150. 2. maybe the key number is full in system*/
  151. /* TODO: Check other modules which use lots of keys
  152. Example: ESPNOW and STA/AP use lots of keys */
  153. break;
  154. case SP_ST_KEY_OVERLAP_ERR:
  155. os_printf("Simple Pair: Key Overlap Error\n");
  156. /* 1. maybe something argument error.
  157. 2. maybe the MAC Address is already use in ESP-NOW or other module
  158. the same MAC Address has multi key*/
  159. /* TODO: Check if the same MAC Address used already,
  160. Example: del MAC item of ESPNOW or other module */
  161. esp_now_del_peer(sa);
  162. break;
  163. case SP_ST_OP_ERROR:
  164. os_printf("Simple Pair: Operation Order Error\n");
  165. /* 1. maybe the function call order has something wrong */
  166. /* TODO: Adjust your function call order */
  167. break;
  168. default:
  169. os_printf("Simple Pair: Unknown Error\n");
  170. break;
  171. }
  172. #endif /* AS_STA */
  173. }
  174. LOCAL void ICACHE_FLASH_ATTR
  175. ctrller_simple_pair_time_cb(void)
  176. {
  177. os_printf("%s\n", __FUNCTION__);
  178. os_timer_disarm(&dev_espnow_timer);
  179. if(simple_pair_stat)
  180. {
  181. simple_pair_deinit();
  182. simple_pair_stat = 0;
  183. }
  184. }
  185. LOCAL void ICACHE_FLASH_ATTR
  186. ctrller_start_simple_pair(void)
  187. {
  188. int ret;
  189. #ifdef AS_STA
  190. os_printf("dev chn %d\n", wifi_get_channel());
  191. os_printf(" @t %d\n", system_get_time());
  192. // show_espnow_cnt();
  193. if(simple_pair_stat)
  194. {
  195. simple_pair_deinit();
  196. simple_pair_stat = 0;
  197. }
  198. /* init simple pair */
  199. ret = simple_pair_init();
  200. if (ret) {
  201. os_printf("Simple Pair: init error, %d\n", ret);
  202. // return;
  203. goto WAIT_PAIR;
  204. }
  205. simple_pair_stat = 1;
  206. /* register simple pair status callback function */
  207. ret = register_simple_pair_status_cb(sp_status);
  208. if (ret) {
  209. os_printf("Simple Pair: register status cb error, %d\n", ret);
  210. // return;
  211. goto WAIT_PAIR;
  212. }
  213. simple_pair_stat = 2;
  214. os_printf("Simple Pair: STA Enter Scan Mode ...\n");
  215. ret = simple_pair_sta_enter_scan_mode();
  216. if (ret) {
  217. os_printf("Simple Pair: STA Enter Scan Mode Error, %d\n", ret);
  218. // return;
  219. goto WAIT_PAIR;
  220. }
  221. simple_pair_stat = 3;
  222. /* scan ap to searh which ap is ready to simple pair */
  223. os_printf("Simple Pair: STA Scan AP ...\n");
  224. wifi_station_scan(NULL,scan_done);
  225. #endif
  226. WAIT_PAIR:
  227. os_timer_disarm(&dev_espnow_timer);
  228. os_timer_setfn(&dev_espnow_timer, (os_timer_func_t *)ctrller_simple_pair_time_cb, NULL);
  229. os_timer_arm(&dev_espnow_timer, 30000, 0);
  230. }
  231. void esp_now_recv_cb(u8 *mac_addr, u8 *data, u8 len)
  232. {
  233. u8 all_cnt, encrypt_cnt;
  234. if (esp_now_get_cnt_info(&all_cnt, &encrypt_cnt))
  235. os_printf("get_cnt_info failed\r\n");
  236. os_printf("client:%d, encrypted client:%d\r\n", all_cnt, encrypt_cnt);
  237. os_printf("esp_now_recv_cb_t %d from " MACSTR "\n", len, MAC2STR(mac_addr));
  238. data[len-1] = '\0';
  239. os_printf("esp_now_recv_cb_date: %s\n",data);
  240. // user_printbuf(data, len, "nowin");
  241. }
  242. void esp_now_send_cb(u8 *mac_addr, u8 status)
  243. {
  244. os_printf("esp_now_send_cb_t\n");
  245. }
  246. void ICACHE_FLASH_ATTR
  247. init_done(void)
  248. {
  249. ctrller_start_simple_pair();
  250. if(esp_now_init() == 0)
  251. {
  252. os_printf("esp_now_init ok\n");
  253. esp_now_stat = 1;
  254. esp_now_register_recv_cb(esp_now_recv_cb);
  255. esp_now_register_send_cb(esp_now_send_cb);
  256. uint8 ch = PWLDEV_ESP_NOW_CHANNEL_NUM;
  257. wifi_set_channel(ch);
  258. ch = wifi_get_channel();
  259. os_printf("wifi channel is %d\n",ch);
  260. if(esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER) == 0)
  261. os_printf("set myself as slave\n");
  262. // u8 key[16]= {0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44};
  263. // if(esp_now_add_peer(hwaddr, ESP_NOW_ROLE_CONTROLLER, ch, key, 16) == 0)
  264. // os_printf("add local peer addr success\n");
  265. os_printf("esp config success\n");
  266. }
  267. else
  268. {
  269. os_printf("esp_now_init fail\n");
  270. }
  271. }
  272. LOCAL void ICACHE_FLASH_ATTR
  273. ctrller_key_long_press(void)
  274. {
  275. os_printf("%s\n", __FUNCTION__);
  276. ctrller_start_simple_pair();
  277. }
  278. LOCAL void ICACHE_FLASH_ATTR
  279. ctrller_key_short_press(void)
  280. {
  281. os_printf("%s\n", __FUNCTION__);
  282. ap_channel = (ap_channel+1)%12;
  283. if(ap_channel == 0)
  284. ap_channel = 12;
  285. os_printf("set chn %d\n", ap_channel);
  286. }
  287. LOCAL void ICACHE_FLASH_ATTR
  288. device_io_init(void)
  289. {
  290. PIN_FUNC_SELECT(KEY_0_IO_MUX, KEY_0_IO_FUNC);
  291. PIN_FUNC_SELECT(LED_0_OUT_IO_MUX, LED_0_OUT_IO_FUNC);
  292. GPIO_OUTPUT_SET(GPIO_ID_PIN(LED_0_OUT_IO_NUM), 0);
  293. }
  294. void ICACHE_FLASH_ATTR
  295. device_init(void)
  296. {
  297. device_io_init();
  298. single_key[0] = key_init_single(KEY_0_IO_NUM, KEY_0_IO_MUX, KEY_0_IO_FUNC,
  299. ctrller_key_long_press, ctrller_key_short_press);
  300. keys.key_num = KEY_NUM;
  301. keys.single_key = single_key;
  302. key_init(&keys);
  303. #ifdef AS_STA
  304. wifi_set_opmode(STATION_MODE);
  305. #endif
  306. system_init_done_cb(init_done);
  307. }
  308. #endif // ESPNOW_CONTROLLER device