app_main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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:%s", ip4addr_ntoa(&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()
  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()
  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. tcpip_adapter_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 */
  105. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  106. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  107. /* Configuration for the provisioning manager */
  108. wifi_prov_mgr_config_t config = {
  109. /* What is the Provisioning Scheme that we want ?
  110. * wifi_prov_scheme_softap or wifi_prov_scheme_ble */
  111. .scheme = wifi_prov_scheme_ble,
  112. /* Any default scheme specific event handler that you would
  113. * like to choose. Since our example application requires
  114. * neither BT nor BLE, we can choose to release the associated
  115. * memory once provisioning is complete, or not needed
  116. * (in case when device is already provisioned). Choosing
  117. * appropriate scheme specific event handler allows the manager
  118. * to take care of this automatically. This can be set to
  119. * WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/
  120. .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
  121. };
  122. /* Initialize provisioning manager with the
  123. * configuration parameters set above */
  124. ESP_ERROR_CHECK(wifi_prov_mgr_init(config));
  125. bool provisioned = false;
  126. /* Let's find out if the device is provisioned */
  127. ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));
  128. /* If device is not yet provisioned start provisioning service */
  129. if (!provisioned) {
  130. ESP_LOGI(TAG, "Starting provisioning");
  131. /* What is the Device Service Name that we want
  132. * This translates to :
  133. * - Wi-Fi SSID when scheme is wifi_prov_scheme_softap
  134. * - device name when scheme is wifi_prov_scheme_ble
  135. */
  136. char service_name[12];
  137. get_device_service_name(service_name, sizeof(service_name));
  138. /* What is the security level that we want (0 or 1):
  139. * - WIFI_PROV_SECURITY_0 is simply plain text communication.
  140. * - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake
  141. * using X25519 key exchange and proof of possession (pop) and AES-CTR
  142. * for encryption/decryption of messages.
  143. */
  144. wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
  145. /* Do we want a proof-of-possession (ignored if Security 0 is selected):
  146. * - this should be a string with length > 0
  147. * - NULL if not used
  148. */
  149. const char *pop = "abcd1234";
  150. /* What is the service key (could be NULL)
  151. * This translates to :
  152. * - Wi-Fi password when scheme is wifi_prov_scheme_softap
  153. * - simply ignored when scheme is wifi_prov_scheme_ble
  154. */
  155. const char *service_key = NULL;
  156. /* This step is only useful when scheme is wifi_prov_scheme_ble. This will
  157. * set a custom 128 bit UUID which will be included in the BLE advertisement
  158. * and will correspond to the primary GATT service that provides provisioning
  159. * endpoints as GATT characteristics. Each GATT characteristic will be
  160. * formed using the primary service UUID as base, with different auto assigned
  161. * 12th and 13th bytes (assume counting starts from 0th byte). The client side
  162. * applications must identify the endpoints by reading the User Characteristic
  163. * Description descriptor (0x2901) for each characteristic, which contains the
  164. * endpoint name of the characteristic */
  165. uint8_t custom_service_uuid[] = {
  166. /* LSB <---------------------------------------
  167. * ---------------------------------------> MSB */
  168. 0x21, 0x43, 0x65, 0x87, 0x09, 0xba, 0xdc, 0xfe,
  169. 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12
  170. };
  171. wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid);
  172. /* Start provisioning service */
  173. ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
  174. /* Uncomment the following to wait for the provisioning to finish and then release
  175. * the resources of the manager. Since in this case de-initialization is triggered
  176. * by the default event loop handler, we don't need to call the following */
  177. // wifi_prov_mgr_wait();
  178. // wifi_prov_mgr_deinit();
  179. } else {
  180. ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");
  181. /* We don't need the manager as device is already provisioned,
  182. * so let's release it's resources */
  183. wifi_prov_mgr_deinit();
  184. /* Start Wi-Fi station */
  185. wifi_init_sta();
  186. }
  187. /* Wait for Wi-Fi connection */
  188. xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);
  189. /* Start main application now */
  190. while (1) {
  191. ESP_LOGI(TAG, "Hello World!");
  192. vTaskDelay(1000 / portTICK_PERIOD_MS);
  193. }
  194. }