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