blob: 8b1ffdfa2d6f424b60186007b11a5c64c3aba593 [file] [log] [blame]
Harald Weltea362ee92017-05-15 16:39:56 +02001
2/* (C) 2017 by Harald Welte <laforge@gnumonks.org>
3 *
4 * All Rights Reserved
5 *
6 * This program 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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <stdio.h>
23#include <errno.h>
24
25#include <osmocom/core/sercomm.h>
26#include <osmocom/core/msgb.h>
27
28struct osmo_sercomm_inst g_osi;
29
30static const uint8_t valid_dlci3[] = { 0x7E, 3, 0x03, 'f', 'o', 'o', 0x7E };
31static const uint8_t valid_dlci23[] = { 0x7E, 23, 0x03, '2', '3', 0x7E };
32static const uint8_t valid_dlci23esc[] = { 0x7E, 23, 0x03, 0x7D, '2' ^ (1 << 5), '3', 0x7E };
33static const uint8_t valid_echo[] = { 0x7E, SC_DLCI_ECHO, 0x03, 'e', 'c', 'h', 'o', 0x7E };
34
35static void rx_ser_data(struct osmo_sercomm_inst *sc, const uint8_t *data, unsigned int len)
36{
37 unsigned int i;
38
39 printf("Feeding data into sercomm: %s\n", osmo_hexdump(data, len));
40 for (i = 0; i < len; i++) {
41 int rc = osmo_sercomm_drv_rx_char(sc, data[i]);
42 OSMO_ASSERT(rc == 1);
43 }
44}
45
46
47static void dlci_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
48{
49 printf("%s(): %s\n", __func__, msgb_hexdump(msg));
50}
51
52static struct msgb *create_mahlzeit_msg(void)
53{
54 struct msgb *msg = osmo_sercomm_alloc_msgb(10);
55 OSMO_ASSERT(msg);
56 msgb_put_u8(msg, 'M');
57 msgb_put_u8(msg, 'a');
58 msgb_put_u8(msg, 'h');
59 msgb_put_u8(msg, 'l');
60 msgb_put_u8(msg, 'z');
61 msgb_put_u8(msg, 'e');
62 msgb_put_u8(msg, 'i');
63 msgb_put_u8(msg, 't');
64 return msg;
65}
66
67static void drain_from_uart_side(struct osmo_sercomm_inst *osi)
68{
69 uint8_t ch;
70 int rc;
71
72 printf("Draining from UART: ");
73 while ((rc = osmo_sercomm_drv_pull(osi, &ch) == 1))
74 printf("0x%02x ", ch);
75 printf("\n");
76}
77
78static void test_echo(struct osmo_sercomm_inst *osi)
79{
80 printf("Testing built-in echo DLCI\n");
81 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 0);
82 rx_ser_data(osi, valid_echo, sizeof(valid_echo));
83 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 1);
84 drain_from_uart_side(osi);
85 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 0);
86}
87
88static void test_sercomm(void)
89{
90 int rc;
91 uint8_t ch;
92 struct msgb *msg;
93
94 printf("Initializing sercomm_inst\n");
95 osmo_sercomm_init(&g_osi);
96 g_osi.uart_id = 2342;
97
98 printf("Registering callback for invalid DLCI\n");
99 rc = osmo_sercomm_register_rx_cb(&g_osi, 255, NULL);
100 OSMO_ASSERT(rc == -EINVAL);
101
102 printf("Registering callback for valid DLCI\n");
103 rc = osmo_sercomm_register_rx_cb(&g_osi, 23, &dlci_rx_cb);
104 OSMO_ASSERT(rc == 0);
105
106 printf("Checking reject of overlod of valid DLCI\n");
107 rc = osmo_sercomm_register_rx_cb(&g_osi, 23, NULL);
108 OSMO_ASSERT(rc == -EBUSY);
109
110 printf("Checking Rx of incoming msg for valid DLCI\n");
111 rx_ser_data(&g_osi, valid_dlci23, sizeof(valid_dlci23));
112 printf("Checking Rx of incoming msg for unequipped DLCI\n");
113 rx_ser_data(&g_osi, valid_dlci3, sizeof(valid_dlci3));
114 printf("Checking Rx of incoming msg for valid DLCI\n");
115 rx_ser_data(&g_osi, valid_dlci23, sizeof(valid_dlci23));
116 printf("Checking Rx of incoming msg with escaped char for valid DLCI\n");
117 rx_ser_data(&g_osi, valid_dlci23esc, sizeof(valid_dlci23esc));
118
119 printf("Checking that no chars are to be transmitted\n");
120 OSMO_ASSERT(osmo_sercomm_drv_pull(&g_osi, &ch) == 0);
121
122 printf("Transmitting msgb through sercomm\n");
123 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 0);
124 msg = create_mahlzeit_msg();
125 osmo_sercomm_sendmsg(&g_osi, 42, msg);
126 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 1);
127 drain_from_uart_side(&g_osi);
128 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 0);
129
130 test_echo(&g_osi);
131}
132
133int main(int argc, char **argv)
134{
135 test_sercomm();
136}