Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 2 | * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 15 | * GNU General Public License for more details. |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 16 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/core/msgb.h> |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 26 | #include <osmocom/core/byteswap.h> |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 27 | #include <string.h> |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm48.h> |
| 31 | #include <osmocom/gsm/gsm0808_utils.h> |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 32 | |
| 33 | #define IP_V4_ADDR_LEN 4 |
| 34 | #define IP_V6_ADDR_LEN 16 |
| 35 | #define IP_PORT_LEN 2 |
| 36 | |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 37 | #define CHANNEL_TYPE_ELEMENT_MAXLEN 11 |
| 38 | #define CHANNEL_TYPE_ELEMENT_MINLEN 3 |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 39 | #define ENCRYPT_INFO_ELEMENT_MINLEN 1 |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 40 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 41 | #ifdef HAVE_SYS_SOCKET_H |
| 42 | |
| 43 | #include <sys/socket.h> |
| 44 | #include <netinet/in.h> |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 45 | |
| 46 | /*! \addtogroup gsm0808 |
| 47 | * @{ |
Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 48 | * \file gsm0808_utils.c |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 49 | */ |
| 50 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 51 | /*! Encode TS 08.08 AoIP transport address IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 52 | * \param[out] msg Message Buffer to which to append IE |
| 53 | * \param[in] ss Socket Address to be used in IE |
| 54 | * \returns number of bytes added to \a msg */ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 55 | uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg, |
| 56 | const struct sockaddr_storage *ss) |
| 57 | { |
| 58 | /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */ |
| 59 | struct sockaddr_in *sin; |
| 60 | struct sockaddr_in6 *sin6; |
| 61 | uint16_t port = 0; |
| 62 | uint8_t *ptr; |
| 63 | uint8_t *old_tail; |
| 64 | uint8_t *tlv_len; |
| 65 | |
| 66 | OSMO_ASSERT(msg); |
| 67 | OSMO_ASSERT(ss); |
| 68 | OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6); |
| 69 | |
| 70 | msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR); |
| 71 | tlv_len = msgb_put(msg,1); |
| 72 | old_tail = msg->tail; |
| 73 | |
| 74 | switch (ss->ss_family) { |
| 75 | case AF_INET: |
| 76 | sin = (struct sockaddr_in *)ss; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 77 | port = osmo_ntohs(sin->sin_port); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 78 | ptr = msgb_put(msg, IP_V4_ADDR_LEN); |
| 79 | memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN); |
| 80 | break; |
| 81 | case AF_INET6: |
| 82 | sin6 = (struct sockaddr_in6 *)ss; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 83 | port = osmo_ntohs(sin6->sin6_port); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 84 | ptr = msgb_put(msg, IP_V6_ADDR_LEN); |
| 85 | memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN); |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | msgb_put_u16(msg, port); |
| 90 | |
| 91 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 92 | return *tlv_len + 2; |
| 93 | } |
| 94 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 95 | /*! Decode TS 08.08 AoIP transport address IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 96 | * \param[out] ss Caller-provided memory where decoded socket addr is stored |
| 97 | * \param[in] elem pointer to IE value |
| 98 | * \param[in] len length of \a elem in bytes |
| 99 | * \returns number of bytes parsed */ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 100 | int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss, |
| 101 | const uint8_t *elem, uint8_t len) |
| 102 | { |
| 103 | /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */ |
| 104 | struct sockaddr_in sin; |
| 105 | struct sockaddr_in6 sin6; |
| 106 | const uint8_t *old_elem = elem; |
| 107 | |
| 108 | OSMO_ASSERT(ss); |
| 109 | if (!elem) |
| 110 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 111 | if (len == 0) |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | |
| 114 | memset(ss, 0, sizeof(*ss)); |
| 115 | |
| 116 | switch (len) { |
| 117 | case IP_V4_ADDR_LEN + IP_PORT_LEN: |
| 118 | memset(&sin, 0, sizeof(sin)); |
| 119 | sin.sin_family = AF_INET; |
| 120 | |
| 121 | memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN); |
| 122 | elem += IP_V4_ADDR_LEN; |
| 123 | sin.sin_port = osmo_load16le(elem); |
| 124 | elem += IP_PORT_LEN; |
| 125 | |
| 126 | memcpy(ss, &sin, sizeof(sin)); |
| 127 | break; |
| 128 | case IP_V6_ADDR_LEN + IP_PORT_LEN: |
| 129 | memset(&sin6, 0, sizeof(sin6)); |
| 130 | sin6.sin6_family = AF_INET6; |
| 131 | |
| 132 | memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN); |
| 133 | elem += IP_V6_ADDR_LEN; |
| 134 | sin6.sin6_port = osmo_load16le(elem); |
| 135 | elem += IP_PORT_LEN; |
| 136 | |
| 137 | memcpy(ss, &sin6, sizeof(sin6)); |
| 138 | break; |
| 139 | default: |
| 140 | /* Malformed element! */ |
| 141 | return -EINVAL; |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | return (int)(elem - old_elem); |
| 146 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 147 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 148 | #endif /* HAVE_SYS_SOCKET_H */ |
| 149 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 150 | /* Helper function for gsm0808_enc_speech_codec() |
| 151 | * and gsm0808_enc_speech_codec_list() */ |
| 152 | static uint8_t enc_speech_codec(struct msgb *msg, |
| 153 | const struct gsm0808_speech_codec *sc) |
| 154 | { |
| 155 | /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
| 156 | uint8_t header = 0; |
| 157 | uint8_t *old_tail; |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 158 | bool type_extended; |
| 159 | |
| 160 | /* Note: Extended codec types are codec types that require 8 instead |
| 161 | * of 4 bit to fully specify the selected codec. In the following, |
| 162 | * we check if we work with an extended type or not. We also check |
| 163 | * if the codec type is valid at all. */ |
| 164 | switch(sc->type) { |
| 165 | case GSM0808_SCT_FR1: |
| 166 | case GSM0808_SCT_FR2: |
| 167 | case GSM0808_SCT_FR3: |
| 168 | case GSM0808_SCT_FR4: |
| 169 | case GSM0808_SCT_FR5: |
| 170 | case GSM0808_SCT_HR1: |
| 171 | case GSM0808_SCT_HR3: |
| 172 | case GSM0808_SCT_HR4: |
| 173 | case GSM0808_SCT_HR6: |
| 174 | type_extended = false; |
| 175 | break; |
| 176 | case GSM0808_SCT_CSD: |
| 177 | type_extended = true; |
| 178 | break; |
| 179 | default: |
| 180 | /* Invalid codec type specified */ |
| 181 | OSMO_ASSERT(false); |
| 182 | break; |
| 183 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 184 | |
| 185 | old_tail = msg->tail; |
| 186 | |
| 187 | if (sc->fi) |
| 188 | header |= (1 << 7); |
| 189 | if (sc->pi) |
| 190 | header |= (1 << 6); |
| 191 | if (sc->pt) |
| 192 | header |= (1 << 5); |
| 193 | if (sc->tf) |
| 194 | header |= (1 << 4); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 195 | |
| 196 | if (type_extended) { |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 197 | header |= 0x0f; |
| 198 | msgb_put_u8(msg, header); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 199 | msgb_put_u8(msg, sc->type); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 200 | } else { |
| 201 | OSMO_ASSERT(sc->type < 0x0f); |
| 202 | header |= sc->type; |
| 203 | msgb_put_u8(msg, header); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 206 | /* Note: Whether a configuration is present or not depends on the |
| 207 | * selected codec type. If present, it can either consist of one |
| 208 | * or two octets, depending on the codec type */ |
| 209 | switch (sc->type) { |
| 210 | case GSM0808_SCT_FR3: |
| 211 | case GSM0808_SCT_HR3: |
| 212 | case GSM0808_SCT_HR6: |
Philipp Maier | 7e27b14 | 2018-03-22 17:26:46 +0100 | [diff] [blame] | 213 | msgb_put_u16(msg, osmo_ntohs(sc->cfg)); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 214 | break; |
| 215 | case GSM0808_SCT_FR4: |
| 216 | case GSM0808_SCT_FR5: |
| 217 | case GSM0808_SCT_HR4: |
| 218 | case GSM0808_SCT_CSD: |
| 219 | OSMO_ASSERT((sc->cfg & 0xff00) == 0) |
| 220 | msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff); |
| 221 | break; |
| 222 | default: |
| 223 | OSMO_ASSERT(sc->cfg == 0); |
| 224 | break; |
| 225 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 226 | |
| 227 | return (uint8_t) (msg->tail - old_tail); |
| 228 | } |
| 229 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 230 | /*! Encode TS 08.08 Speech Codec IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 231 | * \param[out] msg Message Buffer to which IE will be appended |
| 232 | * \param[in] sc Speech Codec to be encoded into IE |
| 233 | * \returns number of bytes appended to \a msg */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 234 | uint8_t gsm0808_enc_speech_codec(struct msgb *msg, |
| 235 | const struct gsm0808_speech_codec *sc) |
| 236 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 237 | /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 238 | uint8_t *old_tail; |
| 239 | uint8_t *tlv_len; |
| 240 | |
| 241 | OSMO_ASSERT(msg); |
| 242 | OSMO_ASSERT(sc); |
| 243 | |
| 244 | msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC); |
| 245 | tlv_len = msgb_put(msg, 1); |
| 246 | old_tail = msg->tail; |
| 247 | |
| 248 | enc_speech_codec(msg, sc); |
| 249 | |
| 250 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 251 | return *tlv_len + 2; |
| 252 | } |
| 253 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 254 | /*! Decode TS 08.08 Speech Codec IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 255 | * \param[out] sc Caller-allocated memory for Speech Codec |
| 256 | * \param[in] elem IE value to be decoded |
| 257 | * \param[in] len Length of \a elem in bytes |
| 258 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 259 | int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc, |
| 260 | const uint8_t *elem, uint8_t len) |
| 261 | { |
| 262 | /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
| 263 | uint8_t header; |
| 264 | const uint8_t *old_elem = elem; |
| 265 | |
| 266 | OSMO_ASSERT(sc); |
| 267 | if (!elem) |
| 268 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 269 | if (len == 0) |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | |
| 272 | memset(sc, 0, sizeof(*sc)); |
| 273 | |
| 274 | header = *elem; |
| 275 | |
Philipp Maier | 85a6af2 | 2017-04-28 10:55:05 +0200 | [diff] [blame] | 276 | /* An extended codec type needs at least two fields, |
| 277 | * bail if the input data length is not sufficient. */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 278 | if ((header & 0x0F) == 0x0F && len < 2) |
| 279 | return -EINVAL; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 280 | |
| 281 | elem++; |
| 282 | len--; |
| 283 | |
| 284 | if (header & (1 << 7)) |
| 285 | sc->fi = true; |
| 286 | if (header & (1 << 6)) |
| 287 | sc->pi = true; |
| 288 | if (header & (1 << 5)) |
| 289 | sc->pt = true; |
| 290 | if (header & (1 << 4)) |
| 291 | sc->tf = true; |
| 292 | |
| 293 | if ((header & 0x0F) != 0x0F) { |
| 294 | sc->type = (header & 0x0F); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 295 | } else { |
| 296 | sc->type = *elem; |
| 297 | elem++; |
| 298 | len--; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 301 | /* Note: Whether a configuration is present or not depends on the |
| 302 | * selected codec type. If present, it can either consist of one or |
| 303 | * two octets depending on the codec type */ |
| 304 | switch (sc->type) { |
| 305 | case GSM0808_SCT_FR1: |
| 306 | case GSM0808_SCT_FR2: |
| 307 | case GSM0808_SCT_HR1: |
| 308 | break; |
| 309 | case GSM0808_SCT_HR4: |
| 310 | case GSM0808_SCT_CSD: |
| 311 | case GSM0808_SCT_FR4: |
| 312 | case GSM0808_SCT_FR5: |
| 313 | if (len < 1) |
| 314 | return -EINVAL; |
| 315 | sc->cfg = *elem; |
| 316 | elem++; |
| 317 | break; |
| 318 | case GSM0808_SCT_FR3: |
| 319 | case GSM0808_SCT_HR3: |
| 320 | case GSM0808_SCT_HR6: |
| 321 | if (len < 2) |
| 322 | return -EINVAL; |
Philipp Maier | 7e27b14 | 2018-03-22 17:26:46 +0100 | [diff] [blame] | 323 | sc->cfg = osmo_load16le(elem); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 324 | elem += 2; |
| 325 | break; |
| 326 | default: |
| 327 | /* Invalid codec type => malformed speech codec element! */ |
| 328 | return -EINVAL; |
| 329 | break; |
| 330 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 331 | |
| 332 | return (int)(elem - old_elem); |
| 333 | } |
| 334 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 335 | /*! Encode TS 08.08 Speech Codec list |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 336 | * \param[out] msg Message Buffer to which IE is to be appended |
| 337 | * \param[in] scl Speech Codec List to be encoded into IE |
| 338 | * \returns number of bytes added to \a msg */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 339 | uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg, |
| 340 | const struct gsm0808_speech_codec_list *scl) |
| 341 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 342 | /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 343 | uint8_t *old_tail; |
| 344 | uint8_t *tlv_len; |
| 345 | unsigned int i; |
| 346 | uint8_t rc; |
| 347 | unsigned int bytes_used = 0; |
| 348 | |
| 349 | OSMO_ASSERT(msg); |
| 350 | OSMO_ASSERT(scl); |
| 351 | |
| 352 | /* Empty list */ |
| 353 | OSMO_ASSERT(scl->len >= 1); |
| 354 | |
| 355 | msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST); |
| 356 | tlv_len = msgb_put(msg, 1); |
| 357 | old_tail = msg->tail; |
| 358 | |
| 359 | for (i = 0; i < scl->len; i++) { |
| 360 | rc = enc_speech_codec(msg, &scl->codec[i]); |
| 361 | OSMO_ASSERT(rc >= 1); |
| 362 | bytes_used += rc; |
| 363 | OSMO_ASSERT(bytes_used <= 255); |
| 364 | } |
| 365 | |
| 366 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 367 | return *tlv_len + 2; |
| 368 | } |
| 369 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 370 | /*! Decode TS 08.08 Speech Codec list IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 371 | * \param[out] scl Caller-provided memory to store codec list |
| 372 | * \param[in] elem IE value to be decoded |
| 373 | * \param[in] len Length of \a elem in bytes |
| 374 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 375 | int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl, |
| 376 | const uint8_t *elem, uint8_t len) |
| 377 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 378 | /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 379 | const uint8_t *old_elem = elem; |
| 380 | unsigned int i; |
| 381 | int rc; |
| 382 | uint8_t decoded = 0; |
| 383 | |
| 384 | OSMO_ASSERT(scl); |
| 385 | if (!elem) |
| 386 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 387 | if (len == 0) |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | |
| 390 | memset(scl, 0, sizeof(*scl)); |
| 391 | |
| 392 | for (i = 0; i < ARRAY_SIZE(scl->codec); i++) { |
| 393 | if (len <= 0) |
| 394 | break; |
| 395 | |
| 396 | rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len); |
| 397 | if (rc < 1) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | elem+=rc; |
| 401 | len -= rc; |
| 402 | decoded++; |
| 403 | } |
| 404 | |
| 405 | scl->len = decoded; |
| 406 | |
| 407 | /* Empty list */ |
| 408 | if (decoded < 1) { |
| 409 | return -EINVAL; |
| 410 | } |
| 411 | |
| 412 | return (int)(elem - old_elem); |
| 413 | } |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 414 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 415 | /*! Encode TS 08.08 Channel Type IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 416 | * \param[out] msg Message Buffer to which IE is to be appended |
| 417 | * \param[in] ct Channel Type to be encoded |
| 418 | * \returns number of bytes added to \a msg */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 419 | uint8_t gsm0808_enc_channel_type(struct msgb *msg, |
| 420 | const struct gsm0808_channel_type *ct) |
| 421 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 422 | /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 423 | unsigned int i; |
| 424 | uint8_t byte; |
| 425 | uint8_t *old_tail; |
| 426 | uint8_t *tlv_len; |
| 427 | |
| 428 | OSMO_ASSERT(msg); |
| 429 | OSMO_ASSERT(ct); |
| 430 | OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2); |
| 431 | |
| 432 | /* FIXME: Implement encoding support for Data |
| 433 | * and Speech + CTM Text Telephony */ |
| 434 | if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH |
| 435 | && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN) |
| 436 | OSMO_ASSERT(false); |
| 437 | |
| 438 | msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE); |
| 439 | tlv_len = msgb_put(msg, 1); |
| 440 | old_tail = msg->tail; |
| 441 | |
| 442 | msgb_put_u8(msg, ct->ch_indctr & 0x0f); |
| 443 | msgb_put_u8(msg, ct->ch_rate_type); |
| 444 | |
| 445 | for (i = 0; i < ct->perm_spch_len; i++) { |
| 446 | byte = ct->perm_spch[i]; |
| 447 | |
| 448 | if (i < ct->perm_spch_len - 1) |
| 449 | byte |= 0x80; |
| 450 | msgb_put_u8(msg, byte); |
| 451 | } |
| 452 | |
| 453 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 454 | return *tlv_len + 2; |
| 455 | } |
| 456 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 457 | /*! Decode TS 08.08 Channel Type IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 458 | * \param[out] ct Caller-provided memory to store channel type |
| 459 | * \param[in] elem IE Value to be decoded |
| 460 | * \param[in] len Length of \a elem in bytes |
| 461 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 462 | int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct, |
| 463 | const uint8_t *elem, uint8_t len) |
| 464 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 465 | /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 466 | unsigned int i; |
| 467 | uint8_t byte; |
| 468 | const uint8_t *old_elem = elem; |
| 469 | |
| 470 | OSMO_ASSERT(ct); |
| 471 | if (!elem) |
| 472 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 473 | if (len < 3 || len > 11) |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 474 | return -EINVAL; |
| 475 | |
| 476 | memset(ct, 0, sizeof(*ct)); |
| 477 | |
| 478 | ct->ch_indctr = (*elem) & 0x0f; |
| 479 | elem++; |
| 480 | ct->ch_rate_type = (*elem) & 0x0f; |
| 481 | elem++; |
| 482 | |
| 483 | for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) { |
| 484 | byte = *elem; |
| 485 | elem++; |
| 486 | ct->perm_spch[i] = byte & 0x7f; |
| 487 | if ((byte & 0x80) == 0x00) |
| 488 | break; |
| 489 | } |
| 490 | ct->perm_spch_len = i + 1; |
| 491 | |
| 492 | return (int)(elem - old_elem); |
| 493 | } |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 494 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 495 | /*! Encode TS 08.08 Encryption Information IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 496 | * \param[out] msg Message Buffer to which IE is to be appended |
| 497 | * \param[in] ei Encryption Information to be encoded |
| 498 | * \returns number of bytes appended to \a msg */ |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 499 | uint8_t gsm0808_enc_encrypt_info(struct msgb *msg, |
| 500 | const struct gsm0808_encrypt_info *ei) |
| 501 | { |
| 502 | unsigned int i; |
| 503 | uint8_t perm_algo = 0; |
| 504 | uint8_t *ptr; |
| 505 | uint8_t *old_tail; |
| 506 | uint8_t *tlv_len; |
| 507 | |
| 508 | OSMO_ASSERT(msg); |
| 509 | OSMO_ASSERT(ei); |
| 510 | OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key)); |
| 511 | OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN); |
| 512 | |
| 513 | msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION); |
| 514 | tlv_len = msgb_put(msg, 1); |
| 515 | old_tail = msg->tail; |
| 516 | |
| 517 | for (i = 0; i < ei->perm_algo_len; i++) { |
| 518 | /* Note: gsm_08_08.h defines the permitted algorithms |
| 519 | * as an enum which ranges from 0x01 to 0x08 */ |
| 520 | OSMO_ASSERT(ei->perm_algo[i] != 0); |
| 521 | OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN); |
| 522 | perm_algo |= (1 << (ei->perm_algo[i] - 1)); |
| 523 | } |
| 524 | |
| 525 | msgb_put_u8(msg, perm_algo); |
| 526 | ptr = msgb_put(msg, ei->key_len); |
| 527 | memcpy(ptr, ei->key, ei->key_len); |
| 528 | |
| 529 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 530 | return *tlv_len + 2; |
| 531 | } |
| 532 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 533 | /*! Decode TS 08.08 Encryption Information IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 534 | * \param[out] ei Caller-provided memory to store encryption information |
| 535 | * \param[in] elem IE value to be decoded |
| 536 | * \param[in] len Length of \a elem in bytes |
| 537 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 538 | int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei, |
| 539 | const uint8_t *elem, uint8_t len) |
| 540 | { |
| 541 | uint8_t perm_algo; |
| 542 | unsigned int i; |
| 543 | unsigned int perm_algo_len = 0; |
| 544 | const uint8_t *old_elem = elem; |
| 545 | |
| 546 | OSMO_ASSERT(ei); |
| 547 | if (!elem) |
| 548 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 549 | if (len == 0) |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 550 | return -EINVAL; |
| 551 | |
| 552 | memset(ei, 0, sizeof(*ei)); |
| 553 | |
| 554 | perm_algo = *elem; |
| 555 | elem++; |
| 556 | |
| 557 | for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) { |
| 558 | if (perm_algo & (1 << i)) { |
| 559 | ei->perm_algo[perm_algo_len] = i + 1; |
| 560 | perm_algo_len++; |
| 561 | } |
| 562 | } |
| 563 | ei->perm_algo_len = perm_algo_len; |
| 564 | |
| 565 | ei->key_len = len - 1; |
| 566 | memcpy(ei->key, elem, ei->key_len); |
| 567 | elem+=ei->key_len; |
| 568 | |
| 569 | return (int)(elem - old_elem); |
| 570 | } |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 571 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 572 | /*! Encode TS 08.08 Cell Identifier List IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 573 | * \param[out] msg Message Buffer to which IE is to be appended |
| 574 | * \param[in] cil Cell ID List to be encoded |
| 575 | * \returns number of bytes appended to \a msg */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 576 | uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, |
| 577 | const struct gsm0808_cell_id_list2 *cil) |
| 578 | { |
| 579 | uint8_t *old_tail; |
| 580 | uint8_t *tlv_len; |
| 581 | unsigned int i; |
| 582 | |
| 583 | OSMO_ASSERT(msg); |
| 584 | OSMO_ASSERT(cil); |
| 585 | |
| 586 | msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 587 | tlv_len = msgb_put(msg, 1); |
| 588 | old_tail = msg->tail; |
| 589 | |
| 590 | msgb_put_u8(msg, cil->id_discr & 0x0f); |
| 591 | |
| 592 | OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 593 | switch (cil->id_discr) { |
| 594 | case CELL_IDENT_WHOLE_GLOBAL: |
| 595 | for (i = 0; i < cil->id_list_len; i++) { |
| 596 | const struct osmo_cell_global_id *id = &cil->id_list[i].global; |
| 597 | struct gsm48_loc_area_id lai; |
Neels Hofmeyr | 8b8cd93 | 2018-03-23 01:47:37 +0100 | [diff] [blame] | 598 | gsm48_generate_lai2(&lai, &id->lai); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 599 | memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai)); |
| 600 | msgb_put_u16(msg, id->cell_identity); |
| 601 | } |
| 602 | break; |
| 603 | case CELL_IDENT_LAC_AND_CI: |
| 604 | for (i = 0; i < cil->id_list_len; i++) { |
| 605 | const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci; |
| 606 | msgb_put_u16(msg, id->lac); |
| 607 | msgb_put_u16(msg, id->ci); |
| 608 | } |
| 609 | break; |
| 610 | case CELL_IDENT_CI: |
| 611 | for (i = 0; i < cil->id_list_len; i++) |
| 612 | msgb_put_u16(msg, cil->id_list[i].ci); |
| 613 | break; |
| 614 | case CELL_IDENT_LAI_AND_LAC: |
| 615 | for (i = 0; i < cil->id_list_len; i++) { |
| 616 | const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac; |
| 617 | struct gsm48_loc_area_id lai; |
Neels Hofmeyr | 8b8cd93 | 2018-03-23 01:47:37 +0100 | [diff] [blame] | 618 | gsm48_generate_lai2(&lai, id); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 619 | memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai)); |
| 620 | } |
| 621 | break; |
| 622 | case CELL_IDENT_LAC: |
| 623 | for (i = 0; i < cil->id_list_len; i++) |
| 624 | msgb_put_u16(msg, cil->id_list[i].lac); |
| 625 | break; |
| 626 | case CELL_IDENT_BSS: |
| 627 | case CELL_IDENT_NO_CELL: |
| 628 | /* Does not have any list items */ |
| 629 | break; |
| 630 | default: |
| 631 | /* Support for other identifier list types is not implemented. */ |
| 632 | OSMO_ASSERT(false); |
| 633 | } |
| 634 | |
| 635 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 636 | return *tlv_len + 2; |
| 637 | } |
| 638 | |
| 639 | /*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead. |
| 640 | * |
| 641 | * Encode TS 08.08 Cell Identifier List IE |
| 642 | * \param[out] msg Message Buffer to which IE is to be appended |
| 643 | * \param[in] cil Cell ID List to be encoded |
| 644 | * \returns number of bytes appended to \a msg */ |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 645 | uint8_t gsm0808_enc_cell_id_list(struct msgb *msg, |
| 646 | const struct gsm0808_cell_id_list *cil) |
| 647 | { |
| 648 | uint8_t *old_tail; |
| 649 | uint8_t *tlv_len; |
| 650 | unsigned int i; |
| 651 | |
| 652 | OSMO_ASSERT(msg); |
| 653 | OSMO_ASSERT(cil); |
| 654 | |
| 655 | msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 656 | tlv_len = msgb_put(msg, 1); |
| 657 | old_tail = msg->tail; |
| 658 | |
| 659 | msgb_put_u8(msg, cil->id_discr & 0x0f); |
| 660 | |
| 661 | switch (cil->id_discr) { |
| 662 | case CELL_IDENT_LAC: |
| 663 | OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN) |
| 664 | for (i=0;i<cil->id_list_len;i++) { |
| 665 | msgb_put_u16(msg, cil->id_list_lac[i]); |
| 666 | } |
| 667 | break; |
| 668 | case CELL_IDENT_BSS: |
| 669 | /* Does not have any list items */ |
| 670 | break; |
| 671 | default: |
| 672 | /* FIXME: Implement support for all identifier list elements */ |
| 673 | OSMO_ASSERT(false); |
| 674 | } |
| 675 | |
| 676 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 677 | return *tlv_len + 2; |
| 678 | } |
| 679 | |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 680 | /* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */ |
| 681 | static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded) |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 682 | { |
| 683 | struct gsm48_loc_area_id lai; |
| 684 | |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 685 | /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 686 | memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */ |
| 687 | |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 688 | gsm48_decode_lai2(&lai, decoded); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 691 | static int parse_cell_id_global_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 692 | size_t *consumed) |
| 693 | { |
| 694 | struct osmo_cell_global_id *id; |
| 695 | uint16_t *ci_be; |
| 696 | size_t lai_offset; |
| 697 | int i = 0; |
| 698 | const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be); |
| 699 | |
| 700 | *consumed = 0; |
| 701 | while (remain >= elemlen) { |
| 702 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 703 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 704 | id = &cil->id_list[i].global; |
Stefan Sperling | 2873bf1 | 2018-03-14 18:38:41 +0100 | [diff] [blame] | 705 | lai_offset = i * elemlen; |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 706 | decode_lai(&data[lai_offset], &id->lai); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 707 | ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]); |
| 708 | id->cell_identity = osmo_load16be(ci_be); |
| 709 | *consumed += elemlen; |
| 710 | remain -= elemlen; |
| 711 | i++; |
| 712 | } |
| 713 | |
| 714 | return i; |
| 715 | } |
| 716 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 717 | static int parse_cell_id_lac_and_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 718 | size_t *consumed) |
| 719 | { |
| 720 | uint16_t *lacp_be, *ci_be; |
| 721 | struct osmo_lac_and_ci_id *id; |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 722 | int i = 0, j = 0; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 723 | const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be); |
| 724 | |
| 725 | *consumed = 0; |
| 726 | |
| 727 | if (remain < elemlen) |
| 728 | return -EINVAL; |
| 729 | |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 730 | lacp_be = (uint16_t *)(&data[j]); |
| 731 | ci_be = (uint16_t *)(&data[j + elemlen/2]); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 732 | while (remain >= elemlen) { |
| 733 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 734 | return -ENOSPC; |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 735 | id = &cil->id_list[i++].lac_and_ci; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 736 | id->lac = osmo_load16be(lacp_be); |
| 737 | id->ci = osmo_load16be(ci_be); |
| 738 | *consumed += elemlen; |
| 739 | remain -= elemlen; |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 740 | j += elemlen; |
| 741 | lacp_be = (uint16_t *)(&data[j]); |
| 742 | ci_be = (uint16_t *)(&data[j + elemlen/2]); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | return i; |
| 746 | } |
| 747 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 748 | static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
| 749 | size_t *consumed) |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 750 | { |
| 751 | const uint16_t *ci_be = (const uint16_t *)data; |
| 752 | int i = 0; |
| 753 | const size_t elemlen = sizeof(*ci_be); |
| 754 | |
| 755 | *consumed = 0; |
| 756 | while (remain >= elemlen) { |
| 757 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 758 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 759 | cil->id_list[i++].ci = osmo_load16be(ci_be++); |
Stefan Sperling | 9c62fc6 | 2018-03-16 10:23:34 +0100 | [diff] [blame] | 760 | *consumed += elemlen; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 761 | remain -= elemlen; |
| 762 | } |
| 763 | return i; |
| 764 | } |
| 765 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 766 | static int parse_cell_id_lai_and_lac(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 767 | size_t *consumed) |
| 768 | { |
| 769 | struct osmo_location_area_id *id; |
| 770 | int i = 0; |
| 771 | const size_t elemlen = sizeof(struct gsm48_loc_area_id); |
| 772 | |
| 773 | *consumed = 0; |
| 774 | while (remain >= elemlen) { |
| 775 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 776 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 777 | id = &cil->id_list[i].lai_and_lac; |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 778 | decode_lai(&data[i * elemlen], id); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 779 | *consumed += elemlen; |
| 780 | remain -= elemlen; |
| 781 | i++; |
| 782 | } |
| 783 | |
| 784 | return i; |
| 785 | } |
| 786 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 787 | static int parse_cell_id_lac_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed) |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 788 | { |
| 789 | const uint16_t *lac_be = (const uint16_t *)data; |
| 790 | int i = 0; |
| 791 | const size_t elemlen = sizeof(*lac_be); |
| 792 | |
| 793 | *consumed = 0; |
| 794 | while (remain >= elemlen) { |
| 795 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 796 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 797 | cil->id_list[i++].lac = osmo_load16be(lac_be++); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 798 | *consumed += elemlen; |
| 799 | remain -= elemlen; |
| 800 | } |
| 801 | return i; |
| 802 | } |
| 803 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 804 | /*! Decode Cell Identifier List IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 805 | * \param[out] cil Caller-provided memory to store Cell ID list |
| 806 | * \param[in] elem IE value to be decoded |
| 807 | * \param[in] len Length of \a elem in bytes |
| 808 | * \returns number of bytes parsed; negative on error */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 809 | int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil, |
| 810 | const uint8_t *elem, uint8_t len) |
| 811 | { |
| 812 | uint8_t id_discr; |
| 813 | size_t bytes_elem = 0; |
| 814 | int list_len = 0; |
| 815 | |
| 816 | OSMO_ASSERT(cil); |
| 817 | if (!elem) |
| 818 | return -EINVAL; |
| 819 | if (len == 0) |
| 820 | return -EINVAL; |
| 821 | |
| 822 | memset(cil, 0, sizeof(*cil)); |
| 823 | |
| 824 | id_discr = *elem & 0x0f; |
| 825 | elem++; |
| 826 | len--; |
| 827 | |
| 828 | switch (id_discr) { |
| 829 | case CELL_IDENT_WHOLE_GLOBAL: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 830 | list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 831 | break; |
| 832 | case CELL_IDENT_LAC_AND_CI: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 833 | list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 834 | break; |
| 835 | case CELL_IDENT_CI: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 836 | list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 837 | break; |
| 838 | case CELL_IDENT_LAI_AND_LAC: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 839 | list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 840 | break; |
| 841 | case CELL_IDENT_LAC: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 842 | list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 843 | break; |
| 844 | case CELL_IDENT_BSS: |
| 845 | case CELL_IDENT_NO_CELL: |
| 846 | /* Does not have any list items */ |
| 847 | break; |
| 848 | default: |
| 849 | /* Remaining cell identification types are not implemented. */ |
| 850 | return -EINVAL; |
| 851 | } |
| 852 | |
| 853 | if (list_len < 0) /* parsing error */ |
| 854 | return list_len; |
| 855 | |
| 856 | cil->id_discr = id_discr; |
| 857 | cil->id_list_len = list_len; |
| 858 | |
| 859 | /* One byte for the cell ID discriminator + any remaining bytes in |
| 860 | * the IE which were consumed by the parser functions above. */ |
| 861 | return 1 + (int)bytes_elem; |
| 862 | } |
| 863 | |
| 864 | /*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead. |
| 865 | * |
| 866 | * Decode Cell Identifier List IE |
| 867 | * \param[out] cil Caller-provided memory to store Cell ID list |
| 868 | * \param[in] elem IE value to be decoded |
| 869 | * \param[in] len Length of \a elem in bytes |
| 870 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 871 | int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil, |
| 872 | const uint8_t *elem, uint8_t len) |
| 873 | { |
| 874 | uint8_t id_discr; |
| 875 | const uint8_t *old_elem = elem; |
| 876 | unsigned int item_count = 0; |
| 877 | |
| 878 | OSMO_ASSERT(cil); |
| 879 | if (!elem) |
| 880 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 881 | if (len == 0) |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | |
| 884 | memset(cil, 0, sizeof(*cil)); |
| 885 | |
| 886 | id_discr = *elem & 0x0f; |
| 887 | elem++; |
| 888 | len--; |
| 889 | |
| 890 | cil->id_discr = id_discr; |
| 891 | |
| 892 | switch (id_discr) { |
| 893 | case CELL_IDENT_LAC: |
| 894 | while (len >= 2) { |
| 895 | cil->id_list_lac[item_count] = osmo_load16be(elem); |
| 896 | elem += 2; |
| 897 | item_count++; |
| 898 | len -= 2; |
| 899 | } |
| 900 | case CELL_IDENT_BSS: |
| 901 | /* Does not have any list items */ |
| 902 | break; |
| 903 | default: |
| 904 | /* FIXME: Implement support for all identifier list elements */ |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | |
| 908 | cil->id_list_len = item_count; |
| 909 | return (int)(elem - old_elem); |
| 910 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 911 | |
Neels Hofmeyr | 74663d9 | 2018-03-23 01:46:42 +0100 | [diff] [blame] | 912 | static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai, |
| 913 | const struct gsm0808_cell_id_list2 *b, int bi) |
| 914 | { |
| 915 | struct gsm0808_cell_id_list2 tmp = { |
| 916 | .id_discr = a->id_discr, |
| 917 | .id_list_len = 1, |
| 918 | }; |
| 919 | uint8_t buf_a[32 + sizeof(struct msgb)]; |
| 920 | uint8_t buf_b[32 + sizeof(struct msgb)]; |
| 921 | struct msgb *msg_a = (void*)buf_a; |
| 922 | struct msgb *msg_b = (void*)buf_b; |
| 923 | |
| 924 | msg_a->data_len = 32; |
| 925 | msg_b->data_len = 32; |
| 926 | msgb_reset(msg_a); |
| 927 | msgb_reset(msg_b); |
| 928 | |
| 929 | if (a->id_discr != b->id_discr) |
| 930 | return false; |
| 931 | if (ai >= a->id_list_len |
| 932 | || bi >= b->id_list_len) |
| 933 | return false; |
| 934 | |
| 935 | tmp.id_list[0] = a->id_list[ai]; |
| 936 | gsm0808_enc_cell_id_list2(msg_a, &tmp); |
| 937 | |
| 938 | tmp.id_list[0] = b->id_list[bi]; |
| 939 | gsm0808_enc_cell_id_list2(msg_b, &tmp); |
| 940 | |
| 941 | if (msg_a->len != msg_b->len) |
| 942 | return false; |
| 943 | if (memcmp(msg_a->data, msg_b->data, msg_a->len)) |
| 944 | return false; |
| 945 | |
| 946 | return true; |
| 947 | } |
| 948 | |
| 949 | /*! Append entries from one Cell Identifier List to another. |
| 950 | * The cell identifier types must be identical between the two lists. |
| 951 | * \param dst[out] Append entries to this list. |
| 952 | * \param src[in] Append these entries to \a dst. |
| 953 | * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch |
| 954 | * between the lists, -ENOSPC if the destination list does not have enough space. If an error is |
| 955 | * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value |
| 956 | * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist |
| 957 | * in \a dst, and does not indicate error per se. */ |
| 958 | int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src) |
| 959 | { |
| 960 | int i, j; |
| 961 | int added = 0; |
| 962 | |
| 963 | if (dst->id_list_len == 0 |
| 964 | && dst->id_discr != CELL_IDENT_BSS) |
| 965 | dst->id_discr = src->id_discr; |
| 966 | else if (dst->id_discr != src->id_discr) |
| 967 | return -EINVAL; |
| 968 | |
| 969 | for (i = 0; i < src->id_list_len; i++) { |
| 970 | /* don't add duplicate entries */ |
| 971 | bool skip = false; |
| 972 | for (j = 0; j < dst->id_list_len; j++) { |
| 973 | if (same_cell_id_list_entries(dst, j, src, i)) { |
| 974 | skip = true; |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | if (skip) |
| 979 | continue; |
| 980 | |
| 981 | if (dst->id_list_len >= ARRAY_SIZE(dst->id_list)) |
| 982 | return -ENOSPC; |
| 983 | |
| 984 | dst->id_list[dst->id_list_len++] = src->id_list[i]; |
| 985 | added ++; |
| 986 | } |
| 987 | |
| 988 | return added; |
| 989 | } |
| 990 | |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 991 | /*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17). |
| 992 | * \param[out] msg Message Buffer to which IE is to be appended |
| 993 | * \param[in] ci Cell ID to be encoded |
| 994 | * \returns number of bytes appended to \a msg */ |
| 995 | uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci) |
| 996 | { |
| 997 | uint8_t rc; |
| 998 | uint8_t *ie_tag; |
| 999 | struct gsm0808_cell_id_list2 cil = { |
| 1000 | .id_discr = ci->id_discr, |
| 1001 | .id_list = { ci->id }, |
| 1002 | .id_list_len = 1, |
| 1003 | }; |
| 1004 | |
| 1005 | OSMO_ASSERT(msg); |
| 1006 | OSMO_ASSERT(ci); |
| 1007 | |
| 1008 | ie_tag = msg->tail; |
| 1009 | rc = gsm0808_enc_cell_id_list2(msg, &cil); |
| 1010 | |
| 1011 | if (rc <= 0) |
| 1012 | return rc; |
| 1013 | |
| 1014 | *ie_tag = GSM0808_IE_CELL_IDENTIFIER; |
| 1015 | return rc; |
| 1016 | } |
| 1017 | |
| 1018 | /*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17). |
| 1019 | * \param[out] ci Caller-provided memory to store Cell ID. |
| 1020 | * \param[in] elem IE value to be decoded. |
| 1021 | * \param[in] len Length of \a elem in bytes. |
| 1022 | * \returns number of bytes parsed; negative on error */ |
| 1023 | int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len) |
| 1024 | { |
| 1025 | struct gsm0808_cell_id_list2 cil; |
| 1026 | int rc; |
| 1027 | rc = gsm0808_dec_cell_id_list2(&cil, elem, len); |
| 1028 | if (rc < 0) |
| 1029 | return rc; |
| 1030 | if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) { |
| 1031 | if (cil.id_list_len != 0) |
| 1032 | return -EINVAL; |
| 1033 | } else { |
| 1034 | if (cil.id_list_len != 1) |
| 1035 | return -EINVAL; |
| 1036 | } |
| 1037 | ci->id_discr = cil.id_discr; |
| 1038 | ci->id = cil.id_list[0]; |
| 1039 | return rc; |
| 1040 | } |
| 1041 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1042 | /*! Convert the representation of the permitted speech codec identifier |
Philipp Maier | 3149b0d | 2017-06-02 13:22:34 +0200 | [diff] [blame] | 1043 | * that is used in struct gsm0808_channel_type to the speech codec |
| 1044 | * representation we use in struct gsm0808_speech_codec. |
| 1045 | * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech) |
| 1046 | * \returns GSM speech codec type; negative on error */ |
| 1047 | int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch) |
| 1048 | { |
| 1049 | /*! The speech codec type, which is used in the channel type field to |
| 1050 | * signal the permitted speech versions (codecs) has a different |
| 1051 | * encoding than the type field in the speech codec type element |
| 1052 | * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */ |
| 1053 | |
| 1054 | switch (perm_spch) { |
| 1055 | case GSM0808_PERM_FR1: |
| 1056 | return GSM0808_SCT_FR1; |
| 1057 | case GSM0808_PERM_FR2: |
| 1058 | return GSM0808_SCT_FR2; |
| 1059 | case GSM0808_PERM_FR3: |
| 1060 | return GSM0808_SCT_FR3; |
| 1061 | case GSM0808_PERM_FR4: |
| 1062 | return GSM0808_SCT_FR4; |
| 1063 | case GSM0808_PERM_FR5: |
| 1064 | return GSM0808_SCT_FR5; |
| 1065 | case GSM0808_PERM_HR1: |
| 1066 | return GSM0808_SCT_HR1; |
| 1067 | case GSM0808_PERM_HR3: |
| 1068 | return GSM0808_SCT_HR3; |
| 1069 | case GSM0808_PERM_HR4: |
| 1070 | return GSM0808_SCT_HR4; |
| 1071 | case GSM0808_PERM_HR6: |
| 1072 | return GSM0808_SCT_HR6; |
| 1073 | } |
| 1074 | |
| 1075 | /* Invalid input */ |
| 1076 | return -EINVAL; |
| 1077 | } |
| 1078 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1079 | /*! Extrapolate a speech codec field from a given permitted speech |
Philipp Maier | 884ba0f | 2017-06-02 13:49:16 +0200 | [diff] [blame] | 1080 | * parameter (channel type). |
| 1081 | * \param[out] sc Caller provided memory to store the resulting speech codec |
| 1082 | * \param[in] perm_spch value that is used to derive the speech codec info |
| 1083 | * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h) |
| 1084 | * \returns zero when successful; negative on error */ |
| 1085 | int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc, |
| 1086 | uint8_t perm_spch) |
| 1087 | { |
| 1088 | int rc; |
| 1089 | |
| 1090 | memset(sc, 0, sizeof(*sc)); |
| 1091 | |
| 1092 | /* Determine codec type */ |
| 1093 | rc = gsm0808_chan_type_to_speech_codec(perm_spch); |
| 1094 | if (rc < 0) |
| 1095 | return -EINVAL; |
| 1096 | sc->type = (uint8_t) rc; |
| 1097 | |
| 1098 | /* Depending on the speech codec type, pick a default codec |
| 1099 | * configuration that exactly matches the configuration on the |
| 1100 | * air interface. */ |
| 1101 | switch (sc->type) { |
| 1102 | case GSM0808_SCT_FR3: |
| 1103 | sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR; |
| 1104 | break; |
| 1105 | case GSM0808_SCT_FR4: |
| 1106 | sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB; |
| 1107 | break; |
| 1108 | case GSM0808_SCT_FR5: |
| 1109 | sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB; |
| 1110 | break; |
| 1111 | case GSM0808_SCT_HR3: |
| 1112 | sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR; |
| 1113 | break; |
| 1114 | case GSM0808_SCT_HR4: |
| 1115 | sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB; |
| 1116 | break; |
| 1117 | case GSM0808_SCT_HR6: |
| 1118 | sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR; |
| 1119 | break; |
| 1120 | default: |
| 1121 | /* Note: Not all codec types specify a default setting, |
| 1122 | * in this case, we just set the field to zero. */ |
| 1123 | sc->cfg = 0; |
| 1124 | } |
| 1125 | |
| 1126 | /* Tag all codecs as "Full IP" |
| 1127 | * (see als 3GPP TS 48.008 3.2.2.103) */ |
| 1128 | sc->fi = true; |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 1133 | /*! Print a human readable name of the cell identifier to the char buffer. |
| 1134 | * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2. |
| 1135 | * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name(). |
| 1136 | * \param[out] buf Destination buffer to write string representation to. |
| 1137 | * \param[in] buflen Amount of memory available in \a buf. |
| 1138 | * \param[in] id_discr Cell Identifier type. |
| 1139 | * \param[in] u Cell Identifer value. |
| 1140 | * \returns Like snprintf(): the amount of characters (excluding terminating nul) written, |
| 1141 | * or that would have been written if the buffer were large enough. |
| 1142 | */ |
| 1143 | int gsm0808_cell_id_u_name(char *buf, size_t buflen, |
| 1144 | enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u) |
| 1145 | { |
| 1146 | switch (id_discr) { |
| 1147 | case CELL_IDENT_LAC: |
| 1148 | return snprintf(buf, buflen, "%u", u->lac); |
| 1149 | case CELL_IDENT_CI: |
| 1150 | return snprintf(buf, buflen, "%u", u->ci); |
| 1151 | case CELL_IDENT_LAC_AND_CI: |
| 1152 | return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci); |
| 1153 | case CELL_IDENT_LAI_AND_LAC: |
| 1154 | return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac)); |
| 1155 | case CELL_IDENT_WHOLE_GLOBAL: |
| 1156 | return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global)); |
| 1157 | default: |
| 1158 | /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator. |
| 1159 | * Same for kinds we have no string representation of yet. */ |
| 1160 | return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr)); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /*! value_string[] for enum CELL_IDENT. */ |
| 1165 | const struct value_string gsm0808_cell_id_discr_names[] = { |
| 1166 | { CELL_IDENT_WHOLE_GLOBAL, "CGI" }, |
| 1167 | { CELL_IDENT_LAC_AND_CI, "LAC-CI" }, |
| 1168 | { CELL_IDENT_CI, "CI" }, |
| 1169 | { CELL_IDENT_NO_CELL, "NO-CELL" }, |
| 1170 | { CELL_IDENT_LAI_AND_LAC, "LAI" }, |
| 1171 | { CELL_IDENT_LAC, "LAC" }, |
| 1172 | { CELL_IDENT_BSS, "BSS" }, |
| 1173 | { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" }, |
| 1174 | { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" }, |
| 1175 | { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" }, |
| 1176 | { 0, NULL } |
| 1177 | }; |
| 1178 | |
| 1179 | #define APPEND_THING(func, args...) do { \ |
| 1180 | int remain = buflen - (pos - buf); \ |
| 1181 | int l = func(pos, remain, ##args); \ |
| 1182 | if (l < 0 || l > remain) \ |
| 1183 | pos = buf + buflen; \ |
| 1184 | else \ |
| 1185 | pos += l; \ |
| 1186 | if (l > 0) \ |
| 1187 | total_len += l; \ |
| 1188 | } while(0) |
| 1189 | #define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args) |
| 1190 | #define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U) |
| 1191 | |
| 1192 | static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid, |
| 1193 | char *buf, size_t buflen) |
| 1194 | { |
| 1195 | char *pos = buf; |
| 1196 | int total_len = 0; |
| 1197 | APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr)); |
| 1198 | APPEND_CELL_ID_U(cid->id_discr, &cid->id); |
| 1199 | return buf; |
| 1200 | } |
| 1201 | |
| 1202 | /*! Return a human readable representation of a Cell Identifier, like "LAC:123" |
| 1203 | * or "CGI:001-01-42-23". |
| 1204 | * \param[in] cid Cell Identifer. |
| 1205 | * \returns String in a static buffer. |
| 1206 | */ |
| 1207 | const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid) |
| 1208 | { |
| 1209 | static char buf[64]; |
| 1210 | return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf)); |
| 1211 | } |
| 1212 | |
| 1213 | /*! Like gsm0808_cell_id_name() but uses a different static buffer. |
| 1214 | * \param[in] cid Cell Identifer. |
| 1215 | * \returns String in a static buffer. |
| 1216 | */ |
| 1217 | const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid) |
| 1218 | { |
| 1219 | static char buf[64]; |
| 1220 | return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf)); |
| 1221 | } |
| 1222 | |
| 1223 | /*! Return a human readable representation of the Cell Identifier List, like |
| 1224 | * "LAC[2]:{123, 456}". |
| 1225 | * The return value semantics are like snprintf() and thus allow ensuring a complete |
| 1226 | * untruncated string by determining the required string length from the return value. |
| 1227 | * If buflen > 0, always nul-terminate the string in buf, also when it is truncated. |
| 1228 | * If buflen == 0, do not modify buf, just return the would-be length. |
| 1229 | * \param[out] buf Destination buffer to write string representation to. |
| 1230 | * \param[in] buflen Amount of memory available in \a buf. |
| 1231 | * \param[in] cil Cell Identifer List. |
| 1232 | * \returns Like snprintf(): the amount of characters (excluding terminating nul) written, |
| 1233 | * or that would have been written if the buffer were large enough. |
| 1234 | */ |
| 1235 | int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil) |
| 1236 | { |
| 1237 | char *pos = buf; |
| 1238 | int total_len = 0; |
| 1239 | int i; |
| 1240 | |
| 1241 | APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len); |
| 1242 | |
| 1243 | switch (cil->id_discr) { |
| 1244 | case CELL_IDENT_BSS: |
| 1245 | case CELL_IDENT_NO_CELL: |
| 1246 | return total_len; |
| 1247 | default: |
| 1248 | break; |
| 1249 | } |
| 1250 | |
| 1251 | APPEND_STR(":{"); |
| 1252 | |
| 1253 | for (i = 0; i < cil->id_list_len; i++) { |
| 1254 | if (i) |
| 1255 | APPEND_STR(", "); |
| 1256 | APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]); |
| 1257 | } |
| 1258 | |
| 1259 | APPEND_STR("}"); |
| 1260 | return total_len; |
| 1261 | } |
| 1262 | |
| 1263 | /*! Return a human-readable representation of \a cil in a static buffer. |
| 1264 | * If the list is too long, the output may be truncated. |
| 1265 | * See also gsm0808_cell_id_list_name_buf(). */ |
| 1266 | const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil) |
| 1267 | { |
| 1268 | static char buf[1024]; |
| 1269 | gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil); |
| 1270 | return buf; |
| 1271 | } |
| 1272 | |
| 1273 | #undef APPEND_STR |
| 1274 | #undef APPEND_CELL_ID_U |
| 1275 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 1276 | /*! @} */ |