Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 1 | /* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 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 |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 22 | |
| 23 | |
Harald Welte | 0eaa0ef | 2019-03-02 15:12:42 +0100 | [diff] [blame] | 24 | #include <netinet/in.h> |
| 25 | #include <arpa/inet.h> |
| 26 | |
Kévin Redon | ff5db6e | 2018-10-11 17:16:18 +0200 | [diff] [blame] | 27 | #include <asn_application.h> |
| 28 | #include <der_encoder.h> |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 29 | |
Harald Welte | b49ac9c | 2019-03-09 20:36:07 +0100 | [diff] [blame] | 30 | #include "asn1c_helpers.h" |
| 31 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/rspro/RsproPDU.h> |
| 34 | |
Harald Welte | 137c440 | 2018-08-17 21:25:56 +0200 | [diff] [blame] | 35 | #include "rspro_util.h" |
| 36 | |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 37 | #define ASN_ALLOC_COPY(out, in) \ |
| 38 | do { \ |
| 39 | if (in) { \ |
| 40 | out = CALLOC(1, sizeof(*in)); \ |
| 41 | OSMO_ASSERT(out); \ |
| 42 | memcpy(out, in, sizeof(*in)); \ |
| 43 | } \ |
| 44 | } while (0) |
| 45 | |
| 46 | |
Harald Welte | b49ac9c | 2019-03-09 20:36:07 +0100 | [diff] [blame] | 47 | const char *rspro_msgt_name(const RsproPDU_t *pdu) |
| 48 | { |
| 49 | return asn_choice_name(&asn_DEF_RsproPDUchoice, &pdu->msg); |
| 50 | } |
| 51 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 52 | struct msgb *rspro_msgb_alloc(void) |
| 53 | { |
| 54 | return msgb_alloc_headroom(1024, 8, "RSPRO"); |
| 55 | } |
| 56 | |
| 57 | /*! BER-Encode an RSPRO message into msgb. |
| 58 | * \param[in] pdu Structure describing RSPRO PDU. Is freed by this function on success |
| 59 | * \returns callee-allocated message buffer containing encoded RSPRO PDU; NULL on error. |
| 60 | */ |
| 61 | struct msgb *rspro_enc_msg(RsproPDU_t *pdu) |
| 62 | { |
| 63 | struct msgb *msg = rspro_msgb_alloc(); |
| 64 | asn_enc_rval_t rval; |
| 65 | |
| 66 | if (!msg) |
| 67 | return NULL; |
| 68 | |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 69 | msg->l2h = msg->data; |
Harald Welte | 6b8d4f8 | 2018-08-17 22:06:24 +0200 | [diff] [blame] | 70 | rval = der_encode_to_buffer(&asn_DEF_RsproPDU, pdu, msgb_data(msg), msgb_tailroom(msg)); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 71 | if (rval.encoded < 0) { |
Harald Welte | a2b23c3 | 2018-08-17 22:09:06 +0200 | [diff] [blame] | 72 | fprintf(stderr, "Failed to encode %s\n", rval.failed_type->name); |
Harald Welte | 703d686 | 2018-09-24 17:44:50 +0200 | [diff] [blame] | 73 | msgb_free(msg); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
Harald Welte | d5c5c0b | 2018-08-17 22:04:01 +0200 | [diff] [blame] | 76 | msgb_put(msg, rval.encoded); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 77 | |
| 78 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
| 79 | |
| 80 | return msg; |
| 81 | } |
| 82 | |
| 83 | /* consumes 'msg' _if_ it is successful */ |
| 84 | RsproPDU_t *rspro_dec_msg(struct msgb *msg) |
| 85 | { |
Harald Welte | 9ebbacc | 2018-09-24 17:43:39 +0200 | [diff] [blame] | 86 | RsproPDU_t *pdu = NULL; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 87 | asn_dec_rval_t rval; |
| 88 | |
Harald Welte | 05dc39e | 2019-04-01 10:51:08 +0200 | [diff] [blame] | 89 | //printf("decoding %s\n", msgb_hexdump(msg)); |
Harald Welte | 7585286 | 2018-09-24 14:55:34 +0200 | [diff] [blame] | 90 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, msgb_l2(msg), msgb_l2len(msg)); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 91 | if (rval.code != RC_OK) { |
Harald Welte | 7585286 | 2018-09-24 14:55:34 +0200 | [diff] [blame] | 92 | fprintf(stderr, "Failed to decode: %d. Consumed %lu of %u bytes\n", |
| 93 | rval.code, rval.consumed, msgb_length(msg)); |
| 94 | msgb_free(msg); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | msgb_free(msg); |
| 99 | |
| 100 | return pdu; |
| 101 | } |
| 102 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 103 | static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in) |
| 104 | { |
Harald Welte | 137c440 | 2018-08-17 21:25:56 +0200 | [diff] [blame] | 105 | out->type = in->type; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 106 | OCTET_STRING_fromString(&out->name, in->name); |
| 107 | OCTET_STRING_fromString(&out->software, in->software); |
| 108 | OCTET_STRING_fromString(&out->swVersion, in->sw_version); |
| 109 | if (strlen(in->hw_manufacturer)) |
| 110 | out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, |
| 111 | in->hw_manufacturer, -1); |
| 112 | if (strlen(in->hw_model)) |
| 113 | out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1); |
| 114 | if (strlen(in->hw_serial_nr)) |
| 115 | out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1); |
| 116 | if (strlen(in->hw_version)) |
| 117 | out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1); |
| 118 | if (strlen(in->fw_version)) |
| 119 | out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1); |
| 120 | } |
| 121 | |
Harald Welte | 92bd881 | 2019-03-02 14:52:17 +0100 | [diff] [blame] | 122 | void string_fromOCTET_STRING(char *out, size_t out_size, const OCTET_STRING_t *in) |
| 123 | { |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 124 | if (!in) { |
Harald Welte | 92bd881 | 2019-03-02 14:52:17 +0100 | [diff] [blame] | 125 | out[0] = '\0'; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 126 | return; |
| 127 | } |
Harald Welte | 92bd881 | 2019-03-02 14:52:17 +0100 | [diff] [blame] | 128 | memcpy(out, in->buf, out_size < in->size ? out_size : in->size); |
| 129 | if (in->size < out_size) |
| 130 | out[in->size] = '\0'; |
| 131 | else |
| 132 | out[out_size-1] = '\0'; |
| 133 | } |
| 134 | #define string_fromOCTET_STRING_ARRAY(out, in) string_fromOCTET_STRING(out, ARRAY_SIZE(out), in) |
| 135 | |
| 136 | |
| 137 | void rspro_comp_id_retrieve(struct app_comp_id *out, const ComponentIdentity_t *in) |
| 138 | { |
| 139 | memset(out, 0, sizeof(*out)); |
| 140 | out->type = in->type; |
| 141 | string_fromOCTET_STRING_ARRAY(out->name, &in->name); |
| 142 | string_fromOCTET_STRING_ARRAY(out->software, &in->software); |
| 143 | string_fromOCTET_STRING_ARRAY(out->sw_version, &in->swVersion); |
| 144 | string_fromOCTET_STRING_ARRAY(out->hw_manufacturer, in->hwManufacturer); |
| 145 | string_fromOCTET_STRING_ARRAY(out->hw_serial_nr, in->hwSerialNr); |
| 146 | string_fromOCTET_STRING_ARRAY(out->hw_version, in->hwVersion); |
| 147 | string_fromOCTET_STRING_ARRAY(out->fw_version, in->fwVersion); |
| 148 | } |
| 149 | |
Harald Welte | 0eaa0ef | 2019-03-02 15:12:42 +0100 | [diff] [blame] | 150 | const char *rspro_IpAddr2str(const IpAddress_t *in) |
| 151 | { |
| 152 | static char buf[128]; |
| 153 | |
| 154 | switch (in->present) { |
| 155 | case IpAddress_PR_ipv4: |
| 156 | return inet_ntop(AF_INET, in->choice.ipv4.buf, buf, sizeof(buf)); |
| 157 | case IpAddress_PR_ipv6: |
| 158 | return inet_ntop(AF_INET6, in->choice.ipv6.buf, buf, sizeof(buf)); |
| 159 | default: |
| 160 | return NULL; |
| 161 | } |
| 162 | } |
| 163 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 164 | static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port) |
| 165 | { |
| 166 | uint32_t ip_n = htonl(ip); |
| 167 | out->ip.present = IpAddress_PR_ipv4; |
| 168 | OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4); |
Harald Welte | 6971412 | 2019-03-30 19:16:30 +0100 | [diff] [blame] | 169 | out->port = port; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid, |
| 174 | uint16_t bank_id, uint16_t num_slots) |
| 175 | { |
| 176 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 177 | if (!pdu) |
| 178 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 179 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 180 | pdu->msg.present = RsproPDUchoice_PR_connectBankReq; |
| 181 | fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid); |
| 182 | pdu->msg.choice.connectBankReq.bankId = bank_id; |
| 183 | pdu->msg.choice.connectBankReq.numberOfSlots = num_slots; |
| 184 | |
| 185 | return pdu; |
| 186 | } |
| 187 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 188 | RsproPDU_t *rspro_gen_ConnectBankRes(const struct app_comp_id *a_cid, e_ResultCode res) |
| 189 | { |
| 190 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 191 | if (!pdu) |
| 192 | return NULL; |
| 193 | pdu->version = 2; |
| 194 | pdu->msg.present = RsproPDUchoice_PR_connectBankRes; |
| 195 | fill_comp_id(&pdu->msg.choice.connectBankRes.identity, a_cid); |
| 196 | pdu->msg.choice.connectBankRes.result = res; |
| 197 | |
| 198 | return pdu; |
| 199 | } |
| 200 | |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 201 | RsproPDU_t *rspro_gen_ConnectClientReq(const struct app_comp_id *a_cid, const ClientSlot_t *client) |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 202 | { |
| 203 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 204 | if (!pdu) |
| 205 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 206 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 207 | pdu->msg.present = RsproPDUchoice_PR_connectClientReq; |
| 208 | fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid); |
Harald Welte | c3632a7 | 2018-10-14 20:38:34 +0200 | [diff] [blame] | 209 | if (client) |
| 210 | ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 211 | |
| 212 | return pdu; |
| 213 | } |
| 214 | |
Harald Welte | 10f6c21 | 2018-09-24 14:55:55 +0200 | [diff] [blame] | 215 | RsproPDU_t *rspro_gen_ConnectClientRes(const struct app_comp_id *a_cid, e_ResultCode res) |
| 216 | { |
| 217 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 218 | if (!pdu) |
| 219 | return NULL; |
| 220 | pdu->version = 2; |
| 221 | pdu->tag = 2342; |
| 222 | pdu->msg.present = RsproPDUchoice_PR_connectClientRes; |
| 223 | fill_comp_id(&pdu->msg.choice.connectClientRes.identity, a_cid); |
| 224 | pdu->msg.choice.connectClientRes.result = res; |
| 225 | |
| 226 | return pdu; |
| 227 | } |
| 228 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 229 | RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank) |
| 230 | { |
| 231 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 232 | if (!pdu) |
| 233 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 234 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 235 | pdu->msg.present = RsproPDUchoice_PR_createMappingReq; |
| 236 | pdu->msg.choice.createMappingReq.client = *client; |
| 237 | pdu->msg.choice.createMappingReq.bank = *bank; |
| 238 | |
| 239 | return pdu; |
| 240 | } |
| 241 | |
Harald Welte | c0a4ae4 | 2018-10-15 00:44:53 +0200 | [diff] [blame] | 242 | RsproPDU_t *rspro_gen_CreateMappingRes(e_ResultCode res) |
| 243 | { |
| 244 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 245 | if (!pdu) |
| 246 | return NULL; |
| 247 | pdu->version = 2; |
| 248 | pdu->msg.present = RsproPDUchoice_PR_createMappingRes; |
| 249 | pdu->msg.choice.createMappingRes.result = res; |
| 250 | |
| 251 | return pdu; |
| 252 | } |
| 253 | |
| 254 | RsproPDU_t *rspro_gen_RemoveMappingReq(const ClientSlot_t *client, const BankSlot_t *bank) |
| 255 | { |
| 256 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 257 | if (!pdu) |
| 258 | return NULL; |
| 259 | pdu->version = 2; |
| 260 | pdu->msg.present = RsproPDUchoice_PR_removeMappingReq; |
| 261 | pdu->msg.choice.removeMappingReq.client = *client; |
| 262 | pdu->msg.choice.removeMappingReq.bank = *bank; |
| 263 | |
| 264 | return pdu; |
| 265 | } |
| 266 | |
| 267 | RsproPDU_t *rspro_gen_RemoveMappingRes(e_ResultCode res) |
| 268 | { |
| 269 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 270 | if (!pdu) |
| 271 | return NULL; |
| 272 | pdu->version = 2; |
| 273 | pdu->msg.present = RsproPDUchoice_PR_removeMappingRes; |
| 274 | pdu->msg.choice.removeMappingRes.result = res; |
| 275 | |
| 276 | return pdu; |
| 277 | } |
| 278 | |
Harald Welte | eae30c3 | 2019-03-30 17:32:37 +0100 | [diff] [blame] | 279 | RsproPDU_t *rspro_gen_ConfigClientIdReq(const ClientSlot_t *client) |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 280 | { |
| 281 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 282 | if (!pdu) |
| 283 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 284 | pdu->version = 2; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 285 | pdu->msg.present = RsproPDUchoice_PR_configClientIdReq; |
| 286 | pdu->msg.choice.configClientIdReq.clientSlot = *client; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 287 | |
| 288 | return pdu; |
| 289 | } |
| 290 | |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 291 | RsproPDU_t *rspro_gen_ConfigClientIdRes(e_ResultCode res) |
Harald Welte | c0097b1 | 2019-03-02 14:22:24 +0100 | [diff] [blame] | 292 | { |
| 293 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 294 | if (!pdu) |
| 295 | return NULL; |
| 296 | pdu->version = 2; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 297 | pdu->msg.present = RsproPDUchoice_PR_configClientIdRes; |
| 298 | pdu->msg.choice.configClientIdRes.result = res; |
| 299 | |
| 300 | return pdu; |
| 301 | } |
| 302 | |
| 303 | RsproPDU_t *rspro_gen_ConfigClientBankReq(const BankSlot_t *bank, uint32_t ip, uint16_t port) |
| 304 | { |
| 305 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 306 | if (!pdu) |
| 307 | return NULL; |
| 308 | pdu->version = 2; |
| 309 | pdu->msg.present = RsproPDUchoice_PR_configClientBankReq; |
| 310 | pdu->msg.choice.configClientBankReq.bankSlot = *bank; |
| 311 | fill_ip4_port(&pdu->msg.choice.configClientBankReq.bankd, ip, port); |
| 312 | |
| 313 | return pdu; |
| 314 | } |
| 315 | |
| 316 | RsproPDU_t *rspro_gen_ConfigClientBankRes(e_ResultCode res) |
| 317 | { |
| 318 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 319 | if (!pdu) |
| 320 | return NULL; |
| 321 | pdu->version = 2; |
| 322 | pdu->msg.present = RsproPDUchoice_PR_configClientBankRes; |
| 323 | pdu->msg.choice.configClientBankRes.result = res; |
Harald Welte | c0097b1 | 2019-03-02 14:22:24 +0100 | [diff] [blame] | 324 | |
| 325 | return pdu; |
| 326 | } |
| 327 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 328 | RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr, |
| 329 | unsigned int atr_len) |
| 330 | { |
| 331 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 332 | if (!pdu) |
| 333 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 334 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 335 | pdu->msg.present = RsproPDUchoice_PR_setAtrReq; |
| 336 | pdu->msg.choice.setAtrReq.slot.clientId = client_id; |
| 337 | pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr; |
| 338 | OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len); |
| 339 | |
| 340 | return pdu; |
| 341 | } |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 342 | |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 343 | RsproPDU_t *rspro_gen_SetAtrRes(e_ResultCode res) |
| 344 | { |
| 345 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 346 | if (!pdu) |
| 347 | return NULL; |
| 348 | pdu->version = 2; |
| 349 | pdu->msg.present = RsproPDUchoice_PR_setAtrRes; |
| 350 | pdu->msg.choice.setAtrRes.result = res; |
| 351 | |
| 352 | return pdu; |
| 353 | } |
| 354 | |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 355 | RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank, |
| 356 | const uint8_t *tpdu, unsigned int tpdu_len) |
| 357 | { |
| 358 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 359 | if (!pdu) |
| 360 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 361 | pdu->version = 2; |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 362 | pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 363 | OSMO_ASSERT(client); |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 364 | pdu->msg.choice.tpduModemToCard.fromClientSlot = *client; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 365 | OSMO_ASSERT(bank); |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 366 | pdu->msg.choice.tpduModemToCard.toBankSlot = *bank; |
| 367 | /* TODO: flags? */ |
| 368 | OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len); |
| 369 | |
| 370 | return pdu; |
| 371 | } |
| 372 | |
| 373 | RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client, |
| 374 | const uint8_t *tpdu, unsigned int tpdu_len) |
| 375 | { |
| 376 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 377 | if (!pdu) |
| 378 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 379 | pdu->version = 2; |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 380 | pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 381 | OSMO_ASSERT(bank); |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 382 | pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 383 | OSMO_ASSERT(client) |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 384 | pdu->msg.choice.tpduCardToModem.toClientSlot = *client; |
| 385 | /* TODO: flags? */ |
| 386 | OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len); |
| 387 | |
| 388 | return pdu; |
| 389 | } |
Harald Welte | cf8b89a | 2019-03-11 22:16:22 +0100 | [diff] [blame] | 390 | |
Harald Welte | 4e7a285 | 2019-03-17 21:01:50 +0100 | [diff] [blame] | 391 | e_ResultCode rspro_get_result(const RsproPDU_t *pdu) |
| 392 | { |
| 393 | switch (pdu->msg.present) { |
| 394 | case RsproPDUchoice_PR_connectBankRes: |
| 395 | return pdu->msg.choice.connectBankRes.result; |
| 396 | case RsproPDUchoice_PR_connectClientRes: |
| 397 | return pdu->msg.choice.connectClientRes.result; |
| 398 | case RsproPDUchoice_PR_createMappingRes: |
| 399 | return pdu->msg.choice.createMappingRes.result; |
| 400 | case RsproPDUchoice_PR_removeMappingRes: |
| 401 | return pdu->msg.choice.removeMappingRes.result; |
| 402 | case RsproPDUchoice_PR_configClientIdRes: |
| 403 | return pdu->msg.choice.configClientIdRes.result; |
| 404 | case RsproPDUchoice_PR_configClientBankRes: |
| 405 | return pdu->msg.choice.configClientBankRes.result; |
| 406 | case RsproPDUchoice_PR_setAtrRes: |
| 407 | return pdu->msg.choice.setAtrRes.result; |
| 408 | default: |
| 409 | OSMO_ASSERT(0); |
| 410 | } |
| 411 | } |
| 412 | |
Harald Welte | cf8b89a | 2019-03-11 22:16:22 +0100 | [diff] [blame] | 413 | void rspro2bank_slot(struct bank_slot *out, const BankSlot_t *in) |
| 414 | { |
| 415 | out->bank_id = in->bankId; |
| 416 | out->slot_nr = in->slotNr; |
| 417 | } |
| 418 | |
| 419 | void bank_slot2rspro(BankSlot_t *out, const struct bank_slot *in) |
| 420 | { |
| 421 | out->bankId = in->bank_id; |
| 422 | out->slotNr = in->slot_nr; |
| 423 | } |
| 424 | |
| 425 | void rspro2client_slot(struct client_slot *out, const ClientSlot_t *in) |
| 426 | { |
| 427 | out->client_id = in->clientId; |
| 428 | out->slot_nr = in->slotNr; |
| 429 | } |
| 430 | |
| 431 | void client_slot2rspro(ClientSlot_t *out, const struct client_slot *in) |
| 432 | { |
| 433 | out->clientId = in->client_id; |
| 434 | out->slotNr = in->slot_nr; |
| 435 | } |