atd

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

commit 773f86531c6999f14aa5a38d78dd085d12c3e732
parent 7a37115628b13c8d15430390d95d0b3e916db03b
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Mon,  7 Jun 2021 10:55:12 -0500

extract encoding/decoding code out

Diffstat:
Matc.c | 47+++++++++++++++++++++++++++++------------------
Matd.c | 36++++++++++++++++--------------------
Matd.h | 18+++++++-----------
Aencdec.c | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aencdec.h | 4++++
5 files changed, 174 insertions(+), 49 deletions(-)

diff --git a/atc.c b/atc.c @@ -5,6 +5,7 @@ #include <unistd.h> #include "atd.h" +#include "encdec.h" int sendcode(int fd, int code) @@ -47,6 +48,12 @@ dial(int fd, char *num) } int +parse_callevent(int fd) +{ + struct call calls[MAX_CALLS]; +} + +int main(int argc, char *argv[]) { enum ops cmd; @@ -86,28 +93,32 @@ main(int argc, char *argv[]) switch (cmd) { case CMD_DIAL: - dial(sock, argv[2]); + atd_cmd_dial(sock, argv[2]); break; - default: - sendcode(sock, cmd); + case CMD_HANGUP: + atd_cmd_hangup(sock); + break; + case CMD_ANSWER: + atd_cmd_answer(sock); break; } - char buf[1024]; - - if (cmd != CMD_CALL_EVENTS) { - read(sock, buf, 1); - } else { - read(sock, buf, 1024); - } - - if (*buf == STATUS_OK) - fprintf(stderr, "OK\n"); - else if (*buf == STATUS_OK) - fprintf(stderr, "ERROR\n"); - else if (*buf == STATUS_CALL) { - fprintf(stderr, "CALLSTATUS\n"); - } + char op; + + read(sock, &op, 1); + /* + if (op == STATUS_CALL) { + parse_callevent(fd); + } + */ + + 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"); + } sleep(1); diff --git a/atd.c b/atd.c @@ -15,6 +15,7 @@ #include <unistd.h> #include "atd.h" +#include "encdec.h" #include "util.h" #include "queue.h" @@ -38,6 +39,19 @@ #define BUFSIZE 256 +struct command_args cmddata[] = { + [CMD_DIAL] = { ATD, { TYPE_STRING, TYPE_NONE} }, + [CMD_ANSWER] = { ATA, { TYPE_NONE} }, + [CMD_HANGUP] = { ATH, { TYPE_NONE} }, +}; + +char *atcmds[] = { + [ATD] = "ATD%s;\r", + [ATA] = "ATA\r", + [ATH] = "ATH\r", + [CLCC] = "AT+CLCC\r", +}; + char *argv0; struct fdbuf { @@ -59,27 +73,9 @@ int calld = -1; struct fdbuf fdbufs[MAX_FDS] = {0}; -#define MAX_CALLS 8 struct call calls[MAX_CALLS]; struct call calls2[MAX_CALLS]; -ssize_t -parse_str(char *in, char **out) -{ - short len; - char *ptr = in; - len = ptr[0] + (ptr[1] << 8); - assert(len < BUFSIZE); - ptr += 2; - - *out = malloc(len); - if (!(*out)) - return -1; - - memcpy(*out, ptr, len); - return len + 2; -} - /* 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 * the command is invalid but terminated */ @@ -94,7 +90,7 @@ ssize_t cmdadd(int index) { cmd.op = *(ptr++); switch (cmd.op) { case CMD_DIAL: - count = parse_str(ptr, &cmd.data.dial.num); + count = dec_str(ptr, &cmd.data.dial.num); if (count == -1) return -1; @@ -192,7 +188,7 @@ update_call_status() len = strlen(calls2[i].num); *ptr = len; strcpy(ptr + 1, calls2[i].num); - ptr += len; + ptr += len + 1; buf[1]++; } diff --git a/atd.h b/atd.h @@ -1,7 +1,11 @@ +#ifndef ATD_H +#define ATD_H + #include <stdbool.h> #define PHONE_NUMBER_MAX_LEN 15 #define DIALING_DIGITS "0123456789*#+ABC" +#define MAX_CALLS 8 /* should have at most 256 things */ enum ops { @@ -65,15 +69,7 @@ struct command_args { enum type type[MAX_PARAMS + 1]; }; -struct command_args cmddata[] = { - [CMD_DIAL] = { ATD, { TYPE_STRING, TYPE_NONE} }, - [CMD_ANSWER] = { ATA, { TYPE_NONE} }, - [CMD_HANGUP] = { ATH, { TYPE_NONE} }, -}; +extern struct command_args cmddata[]; +extern char *atcmds[]; -char *atcmds[] = { - [ATD] = "ATD%s;\r", - [ATA] = "ATA\r", - [ATH] = "ATH\r", - [CLCC] = "AT+CLCC\r", -}; +#endif diff --git a/encdec.c b/encdec.c @@ -0,0 +1,118 @@ +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "atd.h" +#include "encdec.h" + +static unsigned short +dec_short(char *in) +{ + return in[0] + (in[1] << 8); +} + +static unsigned short +enc_short(char *buf, unsigned short num) +{ + buf[0] = num; + buf[1] = num >> 8; +} + +ssize_t +dec_str(char *in, char **out) +{ + unsigned short len; + char *ptr = in; + len = dec_short(in); + ptr += 2; + + *out = malloc(len); + if (!(*out)) + return -1; + + memcpy(*out, ptr, len); + return len + 2; +} + +static ssize_t +enc_str(char *buf, char *str) +{ + size_t len = strlen(str); + enc_short(buf, len); + buf += 2; + strcpy(buf, str); + + return len + 2; +} + +int +xwrite(int fd, char *buf, size_t len) +{ + char *ptr; + ssize_t ret; + while (len) { + ret = write(fd, buf, len); + if (ret == -1) + return -1; + + len -= ret; + ptr += ret; + } +} + +int +atd_cmd_dial(int fd, char *num) +{ + size_t len = strlen(num) + 3; // 3 = op + length + char buf[len]; + buf[0] = CMD_DIAL; + enc_str(buf + 1, num); + + return xwrite(fd, buf, len); +} + +int +atd_cmd_hangup(int fd) +{ + char buf = CMD_HANGUP; + return xwrite(fd, &buf, 1); +} + +int +atd_cmd_answer(int fd) +{ + char buf = CMD_ANSWER; + return xwrite(fd, &buf, 1); +} + +int +atd_cmd_call_events(int fd) +{ + char buf = CMD_CALL_EVENTS; + return xwrite(fd, &buf, 1); +} + +int +atd_status_call(int fd, struct call *calls, size_t len) +{ + char buf[(PHONE_NUMBER_MAX_LEN + 2) * MAX_CALLS + 2]; + char *ptr; + ssize_t ret; + + buf[0] = STATUS_CALL; + buf[1] = 0; + + ptr = buf + 2; + + for (int i = 0; i < len; i++) { + if (!(calls[i].present)) + continue; + + ptr += enc_str(ptr, calls[i].num); + + buf[1]++; + } + + return xwrite(fd, buf, ptr - buf); +} diff --git a/encdec.h b/encdec.h @@ -0,0 +1,4 @@ +int atd_cmd_dial(int fd, char *num); +int atd_cmd_hangup(int fd); +int atd_cmd_answer(int fd); +ssize_t dec_str(char *in, char **out);