sync from Azure to LF
[ric-plt/e2mgr.git] / tools / xapp_mock / rmr / rmrCgoUtils.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 rmr
19
20 // #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
21 // #include <rmr/rmr.h>
22 // #include <stdlib.h>
23 import "C"
24 import (
25         "bytes"
26         "encoding/binary"
27         "fmt"
28         "github.com/pkg/errors"
29         "strconv"
30         "strings"
31         "unsafe"
32 )
33
34 /*
35 Allocates an mBuf and initialize it with the content of C.rmr_mbuf_t.
36 The xAction field is assigned a a value without trailing spaces.
37 */
38 func convertToMBuf( m *C.rmr_mbuf_t) (*MBuf, error) {
39         payloadArr := C.GoBytes(unsafe.Pointer(m.payload),C.int(m.len))
40         xActionArr := C.GoBytes(unsafe.Pointer(m.xaction),C.int(RMR_MAX_XACTION_LEN))
41
42         // Trim padding (space and 0)
43         xActionStr := strings.TrimRight(string(xActionArr), "\040\000")
44         xActionArr = []byte(xActionStr)
45
46         mbuf := &MBuf{
47                 MType: int(m.mtype),
48                 Len:   int(m.len),
49                 //Payload: (*[]byte)(unsafe.Pointer(m.payload)),
50                 Payload: payloadArr,
51                 //XAction: (*[]byte)(unsafe.Pointer(m.xaction)),
52                 XAction: xActionArr,
53         }
54
55         meidBuf := make([]byte, RMR_MAX_MEID_LEN)
56         if meidCstr := C.rmr_get_meid(m, (*C.uchar)(unsafe.Pointer(&meidBuf[0]))); meidCstr != nil {
57                 mbuf.Meid =     strings.TrimRight(string(meidBuf), "\000")
58         }
59
60         return mbuf, nil
61 }
62
63 /*
64 Allocates an C.rmr_mbuf_t and initialize it with the content of mBuf.
65 The xAction field is padded with trailing spaces upto capacity
66 */
67 func (ctx *Context) getAllocatedCRmrMBuf( mBuf *MBuf, maxMsgSize int) (cMBuf *C.rmr_mbuf_t, rc error) {
68         var xActionBuf [RMR_MAX_XACTION_LEN]byte
69         var meidBuf[RMR_MAX_MEID_LEN]byte
70
71         cMBuf = C.rmr_alloc_msg(ctx.RmrCtx, C.int(maxMsgSize))
72         cMBuf.mtype = C.int(mBuf.MType)
73         cMBuf.len = C.int(mBuf.Len)
74
75         payloadLen := len(mBuf.Payload)
76         xActionLen := len(mBuf.XAction)
77
78         copy(xActionBuf[:], mBuf.XAction)
79         for i:= xActionLen; i < RMR_MAX_XACTION_LEN; i++{
80                 xActionBuf[i] = '\040' //space
81         }
82
83         // Add padding
84         copy(meidBuf[:], mBuf.Meid)
85         for i:= len(mBuf.Meid); i < RMR_MAX_MEID_LEN; i++{
86                 meidBuf[i] = 0
87         }
88
89         payloadArr := (*[1 << 30]byte)(unsafe.Pointer(cMBuf.payload))[:payloadLen:payloadLen]
90         xActionArr := (*[1 << 30]byte)(unsafe.Pointer(cMBuf.xaction))[:RMR_MAX_XACTION_LEN:RMR_MAX_XACTION_LEN]
91
92         err := binary.Read(bytes.NewReader(mBuf.Payload), binary.LittleEndian, payloadArr)
93         if err != nil {
94                 return nil, errors.New(fmt.Sprintf("#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read payload to allocated RMR message buffer, %s", err))
95         }
96         err = binary.Read(bytes.NewReader(xActionBuf[:]), binary.LittleEndian, xActionArr)
97         if err != nil {
98                 return nil, errors.New(fmt.Sprintf("#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read xAction data to allocated RMR message buffer, %s", err))
99         }
100
101         len := C.rmr_bytes2meid(cMBuf,  (*C.uchar)(unsafe.Pointer(&meidBuf[0])), C.int(RMR_MAX_XACTION_LEN))
102         if int(len) != RMR_MAX_MEID_LEN {
103                 return nil, errors.New(
104                         "#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to copy meid data to allocated RMR message buffer")
105         }
106         return cMBuf,nil
107 }
108
109 func MessageIdToUint(id string) (msgId uint64, err error) {
110         if len(id) == 0 {
111                 msgId, err = 0, nil
112         } else{
113                 msgId, err = strconv.ParseUint(id, 10, 16)
114         }
115         return
116 }