d2d_app.c 67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
22 23
/*! \file d2d_app.c
 * \brief An example of how to exchange messages between OAI and D2D App via a control socket
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 * \author Tien-Thinh NGUYEN
 * \date 20/11/2017
 * \version 0.1
 * \email:tien-thinh.nguyen@eurecom.fr
 * \company Eurecom
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include<pthread.h>
#include <arpa/inet.h>
#include <stdbool.h>

#include "d2d_app.h"


static void usage(const char *argv0)
{
	fprintf(stderr,
			"Usage: %s [-s][-r][-g] source_L2ID dest/groupL2ID or -d  \n example: \n %s -s source_L2ID dest_L2ID #starting app as a sender for a 1-1 communication \n "
			"%s -r source_L2ID dest_L2ID #starting app as a receiver for a 1-1 communication \n %s -g source_L2ID group_L2ID #starting app as a sender/receiver for a 1-n communication \n"
			" %s -d  #starting app for discovery mode\n",
			argv0, argv0, argv0, argv0, argv0);
	exit(EXIT_FAILURE);
}




//--------------------------------------------------------
int main(int argc, char *argv[]) {

	//for the socket
	int serverlen;
	struct hostent *server;
	char *hostname;

	//define the running mode
	bool sender_mode = false;
	bool receiver_mode = false;
	bool onetomany_scenario = false;
	bool discovery_mode = false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
72 73 74 75
    //cmd parameters
	char *src_string = NULL;
	char *dst_string = NULL;
	uint32_t src_id, dst_id;
76 77 78 79 80

	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	int status;
	int slrb_id;
	int opt, i, n;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
81
	uint8_t pppp = 0;
82 83 84
	uint8_t type = 0;
	int pc5s_slrb = 0;
	int direct_comm_slrb = 0;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
85 86
	uint32_t group_ip_address = inet_addr("224.0.0.3"); //hardcoded
	uint32_t grp_id = 0x03;//hardcoded
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167


	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	sidelink_pc5s_element *sl_pc5s_msg_rcv = NULL;
	sidelink_pc5s_element *sl_pc5s_msg_send = NULL;
	PC5SignallingMessage *pc5_signalling_message = NULL;
	pc5s_header_t *pc5s_header = NULL;
	PC5DiscoveryMessage *pc5_discovery_message = NULL;


   //get input parameters
	while ((opt = getopt(argc, argv, "srdg")) != -1) {
		switch (opt) {
		case 's': //sender for 1-1 communication
			sender_mode = true;
			break;
		case 'r': //receiver for 1-1 communication
			receiver_mode = true;
			break;
		case 'd': //discovery mode
			discovery_mode = true;
			break;
		case 'g'://group communication
			onetomany_scenario = true;
			break;
		default:
			usage(argv[0]);
			break;
		}
	}

	//except the discovery mode, source and destination L2IDs should be provided
	if (!discovery_mode && (argc - optind < 2))
	{
		fprintf(stderr, "%s: too few arguments\n", argv[0]);
		usage(argv[0]);
	}

	//get the information for source and destination L2ID from command line
	if (!discovery_mode){
		src_string = calloc (1, sizeof(char));
		dst_string = calloc  (1, sizeof(char));
		src_string = argv[optind];
		dst_string = argv[optind+1];
		src_id = (uint32_t) strtol(src_string, NULL, 0);
		dst_id = (uint32_t) strtol(dst_string, NULL, 0);
	}

	//printout the selected scenario
	printf ("--------------------------------------------------\n");
	if (sender_mode) printf ("One-to-one Scenario, Sender\n");
	else if (receiver_mode) printf ("One-to-one Scenario, Receiver\n");
	else if (onetomany_scenario) printf ("One-to-many Scenario\n");
	else if (discovery_mode) printf ("Discovery Scenario\n");

	if (!discovery_mode){
		printf("Source L2ID: 0x%08x \n", src_id);
		printf("Destination/Group L2ID: 0x%08x \n", dst_id);
	}


	//get host information
	hostname = "localhost";
	server = gethostbyname(hostname);
	if (server == NULL) {
		fprintf(stderr,"ERROR, no such host as %s\n", hostname);
		exit(0);
	}

	// create the control socket
	ctrl_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (ctrl_sockfd < 0)
		error("ERROR: Failed to create control socket");
	// build the server's address
	memset(&ctrl_sin, 0, sizeof(struct sockaddr_in));
	ctrl_sin.sin_family = AF_INET;
	bcopy((char *)server->h_addr,
			(char *)&ctrl_sin.sin_addr.s_addr, server->h_length);
	ctrl_sin.sin_port = htons(CONTROL_SOCKET_PORT_NO);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
168
	//create the MAC socket
169 170 171 172 173 174 175 176 177
	mac_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (mac_sockfd < 0)
		error("ERROR: Failed to create MAC socket");
	memset(&mac_sin, 0, sizeof(struct sockaddr_in));
	mac_sin.sin_family = AF_INET;
	bcopy((char *)server->h_addr,
			(char *)&mac_sin.sin_addr.s_addr, server->h_length);
	mac_sin.sin_port = htons(MAC_SOCKET_PORT_NO);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
178
	//create the PDCP socket
179 180 181 182 183 184 185 186 187 188
	pdcp_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (pdcp_sockfd < 0)
		error("ERROR: Failed to create PDCP socket");
	memset(&pdcp_sin, 0, sizeof(struct sockaddr_in));
	pdcp_sin.sin_family = AF_INET;
	bcopy((char *)server->h_addr,
			(char *)&pdcp_sin.sin_addr.s_addr, server->h_length);
	pdcp_sin.sin_port = htons(PDCP_SOCKET_PORT_NO);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
189
	//Init a session with OAI (via control socket)
190 191 192 193 194 195 196
	status = session_init ();
	if (status == UE_STATE_ON_NETWORK ) {
		printf("[UEStatusInformation] UE is on coverage\n");
	} else if (status == UE_STATE_OFF_NETWORK) {
		printf("[UEStatusInformation] UE is out of coverage\n");
	}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
197
	//Discovery Mode
198 199
	if (discovery_mode){
		sleep(10);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
200
		//send PC5_DISCOVERY message via Ctrl socket
201 202 203
		pc5_discovery_message = calloc(1, sizeof(PC5DiscoveryMessage));
		send_PC5D_message(pc5_discovery_message);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
204
		//wait for the replies from other UEs
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		printf("Waiting for incoming PC5_discovery_message from other UEs.... \n");
		memset(receive_buf, 0, BUFSIZE);
		serverlen = sizeof(ctrl_sin);
		while (1) {
			n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
			if (sl_ctrl_msg->type == PC5_DISCOVERY_MESSAGE) {
				printf("Received PC5DiscoveryMessage on socket from OAI\n");
				//store UE Id which then can be used as the destination address of a PC5-S message
				// receiver_id = sl_ctrl_msg->sidelinkPrimitive.pc5_discovery_message.proSeUEId;
			}
		}
	}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
222 223

	//One-to-one Communication
224
	/*
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
225 226
	 * for 1-1 communication,first PC5S signaling messages need to be exchanged between UEs before exchanging the actual data
	 * the call flow is as following:
227 228 229 230 231 232 233 234 235
	 *            UE Sender                                     UE Receiver
	 *                     ----Direct_Communication_Request  -->
	 *                     <---Direct_Security_Mode_Command ----
	 *                     ----Direct_Security_Mode_Complete -->
	 *                     <---Direct_Communication_Accept------
	 *                     <===========Exchanging Data==========>
	 *
	 */

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
236 237 238
	if (sender_mode | receiver_mode) {

		//Initiate a PDCP session between D2D application and OAI
239 240
		status = pdcp_session_init();

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
241
		//PC5S signaling messages for sender
242 243 244
		if (sender_mode) {
			//in order to receive the first message from any node, we need to establish a default RB to receive the PC5S message
			//for the moment, we use only one SLRB_ID for PC5S signaling (instead of using 28,29,30)
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
245
			type = 0; //rx
246 247 248 249 250 251 252 253 254
			status = setup_PC5s_request(type, src_id, 0x00000000);
			if (status > -1 ){
				printf("[PC5S] SRLBs have been established for PC5S\n");
			} else{
				printf("[PC5S] Failed to establish SLRBs for PC5S\n");
			}

            //then another RB which will be used to send message to another node
			//for the moment, we use only one SLRB_ID for PC5S signaling (instead of using 28,29,30)
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
255
			type = 1;//tx
256 257 258 259 260 261 262
			pc5s_slrb = setup_PC5s_request(type, src_id, dst_id);
			if (pc5s_slrb > -1 ){
				printf("[PC5S] SRLB %d have been established for PC5S\n", pc5s_slrb);
			} else{
				printf("[PC5S] Failed to establish SLRBs for PC5S\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
263
			//After that,we can send a PC5-S message over the established SLRB ID [DIRECT COMMUNICATION REQUEST]
264 265 266 267
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Communication Request\n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
268
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING; //PC5S message is transparent to the OAI stack
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			pc5s_header->rb_id = pc5s_slrb; //for the moment, we use only one SLRB for all PC5S messages
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message with a dummy content
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			//allocate the message, fill the content and send
			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message content: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
302
			//Wait for DIRECT SECURITY MODE COMMAND message
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Security Mode Command.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
333
			//send DIRECT SECURITY MODE COMPLETE message
334 335 336 337
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Security Mode Complete\n");
			send_pc5s_message(sl_pc5s_msg_send);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
338
			//Wait for DIRECT COMMUNICATIOIN ACCEPT message
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Communication Accept.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

		}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
371 372 373
		//PC5S signaling messages for receiver
		if (receiver_mode){
			//in order to receive the first message from any node, we need to establish a default RB to receive the PC5S message
374
			//for the moment, we use only one SLRB_ID for PC5S signaling (instead of using 28,29,30)
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
375 376

			type = 0; //rx
377 378 379 380 381 382 383
			status = setup_PC5s_request(type, src_id, 0x00000000);
			if (status > -1 ){
				printf("[PC5S] SRLBs have been established for PC5S\n");
			} else{
				printf("[PC5S] Failed to establish SLRBs for PC5S\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
384
			//Then, wait for DIRECT COMMUNICATION REQUEST from Sender
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Communication Request.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));
			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			//print the content of the received messages
			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
415
			//then establish another RB which will be used to send the reply message to the sender
416 417 418 419 420 421 422 423
			type = 1;//tx
			pc5s_slrb = setup_PC5s_request(type, src_id, dst_id);
			if (pc5s_slrb > -1 ){
				printf("[PC5S] SRLB %d have been established for PC5S\n", pc5s_slrb);
			} else{
				printf("[PC5S] Failed to establish SLRBs for PC5S\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
424
			//send DIRECT SECURITY MODE COMMAND message over PC5-S
425 426 427 428
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Security Mode Command\n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
429
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING;
430 431 432 433 434 435 436 437 438 439 440 441 442 443
			pc5s_header->rb_id = pc5s_slrb; //for the moment, we use only one SLRB for all PC5S messages
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;
			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}
			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
444
			//for debugging purpose, printout the message content
445 446 447 448 449 450 451 452 453 454 455 456
			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
457
            //and send the message
458 459 460
			send_pc5s_message(sl_pc5s_msg_send);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
461
			//wait for DIRECT SECURITY MODE COMPLETE
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Security Mode Complete.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
493
			//send DIRECT COMMUNICATION ACCEPT message over PC5-S
494 495 496 497
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Communication Accept\n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
498
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING;
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
			pc5s_header->rb_id = pc5s_slrb; //for the moment, we use only one SLRB for all PC5S messages
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);

		}

		//After exchanging PC5-S messages (including L2 ID), the sender/receiver can establish a SLRBID for data communication (1-1 or 1-n)
		//it is noted that at this stage, Source ID + Dest ID are known from both sides.

		//we need to create a new RB for 1-1 data communication
		// Send direct establishment request to OAI (for the first unicast flow)
		direct_comm_slrb  = setup_PC5u_direct_request (src_id, dst_id, pppp);
		if (direct_comm_slrb > 0) {
			printf("PC5u has been established with SLRB_ID %i for 1-1 communication, source 0x%08x, destination 0x%08x\n",direct_comm_slrb, src_id, dst_id);
		} else {
			printf("PC5u cannot be established\n");
		}
		printf("------------------------------------------------\n");

		//For 1-n Communication
		sleep(10);
		// Send GroupCommunicationEstablisReq to OAI
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
548
		pppp = 2;
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564

		slrb_id  = setup_PC5u_group_request (src_id, grp_id, group_ip_address, pppp);
		if (slrb_id > 0) {
			printf("PC5u has been established with SLRB_ID %i for 1-n communication, source 0x%08x, group 0x%08x\n",slrb_id, src_id, grp_id);
		} else {
			printf("PC5u cannot be established\n");
		}

		//test the traffic by using Ping from command line
		sleep(10);

		//Uncomment to test the release procedure
/*
		//DIRECT COMMUNICATION RELEASE exchanged between UEs
		if (sender_mode){
			//send DIRECT COMMUNICATION RELEASE
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
565

566 567 568 569
			printf("------------------------------------------------\n");
			printf("Send DIRECT COMMUNICATION RELEASE (UE-UE) \n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
570
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
			pc5s_header->rb_id = pc5s_slrb; //for the moment, we use only one SLRB for all PC5S messages
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
603
			//Wait for DIRECT COMMUNICATION RELEASE ACCEPT
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OTHER ue.... \n");
			printf("Waiting DIRECT COMMUNICATION RELEASE ACCEPT (UE-UE)... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}
		} else if (receiver_mode){

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
635
			//Wait for DIRECT COMMUNICATION RELEASE ACCEPT
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OTHER ue.... \n");
			printf("Waiting DIRECT COMMUNICATION RELEASE (UE-UE)... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
666
			//send DIRECT COMMUNICATION RELEASE ACCEPT
667 668 669 670
			printf("------------------------------------------------\n");
			printf("Send DIRECT COMMUNICATION RELEASE ACCEPT(UE-UE) \n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
671 672
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING;
			pc5s_header->rb_id = pc5s_slrb;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);

		}

		sleep(10);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
707
		//Send a PC5S release request to OAI to release the corresponding SLRB for PC5S communication
708 709 710 711 712 713 714 715 716 717

		status  = release_PC5s_request (pc5s_slrb);
		if (status == PC5S_RELEASE_OK) {
			printf("[PC5S Release] SLRB with ID %i has been released\n",pc5s_slrb);
		} else {
			printf("[PC5S Release] SLRD with ID %i cannot be released\n", pc5s_slrb);
		}

		sleep(10);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
718
		//Send a direct communication release request to OAI to release the corresponding SLRB for PC5U communication
719 720 721 722 723 724 725 726 727 728
		status  = release_PC5u_direct_request (direct_comm_slrb);
		if (status == DIRECT_COMMUNICATION_RELEASE_OK) {
			printf("[Direct Communication Release] SLRB with ID %i has been released\n",direct_comm_slrb);
		} else {
			printf("[Direct Communication Release] SLRD with ID %i cannot be released\n", direct_comm_slrb);
		}

		sleep(10);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
729
		//RE-Establish the connection following the similar process
730 731
		//For sender
		if (sender_mode) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
732
		    //we don't need to establish the default RB for receiving PC5S anymore!
733 734 735 736 737 738 739 740 741
			type = 1;//tx
			pc5s_slrb = setup_PC5s_request(type, src_id, dst_id);
			if (pc5s_slrb > -1 ){
				printf("[PC5S] SRLB %d have been established for PC5S\n", pc5s_slrb);
			} else{
				printf("[PC5S] Failed to establish SLRBs for PC5S\n");
			}


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
742
			//send DIRECT COMMUNICATION REQUEST message over PC5-S
743 744 745 746
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Communication Request\n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
747 748
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING;
			pc5s_header->rb_id = pc5s_slrb;
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
779
			//Then wait for IRECT SECURITY MODE COMMAND message
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Security Mode Command.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
810
			//Then send DIRECT SECURITY MODE COMPLETE message
811 812 813 814
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Security Mode Complete\n");
			send_pc5s_message(sl_pc5s_msg_send);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
815
			//Wait for DIRECT COMMUNICATION ACCEPT message
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Communication Accept.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

		}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
847 848 849
		//For Receiver
		if (receiver_mode){
		    //again we don't need to re-establish the default SLRB for receiving PC5S message from any UE
850

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
851
			//Wait for DIRECT COMMUNICATION REQUEST message
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Communication Request.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
882
            //establish a new SLRB to transmit PC5S messages
883 884 885 886 887 888 889 890
			type = 1;//tx
			pc5s_slrb = setup_PC5s_request(type, src_id, dst_id);
			if (pc5s_slrb > -1 ){
				printf("[PC5S] SRLB %d have been established for PC5S\n", pc5s_slrb);
			} else{
				printf("[PC5S] Failed to establish SLRBs for PC5S\n");
			}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
891
			//and use the established SLRB to send DIRECT SECURITY MODE COMMAND message
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Security Mode Command\n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING; //DirectCommunicationRequest
			pc5s_header->rb_id = pc5s_slrb; //for the moment, we use only one SLRB for all PC5S messages
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
929
			//Wait for DIRECT SECURITY MODE COMPLETE message
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");
			printf("Waiting for Direct Security Mode Complete.... \n");
			// get the reply from OAI
			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(pdcp_sin);
			sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));

			n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
			printf("------------------------------------------------\n");
			printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
			printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
			printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
			printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
			printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
			printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

			if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
				printf("Received on socket from OAI, PC5S message\n");
				printf("Message: \n");
				for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
					printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
				}
				printf("\n");
			}


Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
961
			//send DIRECT SECURITY COMMUNICATION ACCEPT message
962 963 964 965
			printf("------------------------------------------------\n");
			printf("Send PC5 Direct Security Communication Accept\n");
			//fill PC5S header
			pc5s_header = calloc(1, sizeof(pc5s_header_t));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
966
			pc5s_header->traffic_type = TRAFFIC_PC5S_SIGNALLING;
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
			pc5s_header->rb_id = pc5s_slrb; //for the moment, we use only one SLRB for all PC5S messages
			pc5s_header->data_size = sizeof(sidelink_pc5s_element);
			pc5s_header->inst = 0;
			pc5s_header->sourceL2Id = src_id;
			pc5s_header->destinationL2Id = dst_id;

			//fill the message
			pc5_signalling_message = calloc(1, sizeof(PC5SignallingMessage));
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				pc5_signalling_message->bytes[i] = i+1;
			}

			sl_pc5s_msg_send = calloc(1, sizeof(sidelink_pc5s_element));
			memcpy(&sl_pc5s_msg_send->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
			memcpy(&sl_pc5s_msg_send->pc5sPrimitive.pc5_signalling_message, pc5_signalling_message,sizeof(PC5SignallingMessage));

			printf("Send pc5_signalling_message\n");
			printf("Send pc5_signalling_message, pc5-s header traffic_type: %d \n", pc5s_header->traffic_type);
			printf("Send pc5_signalling_message, pc5-s header rb_id: %d \n", pc5s_header->rb_id);
			printf("Send pc5_signalling_message, pc5-s header data_size: %d \n", pc5s_header->data_size);
			printf("Send pc5_signalling_message, pc5-s header inst: %d \n", pc5s_header->inst);
			printf("Send pc5_signalling_message, pc5-s header source L2Id 0x%08x \n", pc5s_header->sourceL2Id);
			printf("Send pc5_signalling_message, pc5-s header destination L2Id 0x%08x \n", pc5s_header->destinationL2Id);
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", pc5_signalling_message->bytes[i]);
			}
			printf("\n");

			send_pc5s_message(sl_pc5s_msg_send);

		}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1000 1001
        //Then establish a SLRB for data communication between two UEs
		// Send direct communication establishment request to OAI (for the first unicast flow)
1002 1003 1004 1005 1006 1007 1008 1009
		direct_comm_slrb  = setup_PC5u_direct_request (src_id, dst_id, pppp);
		if (direct_comm_slrb > 0) {
			printf("PC5u has been established with SLRB_ID %i for 1-1 communication, source 0x%08x, destination 0x%08x\n",direct_comm_slrb, src_id, dst_id);
		} else {
			printf("PC5u cannot be established\n");
		}
		printf("------------------------------------------------\n");

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1010
        //after that, the data can be exchanged between two UEs (e.g., using Ping to generate traffic)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
*/

		//listening to incoming message from OAI (PC5-U)
		while (1) {
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");

			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(ctrl_sin);
			n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
			if (sl_ctrl_msg->type == UE_STATUS_INFO) {
				printf("Received UEStatusInformation on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
				status =  sl_ctrl_msg->sidelinkPrimitive.ue_state;
				if (status == UE_STATE_ON_NETWORK ) {
					printf("[UEStatusInformation] UE is on coverage\n");
				} else if (status == UE_STATE_OFF_NETWORK) {
					printf("[UEStatusInformation] UE is out of coverage\n");
				}
			}
		}

	}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1038 1039 1040
	//One-to-many communication scenario
	//in this scenario, there is no PC5S signaling messages exchanged between UEs
	else if (onetomany_scenario) {
1041 1042

		// Send group establishment request to OAI
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1043 1044
		group_ip_address = inet_addr("224.0.0.3");
		pppp = 2;
1045
		printf("group_ip_address "IPV4_ADDR" \n", IPV4_ADDR_FORMAT(group_ip_address));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1046

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

		slrb_id  = setup_PC5u_group_request (src_id, dst_id, group_ip_address, pppp); //dst_id -> groupd_id
		if (slrb_id > 0) {
			printf("PC5u has been established with SLRB_ID %i for 1-n communication\n",slrb_id);
		} else {
			printf("PC5u cannot be established\n");
		}

		//create thread to receive the status notification
		//  pthread_t ue_state_notification_thread;
		//  if( pthread_create( &ue_state_notification_thread , NULL ,  rcv_UE_status_notification , (void*) NULL) < 0)
		//     printf("ERROR: could not create thread");

		//create thread to generate the traffic (if necessary)
		pthread_t generate_traffic_thread;
		if( pthread_create( &generate_traffic_thread , NULL ,  generate_traffic , (void*) NULL) < 0)
			printf("ERROR: could not create thread");

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1065
		//listen to incoming messages from OAI
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
		int count = 0;
		while (1) {
			printf("------------------------------------------------\n");
			printf("Listening to incoming message from OAI.... \n");

			memset(receive_buf, 0, BUFSIZE);
			serverlen = sizeof(ctrl_sin);
			n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
			if (n < 0)
				error("ERROR: Failed to receive from OAI");

			sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
			if (sl_ctrl_msg->type == UE_STATUS_INFO) {
				printf("Received UEStatusInformation on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
				status =  sl_ctrl_msg->sidelinkPrimitive.ue_state;
				if (status == UE_STATE_ON_NETWORK ) {
					printf("[UEStatusInformation] UE is on coverage\n");
				} else if (status == UE_STATE_OFF_NETWORK) {
					printf("[UEStatusInformation] UE is out of coverage\n");
				}
			}
			/*  uncomment to test release scenarios
			count++;
			if (count == 1) {
				//Send a group communication release request to OAI
				slrb_id = SLRB_TO_BE_RELEASED;
				status  = release_PC5u_group_request (slrb_id);
				if (status == GROUP_COMMUNICATION_RELEASE_OK) {
					printf("SLRB with ID %i has been released\n",slrb_id);
				} else {
					printf("SLRD with ID %i cannot be released\n", slrb_id);
				}
				break;
			}
			*/
		}

	}

	return 0;
}


//--------------------------------------------------------
int session_init()
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	int n;

	printf("------------------------------------------------\n");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1117
	printf("Send SessionInitializationRequest message to OAI (msg type: %d )\n", SESSION_INIT_REQ);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = SESSION_INIT_REQ;
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the initialization message to the OAI
	int serverlen = sizeof(ctrl_sin);
	n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	if (sl_ctrl_msg->type == UE_STATUS_INFO) {
		printf("Connection with OAI is established successfully!\n");
		printf("Received UEStatusInformation on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
		printf("UEStatusInformation, Status: %d\n",sl_ctrl_msg->sidelinkPrimitive.ue_state);
		return sl_ctrl_msg->sidelinkPrimitive.ue_state;
	}
	return 0;
}

//--------------------------------------------------------
int setup_PC5u_group_request(uint32_t src_id, uint32_t grp_id, uint32_t group_ip_address, uint8_t pppp)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	struct GroupCommunicationEstablishReq *group_comm_establish_req = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1151
	int slrb_id = -1;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

	printf("------------------------------------------------\n");
	printf("Send GroupCommunicationEstablishReq message to OAI (msg type: %d )\n",GROUP_COMMUNICATION_ESTABLISH_REQ);
	printf("group_ip_address "IPV4_ADDR" \n", IPV4_ADDR_FORMAT(group_ip_address));
	memset(send_buf,0,BUFSIZE);
	//fill the message
	group_comm_establish_req = calloc(1, sizeof(struct GroupCommunicationEstablishReq));
	group_comm_establish_req->sourceL2Id = src_id;
	group_comm_establish_req->groupL2Id = grp_id;
	group_comm_establish_req->groupIpAddress = group_ip_address;
	group_comm_establish_req->pppp = pppp;

	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = GROUP_COMMUNICATION_ESTABLISH_REQ;
	memcpy(&sl_ctrl_msg->sidelinkPrimitive.group_comm_establish_req, group_comm_establish_req,sizeof(struct GroupCommunicationEstablishReq));
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the message to OAI
	int serverlen = sizeof(ctrl_sin);
	int n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	printf("Received on socket from OAI, msg type: %d\n",sl_ctrl_msg->type);
	if (sl_ctrl_msg->type == GROUP_COMMUNICATION_ESTABLISH_RSP) {
		slrb_id = sl_ctrl_msg->sidelinkPrimitive.slrb_id;
		printf("Received GroupCommunicationEstablishResponse on socket from OAI, slrb %d\n",slrb_id);
		return slrb_id;
	} else {
		return -1;
	}
}

//--------------------------------------------------------
int release_PC5u_group_request(uint8_t slrb_id)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1197
	int status = -1;
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	int n;

	printf("------------------------------------------------\n");
	printf("Send GroupCommunicationReleaseRequest message to OAI (msg type: %d )\n",GROUP_COMMUNICATION_RELEASE_REQ);
	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = GROUP_COMMUNICATION_RELEASE_REQ;
	sl_ctrl_msg->sidelinkPrimitive.slrb_id = slrb_id;
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the GroupCommunicationReleaseRequest message to the OAI
	int serverlen = sizeof(ctrl_sin);
	n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	if (sl_ctrl_msg->type == GROUP_COMMUNICATION_RELEASE_RSP) {
		printf("Received GroupCommunicationReleaseResponse on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
		status = sl_ctrl_msg->sidelinkPrimitive.group_comm_release_rsp;
		printf("GroupCommunicationReleaseResponse, Status: %d\n", status);
		return status;
	} else {
		return -1;
	}

	return 0;
}

//--------------------------------------------------------
void *rcv_UE_status_notification(void *sock)
{
	char receive_buf[BUFSIZE];
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	int serverlen, n, status;

	printf("------------------------------------------------\n");
	printf("Thread to receive UEStateInformation from OAI\n");
	printf("Listening to incoming message from OAI.... \n");


	while (1) {
		memset(receive_buf, 0, BUFSIZE);
		serverlen = sizeof(ctrl_sin);
		n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
		if (n < 0)
			error("ERROR: Failed to receive from OAI");

		sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
		if (sl_ctrl_msg->type == UE_STATUS_INFO) {
			printf("Received UEStatusInformation on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
			status =  sl_ctrl_msg->sidelinkPrimitive.ue_state;
			if (status == UE_STATE_ON_NETWORK ) {
				printf("[UEStatusInformation] UE is on coverage\n");
			} else if (status == UE_STATE_OFF_NETWORK) {
				printf("[UEStatusInformation] UE is out of coverage\n");
			}
		}
	}

	return 0;
}

//--------------------------------------------------------
int    pdcp_session_init()
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	sidelink_pc5s_element *sl_pc5s_msg = NULL;
	pc5s_header_t *pc5s_header = NULL;
	int slrb_id;

	printf("------------------------------------------------\n");
	printf("Initiate a PDCP session \n");
	memset(send_buf,0,BUFSIZE);

	//fill PC5S header
	pc5s_header = calloc(1, sizeof(pc5s_header_t));
	pc5s_header->traffic_type = TRAFFIC_PC5S_SESSION_INIT; //init a new session
	pc5s_header->rb_id = 0;
	pc5s_header->data_size = 0;
	pc5s_header->inst = 0;

	sl_pc5s_msg = calloc(1, sizeof(sidelink_pc5s_element));
	memcpy(&sl_pc5s_msg->pc5s_header, pc5s_header,sizeof(pc5s_header_t));
	memcpy((void *)send_buf, (void *)sl_pc5s_msg, sizeof(sidelink_pc5s_element));

	// send the message to OAI
	int serverlen = sizeof(pdcp_sin);
	int n = sendto(pdcp_sockfd, (char *)send_buf, sizeof(sidelink_pc5s_element), 0, (struct sockaddr *)&pdcp_sin, serverlen);

	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(pdcp_sin);
	n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_pc5s_msg = (sidelink_pc5s_element *) receive_buf;
	if (sl_pc5s_msg->pc5s_header.traffic_type == TRAFFIC_PC5S_SESSION_INIT) {
		if (sl_pc5s_msg->pc5sPrimitive.status == 1)
			printf("A PDCP session has been established successfully! \n");
		return sl_pc5s_msg->pc5sPrimitive.status;
	}
	return 0;
}

//--------------------------------------------------------
int  send_pc5s_message(sidelink_pc5s_element *sl_pc5s_msg )
{
	char send_buf[BUFSIZE];

	memcpy((void *)send_buf, (void *)sl_pc5s_msg, sizeof(sidelink_pc5s_element));
	// send the message to PDCP
	int serverlen = sizeof(pdcp_sin);
	int n = sendto(pdcp_sockfd, (char *)send_buf, sizeof(sidelink_pc5s_element), 0, (struct sockaddr *)&pdcp_sin, serverlen);

	if (n < 0)
		error("ERROR: Failed to send to OAI/PDCP");

}


//--------------------------------------------------------
int setup_PC5s_request(uint8_t type,  uint32_t src_id, uint32_t destination_id)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	struct PC5SEstablishReq *pc5s_establish_req = NULL;
	struct PC5SEstablishRsp *pc5s_establish_rsp = NULL;
	int n;

	printf("------------------------------------------------\n");
	printf("Send PC5SEstablishReq message to OAI (msg type: %d )\n",PC5S_ESTABLISH_REQ);
	memset(send_buf,0,BUFSIZE);
	//fill the message
	pc5s_establish_req = calloc(1, sizeof(struct PC5SEstablishReq));
	pc5s_establish_req->type = type;
	pc5s_establish_req->sourceL2Id = src_id;
	pc5s_establish_req->destinationL2Id = destination_id;

	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = PC5S_ESTABLISH_REQ;
	memcpy(&sl_ctrl_msg->sidelinkPrimitive.pc5s_establish_req, pc5s_establish_req,sizeof(struct PC5SEstablishReq));
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the message to OAI
	int serverlen = sizeof(ctrl_sin);
	n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	printf("Received on socket from OAI, msg type: %d\n",sl_ctrl_msg->type);
	if (sl_ctrl_msg->type == PC5S_ESTABLISH_RSP) {
		printf("[PC5S] SRLBs %d, %d, %d have been established for PC5S\n",sl_ctrl_msg->sidelinkPrimitive.pc5s_establish_rsp.slrbid_lcid28, sl_ctrl_msg->sidelinkPrimitive.pc5s_establish_rsp.slrbid_lcid29, sl_ctrl_msg->sidelinkPrimitive.pc5s_establish_rsp.slrbid_lcid30 );
		return sl_ctrl_msg->sidelinkPrimitive.pc5s_establish_rsp.slrbid_lcid28;
	}
	else return -1;
}


//--------------------------------------------------------
int  send_PC5D_message(PC5DiscoveryMessage *pc5_discovery_message)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	int n;

	printf("------------------------------------------------\n");
	printf("Send PC5D message to OAI (msg type: %d )\n",PC5_DISCOVERY_MESSAGE);
	memset(send_buf,0,BUFSIZE);
	//fill the message
	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = PC5_DISCOVERY_MESSAGE;
	memcpy(&sl_ctrl_msg->sidelinkPrimitive.pc5_discovery_message, pc5_discovery_message,sizeof(PC5DiscoveryMessage));
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the message to OAI
	int serverlen = sizeof(ctrl_sin);
	n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

}



