-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpharse.c
381 lines (368 loc) · 12 KB
/
pharse.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/******************************************************************
parse.c
创建者:黄洪磊
时间:2018/4/23
功能:将raw信息进行解析成phy信息
注意:真正使用的是parse3
*******************************************************************/
#include "pharse.h"
extern int bit_mask(int nbits);
/*for bit <=8*/
char bit_get(const int start,const int size,const char target)
{
int s = start;
if(s > 8)
{
s = s%8;
}
char tmp = 0;
int z = size;
char t = target;
while(z>0)
{
tmp |= 1<<s;
s--;
z--;
}
t = (t&tmp) >> (s - z +1);
return t;
}
/*for bit >8*/ /*unsigned char? char?*/
long nbyte_get(int start,int size, unsigned char *target)
{
int nbytes = size/8 +1;
//double ret = 0;
char tmp[8];
long ret = 0;
if(start%8 != 7)
{
tmp[0] = bit_get(start,start%8+1,target[start/8]);
int i = 1;
while(nbytes>0)
{
tmp[i] = target[start/8 + i];
nbytes--;
i++;
}
int t = 0;
for(i = size/8 ;i>=0;i--)
{
ret = ret |(tmp[i] << (8*t));
//printf("i is %d ret is %ld\n",i,ret);
t++;
}
//printf("ret is :%ld\n",ret);
}
else if(start%8 == 7)
{
int i = 0;
while(nbytes > 1)
{
tmp[i] = target[start/8 + i];
i++;
nbytes --;
}
tmp[i] = bit_get(start,size%8,target[start/8+i]);
int t = 0;
for(i = size/8 ;i>=0;i--)
{
if(i == size/8)
{
ret = ret |tmp[size/8];
}
else
{
ret = ret |(tmp[i] << (8*t + (size%8)));
t++;
}
//printf("i is %d ret is %ld\n",i,ret);
}
//printf("ret is :%ld\n",ret);
}
return ret;
}
//extern BO_Unit_t * BO_List[50];
void pharse(struct can_frame *frame,BO_Unit_t **BO_List)
{
int i = 0;
while(BO_List[i] != NULL)
{
if(frame->can_id == BO_List[i]->message_id) //detect the can id
{
/*ID Mached*/
int t = 0;
while(BO_List[i]->SG_List[t] != NULL) /*SG_LOOP*/
{
/*motorola Mode*/
if(0 == BO_List[i]->SG_List[t]->Bit_order)
{
int size = BO_List[i]->SG_List[t]->signal_size;
int start = BO_List[i]->SG_List[t]->start_bit;
double tmp = 0;
/*Pharse the motorla mode msg!*/
if(size <= 8)
{
tmp = (double)bit_get(start,size,frame->data[start/8]);
}
else if(size > 8)
{
tmp = (double)nbyte_get(start,size, frame->data);
}
/*value type_ signed or unsigned*/
if(BO_List[i]->SG_List[t]->val_type)
{
if(8 == size)
{
tmp = (signed char)tmp;
tmp = (double)tmp;
}
else if(16 == size)
{
tmp = (short)tmp;
//tmp = (double)tmp;
}
else if(32 == size)
{
tmp = (float)tmp;
//tmp = (double)tmp;
}
}
/*Factor and Offset*/
tmp = tmp * BO_List[i]->SG_List[t]->facator + BO_List[i]->SG_List[t]->offset;
/*Max and Min*/
if(tmp > BO_List[i]->SG_List[t]->maximum)
{
tmp = BO_List[i]->SG_List[t]->maximum;
}
if(tmp < BO_List[i]->SG_List[t]->minimum)
{
tmp = BO_List[i]->SG_List[t]->minimum;
}
/*assign value*/
BO_List[i]->SG_List[t]->value = tmp;
}
t++;
}
break;
}
i++;
}
//printf("Number of BO:%d\n",i);
}
void pharse2(struct can_frame *frame,BO_Unit_t **BO_List)
{
int i = 0;
while(BO_List[i] != NULL)
{
//printf("bolist loop \n ");
if(frame->can_id == BO_List[i]->message_id) //detect the can id
{
/*ID Mached*/
#ifdef SHOW
system("clear");
printf("BO_ %d %s\n",BO_List[i]->message_id,BO_List[i]->message_name);
#endif // SHOW
//printf("\r");
int t = 0;
while(BO_List[i]->SG_List[t] != NULL) /*SG_LOOP*/
{
#ifdef SHOW
printf("%s ",BO_List[i]->SG_List[t]->signal_name);
#endif // SHOW
/*motorola Mode*/
if(0 == BO_List[i]->SG_List[t]->Bit_order)
{
int size = BO_List[i]->SG_List[t]->signal_size;
int start = BO_List[i]->SG_List[t]->start_bit;
double tmp = 0;
/*Pharse the motorla mode msg!*/
//bite < 8
if(size <= 8)
{
tmp = ( (frame->data[start/8] ) >> (start%8 - size +1)) & bit_mask(size);
// printf("%lf\r",tmp);
}
else if(size > 8 && size <= 16)
{
//printf("==\r");
if(start / 8 == 7)
{
tmp = ((short)(frame->data[start/8 ]<<8 | frame->data[start/8 + 1]) >> (size % 8))& bit_mask(size);
}
tmp = (short)(frame->data[start/8 ]<<8 | frame->data[start/8 + 1]) & bit_mask(size);
// printf(">>%lf\r",tmp);
}
/*value type_ signed or unsigned*/
else if(size >16)
{
tmp = ntohl(*(long *)(frame->data+(start/8))) & bit_mask(size);
}
if(BO_List[i]->SG_List[t]->val_type)
{
if(8 == size)
{
tmp = (signed char)tmp;
tmp = (double)tmp;
}
else if(16 == size)
{
tmp = (short)tmp;
//tmp = (double)tmp;
}
else if(32 == size)
{
tmp = (float)tmp;
//tmp = (double)tmp;
}
}
/*Factor and Offset*/
tmp = tmp * BO_List[i]->SG_List[t]->facator + BO_List[i]->SG_List[t]->offset;
/*Max and Min*/
if(tmp > BO_List[i]->SG_List[t]->maximum)
{
tmp = BO_List[i]->SG_List[t]->maximum;
}
if(tmp < BO_List[i]->SG_List[t]->minimum)
{
tmp = BO_List[i]->SG_List[t]->minimum;
}
/*assign value*/
BO_List[i]->SG_List[t]->value = tmp;
/*Print nuit*/
#ifdef SHOW
printf(">>%lf ",tmp);
printf(" %s ", BO_List[i]->SG_List[t]->unit);
#endif // SHOW
}
#ifdef SHOW
printf("\n");
#endif // SHOW
t++;
}
break;
}
i++;
}
//printf("Number of BO:%d\n",i);
}
//if the signal cross bytes
int CrossByte(int start, int size)
{
int tmp = (start % 8)+1 -size;
if( tmp < 0 )
{
if(tmp < -16)
{
return 4;
}
else if(tmp < -8)
{
return 3;
}
else
return 2;
}
return 0;
}
int CrossBytesLocation(int start, int size,int crossbyte)
{
int offset = 0;
if(crossbyte == 2)
offset = 8;
else if(crossbyte == 3)
offset = 16;
else if(crossbyte == 4)
offset = 24;
int ret = 1 + offset -(size - (start % 8));
return ret ;
}
/*
raw信息解析函数,将frame中的raw信息解析为phy信息
放入BO_List中的value中
struct can_frame *frame:要解析的frame
BO_Unit_t **BO_List:BO_List结构体
*/
void parse3(struct can_frame *frame,BO_Unit_t **BO_List)
{
int i = 0;
while(BO_List[i] != NULL)
{
//printf("bolist loop \n ");
if(frame->can_id == BO_List[i]->message_id) //detect the can id
{
/*ID Mached*/
int t = 0;
while(BO_List[i]->SG_List[t] != NULL) /*SG_LOOP*/
{
if(0 == BO_List[i]->SG_List[t]->Bit_order)
{
int size = BO_List[i]->SG_List[t]->signal_size;
int start = BO_List[i]->SG_List[t]->start_bit;
double tmp = 0;
/*Pharse the motorla mode msg!*/
int crossBytes = CrossByte(start, size);
if(crossBytes)/*cross bytes*/
{
int offset = CrossBytesLocation(start, size, crossBytes);
if(2 == crossBytes)
{
tmp = ((((frame->data[start/8])<<8) | (frame->data[start/8+1])) >> (offset)) & bit_mask(size);
}
if(3 == crossBytes)
{
tmp =((((frame->data[start/8])<<16) | (frame->data[start/8+1]<<8)|frame->data[start/8+2]) >> (offset)) & bit_mask(size);
}
}
else
{
tmp = ( (frame->data[start/8] ) >> (start%8 - size +1)) & bit_mask(size);
}
/*value type_ signed or unsigned*/
if(BO_List[i]->SG_List[t]->val_type)
{
if(8 == size)
{
tmp = (signed char)tmp;
tmp = (double)tmp;
}
else if(16 == size)
{
tmp = (short)tmp;
//tmp = (double)tmp;
}
else if(32 == size)
{
tmp = (float)tmp;
//tmp = (double)tmp;
}
}
/*Factor and Offset*/
tmp = tmp * BO_List[i]->SG_List[t]->facator + BO_List[i]->SG_List[t]->offset;
/*Max and Min*/
if(tmp > BO_List[i]->SG_List[t]->maximum)
{
tmp = BO_List[i]->SG_List[t]->maximum;
}
if(tmp < BO_List[i]->SG_List[t]->minimum)
{
tmp = BO_List[i]->SG_List[t]->minimum;
}
/*assign value*/
BO_List[i]->SG_List[t]->value = tmp;
/*Print nuit*/
#ifdef SHOW
printf(">>%lf ",tmp);
printf(" %s ", BO_List[i]->SG_List[t]->unit);
#endif // SHOW
}
#ifdef SHOW
printf("\n");
#endif // SHOW
t++;
}
break;
}
i++;
}
//printf("Number of BO:%d\n",i);
}