atd

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

commit 74fe540fa16843f13761680946601e1f4663ba79
parent ea3e8cb5fd13f138a06d2ff1b066b65a5e411b87
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Mon,  7 Jun 2021 12:27:54 -0500

call status response works!

Diffstat:
Matc.c | 60+++++++++++-------------------------------------------------
Matd.c | 51+++++++++++----------------------------------------
Mencdec.c | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mencdec.h | 3+++
4 files changed, 87 insertions(+), 92 deletions(-)

diff --git a/atc.c b/atc.c @@ -7,51 +7,7 @@ #include "atd.h" #include "encdec.h" -int -sendcode(int fd, int code) -{ - char c = code; - /* TODO make more robust */ - int ret = write(fd, &c, 1); - if (ret == -1) - return -1; -} - -int -dial(int fd, char *num) -{ - int ret, len = strlen(num), left; - char buf[PHONE_NUMBER_MAX_LEN + 3]; - char *ptr; - - if (len > PHONE_NUMBER_MAX_LEN) - return -1; - - buf[0] = CMD_DIAL; - buf[1] = len & 0xFF; - buf[2] = (len >> 8) & 0xFF; - - memcpy(buf + 3, num, len); - - left = len + 3; - ptr = buf; - - do { - ret = write(fd, ptr, left); - if (ret == -1) - return -1; - ptr += ret; - left -= ret; - } while (left); - - return 0; -} - -int -parse_callevent(int fd) -{ - struct call calls[MAX_CALLS]; -} +struct call calls[MAX_CALLS]; int main(int argc, char *argv[]) @@ -101,23 +57,29 @@ main(int argc, char *argv[]) case CMD_ANSWER: atd_cmd_answer(sock); break; + case CMD_CALL_EVENTS: + atd_cmd_call_events(sock); + break; } char op; read(sock, &op, 1); - /* if (op == STATUS_CALL) { - parse_callevent(fd); + dec_call_status(sock, calls); } - */ if (op == STATUS_OK) fprintf(stderr, "OK\n"); else if (op == STATUS_OK) fprintf(stderr, "ERROR\n"); else if (op == STATUS_CALL) { - fprintf(stderr, "CALLSTATUS\n"); + for (int i = 0; i < MAX_CALLS; i++) { + if (!calls[i].present) + continue; + + fprintf(stderr, "status: %s, %d", calls[i].num, calls[i].status); + } } sleep(1); diff --git a/atd.c b/atd.c @@ -75,7 +75,6 @@ struct fdbuf fdbufs[MAX_FDS] = {0}; struct pollfd fds[MAX_FDS]; struct call calls[MAX_CALLS]; -struct call calls2[MAX_CALLS]; /* add one command to queue, returns the number of bytes intepreted if the * command was validated and added successfully, -1 if the queue is full, -2 if @@ -154,54 +153,24 @@ parseclcc(char *start, size_t len) fprintf(stderr, "clcc: got %d successful matches\n", ret); - calls2[idx] = call; + calls[idx] = call; } /* [0] = STATUS_CALL [1] = # of update entries list of update entries follows update entry is a callstatus, followed by a string containing the phone number */ -void +int update_call_status() { - fprintf(stderr, "update call status\n"); - char buf[(PHONE_NUMBER_MAX_LEN + 2) * MAX_CALLS + 2]; - char *ptr; - size_t len; - ssize_t ret; - - if (calld < 0) - return; - - buf[0] = STATUS_CALL; - buf[1] = 0; - - ptr = buf + 2; - - for (int i = 1; i < MAX_CALLS; i++) { - if (!(calls[i].present || calls2[i].present)) - continue; - - if ((calls[i].status == calls2[i].status) && strcmp(calls[i].num, calls2[i].num) == 0) - continue; - fprintf(stderr, "update POINT 0: %d, %s\n", i, calls2[i].num); - - len = strlen(calls2[i].num); - *ptr = len; - strcpy(ptr + 1, calls2[i].num); - ptr += len + 1; - - buf[1]++; + int status; + if (calld != -1) { + fprintf(stderr, "update call status\n"); + status = atd_status_call(fds[calld].fd, calls, MAX_CALLS); } - fprintf(stderr, "update POINT 1: %d\n", ptr - buf); - memcpy(fdbufs[calld].inptr, buf, ptr - buf); - fdbufs[calld].inlen += ptr - buf; - - fprintf(stderr, "update POINT 2\n"); - memcpy(calls, calls2, MAX_CALLS*sizeof(calls[0])); - memset(calls2, 0, MAX_CALLS*sizeof(calls[0])); - fprintf(stderr, "update POINT 3\n"); + memset(calls, 0, MAX_CALLS*sizeof(calls[0])); + return status; } size_t @@ -227,8 +196,10 @@ handle_resp(int fd, int idx) if (handling_urc) handling_urc = false; if (currentatcmd == CLCC) { - update_call_status(); + if (update_call_status() < 0) + fprintf(stderr, "failed to update call status\n"); check_call_status = false; + return ptr - fdbufs[idx].out; } fprintf(stderr, "got OK\n"); } else if (strncmp(start, "ERROR", sizeof("ERROR") - 1) == 0) { diff --git a/encdec.c b/encdec.c @@ -49,10 +49,25 @@ enc_str(char *buf, char *str) int xwrite(int fd, char *buf, size_t len) { - char *ptr; + char *ptr = buf; + ssize_t ret; + while (len) { + ret = write(fd, ptr, len); + if (ret == -1) + return -1; + + len -= ret; + ptr += ret; + } +} + +int +xread(int fd, char *buf, size_t len) +{ + char *ptr = buf; ssize_t ret; while (len) { - ret = write(fd, buf, len); + ret = read(fd, ptr, len); if (ret == -1) return -1; @@ -96,7 +111,7 @@ atd_cmd_call_events(int fd) int atd_status_call(int fd, struct call *calls, size_t len) { - char buf[(PHONE_NUMBER_MAX_LEN + 2) * MAX_CALLS + 2]; + char buf[(PHONE_NUMBER_MAX_LEN + 3) * MAX_CALLS + 2]; char *ptr; ssize_t ret; @@ -109,6 +124,8 @@ atd_status_call(int fd, struct call *calls, size_t len) if (!(calls[i].present)) continue; + *(ptr++) = i; + *(ptr++) = calls[i].status; ptr += enc_str(ptr, calls[i].num); buf[1]++; @@ -116,3 +133,45 @@ atd_status_call(int fd, struct call *calls, size_t len) return xwrite(fd, buf, ptr - buf); } + +/* calls should be MAX_CALLS long */ +int +dec_call_status(int fd, struct call *calls) +{ + char count = 0, idx = 0, status = 0; + unsigned short len = 0; + ssize_t ret; + char buf[PHONE_NUMBER_MAX_LEN]; + ret = xread(fd, &count, 1); + + if (ret == -1 || count > MAX_CALLS) + return -1; + + for (int i = 0; i < count; i++) { + ret = xread(fd, &idx, 1); + if (ret == -1 || idx > MAX_CALLS - 1) + return -1; + + ret = xread(fd, &status, 1); + if (ret == -1 || status > CALL_WAITING) + return -1; + + ret = xread(fd, buf, 2); + if (ret == -1) + return -1; + + len = dec_short(buf); + if (len > PHONE_NUMBER_MAX_LEN) + return -1; + + ret = xread(fd, buf, len); + if (ret == -1) + return -1; + + calls[idx].present = true; + calls[idx].status = status; + memcpy(calls[idx].num, buf, len); + } + + return 0; +} diff --git a/encdec.h b/encdec.h @@ -1,4 +1,7 @@ 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_status_call(int fd, struct call *calls, size_t len); ssize_t dec_str(char *in, char **out); +int dec_call_status(int fd, struct call *calls);