1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include <esp_log.h>
17 #include <esp_err.h>
18 #include <esp_wifi.h>
19 
20 #include <protocomm.h>
21 #include <protocomm_console.h>
22 
23 #include "wifi_provisioning/scheme_console.h"
24 #include "wifi_provisioning_priv.h"
25 
26 static const char *TAG = "wifi_prov_scheme_console";
27 
28 extern const wifi_prov_scheme_t wifi_prov_scheme_console;
29 
prov_start(protocomm_t * pc,void * config)30 static esp_err_t prov_start(protocomm_t *pc, void *config)
31 {
32     if (!pc) {
33         ESP_LOGE(TAG, "Protocomm handle cannot be null");
34         return ESP_ERR_INVALID_ARG;
35     }
36 
37     if (!config) {
38         ESP_LOGE(TAG, "Cannot start with null configuration");
39         return ESP_ERR_INVALID_ARG;
40     }
41 
42     protocomm_console_config_t *console_config = (protocomm_console_config_t *) config;
43 
44     /* Start protocomm console */
45     esp_err_t err = protocomm_console_start(pc, console_config);
46     if (err != ESP_OK) {
47         ESP_LOGE(TAG, "Failed to start protocomm HTTP server");
48         return ESP_FAIL;
49     }
50     return ESP_OK;
51 }
52 
new_config(void)53 static void *new_config(void)
54 {
55     protocomm_console_config_t *console_config = malloc(sizeof(protocomm_console_config_t));
56     if (!console_config) {
57         ESP_LOGE(TAG, "Error allocating memory for new configuration");
58         return NULL;
59     }
60     protocomm_console_config_t default_config = PROTOCOMM_CONSOLE_DEFAULT_CONFIG();
61     memcpy(console_config, &default_config, sizeof(default_config));
62     return console_config;
63 }
64 
delete_config(void * config)65 static void delete_config(void *config)
66 {
67     if (!config) {
68         ESP_LOGE(TAG, "Cannot delete null configuration");
69         return;
70     }
71     free(config);
72 }
73 
set_config_service(void * config,const char * service_name,const char * service_key)74 static esp_err_t set_config_service(void *config, const char *service_name, const char *service_key)
75 {
76     return ESP_OK;
77 }
78 
set_config_endpoint(void * config,const char * endpoint_name,uint16_t uuid)79 static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, uint16_t uuid)
80 {
81     return ESP_OK;
82 }
83 
84 const wifi_prov_scheme_t wifi_prov_scheme_console = {
85     .prov_start          = prov_start,
86     .prov_stop           = protocomm_console_stop,
87     .new_config          = new_config,
88     .delete_config       = delete_config,
89     .set_config_service  = set_config_service,
90     .set_config_endpoint = set_config_endpoint,
91     .wifi_mode           = WIFI_MODE_STA
92 };
93