Fix licensing issues
[ric-plt/resource-status-manager.git] / RSM / 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
18 package converters
19
20 // #cgo CFLAGS: -I../3rdparty/asn1codec/inc/ -I../3rdparty/asn1codec/e2ap_engine/
21 // #cgo LDFLAGS: -L ../3rdparty/asn1codec/lib/ -L../3rdparty/asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
22 // #include <asn1codec_utils.h>
23 import "C"
24 import (
25         "fmt"
26         "github.com/pkg/errors"
27         "rsm/logger"
28         "unsafe"
29 )
30
31 type Asn1PduUnpacker interface {
32         UnpackX2apPduAsString(packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (string, error)
33 }
34
35 type X2apPduUnpacker struct {
36         logger *logger.Logger
37
38 }
39
40 func NewX2apPduUnpacker(logger *logger.Logger) Asn1PduUnpacker {
41         return &X2apPduUnpacker{logger :logger}
42 }
43
44 func (r X2apPduUnpacker) UnpackX2apPdu(packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) {
45         pdu := C.new_pdu()
46
47         if pdu == nil {
48                 return nil, errors.New("allocation failure (pdu)")
49         }
50
51         r.logger.Infof("#x2apPdu_asn1_unpacker.UnpackX2apPdu - Packed pdu(%d):%x", packedBufferSize, packedBuf)
52
53         errBuf := make([]C.char, maxMessageBufferSize)
54         if !C.per_unpack_pdu(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0]) {
55                 return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0])))
56         }
57
58         if r.logger.DebugEnabled() {
59                 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
60                 r.logger.Debugf("#x2apPdu_asn1_unpacker.UnpackX2apPdu - PDU: %v  packed size:%d", C.GoString(&errBuf[0]), packedBufferSize)
61         }
62
63         return pdu, nil
64 }
65
66 func (r X2apPduUnpacker)UnpackX2apPduAsString(packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (string, error) {
67         pdu, err := r.UnpackX2apPdu(packedBufferSize, packedBuf, maxMessageBufferSize)
68         if err != nil {
69                 return "", err
70         }
71
72         defer C.delete_pdu(pdu)
73
74         buf := make([]C.char, 16*maxMessageBufferSize)
75         C.asn1_pdu_printer(pdu, C.size_t(len(buf)), &buf[0])
76         return C.GoString(&buf[0]), nil
77 }
78
79
80