blob: 058c9eb45fb09cac745d9126067a29f7f2f6b9f6 [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));
Neels Hofmeyrfe1ed3982017-11-17 00:15:44 +010050 msgb_free(msg);
Harald Weltea362ee92017-05-15 16:39:56 +020051}
52
53static struct msgb *create_mahlzeit_msg(void)
54{
55 struct msgb *msg = osmo_sercomm_alloc_msgb(10);
56 OSMO_ASSERT(msg);
57 msgb_put_u8(msg, 'M');
58 msgb_put_u8(msg, 'a');
59 msgb_put_u8(msg, 'h');
60 msgb_put_u8(msg, 'l');
61 msgb_put_u8(msg, 'z');
62 msgb_put_u8(msg, 'e');
63 msgb_put_u8(msg, 'i');
64 msgb_put_u8(msg, 't');
65 return msg;
66}
67
68static void drain_from_uart_side(struct osmo_sercomm_inst *osi)
69{
70 uint8_t ch;
71 int rc;
72
73 printf("Draining from UART: ");
74 while ((rc = osmo_sercomm_drv_pull(osi, &ch) == 1))
75 printf("0x%02x ", ch);
76 printf("\n");
77}
78
79static void test_echo(struct osmo_sercomm_inst *osi)
80{
81 printf("Testing built-in echo DLCI\n");
82 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 0);
83 rx_ser_data(osi, valid_echo, sizeof(valid_echo));
84 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 1);
85 drain_from_uart_side(osi);
86 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 0);
87}
88
89static void test_sercomm(void)
90{
91 int rc;
92 uint8_t ch;
93 struct msgb *msg;
94
95 printf("Initializing sercomm_inst\n");
96 osmo_sercomm_init(&g_osi);
97 g_osi.uart_id = 2342;
98
99 printf("Registering callback for invalid DLCI\n");
100 rc = osmo_sercomm_register_rx_cb(&g_osi, 255, NULL);
101 OSMO_ASSERT(rc == -EINVAL);
102
103 printf("Registering callback for valid DLCI\n");
104 rc = osmo_sercomm_register_rx_cb(&g_osi, 23, &dlci_rx_cb);
105 OSMO_ASSERT(rc == 0);
106
107 printf("Checking reject of overlod of valid DLCI\n");
108 rc = osmo_sercomm_register_rx_cb(&g_osi, 23, NULL);
109 OSMO_ASSERT(rc == -EBUSY);
110
111 printf("Checking Rx of incoming msg for valid DLCI\n");
112 rx_ser_data(&g_osi, valid_dlci23, sizeof(valid_dlci23));
113 printf("Checking Rx of incoming msg for unequipped DLCI\n");
114 rx_ser_data(&g_osi, valid_dlci3, sizeof(valid_dlci3));
115 printf("Checking Rx of incoming msg for valid DLCI\n");
116 rx_ser_data(&g_osi, valid_dlci23, sizeof(valid_dlci23));
117 printf("Checking Rx of incoming msg with escaped char for valid DLCI\n");
118 rx_ser_data(&g_osi, valid_dlci23esc, sizeof(valid_dlci23esc));
119
120 printf("Checking that no chars are to be transmitted\n");
121 OSMO_ASSERT(osmo_sercomm_drv_pull(&g_osi, &ch) == 0);
122
123 printf("Transmitting msgb through sercomm\n");
124 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 0);
125 msg = create_mahlzeit_msg();
126 osmo_sercomm_sendmsg(&g_osi, 42, msg);
127 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 1);
128 drain_from_uart_side(&g_osi);
129 OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 0);
130
131 test_echo(&g_osi);
132}
133
134int main(int argc, char **argv)
135{
136 test_sercomm();
Pau Espin Pedrolfc1911c2017-06-18 11:23:33 +0200137 return 0;
Harald Weltea362ee92017-05-15 16:39:56 +0200138}