Restructure O1 module to run as a thread in O-DU High binary [Issue-Id: ODUHIGH-297]
[o-du/l2.git] / src / o1 / UnixSocketClient.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [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 unix socket server and send/recv
20    messages 
21 */
22
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <sys/un.h>
33
34 #include "UnixSocketClient.hpp"
35 #include "GlobalDefs.hpp"
36
37
38 /*******************************************************************
39  *
40  * @brief Constructor
41  *
42  * @details
43  *
44  *    Function : UnixSocketClient
45  *
46  *    Functionality:
47  *      - Constructor intialization
48  *
49  * @params[in] socket path
50  * @return None
51  ******************************************************************/
52 UnixSocketClient::UnixSocketClient(const string& path) 
53                                   : mSockPath(path)
54 {
55
56 }
57
58 /*******************************************************************
59  *
60  * @brief Destructor
61  *
62  * @details
63  *
64  *    Function : ~UnixSocketClient
65  *
66  *    Functionality:
67  *      - Destructor
68  *
69  * @params[in] None
70  * @return None
71  ******************************************************************/
72  UnixSocketClient::~UnixSocketClient()
73  {
74
75  }
76
77 /*******************************************************************
78  *
79  * @brief Open a Unix socket
80  *
81  * @details
82  *
83  *    Function : openSocket
84  *
85  *    Functionality:
86  *      - Opens a Unix socket
87  *
88  * @params[in] void 
89  * @return O1::SUCCESS - success
90  *         O1::FAILURE - failure
91  ******************************************************************/
92 uint8_t UnixSocketClient::openSocket()
93 {
94    /* Create the socket. */
95    mSock = socket (AF_UNIX, SOCK_STREAM, 0);
96    if (mSock < 0)
97    {
98       O1_LOG("\nO1 UnixSocketClient : Error opening socket");
99       return O1::FAILURE;
100    }
101
102    /* Init the sockaddr_un structure */
103    mSockName.sun_family = AF_UNIX;
104    strcpy(mSockName.sun_path, mSockPath.c_str());
105
106    /* Connect to the server */
107    if (0 > connect (mSock,
108                    (struct sockaddr *)&mSockName,
109                    sizeof (mSockName)))
110    {
111       O1_LOG("\nO1 UnixSocketClient : Error connecting");
112       return O1::FAILURE;
113    }
114
115    return O1::SUCCESS;
116
117 }
118 /*******************************************************************
119  *
120  * @brief Send message over Unix socket
121  *
122  * @details
123  *
124  *    Function : sendData
125  *
126  *    Functionality:
127  *      - Sends message over Unix socket
128  *
129  * @params[in] message, size of the message
130  * @return Number of bytes sent
131  *
132  ******************************************************************/
133 int UnixSocketClient::sendData(void* data, const int size)
134 {
135    int nbytes = write (mSock, data, size);
136    if (nbytes < 0)
137    {
138       O1_LOG("\nO1 UnixSocketClient : Error writing. %d bytes sent", nbytes);
139    }
140    return nbytes;
141 }
142
143 /*******************************************************************
144  *
145  * @brief Recieve message over Unix socket
146  *
147  * @details
148  *
149  *    Function : receiveData
150  *
151  *    Functionality:
152  *      - Recieves message over Unix socket
153  *
154  * @params[in] message, size of the message
155  * @return Number of bytes received
156  *
157  ******************************************************************/
158 int UnixSocketClient::receiveData(void* data, const int size)
159 {
160    int nbytes = read (mSock, data, size);
161    if (nbytes < 0)
162    {
163       O1_LOG("\nO1 UnixSocketClient : Error reading. %d bytes sent", nbytes);
164    }
165    return nbytes;
166 }
167
168 /*******************************************************************
169  *
170  * @brief Close the Unix socket
171  *
172  * @details
173  *
174  *    Function : closeSocket
175  *
176  *    Functionality:
177  *      - Closes the Unix socket
178  *
179  * @params[in] void
180  * @return O1::SUCCESS - success
181  *         O1::FAILURE - failure
182  ******************************************************************/
183 uint8_t UnixSocketClient::closeSocket()
184 {
185    if( close(mSock) != 0 )
186       return O1::FAILURE;
187    return O1::SUCCESS;
188 }
189
190 /**********************************************************************
191          End of file
192 **********************************************************************/