Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | |
| 3 | #include <osmocom/core/msgb.h> |
| 4 | |
| 5 | #include "HNBAP-PDU.h" |
| 6 | #include "hnbap_common.h" |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 7 | #include "hnbgw.h" |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 8 | |
| 9 | int asn_debug = 0; |
| 10 | int asn1_xer_print = 0; |
| 11 | |
| 12 | static struct msgb *hnbap_msgb_alloc(void) |
| 13 | { |
| 14 | return msgb_alloc(1024, "HNBAP Tx"); |
| 15 | } |
| 16 | |
| 17 | #if 0 |
| 18 | ssize_t s1ap_generate_initiating_message(uint8_t ** buffer, |
| 19 | uint32_t * length, |
| 20 | e_ProcedureCode procedureCode, |
| 21 | Criticality_t criticality, |
| 22 | asn_TYPE_descriptor_t * td, void *sptr) |
| 23 | { |
| 24 | |
| 25 | HNBAP_PDU_t pdu; |
| 26 | ssize_t encoded; |
| 27 | |
| 28 | memset(&pdu, 0, sizeof(HNBAP_PDU_t)); |
| 29 | |
| 30 | pdu.present = HNBAP_PDU_PR_initiatingMessage; |
| 31 | pdu.choice.initiatingMessage.procedureCode = procedureCode; |
| 32 | pdu.choice.initiatingMessage.criticality = criticality; |
| 33 | ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr); |
| 34 | |
| 35 | if (asn1_xer_print) |
| 36 | xer_fprint(stdout, &asn_DEF_HNBAP_PDU, (void *)&pdu); |
| 37 | |
| 38 | if ((encoded = |
| 39 | aper_encode_to_new_buffer(&asn_DEF_HNBAP_PDU, 0, &pdu, |
| 40 | (void **)buffer)) < 0) { |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | *length = encoded; |
| 45 | return encoded; |
| 46 | } |
| 47 | #endif |
| 48 | |
Harald Welte | 339b8e2 | 2015-08-30 23:08:32 +0200 | [diff] [blame] | 49 | struct msgb *hnbap_generate_successful_outcome( |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 50 | e_ProcedureCode procedureCode, |
| 51 | Criticality_t criticality, |
| 52 | asn_TYPE_descriptor_t * td, |
| 53 | void *sptr) |
| 54 | { |
| 55 | |
| 56 | HNBAP_PDU_t pdu; |
| 57 | struct msgb *msg = hnbap_msgb_alloc(); |
| 58 | asn_enc_rval_t rval; |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 59 | int rc; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 60 | |
| 61 | memset(&pdu, 0, sizeof(HNBAP_PDU_t)); |
| 62 | pdu.present = HNBAP_PDU_PR_successfulOutcome; |
| 63 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 64 | pdu.choice.successfulOutcome.criticality = criticality; |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 65 | rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 66 | if (rc < 0) { |
| 67 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 68 | msgb_free(msg); |
| 69 | return NULL; |
| 70 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 71 | |
| 72 | rval = aper_encode_to_buffer(&asn_DEF_HNBAP_PDU, &pdu, |
Harald Welte | 1c1c53c | 2015-09-07 22:41:02 +0200 | [diff] [blame] | 73 | msg->data, msgb_tailroom(msg)); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 74 | if (rval.encoded < 0) { |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 75 | LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 76 | msgb_free(msg); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | msgb_put(msg, rval.encoded); |
| 81 | |
| 82 | return msg; |
| 83 | } |
| 84 | |
| 85 | #if 0 |
Harald Welte | 339b8e2 | 2015-08-30 23:08:32 +0200 | [diff] [blame] | 86 | ssize_t s1ap_generate_unsuccessful_outcome(uint8_t ** buffer, |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 87 | uint32_t * length, |
| 88 | e_ProcedureCode procedureCode, |
| 89 | Criticality_t criticality, |
| 90 | asn_TYPE_descriptor_t * td, |
| 91 | void *sptr) |
| 92 | { |
| 93 | |
| 94 | HNBAP_PDU_t pdu; |
| 95 | ssize_t encoded; |
| 96 | |
| 97 | memset(&pdu, 0, sizeof(HNBAP_PDU_t)); |
| 98 | |
| 99 | pdu.present = HNBAP_PDU_PR_unsuccessfulOutcome; |
| 100 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 101 | pdu.choice.successfulOutcome.criticality = criticality; |
| 102 | ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 103 | |
| 104 | if ((encoded = |
| 105 | aper_encode_to_new_buffer(&asn_DEF_HNBAP_PDU, 0, &pdu, |
| 106 | (void **)buffer)) < 0) { |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | *length = encoded; |
| 111 | |
| 112 | return encoded; |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | IE_t *hnbap_new_ie(ProtocolIE_ID_t id, |
| 117 | Criticality_t criticality, |
| 118 | asn_TYPE_descriptor_t * type, void *sptr) |
| 119 | { |
| 120 | |
| 121 | IE_t *buff; |
| 122 | |
| 123 | if ((buff = malloc(sizeof(IE_t))) == NULL) { |
| 124 | // Possible error on malloc |
| 125 | return NULL; |
| 126 | } |
| 127 | memset((void *)buff, 0, sizeof(IE_t)); |
| 128 | |
| 129 | buff->id = id; |
| 130 | buff->criticality = criticality; |
| 131 | |
| 132 | ANY_fromType_aper(&buff->value, type, sptr); |
| 133 | |
| 134 | if (asn1_xer_print) |
| 135 | if (xer_fprint(stdout, &asn_DEF_IE, buff) < 0) { |
| 136 | free(buff); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | return buff; |
| 141 | } |