AX25:add new print function compatible with TNC-2 format.
[bertos.git] / bertos / net / ax25_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 AX25 test.
34  *
35  * \version $Id$
36  * \author Francesco Sacchi <batt@develer.com>
37  */
38
39 #include "ax25.h"
40
41 #include <struct/kfile_mem.h>
42
43 #include <cfg/debug.h>
44 #include <cfg/kfile_debug.h>
45 #include <cfg/test.h>
46
47 #include <string.h> //strncmp
48
49 static AX25Ctx ax25;
50 static KFileMem mem;
51 static KFileDebug dbg;
52
53 #define APRS_MSG \
54         0x3D, 0x34, 0x36, 0x30, 0x33, 0x2E, 0x36, 0x33, \
55         0x4E, 0x2F, 0x30, 0x31, 0x34, 0x33, 0x31, 0x2E, \
56         0x32, 0x36, 0x45, 0x2D, 0x4F, 0x70, 0x2E, 0x20, \
57         0x41, 0x6E, 0x64, 0x72, 0x65, 0x6A
58
59 uint8_t aprs_packet[] =
60 {
61         HDLC_FLAG,
62         0x82, 0xA0, 0xA4, 0xA6, 0x40, 0x40, 0xE0, /* dst */
63         0xA6, 0x6A, 0x6E, 0x98, 0x9C, 0x40, 0x61, /* src */
64         0x03, /* ctrl */
65         0xF0, /* pid */
66         APRS_MSG, /* payload */
67         0x40, 0x65, /* CRC */
68         HDLC_FLAG,
69 };
70
71 uint8_t buf[] = { APRS_MSG };
72 KFileMem mem1;
73 uint8_t aprs_packet_check[256];
74
75
76 static void msg_callback(AX25Msg *msg)
77 {
78         ax25_print(&dbg.fd, msg);
79         ASSERT(strncmp(msg->dst.call, "APRS\x0\x0", 6) == 0);
80         ASSERT(strncmp(msg->src.call, "S57LN\x0", 6) == 0);
81         ASSERT(msg->src.ssid == 0);
82         ASSERT(msg->dst.ssid == 0);
83         ASSERT(msg->ctrl == AX25_CTRL_UI);
84         ASSERT(msg->pid == AX25_PID_NOLAYER3);
85         ASSERT(msg->len == 30);
86         ASSERT(strncmp((const char *)msg->info, "=4603.63N/01431.26E-Op. Andrej", 30) == 0);
87 }
88
89 int ax25_testSetup(void)
90 {
91         kdbg_init();
92         kfiledebug_init(&dbg);
93         kfilemem_init(&mem, aprs_packet, sizeof(aprs_packet));
94         kfilemem_init(&mem1, aprs_packet_check, sizeof(aprs_packet_check));
95         ax25_init(&ax25, &mem.fd, msg_callback);
96         return 0;
97 }
98
99 int ax25_testTearDown(void)
100 {
101         return 0;
102 }
103
104 int ax25_testRun(void)
105 {
106         ax25_poll(&ax25);
107         ax25_init(&ax25, &mem1.fd, NULL);
108         ax25_send(&ax25, AX25_CALL("aprs", 0x70), AX25_CALL("s57ln", 0x30), buf, sizeof(buf));
109         ASSERT(memcmp(aprs_packet, aprs_packet_check, sizeof(aprs_packet)) == 0);
110         return  0;
111 }
112
113 TEST_MAIN(ax25);