atd

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/atd
Log | Files | Refs

commit d9f3212b7770973e6ad6d5744b4098dda197ccd0
parent 729035bda27181f0bbfab09964ca7505f8cda77e
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Wed, 11 Aug 2021 14:22:08 -0500

add sms events event, and sms delivered status

Diffstat:
Matd.h | 6++++++
Mencdec.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mencdec.h | 3+++
3 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/atd.h b/atd.h @@ -14,6 +14,7 @@ enum ops { CMD_ANSWER, CMD_HANGUP, CMD_CALL_EVENTS, + CMD_SMS_EVENTS, CMD_SUBMIT, }; @@ -31,6 +32,7 @@ enum status { STATUS_OK = 1, STATUS_ERROR, STATUS_CALL, + STATUS_DELIVERED, }; enum atcmd { @@ -63,6 +65,10 @@ struct call { char num[PHONE_NUMBER_MAX_LEN + 1]; }; +struct sms { + char num[PHONE_NUMBER_MAX_LEN + 1]; + char *msg; +}; /* should have at most 256 things */ enum type { diff --git a/encdec.c b/encdec.c @@ -110,6 +110,13 @@ atd_cmd_call_events(int fd) } int +atd_cmd_sms_events(int fd) +{ + char buf = CMD_SMS_EVENTS; + return xwrite(fd, &buf, 1); +} + +int atd_cmd_submit(int fd, char *num, char *msg) { size_t len = strlen(num) + strlen(msg) + 5; // 5 = op + length + length @@ -122,6 +129,22 @@ atd_cmd_submit(int fd, char *num, char *msg) } int +atd_status_delivered(int fd, char *num, char *msg) +{ + if (strlen(num) > PHONE_NUMBER_MAX_LEN) + return -1; + + size_t len = strlen(num) + strlen(msg) + 5; // 5 = op + length + length + char buf[len]; + buf[0] = STATUS_DELIVERED; + + enc_str(buf + 1, num); + enc_str(buf + 3 + strlen(num), msg); + + return xwrite(fd, buf, len); +} + +int atd_status_call(int fd, enum callstatus status, char *num) { char buf[4 + strlen(num)]; @@ -167,3 +190,42 @@ dec_call_status(int fd, struct call *call) return 0; } + +int +dec_sms_status(int fd, struct sms *sms) +{ + char status = 0; + unsigned short len = 0; + ssize_t ret; + char buf[PHONE_NUMBER_MAX_LEN]; + + ret = xread(fd, buf, 2); + if (ret == -1) + return -1; + + len = dec_short(buf); + if (len > PHONE_NUMBER_MAX_LEN || len == 0) + return -1; + + ret = xread(fd, sms->num, len); + if (ret == -1) + return -1; + + ret = xread(fd, buf, 2); + if (ret == -1) + return -1; + + len = dec_short(buf); + if (len == 0) + return 0; // XXX should we accept empty messages? + + sms->msg = calloc(1, len); + if (sms->msg == NULL) + return -1; + + ret = xread(fd, sms->msg, len); + if (ret == -1) + return -1; + + return 0; +} diff --git a/encdec.h b/encdec.h @@ -2,9 +2,12 @@ int atd_cmd_dial(int fd, char *num); int atd_cmd_hangup(int fd); int atd_cmd_answer(int fd); int atd_cmd_call_events(int fd); +int atd_cmd_sms_events(int fd); int atd_cmd_submit(int fd, char *num, char *msg); int atd_status_call(int fd, enum callstatus status, char *num); +int atd_status_delivered(int fd, char *num, char *msg); ssize_t dec_str(char *in, char **out); int dec_call_status(int fd, struct call *calls); +int dec_sms_status(int fd, struct sms *sms); int xwrite(int fd, char *buf, size_t len); int xread(int fd, char *buf, size_t len);