Browse Source

filedata is part of context

Olaf Rempel 12 years ago
parent
commit
5957dcc2bb
7 changed files with 23 additions and 34 deletions
  1. 9
    1
      context.c
  2. 4
    1
      context.h
  3. 1
    5
      serial.c
  4. 3
    1
      serial.h
  5. 3
    19
      xmodem.c
  6. 0
    3
      xmodem.h
  7. 3
    4
      zyxel-revert.c

+ 9
- 1
context.c View File

@@ -2,12 +2,13 @@
2 2
 #include <string.h>
3 3
 
4 4
 #include "context.h"
5
+#include "filedata.h"
5 6
 #include "list.h"
6 7
 #include "logging.h"
7 8
 
8 9
 static LIST_HEAD(context_list);
9 10
 
10
-struct context * create_context(void)
11
+struct context * create_context(const char *filename)
11 12
 {
12 13
 	struct context *ctx = malloc(sizeof(struct context));
13 14
 	if (ctx == NULL) {
@@ -17,6 +18,12 @@ struct context * create_context(void)
17 18
 
18 19
 	memset(ctx, 0, sizeof(struct context));
19 20
 
21
+	ctx->file = get_filedata(filename);
22
+	if (ctx->file == NULL) {
23
+		free(ctx);
24
+		return NULL;
25
+	}
26
+
20 27
 	list_add(&ctx->list, &context_list);
21 28
 	return ctx;
22 29
 }
@@ -28,6 +35,7 @@ int destroy_context(struct context *ctx)
28 35
 	if (ctx->dev_close != NULL)
29 36
 		ctx->dev_close(ctx);
30 37
 
38
+	free(ctx->file);
31 39
 	free(ctx);
32 40
 	return 0;
33 41
 }

+ 4
- 1
context.h View File

@@ -1,6 +1,7 @@
1 1
 #ifndef _CONTEXT_H_
2 2
 #define _CONTEXT_H_
3 3
 
4
+#include "filedata.h"
4 5
 #include "list.h"
5 6
 
6 7
 struct context {
@@ -25,9 +26,11 @@ struct context {
25 26
 
26 27
 	/* xmodem */
27 28
 	int lastpkt;
29
+
30
+	struct filedata *file;
28 31
 };
29 32
 
30
-struct context * create_context(void);
33
+struct context * create_context(const char *filename);
31 34
 int destroy_context(struct context *ctx);
32 35
 
33 36
 int context_close(void);

+ 1
- 5
serial.c View File

@@ -79,12 +79,8 @@ static int close_serial(struct context *ctx)
79 79
 	return 0;
80 80
 }
81 81
 
82
-int serial_init(const char *device)
82
+int serial_init(struct context *ctx, const char *device)
83 83
 {
84
-	struct context *ctx = create_context();
85
-	if (ctx == NULL)
86
-		return -1;
87
-
88 84
 	ctx->dev_privdata = malloc(sizeof(struct serial_device));
89 85
 	if (ctx->dev_privdata == NULL) {
90 86
 		log_print(LOG_WARN, "serial_init_cb(): out of memory");

+ 3
- 1
serial.h View File

@@ -1,6 +1,8 @@
1 1
 #ifndef _SERIAL_H_
2 2
 #define _SERIAL_H_
3 3
 
4
-int serial_init(const char *device);
4
+#include "context.h"
5
+
6
+int serial_init(struct context *ctx, const char *device);
5 7
 
6 8
 #endif /* _SERIAL_H_ */

+ 3
- 19
xmodem.c View File

@@ -12,8 +12,6 @@
12 12
 #include "filedata.h"
13 13
 #include "logging.h"
14 14
 
15
-struct filedata *filedata;
16
-
17 15
 enum {
18 16
 	XM_SOH = 0x01,
19 17
 	XM_EOT = 0x04,
@@ -69,7 +67,7 @@ int xmodem_read(int fd, void *privdata)
69 67
 		break;
70 68
 
71 69
 	case XM_ACK:	/* next packet */
72
-		if (ctx->lastpkt * 128 == filedata->size)
70
+		if (ctx->lastpkt * 128 == ctx->file->size)
73 71
 			return -1;
74 72
 
75 73
 		pktnum = ctx->lastpkt +1;
@@ -80,12 +78,12 @@ int xmodem_read(int fd, void *privdata)
80 78
 		break;
81 79
 	}
82 80
 
83
-	if (pktnum * 128 < filedata->size) {
81
+	if (pktnum * 128 < ctx->file->size) {
84 82
 		pkt.header = XM_SOH;
85 83
 		pkt.count = ((pktnum +1) & 0xFF);
86 84
 		pkt.ncount = 0xFF - pkt.count;
87 85
 
88
-		memcpy(pkt.data, (void *)(filedata->data) + pktnum * 128, 128);
86
+		memcpy(pkt.data, (void *)(ctx->file->data) + pktnum * 128, 128);
89 87
 
90 88
 		calc_crc(&pkt);
91 89
 		write(fd, &pkt, sizeof(pkt));
@@ -99,17 +97,3 @@ int xmodem_read(int fd, void *privdata)
99 97
 	ctx->lastpkt = pktnum;
100 98
 	return 0;
101 99
 }
102
-
103
-int xmodem_init(const char *filename)
104
-{
105
-	filedata = get_filedata(filename);
106
-	if (filedata == NULL)
107
-		return -1;
108
-
109
-	return 0;
110
-}
111
-
112
-void xmodem_close(void)
113
-{
114
-	free(filedata);
115
-}

+ 0
- 3
xmodem.h View File

@@ -1,9 +1,6 @@
1 1
 #ifndef _XMODEM_H_
2 2
 #define _XMODEM_H_
3 3
 
4
-int xmodem_init(const char *filename);
5
-void xmodem_close(void);
6
-
7 4
 int xmodem_read(int fd, void *privdata);
8 5
 
9 6
 #endif /* _XMODEM_H_ */

+ 3
- 4
zyxel-revert.c View File

@@ -8,7 +8,6 @@
8 8
 #include "context.h"
9 9
 #include "event.h"
10 10
 #include "serial.h"
11
-#include "xmodem.h"
12 11
 
13 12
 static struct option opts[] = {
14 13
 	{"device",	1, 0, 'd'},
@@ -45,17 +44,17 @@ int main(int argc, char *argv[])
45 44
 		}
46 45
 	} while (code != -1);
47 46
 
48
-	if (devicename == NULL || serial_init(devicename))
47
+	struct context *ctx = create_context(filename);
48
+	if (ctx == NULL)
49 49
 		exit(1);
50 50
 
51
-	if (filename == NULL || xmodem_init(filename)) {
51
+	if (devicename == NULL || serial_init(ctx, devicename)) {
52 52
 		context_close();
53 53
 		exit(1);
54 54
 	}
55 55
 
56 56
 	event_loop();
57 57
 
58
-	xmodem_close();
59 58
 	context_close();
60 59
 	return 0;
61 60
 }

Loading…
Cancel
Save