diff options
Diffstat (limited to 'src/modemman.c')
-rw-r--r-- | src/modemman.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/modemman.c b/src/modemman.c new file mode 100644 index 0000000..c156e61 --- /dev/null +++ b/src/modemman.c @@ -0,0 +1,60 @@ +#include "modemman.h" +#include "modem.h" + +#define MAX_CHANNELS 10 +#define TTY_START 11 + +struct llist { + struct modem *modem; + struct llist *next; +}; + +static struct llist *llist = NULL; +static int n_llist = 0; + +int modem_manager_init(int channels) { + int i; + + assert(channels <= MAX_CHANNELS && channels > 0); + assert(n_llist == 0); + + for (i = TTY_START; i < TTY_START+MAX_CHANNLES && n_llist < channels; i++) { + char d[PATH_MAX]; + struct modem *m; + + snprintf(d, sizeof(d), "/dev/ttyI%i", i); + + if (m = modem_open(d)) { + struct llist *l = malloc(sizeof(struct llist)); + assert(l); + + l->modem = m; + l->next = llist; + llist = l; + n_llist++; + } + } + + if (n_llist < channels) { + daemon_log(LOG_ERR, "Failed to allocate the requested channels. Got %i of %i.", n_llist, channels); + modem_manager_done(); + return -1; + } + + return 0; +} + +void modem_manager_done(void) { + + while (llist) { + struct llist *l; + + modem_close(l->modem); + l = llist; + llist = l->next; + free(l); + n_llist = 0; + } + + assert(!n_llist && !llist); +} |