Changing status to connected state after timeout.
[ric-plt/e2mgr.git] / E2Manager / converters / 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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package converters
22
23 // #cgo CFLAGS: -I../3rdparty/asn1codec/inc/ -I../3rdparty/asn1codec/e2ap_engine/
24 // #cgo LDFLAGS: -L ../3rdparty/asn1codec/lib/ -L../3rdparty/asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
25 // #include <asn1codec_utils.h>
26 // #include <x2setup_request_wrapper.h>
27 import "C"
28 import (
29         "e2mgr/logger"
30         "fmt"
31         "github.com/pkg/errors"
32         "unsafe"
33 )
34
35 type X2PduRefinedResponse struct {
36         PduPrint string
37 }
38
39 //func unpackX2apPduUPer(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) {
40 //      pdu := C.new_pdu(C.ulong(allocationBufferSize))
41 //
42 //      if pdu == nil {
43 //              return nil, errors.New("allocation failure (pdu)")
44 //      }
45 //
46 //      logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPduUPer - Packed pdu(%d):%x", packedBufferSize, packedBuf)
47 //
48 //      errBuf := make([]C.char, maxMessageBufferSize)
49 //      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) {
50 //              return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0])))
51 //      }
52 //
53 //      if logger.DebugEnabled() {
54 //              C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
55 //              logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPduUPer - PDU: %v  packed size:%d", C.GoString(&errBuf[0]), packedBufferSize)
56 //      }
57 //
58 //      return pdu, nil
59 //}
60
61 func UnpackX2apPdu(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) {
62         pdu := C.new_pdu(C.ulong(allocationBufferSize))
63
64         if pdu == nil {
65                 return nil, errors.New("allocation failure (pdu)")
66         }
67
68         logger.Infof("#x2apPdu_asn1_unpacker.UnpackX2apPdu - Packed pdu(%d):%x", packedBufferSize, packedBuf)
69
70         errBuf := make([]C.char, maxMessageBufferSize)
71         if !C.per_unpack_pdu(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0]) {
72                 return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0])))
73         }
74
75         if logger.DebugEnabled() {
76                 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
77                 logger.Debugf("#x2apPdu_asn1_unpacker.UnpackX2apPdu - PDU: %v  packed size:%d", C.GoString(&errBuf[0]), packedBufferSize)
78         }
79
80         return pdu, nil
81 }
82
83 func UnpackX2apPduAndRefine(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*X2PduRefinedResponse, error) {
84         pdu, err := UnpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
85         if err != nil {
86                 return nil, err
87         }
88
89         defer C.delete_pdu(pdu)
90
91         var refinedResponse = ""
92         if logger.DebugEnabled() {
93                 buf := make([]C.char, 16*maxMessageBufferSize)
94                 C.asn1_pdu_printer(pdu, C.size_t(len(buf)), &buf[0])
95                 refinedResponse  = C.GoString(&buf[0])
96         }
97
98         return &X2PduRefinedResponse{PduPrint: refinedResponse}, nil
99 }
100
101
102