app_main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Wi-Fi Provisioning Manager Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <freertos/FreeRTOS.h>
  10. #include <freertos/task.h>
  11. #include <freertos/event_groups.h>
  12. #include <esp_log.h>
  13. #include <esp_wifi.h>
  14. #include <esp_event_loop.h>
  15. #include <nvs_flash.h>
  16. #include <wifi_provisioning/manager.h>
  17. #include <wifi_provisioning/scheme_ble.h>
  18. #include <wifi_provisioning/scheme_softap.h>
  19. static const char *TAG = "app";
  20. /* Signal Wi-Fi events on this event-group */
  21. const int WIFI_CONNECTED_EVENT = BIT0;
  22. static EventGroupHandle_t wifi_event_group;
  23. /* Event handler for catching system events */
  24. static esp_err_t event_handler(void *ctx, system_event_t *event)
  25. {
  26. /* Pass event information to provisioning manager so that it can
  27. * maintain its internal state depending upon the system event */
  28. wifi_prov_mgr_event_handler(ctx, event);
  29. /* Global event handling */
  30. switch (event->event_id) {
  31. case SYSTEM_EVENT_STA_START:
  32. esp_wifi_connect();
  33. break;
  34. case SYSTEM_EVENT_STA_GOT_IP:
  35. ESP_LOGI(TAG, "Connected with IP Address:%s",
  36. ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  37. xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT);
  38. break;
  39. case SYSTEM_EVENT_STA_DISCONNECTED:
  40. ESP_LOGI(TAG, "Disconnected. Connecting to the AP again...");
  41. esp_wifi_connect();
  42. break;
  43. default:
  44. break;
  45. }
  46. return ESP_OK;
  47. }
  48. /* Event handler for catching provisioning manager events */
  49. static void prov_event_handler(void *user_data,
  50. wifi_prov_cb_event_t event, void *event_data)
  51. {
  52. switch (event) {
  53. case WIFI_PROV_START:
  54. ESP_LOGI(TAG, "Provisioning started");
  55. break;
  56. case WIFI_PROV_CRED_RECV: {
  57. wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
  58. /* If SSID length is exactly 32 bytes, null termination
  59. * will not be present, so explicitly obtain the length */
  60. size_t ssid_len = strnlen((const char *)wifi_sta_cfg->ssid, sizeof(wifi_sta_cfg->ssid));
  61. ESP_LOGI(TAG, "Received Wi-Fi credentials"
  62. "\n\tSSID : %.*s\n\tPassword : %s",
  63. ssid_len, (const char *) wifi_sta_cfg->ssid,
  64. (const char *) wifi_sta_cfg->password);
  65. break;
  66. }
  67. case WIFI_PROV_CRED_FAIL: {
  68. wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
  69. ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
  70. "\n\tPlease reset to factory and retry provisioning",
  71. (*reason == WIFI_PROV_STA_AUTH_ERROR) ?
  72. "Wi-Fi AP password incorrect" : "Wi-Fi AP not found");
  73. break;
  74. }
  75. case WIFI_PROV_CRED_SUCCESS:
  76. ESP_LOGI(TAG, "Provisioning successful");
  77. break;
  78. case WIFI_PROV_END:
  79. /* De-initialize manager once provisioning is finished */
  80. wifi_prov_mgr_deinit();
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. static void wifi_init_sta()
  87. {
  88. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  89. ESP_ERROR_CHECK(esp_wifi_start());
  90. }
  91. static void get_device_service_name(char *service_name, size_t max)
  92. {
  93. uint8_t eth_mac[6];
  94. const char *ssid_prefix = "PROV_";
  95. esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
  96. snprintf(service_name, max, "%s%02X%02X%02X",
  97. ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
  98. }
  99. void app_main()
  100. {
  101. /* Initialize NVS partition */
  102. esp_err_t ret = nvs_flash_init();
  103. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  104. /* NVS partition was truncated
  105. * and needs to be erased */
  106. ESP_ERROR_CHECK(nvs_flash_erase());
  107. /* Retry nvs_flash_init */
  108. ESP_ERROR_CHECK(nvs_flash_init());
  109. }
  110. /* Initialize TCP/IP and the event loop */
  111. tcpip_adapter_init();
  112. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  113. wifi_event_group = xEventGroupCreate();
  114. /* Initialize Wi-Fi */
  115. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  116. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  117. /* Configuration for the provisioning manager */
  118. wifi_prov_mgr_config_t config = {
  119. /* What is the Provisioning Scheme that we want ?
  120. * wifi_prov_scheme_softap or wifi_prov_scheme_ble */
  121. .scheme = wifi_prov_scheme_ble,
  122. /* Any default scheme specific event handler that you would
  123. * like to choose. Since our example application requires
  124. * neither BT nor BLE, we can choose to release the associated
  125. * memory once provisioning is complete, or not needed
  126. * (in case when device is already provisioned). Choosing
  127. * appropriate scheme specific event handler allows the manager
  128. * to take care of this automatically. This can be set to
  129. * WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/
  130. .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM,
  131. /* Do we want an application specific handler be executed on
  132. * various provisioning related events */
  133. .app_event_handler = {
  134. .event_cb = prov_event_handler,
  135. .user_data = NULL
  136. }
  137. };
  138. /* Initialize provisioning manager with the
  139. * configuration parameters set above */
  140. ESP_ERROR_CHECK(wifi_prov_mgr_init(config));
  141. bool provisioned = false;
  142. /* Let's find out if the device is provisioned */
  143. ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));
  144. /* If device is not yet provisioned start provisioning service */
  145. if (!provisioned) {
  146. ESP_LOGI(TAG, "Starting provisioning");
  147. /* What is the Device Service Name that we want
  148. * This translates to :
  149. * - Wi-Fi SSID when scheme is wifi_prov_scheme_softap
  150. * - device name when scheme is wifi_prov_scheme_ble
  151. */
  152. char service_name[12];
  153. get_device_service_name(service_name, sizeof(service_name));
  154. /* What is the security level that we want (0 or 1):
  155. * - WIFI_PROV_SECURITY_0 is simply plain text communication.
  156. * - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake
  157. * using X25519 key exchange and proof of possession (pop) and AES-CTR
  158. * for encryption/decryption of messages.
  159. */
  160. wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
  161. /* Do we want a proof-of-possession (ignored if Security 0 is selected):
  162. * - this should be a string with length > 0
  163. * - NULL if not used
  164. */
  165. const char *pop = "abcd1234";
  166. /* What is the service key (could be NULL)
  167. * This translates to :
  168. * - Wi-Fi password when scheme is wifi_prov_scheme_softap
  169. * - simply ignored when scheme is wifi_prov_scheme_ble
  170. */
  171. const char *service_key = NULL;
  172. /* This step is only useful when scheme is wifi_prov_scheme_ble. This will
  173. * set a custom 128 bit UUID which will be included in the BLE advertisement
  174. * and will correspond to the primary GATT service that provides provisioning
  175. * endpoints as GATT characteristics. Each GATT characteristic will be
  176. * formed using the primary service UUID as base, with different auto assigned
  177. * 12th and 13th bytes (assume counting starts from 0th byte). The client side
  178. * applications must identify the endpoints by reading the User Characteristic
  179. * Description descriptor (0x2901) for each characteristic, which contains the
  180. * endpoint name of the characteristic */
  181. uint8_t custom_service_uuid[] = {
  182. /* LSB <---------------------------------------
  183. * ---------------------------------------> MSB */
  184. 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
  185. 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
  186. };
  187. wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid);
  188. /* Start provisioning service */
  189. ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
  190. /* Uncomment the following to wait for the provisioning to finish and then release
  191. * the resources of the manager. Since in this case de-initialization is triggered
  192. * by the configured prov_event_handler(), we don't need to call the following */
  193. // wifi_prov_mgr_wait();
  194. // wifi_prov_mgr_deinit();
  195. } else {
  196. ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");
  197. /* We don't need the manager as device is already provisioned,
  198. * so let's release it's resources */
  199. wifi_prov_mgr_deinit();
  200. /* Start Wi-Fi station */
  201. wifi_init_sta();
  202. }
  203. /* Wait for Wi-Fi connection */
  204. xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);
  205. /* Start main application now */
  206. while (1) {
  207. ESP_LOGI(TAG, "Hello World!");
  208. vTaskDelay(1000 / portTICK_PERIOD_MS);
  209. }
  210. }