diff --git a/Makefile b/Makefile index f4645e6..0808aba 100644 --- a/Makefile +++ b/Makefile @@ -200,7 +200,7 @@ html: $(Q) ./html_build.sh libesphttpd: libesphttpd/Makefile - $(Q) make -C libesphttpd USE_OPENSDK=$(USE_OPENSDK) + $(Q) make -j4 -C libesphttpd USE_OPENSDK=$(USE_OPENSDK) $(APP_AR): libesphttpd $(OBJ) $(vecho) "AR $@" diff --git a/esp_meas.pro b/esp_meas.pro index aa6ed2c..8aade0f 100644 --- a/esp_meas.pro +++ b/esp_meas.pro @@ -8,6 +8,7 @@ DEFINES = ESPFS_HEATSHRINK HTTPD_MAX_CONNECTIONS=4 __ets__ INCLUDEPATH = . \ esp_iot_sdk_v1.5.2/include \ include \ + user \ libesphttpd/include \ libesphttpd/espfs \ libesphttpd/core \ @@ -52,9 +53,9 @@ SOURCES += \ user/datalink.c \ user/serial.c \ user/uptime.c \ - sbmp/library/payload_parser.c \ - user/sampling.c \ - user/page_home.c + sbmp/library/payload_parser.c \ + user/sampling.c \ + user/page_home.c HEADERS += \ include/uart_hw.h \ @@ -128,10 +129,12 @@ HEADERS += \ user/serial.h \ libesphttpd/include/logging.h \ user/uptime.h \ - sbmp/library/payload_parser.h \ - user/sampling.h \ - user/page_home.h \ - include/sbmp_config.h + sbmp/library/payload_parser.h \ + user/sampling.h \ + user/page_home.h \ + user/timeout.h \ + user/sbmp_config.h \ + sbmp/library/sbmp_config.example.h DISTFILES += \ style.astylerc \ diff --git a/sbmp b/sbmp index 32258ec..6495ad6 160000 --- a/sbmp +++ b/sbmp @@ -1 +1 @@ -Subproject commit 32258ec164775a543486c8e6827c500d1626fb48 +Subproject commit 6495ad6474dbd7a5e7198fbb998a247efed368a2 diff --git a/user/datalink.c b/user/datalink.c index 3a83fff..0409d83 100644 --- a/user/datalink.c +++ b/user/datalink.c @@ -17,7 +17,7 @@ static void FLASH_FN u0_putc(uint8_t c) static void FLASH_FN dg_handler(SBMP_Datagram *dg) { - dbg("[SBMP] Datagram received, type %d", dg->type); + dbg("[SBMP] Datagram received, type %d, session %d", dg->type, dg->session); } /** This is called by the UART rx handler */ @@ -30,6 +30,7 @@ void datalink_receive(uint8_t byte) void FLASH_FN datalinkInit(void) { dlnk_ep = sbmp_ep_init(NULL, NULL, PAYLOAD_BUFFER_LEN, dg_handler, u0_putc); + sbmp_ep_init_listeners(dlnk_ep, NULL, 4); sbmp_ep_enable(dlnk_ep, true); diff --git a/user/sampling.c b/user/sampling.c index 33b3c92..7331456 100644 --- a/user/sampling.c +++ b/user/sampling.c @@ -3,6 +3,7 @@ #include "datalink.h" #include "sampling.h" +#include "timeout.h" // The buffer is big enough for 256 data bytes - 4*64 @@ -37,6 +38,7 @@ typedef struct { static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram *dg, void **obj) { + bool suc = false; dbg("Received msg in session %d, dg type %d", dg->session, dg->type); DataReadState *state = *obj; @@ -47,7 +49,6 @@ static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram *obj = state; } - PayloadParser pp; switch (dg->type) { case DG_BULK_OFFER:// Data ready notification @@ -61,7 +62,7 @@ static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram dbg("Total bytes: %d", state->total); - sbmp_bulk_request(ep, state->pos, CHUNK_LEN, dg->session); // 64 floats in one message + retry_until_timeout(10, sbmp_bulk_request(ep, state->pos, CHUNK_LEN, dg->session)); break; case DG_BULK_DATA: // data received @@ -83,11 +84,12 @@ static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram // make sure the peer has freed the buffer // (may be waiting for us if we wanted to re-read something) - sbmp_bulk_abort(ep, dg->session); + + retry_until_timeout(10, sbmp_bulk_abort(ep, dg->session)); goto cleanup; } else { // read next part - sbmp_bulk_request(ep, state->pos, CHUNK_LEN, dg->session); + retry_until_timeout(10, sbmp_bulk_request(ep, state->pos, CHUNK_LEN, dg->session)); } break; @@ -130,10 +132,11 @@ static bool FLASH_FN meas_request_data(uint16_t count) // start a message uint16_t sesn; bool suc = sbmp_ep_start_message(dlnk_ep, DG_REQUEST_CAPTURE, sizeof(uint16_t), &sesn); - if (!suc) return false; + if (!suc) goto fail; // register the session listener - sbmp_ep_add_listener(dlnk_ep, sesn, request_data_sesn_listener, NULL); + suc = sbmp_ep_add_listener(dlnk_ep, sesn, request_data_sesn_listener, NULL); + if (!suc) goto fail; // request N values sbmp_ep_send_u16(dlnk_ep, count); @@ -143,6 +146,10 @@ static bool FLASH_FN meas_request_data(uint16_t count) acquire_session = sesn; return true; + +fail: + os_timer_disarm(&prSampleAbortTimer); + return false; } diff --git a/include/sbmp_config.h b/user/sbmp_config.h similarity index 60% rename from include/sbmp_config.h rename to user/sbmp_config.h index a5d405e..6207948 100644 --- a/include/sbmp_config.h +++ b/user/sbmp_config.h @@ -3,16 +3,24 @@ #include -/* --- Configuration ------------------- */ +/* ---------- CRC32 ---------------- */ /** - * @brief Enable logging. + * @brief Add support for CRC32 * - * Logging functions are WEAK stubs in sbmp_logging. + * Disabling CRC32 will reduce program size (for small micros). + * If CRC32 is disabled, XOR will be used as the preferred checksum + * method. * - * Disable logging to free up memory taken by the messages. + * Received CRC32'd messages will be accepted without checking. + * + * If handshake is used, the peer will detect that CRC32 is not + * supported here, and should start using XOR. */ -#define SBMP_LOGGING 1 +#define SBMP_HAS_CRC32 1 + + +/* ---------- MALLOC --------------- */ /** * @brief Enable malloc if NULL is passed. @@ -26,27 +34,33 @@ */ #define SBMP_USE_MALLOC 1 +// those will be used if malloc is enabled +#define sbmp_malloc(n) os_malloc(n) +#define sbmp_free(x) os_free(x) +#define sbmp_calloc(n,l) os_zalloc((n)*(l)) + + +/* ---------- LOGGING -------------- */ + /** - * @brief Add support for CRC32 - * - * Disabling CRC32 will reduce program size (for small micros). - * If CRC32 is disabled, XOR will be used as the preferred checksum - * method. + * @brief Enable logging. * - * Received CRC32'd messages will be accepted without checking. + * Logging functions are WEAK stubs in sbmp_logging. * - * If handshake is used, the peer will detect that CRC32 is not - * supported here, and should start using XOR. + * Disable logging to free up memory taken by the messages. */ -#define SBMP_HAS_CRC32 1 - -/* ---------- MALLOC --------------- */ +#define SBMP_LOGGING 1 -#define sbmp_malloc(n) os_malloc(n) -#define sbmp_free(x) os_free(x) -#define sbmp_calloc(n,l) os_zalloc((n)*(l)) +/** + * @brief Enable detailed logging (only for debugging, disable for better performance). + */ +#define SBMP_DEBUG 1 -/* ------------------------------------- */ +// here are the actual logging functions +#define sbmp_error(fmt, ...) (SBMP_DEBUG||SBMP_LOGGING ? os_printf("\x1b[31;1m[SBMP][E] "fmt"\x1b[0m\n", ##__VA_ARGS__) : 0) +#define sbmp_warn(fmt, ...) (SBMP_DEBUG||SBMP_LOGGING ? os_printf("\x1b[33;1m[SBMP][E] "fmt"\x1b[0m\n", ##__VA_ARGS__) : 0) +#define sbmp_info(fmt, ...) (SBMP_DEBUG||SBMP_LOGGING ? os_printf("\x1b[32m[SBMP][i] "fmt"\x1b[0m\n", ##__VA_ARGS__) : 0) +#define sbmp_dbg(fmt, ...) (SBMP_DEBUG ? os_printf("[SBMP][ ] "fmt"\n", ##__VA_ARGS__) : 0) #endif // SBMP_CONFIG_H diff --git a/user/timeout.h b/user/timeout.h new file mode 100644 index 0000000..ed09d1e --- /dev/null +++ b/user/timeout.h @@ -0,0 +1,15 @@ +#ifndef TIMEOUT_H +#define TIMEOUT_H + +#include + +#define until_timeout(to_ms) for(uint32_t _utmeo = system_get_time(); system_get_time() - _utmeo < ((to_ms)*1000);) + +/** Retry a call until a timeout. Variable 'suc' is set to the return value. Must be defined. */ +#define retry_until_timeout(to_ms, call) \ + until_timeout(to_ms) { \ + suc = call; \ + if (suc) break; \ + } + +#endif // TIMEOUT_H