3 * Copyright 2019 AT&T Intellectual Property
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <unistd.h> //for close()
27 #include <arpa/inet.h> //for inet_ntop()
29 #include "e2sim_defs.h"
30 #include "e2sim_sctp.h"
31 #include "x2ap_message_handler.h"
34 #include "Pendulum_asn_codec.h"
35 #include "adruino_serial.h"
39 #include <sys/epoll.h>
41 #include "rmr_wrapper.h"
43 static void pendulum_control_E2_agent(int client_fd)
45 fprintf(stderr, "--------------------------------------\n");
46 fprintf(stderr, "E2 AGENT - START PENDULUM CONTROL\n");
47 fprintf(stderr, "--------------------------------------\n");
54 uint8_t recv_buf[MAX_SCTP_BUFFER];
64 char serial_buffer[MAX_SERIAL_BUFFER];
65 // serial_fd = start_serial_inferface(DEFAULT_BAUDRATE, DEFAULT_SERIAL_PORT);
67 // char *delay_str_prev = "$0#\n";
68 // //char *delay_str_new;
72 // //Always start with 0 delay
73 // serialport_write(serial_fd, "$0#\n");
75 // for(int i = 0; i < MSG_NUM; i++)
79 fprintf(stderr, "----------------\n");
84 // //1.Read from serial
85 // serial_readline(serial_fd, serial_buffer, MAX_SERIAL_BUFFER);
86 // if(serial_buffer[0] == '\n')
88 // //fprintf(stderr, "RECEIVED EOL\n");
91 // //fprintf(stderr, "[Adruino] %s", serial_buffer);
94 snprintf(serial_buffer, sizeof(serial_buffer)-1, "E2 AGENT PING");
98 //2. Encode pendulum angle to ASN1 message
100 len = pendulum_create_asn_msg(&buffer, 0, 0, 0, serial_buffer);
102 //3. Send ASN1 message to socket
103 if(sctp_send_to_socket(client_fd, buffer, (size_t)len) > 0){
104 fprintf(stderr, "Sent ASN1 message to E2 Termination\n");
107 // 4. Receive response from E2 Termination
108 memset(recv_buf, 0, sizeof(recv_buf));
110 recv_len = recv(client_fd, &recv_buf, sizeof(recv_buf), 0);
118 recv_str = pendulum_get_strval(recv_buf, recv_len);
119 fprintf(stderr, "Received response message #%d from xApp: %s\n", count, recv_str);
121 // 5. TODO: Send response to serial
122 // Compare if delay has changed:
123 // if(strcmp(delay_str_prev, recv_str) != 0) {
124 // serial_writeline(serial_fd, recv_str);
127 //serial_writeline(serial_fd, recv_str);
131 f = fopen("arduino_delay.txt", "w");
132 fprintf(f, "%s", recv_str);
135 begin = clock() - begin;
136 rtt = 1000*((double)begin)/CLOCKS_PER_SEC; // in ms
137 fprintf(stderr, "E2Agent-RIC-E2Agent RTT = %f ms\n", rtt);
144 int main(int argc, char *argv[])
146 fprintf(stderr, "E2 AGENT - PENDULUM CONTROL. Version %s\n", VERSION);
148 // char *recv_str = "9";
151 // printf("delay = %d\n", atoi(recv_str));
153 // long delay = 22.5;
155 // printf("delay = %d\n", (int)delay);
158 // test_rmr(); return 0;
159 // test_adruino_serial(); return 0;
161 char* server_ip = DEFAULT_SCTP_IP;
162 int server_port = X2AP_SCTP_PORT;
166 struct sockaddr client_addr;
167 socklen_t client_addr_size;
170 if(argc == 3) //user provided IP and PORT
173 server_port = atoi(argv[2]);
174 if(server_port < 1 || server_port > 65535) {
175 fprintf(stderr, "Invalid port number (%d). Valid values are between 1 and 65535.\n" , server_port);
179 else if(argc == 2) //user provided only IP
185 server_ip = DEFAULT_SCTP_IP;
189 fprintf(stderr, "Unrecognized option.\n");
190 fprintf(stderr, "Usage: %s [SERVER IP ADDRESS] [SERVER PORT]\n", argv[0]);
194 server_fd = sctp_start_server(server_ip, server_port);
196 fprintf(stderr, "Waiting for connection...\n");
197 client_fd = accept(server_fd, &client_addr, &client_addr_size);
204 //Todo: retrieve client ip addr
205 struct sockaddr_in* client_ipv4 = (struct sockaddr_in*)&client_addr;
206 char client_ip_addr[INET_ADDRSTRLEN];
207 inet_ntop(AF_INET, &(client_ipv4->sin_addr), client_ip_addr, INET_ADDRSTRLEN);
209 fprintf(stderr, "New client connected from %s\n", client_ip_addr);
211 // while(1) //put while loop if want to receive from multiple clients
213 uint8_t recv_buf[MAX_SCTP_BUFFER];
216 memset(recv_buf, 0, sizeof(recv_buf));
218 fprintf(stderr, "------------------------\n");
219 recv_len = recv(client_fd, &recv_buf, sizeof(recv_buf), 0);
225 else if(recv_len == 0)
227 fprintf(stderr, "\nConnection from %s closed by remote peer\n", client_ip_addr);
228 if(close(client_fd) == -1)
235 //fprintf(stderr, "Received a message of size %d\n", recv_len);
237 //TODO: check PPID here before calling x2ap handler
239 sctp_data_t response = {NULL, 0};
240 x2ap_eNB_handle_message(recv_buf, recv_len, &response);
242 //=======================================================================
244 assert(response.data != NULL);
245 if(sctp_send_to_socket(client_fd, response.data, (size_t)response.len) > 0){
246 fprintf(stderr, "Sent X2 SETUP RESPONSE \n");
248 perror("send to socket");
252 fprintf(stderr, "X2 Setup Completed \n");
254 //=========================================================================
255 // Pendulum interaction
256 // Send pendulum state to E2 Termination and receive response
257 pendulum_control_E2_agent(client_fd);