//--------------------------------------------------------
void *process_pc5s_signalling(void *sock)
{
	char receive_buf[BUFSIZE];
	sidelink_pc5s_element *sl_pc5s_msg_rcv = NULL;
	int serverlen, n, status;
	int i;

	printf("------------------------------------------------\n");
	printf("Thread to receive PC5-Signalling from OAI\n");
	printf("Listening to incoming message from OAI.... \n");
	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(pdcp_sin);
	sl_pc5s_msg_rcv = calloc(1, sizeof(sidelink_pc5s_element));
	while (1){
		n = recvfrom(pdcp_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&pdcp_sin, &serverlen);
		if (n < 0)
			error("ERROR: Failed to receive from OAI");

		memcpy((void *)sl_pc5s_msg_rcv, (void *)receive_buf, sizeof(sidelink_pc5s_element));
		printf("------------------------------------------------\n");
		printf("Received on socket from OAI, pc5-s header traffic_type: %d\n",sl_pc5s_msg_rcv->pc5s_header.traffic_type);
		printf("Received on socket from OAI, pc5-s header rb_id: %d\n",sl_pc5s_msg_rcv->pc5s_header.rb_id);
		printf("Received on socket from OAI, pc5-s header inst: %d\n",sl_pc5s_msg_rcv->pc5s_header.inst);
		printf("Received on socket from OAI, pc5-s header data_size: %d\n",sl_pc5s_msg_rcv->pc5s_header.data_size);
		printf("Received on socket from OAI, pc5-s header source L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.sourceL2Id);
		printf("Received on socket from OAI, pc5-s header destination L2Id 0x%08x \n", sl_pc5s_msg_rcv->pc5s_header.destinationL2Id);

		if (sl_pc5s_msg_rcv->pc5s_header.traffic_type == TRAFFIC_PC5S_SIGNALLING) {
			printf("Received on socket from OAI, PC5S message\n");
			printf("Message: \n");
			for (i=0; i<PC5_SIGNALLING_PAYLOAD_SIZE; i++ ){
				printf(" %d", sl_pc5s_msg_rcv->pc5sPrimitive.pc5_signalling_message.bytes[i]);
			}
			printf("\n");
		}
	}
	return 0;
}


//--------------------------------------------------------
int setup_PC5u_direct_request(uint32_t src_id, uint32_t dst_id, uint8_t pppp)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	struct DirectCommunicationEstablishReq *direct_comm_establish_req = NULL;
	int slrb_id;

	printf("------------------------------------------------\n");
	printf("Send DirectCommunicationEstablishReq message to OAI (msg type: %d )\n",DIRECT_COMMUNICATION_ESTABLISH_REQ);
	printf("[DirectCommunicationEstablishReq], source L2Id 0x%08x \n", src_id);
	printf("[DirectCommunicationEstablishReq], destination L2Id 0x%08x \n", dst_id);
	memset(send_buf,0,BUFSIZE);
	//fill the message
	direct_comm_establish_req = calloc(1, sizeof(struct DirectCommunicationEstablishReq));
	direct_comm_establish_req->sourceL2Id = src_id;
	direct_comm_establish_req->destinationL2Id = dst_id;
	direct_comm_establish_req->pppp = pppp;

	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = DIRECT_COMMUNICATION_ESTABLISH_REQ;
	memcpy(&sl_ctrl_msg->sidelinkPrimitive.direct_comm_establish_req, direct_comm_establish_req,sizeof(struct DirectCommunicationEstablishReq));
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the message to OAI
	int serverlen = sizeof(ctrl_sin);
	int n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	printf("Received on socket from OAI, msg type: %d\n",sl_ctrl_msg->type);
	if (sl_ctrl_msg->type == DIRECT_COMMUNICATION_ESTABLISH_RSP) {
		slrb_id = sl_ctrl_msg->sidelinkPrimitive.slrb_id;
		printf("Received DirectCommunicationEstablishResponse on socket from OAI, slrb %d\n",slrb_id);
		return slrb_id;
	} else {
		return -1;
	}
}

