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
|
/*##############################################################################
# Copyright (c) 2017 ZTE Coreporation and others.
# hu.zhijiang@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################*/
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "misc.h"
struct buffer_ctl buffctl;
struct packet_ctl *packetctl[PACKETS_PER_BUFFER];
struct packet_ctl empty;
void buffer_init()
{
int i;
for (i = 0; i < PACKETS_PER_BUFFER; i++) {
packetctl[i] = (struct packet_ctl *)wrapper_malloc(PACKET_SIZE);
memset(packetctl[i], 0, PACKET_SIZE);
packetctl[i]->data_size = 0;
packetctl[i]->seq = 0;
}
buffctl.buffer_id = 0;
buffctl.packet_id_base = 0;
buffctl.pkt_count = 0;
empty.data_size = 0;
}
void packetctl_precheck()
{
int i;
for (i = 0; i < PACKETS_PER_BUFFER; i++) {
if (packetctl[i]->data_size != 0) {
crit("Error precheck packet slot %d,%d", i, packetctl[i]->data_size);
}
}
}
// Calculate packet checksum
uint32_t packet_csum(uint8_t *data, int len)
{
uint32_t *item, sum = 0;
int i = 0;
while (len % 4) {
data[len] = 0; len++;
}
while (i < len) {
item = (uint32_t *)((char *)data + i);
sum += *item;
i += 4;
}
return sum;
}
// Fill buffers from file descriptor
long buffer_fill(int fd)
{
int s = 0;
int r = PACKET_PAYLOAD_SIZE;
buffctl.buffer_id++;
buffctl.packet_id_base += buffctl.pkt_count;
buffctl.buffer_size = 0;
while (r > 0 && s < PACKETS_PER_BUFFER) {
if ((r = read(fd, packetctl[s]->data, PACKET_PAYLOAD_SIZE)) < 0) {
crit("Error reading data from stdin");
}
// r == 0 means EOF
if (r > 0) {
buffctl.buffer_size += r;
packetctl[s]->data_size = r;
packetctl[s]->seq = buffctl.packet_id_base + s;
packetctl[s]->crc = packet_csum(packetctl[s]->data, r);
s++;
}
}
log(6, "input %d bytes of data in %d packets", buffctl.buffer_size, s);
buffctl.pkt_count = s;
return s;
}
long buffer_flush(int fd)
{
int s = 0;
while (buffctl.pkt_count--) {
write(fd, packetctl[s]->data, packetctl[s]->data_size);
buffctl.buffer_size -= packetctl[s]->data_size;
packetctl[s]->data_size = 0;
packetctl[s]->seq = 0;
packetctl[s]->crc = 0;
s++;
}
return s;
}
/* Caller should use new_pkt as packet buffer again if this returns NULL */
struct packet_ctl *packet_put(struct packet_ctl *new_pkt)
{
struct packet_ctl *freed_pkt;
uint32_t i;
if (new_pkt->seq < buffctl.packet_id_base ||
new_pkt->seq - buffctl.packet_id_base >= buffctl.pkt_count) {
return NULL;
}
if (new_pkt->data_size == 0) {
return NULL;
}
i = packet_csum(new_pkt->data, new_pkt->data_size);
if (new_pkt->crc != i) {
return NULL;
}
i = new_pkt->seq - buffctl.packet_id_base;
freed_pkt = packetctl[i];
packetctl[i] = new_pkt;
return freed_pkt;
}
struct packet_ctl *packet_get(uint32_t seq)
{
empty.seq = seq;
if (seq < buffctl.packet_id_base ||
seq - buffctl.packet_id_base >= buffctl.pkt_count) {
return ∅
}
if (packetctl[seq - buffctl.packet_id_base]->data_size == 0) {
return ∅
}
return packetctl[seq - buffctl.packet_id_base];
}
|