blob: 67d4ac0fb25b3b43d1f0569e42015b7b240e09d3 [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 Welte703d6862018-09-24 17:44:50 +020042 msgb_free(msg);
Harald Welte3aa901d2018-08-13 18:32:36 +020043 return NULL;
44 }
Harald Welted5c5c0b2018-08-17 22:04:01 +020045 msgb_put(msg, rval.encoded);
Harald Welted08d5052018-10-03 20:43:31 +020046 printf("encoded %s\n", msgb_hexdump(msg));
Harald Welte3aa901d2018-08-13 18:32:36 +020047
48 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
49
50 return msg;
51}
52
53/* consumes 'msg' _if_ it is successful */
54RsproPDU_t *rspro_dec_msg(struct msgb *msg)
55{
Harald Welte9ebbacc2018-09-24 17:43:39 +020056 RsproPDU_t *pdu = NULL;
Harald Welte3aa901d2018-08-13 18:32:36 +020057 asn_dec_rval_t rval;
58
Harald Welte75852862018-09-24 14:55:34 +020059 printf("decoding %s\n", msgb_hexdump(msg));
60 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, msgb_l2(msg), msgb_l2len(msg));
Harald Welte3aa901d2018-08-13 18:32:36 +020061 if (rval.code != RC_OK) {
Harald Welte75852862018-09-24 14:55:34 +020062 fprintf(stderr, "Failed to decode: %d. Consumed %lu of %u bytes\n",
63 rval.code, rval.consumed, msgb_length(msg));
64 msgb_free(msg);
Harald Welte3aa901d2018-08-13 18:32:36 +020065 return NULL;
66 }
67
68 msgb_free(msg);
69
70 return pdu;
71}
72
Harald Welte3aa901d2018-08-13 18:32:36 +020073static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in)
74{
Harald Welte137c4402018-08-17 21:25:56 +020075 out->type = in->type;
Harald Welte3aa901d2018-08-13 18:32:36 +020076 OCTET_STRING_fromString(&out->name, in->name);
77 OCTET_STRING_fromString(&out->software, in->software);
78 OCTET_STRING_fromString(&out->swVersion, in->sw_version);
79 if (strlen(in->hw_manufacturer))
80 out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName,
81 in->hw_manufacturer, -1);
82 if (strlen(in->hw_model))
83 out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1);
84 if (strlen(in->hw_serial_nr))
85 out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1);
86 if (strlen(in->hw_version))
87 out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1);
88 if (strlen(in->fw_version))
89 out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1);
90}
91
92static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port)
93{
94 uint32_t ip_n = htonl(ip);
95 out->ip.present = IpAddress_PR_ipv4;
96 OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4);
97 out->port = htons(port);
98}
99
100
101RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid,
102 uint16_t bank_id, uint16_t num_slots)
103{
104 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
105 if (!pdu)
106 return NULL;
107 pdu->msg.present = RsproPDUchoice_PR_connectBankReq;
108 fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid);
109 pdu->msg.choice.connectBankReq.bankId = bank_id;
110 pdu->msg.choice.connectBankReq.numberOfSlots = num_slots;
111
112 return pdu;
113}
114
Harald Welte57555aa2018-08-17 22:05:06 +0200115RsproPDU_t *rspro_gen_ConnectClientReq(const struct app_comp_id *a_cid, const ClientSlot_t *client)
Harald Welte3aa901d2018-08-13 18:32:36 +0200116{
117 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
118 if (!pdu)
119 return NULL;
120 pdu->msg.present = RsproPDUchoice_PR_connectClientReq;
121 fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid);
Harald Welte57555aa2018-08-17 22:05:06 +0200122 ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client);
Harald Welte3aa901d2018-08-13 18:32:36 +0200123
124 return pdu;
125}
126
Harald Welte10f6c212018-09-24 14:55:55 +0200127RsproPDU_t *rspro_gen_ConnectClientRes(const struct app_comp_id *a_cid, e_ResultCode res)
128{
129 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
130 if (!pdu)
131 return NULL;
Harald Welted08d5052018-10-03 20:43:31 +0200132 //pdu->version = 2;
133 //pdu->tag = 2342;
Harald Welte10f6c212018-09-24 14:55:55 +0200134 pdu->msg.present = RsproPDUchoice_PR_connectClientRes;
135 fill_comp_id(&pdu->msg.choice.connectClientRes.identity, a_cid);
136 pdu->msg.choice.connectClientRes.result = res;
137
138 return pdu;
139}
140
Harald Welte3aa901d2018-08-13 18:32:36 +0200141RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank)
142{
143 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
144 if (!pdu)
145 return NULL;
146 pdu->msg.present = RsproPDUchoice_PR_createMappingReq;
147 pdu->msg.choice.createMappingReq.client = *client;
148 pdu->msg.choice.createMappingReq.bank = *bank;
149
150 return pdu;
151}
152
Harald Welte371d0262018-08-16 15:23:58 +0200153RsproPDU_t *rspro_gen_ConfigClientReq(const ClientSlot_t *client, uint32_t ip, uint16_t port)
Harald Welte3aa901d2018-08-13 18:32:36 +0200154{
155 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
156 if (!pdu)
157 return NULL;
158 pdu->msg.present = RsproPDUchoice_PR_configClientReq;
Harald Welte371d0262018-08-16 15:23:58 +0200159 pdu->msg.choice.configClientReq.clientSlot = *client;
Harald Welte3aa901d2018-08-13 18:32:36 +0200160 fill_ip4_port(&pdu->msg.choice.configClientReq.bankd, ip, port);
161
162 return pdu;
163}
164
Harald Welted08d5052018-10-03 20:43:31 +0200165RsproPDU_t *rspro_gen_SetAtrReq(const ClientSlot_t *client, const uint8_t *atr, unsigned int atr_len)
Harald Welte3aa901d2018-08-13 18:32:36 +0200166{
167 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
168 if (!pdu)
169 return NULL;
170 pdu->msg.present = RsproPDUchoice_PR_setAtrReq;
Harald Welted08d5052018-10-03 20:43:31 +0200171 pdu->msg.choice.setAtrReq.slot = *client;
Harald Welte3aa901d2018-08-13 18:32:36 +0200172 OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len);
173
174 return pdu;
175}
Harald Welte5d16b1c2018-09-23 19:25:46 +0200176
177RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank,
178 const uint8_t *tpdu, unsigned int tpdu_len)
179{
180 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
181 if (!pdu)
182 return NULL;
183 pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard;
184 pdu->msg.choice.tpduModemToCard.fromClientSlot = *client;
185 pdu->msg.choice.tpduModemToCard.toBankSlot = *bank;
186 /* TODO: flags? */
187 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len);
188
189 return pdu;
190}
191
192RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client,
193 const uint8_t *tpdu, unsigned int tpdu_len)
194{
195 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
196 if (!pdu)
197 return NULL;
198 pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem;
199 pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank;
200 pdu->msg.choice.tpduCardToModem.toClientSlot = *client;
201 /* TODO: flags? */
202 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len);
203
204 return pdu;
205}