esp_local_ctrl_service.c 10 KB

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