aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/libmsc/gsm_ussd_map_proto.c
blob: 706ba1d0037cbdcbe5c4daf24ca57f5442074ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* GSM USSD external MAP protocol on pseudo TCAP */

/* (C) 2015 by Sergey Kostanbaev <sergey.kostanbaev@gmail.com>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <openbsc/gsm_ussd_map.h>
#include <openbsc/gsm_ussd_map_proto.h>
#include <openbsc/gsm_subscriber.h>
#include <openbsc/gsm_04_08.h>
#include <openbsc/debug.h>
#include <openbsc/db.h>
#include <openbsc/chan_alloc.h>
#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
#include <osmocom/gsm/gsup.h>
#include <openbsc/osmo_msc.h>
#include <openbsc/gprs_utils.h>
#include <openbsc/ussd.h>

/*
* 0 - OSMO_GSUP_MSGT_USSD_MAP constant
* 1 - LEN
* 2 - message_type [ REGISTER / FACILITY / RELEASE COMPLETE ]
* 3,4,5,6 - tid          ID associated with the session
* 7 - FMAP_MSISDN constant
* 8 - extention_len
* 9..x -  extention
* x+1 .. original MAP message
*/

int subscr_uss_message(struct msgb *msg,
		       struct ss_header *req,
		       const char* extension,
		       uint32_t ref,
		       const uint8_t* component_data)
{
	uint8_t bcd_lvlen;
	uint8_t offset = 0;
	uint8_t *gsup_indicator;

	gsup_indicator = msgb_put(msg, 7);

	/* First byte should always be OSMO_GSUP_MSGT_USSD_MAP */
	gsup_indicator[offset++] = OSMO_GSUP_MSGT_USSD_MAP;
	gsup_indicator[offset++] = 0; // Total length
	gsup_indicator[offset++] = req->message_type;

	gsup_indicator[offset++] = ref >> 24;
	gsup_indicator[offset++] = ref >> 16;
	gsup_indicator[offset++] = ref >> 8;
	gsup_indicator[offset++] = ref;

	if (extension) {
		gsup_indicator[offset++] = FMAP_MSISDN;
		bcd_lvlen = gsm48_encode_bcd_number(gsup_indicator + offset,
						    32, 0, extension);

		offset += bcd_lvlen;
		msgb_put(msg, bcd_lvlen + 1);
	}

	if (component_data) {
		msgb_put(msg, req->component_length);
		memcpy(gsup_indicator + offset, component_data, req->component_length);
	}

	gsup_indicator[1] = offset + req->component_length - 2; //except OSMO_GSUP_MSGT_USSD_MAP and length field
	return 0;
#if 0
	gsup_indicator[6] = req->component_type;

	/* invokeId */
	msgb_tlv_put(msg, GSM0480_COMPIDTAG_INVOKE_ID, 1, &req->invoke_id);

	/* opCode */
	msgb_tlv_put(msg, GSM0480_OPERATION_CODE, 1, &req->opcode);

	if (req->ussd_text_len > 0) {
		msgb_tlv_put(msg, ASN1_OCTET_STRING_TAG, req->ussd_text_len + 1, &req->ussd_text_language);
	}

	if (extension) {
		uint8_t bcd_buf[32];
		bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
						  extension);
		msgb_tlv_put(msg, FMAP_MSISDN, bcd_len - 1, &bcd_buf[1]);
	}

	/* fill actual length */
	gsup_indicator[7] = 3 + 3 + (req->ussd_text_len + 1 + 2) + (bcd_len + 2);;

	/* wrap with GSM0480_CTYPE_INVOKE */
	// gsm0480_wrap_invoke(msg, req->opcode, invoke_id);
	// gsup_indicator = msgb_push(msgb, 1);
	// gsup_indicator[0] = OSMO_GSUP_MSGT_MAP;
	return 0;
#endif
}



int rx_uss_message_parse(const uint8_t* data,
			 size_t len,
			 struct ss_header *ss,
			 uint32_t *pref,
			 char* extention,
			 size_t extention_len)
{
	uint8_t ext_len;
	const uint8_t* const_data = data + 1; // Skip constant
	uint32_t ref;
	int total_len;

	if (len < 7)
		return -1;

	/* skip OSMO_GSUP_MSGT_MAP */
	total_len        = *(const_data++);
	ss->message_type = *(const_data++);

	ref = ((uint32_t)(*(const_data++))) << 24;
	ref |= ((uint32_t)(*(const_data++))) << 16;
	ref |= ((uint32_t)(*(const_data++))) << 8;
	ref |= ((uint32_t)(*(const_data++)));
	if (pref)
		*pref = ref;

	total_len -= 4 + 1; // ref + sizeof(len)

	if (*const_data == FMAP_MSISDN) {
		ext_len = *(++const_data);
		if (extention) {
			gsm48_decode_bcd_number(extention,
						extention_len,
						const_data,
						0);
		}
		const_data += ext_len + 1;
		total_len -= ext_len + 2; // tag FMAP_MSISDN + sizeof(len)
	}

	ss->component_offset = const_data - data;
	ss->component_length = total_len; //data[ss->component_offset + 1];

	return 0;
#if 0
	ss->component_type = *(++const_data);

	/* skip full len and move to component id */
	const_data += 2;

	if (*const_data != GSM0480_COMPIDTAG_INVOKE_ID) {
		return -1;
	}
	const_data += 2;
	ss->invoke_id = *const_data;
	const_data++;

	//
	if (*const_data != GSM0480_OPERATION_CODE) {
		return -1;
	}
	const_data += 2;
	ss->opcode = *const_data;
	const_data++;


	while (const_data - data < len) {
		uint8_t len;
		switch (*const_data) {
		case ASN1_OCTET_STRING_TAG:
			ss->ussd_text_len = len = (*(++const_data) - 1);
			ss->ussd_text_language = *(++const_data);
			memcpy(ss->ussd_text,
				++const_data,
				(len > MAX_LEN_USSD_STRING) ? MAX_LEN_USSD_STRING : len);
			const_data += len;
			break;

		case FMAP_MSISDN:
			len = *(++const_data);
			gsm48_decode_bcd_number(extention,
						extention_len,
						const_data,
						0);
			const_data += len + 1;
			break;
		default:
			DEBUGP(DSS, "Unknown code: %d\n", *const_data);
			return -1;
		}
	}

	return 0;
#endif
}