O1 IP PORT configuration for CM .[Issue-Id: ODUHIGH-196]
[o-du/l2.git] / src / o1 / o1_client / TcpClient.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains functions to connect to TCP server and send massages */
20
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netdb.h>
30
31 #include "TcpClient.h"
32 #include "GlobalDefs.h"
33 #include "ssi.h"
34
35 static int s_sock;
36 static struct sockaddr_in s_serverName;
37 static uint16_t s_port;
38 static const char* s_hostName = NULL;
39
40
41 /*******************************************************************
42  *
43  * @brief Initilize the sockadd_in structure
44  *
45  * @details
46  *
47  *    Function : initSockaddr
48  *
49  *    Functionality:
50  *      - Initilizes the sockadd_in structure
51  *
52  * @params[in] void 
53  * @return ROK     - success
54  *         RFAILED - failure
55  ******************************************************************/
56 static uint8_t initSockaddr()
57 {
58
59    struct hostent *hostInfo;
60    struct sockaddr_in *name = &s_serverName;
61    bzero(&s_serverName, sizeof(s_serverName));
62    name->sin_family = AF_INET;
63    name->sin_port = htons (s_port);
64    hostInfo = gethostbyname (s_hostName);
65    if (hostInfo == NULL)
66    {
67       O1_LOG("\nO1 TcpClient : Unknown host %s", s_hostName);
68       return RFAILED;
69    }
70    name->sin_addr = *(struct in_addr *) hostInfo->h_addr;
71    return ROK;
72 }
73
74
75 /*******************************************************************
76  *
77  * @brief Open a TCP socket
78  *
79  * @details
80  *
81  *    Function : openSocket
82  *
83  *    Functionality:
84  *      - Opens a TCP socket
85  *
86  * @params[in] hostname, port 
87  * @return ROK     - success
88  *         RFAILED - failure
89  ******************************************************************/
90 uint8_t openSocket(const char* hostName, const uint16_t port)
91 {
92    /* Create the socket. */
93    s_port = port;
94    s_hostName = hostName;
95    s_sock = socket (PF_INET, SOCK_STREAM, 0);
96    if (s_sock < 0)
97    {
98       O1_LOG("\nO1 TcpClient : Error opening socket");
99       return RFAILED;
100    }
101
102    /* Init the sockaddr_in structure */
103    if (initSockaddr() == RFAILED)
104    {
105       return RFAILED;
106    }
107    /* Connect to the server */
108    if (0 > connect (s_sock,
109                    (struct sockaddr_in *)&s_serverName ,
110                    sizeof (s_serverName)))
111    {
112       O1_LOG("\nO1 TcpClient : Error connecting");
113       return RFAILED;
114    }
115
116    return ROK;
117
118 }
119
120 /*******************************************************************
121  *
122  * @brief Send message over TCP socket
123  *
124  * @details
125  *
126  *    Function : sendData
127  *
128  *    Functionality:
129  *      - Sends message over TCP socket
130  *
131  * @params[in] message, size of the message
132  * @return Number of bytes sent
133  *
134  ******************************************************************/
135 int sendData(void* data, const int size)
136 {
137    int nbytes = write (s_sock, data, size);
138    if (nbytes < 0)
139    {
140       O1_LOG("\nO1 TcpClient : Error writing. %d bytes sent", nbytes);
141    }
142    return nbytes;
143 }
144
145 /*******************************************************************
146  *
147  * @brief Recieve message over TCP socket
148  *
149  * @details
150  *
151  *    Function : receiveData
152  *
153  *    Functionality:
154  *      - Recieves message over TCP socket
155  *
156  * @params[in] message, size of the message
157  * @return Number of bytes received
158  *
159  ******************************************************************/
160 int receiveData(void* data, const int size)
161 {
162    int nbytes = read (s_sock, data, size);
163    if (nbytes < 0)
164    {
165       O1_LOG("\nO1 TcpClient : Error reading. %d bytes sent", nbytes);
166    }
167    return nbytes;
168 }
169
170 /*******************************************************************
171  *
172  * @brief Close the TCP socket
173  *
174  * @details
175  *
176  *    Function : closeSocket
177  *
178  *    Functionality:
179  *      - Closes the TCP socket
180  *
181  * @params[in] message, size of the message
182  * @return ROK     - success
183  *         RFAILED - failure
184  ******************************************************************/
185 uint8_t closeSocket()
186 {
187    if( close(s_sock) != 0 )
188       return RFAILED;
189    return ROK;
190 }
191
192 /**********************************************************************
193          End of file
194 **********************************************************************/