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