blob: d6bdd0f1d55a2b50c7f12f4ed9da9e9adc772121 [file] [log] [blame]
Harald Welte3aa901d2018-08-13 18:32:36 +02001
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 Welte137c4402018-08-17 21:25:56 +02009#include "rspro_util.h"
10
Harald Welte57555aa2018-08-17 22:05:06 +020011#define ASN_ALLOC_COPY(out, in) \
12do { \
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 Welte3aa901d2018-08-13 18:32:36 +020021struct 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 */
30struct 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 Weltefd471192018-09-24 14:51:14 +020038 msg->l2h = msg->data;
Harald Welte6b8d4f82018-08-17 22:06:24 +020039 rval = der_encode_to_buffer(&asn_DEF_RsproPDU, pdu, msgb_data(msg), msgb_tailroom(msg));
Harald Welte3aa901d2018-08-13 18:32:36 +020040 if (rval.encoded < 0) {
Harald Weltea2b23c32018-08-17 22:09:06 +020041 fprintf(stderr, "Failed to encode %s\n", rval.failed_type->name);
Harald Welte3aa901d2018-08-13 18:32:36 +020042 return NULL;
43 }
Harald Welted5c5c0b2018-08-17 22:04:01 +020044 msgb_put(msg, rval.encoded);
Harald Welte3aa901d2018-08-13 18:32:36 +020045
46 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
47
48 return msg;
49}
50
51/* consumes 'msg' _if_ it is successful */
52RsproPDU_t *rspro_dec_msg(struct msgb *msg)
53{
54 RsproPDU_t *pdu;
55 asn_dec_rval_t rval;
56
57 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, msgb_data(msg), msgb_length(msg));
58 if (rval.code != RC_OK) {
59 /* FIXME */
60 return NULL;
61 }
62
63 msgb_free(msg);
64
65 return pdu;
66}
67
Harald Welte3aa901d2018-08-13 18:32:36 +020068static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in)
69{
Harald Welte137c4402018-08-17 21:25:56 +020070 out->type = in->type;
Harald Welte3aa901d2018-08-13 18:32:36 +020071 OCTET_STRING_fromString(&out->name, in->name);
72 OCTET_STRING_fromString(&out->software, in->software);
73 OCTET_STRING_fromString(&out->swVersion, in->sw_version);
74 if (strlen(in->hw_manufacturer))
75 out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName,
76 in->hw_manufacturer, -1);
77 if (strlen(in->hw_model))
78 out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1);
79 if (strlen(in->hw_serial_nr))
80 out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1);
81 if (strlen(in->hw_version))
82 out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1);
83 if (strlen(in->fw_version))
84 out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1);
85}
86
87static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port)
88{
89 uint32_t ip_n = htonl(ip);
90 out->ip.present = IpAddress_PR_ipv4;
91 OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4);
92 out->port = htons(port);
93}
94
95
96RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid,
97 uint16_t bank_id, uint16_t num_slots)
98{
99 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
100 if (!pdu)
101 return NULL;
102 pdu->msg.present = RsproPDUchoice_PR_connectBankReq;
103 fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid);
104 pdu->msg.choice.connectBankReq.bankId = bank_id;
105 pdu->msg.choice.connectBankReq.numberOfSlots = num_slots;
106
107 return pdu;
108}
109
Harald Welte57555aa2018-08-17 22:05:06 +0200110RsproPDU_t *rspro_gen_ConnectClientReq(const struct app_comp_id *a_cid, const ClientSlot_t *client)
Harald Welte3aa901d2018-08-13 18:32:36 +0200111{
112 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
113 if (!pdu)
114 return NULL;
115 pdu->msg.present = RsproPDUchoice_PR_connectClientReq;
116 fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid);
Harald Welte57555aa2018-08-17 22:05:06 +0200117 ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client);
Harald Welte3aa901d2018-08-13 18:32:36 +0200118
119 return pdu;
120}
121
122RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank)
123{
124 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
125 if (!pdu)
126 return NULL;
127 pdu->msg.present = RsproPDUchoice_PR_createMappingReq;
128 pdu->msg.choice.createMappingReq.client = *client;
129 pdu->msg.choice.createMappingReq.bank = *bank;
130
131 return pdu;
132}
133
Harald Welte371d0262018-08-16 15:23:58 +0200134RsproPDU_t *rspro_gen_ConfigClientReq(const ClientSlot_t *client, uint32_t ip, uint16_t port)
Harald Welte3aa901d2018-08-13 18:32:36 +0200135{
136 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
137 if (!pdu)
138 return NULL;
139 pdu->msg.present = RsproPDUchoice_PR_configClientReq;
Harald Welte371d0262018-08-16 15:23:58 +0200140 pdu->msg.choice.configClientReq.clientSlot = *client;
Harald Welte3aa901d2018-08-13 18:32:36 +0200141 fill_ip4_port(&pdu->msg.choice.configClientReq.bankd, ip, port);
142
143 return pdu;
144}
145
146RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr,
147 unsigned int atr_len)
148{
149 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
150 if (!pdu)
151 return NULL;
152 pdu->msg.present = RsproPDUchoice_PR_setAtrReq;
153 pdu->msg.choice.setAtrReq.slot.clientId = client_id;
154 pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr;
155 OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len);
156
157 return pdu;
158}
Harald Welte5d16b1c2018-09-23 19:25:46 +0200159
160RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank,
161 const uint8_t *tpdu, unsigned int tpdu_len)
162{
163 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
164 if (!pdu)
165 return NULL;
166 pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard;
167 pdu->msg.choice.tpduModemToCard.fromClientSlot = *client;
168 pdu->msg.choice.tpduModemToCard.toBankSlot = *bank;
169 /* TODO: flags? */
170 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len);
171
172 return pdu;
173}
174
175RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client,
176 const uint8_t *tpdu, unsigned int tpdu_len)
177{
178 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
179 if (!pdu)
180 return NULL;
181 pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem;
182 pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank;
183 pdu->msg.choice.tpduCardToModem.toClientSlot = *client;
184 /* TODO: flags? */
185 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len);
186
187 return pdu;
188}