From cbffc8d64707828b045b679e8db0269ae362ffcf Mon Sep 17 00:00:00 2001 From: laforge Date: Mon, 23 Oct 2006 20:28:37 +0000 Subject: more gsmd/libgsm implementation git-svn-id: http://svn.openmoko.org/trunk/src/target/gsm@114 99fdad57-331a-0410-800a-d7fa5415bdb3 --- src/util/Makefile.am | 2 +- src/util/event.c | 38 +++++++++++++++++++++++++++++++++ src/util/event.h | 2 ++ src/util/libgsmd-tool.c | 13 +++++++++++- src/util/pin.c | 39 ++++++++++++++++++++++++++++++++++ src/util/pin.h | 2 ++ src/util/shell.c | 56 ++++++++++++++++++++++++++++++++++++++++--------- 7 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 src/util/event.c create mode 100644 src/util/event.h create mode 100644 src/util/pin.c create mode 100644 src/util/pin.h (limited to 'src/util') diff --git a/src/util/Makefile.am b/src/util/Makefile.am index bec625a..aee83ca 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -3,7 +3,7 @@ AM_CFLAGS = -std=gnu99 bin_PROGRAMS = libgsmd-tool -libgsmd_tool_SOURCES = libgsmd-tool.c shell.c +libgsmd_tool_SOURCES = libgsmd-tool.c shell.c event.c pin.c libgsmd_tool_LDADD = ../libgsmd/libgsmd.la libgsmd_tool_LDFLAGS = -dynamic diff --git a/src/util/event.c b/src/util/event.c new file mode 100644 index 0000000..601ea9a --- /dev/null +++ b/src/util/event.c @@ -0,0 +1,38 @@ +#include +#include + +#include +#include + +static int incall_handler(struct lgsm_handle *lh, int evt, struct gsmd_evt_auxdata *aux) +{ + printf("EVENT: Incoming call type=%u!\n", aux->u.call.type); + + return 0; +} + +static int clip_handler(struct lgsm_handle *lh, int evt, struct gsmd_evt_auxdata *aux) +{ + printf("EVENT: Incoming call clip=`%s'\n", aux->u.clip.addr.number); + + return 0; +} + +static int netreg_handler(struct lgsm_handle *lh, int evt, struct gsmd_evt_auxdata *aux) +{ + printf("EVENT: Netreg\n"); + + return 0; +} + +int event_init(struct lgsm_handle *lh) +{ + int rc; + + rc = lgsm_evt_handler_register(lh, GSMD_EVT_IN_CALL, &incall_handler); + rc |= lgsm_evt_handler_register(lh, GSMD_EVT_IN_CLIP, &clip_handler); + rc |= lgsm_evt_handler_register(lh, GSMD_EVT_NETREG, &netreg_handler); + + return rc; +} + diff --git a/src/util/event.h b/src/util/event.h new file mode 100644 index 0000000..6901a2d --- /dev/null +++ b/src/util/event.h @@ -0,0 +1,2 @@ + +extern int event_init(struct lgsm_handle *lh); diff --git a/src/util/libgsmd-tool.c b/src/util/libgsmd-tool.c index e8590c1..8d4674c 100644 --- a/src/util/libgsmd-tool.c +++ b/src/util/libgsmd-tool.c @@ -8,6 +8,9 @@ #include +#include "pin.h" +#include "event.h" + #ifndef ARRAY_SIZE #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #endif @@ -44,6 +47,7 @@ static struct option opts[] = { { "version", 0, 0, 'V' }, { "verbose", 0, 0, 'v' }, { "mode", 1, 0, 'm' }, + { "pin", 1, 0, 'p' }, { 0, 0, 0, 0 } }; @@ -57,6 +61,7 @@ static void help(void) int main(int argc, char **argv) { + char *pin = NULL; int rc, i, mode; printf("libgsm-tool - (C) 2006 by Harald Welte\n" @@ -64,7 +69,7 @@ int main(int argc, char **argv) while (1) { int c, option_index = 0; - c = getopt_long(argc, argv, "vVhm:", opts, &option_index); + c = getopt_long(argc, argv, "vVhm:p:", opts, &option_index); if (c == -1) break; @@ -86,6 +91,9 @@ int main(int argc, char **argv) exit(2); } break; + case 'p': + pin = optarg; + break; } } @@ -95,6 +103,9 @@ int main(int argc, char **argv) exit(1); } + pin_init(lgsmh, pin); + event_init(lgsmh); + switch (mode) { case MODE_SHELL: shell_main(lgsmh); diff --git a/src/util/pin.c b/src/util/pin.c new file mode 100644 index 0000000..b855e53 --- /dev/null +++ b/src/util/pin.c @@ -0,0 +1,39 @@ +#include +#include + +#include +#include + +#define PIN_SIZE 32 + +static char *pin; +static char pinbuf[PIN_SIZE+1]; + +static int pin_handler(struct lgsm_handle *lh, int evt, struct gsmd_evt_auxdata *aux) +{ + int rc; + + printf("EVENT: PIN request (type=%u) ", aux->u.pin.type); + + /* FIXME: read pin from STDIN and send it back via lgsm_pin */ + if (aux->u.pin.type == 1 && pin) { + printf("Auto-responding with pin `%s'\n", pin); + lgsm_pin(lh, pin); + } else { + do { + printf("Please enter PIN: "); + rc = fscanf(stdin, "%32s\n", &pinbuf); + } while (rc < 1); + + return lgsm_pin(lh, pinbuf); + } + + return 0; +} + +int pin_init(struct lgsm_handle *lh, const char *pin_preset) +{ + pin = pin_preset; + return lgsm_evt_handler_register(lh, GSMD_EVT_PIN, &pin_handler); +} + diff --git a/src/util/pin.h b/src/util/pin.h new file mode 100644 index 0000000..534e2b4 --- /dev/null +++ b/src/util/pin.h @@ -0,0 +1,2 @@ + +extern int pin_init(struct lgsm_handle *lh, const char *pin_preset); diff --git a/src/util/shell.c b/src/util/shell.c index 3b13bc9..77f0c57 100644 --- a/src/util/shell.c +++ b/src/util/shell.c @@ -1,30 +1,66 @@ #include #include +#include +#include #include #define STDIN_BUF_SIZE 1024 -int shell_main(struct lgsmd_handle *lgsmh) +/* this is the handler for receiving passthrough responses */ +static int pt_msghandler(struct lgsm_handle *lh, struct gsmd_msg_hdr *gmh) +{ + char *payload = (char *)gmh + sizeof(*gmh); + printf("RSTR=`%s'\n", payload); +} + +int shell_main(struct lgsm_handle *lgsmh) { int rc; char buf[STDIN_BUF_SIZE+1]; char rbuf[STDIN_BUF_SIZE+1]; int rlen = sizeof(rbuf); + fd_set readset; + + lgsm_register_handler(lgsmh, GSMD_MSG_PASSTHROUGH, &pt_msghandler); + + FD_ZERO(&readset); while (1) { - rc = fscanf(stdin, "%s", buf); - if (rc == EOF) { - printf("EOF\n"); - return -1; + fd_set readset; + int gsm_fd = lgsm_fd(lgsmh); + FD_SET(0, &readset); + FD_SET(gsm_fd, &readset); + + rc = select(gsm_fd+1, &readset, NULL, NULL, NULL); + if (rc <= 0) + break; + if (FD_ISSET(gsm_fd, &readset)) { + /* we've received something on the gsmd socket, pass it + * on to the library */ + rc = read(gsm_fd, buf, sizeof(buf)); + if (rc <= 0) { + printf("ERROR reding from gsm_fd\n"); + break; + } + rc = lgsm_handle_packet(lgsmh, buf, rc); } - if (rc <= 0) { - printf("NULL\n"); - continue; + if (FD_ISSET(0, &readset)) { + /* we've received something on stdin. send it as passthrough + * to gsmd */ + rc = fscanf(stdin, "%s", buf); + if (rc == EOF) { + printf("EOF\n"); + return -1; + } + if (rc <= 0) { + printf("NULL\n"); + continue; + } + printf("STR=`%s'\n", buf); } - printf("STR=`%s'\n", buf); + /* this is a synchronous call for a passthrough command */ lgsm_passthrough(lgsmh, buf, rbuf, &rlen); - printf("RSTR=`%s'\n", rbuf); } } -- cgit v1.2.3