summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/cell_sms.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/esp32/components/eos/cell_sms.c')
-rw-r--r--fw/esp32/components/eos/cell_sms.c66
1 files changed, 37 insertions, 29 deletions
diff --git a/fw/esp32/components/eos/cell_sms.c b/fw/esp32/components/eos/cell_sms.c
index 1cd4cda..c162d4d 100644
--- a/fw/esp32/components/eos/cell_sms.c
+++ b/fw/esp32/components/eos/cell_sms.c
@@ -224,26 +224,30 @@ static ssize_t sms_decode(char *pdu, size_t pdu_len, unsigned char *buf, uint16_
return buf_len;
}
-void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
+void eos_cell_sms_handler(unsigned char _mtype, EOSMessage *msg, uint16_t len) {
+ unsigned char mtype;
+ unsigned char *buffer = msg->buffer;
int rv;
+ if (len < 1) return;
+
+ mtype = buffer[0] & ~EOS_CELL_MTYPE_MASK;
buffer++;
- buf_len--;
+ len--;
+
switch (mtype) {
case EOS_CELL_MTYPE_SMS_LIST: {
- if (buf_len < 1) return;
+ if (len < 1) break;
rv = snprintf(at_cmd_buf, AT_SIZE_CMD_BUF, "AT+CMGL=%d\r", buffer[0]);
- if ((rv < 0) || (rv >= AT_SIZE_CMD_BUF)) return;
+ if ((rv < 0) || (rv >= AT_SIZE_CMD_BUF)) break;
rv = eos_modem_take(1000);
- if (rv) return;
+ if (rv) break;
at_cmd(at_cmd_buf);
do {
- unsigned char *buf;
- uint16_t buf_len;
-
+ EOSMessage _msg;
char *pdu = _pdu_in;
size_t pdu_size = sizeof(_pdu_in);
size_t pdu_len;
@@ -257,16 +261,19 @@ void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t b
pdu_len = strlen(pdu);
- buf = eos_net_alloc();
- buf[0] = EOS_CELL_MTYPE_SMS | EOS_CELL_MTYPE_SMS_LIST_ITEM;
- _rv = sms_decode(pdu, pdu_len, buf + 1, EOS_NET_SIZE_BUF - 1);
+ eos_net_alloc(&_msg);
+ if (_msg.size < 1) {
+ eos_net_free(&_msg);
+ continue;
+ }
+ _msg.buffer[0] = EOS_CELL_MTYPE_SMS | EOS_CELL_MTYPE_SMS_LIST_ITEM;
+ _rv = sms_decode(pdu, pdu_len, _msg.buffer + 1, _msg.size - 1);
if (_rv < 0) {
- eos_net_free(buf);
+ eos_net_free(&_msg);
continue;
}
- buf_len = _rv;
- rv = eos_net_send(EOS_NET_MTYPE_CELL, buf, buf_len + 1);
+ rv = eos_net_send(EOS_NET_MTYPE_CELL, &_msg, _rv + 1);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
} while (1);
@@ -282,19 +289,19 @@ void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t b
size_t pdu_len;
ssize_t _rv;
- _rv = sms_encode(buffer, buf_len, pdu, pdu_size);
- if (_rv < 0) return;
+ _rv = sms_encode(buffer, len, pdu, pdu_size);
+ if (_rv < 0) break;
pdu_len = _rv;
- if (pdu_size < pdu_len + 2) return;
+ if (pdu_size < pdu_len + 2) break;
pdu[pdu_len] = CTRL_Z;
pdu[pdu_len + 1] = '\0';
rv = snprintf(at_cmd_buf, AT_SIZE_CMD_BUF, "AT+CMGS=%d\r", pdu_len / 2);
- if ((rv < 0) || (rv >= AT_SIZE_CMD_BUF)) return;
+ if ((rv < 0) || (rv >= AT_SIZE_CMD_BUF)) break;
rv = eos_modem_take(1000);
- if (rv) return;
+ if (rv) break;
at_cmd(at_cmd_buf);
// wait for: '> ' (0d 0a 3e 20)
@@ -310,15 +317,13 @@ void eos_cell_sms_handler(unsigned char mtype, unsigned char *buffer, uint16_t b
}
static void sms_received_handler(char *urc, regmatch_t m[]) {
+ EOSMessage msg;
char cmd[32];
- unsigned char *buf;
- uint16_t buf_len;
- int ref, rv;
-
char *pdu = _pdu_out;
size_t pdu_size = sizeof(_pdu_out);
size_t pdu_len;
ssize_t _rv;
+ int ref, rv;
sscanf(urc + m[1].rm_so, "%d", &ref);
@@ -337,16 +342,19 @@ static void sms_received_handler(char *urc, regmatch_t m[]) {
rv = at_expect("^OK", NULL, 1000);
- buf = eos_net_alloc();
- buf[0] = EOS_CELL_MTYPE_SMS | EOS_CELL_MTYPE_SMS_MSG;
- _rv = sms_decode(pdu, pdu_len, buf + 1, EOS_NET_SIZE_BUF - 1);
+ eos_net_alloc(&msg);
+ if (msg.size < 1) {
+ eos_net_free(&msg);
+ return;
+ }
+ msg.buffer[0] = EOS_CELL_MTYPE_SMS | EOS_CELL_MTYPE_SMS_MSG;
+ _rv = sms_decode(pdu, pdu_len, msg.buffer + 1, msg.size - 1);
if (_rv < 0) {
- eos_net_free(buf);
+ eos_net_free(&msg);
return;
}
- buf_len = _rv;
- rv = eos_net_send(EOS_NET_MTYPE_CELL, buf, buf_len + 1);
+ rv = eos_net_send(EOS_NET_MTYPE_CELL, &msg, _rv + 1);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
}