app_main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.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 void event_handler(void* arg, esp_event_base_t event_base,
  25. int event_id, void* event_data)
  26. {
  27. if (event_base == WIFI_PROV_EVENT) {
  28. switch (event_id) {
  29. case WIFI_PROV_START:
  30. ESP_LOGI(TAG, "Provisioning started");
  31. break;
  32. case WIFI_PROV_CRED_RECV: {
  33. wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
  34. ESP_LOGI(TAG, "Received Wi-Fi credentials"
  35. "\n\tSSID : %s\n\tPassword : %s",
  36. (const char *) wifi_sta_cfg->ssid,
  37. (const char *) wifi_sta_cfg->password);
  38. break;
  39. }
  40. case WIFI_PROV_CRED_FAIL: {
  41. wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
  42. ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
  43. "\n\tPlease reset to factory and retry provisioning",
  44. (*reason == WIFI_PROV_STA_AUTH_ERROR) ?
  45. "Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
  46. break;
  47. }
  48. case WIFI_PROV_CRED_SUCCESS:
  49. ESP_LOGI(TAG, "Provisioning successful");
  50. break;
  51. case WIFI_PROV_END:
  52. /* De-initialize manager once provisioning is finished */
  53. wifi_prov_mgr_deinit();
  54. break;
  55. default:
  56. break;
  57. }
  58. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  59. esp_wifi_connect();
  60. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  61. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  62. ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip));
  63. /* Signal main application to continue execution */
  64. xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT);
  65. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  66. ESP_LOGI(TAG, "Disconnected. Connecting to the AP again...");
  67. esp_wifi_connect();
  68. }
  69. }
  70. static void wifi_init_sta(void)
  71. {
  72. /* Start Wi-Fi in station mode */
  73. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  74. ESP_ERROR_CHECK(esp_wifi_start());
  75. }
  76. static void get_device_service_name(char *service_name, size_t max)
  77. {
  78. uint8_t eth_mac[6];
  79. const char *ssid_prefix = "PROV_";
  80. esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
  81. snprintf(service_name, max, "%s%02X%02X%02X",
  82. ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
  83. }
  84. void app_main(void)
  85. {
  86. /* Initialize NVS partition */
  87. esp_err_t ret = nvs_flash_init();
  88. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  89. /* NVS partition was truncated
  90. * and needs to be erased */
  91. ESP_ERROR_CHECK(nvs_flash_erase());
  92. /* Retry nvs_flash_init */
  93. ESP_ERROR_CHECK(nvs_flash_init());
  94. }
  95. /* Initialize TCP/IP */
  96. ESP_ERROR_CHECK(esp_netif_init());
  97. /* Initialize the event loop */
  98. ESP_ERROR_CHECK(esp_event_loop_create_default());
  99. wifi_event_group = xEventGroupCreate();
  100. /* Register our event handler for Wi-Fi, IP and Provisioning related events */
  101. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  102. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  103. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  104. /* Initialize Wi-Fi including netif with default config */
  105. esp_netif_create_default_wifi_sta();
  106. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  107. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  108. /* Configuration for the provisioning manager */
  109. wifi_prov_mgr_config_t config = {
  110. /* What is the Provisioning Scheme that we want ?
  111. * wifi_prov_scheme_softap or wifi_prov_scheme_ble */
  112. .scheme = wifi_prov_scheme_ble,
  113. /* Any default scheme specific event handler that you would
  114. * like to choose. Since our example application requires
  115. * neither BT nor BLE, we can choose to release the associated
  116. * memory once provisioning is complete, or not needed
  117. * (in case when device is already provisioned). Choosing
  118. * appropriate scheme specific event handler allows the manager
  119. * to take care of this automatically. This can be set to
  120. * WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/
  121. .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
  122. };
  123. /* Initialize provisioning manager with the
  124. * configuration parameters set above */
  125. ESP_ERROR_CHECK(wifi_prov_mgr_init(config));
  126. bool provisioned = false;
  127. /* Let's find out if the device is provisioned */
  128. ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));
  129. /* If device is not yet provisioned start provisioning service */
  130. if (!provisioned) {
  131. ESP_LOGI(TAG, "Starting provisioning");
  132. /* What is the Device Service Name that we want
  133. * This translates to :
  134. * - Wi-Fi SSID when scheme is wifi_prov_scheme_softap
  135. * - device name when scheme is wifi_prov_scheme_ble
  136. */
  137. char service_name[12];
  138. get_device_service_name(service_name, sizeof(service_name));
  139. /* What is the security level that we want (0 or 1):
  140. * - WIFI_PROV_SECURITY_0 is simply plain text communication.
  141. * - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake
  142. * using X25519 key exchange and proof of possession (pop) and AES-CTR
  143. * for encryption/decryption of messages.
  144. */
  145. wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
  146. /* Do we want a proof-of-possession (ignored if Security 0 is selected):
  147. * - this should be a string with length > 0
  148. * - NULL if not used
  149. */
  150. const char *pop = "abcd1234";
  151. /* What is the service key (could be NULL)
  152. * This translates to :
  153. * - Wi-Fi password when scheme is wifi_prov_scheme_softap
  154. * - simply ignored when scheme is wifi_prov_scheme_ble
  155. */
  156. const char *service_key = NULL;
  157. /* This step is only useful when scheme is wifi_prov_scheme_ble. This will
  158. * set a custom 128 bit UUID which will be included in the BLE advertisement
  159. * and will correspond to the primary GATT service that provides provisioning
  160. * endpoints as GATT characteristics. Each GATT characteristic will be
  161. * formed using the primary service UUID as base, with different auto assigned
  162. * 12th and 13th bytes (assume counting starts from 0th byte). The client side
  163. * applications must identify the endpoints by reading the User Characteristic
  164. * Description descriptor (0x2901) for each characteristic, which contains the
  165. * endpoint name of the characteristic */
  166. uint8_t custom_service_uuid[] = {
  167. /* LSB <---------------------------------------
  168. * ---------------------------------------> MSB */
  169. 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
  170. 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
  171. };
  172. wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid);
  173. /* Start provisioning service */
  174. ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
  175. /* Uncomment the following to wait for the provisioning to finish and then release
  176. * the resources of the manager. Since in this case de-initialization is triggered
  177. * by the default event loop handler, we don't need to call the following */
  178. // wifi_prov_mgr_wait();
  179. // wifi_prov_mgr_deinit();
  180. } else {
  181. ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");
  182. /* We don't need the manager as device is already provisioned,
  183. * so let's release it's resources */
  184. wifi_prov_mgr_deinit();
  185. /* Start Wi-Fi station */
  186. wifi_init_sta();
  187. }
  188. /* Wait for Wi-Fi connection */
  189. xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);
  190. /* Start main application now */
  191. while (1) {
  192. ESP_LOGI(TAG, "Hello World!");
  193. vTaskDelay(1000 / portTICK_PERIOD_MS);
  194. }
  195. }