Fix some warnings.
[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         #if CPU_AVR
77                 #warning TODO: open the file?
78         #else
79                 fp_adc = fopen("test/afsk_test.au", "rb");
80         #endif
81         ASSERT(fp_adc);
82
83         char snd[5];
84         ASSERT(fread(snd, 1, 4, fp_adc) == 4);
85         snd[4] = 0;
86         ASSERT(strcmp(snd, ".snd") == 0);
87
88         uint32_t offset;
89         ASSERT(fread(&offset, 1, sizeof(offset), fp_adc) == sizeof(offset));
90         offset = be32_to_cpu(offset);
91         kprintf("AU file offset: %ld\n", offset);
92         ASSERT(offset >= 24);
93
94         ASSERT(fread(&data_size, 1, sizeof(data_size), fp_adc) == sizeof(data_size));
95         data_size = be32_to_cpu(data_size);
96         kprintf("AU file data_size: %ld\n", data_size);
97         ASSERT(data_size);
98
99         uint32_t encoding;
100         ASSERT(fread(&encoding, 1, sizeof(encoding), fp_adc) == sizeof(encoding));
101         encoding = be32_to_cpu(encoding);
102         kprintf("AU file encoding: %ld\n", encoding);
103         ASSERT(encoding == 2); // 8 bit linear PCM
104
105         uint32_t sample_rate;
106         ASSERT(fread(&sample_rate, 1, sizeof(sample_rate), fp_adc) == sizeof(sample_rate));
107         sample_rate = be32_to_cpu(sample_rate);
108         kprintf("AU file sample_rate: %ld\n", sample_rate);
109         ASSERT(sample_rate == 9600);
110
111         uint32_t channels;
112         ASSERT(fread(&channels, 1, sizeof(channels), fp_adc) == sizeof(channels));
113         channels = be32_to_cpu(channels);
114         kprintf("AU file channels: %ld\n", channels);
115         ASSERT(channels == 1);
116
117         #if CPU_AVR
118                 #warning TODO: fseek?
119         #else
120                 ASSERT(fseek(fp_adc, offset, SEEK_SET) == 0);
121         #endif
122
123         #if 0
124         fp_dac = fopen("test/afsk_test_out.au", "w+b");
125         ASSERT(fp_dac);
126         #define FS_HH ((CONFIG_AFSK_DAC_SAMPLERATE) >> 24)
127         #define FS_HL (((CONFIG_AFSK_DAC_SAMPLERATE) >> 16) & 0xff)
128         #define FS_LH (((CONFIG_AFSK_DAC_SAMPLERATE) >> 8) & 0xff)
129         #define FS_LL ((CONFIG_AFSK_DAC_SAMPLERATE) & 0xff)
130
131         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};
132
133         ASSERT(fwrite(snd_header, 1, sizeof(snd_header), fp_dac) == sizeof(snd_header));
134         #endif
135         timer_init();
136         afsk_init(&afsk_fd);
137         afsk_fd.fd.error = kfile_genericClose;
138         ax25_init(&ax25, &afsk_fd.fd, message_hook);
139         return 0;
140 }
141
142
143 int afsk_testRun(void)
144 {
145         int c;
146         while ((c = fgetc(fp_adc)) != EOF)
147         {
148                 afsk_adc_val = (int8_t)c;
149                 afsk_adc_isr();
150
151                 ax25_poll(&ax25);
152         }
153         kprintf("Messages correctly received: %d\n", msg_cnt);
154         ASSERT(msg_cnt >= 15);
155         #if 0
156
157         for (int i = 0; i < 75; i++)
158                 kfile_putc(HDLC_FLAG, &afsk_fd.fd);
159
160         uint8_t tst[] =
161         {
162                 0x92, 0xAE, 0x6A, 0x84, 0x9C, 0xB2, 0xF2, 0x92, 0xA4, 0x6A, 0xA0, 0x40, 0xC0, 0xE1, 0xAE, 0x92,
163                 0x88, 0x8A, 0x6E, 0x40, 0x6B, 0x03, 0xF0, 0x60, 0x27, 0x5B, 0x1E, 0x6C, 0x23, 0x43, 0x6A, 0x2F,
164                 0x5D, 0x22, 0x35, 0x70, 0x7D, 0x6F, 0x70, 0x2E, 0x52, 0x4F, 0x42, 0x45, 0x52, 0x54, 0x4F, 0x2C,
165                 0x5F, 0x51, 0x52, 0x56, 0x3A, 0x34, 0x33, 0x30, 0x2E, 0x31, 0x36, 0x32, 0x2C, 0x35, 0x0D, 0x09,
166                 0xCB,
167         };
168
169         for (int i = 0; i < sizeof(tst); i++)
170         {
171                 if (tst[i] == AFSK_ESC || tst[i] == HDLC_FLAG || tst[i] == HDLC_RESET)
172                         kfile_putc(AFSK_ESC, &afsk_fd.fd);
173                 kfile_putc(tst[i], &afsk_fd.fd);
174         }
175
176         kfile_putc(0xE0, &afsk_fd.fd);
177         kfile_putc(0x03, &afsk_fd.fd);
178
179         kfile_putc(HDLC_FLAG, &afsk_fd.fd);
180
181         while (afsk_tx_test)
182                 afsk_dac_isr();
183
184         #endif
185         return 0;
186 }
187
188 #define SND_DATASIZE_OFF 8
189
190 int afsk_testTearDown(void)
191 {
192         #if 0
193         ASSERT(fseek(fp_dac, SND_DATASIZE_OFF, SEEK_SET) == 0);
194         data_written = cpu_to_be32(data_written);
195         ASSERT(fwrite(&data_written, 1, sizeof(data_written), fp_dac) == sizeof(data_written));
196         return fclose(fp_adc) + fclose(fp_dac);
197         #endif
198         return fclose(fp_adc);
199 }
200
201 TEST_MAIN(afsk);