File: tfreedv_data_channel.c

package info (click to toggle)
codec2 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 121,056 kB
  • sloc: ansic: 414,118; sh: 2,612; objc: 2,574; python: 2,105; cpp: 2,091; asm: 683; makefile: 598
file content (315 lines) | stat: -rw-r--r-- 9,549 bytes parent folder | download
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
/*---------------------------------------------------------------------------*\

  FILE........: tfreedv_data_channel
  AUTHOR......: Jeroen Vreeken
  DATE CREATED: May 3 2016

  Tests for the data channel code.
  Data channel frame behaviour is tested with test vectors.

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2016 Jeroen Vreeken

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "freedv_data_channel.h"

#include <stdio.h>
#include <string.h>

unsigned char test_header[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
unsigned char bcast_header[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };


struct testvec {
    char *testname;
    
    unsigned char *data;
    size_t data_size;
    
    size_t frame_size;
    
    unsigned char *frame_data;
    size_t frame_data_size;

    unsigned char *flags;
} testvec[] = {
    {
        "Regular packet, does not match header and no broadcast",
        (unsigned char[]){ 
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11, 0x12
        },
        0x12,
        8,
        (unsigned char[]){ 
            0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x01, 0x02,
            0x03, 0x04, 0x05, 0x06, 0x0d, 0x0e, 0x0f, 0x10,
            0x11, 0x12, 0x47, 0x6e
        },
        0x14,
        (unsigned char[]){ 0x00, 0x00, 0x04 },
    },
    {
        "Header",
        NULL,
        0,
        8,
        (unsigned char[]){ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x5a, 0x60 },
        0x08,
        (unsigned char[]){ 0x08 },
    },
    {
        "Broadcast packet",
        (unsigned char[]){ 
            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x11, 0x22,
            0x33, 0x44, 0x55, 0x66, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11
        },
        0x19,
        8,
        (unsigned char[]){ 
            0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11, 0x3c, 0xbe
        },
        0x0f,
        (unsigned char[]){ 0xc0, 0x07 },
    },
    {
        "Broadcast packet, header does not match",
        (unsigned char[]){ 
            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xaa, 0x22,
            0xbb, 0xcc, 0xdd, 0xee, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11
        },
        0x19,
        8,
        (unsigned char[]){ 
            0xaa, 0x22,
            0xbb, 0xcc, 0xdd, 0xee, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11, 0x1a, 0x68
        },
        0x15,
        (unsigned char[]){ 0x40, 0x00, 0x05 },
    },
    {
        "Header 6 bytes",
        NULL,
        0,
        6,
        (unsigned char[]){ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 },
        0x06,
        (unsigned char[]){ 0x2f },
    },
    {
        "Broadcast packet (6 byte frames)",
        (unsigned char[]){ 
            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x11, 0x22,
            0x33, 0x44, 0x55, 0x66, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11
        },
        0x19,
        6,
        (unsigned char[]){ 
            0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11, 0x3c, 0xbe
        },
        0x0f,
        (unsigned char[]){ 0xc0, 0x00, 0x03 },
    },
    {
        "Broadcast packet, header does not match (6 byte frames)",
        (unsigned char[]){ 
            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xaa, 0x22,
            0xbb, 0xcc, 0xdd, 0xee, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11
        },
        0x19,
        6,
        (unsigned char[]){ 
            0xaa, 0x22,
            0xbb, 0xcc, 0xdd, 0xee, 0x05, 0x06, 0x07, 0x08, 
            0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
            0x11, 0x1a, 0x68
        },
        0x15,
        (unsigned char[]){ 0x40, 0x00, 0x00, 0x03 },
    },
};


static int ret = 0;
static int vector = 0;
static size_t frame_data_pos = 0;
static int rx_done = 0;

void *tx_cb_arg = (void*)0xaa55;
void *rx_cb_arg = (void*)0xbb44;

void tfreedv_data_callback_tx(void *arg, unsigned char *packet, size_t *size)
{
    if (tx_cb_arg != arg) {
        ret++;
        printf("FAIL: %s called with wrong argument value\n", __func__);
    }
    printf("--------------------------------------\n");
    printf("TX callback called for test %zd bytes data for test %d:\n'%s'\n",
        testvec[vector].data_size, vector,
        testvec[vector].testname);

    memcpy(packet, testvec[vector].data, testvec[vector].data_size);
    *size = testvec[vector].data_size;
    
    return;
}

void tfreedv_data_callback_rx(void *arg, unsigned char *packet, size_t size)
{
    if (rx_cb_arg != arg) {
        ret++;
        printf("FAIL: %s called with wrong argument value\n", __func__);
    }
    printf("RX callback called with %zd bytes\n", size);
    
    if (testvec[vector].data_size) {
        size_t data_size = testvec[vector].data_size;
        if (data_size != size) {
            printf("FAIL: Received size does not match test vector: %zd != %zd\n",
                size, data_size);
            ret++;
        } else {
            size_t i;
            for (i = 0; i < data_size; i++) {
                if (packet[i] != testvec[vector].data[i]) {
                    printf("FAIL: byte %zd does not match 0x%02x != 0x%02x\n",
                        i, packet[i], testvec[vector].data[i]);
                    ret++;
                }
            }
        }
    } else {
        if (size != 12) {
            printf("FAIL: Received header is not 12 bytes: %zd\n", size);
            ret++;
        } else {
            if (memcmp(packet, bcast_header, 6)) {
                printf("FAIL: Header is not a broadcast!\n");
                ret++;
	    }
            if (memcmp(packet+6, test_header, 6)) {
                printf("FAIL: Header does not match!\n");
                ret++;
            }
        }
    }
    
    rx_done = 1;
}

int main(int argc, char **argv)
{
    struct freedv_data_channel *fdc;
    
    fdc = freedv_data_channel_create();

    freedv_data_set_header(fdc, test_header);
    freedv_data_set_cb_tx(fdc, tfreedv_data_callback_tx, tx_cb_arg);
    freedv_data_set_cb_rx(fdc, tfreedv_data_callback_rx, rx_cb_arg);

    while (vector < sizeof(testvec)/sizeof(struct testvec)) {
        size_t frame_size = testvec[vector].frame_size;
        unsigned char frame[frame_size];
        int from, bcast, crc, end;
        int i;
        size_t check_size;
        unsigned char flags;
        int nr_frames;
	
        freedv_data_channel_tx_frame(fdc, frame, frame_size, &from, &bcast, &crc, &end);

        check_size = frame_size;
        if (frame_data_pos + check_size > testvec[vector].frame_data_size)
            check_size = testvec[vector].frame_data_size - frame_data_pos;
        
        flags = from * 0x80 + bcast * 0x40 + crc * 0x20 + end;
        printf("0x%02x:", flags);
        for (i = 0; i < check_size; i++) {
            if (frame[i] != testvec[vector].frame_data[frame_data_pos + i]) {
                printf(" [0x%02x!=0x%02x]", 
                    frame[i], testvec[vector].frame_data[frame_data_pos + i]);
                ret++;
            } else {
                printf(" 0x%02x", frame[i]);
            }
        }
        printf("\n");
        
        if (flags != testvec[vector].flags[frame_data_pos / frame_size]) {
            printf("FAIL: Flags byte does not match 0x%02x != 0x%02x\n",
                flags, testvec[vector].flags[frame_data_pos / frame_size]);
            ret++;
        }

        freedv_data_channel_rx_frame(fdc, frame, frame_size, from, bcast, crc, end);

        frame_data_pos += frame_size;

        nr_frames = freedv_data_get_n_tx_frames(fdc, frame_size);

        if (frame_data_pos >= testvec[vector].frame_data_size) {
    	    if (nr_frames) {
    	        printf("FAIL: nr_frames is not zero: %d\n", nr_frames);
    	    	ret++;
    	    }
            vector++;
            frame_data_pos = 0;
            if (!rx_done) {
                printf("FAIL: RX callback not executed\n");
                ret++;
            }
            rx_done = 0;
        } else {
            int vec_frames = (testvec[vector].frame_data_size - frame_data_pos);
            vec_frames /= frame_size;
            vec_frames++;
            if (nr_frames != vec_frames) {
                printf("FAIL: nr_frames != vec_frames: %d != %d\n", nr_frames, vec_frames);
                ret++;
            }
        }
    }

    freedv_data_channel_destroy(fdc);

    printf("--------------------------------------\n");
    printf("tfreedv_data_channel test result: ");
    if (ret) {
        printf("Failed %d\n", ret);
    } else {
        printf("Passed\n");
    }

    return ret;
}