//--------------------------------------------------------
int release_PC5u_direct_request(uint8_t slrb_id)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	int status;
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	int n;

	printf("------------------------------------------------\n");
	printf("Send DirectCommunicationReleaseRequest message to OAI (msg type: %d )\n",DIRECT_COMMUNICATION_RELEASE_REQ);
	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = DIRECT_COMMUNICATION_RELEASE_REQ;
	sl_ctrl_msg->sidelinkPrimitive.slrb_id = slrb_id;
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the DirectCommunicationReleaseRequest message to the OAI
	int serverlen = sizeof(ctrl_sin);
	n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	if (sl_ctrl_msg->type == DIRECT_COMMUNICATION_RELEASE_RSP) {
		printf("Received DirectCommunicationReleaseResponse on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
		status = sl_ctrl_msg->sidelinkPrimitive.group_comm_release_rsp;
		printf("DirectCommunicationReleaseResponse, Status: %d\n", status);
		return status;
	} else {
		return -1;
	}

	return 0;
}


//--------------------------------------------------------
int release_PC5s_request(uint8_t slrb_id)
{
	char send_buf[BUFSIZE], receive_buf[BUFSIZE];
	int status;
	struct sidelink_ctrl_element *sl_ctrl_msg = NULL;
	int n;

	printf("------------------------------------------------\n");
	printf("Send PC5SReleaseRequest message to OAI (msg type: %d )\n",PC5S_RELEASE_REQ);
	sl_ctrl_msg = calloc(1, sizeof(struct sidelink_ctrl_element));
	sl_ctrl_msg->type = PC5S_RELEASE_REQ;
	sl_ctrl_msg->sidelinkPrimitive.slrb_id = slrb_id;
	memcpy((void *)send_buf, (void *)sl_ctrl_msg, sizeof(struct sidelink_ctrl_element));

	// send the PC5SReleaseRequest message to the OAI
	int serverlen = sizeof(ctrl_sin);
	n = sendto(ctrl_sockfd, (char *)send_buf, sizeof(struct sidelink_ctrl_element), 0, (struct sockaddr *)&ctrl_sin, serverlen);
	if (n < 0)
		error("ERROR: Failed to send to OAI");

	// get the reply from OAI
	memset(receive_buf, 0, BUFSIZE);
	serverlen = sizeof(ctrl_sin);
	n = recvfrom(ctrl_sockfd, receive_buf, BUFSIZE, 0, (struct sockaddr *)&ctrl_sin, &serverlen);
	if (n < 0)
		error("ERROR: Failed to receive from OAI");

	sl_ctrl_msg = (struct sidelink_ctrl_element *) receive_buf;
	if (sl_ctrl_msg->type == PC5S_RELEASE_RSP) {
		printf("Received PC5SReleaseResponse on socket from OAI (msg type: %d)\n",sl_ctrl_msg->type);
		status = sl_ctrl_msg->sidelinkPrimitive.pc5s_release_rsp;
		printf("PC5SReleaseResponse, Status: %d\n", status);
		return status;
	} else {
		return -1;
	}

	return 0;
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
//--------------------------------------------------------
void *generate_traffic()
{
	//generate PC5-U packet
	//system("ping 224.0.0.1 -I oip0 -c 100");
	//system("ping -b 10.0.0.255 -I oip0 -c 10"); //for broadcast
	//system("ping 10.0.0.2 -I oip0 -m 1 -c 10"); //for unicast traffic
	//system("iperf -c 224.0.0.1 -u -b 0.1M --bind 10.0.0.1 -t 100"); //iperf - multicast
	return 0;
}

//--------------------------------------------------------
// error - wrapper for perror
void error(char *msg) {
	perror(msg);
	exit(1);
}