Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 1 | |
| 2 | |
| 3 | #include <asn1c/asn_application.h> |
| 4 | #include <asn1c/der_encoder.h> |
| 5 | |
| 6 | #include <osmocom/core/msgb.h> |
| 7 | #include <osmocom/rspro/RsproPDU.h> |
| 8 | |
Harald Welte | 137c440 | 2018-08-17 21:25:56 +0200 | [diff] [blame] | 9 | #include "rspro_util.h" |
| 10 | |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 11 | #define ASN_ALLOC_COPY(out, in) \ |
| 12 | do { \ |
| 13 | if (in) { \ |
| 14 | out = CALLOC(1, sizeof(*in)); \ |
| 15 | OSMO_ASSERT(out); \ |
| 16 | memcpy(out, in, sizeof(*in)); \ |
| 17 | } \ |
| 18 | } while (0) |
| 19 | |
| 20 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 21 | struct msgb *rspro_msgb_alloc(void) |
| 22 | { |
| 23 | return msgb_alloc_headroom(1024, 8, "RSPRO"); |
| 24 | } |
| 25 | |
| 26 | /*! BER-Encode an RSPRO message into msgb. |
| 27 | * \param[in] pdu Structure describing RSPRO PDU. Is freed by this function on success |
| 28 | * \returns callee-allocated message buffer containing encoded RSPRO PDU; NULL on error. |
| 29 | */ |
| 30 | struct msgb *rspro_enc_msg(RsproPDU_t *pdu) |
| 31 | { |
| 32 | struct msgb *msg = rspro_msgb_alloc(); |
| 33 | asn_enc_rval_t rval; |
| 34 | |
| 35 | if (!msg) |
| 36 | return NULL; |
| 37 | |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 38 | msg->l2h = msg->data; |
Harald Welte | 6b8d4f8 | 2018-08-17 22:06:24 +0200 | [diff] [blame] | 39 | 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] | 40 | if (rval.encoded < 0) { |
Harald Welte | a2b23c3 | 2018-08-17 22:09:06 +0200 | [diff] [blame] | 41 | fprintf(stderr, "Failed to encode %s\n", rval.failed_type->name); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 42 | return NULL; |
| 43 | } |
Harald Welte | d5c5c0b | 2018-08-17 22:04:01 +0200 | [diff] [blame] | 44 | msgb_put(msg, rval.encoded); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 45 | |
| 46 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
| 47 | |
| 48 | return msg; |
| 49 | } |
| 50 | |
| 51 | /* consumes 'msg' _if_ it is successful */ |
| 52 | RsproPDU_t *rspro_dec_msg(struct msgb *msg) |
| 53 | { |
| 54 | RsproPDU_t *pdu; |
| 55 | asn_dec_rval_t rval; |
| 56 | |
Harald Welte | 7585286 | 2018-09-24 14:55:34 +0200 | [diff] [blame^] | 57 | printf("decoding %s\n", msgb_hexdump(msg)); |
| 58 | 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] | 59 | if (rval.code != RC_OK) { |
Harald Welte | 7585286 | 2018-09-24 14:55:34 +0200 | [diff] [blame^] | 60 | fprintf(stderr, "Failed to decode: %d. Consumed %lu of %u bytes\n", |
| 61 | rval.code, rval.consumed, msgb_length(msg)); |
| 62 | msgb_free(msg); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | msgb_free(msg); |
| 67 | |
| 68 | return pdu; |
| 69 | } |
| 70 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 71 | static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in) |
| 72 | { |
Harald Welte | 137c440 | 2018-08-17 21:25:56 +0200 | [diff] [blame] | 73 | out->type = in->type; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 74 | OCTET_STRING_fromString(&out->name, in->name); |
| 75 | OCTET_STRING_fromString(&out->software, in->software); |
| 76 | OCTET_STRING_fromString(&out->swVersion, in->sw_version); |
| 77 | if (strlen(in->hw_manufacturer)) |
| 78 | out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, |
| 79 | in->hw_manufacturer, -1); |
| 80 | if (strlen(in->hw_model)) |
| 81 | out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1); |
| 82 | if (strlen(in->hw_serial_nr)) |
| 83 | out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1); |
| 84 | if (strlen(in->hw_version)) |
| 85 | out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1); |
| 86 | if (strlen(in->fw_version)) |
| 87 | out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1); |
| 88 | } |
| 89 | |
| 90 | static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port) |
| 91 | { |
| 92 | uint32_t ip_n = htonl(ip); |
| 93 | out->ip.present = IpAddress_PR_ipv4; |
| 94 | OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4); |
| 95 | out->port = htons(port); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid, |
| 100 | uint16_t bank_id, uint16_t num_slots) |
| 101 | { |
| 102 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 103 | if (!pdu) |
| 104 | return NULL; |
| 105 | pdu->msg.present = RsproPDUchoice_PR_connectBankReq; |
| 106 | fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid); |
| 107 | pdu->msg.choice.connectBankReq.bankId = bank_id; |
| 108 | pdu->msg.choice.connectBankReq.numberOfSlots = num_slots; |
| 109 | |
| 110 | return pdu; |
| 111 | } |
| 112 | |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 113 | 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] | 114 | { |
| 115 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 116 | if (!pdu) |
| 117 | return NULL; |
| 118 | pdu->msg.present = RsproPDUchoice_PR_connectClientReq; |
| 119 | fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid); |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 120 | ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 121 | |
| 122 | return pdu; |
| 123 | } |
| 124 | |
| 125 | RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank) |
| 126 | { |
| 127 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 128 | if (!pdu) |
| 129 | return NULL; |
| 130 | pdu->msg.present = RsproPDUchoice_PR_createMappingReq; |
| 131 | pdu->msg.choice.createMappingReq.client = *client; |
| 132 | pdu->msg.choice.createMappingReq.bank = *bank; |
| 133 | |
| 134 | return pdu; |
| 135 | } |
| 136 | |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 137 | RsproPDU_t *rspro_gen_ConfigClientReq(const ClientSlot_t *client, uint32_t ip, uint16_t port) |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 138 | { |
| 139 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 140 | if (!pdu) |
| 141 | return NULL; |
| 142 | pdu->msg.present = RsproPDUchoice_PR_configClientReq; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 143 | pdu->msg.choice.configClientReq.clientSlot = *client; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 144 | fill_ip4_port(&pdu->msg.choice.configClientReq.bankd, ip, port); |
| 145 | |
| 146 | return pdu; |
| 147 | } |
| 148 | |
| 149 | RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr, |
| 150 | unsigned int atr_len) |
| 151 | { |
| 152 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 153 | if (!pdu) |
| 154 | return NULL; |
| 155 | pdu->msg.present = RsproPDUchoice_PR_setAtrReq; |
| 156 | pdu->msg.choice.setAtrReq.slot.clientId = client_id; |
| 157 | pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr; |
| 158 | OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len); |
| 159 | |
| 160 | return pdu; |
| 161 | } |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 162 | |
| 163 | RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank, |
| 164 | const uint8_t *tpdu, unsigned int tpdu_len) |
| 165 | { |
| 166 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 167 | if (!pdu) |
| 168 | return NULL; |
| 169 | pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard; |
| 170 | pdu->msg.choice.tpduModemToCard.fromClientSlot = *client; |
| 171 | pdu->msg.choice.tpduModemToCard.toBankSlot = *bank; |
| 172 | /* TODO: flags? */ |
| 173 | OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len); |
| 174 | |
| 175 | return pdu; |
| 176 | } |
| 177 | |
| 178 | RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client, |
| 179 | const uint8_t *tpdu, unsigned int tpdu_len) |
| 180 | { |
| 181 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 182 | if (!pdu) |
| 183 | return NULL; |
| 184 | pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem; |
| 185 | pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank; |
| 186 | pdu->msg.choice.tpduCardToModem.toClientSlot = *client; |
| 187 | /* TODO: flags? */ |
| 188 | OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len); |
| 189 | |
| 190 | return pdu; |
| 191 | } |