int get_array static uint8_t buf ARRAY_SIZE uint8_t ibuf ARRAY_SIZE st

 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
int get_array()
{
static uint8_t buf[ARRAY_SIZE];
uint8_t ibuf[ARRAY_SIZE] = {0};
static int bytes_read = 0, got_hdr = 0;
int i, j = 0, num_bytes, bytes2read = ARRAY_SIZE-bytes_read;
if((num_bytes = read (fdrs232, ibuf, bytes2read)) <= 0) {
return errno;
}
for(i = 0; i < num_bytes; i++) {
if(ibuf[i] == ARRAY_HEADER) {
if(got_hdr) {
++cnt_array_err;
bytes_read = 0;
}
else
got_hdr = 1;
--num_bytes;
}
}
memcpy(&buf[bytes_read], &ibuf[j], num_bytes);
bytes_read += num_bytes;
if(bytes_read == ARRAY_SIZE) {
for(i=1, j=0; i<ARRAY_SIZE; i++) {
if(!(buf[i] & 0x80)) {
j++;
}
}
bytes_read = 0;
got_hdr = 0;
if(!j) {
cnt_array_ok++;
memcpy(array,buf,ARRAY_SIZE);
return 1;
} else {
cnt_array_err++;
return -1;
}
}
return 0;
}