diff --git a/context.c b/context.c index f51c6e1..5776ed6 100644 --- a/context.c +++ b/context.c @@ -2,12 +2,13 @@ #include #include "context.h" +#include "filedata.h" #include "list.h" #include "logging.h" static LIST_HEAD(context_list); -struct context * create_context(void) +struct context * create_context(const char *filename) { struct context *ctx = malloc(sizeof(struct context)); if (ctx == NULL) { @@ -17,6 +18,12 @@ struct context * create_context(void) memset(ctx, 0, sizeof(struct context)); + ctx->file = get_filedata(filename); + if (ctx->file == NULL) { + free(ctx); + return NULL; + } + list_add(&ctx->list, &context_list); return ctx; } @@ -28,6 +35,7 @@ int destroy_context(struct context *ctx) if (ctx->dev_close != NULL) ctx->dev_close(ctx); + free(ctx->file); free(ctx); return 0; } diff --git a/context.h b/context.h index 7975995..daaccbe 100644 --- a/context.h +++ b/context.h @@ -1,6 +1,7 @@ #ifndef _CONTEXT_H_ #define _CONTEXT_H_ +#include "filedata.h" #include "list.h" struct context { @@ -25,9 +26,11 @@ struct context { /* xmodem */ int lastpkt; + + struct filedata *file; }; -struct context * create_context(void); +struct context * create_context(const char *filename); int destroy_context(struct context *ctx); int context_close(void); diff --git a/serial.c b/serial.c index 4760930..23aea78 100644 --- a/serial.c +++ b/serial.c @@ -79,12 +79,8 @@ static int close_serial(struct context *ctx) return 0; } -int serial_init(const char *device) +int serial_init(struct context *ctx, const char *device) { - struct context *ctx = create_context(); - if (ctx == NULL) - return -1; - ctx->dev_privdata = malloc(sizeof(struct serial_device)); if (ctx->dev_privdata == NULL) { log_print(LOG_WARN, "serial_init_cb(): out of memory"); diff --git a/serial.h b/serial.h index a0e1596..0b3237e 100644 --- a/serial.h +++ b/serial.h @@ -1,6 +1,8 @@ #ifndef _SERIAL_H_ #define _SERIAL_H_ -int serial_init(const char *device); +#include "context.h" + +int serial_init(struct context *ctx, const char *device); #endif /* _SERIAL_H_ */ diff --git a/xmodem.c b/xmodem.c index a51de86..bbb9dbc 100644 --- a/xmodem.c +++ b/xmodem.c @@ -12,8 +12,6 @@ #include "filedata.h" #include "logging.h" -struct filedata *filedata; - enum { XM_SOH = 0x01, XM_EOT = 0x04, @@ -69,7 +67,7 @@ int xmodem_read(int fd, void *privdata) break; case XM_ACK: /* next packet */ - if (ctx->lastpkt * 128 == filedata->size) + if (ctx->lastpkt * 128 == ctx->file->size) return -1; pktnum = ctx->lastpkt +1; @@ -80,12 +78,12 @@ int xmodem_read(int fd, void *privdata) break; } - if (pktnum * 128 < filedata->size) { + if (pktnum * 128 < ctx->file->size) { pkt.header = XM_SOH; pkt.count = ((pktnum +1) & 0xFF); pkt.ncount = 0xFF - pkt.count; - memcpy(pkt.data, (void *)(filedata->data) + pktnum * 128, 128); + memcpy(pkt.data, (void *)(ctx->file->data) + pktnum * 128, 128); calc_crc(&pkt); write(fd, &pkt, sizeof(pkt)); @@ -99,17 +97,3 @@ int xmodem_read(int fd, void *privdata) ctx->lastpkt = pktnum; return 0; } - -int xmodem_init(const char *filename) -{ - filedata = get_filedata(filename); - if (filedata == NULL) - return -1; - - return 0; -} - -void xmodem_close(void) -{ - free(filedata); -} diff --git a/xmodem.h b/xmodem.h index 362c785..b99ebb4 100644 --- a/xmodem.h +++ b/xmodem.h @@ -1,9 +1,6 @@ #ifndef _XMODEM_H_ #define _XMODEM_H_ -int xmodem_init(const char *filename); -void xmodem_close(void); - int xmodem_read(int fd, void *privdata); #endif /* _XMODEM_H_ */ diff --git a/zyxel-revert.c b/zyxel-revert.c index 829be57..f6c28f2 100644 --- a/zyxel-revert.c +++ b/zyxel-revert.c @@ -8,7 +8,6 @@ #include "context.h" #include "event.h" #include "serial.h" -#include "xmodem.h" static struct option opts[] = { {"device", 1, 0, 'd'}, @@ -45,17 +44,17 @@ int main(int argc, char *argv[]) } } while (code != -1); - if (devicename == NULL || serial_init(devicename)) + struct context *ctx = create_context(filename); + if (ctx == NULL) exit(1); - if (filename == NULL || xmodem_init(filename)) { + if (devicename == NULL || serial_init(ctx, devicename)) { context_close(); exit(1); } event_loop(); - xmodem_close(); context_close(); return 0; }