filedata is part of context
This commit is contained in:
parent
3cea5a1374
commit
5957dcc2bb
10
context.c
10
context.c
@ -2,12 +2,13 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
|
#include "filedata.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
static LIST_HEAD(context_list);
|
static LIST_HEAD(context_list);
|
||||||
|
|
||||||
struct context * create_context(void)
|
struct context * create_context(const char *filename)
|
||||||
{
|
{
|
||||||
struct context *ctx = malloc(sizeof(struct context));
|
struct context *ctx = malloc(sizeof(struct context));
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
@ -17,6 +18,12 @@ struct context * create_context(void)
|
|||||||
|
|
||||||
memset(ctx, 0, sizeof(struct context));
|
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);
|
list_add(&ctx->list, &context_list);
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
@ -28,6 +35,7 @@ int destroy_context(struct context *ctx)
|
|||||||
if (ctx->dev_close != NULL)
|
if (ctx->dev_close != NULL)
|
||||||
ctx->dev_close(ctx);
|
ctx->dev_close(ctx);
|
||||||
|
|
||||||
|
free(ctx->file);
|
||||||
free(ctx);
|
free(ctx);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef _CONTEXT_H_
|
#ifndef _CONTEXT_H_
|
||||||
#define _CONTEXT_H_
|
#define _CONTEXT_H_
|
||||||
|
|
||||||
|
#include "filedata.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
struct context {
|
struct context {
|
||||||
@ -25,9 +26,11 @@ struct context {
|
|||||||
|
|
||||||
/* xmodem */
|
/* xmodem */
|
||||||
int lastpkt;
|
int lastpkt;
|
||||||
|
|
||||||
|
struct filedata *file;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct context * create_context(void);
|
struct context * create_context(const char *filename);
|
||||||
int destroy_context(struct context *ctx);
|
int destroy_context(struct context *ctx);
|
||||||
|
|
||||||
int context_close(void);
|
int context_close(void);
|
||||||
|
6
serial.c
6
serial.c
@ -79,12 +79,8 @@ static int close_serial(struct context *ctx)
|
|||||||
return 0;
|
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));
|
ctx->dev_privdata = malloc(sizeof(struct serial_device));
|
||||||
if (ctx->dev_privdata == NULL) {
|
if (ctx->dev_privdata == NULL) {
|
||||||
log_print(LOG_WARN, "serial_init_cb(): out of memory");
|
log_print(LOG_WARN, "serial_init_cb(): out of memory");
|
||||||
|
4
serial.h
4
serial.h
@ -1,6 +1,8 @@
|
|||||||
#ifndef _SERIAL_H_
|
#ifndef _SERIAL_H_
|
||||||
#define _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_ */
|
#endif /* _SERIAL_H_ */
|
||||||
|
22
xmodem.c
22
xmodem.c
@ -12,8 +12,6 @@
|
|||||||
#include "filedata.h"
|
#include "filedata.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
struct filedata *filedata;
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
XM_SOH = 0x01,
|
XM_SOH = 0x01,
|
||||||
XM_EOT = 0x04,
|
XM_EOT = 0x04,
|
||||||
@ -69,7 +67,7 @@ int xmodem_read(int fd, void *privdata)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case XM_ACK: /* next packet */
|
case XM_ACK: /* next packet */
|
||||||
if (ctx->lastpkt * 128 == filedata->size)
|
if (ctx->lastpkt * 128 == ctx->file->size)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
pktnum = ctx->lastpkt +1;
|
pktnum = ctx->lastpkt +1;
|
||||||
@ -80,12 +78,12 @@ int xmodem_read(int fd, void *privdata)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pktnum * 128 < filedata->size) {
|
if (pktnum * 128 < ctx->file->size) {
|
||||||
pkt.header = XM_SOH;
|
pkt.header = XM_SOH;
|
||||||
pkt.count = ((pktnum +1) & 0xFF);
|
pkt.count = ((pktnum +1) & 0xFF);
|
||||||
pkt.ncount = 0xFF - pkt.count;
|
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);
|
calc_crc(&pkt);
|
||||||
write(fd, &pkt, sizeof(pkt));
|
write(fd, &pkt, sizeof(pkt));
|
||||||
@ -99,17 +97,3 @@ int xmodem_read(int fd, void *privdata)
|
|||||||
ctx->lastpkt = pktnum;
|
ctx->lastpkt = pktnum;
|
||||||
return 0;
|
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);
|
|
||||||
}
|
|
||||||
|
3
xmodem.h
3
xmodem.h
@ -1,9 +1,6 @@
|
|||||||
#ifndef _XMODEM_H_
|
#ifndef _XMODEM_H_
|
||||||
#define _XMODEM_H_
|
#define _XMODEM_H_
|
||||||
|
|
||||||
int xmodem_init(const char *filename);
|
|
||||||
void xmodem_close(void);
|
|
||||||
|
|
||||||
int xmodem_read(int fd, void *privdata);
|
int xmodem_read(int fd, void *privdata);
|
||||||
|
|
||||||
#endif /* _XMODEM_H_ */
|
#endif /* _XMODEM_H_ */
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "event.h"
|
#include "event.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
#include "xmodem.h"
|
|
||||||
|
|
||||||
static struct option opts[] = {
|
static struct option opts[] = {
|
||||||
{"device", 1, 0, 'd'},
|
{"device", 1, 0, 'd'},
|
||||||
@ -45,17 +44,17 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
} while (code != -1);
|
} while (code != -1);
|
||||||
|
|
||||||
if (devicename == NULL || serial_init(devicename))
|
struct context *ctx = create_context(filename);
|
||||||
|
if (ctx == NULL)
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
if (filename == NULL || xmodem_init(filename)) {
|
if (devicename == NULL || serial_init(ctx, devicename)) {
|
||||||
context_close();
|
context_close();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
event_loop();
|
event_loop();
|
||||||
|
|
||||||
xmodem_close();
|
|
||||||
context_close();
|
context_close();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user