blob: 5add1b18b544309b47d313f6da0985070e25388f [file] [log] [blame]
Max7918f842018-12-10 10:57:59 +01001/*
2 * (C) 2018 by sysmocom - s.f.m.c. GmbH
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <osmocom/gsm/gsm29205.h>
22#include <osmocom/core/msgb.h>
23#include <osmocom/gsm/protocol/gsm_08_08.h>
24#include <osmocom/gsm/gsm0808.h>
25#include <osmocom/gsm/tlv.h>
26#include <osmocom/core/logging.h>
27#include <osmocom/core/application.h>
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <stdbool.h>
32#include <string.h>
33#include <errno.h>
34
35static void test_gcr()
36{
37 static const uint8_t res[] = {
38 0x03, /* .net_len */
39 0x51, 0x52, 0x53, /* .net */
40 0x02, /* .node length */
41 0xde, 0xad, /* .node */
42 0x05, /* length of Call. Ref. */
43 0x41, 0x42, 0x43, 0x44, 0x45 /* .cr - Call. Ref. */
44 };
45 uint8_t len;
46 struct msgb *msg;
47 struct osmo_gcr_parsed p = { 0 }, g = {
48 .net_len = 3,
49 .net = { 0x51, 0x52, 0x53 },
50 .node = 0xDEAD,
51 .cr = { 0x41, 0x42, 0x43, 0x44, 0x45 }
52 };
53 int rc;
54
55 msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, "global call reference");
56 if (!msg)
57 return;
58
59 len = osmo_enc_gcr(msg, &g);
60 printf("Testing Global Call Reference encoder...\n\t%d bytes added: %s\n",
61 len, len == ARRAY_SIZE(res) ? "OK" : "FAIL");
62
63 if (!msgb_eq_data_print(msg, res, ARRAY_SIZE(res)))
64 abort();
65
66 rc = osmo_dec_gcr(&p, msgb_data(msg), msgb_length(msg));
67 if (rc < 0) {
68 printf("decoding failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg));
69 abort();
70 }
71
72 if (p.net_len != g.net_len) {
73 printf("Network ID length parsed wrong: %u != %u\n", p.net_len, g.net_len);
74 abort();
75 }
76
77 if (p.node != g.node) {
78 printf("Node ID parsed wrong: 0x%X != 0x%X\n", p.node, g.node);
79 abort();
80 }
81
82 if (memcmp(p.net, g.net, g.net_len) != 0) {
83 printf("Network ID parsed wrong: %s\n", osmo_hexdump(p.net, p.net_len));
84 abort();
85 }
86
87 if (memcmp(p.cr, g.cr, 5) != 0) {
88 printf("Call ref. ID parsed wrong: %s\n", osmo_hexdump(p.cr, 5));
89 abort();
90 }
91
92 printf("\tdecoded %d bytes: %s\n", rc, rc == len ? "OK" : "FAIL");
93 msgb_free(msg);
94}
95
96int main(int argc, char **argv)
97{
98 osmo_init_logging2(talloc_named_const(NULL, 0, "gsm29205 test"), NULL);
99
100 printf("Testing 3GPP TS 29.205 routines...\n");
101
102 test_gcr();
103
104 printf("Done.\n");
105
106 return EXIT_SUCCESS;
107}