blob: 8f4da1296b4818fdc7f35d9a74e06e4a2f9f8b4a [file] [log] [blame]
Harald Welte656ad302015-09-10 18:33:47 +02001#include <stdint.h>
2
3#include <osmocom/core/msgb.h>
4
5#include "rua_common.h"
6#include "hnbgw.h"
7
8extern int asn1_xer_print;
9
Harald Weltee2e5d4d2015-09-10 23:49:45 +020010static const struct value_string rua_cause_radio_vals[] = {
11 { RUA_CauseRadioNetwork_normal, "normal" },
12 { RUA_CauseRadioNetwork_connect_failed, "connect failed" },
13 { RUA_CauseRadioNetwork_network_release, "network release" },
14 { RUA_CauseRadioNetwork_unspecified, "unspecified" },
15 { 0, NULL }
16};
17
18static const struct value_string rua_cause_transp_vals[] = {
19 { RUA_CauseTransport_transport_resource_unavailable, "resource unavailable" },
20 { RUA_CauseTransport_unspecified, "unspecified" },
21 { 0, NULL }
22};
23
24static const struct value_string rua_cause_prot_vals[] = {
25 { RUA_CauseProtocol_transfer_syntax_error, "syntax error" },
26 { RUA_CauseProtocol_abstract_syntax_error_reject,
27 "abstract syntax error; reject" },
28 { RUA_CauseProtocol_abstract_syntax_error_ignore_and_notify,
29 "abstract syntax error; ignore and notify" },
30 { RUA_CauseProtocol_message_not_compatible_with_receiver_state,
31 "message not compatible with receiver state" },
32 { RUA_CauseProtocol_semantic_error, "semantic error" },
33 { RUA_CauseProtocol_unspecified, "unspecified" },
34 { RUA_CauseProtocol_abstract_syntax_error_falsely_constructed_message,
35 "falsely constructed message" },
36 { 0, NULL }
37};
38
39static const struct value_string rua_cause_misc_vals[] = {
40 { RUA_CauseMisc_processing_overload, "processing overload" },
41 { RUA_CauseMisc_hardware_failure, "hardware failure" },
42 { RUA_CauseMisc_o_and_m_intervention, "OAM intervention" },
43 { RUA_CauseMisc_unspecified, "unspecified" },
44 { 0, NULL }
45};
46
47char *rua_cause_str(RUA_Cause_t *cause)
48{
49 static char buf[32];
50
51 switch (cause->present) {
52 case RUA_Cause_PR_radioNetwork:
53 snprintf(buf, sizeof(buf), "radio(%s)",
54 get_value_string(rua_cause_radio_vals,
55 cause->choice.radioNetwork));
56 break;
57 case RUA_Cause_PR_transport:
58 snprintf(buf, sizeof(buf), "transport(%s)",
59 get_value_string(rua_cause_transp_vals,
60 cause->choice.transport));
61 break;
62 case RUA_Cause_PR_protocol:
63 snprintf(buf, sizeof(buf), "protocol(%s)",
64 get_value_string(rua_cause_prot_vals,
65 cause->choice.protocol));
66 break;
67 case RUA_Cause_PR_misc:
68 snprintf(buf, sizeof(buf), "misc(%s)",
69 get_value_string(rua_cause_misc_vals,
70 cause->choice.misc));
71 break;
72 }
73 return buf;
74}
75
76
Harald Welte656ad302015-09-10 18:33:47 +020077static struct msgb *rua_msgb_alloc(void)
78{
79 return msgb_alloc(1024, "RUA Tx");
80}
81
Harald Weltee2e5d4d2015-09-10 23:49:45 +020082struct msgb *rua_generate_initiating_message(
Harald Welte656ad302015-09-10 18:33:47 +020083 e_RUA_ProcedureCode procedureCode,
84 RUA_Criticality_t criticality,
85 asn_TYPE_descriptor_t * td, void *sptr)
86{
87 RUA_RUA_PDU_t pdu;
Harald Weltee2e5d4d2015-09-10 23:49:45 +020088 struct msgb *msg = rua_msgb_alloc();
89 asn_enc_rval_t rval;
Harald Welte656ad302015-09-10 18:33:47 +020090 ssize_t encoded;
91
92 memset(&pdu, 0, sizeof(pdu));
Harald Welte656ad302015-09-10 18:33:47 +020093 pdu.present = RUA_RUA_PDU_PR_initiatingMessage;
94 pdu.choice.initiatingMessage.procedureCode = procedureCode;
95 pdu.choice.initiatingMessage.criticality = criticality;
96 ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
97
Harald Weltee2e5d4d2015-09-10 23:49:45 +020098 rval = aper_encode_to_buffer(&asn_DEF_RUA_RUA_PDU, &pdu,
99 msg->data, msgb_tailroom(msg));
100 if (rval.encoded < 0) {
101 LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name);
102 msgb_free(msg);
103 return NULL;
Harald Welte656ad302015-09-10 18:33:47 +0200104 }
105
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200106 msgb_put(msg, rval.encoded/8);
107
108 return msg;
Harald Welte656ad302015-09-10 18:33:47 +0200109}
Harald Welte656ad302015-09-10 18:33:47 +0200110
111struct msgb *rua_generate_successful_outcome(
112 e_RUA_ProcedureCode procedureCode,
113 RUA_Criticality_t criticality,
114 asn_TYPE_descriptor_t * td,
115 void *sptr)
116{
117
118 RUA_RUA_PDU_t pdu;
119 struct msgb *msg = rua_msgb_alloc();
120 asn_enc_rval_t rval;
121 int rc;
122
123 memset(&pdu, 0, sizeof(pdu));
124 pdu.present = RUA_RUA_PDU_PR_successfulOutcome;
125 pdu.choice.successfulOutcome.procedureCode = procedureCode;
126 pdu.choice.successfulOutcome.criticality = criticality;
127 rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
128 if (rc < 0) {
129 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
130 msgb_free(msg);
131 return NULL;
132 }
133
134 rval = aper_encode_to_buffer(&asn_DEF_RUA_RUA_PDU, &pdu,
135 msg->data, msgb_tailroom(msg));
136 if (rval.encoded < 0) {
137 LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name);
138 msgb_free(msg);
139 return NULL;
140 }
141
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200142 msgb_put(msg, rval.encoded/8);
Harald Welte656ad302015-09-10 18:33:47 +0200143
144 return msg;
145}
146
147#if 0
148ssize_t rua_generate_unsuccessful_outcome(uint8_t ** buffer,
149 uint32_t * length,
150 e_RUA_ProcedureCode procedureCode,
151 RUA_Criticality_t criticality,
152 asn_TYPE_descriptor_t * td,
153 void *sptr)
154{
155
156 RUA_RUA_PDU_t pdu;
157 ssize_t encoded;
158
159 memset(&pdu, 0, sizeof(pdu));
160
161 pdu.present = RUA_RUA_PDU_PR_unsuccessfulOutcome;
162 pdu.choice.successfulOutcome.procedureCode = procedureCode;
163 pdu.choice.successfulOutcome.criticality = criticality;
164 ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
165
166 if ((encoded =
167 aper_encode_to_new_buffer(&asn_DEF_RUA_RUA_PDU, 0, &pdu,
168 (void **)buffer)) < 0) {
169 return -1;
170 }
171
172 *length = encoded;
173
174 return encoded;
175}
176#endif
177
178RUA_IE_t *rua_new_ie(RUA_ProtocolIE_ID_t id,
179 RUA_Criticality_t criticality,
180 asn_TYPE_descriptor_t * type, void *sptr)
181{
182
183 RUA_IE_t *buff;
184
185 if ((buff = malloc(sizeof(*buff))) == NULL) {
186 // Possible error on malloc
187 return NULL;
188 }
189 memset((void *)buff, 0, sizeof(*buff));
190
191 buff->id = id;
192 buff->criticality = criticality;
193
194 ANY_fromType_aper(&buff->value, type, sptr);
195
196 if (asn1_xer_print)
197 if (xer_fprint(stdout, &asn_DEF_RUA_IE, buff) < 0) {
198 free(buff);
199 return NULL;
200 }
201
202 return buff;
203}