blob: 3a89e5c82de903fd8523e9e30be629d6e5b9973c [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
2// Copyright 2019 AT&T Intellectual Property
3// Copyright 2019 Nokia
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
18package handlers
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>
24import "C"
25import (
26 "e2mgr/logger"
27 "fmt"
28 "github.com/pkg/errors"
29 "unsafe"
30)
31
32type X2PduRefinedResponse struct {
33 pduPrint string
34}
35
is005q19bf35e2019-08-12 18:59:29 +030036//func unpackX2apPduUPer(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) {
37// pdu := C.new_pdu(C.ulong(allocationBufferSize))
38//
39// if pdu == nil {
40// return nil, errors.New("allocation failure (pdu)")
41// }
42//
43// logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPduUPer - Packed pdu(%d):%x", packedBufferSize, packedBuf)
44//
45// errBuf := make([]C.char, maxMessageBufferSize)
46// if !C.unpack_pdu_aux(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0], C.ATS_UNALIGNED_BASIC_PER) {
47// return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0])))
48// }
49//
50// if logger.DebugEnabled() {
51// C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
52// logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPduUPer - PDU: %v packed size:%d", C.GoString(&errBuf[0]), packedBufferSize)
53// }
54//
55// return pdu, nil
56//}
57
ss412g07ef76d2019-08-12 17:26:40 +030058func unpackX2apPdu(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) {
59 pdu := C.new_pdu(C.ulong(allocationBufferSize))
60
61 if pdu == nil {
62 return nil, errors.New("allocation failure (pdu)")
63 }
64
65 logger.Infof("#x2apPdu_asn1_unpacker.unpackX2apPdu - Packed pdu(%d):%x", packedBufferSize, packedBuf)
66
67 errBuf := make([]C.char, maxMessageBufferSize)
68 if !C.per_unpack_pdu(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0]) {
69 return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0])))
70 }
71
72 if logger.DebugEnabled() {
73 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
74 logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPdu - PDU: %v packed size:%d", C.GoString(&errBuf[0]), packedBufferSize)
75 }
76
77 return pdu, nil
78}
79
80func unpackX2apPduAndRefine(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*X2PduRefinedResponse, error) {
81 pdu, err := unpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
82 if err != nil {
83 return nil, err
84 }
85
86 defer C.delete_pdu(pdu)
87
88 var refinedResponse = ""
89 if logger.DebugEnabled() {
90 buf := make([]C.char, 16*maxMessageBufferSize)
91 C.asn1_pdu_printer(pdu, C.size_t(len(buf)), &buf[0])
92 refinedResponse = C.GoString(&buf[0])
93 }
94
95 return &X2PduRefinedResponse{pduPrint: refinedResponse}, nil
96}
97
98
99