-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_com.ino
More file actions
executable file
·357 lines (319 loc) · 7.8 KB
/
node_com.ino
File metadata and controls
executable file
·357 lines (319 loc) · 7.8 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*节点相关宏定义*/
#define NID 04
//节点编号,nodeID
#define NCT 0 //控制节点
#define NBC 255 //广播号
#define NSZ 5 //允许的最大节点数,nodeSize
#define FML 64 //协议栈最大帧长度
#define MXTMR 8 //链路状态计时器最大值
#define BUFSZ 128 //读数据缓冲区长度
enum CMD{
Broadcast = 0xA5, //广播路由表作为心跳包
SendData = 0x01, //数据传输
LinkCtrl = 0x30, //路由表控制命令
SendCtrl = 0x50, //控制发送
};
struct RouteNode{
bool ctrlAble; //链路控制器
byte timer; //链路状态计数器
};
//路由树节点
struct SearchNode{
byte nodeId;
SearchNode *parent;
SearchNode *bother;
SearchNode *sons;
SearchNode(byte id){
nodeId = id;
parent = NULL;
bother = NULL;
sons = NULL;
}
};
RouteNode RouteTable[NSZ][NSZ]; //路由表记录
byte FrameNo = 0; //frame No
byte RCtr = 0; //网络层控制字
byte MACCtr = 0; //MAC控制字
byte readBuff[BUFSZ];
byte buffLength;
void setup() {
//初始化路由表和路由控制表
for(int i = 0; i < NSZ; ++i){
for(int j = 0; j < NSZ; ++j){
RouteTable[i][j].ctrlAble = true;
RouteTable[i][j].timer = 0;
}
}
buffLength = 0;
Serial.begin(9600);//设置波特率
}
void loop() {
for(int i = 0; i < 4; i++){
while(receive());
delay(500);
}
routeCount();
broadcast();
delay(2000);
}
void broadcast()
{
byte arr[NSZ*NSZ];
for(int i = 0; i < NSZ; ++i){
for(int j = 0; j < NSZ; ++j){
if(RouteTable[i][j].ctrlAble){
arr[i*NSZ+j] = RouteTable[i][j].timer;
}else{
arr[i*NSZ+j] = 0;
}
}
}
sendTo(arr,NSZ*NSZ, NBC, CMD::Broadcast);
}
bool sendTo(byte *data, byte Rlen, byte NDS, byte cmd)
{
byte RFrame[56];
RFrame[0] = NID; //相当于ip协议中的源ip地址
RFrame[1] = NDS; //相当于ip协议中的目的ip地址
RFrame[2] = RCtr; //网络层控制字
RFrame[3] = cmd; //命令
for(int i = 0; i < Rlen; ++i){
RFrame[4+i] = data[i];
}
byte NodeDst = findWay(NDS);
if(NodeDst == byte(0xFE)){
return false;
}
structFrame(RFrame, Rlen+4, NodeDst);
return true;
}
byte findWay(byte NDS)
{
if(NDS == NBC || NDS == NCT){
return NDS;
}else{
byte dnode = search(NDS);
if(dnode == byte(0xFE)){
byte info[] = "[Error]:0 connot find way to 0\n";
info[8] = '0'+ NID;
info[29] = '0'+NDS;
sendTo(info, 31, 0, CMD::SendData);
}
return dnode;
}
return byte(0xFE);
}
bool structFrame(byte *data, byte len, byte NodeDst)
{
if(len > 56){
return false;
}
byte frame[64];
frame[0] = 0xFE; //帧头
frame[1] = NID; //相当于源MAC地址
frame[2] = NodeDst; //相当于目的MAC地址
frame[3] = FrameNo; //帧号高位
frame[4] = MACCtr; //控制字
frame[5] = len; //帧长
for(int i = 0; i < len; ++i){
frame[6+i] = data[i];
}
frame[6+len] = getCheck(frame, len+6);
while(receive());
Serial.write(frame, len+7);
++ FrameNo;
return true;
}
byte getCheck(byte *frame, byte len)
{
byte checkdata = 0;
for(int i = 0; i < len; ++i){
checkdata += frame[i];
}
return (~checkdata);
}
bool receive()
{
int len = Serial.available();
if(len > 0){
Serial.readBytes(&(readBuff[buffLength]), len);
buffLength += len;
parseBuf();
return true;
}
return false;
}
//从缓冲区中寻找一个帧
void parseBuf()
{
byte *frame;
int frameBegin = 0, frameLength = 0;
while(frameBegin + 5 < buffLength && readBuff[frameBegin] != 0xFE) { frameBegin++; }
if(frameBegin + 5 < buffLength){
frameLength = readBuff[frameBegin+5]+7;
if(frameBegin+frameLength <= BUFSZ){
frame = new byte[frameLength];
memcpy(frame, readBuff, frameLength);
frameBegin += frameLength;
}
}
cleanBuff(frameBegin);
parseFrame(frame, frameLength); //帧内数据解析
delete frame;
}
//清理读数据缓冲区,删除已被解析的数据
void cleanBuff(int frameBegin)
{
buffLength -= frameBegin;
memcpy(readBuff, &(readBuff[frameBegin]), buffLength);
}
void parseFrame(byte *frame, int frameLength)
{
if(frame[2] != NID && frame[2] != NBC){
//MAC目的地址不是本节点目的地址或广播地址就忽略
return;
}else if(frame[frameLength-1] != getCheck(frame, frameLength-1)){
//byte info[] = "[Error]:0 check error!";
//info[8] = '0'+ NID;
//sendTo(info, 22, 0, CMD::SendData);
return;
}
switch(frame[9]){
case CMD::Broadcast:
rcvBC(frame, frameLength);
break;
case CMD::SendData:
rcvSD(frame, frameLength);
break;
case CMD::LinkCtrl:
rcvCN(frame, frameLength);
break;
case CMD::SendCtrl:
rcvNT(frame, frameLength);
break;
}
}
void rcvBC(byte *frame, int frameLength)
{
if(RouteTable[NID-1][frame[1]-1].ctrlAble){
RouteTable[NID-1][frame[1]-1].timer = MXTMR;
RouteTable[frame[1]-1][NID-1].timer = MXTMR;
}
for(int i = 0; i < NSZ; ++i){
for(int j = 0; j < NSZ; ++j){
if(frame[i*NSZ+j+10] > RouteTable[i][j].timer){
RouteTable[i][j].timer = frame[i*NSZ+j+10];
}
}
}
}
void rcvSD(byte *frame, int frameLength)
{
if(frame[7] == NID){
//如果本机就是目的节点,向控制主机报告收到数据
byte *info = new byte[13+frame[5]];
memcpy(info, "[info]:0 rcv:", 13);
memcpy(&(info[13]), &(frame[9]), frame[5]);
info[7] = '0'+NID;
sendTo(info, 13+frame[5], 0, SendData);
delete info;
}else{
byte NDS = findWay(frame[7]);
if(NDS != -1){
structFrame(&(frame[6]), frame[5], NDS);
}
}
}
void rcvCN(byte *frame, int frameLength)
{
for(int i = 0; i < NSZ; ++i){
for(int j = 0; j < NSZ; ++j){
RouteTable[i][j].ctrlAble = bool(frame[i*NSZ+j+10]);
if(!RouteTable[i][j].ctrlAble){
RouteTable[i][j].timer = 0;
}
}
}
}
void rcvNT(byte *frame, int frameLength)
{
sendTo(&(frame[11]), frame[5]-5, frame[10], CMD::SendData);
}
byte search(byte target)
{
SearchNode *root = new SearchNode(NID);
byte result = doSearch(root, target);
clearTree(root);
return result;
}
byte doSearch(SearchNode *parent, byte target)
{
byte result = 0xFE;
SearchNode *frontBother = NULL;
for(int i = 0; i < NSZ; ++i){
//判断路由链路是否可达
if(RouteTable[parent->nodeId-1][i].timer>0 && RouteTable[parent->nodeId-1][i].ctrlAble){
//环路检测,防止出现环路
bool flag = true;
for(SearchNode *tmp = parent; tmp != NULL && flag; tmp = tmp->parent){
if(tmp->nodeId == i+1){
flag = false;
}
}
//加入子/兄节点
if(flag){
SearchNode *tmp = new SearchNode(i+1);
tmp->parent = parent;
if(frontBother == NULL){
parent->sons = tmp;
}else{
frontBother->bother = tmp;
}
frontBother = tmp;
}
}
}
//搜索子节点
for(frontBother = parent->sons; frontBother != NULL; frontBother = frontBother->bother){
if(frontBother->nodeId == target){
result = getNextNode(frontBother);
}else{
result = doSearch(frontBother, target);
}
if(result != byte(0xFE)){
break;
}
}
return result;
}
//返回下一跳目标节点
byte getNextNode(SearchNode *leaf)
{
SearchNode *tmp = leaf;
while(tmp->parent->parent != NULL){
tmp = tmp->parent;
}
return tmp->nodeId;
}
void clearTree(SearchNode *parent)
{
for(SearchNode *frontBother = parent->sons; frontBother != NULL; frontBother = frontBother->bother){
clearTree(frontBother);
}
delete parent;
}
void routeCount()
{
//若链路计时器到0,认为链路已断开
for(int i = 0; i < NSZ; ++i){
for(int j = 0; j < NSZ; ++j){
if(RouteTable[i][j].timer > 0){
RouteTable[i][j].timer -= 1;
}
}
}
}
void serialEvent()
{
receive();
}