1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020] [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 TCP server and send massages */
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
31 #include "TcpClient.h"
32 #include "GlobalDefs.h"
36 static struct sockaddr_in s_serverName;
37 static uint16_t s_port;
38 static const char* s_hostName = NULL;
41 /*******************************************************************
43 * @brief Initilize the sockadd_in structure
47 * Function : initSockaddr
50 * - Initilizes the sockadd_in structure
53 * @return ROK - success
55 ******************************************************************/
56 static uint8_t initSockaddr()
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);
67 O1_LOG("\nO1 TcpClient : Unknown host %s", s_hostName);
70 name->sin_addr = *(struct in_addr *) hostInfo->h_addr;
75 /*******************************************************************
77 * @brief Open a TCP socket
81 * Function : openSocket
84 * - Opens a TCP socket
86 * @params[in] hostname, port
87 * @return ROK - success
89 ******************************************************************/
90 uint8_t openSocket(const char* hostName, const uint16_t port)
92 /* Create the socket. */
94 s_hostName = hostName;
95 s_sock = socket (PF_INET, SOCK_STREAM, 0);
98 O1_LOG("\nO1 TcpClient : Error opening socket");
102 /* Init the sockaddr_in structure */
103 if (initSockaddr() == RFAILED)
107 /* Connect to the server */
108 if (0 > connect (s_sock,
109 (struct sockaddr_in *)&s_serverName ,
110 sizeof (s_serverName)))
112 O1_LOG("\nO1 TcpClient : Error connecting");
120 /*******************************************************************
122 * @brief Send message over TCP socket
126 * Function : sendData
129 * - Sends message over TCP socket
131 * @params[in] message, size of the message
132 * @return Number of bytes sent
134 ******************************************************************/
135 int sendData(void* data, const int size)
137 int nbytes = write (s_sock, data, size);
140 O1_LOG("\nO1 TcpClient : Error writing. %d bytes sent", nbytes);
145 /*******************************************************************
147 * @brief Recieve message over TCP socket
151 * Function : receiveData
154 * - Recieves message over TCP socket
156 * @params[in] message, size of the message
157 * @return Number of bytes received
159 ******************************************************************/
160 int receiveData(void* data, const int size)
162 int nbytes = read (s_sock, data, size);
165 O1_LOG("\nO1 TcpClient : Error reading. %d bytes sent", nbytes);
170 /*******************************************************************
172 * @brief Close the TCP socket
176 * Function : closeSocket
179 * - Closes the TCP socket
181 * @params[in] message, size of the message
182 * @return ROK - success
184 ******************************************************************/
185 uint8_t closeSocket()
187 if( close(s_sock) != 0 )
192 /**********************************************************************
194 **********************************************************************/