Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | |
| 3 | #include <osmocom/core/msgb.h> |
| 4 | |
| 5 | #include "rua_common.h" |
| 6 | #include "hnbgw.h" |
| 7 | |
| 8 | extern int asn1_xer_print; |
| 9 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 10 | static 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 | |
| 18 | static 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 | |
| 24 | static 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 | |
| 39 | static 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 | |
| 47 | char *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 Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 77 | static struct msgb *rua_msgb_alloc(void) |
| 78 | { |
| 79 | return msgb_alloc(1024, "RUA Tx"); |
| 80 | } |
| 81 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 82 | struct msgb *rua_generate_initiating_message( |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 83 | 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 Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 88 | struct msgb *msg = rua_msgb_alloc(); |
| 89 | asn_enc_rval_t rval; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 90 | ssize_t encoded; |
| 91 | |
| 92 | memset(&pdu, 0, sizeof(pdu)); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 93 | 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 Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 98 | 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 Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 106 | msgb_put(msg, rval.encoded/8); |
| 107 | |
| 108 | return msg; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 109 | } |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 110 | |
| 111 | struct 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 Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 142 | msgb_put(msg, rval.encoded/8); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 143 | |
| 144 | return msg; |
| 145 | } |
| 146 | |
| 147 | #if 0 |
| 148 | ssize_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 | |
| 178 | RUA_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 | } |