tools.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * (c) Philippe G. 20201, philippe_44@outlook.com
  3. * see other copyrights below
  4. *
  5. * This software is released under the MIT License.
  6. * https://opensource.org/licenses/MIT
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "esp_task.h"
  17. #include "esp_tls.h"
  18. #include "esp_http_client.h"
  19. #include "esp_heap_caps.h"
  20. #include "esp_log.h"
  21. #include "tools.h"
  22. #if CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS < 2
  23. #error CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS must be at least 2
  24. #endif
  25. const static char TAG[] = "tools";
  26. /****************************************************************************************
  27. * UTF-8 tools
  28. */
  29. // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  30. // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  31. // Copyright (c) 2017 ZephRay <zephray@outlook.com>
  32. //
  33. // utf8to1252 - almost equivalent to iconv -f utf-8 -t windows-1252, but better
  34. #define UTF8_ACCEPT 0
  35. #define UTF8_REJECT 1
  36. static const uint8_t utf8d[] = {
  37. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f
  38. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f
  39. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f
  40. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f
  41. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
  42. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
  43. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
  44. 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
  45. 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
  46. 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
  47. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
  48. 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
  49. 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
  50. 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
  51. };
  52. static uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
  53. uint32_t type = utf8d[byte];
  54. *codep = (*state != UTF8_ACCEPT) ?
  55. (byte & 0x3fu) | (*codep << 6) :
  56. (0xff >> type) & (byte);
  57. *state = utf8d[256 + *state*16 + type];
  58. return *state;
  59. }
  60. static uint8_t UNICODEtoCP1252(uint16_t chr) {
  61. if (chr <= 0xff)
  62. return (chr&0xff);
  63. else {
  64. ESP_LOGI(TAG, "some multi-byte %hx", chr);
  65. switch(chr) {
  66. case 0x20ac: return 0x80; break;
  67. case 0x201a: return 0x82; break;
  68. case 0x0192: return 0x83; break;
  69. case 0x201e: return 0x84; break;
  70. case 0x2026: return 0x85; break;
  71. case 0x2020: return 0x86; break;
  72. case 0x2021: return 0x87; break;
  73. case 0x02c6: return 0x88; break;
  74. case 0x2030: return 0x89; break;
  75. case 0x0160: return 0x8a; break;
  76. case 0x2039: return 0x8b; break;
  77. case 0x0152: return 0x8c; break;
  78. case 0x017d: return 0x8e; break;
  79. case 0x2018: return 0x91; break;
  80. case 0x2019: return 0x92; break;
  81. case 0x201c: return 0x93; break;
  82. case 0x201d: return 0x94; break;
  83. case 0x2022: return 0x95; break;
  84. case 0x2013: return 0x96; break;
  85. case 0x2014: return 0x97; break;
  86. case 0x02dc: return 0x98; break;
  87. case 0x2122: return 0x99; break;
  88. case 0x0161: return 0x9a; break;
  89. case 0x203a: return 0x9b; break;
  90. case 0x0153: return 0x9c; break;
  91. case 0x017e: return 0x9e; break;
  92. case 0x0178: return 0x9f; break;
  93. default: return 0x00; break;
  94. }
  95. }
  96. }
  97. void utf8_decode(char *src) {
  98. uint32_t codep = 0, state = UTF8_ACCEPT;
  99. char *dst = src;
  100. while (src && *src) {
  101. if (!decode(&state, &codep, *src++)) *dst++ = UNICODEtoCP1252(codep);
  102. }
  103. *dst = '\0';
  104. }
  105. /****************************************************************************************
  106. * URL tools
  107. */
  108. static inline char from_hex(char ch) {
  109. return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  110. }
  111. void url_decode(char *url) {
  112. char *p, *src = strdup(url);
  113. for (p = src; *src; url++) {
  114. *url = *src++;
  115. if (*url == '%') {
  116. *url = from_hex(*src++) << 4;
  117. *url |= from_hex(*src++);
  118. } else if (*url == '+') {
  119. *url = ' ';
  120. }
  121. }
  122. *url = '\0';
  123. free(p);
  124. }
  125. /****************************************************************************************
  126. * Memory tools
  127. */
  128. void * malloc_init_external(size_t sz){
  129. void * ptr=NULL;
  130. ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  131. if(ptr==NULL){
  132. ESP_LOGE(TAG,"malloc_init_external: unable to allocate %d bytes of PSRAM!",sz);
  133. }
  134. else {
  135. memset(ptr,0x00,sz);
  136. }
  137. return ptr;
  138. }
  139. void * clone_obj_psram(void * source, size_t source_sz){
  140. void * ptr=NULL;
  141. ptr = heap_caps_malloc(source_sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  142. if(ptr==NULL){
  143. ESP_LOGE(TAG,"clone_obj_psram: unable to allocate %d bytes of PSRAM!",source_sz);
  144. }
  145. else {
  146. memcpy(ptr,source,source_sz);
  147. }
  148. return ptr;
  149. }
  150. char * strdup_psram(const char * source){
  151. void * ptr=NULL;
  152. size_t source_sz = strlen(source)+1;
  153. ptr = heap_caps_malloc(source_sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  154. if(ptr==NULL){
  155. ESP_LOGE(TAG,"strdup_psram: unable to allocate %d bytes of PSRAM! Cannot clone string %s",source_sz,source);
  156. }
  157. else {
  158. memset(ptr,0x00,source_sz);
  159. strcpy(ptr,source);
  160. }
  161. return ptr;
  162. }
  163. /****************************************************************************************
  164. * Task manager
  165. */
  166. #define TASK_TLS_INDEX 1
  167. typedef struct {
  168. StaticTask_t *xTaskBuffer;
  169. StackType_t *xStack;
  170. } task_context_t;
  171. static void task_cleanup(int index, task_context_t *context) {
  172. free(context->xTaskBuffer);
  173. free(context->xStack);
  174. free(context);
  175. }
  176. BaseType_t xTaskCreateEXTRAM( TaskFunction_t pvTaskCode,
  177. const char * const pcName,
  178. configSTACK_DEPTH_TYPE usStackDepth,
  179. void *pvParameters,
  180. UBaseType_t uxPriority,
  181. TaskHandle_t *pxCreatedTask) {
  182. // create the worker task as a static
  183. task_context_t *context = calloc(1, sizeof(task_context_t));
  184. context->xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
  185. context->xStack = heap_caps_malloc(usStackDepth,(MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT));
  186. TaskHandle_t handle = xTaskCreateStatic(pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, context->xStack, context->xTaskBuffer);
  187. // store context in TCB or free everything in case of failure
  188. if (!handle) {
  189. free(context->xTaskBuffer);
  190. free(context->xStack);
  191. free(context);
  192. } else {
  193. vTaskSetThreadLocalStoragePointerAndDelCallback( handle, TASK_TLS_INDEX, context, (TlsDeleteCallbackFunction_t) task_cleanup);
  194. }
  195. if (pxCreatedTask) *pxCreatedTask = handle;
  196. return handle != NULL ? pdPASS : pdFAIL;
  197. }
  198. void vTaskDeleteEXTRAM(TaskHandle_t xTask) {
  199. /* At this point we leverage FreeRTOS extension to have callbacks on task deletion.
  200. * If not, we need to have here our own deletion implementation that include delayed
  201. * free for when this is called with NULL (self-deletion)
  202. */
  203. vTaskDelete(xTask);
  204. }
  205. /****************************************************************************************
  206. * URL download
  207. */
  208. typedef struct {
  209. void *user_context;
  210. http_download_cb_t callback;
  211. size_t max, bytes;
  212. bool abort;
  213. uint8_t *data;
  214. esp_http_client_handle_t client;
  215. } http_context_t;
  216. static void http_downloader(void *arg);
  217. static esp_err_t http_event_handler(esp_http_client_event_t *evt);
  218. void http_download(char *url, size_t max, http_download_cb_t callback, void *context) {
  219. http_context_t *http_context = (http_context_t*) heap_caps_calloc(sizeof(http_context_t), 1, MALLOC_CAP_SPIRAM);
  220. esp_http_client_config_t config = {
  221. .url = url,
  222. .event_handler = http_event_handler,
  223. .user_data = http_context,
  224. };
  225. http_context->callback = callback;
  226. http_context->user_context = context;
  227. http_context->max = max;
  228. http_context->client = esp_http_client_init(&config);
  229. xTaskCreateEXTRAM(http_downloader, "downloader", 8*1024, http_context, ESP_TASK_PRIO_MIN + 1, NULL);
  230. }
  231. static void http_downloader(void *arg) {
  232. http_context_t *http_context = (http_context_t*) arg;
  233. esp_http_client_perform(http_context->client);
  234. esp_http_client_cleanup(http_context->client);
  235. free(http_context);
  236. vTaskDeleteEXTRAM(NULL);
  237. }
  238. static esp_err_t http_event_handler(esp_http_client_event_t *evt) {
  239. http_context_t *http_context = (http_context_t*) evt->user_data;
  240. if (http_context->abort) return ESP_FAIL;
  241. switch(evt->event_id) {
  242. case HTTP_EVENT_ERROR:
  243. http_context->callback(NULL, 0, http_context->user_context);
  244. http_context->abort = true;
  245. break;
  246. case HTTP_EVENT_ON_HEADER:
  247. if (!strcasecmp(evt->header_key, "Content-Length")) {
  248. size_t len = atoi(evt->header_value);
  249. if (!len || len > http_context->max) {
  250. ESP_LOGI(TAG, "content-length null or too large %zu / %zu", len, http_context->max);
  251. http_context->abort = true;
  252. }
  253. }
  254. break;
  255. case HTTP_EVENT_ON_DATA: {
  256. size_t len = esp_http_client_get_content_length(evt->client);
  257. if (!http_context->data) {
  258. if ((http_context->data = (uint8_t*) malloc(len)) == NULL) {
  259. http_context->abort = true;
  260. ESP_LOGE(TAG, "failed to allocate memory for output buffer %zu", len);
  261. return ESP_FAIL;
  262. }
  263. }
  264. memcpy(http_context->data + http_context->bytes, evt->data, evt->data_len);
  265. http_context->bytes += evt->data_len;
  266. break;
  267. }
  268. case HTTP_EVENT_ON_FINISH:
  269. http_context->callback(http_context->data, http_context->bytes, http_context->user_context);
  270. break;
  271. case HTTP_EVENT_DISCONNECTED: {
  272. int mbedtls_err = 0;
  273. esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
  274. if (err != ESP_OK) {
  275. ESP_LOGE(TAG, "HTTP download disconnect %d", err);
  276. if (http_context->data) free(http_context->data);
  277. http_context->callback(NULL, 0, http_context->user_context);
  278. return ESP_FAIL;
  279. }
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. return ESP_OK;
  286. }