blob: 44c3453cd4022f3debc4f5d3b0cd448a67e439d3 [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;
Vadim Yanitskiy14b87b22019-09-24 15:51:03 +070047 struct osmo_gcr_parsed p = {
48 .net_len = 0,
49 .net = { 0 },
50 .node = 0x00,
51 .cr = { 0 },
52 };
53 struct osmo_gcr_parsed g = {
Max7918f842018-12-10 10:57:59 +010054 .net_len = 3,
55 .net = { 0x51, 0x52, 0x53 },
56 .node = 0xDEAD,
57 .cr = { 0x41, 0x42, 0x43, 0x44, 0x45 }
58 };
59 int rc;
60
61 msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, "global call reference");
62 if (!msg)
63 return;
64
65 len = osmo_enc_gcr(msg, &g);
66 printf("Testing Global Call Reference encoder...\n\t%d bytes added: %s\n",
67 len, len == ARRAY_SIZE(res) ? "OK" : "FAIL");
68
69 if (!msgb_eq_data_print(msg, res, ARRAY_SIZE(res)))
70 abort();
71
72 rc = osmo_dec_gcr(&p, msgb_data(msg), msgb_length(msg));
73 if (rc < 0) {
74 printf("decoding failed: %s [%s]\n", strerror(-rc), msgb_hexdump(msg));
75 abort();
76 }
77
78 if (p.net_len != g.net_len) {
79 printf("Network ID length parsed wrong: %u != %u\n", p.net_len, g.net_len);
80 abort();
81 }
82
83 if (p.node != g.node) {
84 printf("Node ID parsed wrong: 0x%X != 0x%X\n", p.node, g.node);
85 abort();
86 }
87
88 if (memcmp(p.net, g.net, g.net_len) != 0) {
89 printf("Network ID parsed wrong: %s\n", osmo_hexdump(p.net, p.net_len));
90 abort();
91 }
92
93 if (memcmp(p.cr, g.cr, 5) != 0) {
94 printf("Call ref. ID parsed wrong: %s\n", osmo_hexdump(p.cr, 5));
95 abort();
96 }
97
98 printf("\tdecoded %d bytes: %s\n", rc, rc == len ? "OK" : "FAIL");
99 msgb_free(msg);
100}
101
102int main(int argc, char **argv)
103{
104 osmo_init_logging2(talloc_named_const(NULL, 0, "gsm29205 test"), NULL);
105
106 printf("Testing 3GPP TS 29.205 routines...\n");
107
108 test_gcr();
109
110 printf("Done.\n");
111
112 return EXIT_SUCCESS;
113}