1 /* Local Ctrl Example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <sys/param.h>
13 #include <string.h>
14 
15 #include <freertos/FreeRTOS.h>
16 #include <freertos/task.h>
17 
18 #include <mdns.h>
19 #include <esp_log.h>
20 #include <esp_timer.h>
21 #include <esp_local_ctrl.h>
22 #include <esp_https_server.h>
23 
24 static const char *TAG = "control";
25 
26 #define SERVICE_NAME "my_esp_ctrl_device"
27 
28 /* Custom allowed property types */
29 enum property_types {
30     PROP_TYPE_TIMESTAMP = 0,
31     PROP_TYPE_INT32,
32     PROP_TYPE_BOOLEAN,
33     PROP_TYPE_STRING,
34 };
35 
36 /* Custom flags that can be set for a property */
37 enum property_flags {
38     PROP_FLAG_READONLY = (1 << 0)
39 };
40 
41 /********* Handler functions for responding to control requests / commands *********/
42 
get_property_values(size_t props_count,const esp_local_ctrl_prop_t props[],esp_local_ctrl_prop_val_t prop_values[],void * usr_ctx)43 static esp_err_t get_property_values(size_t props_count,
44                                      const esp_local_ctrl_prop_t props[],
45                                      esp_local_ctrl_prop_val_t prop_values[],
46                                      void *usr_ctx)
47 {
48     for (uint32_t i = 0; i < props_count; i++) {
49         ESP_LOGI(TAG, "Reading property : %s", props[i].name);
50         /* For the purpose of this example, to keep things simple
51          * we have set the context pointer of each property to
52          * point to its value (except for timestamp) */
53         switch (props[i].type) {
54             case PROP_TYPE_INT32:
55             case PROP_TYPE_BOOLEAN:
56                 /* No need to set size for these types as sizes where
57                  * specified when declaring the properties, unlike for
58                  * string type. */
59                 prop_values[i].data = props[i].ctx;
60                 break;
61             case PROP_TYPE_TIMESTAMP: {
62                 /* Get the time stamp */
63                 static int64_t ts = 0;
64                 ts = esp_timer_get_time();
65 
66                 /* Set the current time. Since this is statically
67                  * allocated, we don't need to provide a free_fn */
68                 prop_values[i].data = &ts;
69                 break;
70             }
71             case PROP_TYPE_STRING: {
72                 char **prop3_value = (char **) props[i].ctx;
73                 if (*prop3_value == NULL) {
74                     prop_values[i].size = 0;
75                     prop_values[i].data = NULL;
76                 } else {
77                     /* We could try dynamically allocating the output value,
78                      * and it should get freed automatically after use, as
79                      * `esp_local_ctrl` internally calls the provided `free_fn` */
80                     prop_values[i].size = strlen(*prop3_value);
81                     prop_values[i].data = strdup(*prop3_value);
82                     if (!prop_values[i].data) {
83                         return ESP_ERR_NO_MEM;
84                     }
85                     prop_values[i].free_fn = free;
86                 }
87             }
88             default:
89                 break;
90         }
91     }
92     return ESP_OK;
93 }
94 
set_property_values(size_t props_count,const esp_local_ctrl_prop_t props[],const esp_local_ctrl_prop_val_t prop_values[],void * usr_ctx)95 static esp_err_t set_property_values(size_t props_count,
96                                      const esp_local_ctrl_prop_t props[],
97                                      const esp_local_ctrl_prop_val_t prop_values[],
98                                      void *usr_ctx)
99 {
100     for (uint32_t i = 0; i < props_count; i++) {
101         /* Cannot set the value of a read-only property */
102         if (props[i].flags & PROP_FLAG_READONLY) {
103             ESP_LOGE(TAG, "%s is read-only", props[i].name);
104             return ESP_ERR_INVALID_ARG;
105         }
106         /* For the purpose of this example, to keep things simple
107          * we have set the context pointer of each property to
108          * point to its value (except for timestamp) */
109         switch (props[i].type) {
110             case PROP_TYPE_STRING: {
111                     /* Free the previously set string */
112                     char **prop3_value = (char **) props[i].ctx;
113                     free(*prop3_value);
114                     *prop3_value = NULL;
115 
116                     /* Copy the input string */
117                     if (prop_values[i].size) {
118                         *prop3_value = strndup((const char *)prop_values[i].data, prop_values[i].size);
119                         if (*prop3_value == NULL) {
120                             return ESP_ERR_NO_MEM;
121                         }
122                         ESP_LOGI(TAG, "Setting %s value to %s", props[i].name, (const char*)*prop3_value);
123                     }
124                 }
125                 break;
126             case PROP_TYPE_INT32: {
127                     const int32_t *new_value = (const int32_t *) prop_values[i].data;
128                     ESP_LOGI(TAG, "Setting %s value to %d", props[i].name, *new_value);
129                     memcpy(props[i].ctx, new_value, sizeof(int32_t));
130                 }
131                 break;
132             case PROP_TYPE_BOOLEAN: {
133                     const bool *value = (const bool *) prop_values[i].data;
134                     ESP_LOGI(TAG, "Setting %s value to %d", props[i].name, *value);
135                     memcpy(props[i].ctx, value, sizeof(bool));
136                 }
137                 break;
138             default:
139                 break;
140         }
141     }
142     return ESP_OK;
143 }
144 
145 /******************************************************************************/
146 
147 /* A custom free_fn to free a pointer to a string as
148  * well as the string being pointed to */
free_str(void * arg)149 static void free_str(void *arg)
150 {
151     char **ptr_to_strptr = (char **)arg;
152     if (ptr_to_strptr) {
153         free(*ptr_to_strptr);
154         free(ptr_to_strptr);
155     }
156 }
157 
158 /* Function used by app_main to start the esp_local_ctrl service */
start_esp_local_ctrl_service(void)159 void start_esp_local_ctrl_service(void)
160 {
161     /* Set the configuration */
162     httpd_ssl_config_t https_conf = HTTPD_SSL_CONFIG_DEFAULT();
163 
164     /* Load server certificate */
165     extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
166     extern const unsigned char cacert_pem_end[]   asm("_binary_cacert_pem_end");
167     https_conf.cacert_pem = cacert_pem_start;
168     https_conf.cacert_len = cacert_pem_end - cacert_pem_start;
169 
170     /* Load server private key */
171     extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
172     extern const unsigned char prvtkey_pem_end[]   asm("_binary_prvtkey_pem_end");
173     https_conf.prvtkey_pem = prvtkey_pem_start;
174     https_conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
175 
176     esp_local_ctrl_config_t config = {
177         .transport = ESP_LOCAL_CTRL_TRANSPORT_HTTPD,
178         .transport_config = {
179             .httpd = &https_conf
180         },
181         .proto_sec = {
182             .version = 0,
183             .custom_handle = NULL,
184             .pop = NULL,
185         },
186         .handlers = {
187             /* User defined handler functions */
188             .get_prop_values = get_property_values,
189             .set_prop_values = set_property_values,
190             .usr_ctx         = NULL,
191             .usr_ctx_free_fn = NULL
192         },
193         /* Maximum number of properties that may be set */
194         .max_properties = 10
195     };
196 
197     mdns_init();
198     mdns_hostname_set(SERVICE_NAME);
199 
200     /* Start esp_local_ctrl service */
201     ESP_ERROR_CHECK(esp_local_ctrl_start(&config));
202     ESP_LOGI(TAG, "esp_local_ctrl service started with name : %s", SERVICE_NAME);
203 
204     /* Create a timestamp property. The client should see this as a read-only property.
205      * Property value is fetched using `esp_timer_get_time()` in the `get_prop_values`
206      * handler */
207     esp_local_ctrl_prop_t timestamp = {
208         .name        = "timestamp (us)",
209         .type        = PROP_TYPE_TIMESTAMP,
210         .size        = sizeof(int64_t),
211         .flags       = PROP_FLAG_READONLY,
212         .ctx         = NULL,
213         .ctx_free_fn = NULL
214     };
215 
216     /* Create a writable integer property. Use dynamically allocated memory
217      * for storing its value and pass it as context, so that it can be accessed
218      * inside the set / get handlers. */
219     int32_t *prop1_value = malloc(sizeof(int32_t));
220     assert(prop1_value != NULL);
221 
222     /* Initialize the property value */
223     *prop1_value = 123456789;
224 
225     /* Populate the property structure accordingly. Since, we would want the memory
226      * occupied by the property value to be freed automatically upon call to
227      * `esp_local_ctrl_stop()` or `esp_local_ctrl_remove_property()`, the `ctx_free_fn`
228      * field will need to be set with the appropriate de-allocation function,
229      * which in this case is simply `free()` */
230     esp_local_ctrl_prop_t property1 = {
231         .name        = "property1",
232         .type        = PROP_TYPE_INT32,
233         .size        = sizeof(int32_t),
234         .flags       = 0,
235         .ctx         = prop1_value,
236         .ctx_free_fn = free
237     };
238 
239     /* Create another read-only property. Just for demonstration, we use statically
240      * allocated value. No `ctx_free_fn` needs to be set for this */
241     static bool prop2_value = false;
242 
243     esp_local_ctrl_prop_t property2 = {
244         .name        = "property2",
245         .type        = PROP_TYPE_BOOLEAN,
246         .size        = sizeof(bool),
247         .flags       = PROP_FLAG_READONLY,
248         .ctx         = &prop2_value,
249         .ctx_free_fn = NULL
250     };
251 
252     /* Create a variable sized property. Its context is a pointer for storing the
253      * pointer to a dynamically allocate string, therefore it will require a
254      * customized free function `free_str()` */
255     char **prop3_value = calloc(1, sizeof(char *));
256     assert(prop3_value != NULL);
257 
258     esp_local_ctrl_prop_t property3 = {
259         .name        = "property3",
260         .type        = PROP_TYPE_STRING,
261         .size        = 0, // When zero, this is assumed to be of variable size
262         .flags       = 0,
263         .ctx         = prop3_value,
264         .ctx_free_fn = free_str
265     };
266 
267     /* Now register the properties */
268     ESP_ERROR_CHECK(esp_local_ctrl_add_property(&timestamp));
269     ESP_ERROR_CHECK(esp_local_ctrl_add_property(&property1));
270     ESP_ERROR_CHECK(esp_local_ctrl_add_property(&property2));
271     ESP_ERROR_CHECK(esp_local_ctrl_add_property(&property3));
272 
273     /* Just for fun, let us keep toggling the value
274      * of the boolean property2, every 1 second */
275     while (1) {
276         vTaskDelay(1000 / portTICK_RATE_MS);
277         prop2_value = !prop2_value;
278     }
279 }
280