Merging changes from ODU_Dev_Bronze_Rel to master
[o-du/l2.git] / src / du_app / du_sys_info_hdl.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
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
19 /* This file contains ASN codec for MIB and SIB1 msgs */
20
21 #include "du_sys_info_hdl.h"
22 #include "MIB.h"
23 #include "SIB1.h"
24 #include "PLMN-IdentityInfo.h"
25 #include "PLMN-IdentitY.h"
26 #include "MCC.h"
27
28 extern char encBuf[ENC_BUF_MAX_LEN];
29
30 extern DuCfgParams duCfgParam;
31
32 /*******************************************************************
33  *
34  * @brief Builds MIB message in Served Cell Info
35  *
36  * @details
37  *
38  *    Function : BuildMibMsg
39  *
40  *    Functionality: Building MIB message in Served Cell Info
41  *
42  * @params[in] GNB_DU_System_Information *gnbDuSysInfo
43  * @return ROK     - success
44  *         RFAILED - failure
45  *
46  * ****************************************************************/
47 S16 BuildMibMsg()
48 {
49         MIB_t          *mibMsg;
50         asn_enc_rval_t encRetVal;        /* Encoder return value */
51
52         DU_ALLOC(mibMsg, sizeof(MIB_t));
53         if(!mibMsg)
54         {
55                 DU_LOG("DU APP: MIB msg memory allocation failure");
56                 return RFAILED;
57         }
58         mibMsg->systemFrameNumber.size = sizeof(uint8_t);
59         DU_ALLOC(mibMsg->systemFrameNumber.buf, 
60                         mibMsg->systemFrameNumber.size);
61         if(!(mibMsg->systemFrameNumber.buf))
62         {
63                 DU_LOG("DU APP: MIB msg memory allocation failure");
64                 return RFAILED;
65         }
66
67         *(mibMsg->systemFrameNumber.buf) =
68                 duCfgParam.mibParams.sysFrmNum;
69         mibMsg->systemFrameNumber.bits_unused = ODU_VALUE_TWO;
70
71         mibMsg->subCarrierSpacingCommon =\
72                 duCfgParam.mibParams.subCarrierSpacingCommon;
73         mibMsg->ssb_SubcarrierOffset =\
74            duCfgParam.mibParams.ssb_SubcarrierOffset;
75         mibMsg->dmrs_TypeA_Position =\
76                 duCfgParam.mibParams.dmrs_TypeA_Position;
77         mibMsg->pdcch_ConfigSIB1.controlResourceSetZero = \
78            duCfgParam.mibParams.controlResourceSetZero;
79         mibMsg->pdcch_ConfigSIB1.searchSpaceZero = \
80            duCfgParam.mibParams.searchSpaceZero;
81         mibMsg->cellBarred = duCfgParam.mibParams.cellBarred;
82         mibMsg->intraFreqReselection =
83                 duCfgParam.mibParams.intraFreqReselection;
84         mibMsg->spare.size = sizeof(uint8_t);
85         DU_ALLOC(mibMsg->spare.buf,sizeof(uint8_t));
86         if(!mibMsg->spare.buf)
87         {
88                 DU_LOG("DU APP: MIB msg memory allocation failure");
89                 return RFAILED;
90         }
91         *(mibMsg->spare.buf) = SPARE;
92         mibMsg->spare.bits_unused = ODU_VALUE_SEVEN;
93
94         xer_fprint(stdout, &asn_DEF_MIB, mibMsg);
95         cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
96         encBufSize = 0;
97         encRetVal = aper_encode(&asn_DEF_MIB, 0,
98                                    mibMsg, PrepFinalEncBuf, encBuf);
99         printf("\nencbufSize:%d\n", encBufSize);
100         if(encRetVal.encoded    == -1) 
101         {   
102                 DU_LOG("\nDU APP: Could not encode MIB structure(at %s)\n", 
103                                 encRetVal.failed_type?\
104                                 encRetVal.failed_type->name
105                                 :"unknown");
106                 return RFAILED;
107         }  
108
109         /* Print encoded buffer */
110         for(int i=0; i< encBufSize; i++)
111         {
112                 printf("%x\t",encBuf[i]);
113         } 
114         printf("\n");
115
116         /* Free allocated memory */
117         DU_FREE(mibMsg->systemFrameNumber.buf,
118                         mibMsg->systemFrameNumber.size);
119         DU_FREE(mibMsg->spare.buf, mibMsg->spare.size);
120         DU_FREE(mibMsg, sizeof(MIB_t));
121
122    return ROK;
123
124 }
125
126 /*******************************************************************
127  *
128  * @brief Builds CellIdentity  within SIB1 message
129  *
130  * @details
131  *
132  *    Function : BuildCellIdentity
133  *
134  *    Functionality: Building RAN area code in SIB1 message 
135  *
136  * @params[in] RAN_AreaCode_t **ranAreaCode 
137  * @return ROK     - success
138  *         RFAILED - failure
139  *
140  * ****************************************************************/
141 S16 BuildCellIdentity(CellIdentity_t  *cellIdentity)
142 {
143    cellIdentity->size = ODU_VALUE_FIVE*sizeof(uint8_t);
144         cellIdentity->bits_unused = ODU_VALUE_FOUR;
145
146         DU_ALLOC(cellIdentity->buf, cellIdentity->size);
147         if(!cellIdentity->buf)
148         {   
149                 DU_LOG("DU APP: CellIdentity memory allocation failure");
150                 return RFAILED;
151         }   
152    *cellIdentity->buf = duCfgParam.sib1Params.cellIdentity;
153
154    return ROK;
155 }
156 /*******************************************************************
157  *
158  * @brief Builds RANAC within SIB1 message
159  *
160  * @details
161  *
162  *    Function : BuildRanac
163  *
164  *    Functionality: Building RAN area code in SIB1 message 
165  *
166  * @params[in] RAN_AreaCode_t **ranAreaCode 
167  * @return ROK     - success
168  *         RFAILED - failure
169  *
170  * ****************************************************************/
171 S16 BuildRanac(RAN_AreaCode_t **ranAreaCode)
172 {
173    RAN_AreaCode_t *ranac;
174    DU_ALLOC(ranac, sizeof(RAN_AreaCode_t));
175         if(!ranac)
176         {   
177                 DU_LOG("DU APP: RANAC memory allocation failure");
178                 return RFAILED;
179         }   
180    *ranac = duCfgParam.sib1Params.ranac;
181         *ranAreaCode = ranac;
182    return ROK;
183 }
184 /*******************************************************************
185  *
186  * @brief Builds TAC within SIB1 message
187  *
188  * @details
189  *
190  *    Function : BuildTac
191  *
192  *    Functionality: Building Tracking area code in SIB1 message 
193  *
194  * @params[in] TrackingAreaCode_t **tac 
195  * @return ROK     - success
196  *         RFAILED - failure
197  *
198  * ****************************************************************/
199 S16 BuildTac(TrackingAreaCode_t **trackAreaCode)
200 {
201    TrackingAreaCode_t *tac;
202   
203
204    DU_ALLOC(tac, sizeof(TrackingAreaCode_t));
205         if(!tac)
206         {   
207                 DU_LOG("DU APP: TAC memory allocation failure");
208                 return RFAILED;
209         }   
210
211         tac->size = ODU_VALUE_THREE*sizeof(uint8_t);
212         DU_ALLOC(tac->buf, tac->size);
213         if(!tac->buf)
214         {   
215                 DU_LOG("DU APP: TAC memory allocation failure");
216                 return RFAILED;
217         }   
218         *(tac->buf) = duCfgParam.sib1Params.tac;
219         tac->bits_unused = ODU_VALUE_ZERO;
220         *trackAreaCode = tac;
221
222    return ROK;
223 }
224
225 /*******************************************************************
226  *
227  * @brief Builds PLMN list within SIB1 message
228  *
229  * @details
230  *
231  *    Function : BuildPlmnList
232  *
233  *    Functionality: Building PLMN list in SIB1 message 
234  *
235  * @params[in] CellAccessRelatedInfo_t *cellAccessInfo 
236  * @return ROK     - success
237  *         RFAILED - failure
238  *
239  * ****************************************************************/
240 S16 BuildPlmnList(CellAccessRelatedInfo_t *cellAccessInfo)
241 {
242    U8                 idx, idx1, idx2;
243         U8                 elementCnt;
244         TrackingAreaCode_t **tac;
245         RAN_AreaCode_t     **ranac;
246         CellIdentity_t     *cellIdentity;
247         struct PLMN_IdentityInfo__plmn_IdentityList
248                 *plmnIdInfo;
249
250         DU_ALLOC(cellAccessInfo->plmn_IdentityList.list.array,
251                         cellAccessInfo->plmn_IdentityList.list.size);
252         if(!cellAccessInfo->plmn_IdentityList.list.array)
253         {   
254                 DU_LOG("DU APP: BuildPlmnList memory allocation failure");
255                 return RFAILED;
256         }   
257    elementCnt = cellAccessInfo->plmn_IdentityList.list.count; 
258         for(idx=0; idx<elementCnt; idx++)
259         {   
260                 DU_ALLOC(cellAccessInfo->plmn_IdentityList.list.array[idx],
261                                 sizeof(PLMN_IdentityInfo_t));
262                 if(!cellAccessInfo->plmn_IdentityList.list.array[idx])
263                 {
264                         DU_LOG("DU APP: BuildPlmnList memory allocation failure");
265                         return RFAILED;
266                 }
267         }
268
269         idx = 0;
270         /* PLMN list */
271         elementCnt = ODU_VALUE_ONE;
272
273         plmnIdInfo = &cellAccessInfo->plmn_IdentityList.list.array[idx]->\
274                                          plmn_IdentityList;
275         plmnIdInfo->list.count = elementCnt;
276         plmnIdInfo->list.size  = elementCnt * sizeof(PLMN_IdentitY_t);
277         DU_ALLOC(plmnIdInfo->list.array, plmnIdInfo->list.size);
278         if(!plmnIdInfo->list.array)
279         {
280                 DU_LOG("DU APP: BuildPlmnList memory allocation failure");
281                 return RFAILED;
282         }
283         for(idx1=0; idx1<elementCnt; idx1++)
284         {
285                 DU_ALLOC(plmnIdInfo->list.array[idx1],
286                                 sizeof(PLMN_IdentitY_t));
287                 if(!(plmnIdInfo->list.array[idx1]))
288                 {
289                         DU_LOG("DU APP: BuildPlmnList memory allocation failure");
290                         return RFAILED;
291                 }
292         }
293         idx1 = 0;
294         DU_ALLOC(plmnIdInfo->list.array[idx1]->mcc,
295                         sizeof(MCC_t));
296         if(!plmnIdInfo->list.array[idx1]->mcc)
297         {
298                 DU_LOG("DU APP: BuildPlmnList memory allocation failure");
299                 return RFAILED;
300         }
301
302    elementCnt = ODU_VALUE_THREE;
303         plmnIdInfo->list.array[idx1]->mcc->list.count = elementCnt;
304         plmnIdInfo->list.array[idx1]->mcc->list.size =\
305                 elementCnt * sizeof(MCC_MNC_Digit_t);
306         DU_ALLOC(plmnIdInfo->list.array[idx1]->mcc->list.array,
307                         plmnIdInfo->list.array[idx1]->mcc->list.size)
308         if(!(plmnIdInfo->list.array[idx1]->mcc->list.array))
309         {
310                 DU_LOG("DU APP: BuildPlmnList memory allocation failure");
311                 return RFAILED;
312         }
313         for(idx2=0; idx2<elementCnt; idx2++)
314         {
315                 DU_ALLOC(plmnIdInfo->list.array[idx1]->mcc->list.array[idx2],
316                                 sizeof(MCC_MNC_Digit_t));
317                 if(!plmnIdInfo->list.array[idx1]->mcc->list.array[idx2])
318                 {
319                         DU_LOG("DU APP: BuildPlmnList memory allocation failure");
320                         return RFAILED;
321                 }
322                 *(plmnIdInfo->list.array[idx1]->mcc->list.array[idx2])=\
323                         duCfgParam.sib1Params.plmn.mcc[idx2];
324         }
325         idx2 = 0;
326         plmnIdInfo->list.array[idx1]->mnc.list.count = elementCnt;
327         plmnIdInfo->list.array[idx1]->mnc.list.size =\
328                 elementCnt * sizeof(MCC_MNC_Digit_t);
329         DU_ALLOC(plmnIdInfo->list.array[idx1]->mnc.list.array,
330                         plmnIdInfo->list.array[idx1]->mnc.list.size);
331         if(!plmnIdInfo->list.array[idx1]->mnc.list.array)
332         {
333                 DU_LOG("DU APP: BuildPlmnList memory allocation failure");
334                 return RFAILED;
335         }
336         for(idx2=0; idx2<elementCnt; idx2++)
337         {
338                 DU_ALLOC(plmnIdInfo->list.array[idx1]->mnc.list.array[idx2],
339                                 sizeof(MCC_MNC_Digit_t));
340                 if(!plmnIdInfo->list.array[idx1]->mnc.list.array[idx2])
341                 {
342                         DU_LOG("DU APP: BuildPlmnList memory allocation failure");
343                         return RFAILED;
344                 }
345                 *(plmnIdInfo->list.array[idx1]->mnc.list.array[idx2])=\
346                         duCfgParam.sib1Params.plmn.mnc[idx2];
347         }
348
349    /* Tracking Area Code */
350    tac = &cellAccessInfo->plmn_IdentityList.list.array[idx]->trackingAreaCode;
351         BuildTac(tac);
352
353    /* RANAC */
354         ranac = &cellAccessInfo->plmn_IdentityList.list.array[idx]->ranac;
355         BuildRanac(ranac);
356
357    /* CellIdentity */
358    cellIdentity =\
359         &cellAccessInfo->plmn_IdentityList.list.array[idx]->cellIdentity;
360         BuildCellIdentity(cellIdentity);
361
362         /* cellReservedForOperatorUse */
363         cellAccessInfo->plmn_IdentityList.list.array[idx]->\
364                 cellReservedForOperatorUse = \
365                 duCfgParam.sib1Params.cellResvdForOpUse;
366
367  
368    return ROK;
369 }
370
371 /*******************************************************************
372  *
373  * @brief Builds SIB message in Served Cell Info
374  *
375  * @details
376  *
377  *    Function : BuildSib1Msg
378  *
379  *    Functionality: Building SIB message in Served Cell Info
380  *
381  * @params[in] GNB_DU_System_Information *gnbDuSysInfo
382  * @return ROK     - success
383  *         RFAILED - failure
384  *
385  * ****************************************************************/
386 S16 BuildSib1Msg()
387 {
388         SIB1_t                   *sib1Msg;
389         CellAccessRelatedInfo_t  *cellAccessInfo;
390         U8                       elementCnt;
391         U8                       idx, idx1, idx2;
392         asn_enc_rval_t           encRetVal;
393         TrackingAreaCode_t       *tac;
394         RAN_AreaCode_t           *ranac;
395         CellIdentity_t           *cellIdentity;
396         struct PLMN_IdentityInfo__plmn_IdentityList
397                 *plmnIdInfo;
398
399         DU_ALLOC(sib1Msg, sizeof(SIB1_t));
400         if(!sib1Msg)
401         {   
402                 DU_LOG("DU APP: SIB1 msg memory allocation failure");
403                 return RFAILED;
404         }   
405         elementCnt = ODU_VALUE_ONE;
406
407    /* PLMN list */
408         cellAccessInfo = &sib1Msg->cellAccessRelatedInfo;
409         cellAccessInfo->plmn_IdentityList.list.count =
410                 elementCnt;
411         cellAccessInfo->plmn_IdentityList.list.size
412                 = elementCnt * sizeof(PLMN_IdentityInfo_t);
413
414    BuildPlmnList(cellAccessInfo);
415
416    xer_fprint(stdout, &asn_DEF_SIB1, sib1Msg);
417
418         /* Encode the F1SetupRequest type as APER */
419         cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
420         encBufSize = 0;
421         encRetVal = aper_encode(&asn_DEF_SIB1, 0, sib1Msg, PrepFinalEncBuf,\
422                         encBuf);
423         printf("\nencbufSize: %d\n", encBufSize);
424         if(encRetVal.encoded == -1)
425         {
426                 DU_LOG("\nDU APP : Could not encode SIB1 structure (at %s)\n",\
427                                 encRetVal.failed_type ?
428                                 encRetVal.failed_type->name :
429                                 "unknown");
430                 return RFAILED;
431         }
432         for(int i=0; i< encBufSize; i++)
433         {
434                 printf("%x\t",encBuf[i]);
435         }
436         printf("\n");
437
438         /* Free allocated memeory */
439         /* Free PLMN List */
440
441         for(idx=0; idx<cellAccessInfo->plmn_IdentityList.list.count; idx++)
442         {
443                 plmnIdInfo = &cellAccessInfo->plmn_IdentityList.list.array[idx]->\
444                                                  plmn_IdentityList;
445                 for(idx1=0; idx1<plmnIdInfo->list.count; idx1++)
446                 {
447                         for(idx2=0; idx2<plmnIdInfo->list.array[idx1]->mnc.list.count; idx2++)
448                         {
449                                 DU_FREE(plmnIdInfo->list.array[idx1]->mcc->list.array[idx2],
450                                                 sizeof(MCC_MNC_Digit_t));
451                                 DU_FREE(plmnIdInfo->list.array[idx1]->mnc.list.array[idx2],
452                                                 sizeof(MCC_MNC_Digit_t));
453                         }
454                         DU_FREE(plmnIdInfo->list.array[idx1]->mcc->list.array,
455                                         plmnIdInfo->list.array[idx1]->mcc->list.size);
456                         DU_FREE(plmnIdInfo->list.array[idx1]->mnc.list.array,
457                                         plmnIdInfo->list.array[idx1]->mnc.list.size);
458                         DU_FREE(plmnIdInfo->list.array[idx1]->mcc,
459                                         sizeof(MCC_t));
460                         DU_FREE(plmnIdInfo->list.array[idx1],
461                                         sizeof(PLMN_IdentitY_t));
462                 }
463                 DU_FREE(cellAccessInfo->plmn_IdentityList.list.array[idx],
464                                 sizeof(PLMN_IdentityInfo_t));
465                 cellIdentity =\
466                                                   &cellAccessInfo->plmn_IdentityList.list.array[idx]->cellIdentity;
467                 DU_FREE(cellIdentity->buf, cellIdentity->size);
468
469                 ranac = cellAccessInfo->plmn_IdentityList.list.array[idx]->ranac;
470                 DU_FREE(ranac, sizeof(RAN_AreaCode_t));
471
472                 tac = cellAccessInfo->plmn_IdentityList.list.array[idx]->trackingAreaCode;
473                 DU_FREE(tac->buf, tac->size);
474                 DU_FREE(tac, sizeof(TrackingAreaCode_t));
475         }
476         DU_FREE(cellAccessInfo->plmn_IdentityList.list.array,
477                         cellAccessInfo->plmn_IdentityList.list.size);
478
479         DU_FREE(sib1Msg, sizeof(SIB1_t));
480    return ROK;
481
482 }
483
484 /**********************************************************************
485   End of file
486  **********************************************************************/