Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / DEF / e2sim_defs.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 # Copyright 2019 Nokia                                                       *
5 #                                                                            *
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                                    *
9 #                                                                            *
10 #      http://www.apache.org/licenses/LICENSE-2.0                            *
11 #                                                                            *
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.                                             *
17 #                                                                            *
18 ******************************************************************************/
19
20 #include "e2sim_defs.h"
21 #include <getopt.h>
22 #include <sys/time.h>
23 #include <time.h>
24
25 char* time_stamp(void)
26 {
27   timeval curTime;
28   gettimeofday(&curTime, NULL);
29   int milli = curTime.tv_usec / 1000;
30
31   char buffer [80];
32   strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec));
33
34   const int time_buffer_len = 84;
35   static char currentTime[time_buffer_len] = "";
36   snprintf(currentTime, time_buffer_len, "%s:%03d", buffer, milli);
37
38   return currentTime;
39 }
40
41 options_t read_input_options_old(int argc, char* argv[])
42 {
43   options_t options;
44
45   options.server_ip         = (char*)DEFAULT_SCTP_IP;
46   options.server_port       = X2AP_SCTP_PORT;
47
48   // Parse command line options
49   static struct option long_options[] =
50     {
51       {"ipv4", required_argument, 0, 'i'},
52       {"ipv6", required_argument, 0, 'I'},
53       {"port", required_argument, 0, 'p'},
54       {"verbose", no_argument, 0, 'v'},
55     };
56
57     while(1)
58     {
59       int option_index = 0;
60
61       char c = getopt_long(argc, argv, "i:I:p:", long_options, &option_index);
62
63       if(c == -1)
64         break;
65
66       switch(c)
67       {
68         case 'i':
69           options.server_ip = optarg;
70           break;
71         case 'I':
72           break;
73         case 'p':
74           options.server_port = atoi(optarg);
75           if(options.server_port < 1 || options.server_port > 65535)
76           {
77             LOG_E("Invalid port number (%d). Valid values are between 1 and 65535.\n",
78                                                               options.server_port);
79             exit(1);
80           }
81           break;
82
83         default:
84           LOG_E("Error: unknown input option: %c\n", optopt);
85           exit(1);
86       }
87     }
88
89     return options;
90 }
91
92 options_t read_input_options(int argc, char *argv[])
93 {
94   options_t options;
95
96   options.server_ip         = (char*)DEFAULT_SCTP_IP;
97   options.server_port       = X2AP_SCTP_PORT;
98
99   if(argc == 3) //user provided IP and PORT
100   {
101     options.server_ip = argv[1];
102     options.server_port = atoi(argv[2]);
103     if(options.server_port < 1 || options.server_port > 65535) {
104       LOG_E("Invalid port number (%d). Valid values are between 1 and 65535.\n",
105                                   options.server_port);
106       exit(1);
107     }
108   }
109   else if(argc == 2) //user provided only IP
110   {
111     options.server_ip = argv[1];
112   }
113   else if(argc == 1)
114   {
115     options.server_ip = (char*)DEFAULT_SCTP_IP;
116   }
117   else
118   {
119     LOG_I("Unrecognized option.\n");
120     LOG_I("Usage: %s [SERVER IP ADDRESS] [SERVER PORT]\n", argv[0]);
121     exit(1);
122   }
123
124   return options;
125 }