2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
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_response_wrapper.h>
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32 // "github.com/pkg/errors"
37 maxCellinengNB = 16384
38 maxofNRNeighbours = 1024
39 maxnoofNrCellBands = 32
42 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
43 var info *entities.NrFrequencyInfo
44 info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
46 if freqInfo.sULInformation != nil {
47 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
49 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
50 info.SulInformation.SulTransmissionBandwidth = value
56 if freqInfo.freqBandListNr.list.count > 0 && freqInfo.freqBandListNr.list.count <= maxnoofNrCellBands {
57 count := int(freqInfo.freqBandListNr.list.count)
58 freqBandListNr_slice := (*[1 << 30]*C.FreqBandNrItem_t)(unsafe.Pointer(freqInfo.freqBandListNr.list.array))[:count:count]
59 for _, freqBandNrItem := range freqBandListNr_slice {
60 frequencyBand := &entities.FrequencyBandItem{NrFrequencyBand: uint32(freqBandNrItem.freqBandIndicatorNr)}
62 if freqBandNrItem.supportedSULBandList.list.count > 0 && freqBandNrItem.supportedSULBandList.list.count <= maxnoofNrCellBands {
63 count:= int(freqBandNrItem.supportedSULBandList.list.count)
64 supportedSULBandList_slice := (*[1 << 30]*C.SupportedSULFreqBandItem_t)(unsafe.Pointer(freqBandNrItem.supportedSULBandList.list.array))[:count:count]
65 for _, supportedSULFreqBandItem := range supportedSULBandList_slice {
66 frequencyBand.SupportedSulBands = append(frequencyBand.SupportedSulBands, uint32(supportedSULFreqBandItem.freqBandIndicatorNr))
70 info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
77 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
78 var bw *entities.NrTransmissionBandwidth
80 bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
81 bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
86 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
87 var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
89 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
90 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
95 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
96 fddInfo.DlFreqInfo = info
101 if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
102 fddInfo.UlTransmissionBandwidth = bw
107 if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
108 fddInfo.DlTransmissionBandwidth = bw
116 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
117 var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
119 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
120 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
126 if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
127 tddInfo.TransmissionBandwidth = bw
135 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
136 var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
138 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
139 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
144 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
145 fddInfo.DlarFcnFreqInfo = info
152 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
153 var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
155 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
156 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
164 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
165 var neighbours []*entities.NrNeighbourInformation
167 if neighbour_Information != nil && neighbour_Information.list.count > 0 && neighbour_Information.list.count <= maxofNRNeighbours {
168 count:=int(neighbour_Information.list.count)
169 neighbour_Information_slice := (*[1 << 30]*C.NRNeighbour_Information__Member)(unsafe.Pointer(neighbour_Information.list.array))[:count:count]
170 for _, member := range neighbour_Information_slice {
171 info := &entities.NrNeighbourInformation{NrPci: uint32(member.nrpCI)}
173 //pLMN_Identity:nRcellIdentifier
174 plmnId := C.GoBytes(unsafe.Pointer(member.nrCellID.pLMN_Identity.buf), C.int(member.nrCellID.pLMN_Identity.size))
175 nRcellIdentifier := C.GoBytes(unsafe.Pointer(member.nrCellID.nRcellIdentifier.buf), C.int(member.nrCellID.nRcellIdentifier.size))
176 info.NrCgi = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
178 if member.fiveGS_TAC != nil {
179 info.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.fiveGS_TAC.buf), C.int(member.fiveGS_TAC.size)))
183 if member.configured_TAC != nil {
184 info.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.configured_TAC.buf), C.int(member.configured_TAC.size)))
186 switch member.nRNeighbourModeInfo.present {
187 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_fdd:
188 if fdd, err := getNRNeighbourInformation_ChoiceNRMode_FDDInfo(*(**C.FDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); fdd != nil && err == nil {
189 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
192 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_tdd:
193 if tdd, err := getNRNeighbourInformation_ChoiceNRMode_TDDInfo(*(**C.TDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); tdd != nil && err == nil {
194 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
197 neighbours = append(neighbours, info)
202 return neighbours, nil
205 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
206 var servedNRCells []*entities.ServedNRCell
208 if servedNRcellsManagementList != nil && servedNRcellsManagementList.list.count > 0 && servedNRcellsManagementList.list.count <= maxCellinengNB {
209 count :=int(servedNRcellsManagementList.list.count)
210 servedNRcellsENDCX2ManagementList__Member_slice := (*[1 << 30]*C.ServedNRcellsENDCX2ManagementList__Member)(unsafe.Pointer(servedNRcellsManagementList.list.array))[:count:count]
211 for _, servedNRcellsENDCX2ManagementList__Member := range servedNRcellsENDCX2ManagementList__Member_slice {
212 servedNRCellInfo := servedNRcellsENDCX2ManagementList__Member.servedNRCellInfo
213 servedNRCell := &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{NrPci: uint32(servedNRCellInfo.nrpCI)}}
215 //pLMN_Identity:nRcellIdentifier
216 plmnId := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.pLMN_Identity.buf), C.int(servedNRCellInfo.nrCellID.pLMN_Identity.size))
217 nRcellIdentifier := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.nRcellIdentifier.buf), C.int(servedNRCellInfo.nrCellID.nRcellIdentifier.size))
218 servedNRCell.ServedNrCellInformation.CellId = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
220 if servedNRCellInfo.fiveGS_TAC != nil {
221 servedNRCell.ServedNrCellInformation.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.fiveGS_TAC.buf), C.int(servedNRCellInfo.fiveGS_TAC.size)))
224 if servedNRCellInfo.configured_TAC != nil {
225 servedNRCell.ServedNrCellInformation.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.configured_TAC.buf), C.int(servedNRCellInfo.configured_TAC.size)))
228 if servedNRCellInfo.broadcastPLMNs.list.count > 0 && servedNRCellInfo.broadcastPLMNs.list.count <= maxnoofBPLMNs {
229 count:=int(servedNRCellInfo.broadcastPLMNs.list.count)
230 pLMN_Identity_slice := (*[1 << 30]*C.PLMN_Identity_t)(unsafe.Pointer(servedNRCellInfo.broadcastPLMNs.list.array))[:count:count]
231 for _, pLMN_Identity := range pLMN_Identity_slice {
232 servedNRCell.ServedNrCellInformation.ServedPlmns = append(servedNRCell.ServedNrCellInformation.ServedPlmns, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(pLMN_Identity.buf), C.int(pLMN_Identity.size))))
235 switch servedNRCellInfo.nrModeInfo.present {
236 case C.ServedNRCell_Information__nrModeInfo_PR_fdd:
237 if fdd, err := getnrModeInfoFDDInfo(*(**C.FDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); fdd != nil && err == nil {
238 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
242 case C.ServedNRCell_Information__nrModeInfo_PR_tdd:
243 if tdd, err := getnrModeInfoTDDInfo(*(**C.TDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); tdd != nil && err == nil {
244 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
250 neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
254 servedNRCell.NrNeighbourInfos = neighbours
256 servedNRCells = append(servedNRCells, servedNRCell)
260 return servedNRCells, nil
263 // Populate the GNB structure with data from the pdu
264 // Return the GNB and the associated key which can later be used to retrieve the GNB from the database.
266 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
268 var gnb *entities.Gnb
269 var globalNbId *entities.GlobalNbId
271 if pdu.present == C.E2AP_PDU_PR_successfulOutcome {
272 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
273 successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
274 if successfulOutcome != nil && successfulOutcome.value.present == C.SuccessfulOutcome__value_PR_ENDCX2SetupResponse {
275 endcX2SetupResponse := (*C.ENDCX2SetupResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
276 if endcX2SetupResponse != nil && endcX2SetupResponse.protocolIEs.list.count > 0 {
277 count:=int(endcX2SetupResponse.protocolIEs.list.count)
278 endcX2SetupResponse_IEs_slice := (*[1 << 30]*C.ENDCX2SetupResponse_IEs_t)(unsafe.Pointer(endcX2SetupResponse.protocolIEs.list.array))[:count:count]
279 for _, endcX2SetupResponse_IE := range endcX2SetupResponse_IEs_slice {
280 if endcX2SetupResponse_IE.value.present == C.ENDCX2SetupResponse_IEs__value_PR_RespondingNodeType_EndcX2Setup {
281 respondingNodeType := (*C.RespondingNodeType_EndcX2Setup_t)(unsafe.Pointer(&endcX2SetupResponse_IE.value.choice[0]))
282 switch respondingNodeType.present {
283 case C.RespondingNodeType_EndcX2Setup_PR_respond_en_gNB:
284 en_gNB_ENDCX2SetupReqAckIEs_Container := *(**C.ProtocolIE_Container_119P89_t)(unsafe.Pointer(&respondingNodeType.choice[0]))
285 if en_gNB_ENDCX2SetupReqAckIEs_Container != nil && en_gNB_ENDCX2SetupReqAckIEs_Container.list.count > 0 {
286 count:=int(en_gNB_ENDCX2SetupReqAckIEs_Container.list.count)
287 en_gNB_ENDCX2SetupReqAckIEs_slice := (*[1 << 30]*C.En_gNB_ENDCX2SetupReqAckIEs_t)(unsafe.Pointer(en_gNB_ENDCX2SetupReqAckIEs_Container.list.array))[:count:count]
288 for _, en_gNB_ENDCX2SetupReqAckIE := range en_gNB_ENDCX2SetupReqAckIEs_slice {
289 switch en_gNB_ENDCX2SetupReqAckIE.value.present {
290 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_GlobalGNB_ID:
291 globalGNB_ID := (*C.GlobalGNB_ID_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0]))
292 plmnId := C.GoBytes(unsafe.Pointer(globalGNB_ID.pLMN_Identity.buf), C.int(globalGNB_ID.pLMN_Identity.size))
293 if globalGNB_ID.gNB_ID.present == C.GNB_ID_PR_gNB_ID {
294 gnbIdAsBitString := (*C.BIT_STRING_t)(unsafe.Pointer(&globalGNB_ID.gNB_ID.choice[0]))
295 globalNbId = &entities.GlobalNbId{}
296 globalNbId.NbId = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(gnbIdAsBitString.buf), C.int(gnbIdAsBitString.size)))
297 globalNbId.PlmnId = fmt.Sprintf("%02x", plmnId)
299 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
300 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
302 return globalNbId, nil, err
304 gnb = &entities.Gnb{}
305 gnb.ServedNrCells = servedCells
309 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
318 return globalNbId, gnb, nil
321 func UnpackEndcX2SetupResponseAndExtract(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*entities.GlobalNbId, *entities.Gnb, error) {
322 pdu, err := UnpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
327 defer C.delete_pdu(pdu)
328 return endcX2SetupResponseToProtobuf(pdu)