diff options
Diffstat (limited to 'src/atcmd.c')
-rw-r--r-- | src/atcmd.c | 117 |
1 files changed, 104 insertions, 13 deletions
diff --git a/src/atcmd.c b/src/atcmd.c index aad15e9..8c6c3f0 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -674,6 +674,51 @@ int atcmd_plus_cmgw_write(int fd, const char *msg, size_t msg_len) return mem_index; } +int atcmd_plus_cmgw_write_text(int fd, const char *addr, int addr_type, + const char *status, const char *msg, size_t msg_len) +{ + char buf[ATCMD_LINE_SIZE]; + int tmp; + int mem_index; + + atcmd_printf(fd, "AT+CMGW"); + if (addr) { + atcmd_printf(fd, "=\"%s\"", addr); + if (addr_type != SMS_ADDR_UNSPEC) { + atcmd_printf(fd, ",%d", addr_type); + if (status) { + atcmd_printf(fd, ",\"%s\"", status); + } + } + } + atcmd_write_str(fd, ATCMD_EOL); + + tmp = atcmd_read_until(fd, buf, sizeof(buf), "> "); + if (tmp <= 0) { + log_debug("expected > start sequence but it was not received"); + return -1; + } + + tmp = atcmd_write(fd, msg, strlen(msg)); + tmp = atcmd_write_str(fd, CONTROL_Z_STR); + + tmp = atcmd_expect_line(fd, buf, sizeof(buf), "+CMGW: "); + if (tmp <= 0) { + log_debug("expected +CMGW: but it was not received"); + return -1; + } + + mem_index = atoi(buf + strlen("+CMGW: ")); + + tmp = atcmd_expect_line(fd, buf, sizeof(buf), "OK"); + if (tmp <= 0) { + log_debug("expected OK but it was not received"); + return -1; + } + + return mem_index; +} + int atcmd_plus_cmgs_write(int fd, const char *msg, size_t msg_len) { char buf[ATCMD_LINE_SIZE]; @@ -706,6 +751,38 @@ int atcmd_plus_cmgs_write(int fd, const char *msg, size_t msg_len) return msg_ref; } +int atcmd_plus_cmgs_write_text(int fd, const char *addr, const char *msg, size_t msg_len) +{ + char buf[ATCMD_LINE_SIZE]; + int tmp; + int msg_ref; + + atcmd_writeline(fd, "AT+CMGS=\"%s\"", addr); + tmp = atcmd_read_until(fd, buf, sizeof(buf), "> "); + if (tmp <= 0) { + log_debug("expected > start sequence but it was not received"); + return -1; + } + + tmp = atcmd_write(fd, msg, strlen(msg)); + tmp = atcmd_write_str(fd, CONTROL_Z_STR); + + tmp = atcmd_expect_line(fd, buf, sizeof(buf), "+CMGS: "); + if (tmp <= 0) { + log_debug("expected +CMGS: but it was not received"); + return -1; + } + msg_ref = atoi(buf + strlen("+CMGS: ")); + + tmp = atcmd_expect_line(fd, buf, sizeof(buf), "OK"); + if (tmp <= 0) { + log_debug("expected OK but it was not received"); + return -1; + } + + return msg_ref; +} + int atcmd_plus_cmss_write(int fd, int index, const char *addr, int addr_type) { char buf[ATCMD_LINE_SIZE]; @@ -785,6 +862,10 @@ int atcmd_plus_cpms_read(int fd, struct data_store *read_store, struct data_store *data_stores[] = {read_store, send_store, new_store}; struct data_store *store; + memset(read_store, 0, sizeof(*read_store)); + memset(send_store, 0, sizeof(*send_store)); + memset(new_store, 0, sizeof(*new_store)); + atcmd_writeline(fd, "AT+CPMS?"); tmp = atcmd_expect_line(fd, buf, sizeof(buf), "+CPMS: "); if (tmp <= 0) { @@ -804,7 +885,7 @@ int atcmd_plus_cpms_read(int fd, struct data_store *read_store, token = atcmd_value_tok(&save); if (!token) { - return -1; + break; } strncpy(store->name, token, STORE_NAME_LEN); @@ -906,8 +987,15 @@ int atcmd_plus_cpms_write(int fd, const char *read_name, char buf[ATCMD_LINE_SIZE]; int tmp; - atcmd_writeline(fd, "AT+CPMS=\"%s\",\"%s\",\"%s\"", - read_name, send_name, new_name); + atcmd_printf(fd, "AT+CPMS=\"%s\"", read_name); + if (send_name) { + atcmd_printf(fd, ",\"%s\"", send_name); + if (new_name) { + atcmd_printf(fd, ",\"%s\"", new_name); + } + } + atcmd_write_str(fd, ATCMD_EOL); + tmp = atcmd_expect_line(fd, buf, sizeof(buf), "OK"); if (tmp <= 0) { log_debug("expected OK but it was not received"); @@ -1158,9 +1246,9 @@ static int sms_atcmd_init(int fd) return -1; } - tmp = atcmd_plus_cmgf_write(fd, SMS_PDU_MODE); + tmp = atcmd_plus_cmgf_write(fd, Global.core.sms_mode); if (tmp < 0) { - return -1; + log_error("atcmd_plus_cmgf_write failed"); } memset(&read_store, 0, sizeof(read_store)); @@ -1175,25 +1263,28 @@ static int sms_atcmd_init(int fd) Global.core.msg_store_new) { if (!msg_store_choice(&read_store, Global.core.msg_store_read)) { - log_error("message storage location %s is not a choice", + log_error("read message storage location %s is not a choice", Global.core.msg_store_read); return -1; } - if (!msg_store_choice(&send_store, Global.core.msg_store_send)) { - log_error("message storage location %s is not a choice", + if (*Global.core.msg_store_send && !msg_store_choice(&send_store, Global.core.msg_store_send)) { + log_error("send message storage location %s is not a choice", Global.core.msg_store_send); return -1; } - if (!msg_store_choice(&new_store, Global.core.msg_store_new)) { - log_error("message storage location %s is not a choice", + if (*Global.core.msg_store_new && !msg_store_choice(&new_store, Global.core.msg_store_new)) { + log_error("new message storage location %s is not a choice", Global.core.msg_store_new); return -1; } - tmp = atcmd_plus_cpms_write(fd, Global.core.msg_store_read, - Global.core.msg_store_send, Global.core.msg_store_new); + tmp = atcmd_plus_cpms_write(fd, + Global.core.msg_store_read, + *Global.core.msg_store_send ? Global.core.msg_store_send : NULL, + *Global.core.msg_store_new ? Global.core.msg_store_new : NULL + ); if (tmp < 0) { - return -1; + log_error("atcmd_plus_cpms_write failed"); } } |