blob: 9f5125723918b85e707acca80224e085eb8eb22d [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
38 rval = der_encode_to_buffer(&asn_DEF_RsproPDU, pdu, msgb_data(msg), msgb_length(msg));
39 if (rval.encoded < 0) {
40 return NULL;
41 }
Harald Welted5c5c0b2018-08-17 22:04:01 +020042 msgb_put(msg, rval.encoded);
Harald Welte3aa901d2018-08-13 18:32:36 +020043
44 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
45
46 return msg;
47}
48
49/* consumes 'msg' _if_ it is successful */
50RsproPDU_t *rspro_dec_msg(struct msgb *msg)
51{
52 RsproPDU_t *pdu;
53 asn_dec_rval_t rval;
54
55 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, msgb_data(msg), msgb_length(msg));
56 if (rval.code != RC_OK) {
57 /* FIXME */
58 return NULL;
59 }
60
61 msgb_free(msg);
62
63 return pdu;
64}
65
Harald Welte3aa901d2018-08-13 18:32:36 +020066static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in)
67{
Harald Welte137c4402018-08-17 21:25:56 +020068 out->type = in->type;
Harald Welte3aa901d2018-08-13 18:32:36 +020069 OCTET_STRING_fromString(&out->name, in->name);
70 OCTET_STRING_fromString(&out->software, in->software);
71 OCTET_STRING_fromString(&out->swVersion, in->sw_version);
72 if (strlen(in->hw_manufacturer))
73 out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName,
74 in->hw_manufacturer, -1);
75 if (strlen(in->hw_model))
76 out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1);
77 if (strlen(in->hw_serial_nr))
78 out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1);
79 if (strlen(in->hw_version))
80 out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1);
81 if (strlen(in->fw_version))
82 out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1);
83}
84
85static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port)
86{
87 uint32_t ip_n = htonl(ip);
88 out->ip.present = IpAddress_PR_ipv4;
89 OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4);
90 out->port = htons(port);
91}
92
93
94RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid,
95 uint16_t bank_id, uint16_t num_slots)
96{
97 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
98 if (!pdu)
99 return NULL;
100 pdu->msg.present = RsproPDUchoice_PR_connectBankReq;
101 fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid);
102 pdu->msg.choice.connectBankReq.bankId = bank_id;
103 pdu->msg.choice.connectBankReq.numberOfSlots = num_slots;
104
105 return pdu;
106}
107
Harald Welte57555aa2018-08-17 22:05:06 +0200108RsproPDU_t *rspro_gen_ConnectClientReq(const struct app_comp_id *a_cid, const ClientSlot_t *client)
Harald Welte3aa901d2018-08-13 18:32:36 +0200109{
110 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
111 if (!pdu)
112 return NULL;
113 pdu->msg.present = RsproPDUchoice_PR_connectClientReq;
114 fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid);
Harald Welte57555aa2018-08-17 22:05:06 +0200115 ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client);
Harald Welte3aa901d2018-08-13 18:32:36 +0200116
117 return pdu;
118}
119
120RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank)
121{
122 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
123 if (!pdu)
124 return NULL;
125 pdu->msg.present = RsproPDUchoice_PR_createMappingReq;
126 pdu->msg.choice.createMappingReq.client = *client;
127 pdu->msg.choice.createMappingReq.bank = *bank;
128
129 return pdu;
130}
131
Harald Welte371d0262018-08-16 15:23:58 +0200132RsproPDU_t *rspro_gen_ConfigClientReq(const ClientSlot_t *client, uint32_t ip, uint16_t port)
Harald Welte3aa901d2018-08-13 18:32:36 +0200133{
134 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
135 if (!pdu)
136 return NULL;
137 pdu->msg.present = RsproPDUchoice_PR_configClientReq;
Harald Welte371d0262018-08-16 15:23:58 +0200138 pdu->msg.choice.configClientReq.clientSlot = *client;
Harald Welte3aa901d2018-08-13 18:32:36 +0200139 fill_ip4_port(&pdu->msg.choice.configClientReq.bankd, ip, port);
140
141 return pdu;
142}
143
144RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr,
145 unsigned int atr_len)
146{
147 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
148 if (!pdu)
149 return NULL;
150 pdu->msg.present = RsproPDUchoice_PR_setAtrReq;
151 pdu->msg.choice.setAtrReq.slot.clientId = client_id;
152 pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr;
153 OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len);
154
155 return pdu;
156}