-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_string.py
448 lines (349 loc) · 9.77 KB
/
temp_string.py
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin
cb_fullmatch_temp='''
$DATA_TYPE $DATA_NAME; // [${MIN_VAL}, ${MAX_VAL}] ${INVALID} ${FACTOR}${OFFSET}${UNIT}${MEANING}${DESCRIPTION}'''
cb_fullmatch_reserved='''
$DATA_TYPE reserved_B${START_BYTE}${END_BYTE};'''
cb_partmatch_reserved='''
$DATA_TYPE reserved_b${START_BIT}${END_BIT} : $DATA_LENGTH;'''
cb_partmatch_reserved_none='''
$DATA_TYPE : $DATA_LENGTH;'''
cb_partmatch_temp='''
$DATA_TYPE $DATA_NAME : $DATA_LENGTH; // [${MIN_VAL},${MAX_VAL}] ${INVALID} ${FACTOR}${OFFSET}${UNIT}${MEANING}${DESCRIPTION}'''
cb_partmatch_head='''
union{
struct{$DATA_BODY
} bits;
$FMT_TYPE vals;
} B${START_BYTE}${END_BYTE};'''
cb_frame_tmp = '''
// id:${CANID} $PERIOD $COMMENT
struct can${RDWR}_${NAME} {$FRAME_BODY
}__attribute__((packed));
'''
fl_head_string = '''// Copyright (C) 2019 - Minieye INC.
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/timerfd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <errno.h>
#include <linux/can/error.h>
#include "gflags/gflags.h"
#include "unified_process_rsp.h"
#define CAN_DEVICE "${CAN_CHANNEL}" // 根据实际产品定义CAN通道
#define PERIOD_SLEEP_MS ${PERIOD_TIME} // 每次循环睡眠50ms
#define ENABLE_SEPARATE_SEND 0 // 是否独立一个线程发送CAN报文
$MACRO_CANSEND
#define min(x, y) ((x) > (y) ? (y) : (x)) // 取较小值
#define max(x, y) ((x) > (y) ? (x) : (y)) // 取较大值
#define mid(val, x, y) (max(min((val), (y)), (x))) // 取中间值
// declare gflag below
DECLARE_double(ldw_speed_thresh);
'''
can_send_msg_temp='''
{.id = $ID, .type = PERIOD, .delay = $DELAY, .msg = (uint8_t*)&$MSG, .len = $LEN},'''
can_send_temp='''
/***************************** CAN发送数据相关变量 **************************/
enum CAN_PACKET_TYPE{EVENT = 0, PERIOD, EVENT_PERIOD};
struct can_transmint_type_t{
uint32_t id; // CAN id
uint8_t type; // 0=事件型, 1=周期型, 2=事件周期型
uint32_t delay; // 事件型or周期型每帧之间的延时
uint8_t *msg; // CAN数据缓存
uint8_t len; // 缓存区大小
uint32_t delay_ev; // 事件周期型中,事件触发后的延时
uint8_t event_nums; // 事件周期型中,事件触发后,以事件型延时发送的报文次数
};
// CAN 发送数据相关信息
struct can_transmint_type_t can_transmit_id_list[NUM_OF_SEND_ID]={
/* 如果为周期型,只需要填充 */
/* id 帧类型 周期延时 事件延时 事件CAN帧数 发送BUFF */$FRAME_LIST
};
'''
func_lds_temp = '''
void do_lane_detect_rsp(struct LaneDetectRsp_all *lds) {
static long long int time_base = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
LOG_LDS("%lld|frame_id:%u\\n",
(tv.tv_sec*1000 + tv.tv_usec/1000) - time_base,
lds->rsp.frame_id);
time_base = tv.tv_sec*1000 + tv.tv_usec/1000;
char timestr[30];
struct tm tm = *localtime(&tv.tv_sec);
strftime(timestr, 29, "%Y-%m-%d %H:%M:%S", &tm);
LOG_LDS("(%s.%lu):\\n", timestr, tv.tv_usec);
if (lds == NULL)
return;
// TODO
for (size_t i = 0; i < lds->rsp.lanelines.size(); i++)
{
// TODO
}
// TODO
}
'''
func_vds_temp = '''
void do_vehicle_detect_rsp(struct VehicleDetectRsp_all *vds) {
static long long int time_base = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
LOG_VDS("%lld|frame_id:%u\\n",
(tv.tv_sec*1000 + tv.tv_usec/1000) - time_base,
vds->frame_id);
time_base = tv.tv_sec*1000 + tv.tv_usec/1000;
if (vds == NULL)
{
LOG_VDS("input ptr invalid\\n");
return;
}
// TODO
for (size_t idx = 0; idx < vds->rsp.dets.size(); idx++)
{
// TODO
}
// TODO
}
'''
func_pds_temp = '''
void do_people_detect_rsp(struct PeopleDetectRsp_all *pds) {
static long long int time_base = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
LOG_PDS("%lld|frame_id:%lld\\n",
(tv.tv_sec*1000 + tv.tv_usec/1000) - time_base,
rsp->processed_frame_id);
time_base = tv.tv_sec*1000 + tv.tv_usec/1000;
struct tm tm = *localtime(&tv.tv_sec);
char timestr[30];
strftime(timestr, 29, "%Y-%m-%d %H:%M:%S", &tm);
LOG_PDS("(%s.%03ld)\\n", timestr, tv.tv_usec/1000);
if (pds == NULL)
{
LOG_PDS("input ptr invalid\\n");
return;
}
// TODO
for (size_t idx = 0; idx < pds->rsp.pedestrians.size(); idx++)
{
// TODO
}
// TODO
}
'''
func_tds_temp = '''
void do_tsr_detect_rsp(struct TsrDetectRsp_all *tds) {
static long long int time_base = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
LOG_TDS("%lld|frame_id:%lld\\n",
(tv.tv_sec*1000 + tv.tv_usec/1000) - time_base, frame_id);
time_base = tv.tv_sec*1000 + tv.tv_usec/1000;
struct tm tm = *localtime(&tv.tv_sec);
char timestr[30];
strftime(timestr, 29, "%Y-%m-%d %H:%M:%S", &tm);
LOG_TDS("(%s.%03ld)\\n", timestr, tv.tv_usec / 1000);
if (tds == NULL)
{
LOG_TDS("input ptr invalid\\n");
return;
}
// TODO
for (size_t idx = 0; idx < tds->rsp.tsr_dets.size(); idx++)
{
// TODO
}
// TODO
}
'''
func_timer_temp = '''
int tmfd_sleep = -1;
static void timerfd_handler(int fd) {
struct timeval tv1, tv2;
uint64_t exp = 0;
int ret = read(fd, &exp, sizeof(uint64_t));
return;
}
static void sleep_period_ms(int tmfd) { timerfd_handler(tmfd); }
static int timerfd_init(int delay) {
int tmfd;
int ret;
struct itimerspec new_value;
if (delay <= 0)
{
return -1;
}
tmfd = timerfd_create(CLOCK_REALTIME, 0);
if (tmfd < 0) {
return -1;
}
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) == -1) {
assert(0);
}
new_value.it_value.tv_sec = now.tv_sec + 1;
new_value.it_value.tv_nsec = now.tv_nsec;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = delay * 1000 * 1000;
ret = timerfd_settime(tmfd, TFD_TIMER_ABSTIME, &new_value, NULL);
if (ret < 0) {
close(tmfd);
return -1;
}
return tmfd;
}
'''
func_can_interface_temp = '''
/************************************** CAN发送接口配置及处理 **************************************/
static void can_transmit(uint32_t id, uint8_t *data, uint8_t len)
{
if (data == NULL || len > 8){
LOG_UPR("%s, intput error pData=%lld, len=%d\\n", __func__, (uint64_t)data, len);
return;
}
struct can_frame fr;
fr.can_id = id;
if (fr.can_id > 0x7ff)
{
fr.can_id |= CAN_EFF_FLAG;
}
fr.can_dlc = len;
memcpy(fr.data, data, fr.can_dlc);
if (sizeof(struct can_frame) != write(fd_can_send, &fr, sizeof(struct can_frame))){
LOG_UPR("can send fail\\n");
} else{
LOG_UPR("%x [%d] %02X %02X %02X %02X %02X %02X %02X %02X\\n", fr.can_id, fr.can_dlc,
fr.data[0], fr.data[1], fr.data[2], fr.data[3], fr.data[4], fr.data[5], fr.data[6], fr.data[7]);
}
}
'''
func_time_init_temp='''
/*************************** 定时器 ***************************/
int tmfd_50ms = -1;
void timerfd_handler(int fd) {
struct timeval tv1, tv2;
uint64_t exp = 0;
int ret = read(fd, &exp, sizeof(uint64_t));
return;
}
void sleep_period_ms(int tmfd) { timerfd_handler(tmfd); }
int timerfd_init(int delay) {
int tmfd;
int ret;
struct itimerspec new_value;
if (delay <= 0)
{
return -1;
}
tmfd = timerfd_create(CLOCK_REALTIME, 0);
if (tmfd < 0) {
return -1;
}
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) == -1) {
assert(0);
}
new_value.it_value.tv_sec = now.tv_sec + 1;
new_value.it_value.tv_nsec = now.tv_nsec;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = delay * 1000 * 1000;
ret = timerfd_settime(tmfd, TFD_TIMER_ABSTIME, &new_value, NULL);
if (ret < 0) {
close(tmfd);
return -1;
}
return tmfd;
}
'''
func_dev_init_loopback_temp = '''
// 本地回环功能是默认开启的,此处关闭CAN发送回环功能
int loopback = 0;
if (0 > setsockopt(can_fd, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback)))
{
LOG_UPR("setsockopt, disable loopback function failed\\n");
close(can_fd);
return -1;
}
'''
func_dev_init_enablereceive_temp='''
// 创建CAN接收线程
pthread_t pth_can_rcv;
int err = pthread_create(&pth_can_rcv, NULL, can_rcv_thread, NULL);
if (err != 0)
{
LOG_UPR("pthread_create can_rcv_thread failed, error = %d\\n", err);
return -1;
}
'''
func_dev_init_disablereceive_temp='''
// 屏蔽CAN接收功能,不接收任何CAN报文
if (0 > setsockopt(can_fd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) {
LOG_UPR("setsockopt");
close(can_fd);
return -1;
}
'''
func_dev_init_send_split_temp='''
// 创建CAN发送线程
pthread_t pth_can_send;
if (0 > pthread_create(&pth_can_send, NULL, can_send_thread, NULL)){
close(can_fd);
LOG_UPR("create can_rcv_thread failed\\n");
return -1;
}
'''
func_dev_init_temp = '''
/* 初始化CAN设备 */
int can_fd = -1;
static int can_dev_init(const char *dev)
{
struct ifreq ifr;
struct sockaddr_can addr;
can_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (can_fd < 0){
LOG_UPR("socket failed\\n");
return -1;
}
strcpy(ifr.ifr_name, dev);
if (0 > ioctl(can_fd, SIOCGIFINDEX, &ifr)){
LOG_UPR("ioctl, err=%s\\n", strerror(errno));
return -1;
}
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (0 > bind(can_fd, (struct sockaddr *)&addr, sizeof(addr))){
LOG_UPR("bind failed\\n");
close(can_fd);
return -1;
}
// 初始化CAN发送定时器(精确延时)
tmfd_50ms = timerfd_init(PERIOD_SLEEP_MS);
if (tmfd_50ms < 0)
{
LOG_UPR("init timefd faile\\n");
close(can_fd);
return -1;
}
${CAN_LOOPBACK}${CAN_RCV_INIT}${CAN_SEND_SPLIT}
return 0;
}
'''
func_can_rcv_temp='''
void *can_rcv_thread(void *arg)
{
struct can_frame fr;
pthread_detach(pthread_self());
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
while (1)
{
if (0 < read(can_fd, &fr, sizeof(fr)))
{
// TODO
}
}
}
'''