From 86a308bbd6ebb54a930f3bffb3f515cba03d792d Mon Sep 17 00:00:00 2001
From: Raymond Knopp <raymond.knopp@eurecom.fr>
Date: Wed, 3 Jan 2018 06:58:28 -0800
Subject: [PATCH] added MAC header handling for TX

---
 openair2/LAYER2/MAC/defs.h          | 84 +++++++++++++++++++++--------
 openair2/LAYER2/MAC/ue_procedures.c | 35 +++++++++++-
 2 files changed, 97 insertions(+), 22 deletions(-)

diff --git a/openair2/LAYER2/MAC/defs.h b/openair2/LAYER2/MAC/defs.h
index a750fc4643..ee8bc56197 100644
--- a/openair2/LAYER2/MAC/defs.h
+++ b/openair2/LAYER2/MAC/defs.h
@@ -237,31 +237,73 @@ typedef struct {
 } __attribute__((__packed__))SCH_SUBHEADER_FIXED;
 
 
-// Panos:
+
 /*!\brief  MAC subheader long  with 24bit DST field */
 typedef struct {
-	uint8_t   V:4; //Version number: Possible values "0001", "0010", "0011" based on TS36.321 section 6.2.3.
-	uint32_t  SRC:24; //Prose UE source ID. Size 24 bits.
-	uint32_t  DST:24; //Prose UE destination ID. Size 16 or 24 bits.
-	uint8_t   LCID:5;
-	uint8_t   L:7;	// Length field indicating the size of the corresponding SDU in byes. Not sure about the size of this field (7).
-	uint8_t	  F:1;
-	uint8_t	  E:1;
-	uint8_t	  R:1;
-}__attribute__((__packed__))SLSCH_SUBHEADER_24_Bit_DST;
-
-/*!\brief  MAC subheader long  with 16bit DST field */
+  uint8_t   R0:4; 
+  uint8_t   V:4;//Version number: Possible values "0001", "0010", "0011" based on TS36.321 section 6.2.3.
+  uint8_t  SRC07; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC815; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC1623; //Prose UE source ID. Size 24 bits.
+  uint8_t  DST07; //Prose UE destination ID. Size 24 bits.
+  uint8_t  DST815; //Prose UE destination ID. Size 24 bits.
+  uint8_t  DST1623; //Prose UE destination ID. Size 24 bits.
+  uint8_t  LCID:5;
+  uint8_t  E:1;
+  uint8_t  R1:2;
+  uint8_t  L:7;	// Length field indicating the size of the corresponding SDU in bytes. 
+  uint8_t  F:1;
+}__attribute__((__packed__))SLSCH_SUBHEADER_24_Bit_DST_SHORT;
+
+/*!\brief  MAC subheader long  with 24bit DST field */
 typedef struct {
-	uint8_t   V:4; //Version number: Possible values "0001", "0010", "0011" based on TS36.321 section 6.2.3.
-	uint32_t  SRC:24; //Prose UE source ID. Size 24 bits.
-	uint32_t  DST:16; //Prose UE destination ID. Size 16 or 24 bits.
-	uint8_t   LCID:5;
-	uint8_t   L:7;	// Length field indicating the size of the corresponding SDU in byes. Not sure about the size of this field (7).
-	uint8_t	  F:1;
-	uint8_t	  E:1;
-	uint8_t	  R:1;
-}__attribute__((__packed__))SLSCH_SUBHEADER_16_Bit_DST;
+  uint8_t   R0:4; 
+  uint8_t   V:4;//Version number: Possible values "0001", "0010", "0011" based on TS36.321 section 6.2.3.
+  uint8_t  SRC07; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC815; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC1623; //Prose UE source ID. Size 24 bits.
+  uint8_t  DST07; //Prose UE destination ID. Size 24 bits.
+  uint8_t  DST815; //Prose UE destination ID. Size 24 bits.
+  uint8_t  DST1623; //Prose UE destination ID. Size 24 bits.
+  uint8_t  LCID:5;
+  uint8_t  E:1;
+  uint8_t  R1:2;
+  uint8_t  L_MSB:7;	// Length field indicating the size of the corresponding SDU in bytes. 
+  uint8_t  F:1;
+  uint8_t  L_LSB:8;
+}__attribute__((__packed__))SLSCH_SUBHEADER_24_Bit_DST_LONG;
 
