-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cc
More file actions
201 lines (183 loc) · 5.21 KB
/
Copy pathMain.cc
File metadata and controls
201 lines (183 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
XBeeSetup Main.cc
Copyright: Copyright (C) 2013,2014 Mitsuru Nakada All Rights Reserved.
License: GNU GPL v2
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include "XBeeIF.h"
#include "Error.h"
int main(int argc, char **argv) {
int error = 0;
int config = 0;
int device = 0;
int debug = 0;
int reset = 0;
int forceUpdate = 0;
XBeeIF XB;
int setupCmds[16];
int setupCmdNum = 0;
for(int i = 1; i < argc; i++) {
if(!strcmp(argv[i], "-f")) {
forceUpdate = 1;
continue;
}
if(!strcmp(argv[i], "-c")) {
i++;
config = i;
continue;
}
if(!strcmp(argv[i], "-d")) {
i++;
device = i;
continue;
}
if(!strcmp(argv[i], "-r")) {
reset = 1;
continue;
}
if(!strcmp(argv[i], "-s")) {
i++;
setupCmds[setupCmdNum++] = i;
continue;
}
if(!strcmp(argv[i], "-debug")) {
XB.EnableLog();
continue;
}
error = Error;
}
if(error || !device) {
fprintf(stderr, "usage : %s [-f] [-c <config file>] -d <device file> -s <setup1> -s <setup2> ...\n", argv[0]);
fprintf(stderr, " -f : force update\n");
fprintf(stderr, " -s : setup config (It's inserted before the setup item in the config file.)\n");
return Error;
}
if(reset) return XB.Reset(argv[device]);
int fd = XB.Initialize(argv[device]);
if(fd < 0) {
fprintf(stderr, "device open error : %s\n", argv[device]);
return fd;
}
XBeeIF::DevInfo devInfo;
error = XB.GetDeviceInfo(&devInfo);
fprintf(stderr, "Baudrate : %d\n", XB.GetBaudRate());
if(!error) {
fprintf(stderr, "FirmwareVersion : %04x\n", devInfo.FWVer);
fprintf(stderr, "HardwareVersion : %04x\n", devInfo.HWVer);
fprintf(stderr, "BootVersion : %04x\n", devInfo.BootVer);
fprintf(stderr, "SerialNumber : %08x-%08x\n", devInfo.SerialHi, devInfo.SerialLo);
fprintf(stderr, "NodeIdentifier : %s\n", devInfo.NodeID);
}
error = Success;
if(config || setupCmdNum) {
XBeeIF::SetupTableSt setupTable[256];
int setupTableSize = 0;
char buf[256];
char firmware[256] = "";
FILE *fp = fopen(argv[config], "r");
if(fp == NULL) return Error;
int mode = -1;
int line = 0;
while(!feof(fp)) {
char *q = fgets(buf, 255, fp);
if(q == NULL) break;
line++;
char *p = strchr(buf, '#');
if(p) *p = 0;
p = strtok(buf, ": \t\r\n");
if(!p) continue;
if(!strcmp(p, "firmware")) {
p = strtok(NULL, ": \t\r\n");
if(p) {
strncpy(firmware, p, 255);
firmware[255] = 0;
fprintf(stderr, "Firmware : %s\n", firmware);
}
continue;
}
if(!strcmp(p, "setup")) {
mode = 1;
continue;
}
if(mode == 1) { // setup:
if(setupTableSize > 255) {
error = 1;
break;
}
setupTable[setupTableSize].Cmd[0] = (unsigned char)toupper(p[0]);
setupTable[setupTableSize].Cmd[1] = (unsigned char)toupper(p[1]);
if(!strncmp((char *)setupTable[setupTableSize].Cmd, "WR", 2)) continue;
int c;
for(c = 2; c < 256; c++) {
p = strtok(NULL, ": \t\r\n");
if(!p) break;
setupTable[setupTableSize].Cmd[c] = strtoul(p, NULL, 16);
}
setupTable[setupTableSize++].Size = c;
}
}
fclose(fp);
for(int i = 0; i < setupCmdNum; i++) {
if(setupTableSize > 255) {
error = 1;
break;
}
strcpy(buf, argv[setupCmds[i]]);
char *p = strtok(buf, ": \t\r\n");
if(!p) {
fprintf(stderr, "setup parapeter error\n");
return Error;
}
setupTable[setupTableSize].Cmd[0] = (unsigned char)toupper(p[0]);
setupTable[setupTableSize].Cmd[1] = (unsigned char)toupper(p[1]);
if(!strncmp((char *)setupTable[setupTableSize].Cmd, "WR", 2)) continue;
int c;
for(c = 2; c < 256; c++) {
p = strtok(NULL, ": \t\r\n");
if(!p) break;
setupTable[setupTableSize].Cmd[c] = strtoul(p, NULL, 16);
}
setupTable[setupTableSize++].Size = c;
}
setupTable[setupTableSize].Cmd[0] = (unsigned char)'W';
setupTable[setupTableSize].Cmd[1] = (unsigned char)'R';
setupTable[setupTableSize++].Size = 2;
if(error) {
fprintf(stderr, "Error : Config format error\n ---> line%d,mode%d: %s\n", line, mode, buf);
return Error;
}
fprintf(stderr, "Read Config Done.\n");
if(setupTableSize < 0) goto error;
char path[256];
strcpy(path, firmware);
char *name = basename(path);
char *dir = dirname(path);
error = NoExec;
if(!strcmp(name, "*")) {
error = XB.Update(dir, devInfo.HWVer, devInfo.BootVer, devInfo.FWVer, forceUpdate);
} else if(strlen(firmware)) {
error = XB.SendFirmware(firmware, "Main", forceUpdate);
}
if(!error) {
XB.Finalize();
fd = XB.Initialize(argv[device]);
if(fd < 0) error = Error;
}
if(error >= 0) {
error = XB.SetupCommands(setupTable, setupTableSize);
if(error) {
fprintf(stderr, "Setup commands error\n");
} else {
fprintf(stderr, "Complete.\n");
}
}
}
error:
return error;
}