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.
20 // #cgo CFLAGS: -I../asn1codec/inc/ -I../asn1codec/e2ap_engine/
21 // #cgo LDFLAGS: -L ../asn1codec/lib/ -L../asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
22 // #include <asn1codec_utils.h>
23 // #include <x2setup_response_wrapper.h>
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29 // "github.com/pkg/errors"
34 maxCellinengNB = 16384
35 maxofNRNeighbours = 1024
36 maxnoofNrCellBands = 32
39 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
40 var info *entities.NrFrequencyInfo
41 info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
43 if freqInfo.sULInformation != nil {
44 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
46 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
47 info.SulInformation.SulTransmissionBandwidth = value
53 if freqInfo.freqBandListNr.list.count > 0 && freqInfo.freqBandListNr.list.count <= maxnoofNrCellBands {
54 count := int(freqInfo.freqBandListNr.list.count)
55 freqBandListNr_slice := (*[1 << 30]*C.FreqBandNrItem_t)(unsafe.Pointer(freqInfo.freqBandListNr.list.array))[:count:count]
56 for _, freqBandNrItem := range freqBandListNr_slice {
57 frequencyBand := &entities.FrequencyBandItem{NrFrequencyBand: uint32(freqBandNrItem.freqBandIndicatorNr)}
59 if freqBandNrItem.supportedSULBandList.list.count > 0 && freqBandNrItem.supportedSULBandList.list.count <= maxnoofNrCellBands {
60 count:= int(freqBandNrItem.supportedSULBandList.list.count)
61 supportedSULBandList_slice := (*[1 << 30]*C.SupportedSULFreqBandItem_t)(unsafe.Pointer(freqBandNrItem.supportedSULBandList.list.array))[:count:count]
62 for _, supportedSULFreqBandItem := range supportedSULBandList_slice {
63 frequencyBand.SupportedSulBands = append(frequencyBand.SupportedSulBands, uint32(supportedSULFreqBandItem.freqBandIndicatorNr))
67 info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
74 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
75 var bw *entities.NrTransmissionBandwidth
77 bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
78 bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
83 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
84 var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
86 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
87 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
92 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
93 fddInfo.DlFreqInfo = info
98 if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
99 fddInfo.UlTransmissionBandwidth = bw
104 if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
105 fddInfo.DlTransmissionBandwidth = bw
113 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
114 var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
116 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
117 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
123 if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
124 tddInfo.TransmissionBandwidth = bw
132 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
133 var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
135 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
136 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
141 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
142 fddInfo.DlarFcnFreqInfo = info
149 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
150 var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
152 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
153 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
161 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
162 var neighbours []*entities.NrNeighbourInformation
164 if neighbour_Information != nil && neighbour_Information.list.count > 0 && neighbour_Information.list.count <= maxofNRNeighbours {
165 count:=int(neighbour_Information.list.count)
166 neighbour_Information_slice := (*[1 << 30]*C.NRNeighbour_Information__Member)(unsafe.Pointer(neighbour_Information.list.array))[:count:count]
167 for _, member := range neighbour_Information_slice {
168 info := &entities.NrNeighbourInformation{NrPci: uint32(member.nrpCI)}
170 //pLMN_Identity:nRcellIdentifier
171 plmnId := C.GoBytes(unsafe.Pointer(member.nrCellID.pLMN_Identity.buf), C.int(member.nrCellID.pLMN_Identity.size))
172 nRcellIdentifier := C.GoBytes(unsafe.Pointer(member.nrCellID.nRcellIdentifier.buf), C.int(member.nrCellID.nRcellIdentifier.size))
173 info.NrCgi = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
175 if member.fiveGS_TAC != nil {
176 info.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.fiveGS_TAC.buf), C.int(member.fiveGS_TAC.size)))
180 if member.configured_TAC != nil {
181 info.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.configured_TAC.buf), C.int(member.configured_TAC.size)))
183 switch member.nRNeighbourModeInfo.present {
184 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_fdd:
185 if fdd, err := getNRNeighbourInformation_ChoiceNRMode_FDDInfo(*(**C.FDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); fdd != nil && err == nil {
186 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
189 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_tdd:
190 if tdd, err := getNRNeighbourInformation_ChoiceNRMode_TDDInfo(*(**C.TDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); tdd != nil && err == nil {
191 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
194 neighbours = append(neighbours, info)
199 return neighbours, nil
202 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
203 var servedNRCells []*entities.ServedNRCell
205 if servedNRcellsManagementList != nil && servedNRcellsManagementList.list.count > 0 && servedNRcellsManagementList.list.count <= maxCellinengNB {
206 count :=int(servedNRcellsManagementList.list.count)
207 servedNRcellsENDCX2ManagementList__Member_slice := (*[1 << 30]*C.ServedNRcellsENDCX2ManagementList__Member)(unsafe.Pointer(servedNRcellsManagementList.list.array))[:count:count]
208 for _, servedNRcellsENDCX2ManagementList__Member := range servedNRcellsENDCX2ManagementList__Member_slice {
209 servedNRCellInfo := servedNRcellsENDCX2ManagementList__Member.servedNRCellInfo
210 servedNRCell := &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{NrPci: uint32(servedNRCellInfo.nrpCI)}}
212 //pLMN_Identity:nRcellIdentifier
213 plmnId := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.pLMN_Identity.buf), C.int(servedNRCellInfo.nrCellID.pLMN_Identity.size))
214 nRcellIdentifier := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.nRcellIdentifier.buf), C.int(servedNRCellInfo.nrCellID.nRcellIdentifier.size))
215 servedNRCell.ServedNrCellInformation.CellId = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
217 if servedNRCellInfo.fiveGS_TAC != nil {
218 servedNRCell.ServedNrCellInformation.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.fiveGS_TAC.buf), C.int(servedNRCellInfo.fiveGS_TAC.size)))
221 if servedNRCellInfo.configured_TAC != nil {
222 servedNRCell.ServedNrCellInformation.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.configured_TAC.buf), C.int(servedNRCellInfo.configured_TAC.size)))
225 if servedNRCellInfo.broadcastPLMNs.list.count > 0 && servedNRCellInfo.broadcastPLMNs.list.count <= maxnoofBPLMNs {
226 count:=int(servedNRCellInfo.broadcastPLMNs.list.count)
227 pLMN_Identity_slice := (*[1 << 30]*C.PLMN_Identity_t)(unsafe.Pointer(servedNRCellInfo.broadcastPLMNs.list.array))[:count:count]
228 for _, pLMN_Identity := range pLMN_Identity_slice {
229 servedNRCell.ServedNrCellInformation.ServedPlmns = append(servedNRCell.ServedNrCellInformation.ServedPlmns, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(pLMN_Identity.buf), C.int(pLMN_Identity.size))))
232 switch servedNRCellInfo.nrModeInfo.present {
233 case C.ServedNRCell_Information__nrModeInfo_PR_fdd:
234 if fdd, err := getnrModeInfoFDDInfo(*(**C.FDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); fdd != nil && err == nil {
235 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
239 case C.ServedNRCell_Information__nrModeInfo_PR_tdd:
240 if tdd, err := getnrModeInfoTDDInfo(*(**C.TDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); tdd != nil && err == nil {
241 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
247 neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
251 servedNRCell.NrNeighbourInfos = neighbours
253 servedNRCells = append(servedNRCells, servedNRCell)
257 return servedNRCells, nil
260 // Populate the GNB structure with data from the pdu
261 // Return the GNB and the associated key which can later be used to retrieve the GNB from the database.
263 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
265 var gnb *entities.Gnb
266 var globalNbId *entities.GlobalNbId
268 if pdu.present == C.E2AP_PDU_PR_successfulOutcome {
269 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
270 successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
271 if successfulOutcome != nil && successfulOutcome.value.present == C.SuccessfulOutcome__value_PR_ENDCX2SetupResponse {
272 endcX2SetupResponse := (*C.ENDCX2SetupResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
273 if endcX2SetupResponse != nil && endcX2SetupResponse.protocolIEs.list.count > 0 {
274 count:=int(endcX2SetupResponse.protocolIEs.list.count)
275 endcX2SetupResponse_IEs_slice := (*[1 << 30]*C.ENDCX2SetupResponse_IEs_t)(unsafe.Pointer(endcX2SetupResponse.protocolIEs.list.array))[:count:count]
276 for _, endcX2SetupResponse_IE := range endcX2SetupResponse_IEs_slice {
277 if endcX2SetupResponse_IE.value.present == C.ENDCX2SetupResponse_IEs__value_PR_RespondingNodeType_EndcX2Setup {
278 respondingNodeType := (*C.RespondingNodeType_EndcX2Setup_t)(unsafe.Pointer(&endcX2SetupResponse_IE.value.choice[0]))
279 switch respondingNodeType.present {
280 case C.RespondingNodeType_EndcX2Setup_PR_respond_en_gNB:
281 en_gNB_ENDCX2SetupReqAckIEs_Container := *(**C.ProtocolIE_Container_119P89_t)(unsafe.Pointer(&respondingNodeType.choice[0]))
282 if en_gNB_ENDCX2SetupReqAckIEs_Container != nil && en_gNB_ENDCX2SetupReqAckIEs_Container.list.count > 0 {
283 count:=int(en_gNB_ENDCX2SetupReqAckIEs_Container.list.count)
284 en_gNB_ENDCX2SetupReqAckIEs_slice := (*[1 << 30]*C.En_gNB_ENDCX2SetupReqAckIEs_t)(unsafe.Pointer(en_gNB_ENDCX2SetupReqAckIEs_Container.list.array))[:count:count]
285 for _, en_gNB_ENDCX2SetupReqAckIE := range en_gNB_ENDCX2SetupReqAckIEs_slice {
286 switch en_gNB_ENDCX2SetupReqAckIE.value.present {
287 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_GlobalGNB_ID:
288 globalGNB_ID := (*C.GlobalGNB_ID_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0]))
289 plmnId := C.GoBytes(unsafe.Pointer(globalGNB_ID.pLMN_Identity.buf), C.int(globalGNB_ID.pLMN_Identity.size))
290 if globalGNB_ID.gNB_ID.present == C.GNB_ID_PR_gNB_ID {
291 gnbIdAsBitString := (*C.BIT_STRING_t)(unsafe.Pointer(&globalGNB_ID.gNB_ID.choice[0]))
292 globalNbId = &entities.GlobalNbId{}
293 globalNbId.NbId = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(gnbIdAsBitString.buf), C.int(gnbIdAsBitString.size)))
294 globalNbId.PlmnId = fmt.Sprintf("%02x", plmnId)
296 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
297 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
299 return globalNbId, nil, err
301 gnb = &entities.Gnb{}
302 gnb.ServedNrCells = servedCells
306 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
315 return globalNbId, gnb, nil
318 func unpackEndcX2SetupResponseAndExtract(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*entities.GlobalNbId, *entities.Gnb, error) {
319 pdu, err := unpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
324 defer C.delete_pdu(pdu)
325 return endcX2SetupResponseToProtobuf(pdu)