tools.c 8.0 KB

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