filedata is part of context

This commit is contained in:
Olaf Rempel 2007-05-02 20:42:12 +02:00
parent 3cea5a1374
commit 5957dcc2bb
7 changed files with 23 additions and 34 deletions

View File

@ -2,12 +2,13 @@
#include <string.h>
#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;
}

View File

@ -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);

View File

@ -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");

View File

@ -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_ */

View File

@ -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);
}

View File

@ -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_ */

View File

@ -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;
}