File
Blob: archive/fft-benchmark/firmware/platform/http.c
| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include "radio_bridge.h" |
| 5 | #include "esp_crt_bundle.h" |
| 6 | #include "esp_heap_caps.h" |
| 7 | #include "esp_http_client.h" |
| 8 | #include "signaling_private.h" |
| 9 | |
| 10 | #define RESPONSE_LIMIT 24576 |
| 11 | struct radio_http { |
| 12 | esp_http_client_handle_t client; |
| 13 | esp_http_client_config_t config; |
| 14 | char *response; |
| 15 | size_t used; |
| 16 | bool overflow; |
| 17 | }; |
| 18 | |
| 19 | static esp_err_t on_http(esp_http_client_event_t *event) |
| 20 | { |
| 21 | radio_http *h = event->user_data; |
| 22 | if (event->event_id == HTTP_EVENT_ON_DATA && event->data_len > 0) { |
| 23 | size_t length = event->data_len; |
| 24 | if (length > RESPONSE_LIMIT - h->used) h->overflow = true; |
| 25 | else if (!h->overflow) { memcpy(h->response + h->used, event->data, length); h->used += length; } |
| 26 | } |
| 27 | return ESP_OK; |
| 28 | } |
| 29 | |
| 30 | radio_http *radio_http_open(void) |
| 31 | { |
| 32 | radio_http *h = calloc(1, sizeof(*h)); |
| 33 | if (!h) return NULL; |
| 34 | h->response = heap_caps_malloc(RESPONSE_LIMIT, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 35 | if (!h->response) { free(h); return NULL; } |
| 36 | h->config = (esp_http_client_config_t){.url = SIGNALING_URL, .crt_bundle_attach = esp_crt_bundle_attach, |
| 37 | .timeout_ms = 12000, .event_handler = on_http, .user_data = h, |
| 38 | .buffer_size = 4096, .buffer_size_tx = 4096, .disable_auto_redirect = true, .keep_alive_enable = true}; |
| 39 | h->client = esp_http_client_init(&h->config); |
| 40 | if (!h->client) { radio_http_free(h); return NULL; } |
| 41 | int err = esp_http_client_set_method(h->client, HTTP_METHOD_POST); |
| 42 | if (!err) err = esp_http_client_set_header(h->client, "Content-Type", "application/json"); |
| 43 | if (!err) err = esp_http_client_set_header(h->client, "Authorization", "Bearer " SIGNALING_DEVICE_TOKEN); |
| 44 | if (!err) err = esp_http_client_set_header(h->client, "User-Agent", "PocketRadio-S3-Rust/1.0"); |
| 45 | if (err) { radio_http_free(h); return NULL; } |
| 46 | return h; |
| 47 | } |
| 48 | |
| 49 | int32_t radio_http_post(radio_http *h, const char *path, const uint8_t *body, size_t length, |
| 50 | uint8_t *out, size_t capacity, size_t *used) |
| 51 | { |
| 52 | *used = 0; |
| 53 | if (!radio_network_ready() || length > 20000) return -1; |
| 54 | char url[256]; |
| 55 | int count = snprintf(url, sizeof(url), "%s/api/device/%s", SIGNALING_URL, path); |
| 56 | if (count < 0 || count >= sizeof(url)) return -1; |
| 57 | h->used = 0; |
| 58 | h->overflow = false; |
| 59 | int err = esp_http_client_set_url(h->client, url); |
| 60 | if (!err) err = esp_http_client_set_post_field(h->client, (const char *)body, length); |
| 61 | if (!err) err = esp_http_client_perform(h->client); |
| 62 | int status = esp_http_client_get_status_code(h->client); |
| 63 | /* perform is blocking; callbacks finish before returning. Clear borrowed body. */ |
| 64 | esp_http_client_set_post_field(h->client, NULL, 0); |
| 65 | if (err || h->overflow || h->used > capacity) { esp_http_client_close(h->client); return -1; } |
| 66 | memcpy(out, h->response, h->used); |
| 67 | *used = h->used; |
| 68 | return status; |
| 69 | } |
| 70 | void radio_http_free(radio_http *h) |
| 71 | { |
| 72 | if (!h) return; |
| 73 | if (h->client) esp_http_client_cleanup(h->client); |
| 74 | free(h->response); |
| 75 | free(h); |
| 76 | } |