summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/cell_ussd.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/esp32/components/eos/cell_ussd.c')
-rw-r--r--fw/esp32/components/eos/cell_ussd.c55
1 files changed, 30 insertions, 25 deletions
diff --git a/fw/esp32/components/eos/cell_ussd.c b/fw/esp32/components/eos/cell_ussd.c
index 976fd51..375025f 100644
--- a/fw/esp32/components/eos/cell_ussd.c
+++ b/fw/esp32/components/eos/cell_ussd.c
@@ -11,8 +11,7 @@
static const char *TAG = "EOS USSD";
-static char cmd[256];
-static int cmd_len;
+extern char *at_cmd_buf;
void eos_cell_ussd_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
int rv;
@@ -21,16 +20,16 @@ void eos_cell_ussd_handler(unsigned char mtype, unsigned char *buffer, uint16_t
buf_len--;
switch (mtype) {
case EOS_CELL_MTYPE_USSD_REQUEST: {
- if (buf_len > EOS_CELL_MAX_USSD_STR) return;
+ if (buf_len > EOS_CELL_USSD_SIZE_REQ) return;
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;
+ rv = snprintf(at_cmd_buf, AT_SIZE_CMD_BUF, "AT+CUSD=1,\"%s\",15\r", buffer);
+ if ((rv < 0) || (rv >= AT_SIZE_CMD_BUF)) return;
rv = eos_modem_take(1000);
if (rv) return;
- at_cmd(cmd);
+ at_cmd(at_cmd_buf);
rv = at_expect("^OK", "^ERROR", 1000);
eos_modem_give();
@@ -56,11 +55,6 @@ static void ussd_reply_handler(char *urc, regmatch_t m[]) {
uint16_t len;
char *_buf;
size_t _len;
- regex_t re;
- regmatch_t match[2];
-
- rv = regcomp(&re, ".*(\",[0-9]+)$", REG_EXTENDED);
- if (rv) return;
sscanf(urc + m[1].rm_so, "%1d", &rep);
@@ -69,15 +63,27 @@ static void ussd_reply_handler(char *urc, regmatch_t m[]) {
buf[1] = rep;
len = 2;
- rv = EOS_OK;
+ if (m[2].rm_so == -1) {
+ eos_net_send(EOS_NET_MTYPE_CELL, buf, len);
+ return;
+ }
+
_buf = (char *)buf + len;
- strcpy(_buf, urc + m[2].rm_so);
+ _len = m[2].rm_eo - m[2].rm_so - 2;
+ if (_len > EOS_NET_SIZE_BUF - len) {
+ eos_net_free(buf);
+ return;
+ }
+ memcpy(_buf, urc + m[2].rm_so + 2, _len);
+ len += _len;
+
+ rv = EOS_OK;
do {
- if (regexec(&re, _buf, 2, match, 0) == 0) {
- ESP_LOGI(TAG, "MATCH:%ld %s", match[1].rm_so, _buf);
- _buf[match[1].rm_so] = '\0';
- _len = strlen(_buf);
- len += _len + 1;
+ char *end;
+
+ end = strchr(_buf, '"');
+ if (end) {
+ len += end - _buf;
break;
} else {
_len = strlen(_buf);
@@ -85,20 +91,19 @@ static void ussd_reply_handler(char *urc, regmatch_t m[]) {
_buf += _len + 1;
len += _len + 1;
}
- rv = eos_modem_readln(_buf, EOS_NET_MTU - len, 1000);
+ rv = eos_modem_readln(_buf, EOS_NET_SIZE_BUF - len, 1000);
if (rv) break;
} while (1);
if (rv) {
ESP_LOGE(TAG, "error");
eos_net_free(buf);
- } else {
- eos_net_send(EOS_NET_MTYPE_CELL, buf, len);
+ return;
}
- regfree(&re);
+
+ eos_net_send(EOS_NET_MTYPE_CELL, buf, len);
}
void eos_cell_ussd_init(void) {
- at_urc_insert("\\+CUSD: ([0-9]),\"(.*)", ussd_reply_handler, REG_EXTENDED);
-
-} \ No newline at end of file
+ at_urc_insert("^\\+CUSD: ([0-9])(,\".*)?", ussd_reply_handler, REG_EXTENDED);
+}