diff options
author | Uros Majstorovic <majstor@majstor.org> | 2022-03-30 13:22:57 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2022-03-30 13:22:57 +0200 |
commit | 55474b81146327e8cfa7702fa9366cc7da6562e7 (patch) | |
tree | b6bf9fe0258ab88d12717bb31ecdf4eda0ffa073 /fw/esp32/components/eos | |
parent | c6962c5700f99441538dafa346626bb7e6d12488 (diff) |
sock api fixed; net reply messages fixed
Diffstat (limited to 'fw/esp32/components/eos')
-rw-r--r-- | fw/esp32/components/eos/app.c | 2 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell.c | 19 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_modem.c | 7 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_pcm.c | 3 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_pdp.c | 31 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_sms.c | 48 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_ussd.c | 10 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_voice.c | 21 | ||||
-rw-r--r-- | fw/esp32/components/eos/gsm.c | 2 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/cell.h | 11 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/net.h | 4 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/wifi.h | 4 | ||||
-rw-r--r-- | fw/esp32/components/eos/net.c | 47 | ||||
-rw-r--r-- | fw/esp32/components/eos/sock.c | 85 | ||||
-rwxr-xr-x | fw/esp32/components/eos/wifi.c | 42 |
15 files changed, 197 insertions, 139 deletions
diff --git a/fw/esp32/components/eos/app.c b/fw/esp32/components/eos/app.c index 5f8cc43..c4244df 100644 --- a/fw/esp32/components/eos/app.c +++ b/fw/esp32/components/eos/app.c @@ -56,7 +56,6 @@ static void _post_trans_cb(spi_slave_transaction_t *trans) { static void app_xchg_task(void *pvParameters) { unsigned char mtype = 0; - unsigned char mtype_flags = 0; unsigned char *buffer; uint16_t len; unsigned char *buf_send = heap_caps_malloc(SPI_SIZE_BUF, MALLOC_CAP_DMA); @@ -133,7 +132,6 @@ static void app_xchg_task(void *pvParameters) { spi_tr.length = SPI_SIZE_BUF * 8; } mtype = buf_recv[0] & ~EOS_APP_MTYPE_FLAG_MASK; - mtype_flags = buf_recv[0] & EOS_APP_MTYPE_FLAG_MASK; len = (uint16_t)buf_recv[1] << 8; len |= (uint16_t)buf_recv[2] & 0xFF; buffer = buf_recv + 3; diff --git a/fw/esp32/components/eos/cell.c b/fw/esp32/components/eos/cell.c index b31e973..19245bb 100644 --- a/fw/esp32/components/eos/cell.c +++ b/fw/esp32/components/eos/cell.c @@ -25,11 +25,15 @@ static unsigned char *cell_bufq_array[CELL_SIZE_QUEUE]; static SemaphoreHandle_t mutex; static QueueHandle_t cell_queue; -static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) { +static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t buf_len) { uint8_t mtype; - if (size < 1) return; + if (buf_len < 1) return; + mtype = buffer[0]; + buffer++; + buf_len--; + switch (mtype & EOS_CELL_MTYPE_MASK) { case EOS_CELL_MTYPE_DEV: switch (mtype & ~EOS_CELL_MTYPE_MASK) { @@ -38,7 +42,7 @@ static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t break; case EOS_CELL_MTYPE_UART_DATA: - if (eos_modem_get_mode() == EOS_CELL_UART_MODE_RELAY) eos_modem_write(buffer+1, size-1); + if (eos_modem_get_mode() == EOS_CELL_UART_MODE_RELAY) eos_modem_write(buffer, buf_len); break; case EOS_CELL_MTYPE_UART_TAKE: @@ -53,19 +57,19 @@ static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t break; case EOS_CELL_MTYPE_VOICE: - eos_cell_voice_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, size); + eos_cell_voice_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len); break; case EOS_CELL_MTYPE_SMS: - eos_cell_sms_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, size); + eos_cell_sms_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len); break; case EOS_CELL_MTYPE_USSD: - eos_cell_ussd_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, size); + eos_cell_ussd_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len); break; case EOS_CELL_MTYPE_PDP: - eos_cell_pdp_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, size); + eos_cell_pdp_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len); break; } } @@ -119,4 +123,3 @@ void eos_cell_init(void) { eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handler); } - diff --git a/fw/esp32/components/eos/cell_modem.c b/fw/esp32/components/eos/cell_modem.c index f5ee0a9..09d65d9 100644 --- a/fw/esp32/components/eos/cell_modem.c +++ b/fw/esp32/components/eos/cell_modem.c @@ -302,8 +302,7 @@ static void modem_event_task(void *pvParameters) { default: break; } - - /* Obsolete!!! + /* Obsolete uint64_t t_start = esp_timer_get_time(); if (xQueueReceive(modem_queue, &level, 200 / portTICK_RATE_MS) && (level == 1)) { uint64_t t_end = esp_timer_get_time(); @@ -312,7 +311,6 @@ static void modem_event_task(void *pvParameters) { ESP_LOGI(TAG, "RING"); } */ - } } vTaskDelete(NULL); @@ -530,8 +528,9 @@ static int ppp_resume(void) { static int ppp_setup(void) { int r; char cmd[64]; - int cmd_len = snprintf(cmd, sizeof(cmd), "AT+CGDCONT=1,\"IP\",\"%s\"\r", ppp_apn); + int cmd_len; + cmd_len = snprintf(cmd, sizeof(cmd), "AT+CGDCONT=1,\"IP\",\"%s\"\r", ppp_apn); if ((cmd_len < 0) || (cmd_len >= sizeof(cmd))) return EOS_ERR; modem_set_mode(EOS_CELL_UART_MODE_NONE); diff --git a/fw/esp32/components/eos/cell_pcm.c b/fw/esp32/components/eos/cell_pcm.c index a022aab..57258c9 100644 --- a/fw/esp32/components/eos/cell_pcm.c +++ b/fw/esp32/components/eos/cell_pcm.c @@ -91,12 +91,15 @@ static void i2s_event_task(void *pvParameters) { xSemaphoreGive(mutex); } break; + case I2S_EVENT_DMA_ERROR: ESP_LOGE(TAG, "*** I2S DMA ERROR ***"); break; + case I2S_EVENT_MAX: hold_cnt = PCM_HOLD_CNT_RX; break; + default: break; } diff --git a/fw/esp32/components/eos/cell_pdp.c b/fw/esp32/components/eos/cell_pdp.c index 1aa0575..4c5321a 100644 --- a/fw/esp32/components/eos/cell_pdp.c +++ b/fw/esp32/components/eos/cell_pdp.c @@ -6,16 +6,33 @@ #include "eos.h" #include "cell.h" -void eos_cell_pdp_handler(unsigned char mtype, unsigned char *buffer, uint16_t size) { - char *apn, *user, *pass; +void eos_cell_pdp_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) { + char *apn, *user, *pass, *_buf; + uint16_t _buf_len; - buffer += 1; - size -= 1; switch (mtype) { case EOS_CELL_MTYPE_PDP_CONFIG: - apn = (char *)buffer; - user = apn + strlen(apn) + 1; - pass = user + strlen(user) + 1; + _buf = (char *)buffer; + _buf_len = 0; + + apn = _buf; + _buf_len = strnlen(_buf, buf_len); + if (_buf_len == buf_len) break; + _buf += _buf_len + 1; + buf_len -= _buf_len + 1; + + user = _buf; + _buf_len = strnlen(_buf, buf_len); + if (_buf_len == buf_len) break; + _buf += _buf_len + 1; + buf_len -= _buf_len + 1; + + pass = _buf; + _buf_len = strnlen(_buf, buf_len); + if (_buf_len == buf_len) break; + _buf += _buf_len + 1; + buf_len -= _buf_len + 1; + eos_ppp_set_apn(apn); eos_ppp_set_auth(user, pass); break; diff --git a/fw/esp32/components/eos/cell_sms.c b/fw/esp32/components/eos/cell_sms.c index 92c016e..9876ef8 100644 --- a/fw/esp32/components/eos/cell_sms.c +++ b/fw/esp32/components/eos/cell_sms.c @@ -44,10 +44,10 @@ static int sms_decode(unsigned char *buf, uint16_t *_len) { uint16_t len = 0; uint8_t smsc_info, smsc_info_len; - if (pdu_len < 2) return GSM_ERR_SIZE; + if (pdu_len < 2) return EOS_ERR; smsc_info = pdu_getc(pdu); smsc_info_len = 2 * (smsc_info + 1); - if (pdu_len < smsc_info_len) return GSM_ERR_SIZE; + if (pdu_len < smsc_info_len) return EOS_ERR; if (smsc_info > 1) { pdu_putc((smsc_info - 1) * 2, pdu); @@ -125,21 +125,20 @@ static int sms_decode(unsigned char *buf, uint16_t *_len) { return EOS_OK; } -static int sms_encode(unsigned char *buffer, uint16_t size) { +static int sms_encode(unsigned char *buffer, uint16_t len) { utf32_t ch; int i, rv; char *addr; uint8_t addr_type; int addr_len; - if (size == 0) return EOS_ERR; - + if (len < 2) return EOS_ERR; flags = buffer[0] << 8; flags |= buffer[1]; buffer += 2; - size -= 2; + len -= 2; - if (size < 2) return EOS_ERR; + if (len < 2) return EOS_ERR; switch(buffer[0]) { case EOS_CELL_SMS_ADDRTYPE_INTL: addr_type = GSM_EXT | GSM_TON_INTERNATIONAL | GSM_NPI_TELEPHONE; @@ -149,18 +148,19 @@ static int sms_encode(unsigned char *buffer, uint16_t size) { addr_type = GSM_EXT | GSM_TON_UNKNOWN | GSM_NPI_TELEPHONE; break; - default: return EOS_ERR; + default: + return EOS_ERR; } addr_len = buffer[1]; addr = (char *)buffer + 2; - if (size < 2 + addr_len) return EOS_ERR; + if (len < 2 + addr_len) return EOS_ERR; buffer += 2 + addr_len; - size -= 2 + addr_len; + len -= 2 + addr_len; i = 0; msg_len = 0; - while (i < size) { + while (i < len) { rv = utf8_dec(buffer + i, &ch); if (rv < 0) return EOS_ERR; if (ch >= 0xffff) return EOS_ERR; @@ -181,19 +181,20 @@ static int sms_encode(unsigned char *buffer, uint16_t size) { return EOS_OK; } -void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t size) { +void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) { int rv; char b[4]; - buffer += 1; - size -= 1; switch (mtype) { case EOS_CELL_MTYPE_SMS_LIST: - if (size == 0) return; - snprintf(cmd, sizeof(cmd), "AT+CMGL=%d\r", buffer[0]); + if (buf_len < 1) return; + + rv = snprintf(cmd, sizeof(cmd), "AT+CMGL=%d\r", buffer[0]); + if ((rv < 0) || (rv >= sizeof(cmd))) return; rv = eos_modem_take(1000); if (rv) return; + at_cmd(cmd); do { unsigned char *buf; @@ -216,26 +217,28 @@ void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t s eos_net_send(EOS_NET_MTYPE_CELL, buf, len + 1); } } while (1); - eos_modem_give(); + eos_modem_give(); break; case EOS_CELL_MTYPE_SMS_SEND: - rv = sms_encode(buffer, size); + rv = sms_encode(buffer, buf_len); if (rv) return; - snprintf(cmd, sizeof(cmd), "AT+CMGS=%d\r", pdu_len / 2); + rv = snprintf(cmd, sizeof(cmd), "AT+CMGS=%d\r", pdu_len / 2); + if ((rv < 0) || (rv >= sizeof(cmd))) return; rv = eos_modem_take(1000); if (rv) return; + at_cmd(cmd); // wait for: '> ' (0d 0a 3e 20) eos_modem_read(b, 4, 1000); at_cmd(pdu); rv = at_expect("^\\+CMGS: [0-9]+", "^ERROR", 5000); if (rv == 1) rv = at_expect("^OK", "^ERROR", 1000); - eos_modem_give(); + eos_modem_give(); break; } } @@ -245,7 +248,9 @@ static void sms_received_handler(char *urc, regmatch_t m[]) { sscanf(urc + m[1].rm_so, "%d", &ref); - snprintf(cmd, sizeof(cmd), "AT+CMGR=%d\r", ref); + rv = snprintf(cmd, sizeof(cmd), "AT+CMGR=%d\r", ref); + if ((rv < 0) || (rv >= sizeof(cmd))) return; + at_cmd(cmd); rv = at_expect("^\\+CMGR: [0-9],.*,[0-9]+$", "^ERROR", 1000); @@ -255,6 +260,7 @@ static void sms_received_handler(char *urc, regmatch_t m[]) { rv = eos_modem_readln(pdu, sizeof(pdu), 1000); if (rv) return; + pdu_len = strlen(pdu); rv = at_expect("^OK", NULL, 1000); diff --git a/fw/esp32/components/eos/cell_ussd.c b/fw/esp32/components/eos/cell_ussd.c index 195feaf..5b90e35 100644 --- a/fw/esp32/components/eos/cell_ussd.c +++ b/fw/esp32/components/eos/cell_ussd.c @@ -14,16 +14,14 @@ static const char *TAG = "EOS USSD"; static char cmd[256]; static int cmd_len; -void eos_cell_ussd_handler(unsigned char mtype, unsigned char *buffer, uint16_t size) { +void eos_cell_ussd_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) { int rv; - buffer += 1; - size -= 1; switch (mtype) { case EOS_CELL_MTYPE_USSD_REQUEST: - if (size == 0) return; + if (buf_len > EOS_CELL_MAX_USSD_STR) return; - buffer[size] = '\0'; + buffer[buf_len] = '\0'; cmd_len = snprintf(cmd, sizeof(cmd), "AT+CUSD=1,\"%s\",15\r", buffer); if ((cmd_len < 0) || (cmd_len >= sizeof(cmd))) return; @@ -89,7 +87,7 @@ static void ussd_reply_handler(char *urc, regmatch_t m[]) { } while (1); if (rv) { - ESP_LOGE(TAG, "USSD error"); + ESP_LOGE(TAG, "error"); eos_net_free(buf); } else { eos_net_send(EOS_NET_MTYPE_CELL, buf, len); diff --git a/fw/esp32/components/eos/cell_voice.c b/fw/esp32/components/eos/cell_voice.c index f0655bd..c0b9a7d 100644 --- a/fw/esp32/components/eos/cell_voice.c +++ b/fw/esp32/components/eos/cell_voice.c @@ -10,40 +10,38 @@ #include "at_cmd.h" #include "cell.h" -static const char *TAG = "EOS VOICE"; - static char cmd[256]; static int cmd_len; -void eos_cell_voice_handler(unsigned char mtype, unsigned char *buffer, uint16_t size) { +void eos_cell_voice_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) { int rv; - buffer += 1; - size -= 1; switch (mtype) { case EOS_CELL_MTYPE_VOICE_DIAL: - if (size == 0) return; + if (buf_len > EOS_CELL_MAX_DIAL_STR) return; - buffer[size] = '\0'; + buffer[buf_len] = '\0'; cmd_len = snprintf(cmd, sizeof(cmd), "ATD%s;\r", buffer); if ((cmd_len < 0) || (cmd_len >= sizeof(cmd))) return; rv = eos_modem_take(1000); if (rv) return; + at_cmd(cmd); rv = at_expect("^OK", "^ERROR", 1000); - eos_modem_give(); + eos_modem_give(); eos_cell_pcm_start(); break; case EOS_CELL_MTYPE_VOICE_ANSWER: rv = eos_modem_take(1000); if (rv) return; + at_cmd("ATA\r"); rv = at_expect("^OK", "^ERROR", 1000); - eos_modem_give(); + eos_modem_give(); eos_cell_pcm_start(); break; @@ -52,14 +50,15 @@ void eos_cell_voice_handler(unsigned char mtype, unsigned char *buffer, uint16_t rv = eos_modem_take(1000); if (rv) return; + at_cmd("AT+CHUP\r"); rv = at_expect("^OK", "^ERROR", 1000); - eos_modem_give(); + eos_modem_give(); break; case EOS_CELL_MTYPE_VOICE_PCM: - eos_cell_pcm_push(buffer+1, size-1); + eos_cell_pcm_push(buffer, buf_len); break; } } diff --git a/fw/esp32/components/eos/gsm.c b/fw/esp32/components/eos/gsm.c index 6ae73ee..9a0874a 100644 --- a/fw/esp32/components/eos/gsm.c +++ b/fw/esp32/components/eos/gsm.c @@ -7,8 +7,8 @@ #define DIVC(x,y) ((x) / (y) + ((x) % (y) != 0)) uint8_t pdu_getc(char *pdu) { - int ch; + sscanf(pdu, "%2X", &ch); return ch; } diff --git a/fw/esp32/components/eos/include/cell.h b/fw/esp32/components/eos/include/cell.h index fa9a0e2..2cbba7e 100644 --- a/fw/esp32/components/eos/include/cell.h +++ b/fw/esp32/components/eos/include/cell.h @@ -54,6 +54,9 @@ #define EOS_CELL_UART_SIZE_BUF 1024 +#define EOS_CELL_MAX_USSD_STR 256 +#define EOS_CELL_MAX_DIAL_STR 256 + void eos_cell_init(void); void eos_modem_init(void); @@ -81,10 +84,10 @@ int eos_cell_pcm_push(unsigned char *data, size_t size); void eos_cell_pcm_start(void); void eos_cell_pcm_stop(void); -void eos_cell_voice_handler(unsigned char mtype, unsigned char *buffer, uint16_t size); -void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t size); -void eos_cell_ussd_handler(unsigned char mtype, unsigned char *buffer, uint16_t size); -void eos_cell_pdp_handler(unsigned char mtype, unsigned char *buffer, uint16_t size); +void eos_cell_voice_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len); +void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len); +void eos_cell_ussd_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len); +void eos_cell_pdp_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len); void eos_cell_voice_init(void); void eos_cell_sms_init(void); diff --git a/fw/esp32/components/eos/include/net.h b/fw/esp32/components/eos/include/net.h index cf0cb6b..5bccc57 100644 --- a/fw/esp32/components/eos/include/net.h +++ b/fw/esp32/components/eos/include/net.h @@ -5,6 +5,7 @@ #define EOS_NET_SIZE_BUF EOS_NET_MTU #define EOS_NET_MTYPE_SOCK 1 +#define EOS_NET_MTYPE_RNG 3 #define EOS_NET_MTYPE_POWER 4 #define EOS_NET_MTYPE_WIFI 5 @@ -15,7 +16,7 @@ #define EOS_NET_MAX_MTYPE 8 #define EOS_NET_MTYPE_FLAG_ONEW 0x40 -#define EOS_NET_MTYPE_FLAG_REPW 0x80 +#define EOS_NET_MTYPE_FLAG_REPL 0x80 #define EOS_NET_MTYPE_FLAG_MASK 0xc0 /* esp32 specific */ @@ -29,6 +30,7 @@ void eos_net_init(void); unsigned char *eos_net_alloc(void); void eos_net_free(unsigned char *buf); int eos_net_send(unsigned char mtype, unsigned char *buffer, uint16_t len); +void eos_net_reply(unsigned char mtype, unsigned char *buffer, uint16_t len); void eos_net_set_handler(unsigned char mtype, eos_net_fptr_t handler); void eos_net_sleep_done(uint8_t mode); void eos_net_wake(uint8_t source, uint8_t mode); diff --git a/fw/esp32/components/eos/include/wifi.h b/fw/esp32/components/eos/include/wifi.h index 11bccec..5a668fb 100644 --- a/fw/esp32/components/eos/include/wifi.h +++ b/fw/esp32/components/eos/include/wifi.h @@ -1,5 +1,5 @@ #define EOS_WIFI_MTYPE_SCAN 1 -#define EOS_WIFI_MTYPE_CONFIG 2 +#define EOS_WIFI_MTYPE_AUTH 2 #define EOS_WIFI_MTYPE_CONNECT 3 #define EOS_WIFI_MTYPE_DISCONNECT 4 @@ -8,6 +8,6 @@ void eos_wifi_init(void); int eos_wifi_scan(void); -int eos_wifi_set_config(char *ssid, char *pass); +int eos_wifi_auth(char *ssid, char *pass); int eos_wifi_connect(void); int eos_wifi_disconnect(void); diff --git a/fw/esp32/components/eos/net.c b/fw/esp32/components/eos/net.c index 0491b41..73d1c43 100644 --- a/fw/esp32/components/eos/net.c +++ b/fw/esp32/components/eos/net.c @@ -59,8 +59,7 @@ static void _post_trans_cb(spi_slave_transaction_t *trans) { static void net_xchg_task(void *pvParameters) { int wake = 0; - int reply = 0; - int skip_next_msg = 0; + int skip_msg = 0; unsigned char mtype = 0; unsigned char mtype_flags = 0; unsigned char *buffer; @@ -99,12 +98,12 @@ static void net_xchg_task(void *pvParameters) { if (eos_power_wakeup_cause()) { wake = 1; - skip_next_msg = 1; + skip_msg = 1; } eos_power_wait4init(); while (1) { - if (!skip_next_msg) { + if (!skip_msg) { xSemaphoreTake(mutex, portMAX_DELAY); eos_msgq_pop(&net_send_q, &mtype, &buffer, &len); @@ -126,25 +125,13 @@ static void net_xchg_task(void *pvParameters) { xSemaphoreGive(mutex); } - skip_next_msg = 0; + skip_msg = 0; - if (reply) { - spi_tr.tx_buffer = buf_recv; - spi_tr.rx_buffer = NULL; - } else { - buf_recv[0] = 0; - buf_recv[1] = 0; - buf_recv[2] = 0; - } + buf_recv[0] = 0; + buf_recv[1] = 0; + buf_recv[2] = 0; spi_slave_transmit(VSPI_HOST, &spi_tr, portMAX_DELAY); // ESP_LOGI(TAG, "RECV:%d", buf_recv[0]); - if (reply) { - reply = 0; - spi_tr.tx_buffer = buf_send; - spi_tr.rx_buffer = buf_recv; - if (buf_send[0]) skip_next_msg = 1; - continue; - } if (wake) { eos_power_net_ready(); @@ -176,7 +163,7 @@ static void net_xchg_task(void *pvParameters) { spi_slave_initialize(VSPI_HOST, &spi_bus_cfg, &spi_slave_cfg, 1); wake = 1; - skip_next_msg = 1; + skip_msg = 1; } continue; } @@ -192,11 +179,14 @@ static void net_xchg_task(void *pvParameters) { bad_handler(mtype, buffer, len); } if ((mtype_flags & EOS_NET_MTYPE_FLAG_ONEW) && buf_send[0]) { - skip_next_msg = 1; + skip_msg = 1; } - if (mtype_flags & EOS_NET_MTYPE_FLAG_REPW) { - skip_next_msg = 1; - reply = 1; + if (mtype_flags & EOS_NET_MTYPE_FLAG_REPL) { + spi_tr.tx_buffer = buf_recv; + spi_tr.rx_buffer = NULL; + spi_slave_transmit(VSPI_HOST, &spi_tr, portMAX_DELAY); + spi_tr.tx_buffer = buf_send; + spi_tr.rx_buffer = buf_recv; } } vTaskDelete(NULL); @@ -276,6 +266,13 @@ int eos_net_send(unsigned char mtype, unsigned char *buffer, uint16_t len) { return rv; } +void eos_net_reply(unsigned char mtype, unsigned char *buffer, uint16_t len) { + buffer -= SPI_SIZE_HDR; + buffer[0] = mtype; + buffer[1] = len >> 8; + buffer[2] = len & 0xFF; +} + void eos_net_set_handler(unsigned char mtype, eos_net_fptr_t handler) { if (handler == NULL) handler = bad_handler; if (mtype && (mtype <= EOS_NET_MAX_MTYPE)) net_handler[mtype - 1] = handler; diff --git a/fw/esp32/components/eos/sock.c b/fw/esp32/components/eos/sock.c index 355d7c7..ec198f0 100644 --- a/fw/esp32/components/eos/sock.c +++ b/fw/esp32/components/eos/sock.c @@ -35,7 +35,7 @@ static int t_open_dgram(void) { memset((char *)&_myaddr, 0, sizeof(_myaddr)); _myaddr.sin_family = AF_INET; _myaddr.sin_addr.s_addr = htonl(INADDR_ANY); - _myaddr.sin_port = htons(3000); + _myaddr.sin_port = htons(0); int rv = bind(sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); if (rv < 0) { @@ -54,7 +54,7 @@ static ssize_t t_sendto(int sock, void *msg, size_t msg_size, EOSNetAddr *addr) memset((void *)&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; - servaddr.sin_port = addr->port; + servaddr.sin_port = htons(addr->port); memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host)); return sendto(sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); } @@ -68,85 +68,102 @@ static ssize_t t_recvfrom(int sock, void *msg, size_t msg_size, EOSNetAddr *addr if (recvlen < 0) return recvlen; if (addr) { - addr->port = servaddr.sin_port; + addr->port = ntohs(servaddr.sin_port); memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host)); } return recvlen; } static void udp_rcvr_task(void *pvParameters) { - EOSNetAddr addr; - uint8_t esock = (uint8_t)pvParameters; - int sock = _socks[esock-1]; - unsigned char *buf; + uint8_t sock_i = (uint8_t)pvParameters; + int sock = _socks[sock_i-1]; do { + EOSNetAddr addr; + unsigned char *buf, *_buf; ssize_t rv; buf = eos_net_alloc(); rv = t_recvfrom(sock, buf + EOS_SOCK_SIZE_UDP_HDR, EOS_NET_MTU - EOS_SOCK_SIZE_UDP_HDR, &addr); if (rv < 0) { - sock = 0; eos_net_free(buf); ESP_LOGE(TAG, "UDP RECV ERR:%d", rv); - continue; + break; } - buf[0] = EOS_SOCK_MTYPE_PKT; - buf[1] = esock; - memcpy(buf+2, addr.host, sizeof(addr.host)); - memcpy(buf+2+sizeof(addr.host), &addr.port, sizeof(addr.port)); + _buf = buf; + _buf[0] = EOS_SOCK_MTYPE_PKT; + _buf[1] = sock_i; + _buf += 2; + memcpy(_buf, addr.host, sizeof(addr.host)); + _buf += sizeof(addr.host); + _buf[0] = addr.port >> 8; + _buf[1] = addr.port; + _buf += sizeof(addr.port); eos_net_send(EOS_NET_MTYPE_SOCK, buf, rv + EOS_SOCK_SIZE_UDP_HDR); - } while(sock); + } while (1); + xSemaphoreTake(mutex, portMAX_DELAY); - _socks[esock-1] = 0; + _socks[sock_i-1] = 0; xSemaphoreGive(mutex); vTaskDelete(NULL); } -static void sock_handler(unsigned char type, unsigned char *buffer, uint16_t size) { +static void sock_handler(unsigned char type, unsigned char *buffer, uint16_t buf_len) { EOSNetAddr addr; - uint8_t esock; + uint8_t sock_i; int sock, i; - unsigned char *rbuf; - if (size < 1) return; + if (buf_len < 1) return; switch(buffer[0]) { case EOS_SOCK_MTYPE_PKT: - if (size < EOS_SOCK_SIZE_UDP_HDR) return; - sock = _socks[buffer[1] - 1]; - memcpy(addr.host, buffer + 2, sizeof(addr.host)); - memcpy(&addr.port, buffer + 2 + sizeof(addr.host), sizeof(addr.port)); - t_sendto(sock, buffer + EOS_SOCK_SIZE_UDP_HDR, size - EOS_SOCK_SIZE_UDP_HDR, &addr); + if (buf_len < EOS_SOCK_SIZE_UDP_HDR) return; + + sock_i = buffer[1]-1; + if (sock_i >= EOS_SOCK_MAX_SOCK) return; + + sock = _socks[sock_i]; + buffer += 2; + memcpy(addr.host, buffer, sizeof(addr.host)); + buffer += sizeof(addr.host); + addr.port = (uint16_t)buffer[0] << 8; + addr.port |= (uint16_t)buffer[1]; + buffer += sizeof(addr.port); + t_sendto(sock, buffer, buf_len - EOS_SOCK_SIZE_UDP_HDR, &addr); break; case EOS_SOCK_MTYPE_OPEN_DGRAM: sock = t_open_dgram(); - esock = 0; + sock_i = 0; if (sock > 0) { xSemaphoreTake(mutex, portMAX_DELAY); for (i=0; i<EOS_SOCK_MAX_SOCK; i++) { if (_socks[i] == 0) { - esock = i+1; + sock_i = i+1; _socks[i] = sock; + break; } } xSemaphoreGive(mutex); } - xTaskCreate(&udp_rcvr_task, "udp_rcvr", EOS_TASK_SSIZE_UDP_RCVR, (void *)esock, EOS_TASK_PRIORITY_UDP_RCVR, NULL); - rbuf = eos_net_alloc(); - rbuf[0] = EOS_SOCK_MTYPE_OPEN_DGRAM; - rbuf[1] = esock; - eos_net_send(EOS_NET_MTYPE_SOCK, rbuf, 2); + if (sock_i) xTaskCreate(&udp_rcvr_task, "udp_rcvr", EOS_TASK_SSIZE_UDP_RCVR, (void *)sock_i, EOS_TASK_PRIORITY_UDP_RCVR, NULL); + buffer[0] = EOS_SOCK_MTYPE_OPEN_DGRAM; + buffer[1] = sock_i; + eos_net_reply(EOS_NET_MTYPE_SOCK, buffer, 2); break; case EOS_SOCK_MTYPE_CLOSE: - if (size < 2) return; - sock = _socks[buffer[1]-1]; + if (buf_len < 2) return; + + sock_i = buffer[1]-1; + if (sock_i >= EOS_SOCK_MAX_SOCK) return; + + sock = _socks[sock_i]; t_close(sock); break; default: + ESP_LOGE(TAG, "BAD TYPE:%d", buffer[0]); break; } } @@ -156,4 +173,4 @@ void eos_sock_init(void) { xSemaphoreGive(mutex); eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handler); ESP_LOGI(TAG, "INIT"); -}
\ No newline at end of file +} diff --git a/fw/esp32/components/eos/wifi.c b/fw/esp32/components/eos/wifi.c index d47ae3a..d294f45 100755 --- a/fw/esp32/components/eos/wifi.c +++ b/fw/esp32/components/eos/wifi.c @@ -73,9 +73,10 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e rbuf[0] = EOS_WIFI_MTYPE_SCAN; p = rbuf + 1; for (i=0; i<scan_n; i++) { - len = strnlen((char *)scan_r[i].ssid, 33); - if (len > 32) continue; + len = strnlen((char *)scan_r[i].ssid, sizeof(scan_r[i].ssid)); + if (len == sizeof(scan_r[i].ssid)) continue; if (p - rbuf + len + 1 > EOS_NET_MTU) break; + strcpy((char *)p, (char *)scan_r[i].ssid); p += len + 1; } @@ -168,27 +169,42 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e if (ret != ESP_OK) ESP_LOGE(TAG, "EVT HANDLER ERR:%d EVT:%d", ret, event_id); } -static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) { +static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t buf_len) { uint8_t mtype; int rv; - char *ssid, *pass; + char *ssid, *pass, *_buf; + uint16_t _buf_len; - if (size < 1) return; + if (buf_len < 1) return; - mtype = buffer[0]; rv = EOS_OK; - buffer += 1; - size -= 1; + + mtype = buffer[0]; + buffer++; + buf_len--; switch (mtype) { case EOS_WIFI_MTYPE_SCAN: rv = eos_wifi_scan(); break; - case EOS_WIFI_MTYPE_CONFIG: - ssid = (char *)buffer; - pass = ssid + strlen(ssid) + 1; - rv = eos_wifi_set_config(ssid, pass); + case EOS_WIFI_MTYPE_AUTH: + _buf = (char *)buffer; + _buf_len = 0; + + ssid = _buf; + _buf_len = strnlen(_buf, buf_len); + if (_buf_len == buf_len) break; + _buf += _buf_len + 1; + buf_len -= _buf_len + 1; + + pass = _buf; + _buf_len = strnlen(_buf, buf_len); + if (_buf_len == buf_len) break; + _buf += _buf_len + 1; + buf_len -= _buf_len + 1; + + rv = eos_wifi_auth(ssid, pass); break; case EOS_WIFI_MTYPE_CONNECT: @@ -269,7 +285,7 @@ int eos_wifi_scan(void) { return rv; } -int eos_wifi_set_config(char *ssid, char *pass) { +int eos_wifi_auth(char *ssid, char *pass) { int rv = EOS_OK; xSemaphoreTake(mutex, portMAX_DELAY); |