1 |
/* |
2 |
* include needed system headers |
3 |
11/9/2005 modified by david fedele to not create file_temp |
4 |
22/11/2005 modified by david fedele to implement crc control */ |
5 |
|
6 |
#include <stdio.h> /* include standard i/o library */ |
7 |
#include <stdlib.h> /* include standard library */ |
8 |
#include <string.h> /* include string library */ |
9 |
#include <ctype.h> /* include conversion library */ |
10 |
#include <unistd.h> /* include unix standard library */ |
11 |
#include <sys/types.h> /* */ |
12 |
#include <sys/stat.h> /* */ |
13 |
#include <fcntl.h> /* */ |
14 |
#include <errno.h> /* error simbol definitions */ |
15 |
#include <time.h> /* system time definitions */ |
16 |
#include <math.h> /* math library */ |
17 |
|
18 |
#define MAXBUFFLEN 0x172c8 //0x12a50 |
19 |
#define HEADERLEN 8 |
20 |
/* find cpu header */ |
21 |
/* starting path --> 0xFAFEDE */ |
22 |
|
23 |
/***************************************************** */ |
24 |
/* 22/11/2005 modified by david fedele */ |
25 |
unsigned char crc16(unsigned char old,unsigned char data); |
26 |
/* ***************************************************** */ |
27 |
void findcpuheader_(int *error, int *fd, int *pkt, unsigned long *pkt_counter, |
28 |
unsigned long *obtt, char* buffer,int *length) |
29 |
|
30 |
{ |
31 |
|
32 |
int i; |
33 |
|
34 |
int nread,nwrite; |
35 |
unsigned short cpu_header[HEADERLEN]; |
36 |
unsigned short temp; |
37 |
|
38 |
int header_flag; |
39 |
unsigned long int start; |
40 |
unsigned char pkt_type; |
41 |
unsigned long int counter,counter_last=0; |
42 |
unsigned long int obt; |
43 |
unsigned long int pkt_len; |
44 |
char cpucrc; |
45 |
|
46 |
char buff[MAXBUFFLEN]; |
47 |
|
48 |
int fd_out; |
49 |
char *ptemp; |
50 |
|
51 |
/***************************************************** */ |
52 |
/* 22/11/2005 modified by david fedele */ |
53 |
char icc; |
54 |
/****************************************************** */ |
55 |
|
56 |
do{ |
57 |
|
58 |
/* search for 0xFAFEDE*/ |
59 |
do { |
60 |
*error=0; |
61 |
nread=read(*fd, cpu_header, 2*HEADERLEN); |
62 |
// printf("%d \n",2*HEADERLEN); |
63 |
if(nread == 0) { |
64 |
// printf("findcpuheader (1): Unexpected end of file %d at %d \n",*fd,nread); |
65 |
*error=1; |
66 |
return ; |
67 |
} |
68 |
if(nread != 2*HEADERLEN) { |
69 |
// printf("findcpuheader (1): Error on reading: bytes read -> nread = %d instead of %d\n",nread,2*HEADERLEN); |
70 |
*error=-1; |
71 |
return ; |
72 |
} |
73 |
/*** corrects byte ***/ //endianess... |
74 |
for(i=0;i<HEADERLEN;i++){ |
75 |
temp = cpu_header[i]; |
76 |
cpu_header[i] = ( (temp&0x00ff) << 8 ) | ( (temp&0xff00) >> 8 ); |
77 |
}; |
78 |
|
79 |
|
80 |
header_flag = 1; |
81 |
|
82 |
start = (cpu_header[0]<<8)|((cpu_header[1]&0xff00)>>8); |
83 |
if(start != 0xfafede)header_flag = 0; |
84 |
pkt_type = (unsigned char)(cpu_header[1]&0x00ff); |
85 |
if((unsigned char)((cpu_header[2]&0xff00)>>8) != pkt_type)header_flag = 0; |
86 |
counter = ((cpu_header[2]&0x00ff)<<16) |
87 |
|cpu_header[3]; |
88 |
obt = ((cpu_header[4]&0xffff)<<16) | (cpu_header[5]&0xffff); |
89 |
pkt_len = (cpu_header[6]<<8) | ((cpu_header[7]&0xff00)>>8); |
90 |
cpucrc = cpu_header[7]&0x00ff; |
91 |
|
92 |
// ************************************ |
93 |
lseek(*fd, -(2*HEADERLEN-1), SEEK_CUR); |
94 |
// SEEK_CUR ---> from current position |
95 |
// ************************************ |
96 |
} |
97 |
while (!header_flag); |
98 |
|
99 |
/***************************************************** */ |
100 |
/* 22/11/2005 modified by david fedele */ |
101 |
icc=0; |
102 |
for (i =0; i < 8 ; i++){ |
103 |
icc=crc16(icc,cpu_header[i]); |
104 |
/* temp=(cpu_header[i]&0x00ff); */ |
105 |
/* icc = crc8_8(icc, temp); */ |
106 |
/* temp=(cpu_header[i]&0xff00); */ |
107 |
/* icc = crc8_8(icc, temp); */ |
108 |
} |
109 |
/* if(icc==cpucrc) */ |
110 |
/* printf("cpucrc=%hx\ticc=%hx\n\n",cpucrc,icc); */ |
111 |
/****************************************************** */ |
112 |
|
113 |
|
114 |
counter_last = counter; |
115 |
/* print header content */ |
116 |
/* if( (pkt_type == 0x20) */ |
117 |
/* || (pkt_type == 0x07) */ |
118 |
/* || (pkt_type == 0x21) */ |
119 |
/* || (pkt_type == 0x12) */ |
120 |
/* || (pkt_type == 0x83) */ |
121 |
/* || (pkt_type == 0x13)){ */ |
122 |
/* printf("\n********************************\n"); */ |
123 |
/* printf("* >>>> CPU HEADER <<<< *\n"); */ |
124 |
/* printf("********************************\n"); */ |
125 |
/* printf(" Packet type \t 0x%x \n",pkt_type); */ |
126 |
/* printf(" Counter \t %u \n",counter); */ |
127 |
/* printf(" On-board time \t 0x%x \n",obt); */ |
128 |
/* printf(" Packet length (byte)\t %d \n",pkt_len); */ |
129 |
/* printf("********************************\n"); */ |
130 |
/* }; */ |
131 |
|
132 |
// ************************************** |
133 |
lseek(*fd, (2*HEADERLEN-1), SEEK_CUR); |
134 |
// SEEK_CUR ---> from current position |
135 |
// ************************************** |
136 |
|
137 |
}while((pkt_type != 0x12)&& |
138 |
(pkt_type != 0x13)&& |
139 |
(pkt_type != 0x10)&& |
140 |
(pkt_type != 0x30)&& |
141 |
(pkt_type != 0x20)&& // RUNHEADER PACKET |
142 |
(pkt_type != 0x21)&& // RUNHEADER PACKET |
143 |
(pkt_type != 0x83)); |
144 |
|
145 |
/* come here when a tracker packet is fownd: */ |
146 |
/* 0x12 --> calibration board 0 */ |
147 |
/* 0x13 --> calibration board 1 */ |
148 |
/* 0x10 --> event */ |
149 |
/* 0x30 --> full event for calib special */ |
150 |
/* 0x20 --> runheader packet */ |
151 |
/* 0x83 --> tracker ALARM (control register) */ |
152 |
|
153 |
nread = read(*fd, buff, pkt_len); |
154 |
if(nread == 0) { |
155 |
printf("findcpuheader: Unexpected end of file %d at %d \n",*fd,nread); |
156 |
*error=1; |
157 |
return ; |
158 |
} |
159 |
if(nread != pkt_len) { |
160 |
printf("findcpuheader: Error on reading: bytes read -> nread = %i instead of %i \n",(int)nread,(int)pkt_len); |
161 |
*error=-1; |
162 |
return ; |
163 |
} |
164 |
// for(i=0;i<12;i++)printf("- %hhx \n",buff[i]); |
165 |
|
166 |
/* //**************************************************** */ |
167 |
/* //ccccc 11/9/2005 modified by david fedele */ |
168 |
/* fd_out = creat("bin-aux/packet.dat",0666); */ |
169 |
/* nwrite = write(fd_out, buff, nread); */ |
170 |
/* if(nwrite != nread) { */ |
171 |
/* printf("findcpuheader: Error on writing: bytes written -> nwrite = %d \n",nwrite); */ |
172 |
/* // *error=-1; */ |
173 |
/* return ; */ |
174 |
/* } */ |
175 |
/* close(fd_out); */ |
176 |
/* ***************************************************** */ |
177 |
|
178 |
ptemp=buffer; |
179 |
for(i =0; i< nread; i++){ |
180 |
*ptemp=buff[i]; |
181 |
ptemp++; |
182 |
}; |
183 |
*length = nread; |
184 |
/***************************************************** */ |
185 |
/* 22/11/2005 modified by david fedele */ |
186 |
/* icc=0; */ |
187 |
/* for (i =0; i < HEADERLEN; i++){ */ |
188 |
/* temp=(buff[i]&0xff00); */ |
189 |
/* icc = crc8_8(icc, temp); */ |
190 |
/* temp=(buff[i]&0x00ff); */ |
191 |
/* icc = crc8_8(icc, temp); */ |
192 |
/* } */ |
193 |
/* if(icc==cpucrc) */ |
194 |
/* printf("cpucrc=%hx\ticc=%hx\n\n",cpucrc,icc); */ |
195 |
|
196 |
/****************************************************** */ |
197 |
*pkt = (short int)pkt_type; |
198 |
*pkt_counter = counter; |
199 |
*obtt = obt; |
200 |
return; |
201 |
} |