Add test for AFSK1200 modem driver.
[bertos.git] / bertos / net / afsk_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief AFSK demodulator test.
34  *
35  * \version $Id$
36  * \author Francesco Sacchi <batt@develer.com>
37  * $test$: cp bertos/cfg/cfg_ax25.h $cfgdir/
38  * $test$: echo "#undef AX25_LOG_LEVEL" >> $cfgdir/cfg_ax25.h
39  * $test$: echo "#define AX25_LOG_LEVEL LOG_LVL_INFO" >> $cfgdir/cfg_ax25.h
40  */
41
42
43 #include "afsk.h"
44 #include "cfg/cfg_afsk.h"
45
46 #include <drv/timer.h>
47 #include <net/ax25.h>
48
49 #include <cfg/test.h>
50 #include <cfg/debug.h>
51
52 #include <cpu/byteorder.h>
53
54 #include <stdio.h>
55 #include <string.h>
56
57 FILE *fp_adc;
58 FILE *fp_dac;
59 uint32_t data_size;
60 uint32_t data_written;
61 bool afsk_tx_test;
62 Afsk afsk_fd;
63 AX25Ctx ax25;
64
65 int8_t afsk_adc_val;
66
67 int msg_cnt;
68 static void message_hook(UNUSED_ARG(struct AX25Msg *, msg))
69 {
70         msg_cnt++;
71 }
72
73 int afsk_testSetup(void)
74 {
75         kdbg_init();
76         fp_adc = fopen("test/afsk_test.au", "rb");
77         ASSERT(fp_adc);
78
79         char snd[5];
80         ASSERT(fread(snd, 1, 4, fp_adc) == 4);
81         snd[4] = 0;
82         ASSERT(strcmp(snd, ".snd") == 0);
83
84         uint32_t offset;
85         ASSERT(fread(&offset, 1, sizeof(offset), fp_adc) == sizeof(offset));
86         offset = be32_to_cpu(offset);
87         kprintf("AU file offset: %d\n", offset);
88         ASSERT(offset >= 24);
89
90         ASSERT(fread(&data_size, 1, sizeof(data_size), fp_adc) == sizeof(data_size));
91         data_size = be32_to_cpu(data_size);
92         kprintf("AU file data_size: %d\n", data_size);
93         ASSERT(data_size);
94
95         uint32_t encoding;
96         ASSERT(fread(&encoding, 1, sizeof(encoding), fp_adc) == sizeof(encoding));
97         encoding = be32_to_cpu(encoding);
98         kprintf("AU file encoding: %d\n", encoding);
99         ASSERT(encoding == 2); // 8 bit linear PCM
100
101         uint32_t sample_rate;
102         ASSERT(fread(&sample_rate, 1, sizeof(sample_rate), fp_adc) == sizeof(sample_rate));
103         sample_rate = be32_to_cpu(sample_rate);
104         kprintf("AU file sample_rate: %d\n", sample_rate);
105         ASSERT(sample_rate == 9600);
106
107         uint32_t channels;
108         ASSERT(fread(&channels, 1, sizeof(channels), fp_adc) == sizeof(channels));
109         channels = be32_to_cpu(channels);
110         kprintf("AU file channels: %d\n", channels);
111         ASSERT(channels == 1);
112
113         ASSERT(fseek(fp_adc, offset, SEEK_SET) == 0);
114
115         #if 0
116         fp_dac = fopen("test/afsk_test_out.au", "w+b");
117         ASSERT(fp_dac);
118         #define FS_HH ((CONFIG_AFSK_DAC_SAMPLERATE) >> 24)
119         #define FS_HL (((CONFIG_AFSK_DAC_SAMPLERATE) >> 16) & 0xff)
120         #define FS_LH (((CONFIG_AFSK_DAC_SAMPLERATE) >> 8) & 0xff)
121         #define FS_LL ((CONFIG_AFSK_DAC_SAMPLERATE) & 0xff)
122
123         uint8_t snd_header[] = { '.','s','n','d', 0,0,0,24, 0,0,0,0, 0,0,0,2, FS_HH,FS_HL,FS_LH,FS_LL, 0,0,0,1};
124
125         ASSERT(fwrite(snd_header, 1, sizeof(snd_header), fp_dac) == sizeof(snd_header));
126         #endif
127         timer_init();
128         afsk_init(&afsk_fd);
129         afsk_fd.fd.error = kfile_genericClose;
130         ax25_init(&ax25, &afsk_fd.fd, message_hook);
131         return 0;
132 }
133
134
135 int afsk_testRun(void)
136 {
137         int c;
138         while ((c = fgetc(fp_adc)) != EOF)
139         {
140                 afsk_adc_val = (int8_t)c;
141                 afsk_adc_isr();
142
143                 ax25_poll(&ax25);
144         }
145         kprintf("Messages correctly received: %d\n", msg_cnt);
146         ASSERT(msg_cnt >= 15);
147         #if 0
148
149         for (int i = 0; i < 75; i++)
150                 kfile_putc(HDLC_FLAG, &afsk_fd.fd);
151
152         uint8_t tst[] =
153         {
154                 0x92, 0xAE, 0x6A, 0x84, 0x9C, 0xB2, 0xF2, 0x92, 0xA4, 0x6A, 0xA0, 0x40, 0xC0, 0xE1, 0xAE, 0x92,
155                 0x88, 0x8A, 0x6E, 0x40, 0x6B, 0x03, 0xF0, 0x60, 0x27, 0x5B, 0x1E, 0x6C, 0x23, 0x43, 0x6A, 0x2F,
156                 0x5D, 0x22, 0x35, 0x70, 0x7D, 0x6F, 0x70, 0x2E, 0x52, 0x4F, 0x42, 0x45, 0x52, 0x54, 0x4F, 0x2C,
157                 0x5F, 0x51, 0x52, 0x56, 0x3A, 0x34, 0x33, 0x30, 0x2E, 0x31, 0x36, 0x32, 0x2C, 0x35, 0x0D, 0x09,
158                 0xCB,
159         };
160
161         for (int i = 0; i < sizeof(tst); i++)
162         {
163                 if (tst[i] == AFSK_ESC || tst[i] == HDLC_FLAG || tst[i] == HDLC_RESET)
164                         kfile_putc(AFSK_ESC, &afsk_fd.fd);
165                 kfile_putc(tst[i], &afsk_fd.fd);
166         }
167
168         kfile_putc(0xE0, &afsk_fd.fd);
169         kfile_putc(0x03, &afsk_fd.fd);
170
171         kfile_putc(HDLC_FLAG, &afsk_fd.fd);
172
173         while (afsk_tx_test)
174                 afsk_dac_isr();
175
176         #endif
177         return 0;
178 }
179
180 #define SND_DATASIZE_OFF 8
181
182 int afsk_testTearDown(void)
183 {
184         #if 0
185         ASSERT(fseek(fp_dac, SND_DATASIZE_OFF, SEEK_SET) == 0);
186         data_written = cpu_to_be32(data_written);
187         ASSERT(fwrite(&data_written, 1, sizeof(data_written), fp_dac) == sizeof(data_written));
188         return fclose(fp_adc) + fclose(fp_dac);
189         #endif
190         return fclose(fp_adc);
191 }
192
193 TEST_MAIN(afsk);