irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * |
| 3 | * Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | *******************************************************************************/ |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 18 | package e2pdus |
| 19 | |
| 20 | // #cgo CFLAGS: -I../asn1codec/inc/ -I../asn1codec/e2ap_engine/ |
| 21 | // #cgo LDFLAGS: -L ../asn1codec/lib/ -L../asn1codec/e2ap_engine/ -le2ap_codec -lasncodec |
| 22 | // #include <asn1codec_utils.h> |
| 23 | // #include <x2setup_request_wrapper.h> |
| 24 | import "C" |
| 25 | import ( |
| 26 | "fmt" |
| 27 | "github.com/pkg/errors" |
| 28 | "unsafe" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | ShortMacro_eNB_ID = 18 |
| 33 | Macro_eNB_ID = 20 |
| 34 | LongMacro_eNB_ID = 21 |
| 35 | Home_eNB_ID = 28 |
| 36 | ) |
| 37 | |
| 38 | var PackedEndcX2setupRequest []byte |
| 39 | var PackedX2setupRequest []byte |
| 40 | var PackedEndcX2setupRequestAsString string |
| 41 | var PackedX2setupRequestAsString string |
| 42 | |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 43 | func PreparePackedEndcX2SetupRequest(maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, pLMNId []byte, eNB_Id []byte /*18, 20, 21, 28 bits length*/, bitqty uint, ricFlag []byte) ([]byte, string, error) { |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 44 | packedBuf := make([]byte, maxAsn1PackedBufferSize) |
| 45 | errBuf := make([]C.char, maxAsn1CodecMessageBufferSize) |
| 46 | packedBufSize := C.ulong(len(packedBuf)) |
| 47 | pduAsString := "" |
| 48 | |
| 49 | if !C.build_pack_endc_x2setup_request( |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 50 | (*C.uchar)(unsafe.Pointer(&pLMNId[0])) /*pLMN_Identity*/, |
| 51 | (*C.uchar)(unsafe.Pointer(&eNB_Id[0])), |
| 52 | C.uint(bitqty), |
| 53 | (*C.uchar)(unsafe.Pointer(&ricFlag[0])) /*pLMN_Identity*/, |
| 54 | &packedBufSize, |
| 55 | (*C.uchar)(unsafe.Pointer(&packedBuf[0])), |
| 56 | C.ulong(len(errBuf)), |
| 57 | &errBuf[0]) { |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 58 | return nil, "", errors.New(fmt.Sprintf("packing error: %s", C.GoString(&errBuf[0]))) |
| 59 | } |
| 60 | |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 61 | pdu := C.new_pdu(C.size_t(1)) //TODO: change signature |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 62 | defer C.delete_pdu(pdu) |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 63 | if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) { |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 64 | C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0]) |
| 65 | pduAsString = C.GoString(&errBuf[0]) |
| 66 | } |
| 67 | |
| 68 | return packedBuf[:packedBufSize], pduAsString, nil |
| 69 | } |
| 70 | |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 71 | func PreparePackedX2SetupRequest(maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, pLMNId []byte, eNB_Id []byte /*18, 20, 21, 28 bits length*/, bitqty uint, ricFlag []byte) ([]byte, string, error) { |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 72 | packedBuf := make([]byte, maxAsn1PackedBufferSize) |
| 73 | errBuf := make([]C.char, maxAsn1CodecMessageBufferSize) |
| 74 | packedBufSize := C.ulong(len(packedBuf)) |
| 75 | pduAsString := "" |
| 76 | |
| 77 | if !C.build_pack_x2setup_request( |
| 78 | (*C.uchar)(unsafe.Pointer(&pLMNId[0])) /*pLMN_Identity*/, |
| 79 | (*C.uchar)(unsafe.Pointer(&eNB_Id[0])), |
| 80 | C.uint(bitqty), |
| 81 | (*C.uchar)(unsafe.Pointer(&ricFlag[0])) /*pLMN_Identity*/, |
| 82 | &packedBufSize, |
| 83 | (*C.uchar)(unsafe.Pointer(&packedBuf[0])), |
| 84 | C.ulong(len(errBuf)), |
| 85 | &errBuf[0]) { |
| 86 | return nil, "", errors.New(fmt.Sprintf("packing error: %s", C.GoString(&errBuf[0]))) |
| 87 | } |
| 88 | |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 89 | pdu := C.new_pdu(C.size_t(1)) //TODO: change signature |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 90 | defer C.delete_pdu(pdu) |
irina | b2e5e2b | 2019-09-03 13:42:05 +0300 | [diff] [blame] | 91 | if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) { |
rh362j | ce7bdb1 | 2019-08-28 15:13:48 +0300 | [diff] [blame] | 92 | C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0]) |
| 93 | pduAsString = C.GoString(&errBuf[0]) |
| 94 | } |
| 95 | return packedBuf[:packedBufSize], pduAsString, nil |
| 96 | } |