-
Notifications
You must be signed in to change notification settings - Fork 1
/
pb_protocol.c
executable file
·304 lines (251 loc) · 7.11 KB
/
pb_protocol.c
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
//******************************************
// This is a C Template file
//作者:
//日期
//功能
//备注
//******************************************
#include "MQ6812.h"
#include "iApp.h"
#include "FlashFunction.h"
#include "pb_protocol.h"
#include "pb_package.h"
#include "pb_comm.h"
#include "DeviceAttachDet.h"
MSG_ROLE pb_role;
uchar decode_result;
uchar pb_waiting_response_flag = 0;
uchar dev0_id[4] = {0};
CHANNEL_STA channel_sta = 0;
extern struct PB_MSG pb_sent_msg1, pb_sent_msg2;
extern struct PB_MSG pb_rec_msg1, pb_rec_msg2;
extern uchar DEV_ATT_PRE ;
extern uchar DEV_ATT_CUR ;
extern uchar DEV_ATT_NXT ;
extern INFO_COMM_STA comm_pro;
extern IDENTIFY_PROCESS id_process ;
extern ID_RESULT id_res ;
extern uchar wich_side_chged ;
extern uchar rx_data[];
extern INFO_COMM_STA comm_pro;
extern uchar start_comm_info_flag;
//------------------------------------------- RECEIVE PROCESS -------------------------------------------//
void Receive_PB_Package(void)
{
uchar side = 0;
Rx_Rcv_Data();
if(flag_rec_msg==0) return;
flag_rec_msg = 0;
if(rx_data[1] != 0)
{
if(start_comm_info_flag==1)
channel_sta = IDLE;
if(channel_sta == IDLE || channel_sta == WAIT_REPLY )
{
channel_sta = RECEIVE;
Buffer_To_Message();
Analysis_PB_Package();
}
}
}
uchar wait_reply_cnt;
uchar Analysis_PB_Package(void)
{
uchar dir=0;
uchar side = 0;
char type =0;
/************ To see wether the package is legal or not ************/
// if(DEV_ATT_CUR!=0) // dont execute this at the very begining.
// {
// if(pb_rec_msg1.msg_dir + pb_sent_msg1.msg_dir != 1 )
// {
// decode_result = 0;
// return decode_result ; // fail, illegal message.
// }
// }
if(channel_sta == IDLE)
channel_sta = RECEIVE;
// if(channel_sta == SENT )//&& pb_rec_msg1.msg_type == (pb_sent_msg1.msg_type + 1) )
// channel_sta = WAIT_FOR_REPLY;
if(channel_sta == RECEIVE || channel_sta == WAIT_REPLY|| channel_sta == IDLE)
{
type = pb_rec_msg1.msg_type;
switch (pb_rec_msg1.msg_type)
{
__asm("NOP"); // switch 与第一个 case 之间置入一行 "NOP",避免编译出 "JP gg" 指令!
case SHOW_ID: {
wich_side_chged = Which_Side_Changed();
if(wich_side_chged==0x01)
dir = 2;
if(wich_side_chged==0x10)
dir=1;
pb_role = PB_Protocol_Compare_Device_ID((uchar *)(pb_rec_msg1.msg_data));
Sent_PB_Package(dir, pb_role, RETURN_ID);
delay_times(t_1ms, 100);
Sent_PB_Package(dir, pb_role, RETURN_ID);
delay_times(t_1ms, 100);
Sent_PB_Package(dir, pb_role, RETURN_ID);
delay_times(t_1ms, 100);
Sent_PB_Package(dir, pb_role, RETURN_ID);
/***********IDENTIFY****************/
if(id_process == ON_ID)
{
if(pb_rec_msg1.msg_dir == DIR_BACK)
id_res = PB_ON_FRONT;
if(pb_rec_msg1.msg_dir == DIR_FRONT)
id_res = PB_ON_BACK;
//if(pb_rec_msg1.msg_dir == 0)
//id_res = PB_ON_BACK;
id_process = FINISH_ID;
}
/**********CHANNEL*****************/
if(channel_sta == WAIT_REPLY|| channel_sta == RECEIVE)
{
channel_sta = IDLE;
}
delay_times(t_1ms, 100);
}break;
case RETURN_ID: {
pb_role = PB_Protocol_Compare_Device_ID((uchar *)(pb_rec_msg1.msg_data));
/***********IDENTIFY****************/
if(id_process == ON_ID)
{
if(pb_rec_msg1.msg_dir == DIR_BACK)
id_res = PB_ON_FRONT;
if(pb_rec_msg1.msg_dir == DIR_FRONT)
id_res = PB_ON_BACK;
id_process = FINISH_ID;
}
/**********CHANNEL*****************/
if(channel_sta == WAIT_REPLY || channel_sta == RECEIVE)
{
channel_sta = IDLE;
}
}break;
case ASK_FOR_INFO: {
comm_pro = REC_ASK;
}break;
case RETURN_INFO: {
comm_pro = REC_ANS;
if(channel_sta == WAIT_REPLY)
{
channel_sta = IDLE;
}
}break;
}
}
}
//------------------------------------------- SENTING PROCESS -------------------------------------------//
uchar wait_reply_timeout_cnt=0;
void Sent_PB_Package(uchar dir0, uchar role0, uchar type0)
{
uchar dir;
if((channel_sta==RECEIVE) || (channel_sta==IDLE) || (channel_sta == WAIT_REPLY) )
{
Form_PB_Package(dir0, role0, type0);
Message_To_Buffer(pb_sent_msg1);
Tx_Send_Data();
}
if(channel_sta==IDLE)
channel_sta = SENT ;
if(channel_sta == SENT )
channel_sta = WAIT_REPLY;
// if(start_comm_info_flag==1 )
// channel_sta=IDLE;
if(wich_side_chged==0x01)
dir = 2;
if(wich_side_chged==0x10)
dir=1;
while(channel_sta == WAIT_REPLY)
{
Receive_PB_Package();
if( flag2_50ms == 1)
{
flag2_50ms=0;
wait_reply_cnt ++;
if(wait_reply_cnt>1) // 100ms
{
wait_reply_timeout_cnt++;
wait_reply_cnt = 0;
Tx_Send_Data();
}
}
if(wait_reply_timeout_cnt>2)
{
wait_reply_timeout_cnt=0;
return;
}
}
}
void Form_PB_Package(uchar dir0, uchar role0, uchar type0) // form device info into pb message type
{
pb_sent_msg1.msg_dir = dir0;
pb_sent_msg1.msg_role = role0;
pb_sent_msg1.msg_type = type0;
Payload_Unit_To_Package(type0);
}
void Payload_Unit_To_Package (uchar t)
{
uchar i ;
uint bat_qua;
if(t == 0) // show id
{
Get_Device_ID();
for(i = 0; i<4; i++)
pb_sent_msg1.msg_data[i] = dev0_id[i];
}
if(t == 1) // return id
{
Get_Device_ID();
for(i = 0; i<4; i++)
pb_sent_msg1.msg_data[i] = dev0_id[i];
}
if(t == 2) // ask for info
{
pb_sent_msg1.msg_data[0] = DEV_ATT_CUR;
pb_sent_msg1.msg_data[1] = 0;
bat_qua = Get_Battery_Quantity();
pb_sent_msg1.msg_data[2] = (uchar)(bat_qua>>8);
pb_sent_msg1.msg_data[3] = (uchar)bat_qua;
}
if(t == 3) // return info
{
pb_sent_msg1.msg_data[0] = DEV_ATT_CUR;
pb_sent_msg1.msg_data[1] = 0;
bat_qua = Get_Battery_Quantity();
pb_sent_msg1.msg_data[2] = (uchar)(bat_qua>>8);
pb_sent_msg1.msg_data[3] = (uchar)bat_qua;
}
}
//------------------------------------------- CHANNEL STATUS PROCESS -------------------------------------------//
uchar wait_reply_count = 0;
uchar time_out_flag = 0;
void PB_If_Channel_Status_Time_Out(void)
{
if(channel_sta !=WAIT_REPLY )
return;
if(flag1_50ms==1)
{
flag1_50ms = 0;
wait_reply_count++;
if(wait_reply_count>50)
{
wait_reply_count++;
channel_sta = TIME_OUT;
time_out_flag = 1;
}
}
if(time_out_flag==1)
{
time_out_flag=0;
if(id_process == ON_ID)
{
if(pb_sent_msg1.msg_dir == DIR_FRONT)
id_res = CASE_ON_FRONT;
if(pb_sent_msg1.msg_dir == DIR_BACK)
id_res = CHA_TAB_ON_BACK;
id_process = FINISH_ID;
}
channel_sta = IDLE;
}
}