1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /* This file contains functions to connect to unix socket server and send/recv
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
34 #include "UnixSocketClient.hpp"
35 #include "GlobalDefs.hpp"
38 /*******************************************************************
44 * Function : UnixSocketClient
47 * - Constructor intialization
49 * @params[in] socket path
51 ******************************************************************/
52 UnixSocketClient::UnixSocketClient(const string& path)
58 /*******************************************************************
64 * Function : ~UnixSocketClient
71 ******************************************************************/
72 UnixSocketClient::~UnixSocketClient()
77 /*******************************************************************
79 * @brief Open a Unix socket
83 * Function : openSocket
86 * - Opens a Unix socket
89 * @return O1::SUCCESS - success
90 * O1::FAILURE - failure
91 ******************************************************************/
92 uint8_t UnixSocketClient::openSocket()
94 /* Create the socket. */
95 mSock = socket (AF_UNIX, SOCK_STREAM, 0);
98 O1_LOG("\nO1 UnixSocketClient : Error opening socket");
102 /* Init the sockaddr_un structure */
103 mSockName.sun_family = AF_UNIX;
104 strcpy(mSockName.sun_path, mSockPath.c_str());
106 /* Connect to the server */
107 if (0 > connect (mSock,
108 (struct sockaddr *)&mSockName,
111 O1_LOG("\nO1 UnixSocketClient : Error connecting");
118 /*******************************************************************
120 * @brief Send message over Unix socket
124 * Function : sendData
127 * - Sends message over Unix socket
129 * @params[in] message, size of the message
130 * @return Number of bytes sent
132 ******************************************************************/
133 int UnixSocketClient::sendData(void* data, const int size)
135 int nbytes = write (mSock, data, size);
138 O1_LOG("\nO1 UnixSocketClient : Error writing. %d bytes sent", nbytes);
143 /*******************************************************************
145 * @brief Recieve message over Unix socket
149 * Function : receiveData
152 * - Recieves message over Unix socket
154 * @params[in] message, size of the message
155 * @return Number of bytes received
157 ******************************************************************/
158 int UnixSocketClient::receiveData(void* data, const int size)
160 int nbytes = read (mSock, data, size);
163 O1_LOG("\nO1 UnixSocketClient : Error reading. %d bytes sent", nbytes);
168 /*******************************************************************
170 * @brief Close the Unix socket
174 * Function : closeSocket
177 * - Closes the Unix socket
180 * @return O1::SUCCESS - success
181 * O1::FAILURE - failure
182 ******************************************************************/
183 uint8_t UnixSocketClient::closeSocket()
185 if( close(mSock) != 0 )
190 /**********************************************************************
192 **********************************************************************/