89ec21692daa42a8204fc98cfa2b6391247649dc
[ric-plt/e2mgr.git] / E2Manager / handlers / x2apPdu_asn1_unpacker.go
1 //
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
18 package 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>
24 import "C"
25 import (
26         "e2mgr/logger"
27         "fmt"
28         "github.com/pkg/errors"
29         "unsafe"
30 )
31
32 type X2PduRefinedResponse struct {
33         pduPrint string
34 }
35
36 func unpackX2apPdu(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.Infof("#x2apPdu_asn1_unpacker.unpackX2apPdu - Packed pdu(%d):%x", packedBufferSize, packedBuf)
44
45         errBuf := make([]C.char, maxMessageBufferSize)
46         if !C.per_unpack_pdu(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0]) {
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.unpackX2apPdu - PDU: %v  packed size:%d", C.GoString(&errBuf[0]), packedBufferSize)
53         }
54
55         return pdu, nil
56 }
57
58 func unpackX2apPduAndRefine(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*X2PduRefinedResponse, error) {
59         pdu, err := unpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
60         if err != nil {
61                 return nil, err
62         }
63
64         defer C.delete_pdu(pdu)
65
66         var refinedResponse = ""
67         if logger.DebugEnabled() {
68                 buf := make([]C.char, 16*maxMessageBufferSize)
69                 C.asn1_pdu_printer(pdu, C.size_t(len(buf)), &buf[0])
70                 refinedResponse  = C.GoString(&buf[0])
71         }
72
73         return &X2PduRefinedResponse{pduPrint: refinedResponse}, nil
74 }
75
76
77