+/*!\brief  MAC subheader long  with 24bit DST field */
+typedef struct {
+  uint8_t   R0:4; 
+  uint8_t   V:4;//Version number: Possible values "0001", "0010", "0011" based on TS36.321 section 6.2.3.
+  uint8_t  SRC07; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC815; //Prose UE source ID. Size 24 bits.
+  uint8_t  DST07; //Prose UE destination ID. Size 16 bits.
+  uint8_t  DST815; //Prose UE destination ID. Size 16 bits.
+  uint8_t  LCID:5;
+  uint8_t  E:1;
+  uint8_t  R1:2;
+  uint8_t  L:7;	// Length field indicating the size of the corresponding SDU in bytes. 
+  uint8_t  F:1;
+}__attribute__((__packed__))SLSCH_SUBHEADER_16_Bit_DST_SHORT;
+
+/*!\brief  MAC subheader long  with 24bit DST field */
+typedef struct {
+  uint8_t   R0:4; 
+  uint8_t   V:4;//Version number: Possible values "0001", "0010", "0011" based on TS36.321 section 6.2.3.
+  uint8_t  SRC07; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC815; //Prose UE source ID. Size 24 bits.
+  uint8_t  SRC1623; //Prose UE source ID. Size 24 bits.
+  uint8_t  DST07; //Prose UE destination ID. Size 16 bits.
+  uint8_t  DST815; //Prose UE destination ID. Size 16 bits.
+  uint8_t  LCID:5;
+  uint8_t  E:1;
+  uint8_t  R1:2;
+  uint8_t  L_MSB:7;	// Length field indicating the size of the corresponding SDU in bytes. 
+  uint8_t  F:1;
+  uint8_t  L_LSB:8;
+}__attribute__((__packed__))SLSCH_SUBHEADER_16_Bit_DST_LONG;
 
 /*!\brief  mac control element: short buffer status report for a specific logical channel group ID*/
 typedef struct {
diff --git a/openair2/LAYER2/MAC/ue_procedures.c b/openair2/LAYER2/MAC/ue_procedures.c
index 35b76a0650..f0fef4cf9d 100644
--- a/openair2/LAYER2/MAC/ue_procedures.c
+++ b/openair2/LAYER2/MAC/ue_procedures.c
@@ -2724,11 +2724,44 @@ SLSCH_t *ue_get_slsch(module_id_t module_idP,int CC_id,frame_t frameP,sub_frame_
 				    MBMS_FLAG_NO,
 				    3,				    
 				    req,
-				    (char*)ue->slsch_pdu.payload);
+				    (char*)(ue->slsch_pdu.payload + sizeof(SLSCH_SUBHEADER_24_Bit_DST_LONG)));
       
+      // Notes: 1. hard-coded to 24-bit destination format for now
+      //        2. LCID hard-coded to 3
+      //        3. SRC/DST IDs with debug values
       if (sdu_length > 0) {
 	LOG_I(MAC,"SFN.SF %d.%d : got %d bytes from Sidelink buffer (%d requested)\n",frameP,subframeP,sdu_length,req);
 	slsch->payload = (unsigned char*)ue->slsch_pdu.payload;
+	if (sdu_length < 128) { 
+	  slsch->payload++;
+	  SLSCH_SUBHEADER_24_Bit_DST_SHORT *shorth= (SLSCH_SUBHEADER_24_Bit_DST_SHORT *)slsch->payload;
+	  shorth->F=0;
+	  shorth->L=sdu_length;
+	  shorth->E=1;
+	  shorth->LCID=3;
+	  shorth->SRC07=0x12;
+	  shorth->SRC815=0x34;
+	  shorth->SRC1623=0x56;
+	  shorth->DST07=0x78;
+	  shorth->DST815=0x9A;
+	  shorth->DST1623=0xBC;
+	  shorth->V=0x1;
+	}
+	else {
+	  SLSCH_SUBHEADER_24_Bit_DST_LONG *longh= (SLSCH_SUBHEADER_24_Bit_DST_LONG *)slsch->payload;
+	  longh->F=1;
+	  longh->L_LSB=sdu_length&0xff;
+	  longh->L_MSB=(sdu_length>>8)&0x7f;
+	  longh->E=1;
+	  longh->LCID=3;
+	  longh->SRC07=0x12;
+	  longh->SRC815=0x34;
+	  longh->SRC1623=0x56;
+	  longh->DST07=0x78;
+	  longh->DST815=0x9A;
+	  longh->DST1623=0xBC;
+	  longh->V=0x1;
+	}
 	slsch->rvidx   = 0;
 	slsch->payload_length = TBS;
 	// fill in SLSCH configuration
-- 
2.26.2