Tcp server and client for O1 alarm message communication[Issue-Id: ODUHIGH-244] 19/4919/4
authorVidhu <vidhu.pandey@hcl.com>
Sat, 24 Oct 2020 19:33:24 +0000 (01:03 +0530)
committerVidhu <vidhu.pandey@hcl.com>
Fri, 13 Nov 2020 11:41:44 +0000 (17:11 +0530)
Signed-off-by: Vidhu <vidhu.pandey@hcl.com>
Change-Id: I4fab9e410abb445fb00df4e966c0979a1b9cbc0a

src/o1/GlobalDefs.cpp [new file with mode: 0644]
src/o1/GlobalDefs.hpp [new file with mode: 0644]
src/o1/TcpServer.cpp [new file with mode: 0644]
src/o1/TcpServer.hpp [new file with mode: 0644]
src/o1/o1_client/AlarmInterface.c [new file with mode: 0644]
src/o1/o1_client/AlarmInterface.h [new file with mode: 0644]
src/o1/o1_client/GlobalDefs.h [new file with mode: 0644]
src/o1/o1_client/TcpClient.c [new file with mode: 0644]
src/o1/o1_client/TcpClient.h [new file with mode: 0644]

diff --git a/src/o1/GlobalDefs.cpp b/src/o1/GlobalDefs.cpp
new file mode 100644 (file)
index 0000000..77ea380
--- /dev/null
@@ -0,0 +1,29 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains global definitions for O1 interface modules */
+
+#include "GlobalDefs.hpp"
+
+const short O1::TCP_PORT = 8282;
+const int   O1::SUCCESS  = 0;
+const int   O1::FAILURE  = 1;
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/GlobalDefs.hpp b/src/o1/GlobalDefs.hpp
new file mode 100644 (file)
index 0000000..0f8d42a
--- /dev/null
@@ -0,0 +1,50 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains global definitions for O1 interface modules */
+
+#ifndef __GLOBAL_DEFS_HPP__
+#define __GLOBAL_DEFS_HPP__
+
+#include <syslog.h>
+
+#define O1_LOG(...) ( {\
+               printf(__VA_ARGS__);\
+               syslog(LOG_DEBUG,__VA_ARGS__);\
+               } )
+
+#define ALARM_MODULE_NAME_3GPP "_3gpp-common-fm"
+#define ALARM_MODULE_PATH_3GPP "/_3gpp-common-fm:AlarmListGrp"
+#define ALARM_MODULE_NAME_ORAN "o-ran-sc-odu-alarm-v1"
+#define ALARM_MODULE_PATH_ORAN "/o-ran-sc-odu-alarm-v1:odu"
+
+#define MAX_ALARM_ID_LEN 10
+
+class O1 
+{
+   public:
+   static const short TCP_PORT;
+   static const int   SUCCESS;
+   static const int   FAILURE;
+};
+
+#endif
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/TcpServer.cpp b/src/o1/TcpServer.cpp
new file mode 100644 (file)
index 0000000..47ce094
--- /dev/null
@@ -0,0 +1,275 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains TcpServer class that listens for Netconf Alarm messages
+   on a TCP socket from ODU. It calls the AlarmManager functions for raising 
+   or clearing the alarms based on the actions received 
+*/
+
+#include "TcpServer.hpp"
+#include "Alarm.hpp"
+#include "AlarmManager.hpp"
+#include "GlobalDefs.hpp"
+#include <iostream>
+#include <cstdio>
+#include <cerrno>
+#include <cstdlib>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+using std::map;
+using std::pair;
+
+/* Destructor */
+TcpServer::~TcpServer()
+{
+
+}
+
+
+/********************************************************************** 
+   Description : Read the data from the connected client application 
+   Params[In]  : fd - File descriptor
+   Return      : int - No of bytes read 
+**********************************************************************/
+int TcpServer::readMessage(int fd)
+{
+   AlarmRecord alrmRec;
+   bzero(&alrmRec,sizeof(alrmRec));
+   int nbytes = read (fd, &alrmRec, sizeof(alrmRec));
+   if (nbytes > 0)
+   {
+      Alarm alrm;
+      uint16_t alrmId;
+      O1_LOG("\nO1 TcpServer :\nAction %d\nalarm ID %s\n%d\n%s\n%d\n%s\n%s\nbytes %d",
+                     alrmRec.msgHeader.action,
+                     alrmRec.alarmId,
+                     alrmRec.perceivedSeverity,
+                     alrmRec.additionalText,
+                     alrmRec.eventType,
+                     alrmRec.specificProblem,
+                     alrmRec.additionalInfo,
+                     nbytes
+                     );
+      
+      /*Fill the alarm structure */
+      sscanf(alrmRec.alarmId,"%hu",&alrmId);
+      alrm.setAlarmId(alrmId);
+      alrm.setPerceivedSeverity(alrmRec.perceivedSeverity);
+      alrm.setAdditionalText(alrmRec.additionalText);
+      alrm.setEventType(alrmRec.eventType);
+      alrm.setSpecificProblem(alrmRec.specificProblem);
+      alrm.setAdditionalInfo(alrmRec.additionalInfo);
+
+      switch(alrmRec.msgHeader.action)
+      {
+         case RAISE: 
+                     if(AlarmManager::instance().raiseAlarm(alrm))
+                     {
+                        O1_LOG("\nO1 TcpServer : Alarm raised for alarm Id %s", alrmRec.alarmId);
+                     }
+                     else
+                     {
+                        O1_LOG("\nO1 TcpServer : Error in raising alarm for alrm Id %s", alrmRec.alarmId);
+                     }
+                     break;  
+         case CLEAR: 
+                     if(AlarmManager::instance().clearAlarm(alrm))
+                     {
+                        O1_LOG("\nO1 TcpServer : Alarm cleared for alarm Id %s", alrmRec.alarmId);
+                     }
+                     else
+                     {
+                        O1_LOG("\nO1 TcpServer : Error in clearing alarm for alarm Id %s", alrmRec.alarmId);
+                     }
+                     break;
+         default:    
+                     O1_LOG("\nO1 TcpServer : No action performed"); 
+                     break;
+      }
+
+   }
+   return nbytes;
+}
+
+
+/********************************************************************** 
+   Description : Open a TCP socket and bind on the port
+   Params[In]  : None
+   Return      : O1::SUCCESS - socket open and bind successful
+                 O1::FAILURE - socket open and bind failed
+**********************************************************************/
+int TcpServer::makeSocket() 
+{
+   struct sockaddr_in name;
+   /* Create the socket. */
+   mSock = socket (PF_INET, SOCK_STREAM, 0);
+   if (mSock < 0)
+   {
+      O1_LOG("\nO1 TcpServer : Socket error");
+      return O1::FAILURE;
+   }
+   /* Give the socket a name. */
+   bzero(&name, sizeof(name));
+   name.sin_family = AF_INET;
+   name.sin_port = htons (mPort);
+   name.sin_addr.s_addr = htonl (INADDR_ANY);
+   if (bind (mSock, (struct sockaddr *) &name, sizeof (name)) < 0)
+   {
+      close(mSock);
+      O1_LOG("\nO1 TcpServer : Bind error");
+      return O1::FAILURE;
+   }
+   return O1::SUCCESS;
+}
+
+
+/********************************************************************** 
+   Description : Start TCP server in thread    
+   Params[In]  : None
+   Return      : true  - task launched in pthread successfully
+                 false - task failed to launch
+**********************************************************************/
+bool TcpServer::start()
+{
+   return (pthread_create(&mThreadId, NULL, task, this) == 0);
+}
+
+/********************************************************************** 
+   Description : A TCP server to handle multiple connection using
+                 select multiplexing    
+   Params[In]  : None
+   Return      : true  - task launched in pthread successfully
+                 false - task failed to launch
+**********************************************************************/
+bool TcpServer::run()
+{
+
+   fd_set active_fd_set, read_fd_set;
+   int i;
+   struct sockaddr_in clientName;
+   socklen_t size;
+   bool ret = true;;
+
+   /* Create the socket and set it up to accept connections. */
+   if( makeSocket() == O1::SUCCESS )
+   {
+      if (listen (mSock, 1) < 0)
+      {
+         O1_LOG("\nO1 TcpServer : Listen error");
+         close(mSock);
+         ret = false;
+      }
+      else
+      {
+         /* Initialize the set of active sockets. */
+         FD_ZERO (&active_fd_set);
+         FD_SET (mSock, &active_fd_set);
+
+         while (1)
+         {
+            /* Block until input arrives on one or more active sockets. */
+            read_fd_set = active_fd_set;
+            if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
+            {
+               O1_LOG("\nO1 TcpServer : Select error");
+               close(mSock);
+               ret = false;
+               break;
+            }
+
+            /* Service all the sockets with input pending. */
+            for (i = 0; i < FD_SETSIZE; ++i)
+            {
+                if (FD_ISSET (i, &read_fd_set))
+                {
+                   if (i == mSock)
+                   {
+                      /* Connection request on original socket. */
+                      int newFd;
+                      bzero(&clientName, sizeof(clientName));
+                      size = sizeof (clientName);
+                      newFd = accept(mSock,(struct sockaddr *) &clientName,&size);
+                      if (newFd < 0)
+                      {
+                         O1_LOG("\nO1 TcpServer : Accept error");
+                         close(mSock);
+                         ret = false;
+                         break;
+                      }
+                      O1_LOG("\nO1 TcpServer : Connected from host %s, port %hd.\n",
+                         inet_ntoa (clientName.sin_addr),
+                         ntohs (clientName.sin_port));
+                      FD_SET (newFd, &active_fd_set);
+                   }      
+                   else
+                   {
+                      /* Data arriving on an already-connected socket. */
+                      if (readMessage(i) < 0)
+                      {
+                         close (i);
+                         FD_CLR (i, &active_fd_set);
+                      }
+                   }
+                }    
+             }/* for loop ends */     
+          } /* while(1) ends */
+      } /* else ends */
+   } /* outer if ends */
+   else
+   {
+      ret = false;
+   }
+   return ret;
+}
+
+
+/********************************************************************** 
+   Description : Static function for launching a TCP server instance 
+                 in a thread    
+   Params[In]  : TcpServer instance
+   Return      : NULL
+**********************************************************************/
+void* TcpServer::task(void *args)
+{
+   TcpServer *tcpServer = (TcpServer*)args;
+   tcpServer->run();
+   return NULL;
+}
+
+
+/********************************************************************** 
+   Description : Wait for the thread to complete in the parent process   
+   Params[In]  : None
+   Return      : true  - pthread join success
+                 false - pthread join failed
+**********************************************************************/
+bool TcpServer::wait()
+{
+    return (pthread_join(mThreadId,NULL) == 0);
+}
+
+
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/TcpServer.hpp b/src/o1/TcpServer.hpp
new file mode 100644 (file)
index 0000000..19f0eba
--- /dev/null
@@ -0,0 +1,56 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains TcpServer class that listens for Netconf Alarm messages
+   on a TCP socket from ODU. It calls the AlarmManager functions for raising 
+   or clearing the alarms based on the actions received  
+*/
+#ifndef __TCP_SERVER_HPP__
+#define __TCP_SERVER_HPP__
+#include <string>
+#include <pthread.h>
+
+using std::string;
+
+class TcpServer
+{
+
+   private:
+   pthread_t mThreadId;
+   int mSock;
+   const int16_t mPort;
+   int readMessage(int);
+   int makeSocket();
+   bool run();
+   static void* task(void*);
+
+   public:
+   bool start();
+   bool wait();
+   TcpServer(const uint16_t port) : mPort(port){};    
+   ~TcpServer();
+
+};
+
+#endif
+
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/o1_client/AlarmInterface.c b/src/o1/o1_client/AlarmInterface.c
new file mode 100644 (file)
index 0000000..1f5bc3b
--- /dev/null
@@ -0,0 +1,131 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains interfaces to raise and clear alarms */
+
+#include "AlarmInterface.h"
+#include "TcpClient.h"
+
+
+/********************************************************************** 
+   Description : Raise an alarm by sending alarm info to O1 module over
+                 TCP socket with action set to RAISE
+   Params[In]  : Alarm information
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+uint8_t raiseAlarm(AlarmRecord* alrm)
+{
+   if (openSocket(TCP_SERVER_IP,TCP_PORT) == RFAILED)
+   {
+      return RFAILED;
+   }
+   alrm->msgHeader.action = RAISE;
+   if (sendData(alrm,sizeof(AlarmRecord)) < 0 )
+   {
+      closeSocket();
+      return RFAILED;
+   }
+   closeSocket();
+   return ROK;
+}
+
+/********************************************************************** 
+   Description : Clears an alarm raised earlier by sending the alrm
+                 information to O1 module over TCP socket with action
+                 set to CLEAR 
+   Params[In]  : Alarm information
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+uint8_t clearAlarm(AlarmRecord* alrm)
+{
+   if (openSocket(TCP_SERVER_IP,TCP_PORT) == RFAILED)
+   {
+      return RFAILED;
+   }
+   alrm->msgHeader.action = CLEAR;
+   if (sendData(alrm,sizeof(AlarmRecord)) < 0)
+   {
+      closeSocket();
+      return RFAILED;
+   }
+   closeSocket();
+   return ROK;
+}
+
+
+/********************************************************************** 
+   Description : Fill the cell specific alarm parmeters and generate
+                 the alarm
+   Params[In]  : alarm Id, cell Id
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+uint8_t raiseCellAlrm(uint16_t alrmId, uint16_t cellId)
+{
+  char buff[BUFF_SIZE];
+  time_t now = time(NULL);
+
+  AlarmRecord alrm;
+  alrm.eventType = COMMUNICATIONS_ALARM;
+  snprintf (alrm.alarmId, sizeof(alrm.alarmId), "%d",alrmId);
+  alrm.perceivedSeverity = INDETERMINATE;
+  
+  strftime(buff, BUFF_SIZE, "%Y-%m-%d %H:%M:%S", localtime(&now));
+  if(CELL_UP_ALARM_ID == alrmId)
+  {
+    /* Fill cell up parameters */
+    snprintf (alrm.additionalText, sizeof(alrm.additionalText), \
+             "cell id  [%d] is up",cellId);
+    strcpy(alrm.additionalInfo , "cell UP");
+    strcpy(alrm.specificProblem, "Active");
+    strcpy(alrm.alarmRaiseTime, buff);
+  }
+  else
+  {
+    /* Clear the cell up alarm */
+    clearCellAlrm(CELL_UP_ALARM_ID);
+    /* Fill the cell down parameters */
+    snprintf (alrm.additionalText, sizeof(alrm.additionalText), \
+              "cell [%d] is down",cellId);
+    strcpy(alrm.additionalInfo , "cell down");
+    strcpy(alrm.specificProblem, "Active");
+    strcpy(alrm.alarmRaiseTime, buff);
+  }
+  /*Raise the alarm */
+  return raiseAlarm(&alrm);
+}
+
+/********************************************************************** 
+   Description : Clear the cell alarm
+   Params[In]  : alarm Id
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+uint8_t clearCellAlrm(uint16_t alrmId)
+{
+  AlarmRecord alrm;
+  snprintf (alrm.alarmId, sizeof(alrm.alarmId), "%d",alrmId);
+  return clearAlarm(&alrm);
+}
+  
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/o1_client/AlarmInterface.h b/src/o1/o1_client/AlarmInterface.h
new file mode 100644 (file)
index 0000000..4dabd43
--- /dev/null
@@ -0,0 +1,41 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020]      [HCL Technologies Ltd.]                          #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains interfaces to raise and clear alarms */
+
+#ifndef __ALARM_INTERFACE_H__
+#define __ALARM_INTERFACE_H__
+
+#include <stdint.h>
+#include "Alarm.h"
+#include "ssi.h"
+#include "GlobalDefs.h"
+
+#define BUFF_SIZE 20
+
+uint8_t raiseAlarm(AlarmRecord* alrm);
+uint8_t clearAlarm(AlarmRecord* alrm);
+uint8_t raiseCellAlrm(uint16_t alrmId, uint16_t cellId);
+uint8_t clearCellAlrm(uint16_t alrmId);
+
+#endif
+
+/**********************************************************************
+         End of file
+**********************************************************************/
+
diff --git a/src/o1/o1_client/GlobalDefs.h b/src/o1/o1_client/GlobalDefs.h
new file mode 100644 (file)
index 0000000..329bac4
--- /dev/null
@@ -0,0 +1,41 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* Class for global defines and constants for O1 interface */
+
+#ifndef __GLOBAL_DEFS_H__
+#define __GLOBAL_DEFS_H__
+
+#include <syslog.h>
+
+#define O1_LOG(...) ({\
+               printf(__VA_ARGS__);\
+               syslog(LOG_DEBUG,__VA_ARGS__);\
+               })
+
+#define TCP_PORT           8282
+#define TCP_SERVER_IP      "127.0.0.1"
+#define CELL_UP_ALARM_ID   1009
+#define CELL_DOWN_ALARM_ID 1010
+#define O1_ENABLE          1
+
+#endif
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/o1_client/TcpClient.c b/src/o1/o1_client/TcpClient.c
new file mode 100644 (file)
index 0000000..a7187e3
--- /dev/null
@@ -0,0 +1,132 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains functions to connect to TCP server and send massages */
+
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#include "TcpClient.h"
+#include "GlobalDefs.h"
+#include "ssi.h"
+
+static int s_sock;
+static struct sockaddr_in s_serverName;
+static uint16_t s_port;
+static const char* s_hostName = NULL;
+
+
+/********************************************************************** 
+   Description : Initilize the sockadd_in structure 
+   Params[In]  : None
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+static uint8_t initSockaddr()
+{
+
+   struct hostent *hostInfo;
+   struct sockaddr_in *name = &s_serverName;
+   bzero(&s_serverName, sizeof(s_serverName));
+   name->sin_family = AF_INET;
+   name->sin_port = htons (s_port);
+   hostInfo = gethostbyname (s_hostName);
+   if (hostInfo == NULL)
+   {
+      O1_LOG("\nO1 TcpClient : Unknown host %s", s_hostName);
+      return RFAILED;
+   }
+   name->sin_addr = *(struct in_addr *) hostInfo->h_addr;
+   return ROK;
+}
+
+
+/********************************************************************** 
+   Description : Open a TCP socket 
+   Params[In]  : hostName, port
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+uint8_t openSocket(const char* hostName, const uint16_t port)
+{
+   /* Create the socket. */
+   s_port = port;
+   s_hostName = hostName;
+   s_sock = socket (PF_INET, SOCK_STREAM, 0);
+   if (s_sock < 0)
+   {
+      O1_LOG("\nO1 TcpClient : Error opening socket");
+      return RFAILED;
+   }
+
+   /* Init the sockaddr_in structure */
+   if (initSockaddr() == RFAILED)
+   {
+      return RFAILED;
+   }
+   /* Connect to the server */
+   if (0 > connect (s_sock,
+                   (struct sockaddr_in *)&s_serverName ,
+                   sizeof (s_serverName)))
+   {
+      O1_LOG("\nO1 TcpClient : Error connecting");
+      return RFAILED;
+   }
+
+   return ROK;
+
+}
+
+/********************************************************************** 
+   Description : Send the message on TCP socket 
+   Params[In]  : message, size of the message
+   Return      : Number of bytes sent
+**********************************************************************/
+int sendData(void* data, const int size)
+{
+   int nbytes = write (s_sock, data, size);
+   if (nbytes < 0)
+   {
+      O1_LOG("\nO1 TcpClient : Error writing. %d bytes sent", nbytes);
+   }
+   return nbytes;
+}
+
+/********************************************************************** 
+   Description : Close the TCP socket 
+   Params[In]  : None
+   Return      : ROK     - success
+                 RFAILED - failure
+**********************************************************************/
+uint8_t closeSocket()
+{
+   if( close(s_sock) != 0 )
+      return RFAILED;
+   return ROK;
+}
+
+/**********************************************************************
+         End of file
+**********************************************************************/
diff --git a/src/o1/o1_client/TcpClient.h b/src/o1/o1_client/TcpClient.h
new file mode 100644 (file)
index 0000000..13d6b3f
--- /dev/null
@@ -0,0 +1,34 @@
+/*******************************************************************************
+################################################################################
+#   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
+#                                                                              #
+#   Licensed under the Apache License, Version 2.0 (the "License");            #
+#   you may not use this file except in compliance with the License.           #
+#   You may obtain a copy of the License at                                    #
+#                                                                              #
+#       http://www.apache.org/licenses/LICENSE-2.0                             #
+#                                                                              #
+#   Unless required by applicable law or agreed to in writing, software        #
+#   distributed under the License is distributed on an "AS IS" BASIS,          #
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
+#   See the License for the specific language governing permissions and        #
+#   limitations under the License.                                             #
+################################################################################
+*******************************************************************************/
+
+/* This file contains functions to connect to a TCP server and send massages */
+
+#ifndef __TCP_CLIENT_H__
+#define __TCP_CLIENT_H__
+#include <stdint.h>
+#include "ssi.h"
+
+uint8_t openSocket(const char*, const uint16_t);
+int sendData(void*, const int);
+uint8_t closeSocket();
+
+#endif
+
+/**********************************************************************
+         End of file
+**********************************************************************/