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