tools.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. const static char TAG[] = "tools";
  23. /****************************************************************************************
  24. * UTF-8 tools
  25. */
  26. // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  27. // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  28. // Copyright (c) 2017 ZephRay <zephray@outlook.com>
  29. //
  30. // utf8to1252 - almost equivalent to iconv -f utf-8 -t windows-1252, but better
  31. #define UTF8_ACCEPT 0
  32. #define UTF8_REJECT 1
  33. static const uint8_t utf8d[] = {
  34. 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
  35. 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
  36. 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
  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, // 60..7f
  38. 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
  39. 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
  40. 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
  41. 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
  42. 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
  43. 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
  44. 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
  45. 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
  46. 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
  47. 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
  48. };
  49. static uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
  50. uint32_t type = utf8d[byte];
  51. *codep = (*state != UTF8_ACCEPT) ?
  52. (byte & 0x3fu) | (*codep << 6) :
  53. (0xff >> type) & (byte);
  54. *state = utf8d[256 + *state*16 + type];
  55. return *state;
  56. }
  57. static uint8_t UNICODEtoCP1252(uint16_t chr) {
  58. if (chr <= 0xff)
  59. return (chr&0xff);
  60. else {
  61. ESP_LOGI(TAG, "some multi-byte %hx", chr);
  62. switch(chr) {
  63. case 0x20ac: return 0x80; break;
  64. case 0x201a: return 0x82; break;
  65. case 0x0192: return 0x83; break;
  66. case 0x201e: return 0x84; break;
  67. case 0x2026: return 0x85; break;
  68. case 0x2020: return 0x86; break;
  69. case 0x2021: return 0x87; break;
  70. case 0x02c6: return 0x88; break;
  71. case 0x2030: return 0x89; break;
  72. case 0x0160: return 0x8a; break;
  73. case 0x2039: return 0x8b; break;
  74. case 0x0152: return 0x8c; break;
  75. case 0x017d: return 0x8e; break;
  76. case 0x2018: return 0x91; break;
  77. case 0x2019: return 0x92; break;
  78. case 0x201c: return 0x93; break;
  79. case 0x201d: return 0x94; break;
  80. case 0x2022: return 0x95; break;
  81. case 0x2013: return 0x96; break;
  82. case 0x2014: return 0x97; break;
  83. case 0x02dc: return 0x98; break;
  84. case 0x2122: return 0x99; break;
  85. case 0x0161: return 0x9a; break;
  86. case 0x203a: return 0x9b; break;
  87. case 0x0153: return 0x9c; break;
  88. case 0x017e: return 0x9e; break;
  89. case 0x0178: return 0x9f; break;
  90. default: return 0x00; break;
  91. }
  92. }
  93. }
  94. void utf8_decode(char *src) {
  95. uint32_t codep = 0, state = UTF8_ACCEPT;
  96. char *dst = src;
  97. while (src && *src) {
  98. if (!decode(&state, &codep, *src++)) *dst++ = UNICODEtoCP1252(codep);
  99. }
  100. *dst = '\0';
  101. }
  102. /****************************************************************************************
  103. * URL tools
  104. */
  105. static inline char from_hex(char ch) {
  106. return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  107. }
  108. void url_decode(char *url) {
  109. char *p, *src = strdup(url);
  110. for (p = src; *src; url++) {
  111. *url = *src++;
  112. if (*url == '%') {
  113. *url = from_hex(*src++) << 4;
  114. *url |= from_hex(*src++);
  115. } else if (*url == '+') {
  116. *url = ' ';
  117. }
  118. }
  119. *url = '\0';
  120. free(p);
  121. }
  122. /****************************************************************************************
  123. * Memory tools
  124. */
  125. void * malloc_init_external(size_t sz){
  126. void * ptr=NULL;
  127. ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  128. if(ptr==NULL){
  129. ESP_LOGE(TAG,"malloc_init_external: unable to allocate %d bytes of PSRAM!",sz);
  130. }
  131. else {
  132. memset(ptr,0x00,sz);
  133. }
  134. return ptr;
  135. }
  136. void * clone_obj_psram(void * source, size_t source_sz){
  137. void * ptr=NULL;
  138. ptr = heap_caps_malloc(source_sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  139. if(ptr==NULL){
  140. ESP_LOGE(TAG,"clone_obj_psram: unable to allocate %d bytes of PSRAM!",source_sz);
  141. }
  142. else {
  143. memcpy(ptr,source,source_sz);
  144. }
  145. return ptr;
  146. }
  147. char * strdup_psram(const char * source){
  148. void * ptr=NULL;
  149. size_t source_sz = strlen(source)+1;
  150. ptr = heap_caps_malloc(source_sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  151. if(ptr==NULL){
  152. ESP_LOGE(TAG,"strdup_psram: unable to allocate %d bytes of PSRAM! Cannot clone string %s",source_sz,source);
  153. }
  154. else {
  155. memset(ptr,0x00,source_sz);
  156. strcpy(ptr,source);
  157. }
  158. return ptr;
  159. }
  160. /****************************************************************************************
  161. * URL download
  162. */
  163. typedef struct {
  164. void *user_context;
  165. http_download_cb_t callback;
  166. size_t max, bytes;
  167. bool abort;
  168. uint8_t *data;
  169. esp_http_client_handle_t client;
  170. } http_context_t;
  171. static void http_downloader(void *arg);
  172. static esp_err_t http_event_handler(esp_http_client_event_t *evt);
  173. void http_download(char *url, size_t max, http_download_cb_t callback, void *context) {
  174. http_context_t *http_context = (http_context_t*) heap_caps_calloc(sizeof(http_context_t), 1, MALLOC_CAP_SPIRAM);
  175. esp_http_client_config_t config = {
  176. .url = url,
  177. .event_handler = http_event_handler,
  178. .user_data = http_context,
  179. };
  180. http_context->callback = callback;
  181. http_context->user_context = context;
  182. http_context->max = max;
  183. http_context->client = esp_http_client_init(&config);
  184. xTaskCreate(http_downloader, "downloader", 4*1024, http_context, ESP_TASK_PRIO_MIN + 1, NULL);
  185. }
  186. static void http_downloader(void *arg) {
  187. http_context_t *http_context = (http_context_t*) arg;
  188. esp_http_client_perform(http_context->client);
  189. esp_http_client_cleanup(http_context->client);
  190. free(http_context);
  191. vTaskDelete(NULL);
  192. }
  193. static esp_err_t http_event_handler(esp_http_client_event_t *evt) {
  194. http_context_t *http_context = (http_context_t*) evt->user_data;
  195. if (http_context->abort) return ESP_FAIL;
  196. switch(evt->event_id) {
  197. case HTTP_EVENT_ERROR:
  198. http_context->callback(NULL, 0, http_context->user_context);
  199. http_context->abort = true;
  200. break;
  201. case HTTP_EVENT_ON_HEADER:
  202. if (!strcasecmp(evt->header_key, "Content-Length")) {
  203. size_t len = atoi(evt->header_value);
  204. if (!len || len > http_context->max) {
  205. ESP_LOGI(TAG, "content-length null or too large %zu / %zu", len, http_context->max);
  206. http_context->abort = true;
  207. }
  208. }
  209. break;
  210. case HTTP_EVENT_ON_DATA: {
  211. size_t len = esp_http_client_get_content_length(evt->client);
  212. if (!http_context->data) {
  213. if ((http_context->data = (uint8_t*) malloc(len)) == NULL) {
  214. http_context->abort = true;
  215. ESP_LOGE(TAG, "gailed to allocate memory for output buffer %zu", len);
  216. return ESP_FAIL;
  217. }
  218. }
  219. memcpy(http_context->data + http_context->bytes, evt->data, evt->data_len);
  220. http_context->bytes += evt->data_len;
  221. break;
  222. }
  223. case HTTP_EVENT_ON_FINISH:
  224. http_context->callback(http_context->data, http_context->bytes, http_context->user_context);
  225. break;
  226. case HTTP_EVENT_DISCONNECTED: {
  227. int mbedtls_err = 0;
  228. esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
  229. if (err != ESP_OK) {
  230. ESP_LOGE(TAG, "HTTP download disconnect %d", err);
  231. if (http_context->data) free(http_context->data);
  232. http_context->callback(NULL, 0, http_context->user_context);
  233. return ESP_FAIL;
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. return ESP_OK;
  241. }