e2a8498ef35cf9b654d292c7b22a23263f5ba25b
[ric-app/rc.git] / control / rcE2SmRc.go
1 package control
2
3 /*
4 #include <e2sm/wrapper.h>
5 #cgo LDFLAGS: -le2smwrapper -lm
6 #cgo CFLAGS: -I/usr/local/include/e2sm
7 */
8 import "C"
9
10 import (
11         "errors"
12         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
13         "unsafe"
14         _ "log"
15         _"encoding/binary"
16         "encoding/hex"
17         "strings"
18         "strconv"
19 )
20
21 type E2sm struct {
22 }
23
24 func (c *E2sm) SetRicControlHeader(buffer []byte, ueIdData *UEid, ricControlStyleType int64, ricControlActionID int64) (newBuffer []byte, err error) {
25
26         xapp.Logger.Info("SetRicControlHeader Enter \n")
27         cptr := unsafe.Pointer(&buffer[0])
28         //cptr_ueIDbuf := unsafe.Pointer(&ueIDbuf[0])
29
30         lplmnIdBuf := strings.Join(strings.Fields(ueIdData.pLMNIdentitybuf), "")
31         lIntvar, _ := strconv.Atoi(lplmnIdBuf)
32         xapp.Logger.Info("lIntvar = %d\n", lIntvar)
33
34         lIntegerByte := ConverIntegerToByte(lIntvar)
35         //ml.MavLog(ml.INFO, lTransId, " lIntegerByte = %v\n", lIntegerByte)
36         xapp.Logger.Info(" lIntegerByte = %v\n", lIntegerByte)
37         lOutByte := get_bytepack_plmnId(lIntegerByte)
38         xapp.Logger.Info("lOutByte Len:%d\n", len(lOutByte))
39         xapp.Logger.Info("lOutByte = %02X\n", lOutByte)
40         cptrRanParameterValue := unsafe.Pointer(&lOutByte[0])
41         
42
43                 pLMNIdentity := C.CString(ueIdData.pLMNIdentitybuf)
44         defer C.free(unsafe.Pointer(pLMNIdentity))
45                 pLMNIdentity_size := C.size_t(len(ueIdData.pLMNIdentitybuf))
46                 aMFRegionID := C.CString(ueIdData.aMFRegionIDbuf)
47         defer C.free(unsafe.Pointer(aMFRegionID))
48                 aMFSetID := C.CString(ueIdData.aMFSetIDbuf)
49         defer C.free(unsafe.Pointer(aMFSetID))
50                 aMFPointer := C.CString(ueIdData.aMFPointerbuf)
51         defer C.free(unsafe.Pointer(aMFPointer))
52
53         uEID_C := C.struct_uEID{
54                 amf_UE_NGAP_Id: C.long(ueIdData.amf_UE_NGAP_Id),
55                 pLMNIdentity : pLMNIdentity,
56                 pLMNIdentity_size : pLMNIdentity_size,
57                 aMFRegionID : aMFRegionID,
58                 aMFRegionID_size : 1, //AMFRegionID ::= BIT STRING (SIZE(8))
59                 aMFSetID : aMFSetID,
60                 aMFSetID_size: 2, //BIT STRING (SIZE(10))
61                 aMFPointer : aMFPointer,
62                 aMFPointer_size : 1,  //BIT STRING (SIZE(6))
63         }
64
65         xapp.Logger.Debug("uEID_C amf_UE_NGAP_Id  = %l",uEID_C.amf_UE_NGAP_Id)
66         xapp.Logger.Debug("uEID_C pLMNIdentity = %s",uEID_C.pLMNIdentity)
67         xapp.Logger.Debug("uEID_C aMFRegionID %s = ",uEID_C.aMFRegionID)
68         xapp.Logger.Debug("uEID_C aMFSetID  %s = ",uEID_C.aMFSetID)
69         xapp.Logger.Debug("uEID_C aMFPointer %s = ",uEID_C.aMFPointer)
70
71         size := C.e2sm_encode_ric_control_header(cptr, C.size_t(len(buffer)),&uEID_C ,(*C.long)(&ueIdData.F1AP_id[0]),C.size_t(len(ueIdData.F1AP_id)),(*C.long)(&ueIdData.E1AP_id[0]),C.size_t(len(ueIdData.E1AP_id)),(C.long)(ricControlStyleType), (C.long)(ricControlActionID), cptrRanParameterValue,C.size_t(len(lOutByte)))
72
73         //size := C.e2sm_encode_ric_control_header(cptr, C.size_t(len(buffer)),&uEID_C ,(*C.long)(&ueIdData.F1AP_id[0]),(*C.long)(&ueIdData.E1AP_id[0]),(C.long)(ricControlStyleType), (C.long)(ricControlActionID))
74         
75         if size < 0 {
76                 return make([]byte, 0), errors.New("e2sm wrapper is unable to set EventTriggerDefinition due to wrong or invalid input")
77         }
78         newBuffer = C.GoBytes(cptr, (C.int(size)+7)/8)
79         xapp.Logger.Info("E2sm SetRicControlHeader is success")
80         return
81 }
82
83 func (c *E2sm) SetRicControlMessage(buffer []byte, targetPrimaryCell int64, targetCell int64, nrCGIOrECGI int64, nrOrEUtraCell int64, ranParameterValue []byte) (newBuffer []byte, err error) {
84         
85         xapp.Logger.Info("SetRicControlMessagei Enter  ")
86
87         cptr := unsafe.Pointer(&buffer[0])
88         cptrRanParameterValue := unsafe.Pointer(&ranParameterValue[0])
89
90         /*
91         lIntvar, _ := strconv.Atoi(string(ranParameterValue))
92         xapp.Logger.Info("lIntvar %d\n", lIntvar)
93         lIntegerByte := ConverIntegerToByte(lIntvar)
94         xapp.Logger.Info("lIntegerByte %v\n", lIntegerByte)
95         lOutByte := get_bytepack_plmnId(lIntegerByte)
96         xapp.Logger.Info("lOutByte Len:%d\n", len(lOutByte))
97         xapp.Logger.Info("lOutByte %02X\n", lOutByte)
98         cptrRanParameterValue := unsafe.Pointer(&lOutByte[0])
99         xapp.Logger.Info("cptrRanParameterValue = %v\n ", cptrRanParameterValue)
100         */
101
102         size := C.e2sm_encode_ric_control_message(cptr, C.size_t(len(buffer)), C.long(targetPrimaryCell),
103                 C.long(targetCell), C.long(nrOrEUtraCell), C.long(nrCGIOrECGI), cptrRanParameterValue, C.size_t(len(ranParameterValue)))
104
105         if size < 0 {
106                 return make([]byte, 0), errors.New("e2sm wrapper is unable to set RicControlMessage due to wrong or invalid input")
107         }
108         newBuffer = C.GoBytes(cptr, (C.int(size)+7)/8) //TOCHECK: if C.int(size) is returning bits not bytes to get correct size of the buffer.
109         xapp.Logger.Info("E2sm SetRicControlMessage is success")
110         return
111 }
112
113 func (c *E2sm) GetControlOutcome(buffer []byte) (controlOutcome *ControlOutcomeMsg, err error) {
114         cptr := unsafe.Pointer(&buffer[0])
115         controlOutcome = &ControlOutcomeMsg{}
116         //lTransId := gTransIdDefVal
117         //ml.MavLog(ml.INFO, lTransId, "Decode Control Outcome: ")
118         xapp.Logger.Info("Decode Control Outcome: ")
119         decodedMsg := C.e2sm_decode_ric_call_process_outcome(cptr, C.size_t(len(buffer)))
120         if decodedMsg == nil {
121                 return controlOutcome, errors.New("e2sm wrapper is unable to get ControlOutcome due to wrong or invalid input")
122         }
123         defer C.e2sm_free_ric_call_process_outcome(decodedMsg)
124         //ml.MavLog(ml.INFO, lTransId, "Decoded Control Outcome: ")
125         controlOutcome.ControlOutcomeType = int32(decodedMsg.ric_controlOutcome_formats.present)
126         if controlOutcome.ControlOutcomeType == 1 {
127                 controlOutcome := &ControlOutcomeFormat1{}
128                 controlOutcomeFormat1_C := *(**C.E2SM_RC_ControlOutcome_Format1_t)(unsafe.Pointer(&decodedMsg.ric_controlOutcome_formats.choice[0]))
129                 controlOutcome.ControlOutcomeFormat1ItemCount = int(controlOutcomeFormat1_C.ranP_List.list.count)
130                 controlOutcome.ControlOutcomeFormat1Item = make([]ControlOutcomeFormat1ItemType, controlOutcome.ControlOutcomeFormat1ItemCount +1)
131                 //ml.MavLog(ml.INFO, lTransId, "RIC Control Outcome  Format1 Item Count = %d ", controlOutcome.ControlOutcomeFormat1ItemCount)
132                 for i := 0; i < controlOutcome.ControlOutcomeFormat1ItemCount; i++ {
133                         ControlOutcomeFormat1Item :=  &controlOutcome.ControlOutcomeFormat1Item[i]
134                         var sizeof_E2SM_RC_ControlOutcome_Format1_Item_t *C.E2SM_RC_ControlOutcome_Format1_Item_t
135                         ControlOutcomeFormat1Item_C :=  *(*(**C.E2SM_RC_ControlOutcome_Format1_Item_t)(unsafe.Pointer(uintptr(unsafe.Pointer(controlOutcomeFormat1_C.ranP_List.list.array)) + (uintptr)(i)*unsafe.Sizeof(sizeof_E2SM_RC_ControlOutcome_Format1_Item_t))))
136                         ControlOutcomeFormat1Item.RanParameterID = int64(ControlOutcomeFormat1Item_C.ranParameter_ID)
137                         ControlOutcomeFormat1Item.RANParameterValueType = int(ControlOutcomeFormat1Item_C.ranParameter_value.present)
138                         ranParameterValue_C := (C.RANParameter_Value_t)(ControlOutcomeFormat1Item_C.ranParameter_value)
139                         if ControlOutcomeFormat1Item.RANParameterValueType == 5 {
140                                 //ml.MavLog(ml.INFO, lTransId, "RANParameterValueType  is RANParameter_Value_PR_valueOctS")
141                                 ranParameterValue := &OctetString{}
142                                 ranParameter_C := (*C.OCTET_STRING_t)(unsafe.Pointer(&ranParameterValue_C.choice[0]))
143                                 ranParameterValue.Size = int(ranParameter_C.size)
144                                 ranParameterValue.Buf = C.GoBytes(unsafe.Pointer(ranParameter_C.buf), C.int(ranParameter_C.size))
145                                 ControlOutcomeFormat1Item.RANParameterValue = ranParameterValue
146                                 //ml.MavLog(ml.INFO, lTransId, "RANParameterValue = ", ControlOutcomeFormat1Item.RANParameterValue)
147                         }
148                 }
149         } else {
150                 return controlOutcome, errors.New("Unknown RIC Control Outcome type")
151         }
152         return controlOutcome , err
153 }
154
155 func (c *E2sm) get_plmn_from_mcc_mnc(plmnIdBuf []byte)(bytePackedPlmnId []uint8) {
156         //lTransId := gTransIdDefVal
157         hexResp, _ := hex.DecodeString(string(plmnIdBuf))
158         //ml.MavLog(ml.INFO, lTransId, "get_plmn_from_mcc_mnc plmnIdBuf and hexResp = ", plmnIdBuf,hexResp)
159         var mcc []uint8 = make([]uint8, 3)
160         var mnc []uint8 = make([]uint8, 3)
161
162         mcc[0] = hexResp[0] >> 4
163         mcc[1] = hexResp[0] & 0xf
164         mcc[2] = hexResp[1] >> 4
165         mnc[0] = hexResp[1] & 0xf
166         mnc[1] = hexResp[2] >> 4
167         mnc[2] = hexResp[2] & 0xf
168
169         var tmpPlmnId []uint8 = make([]uint8, 6)
170         tmpPlmnId[0] = mcc[0]
171         tmpPlmnId[1] =  mcc[1]
172         tmpPlmnId[2] =  mcc[2]
173       if len(mcc) == 2 {
174                 tmpPlmnId[3] = 0x0f
175                 tmpPlmnId[4] = mnc[0]
176                 tmpPlmnId[5] = mnc[1]
177         } else {
178                 tmpPlmnId[3] = mnc[0]
179                 tmpPlmnId[4] = mnc[1]
180                 tmpPlmnId[5] = mnc[2]
181         }
182         //ml.MavLog(ml.INFO, lTransId, "\n tmpPlmnId " , tmpPlmnId)
183         bytePackedPlmnId = make([]uint8,3)
184         bytePackedPlmnId[0] = ((tmpPlmnId[1] & 0x0F) << 4) | (tmpPlmnId[0] & 0x0F)
185         bytePackedPlmnId[1] = ((tmpPlmnId[3] & 0x0F) << 4) | (tmpPlmnId[2] & 0x0F)
186         bytePackedPlmnId[2] = ((tmpPlmnId[5] & 0x0F) << 4) | (tmpPlmnId[4] & 0x0F)
187         //ml.MavLog(ml.INFO, lTransId, "\n bytePackedPlmnId " , bytePackedPlmnId)
188         return
189 }
190