AX25:add new print function compatible with TNC-2 format.
[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  * $test$: cp bertos/cfg/cfg_afsk.h $cfgdir/
41  * $test$: echo "#undef CONFIG_AFSK_TX_BUFLEN" >> $cfgdir/cfg_afsk.h
42  * $test$: echo "#define CONFIG_AFSK_TX_BUFLEN 512" >> $cfgdir/cfg_afsk.h
43  */
44
45
46 #include "afsk.h"
47 #include "cfg/cfg_afsk.h"
48
49 #include <drv/timer.h>
50 #include <net/ax25.h>
51
52 #include <cfg/test.h>
53 #include <cfg/debug.h>
54 #include <cfg/kfile_debug.h>
55
56 #include <cpu/byteorder.h>
57
58 #include <stdio.h>
59 #include <string.h>
60
61 FILE *fp_adc;
62 FILE *fp_dac;
63 uint32_t data_size;
64 uint32_t data_written;
65 Afsk afsk_fd;
66 AX25Ctx ax25;
67 KFileDebug dbg;
68
69 int msg_cnt;
70 static void message_hook(struct AX25Msg *msg)
71 {
72         msg_cnt++;
73         ax25_print(&dbg.fd, msg);
74 }
75
76 static FILE *afsk_fileOpen(const char *name)
77 {
78         FILE *fp = 0;
79         #if CPU_AVR
80                 (void)name;
81                 #warning TODO: open the file?
82         #else
83                 fp = fopen(name, "rb");
84         #endif
85         ASSERT(fp);
86
87         char snd[5];
88         ASSERT(fread(snd, 1, 4, fp) == 4);
89         snd[4] = 0;
90         ASSERT(strcmp(snd, ".snd") == 0);
91
92         uint32_t offset;
93         ASSERT(fread(&offset, 1, sizeof(offset), fp) == sizeof(offset));
94         offset = be32_to_cpu(offset);
95         kprintf("AU file offset: %ld\n", (long)offset);
96         ASSERT(offset >= 24);
97
98         ASSERT(fread(&data_size, 1, sizeof(data_size), fp) == sizeof(data_size));
99         data_size = be32_to_cpu(data_size);
100         kprintf("AU file data_size: %ld\n", (long)data_size);
101         ASSERT(data_size);
102
103         uint32_t encoding;
104         ASSERT(fread(&encoding, 1, sizeof(encoding), fp) == sizeof(encoding));
105         encoding = be32_to_cpu(encoding);
106         kprintf("AU file encoding: %ld\n", (long)encoding);
107         ASSERT(encoding == 2); // 8 bit linear PCM
108
109         uint32_t sample_rate;
110         ASSERT(fread(&sample_rate, 1, sizeof(sample_rate), fp) == sizeof(sample_rate));
111         sample_rate = be32_to_cpu(sample_rate);
112         kprintf("AU file sample_rate: %ld\n", (long)sample_rate);
113         ASSERT(sample_rate == 9600);
114
115         uint32_t channels;
116         ASSERT(fread(&channels, 1, sizeof(channels), fp) == sizeof(channels));
117         channels = be32_to_cpu(channels);
118         kprintf("AU file channels: %ld\n", (long)channels);
119         ASSERT(channels == 1);
120
121         #if CPU_AVR
122                 #warning TODO: fseek?
123         #else
124                 ASSERT(fseek(fp, offset, SEEK_SET) == 0);
125         #endif
126         return fp;
127 }
128
129 int afsk_testSetup(void)
130 {
131         kdbg_init();
132         kfiledebug_init(&dbg);
133         fp_adc = afsk_fileOpen("test/afsk_test.au");
134         #if CPU_AVR
135                 #warning TODO: open the file?
136         #else
137                 fp_dac = fopen("test/afsk_test_out.au", "w+b");
138         #endif
139         ASSERT(fp_dac);
140         #define FS_HH (((uint32_t)CONFIG_AFSK_DAC_SAMPLERATE) >> 24)
141         #define FS_HL ((((uint32_t)CONFIG_AFSK_DAC_SAMPLERATE) >> 16) & 0xff)
142         #define FS_LH ((((uint32_t)CONFIG_AFSK_DAC_SAMPLERATE) >> 8) & 0xff)
143         #define FS_LL (((uint32_t)CONFIG_AFSK_DAC_SAMPLERATE) & 0xff)
144
145         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};
146
147         ASSERT(fwrite(snd_header, 1, sizeof(snd_header), fp_dac) == sizeof(snd_header));
148
149         timer_init();
150         afsk_init(&afsk_fd, 0 ,0);
151         ax25_init(&ax25, &afsk_fd.fd, message_hook);
152         return 0;
153 }
154
155
156 static void messageout_hook(struct AX25Msg *msg)
157 {
158         ASSERT(strncmp(msg->dst.call, "ABCDEF", 6) == 0);
159         ASSERT(strncmp(msg->src.call, "123456", 6) == 0);
160         ASSERT(msg->src.ssid == 1);
161         ASSERT(msg->dst.ssid == 0);
162         ASSERT(msg->ctrl == AX25_CTRL_UI);
163         ASSERT(msg->pid == AX25_PID_NOLAYER3);
164         ASSERT(msg->len == 256);
165         for (int i = 0; i < 256; i++)
166                 ASSERT(msg->info[i] == i);
167 }
168
169 int afsk_testRun(void)
170 {
171         int c;
172         while ((c = fgetc(fp_adc)) != EOF)
173         {
174                 afsk_adc_isr(&afsk_fd, (int8_t)c);
175
176                 ax25_poll(&ax25);
177         }
178         kprintf("Messages correctly received: %d\n", msg_cnt);
179         ASSERT(msg_cnt >= 15);
180
181         char buf[256];
182         for (unsigned i = 0; i < sizeof(buf); i++)
183                 buf[i] = i;
184
185         ax25_send(&ax25, AX25_CALL("abcdef", 0), AX25_CALL("123456", 1), buf, sizeof(buf));
186
187         do
188         {
189                 int8_t val = afsk_dac_isr(&afsk_fd) - 128;
190                 ASSERT(fwrite(&val, 1, sizeof(val), fp_dac) == sizeof(val));
191                 data_written++;
192         }
193         while (afsk_fd.sending);
194
195         #define SND_DATASIZE_OFF 8
196         #if CPU_AVR
197                 #warning TODO: fseek?
198         #else
199                 ASSERT(fseek(fp_dac, SND_DATASIZE_OFF, SEEK_SET) == 0);
200         #endif
201         data_written = cpu_to_be32(data_written);
202         ASSERT(fwrite(&data_written, 1, sizeof(data_written), fp_dac) == sizeof(data_written));
203         ASSERT(fclose(fp_adc) + fclose(fp_dac) == 0);
204
205         fp_adc = afsk_fileOpen("test/afsk_test_out.au");
206         ax25_init(&ax25, &afsk_fd.fd, messageout_hook);
207
208         while ((c = fgetc(fp_adc)) != EOF)
209         {
210                 afsk_adc_isr(&afsk_fd, (int8_t)c);
211
212                 ax25_poll(&ax25);
213         }
214
215         return 0;
216 }
217
218 int afsk_testTearDown(void)
219 {
220         return fclose(fp_adc);
221 }
222
223 TEST_MAIN(afsk);