RAR_message
[o-du/l2.git] / src / 5gnrmac / mac_mux.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 #include <stdlib.h>
20 #include <stdint.h>
21
22 /* header include files -- defines (.h) */
23 #include "envopt.h"        /* environment options */
24 #include "envdep.h"        /* environment dependent */
25 #include "envind.h"        /* environment independent */
26 #include "gen.h"           /* general layer */
27 #include "ssi.h"           /* system service interface */
28 #include "cm_hash.h"       /* common hash list */
29 #include "cm_mblk.h"       /* common memory link list library */
30 #include "cm_llist.h"      /* common linked list library */
31 #include "cm_err.h"        /* common error */
32 #include "cm_lte.h"        /* common LTE */
33 #include "lrg.h"           /* Layer manager interface includes*/
34 #include "crg.h"           /* CRG interface includes*/
35 #include "rgu.h"           /* RGU interface includes*/
36 #include "tfu.h"           /* TFU interface includes */
37 #include "rg_sch_inf.h"    /* SCH interface includes */
38 #include "rg_prg.h"       /* PRG (MAC-MAC) interface includes*/
39 #include "rg_env.h"       /* MAC environmental includes*/
40 #include "rg.h"           /* MAC includes*/
41 #include "rg_err.h"       /* MAC error includes*/
42 #include "du_log.h"
43
44 /* header/extern include files (.x) */
45 #include "gen.x"           /* general layer typedefs */
46 #include "ssi.x"           /* system services typedefs */
47 #include "cm5.x"           /* common timers */
48 #include "cm_hash.x"       /* common hash list */
49 #include "cm_lib.x"        /* common library */
50 #include "cm_llist.x"      /* common linked list */
51 #include "cm_mblk.x"       /* memory management */
52 #include "cm_tkns.x"       /* common tokens */
53 #include "cm_lte.x"       /* common tokens */
54 #include "rgu.x"           /* RGU types */
55 #include "tfu.x"           /* RGU types */
56 #include "lrg.x"           /* layer management typedefs for MAC */
57 #include "crg.x"           /* CRG interface includes */
58 #include "rg_sch_inf.x"    /* SCH interface typedefs */
59 #include "rg_prg.x"        /* PRG (MAC-MAC) Interface typedefs */
60 #include "du_app_mac_inf.h"
61 #include "mac.h"
62 #include "rg.x"            /* typedefs for MAC */
63
64 void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
65 {
66    uint32_t  temp;
67    uint8_t   bytePart1;
68    uint32_t  bytePart2;
69    uint8_t   bytePart1Size;
70    uint32_t  bytePart2Size;
71   
72    if(*bitPos - valSize + 1 >= 0)
73    {
74       bytePart1 = (uint8_t)val;
75       bytePart1 = (bytePart1 << (*bitPos -valSize +1));
76       buf[*bytePos] |= bytePart1;
77       if(*bitPos - valSize < 0)
78       {
79          *bitPos = 7;
80          (*bytePos)++;
81       }
82       else
83          *bitPos -= valSize;
84    }
85    else
86    {
87       temp = 0;
88       bytePart1Size = *bitPos +1;
89       bytePart2Size = valSize - bytePart1Size;
90
91       bytePart1 = (val >> bytePart2Size) << (*bitPos -bytePart1Size +1);
92       bytePart2 =  (~((~temp) << bytePart2Size)) & val;
93  
94       buf[*bytePos] |= bytePart1;
95       (*bytePos)++;
96       *bitPos = 7;
97       packBytes(buf, bytePos, bitPos, bytePart2, bytePart2Size);
98    }  
99 }
100
101 void fillRarPdu(RarInfo *rarInfo)
102 {
103    uint8_t   *rarPdu = rarInfo->rarPdu;
104    uint16_t  totalBits;
105    uint8_t   numBytes;
106    uint8_t   bytePos;
107    uint8_t   bitPos;
108
109    /* RAR subheader fields */
110    uint8_t   EBit;
111    uint8_t   TBit;
112    uint8_t   rapId;
113
114    /* RAR payload fields */
115    uint8_t   RBit;
116    uint16_t  timeAdv;
117    uint32_t  ulGrant;
118    uint16_t  tmpCrnti; 
119
120    /* Size(in bits) of RAR subheader files */
121    uint8_t   EBitSize = 1;
122    uint8_t   TBitSize = 1;
123    uint8_t   rapidSize = 6;
124
125    /* Size(in bits) of RAR payload fields */
126    uint8_t   RBitSize = 1;
127    uint8_t   timeAdvSize = 12;
128    uint8_t   ulGrantSize = 27;
129    uint8_t   tmpCrntiSize = 16;
130
131    /* Fill RAR pdu fields */
132    EBit = 0;
133    TBit = 1;
134    rapId = rarInfo->RAPID;
135    
136    RBit = 0;
137    timeAdv = rarInfo->ta;
138    ulGrant = 0; /* this will be done when implementing msg3 */ 
139    tmpCrnti = rarInfo->tcrnti;
140
141    /* Calulating total number of bytes in buffer */
142    totalBits = EBitSize + TBitSize + rapidSize + RBitSize + timeAdvSize \
143      + ulGrantSize + tmpCrntiSize;
144    
145    /* Calulating total number of bytes in buffer */
146    numBytes = totalBits/8;
147    if(totalBits % 8)
148       numBytes += 1;
149     
150         rarInfo->rarPduLen = numBytes;
151
152    /* Initialize buffer */
153    for(bytePos = 0; bytePos < numBytes; bytePos++)
154       rarPdu[bytePos] = 0;
155    
156    bytePos = 0;
157    bitPos = 7;
158
159    /* Packing fields into RAR PDU */
160    packBytes(rarPdu, &bytePos, &bitPos, EBit, EBitSize); 
161    packBytes(rarPdu, &bytePos, &bitPos, TBit, TBitSize);
162    packBytes(rarPdu, &bytePos, &bitPos, rapId, rapidSize);
163    packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize);
164    packBytes(rarPdu, &bytePos, &bitPos, timeAdv, timeAdvSize);
165    packBytes(rarPdu, &bytePos, &bitPos, ulGrant, ulGrantSize);
166    packBytes(rarPdu, &bytePos, &bitPos, tmpCrnti, tmpCrntiSize);
167 }
168
169 /**********************************************************************
170   End of file
171  **********************************************************************/