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 /**********************************************************************
42 Description : Initilize the sockadd_in structure
44 Return : ROK - success
46 **********************************************************************/
47 static uint8_t initSockaddr()
50 struct hostent *hostInfo;
51 struct sockaddr_in *name = &s_serverName;
52 bzero(&s_serverName, sizeof(s_serverName));
53 name->sin_family = AF_INET;
54 name->sin_port = htons (s_port);
55 hostInfo = gethostbyname (s_hostName);
58 O1_LOG("\nO1 TcpClient : Unknown host %s", s_hostName);
61 name->sin_addr = *(struct in_addr *) hostInfo->h_addr;
66 /**********************************************************************
67 Description : Open a TCP socket
68 Params[In] : hostName, port
69 Return : ROK - success
71 **********************************************************************/
72 uint8_t openSocket(const char* hostName, const uint16_t port)
74 /* Create the socket. */
76 s_hostName = hostName;
77 s_sock = socket (PF_INET, SOCK_STREAM, 0);
80 O1_LOG("\nO1 TcpClient : Error opening socket");
84 /* Init the sockaddr_in structure */
85 if (initSockaddr() == RFAILED)
89 /* Connect to the server */
90 if (0 > connect (s_sock,
91 (struct sockaddr_in *)&s_serverName ,
92 sizeof (s_serverName)))
94 O1_LOG("\nO1 TcpClient : Error connecting");
102 /**********************************************************************
103 Description : Send the message on TCP socket
104 Params[In] : message, size of the message
105 Return : Number of bytes sent
106 **********************************************************************/
107 int sendData(void* data, const int size)
109 int nbytes = write (s_sock, data, size);
112 O1_LOG("\nO1 TcpClient : Error writing. %d bytes sent", nbytes);
117 /**********************************************************************
118 Description : Close the TCP socket
120 Return : ROK - success
122 **********************************************************************/
123 uint8_t closeSocket()
125 if( close(s_sock) != 0 )
130 /**********************************************************************
132 **********************************************************************/