Просмотр исходного кода

Fixes #87, see issue comments, adds trace infos

Martin Melik Merkumians 9 лет назад
Родитель
Сommit
19b6cd9759

+ 2 - 0
source/src/cip/cipconnectionmanager.c

@@ -1524,6 +1524,7 @@ void AddNewActiveConnection(ConnectionObject *connection_object) {
 }
 
 void RemoveFromActiveConnections(ConnectionObject *connection_object) {
+
   if (NULL != connection_object->first_connection_object) {
     connection_object->first_connection_object->next_connection_object =
       connection_object->next_connection_object;
@@ -1534,6 +1535,7 @@ void RemoveFromActiveConnections(ConnectionObject *connection_object) {
     connection_object->next_connection_object->first_connection_object =
       connection_object->first_connection_object;
   }
+
   connection_object->first_connection_object = NULL;
   connection_object->next_connection_object = NULL;
   connection_object->state = kConnectionStateNonExistent;

+ 3 - 0
source/src/cip/cipioconnection.c

@@ -909,10 +909,13 @@ void CloseCommunicationChannelsAndRemoveFromActiveConnectionsList(
   ConnectionObject *connection_object) {
   IApp_CloseSocket_udp(
     connection_object->socket[kUdpCommuncationDirectionConsuming]);
+
   connection_object->socket[kUdpCommuncationDirectionConsuming] =
     kEipInvalidSocket;
+
   IApp_CloseSocket_udp(
     connection_object->socket[kUdpCommuncationDirectionProducing]);
+
   connection_object->socket[kUdpCommuncationDirectionProducing] =
     kEipInvalidSocket;
 

+ 18 - 1
source/src/enet_encap/encap.c

@@ -18,6 +18,7 @@
 #include "cipidentity.h"
 #include "generic_networkhandler.h"
 #include "trace.h"
+#include "socket_timer.h"
 
 /*Identity data from cipidentity.c*/
 extern EipUint16 vendor_id_;
@@ -160,6 +161,7 @@ int HandleReceivedExplictTcpData(int socket,
                                  unsigned int length,
                                  int *remaining_bytes,
                                  struct sockaddr *originator_address) {
+	OPENER_TRACE_INFO("Handles data for TCP socket: %d\n", socket);
   EipStatus return_value = kEipStatusOk;
   EncapsulationData encapsulation_data;
   /* eat the encapsulation header*/
@@ -178,37 +180,45 @@ int HandleReceivedExplictTcpData(int socket,
       /* most of these functions need a reply to be send */
       switch (encapsulation_data.command_code) {
         case (kEncapsulationCommandNoOperation):
+        		OPENER_TRACE_INFO("NOP\n");
           /* NOP needs no reply and does nothing */
           return_value = kEipStatusOk;
           break;
 
         case (kEncapsulationCommandListServices):
+                		OPENER_TRACE_INFO("List services\n");
           HandleReceivedListServicesCommand(&encapsulation_data);
           break;
 
         case (kEncapsulationCommandListIdentity):
+                		OPENER_TRACE_INFO("List identity\n");
           HandleReceivedListIdentityCommandTcp(&encapsulation_data);
           break;
 
         case (kEncapsulationCommandListInterfaces):
+                		OPENER_TRACE_INFO("List interfaces\n");
           HandleReceivedListInterfacesCommand(&encapsulation_data);
           break;
 
         case (kEncapsulationCommandRegisterSession):
+                		OPENER_TRACE_INFO("Register session\n");
           HandleReceivedRegisterSessionCommand(socket, &encapsulation_data);
           break;
 
         case (kEncapsulationCommandUnregisterSession):
+                		OPENER_TRACE_INFO("unregister session\n");
           return_value = HandleReceivedUnregisterSessionCommand(
             &encapsulation_data);
           break;
 
         case (kEncapsulationCommandSendRequestReplyData):
+                		OPENER_TRACE_INFO("Send Request/Reply Data\n");
           return_value = HandleReceivedSendRequestResponseDataCommand(
             &encapsulation_data, originator_address);
           break;
 
         case (kEncapsulationCommandSendUnitData):
+                		OPENER_TRACE_INFO("Send Unit Data\n");
           return_value = HandleReceivedSendUnitDataCommand(&encapsulation_data,
                                                            originator_address);
           break;
@@ -252,10 +262,12 @@ int HandleReceivedExplictUdpData(int socket,
       /* most of these functions need a reply to be send */
       switch (encapsulation_data.command_code) {
         case (kEncapsulationCommandListServices):
+          OPENER_TRACE_INFO("List Service\n");
           HandleReceivedListServicesCommand(&encapsulation_data);
           break;
 
         case (kEncapsulationCommandListIdentity):
+		 OPENER_TRACE_INFO("List Identity\n");
           if(unicast == true) {
             HandleReceivedListIdentityCommandTcp(&encapsulation_data);
           }
@@ -267,6 +279,7 @@ int HandleReceivedExplictUdpData(int socket,
           break;
 
         case (kEncapsulationCommandListInterfaces):
+		  OPENER_TRACE_INFO("List Interfaces\n");
           HandleReceivedListInterfacesCommand(&encapsulation_data);
           break;
 
@@ -277,6 +290,7 @@ int HandleReceivedExplictUdpData(int socket,
         case (kEncapsulationCommandSendRequestReplyData):
         case (kEncapsulationCommandSendUnitData):
         default:
+          OPENER_TRACE_INFO("No command\n");
           encapsulation_data.status = kEncapsulationProtocolInvalidCommand;
           encapsulation_data.data_length = 0;
           break;
@@ -465,6 +479,9 @@ void HandleReceivedRegisterSessionCommand(int socket,
       {
         receive_data->status = kEncapsulationProtocolInsufficientMemory;
       } else { /* successful session registered */
+    	  SocketTimer *socket_timer = SocketTimerArrayGetEmptySocketTimer(g_timestamps, OPENER_NUMBER_OF_SUPPORTED_SESSIONS);
+    	  SocketTimerSetSocket(socket_timer, socket);
+    	  SocketTimerSetLastUpdate(socket_timer, g_actual_time);
         g_registered_sessions[session_index] = socket; /* store associated socket */
         receive_data->session_handle = session_index + 1;
         receive_data->status = kEncapsulationProtocolSuccess;
@@ -592,7 +609,7 @@ EipStatus HandleReceivedSendRequestResponseDataCommand(
  *                      kInvalidSession .. no free session available
  */
 int GetFreeSessionIndex(void) {
-  for (int session_index = 0;
+  for (size_t session_index = 0;
        session_index < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; session_index++) {
     if (kEipInvalidSocket == g_registered_sessions[session_index]) {
       return session_index;

+ 1 - 1
source/src/opener_api.h

@@ -674,7 +674,7 @@ SendUdpData(struct sockaddr_in *socket_data,
  *
  * @param socket_handle socket descriptor to close
  */
-void CloseSocket(int socket);
+void CloseSocket(const int socket);
 
 /** @mainpage OpENer - Open Source EtherNet/IP(TM) Communication Stack
  * Documentation

+ 4 - 2
source/src/ports/POSIX/networkhandler.c

@@ -11,11 +11,13 @@
 #include "networkhandler.h"
 
 #include "encap.h"
+#include "opener_user_conf.h"
 
 MicroSeconds GetMicroSeconds(void) {
-  struct timespec now;
+  struct timespec now = { .tv_nsec = 0, .tv_sec = 0 };
 
-  clock_gettime( CLOCK_MONOTONIC, &now );
+  int error = clock_gettime( CLOCK_MONOTONIC, &now );
+  OPENER_ASSERT(-1 != error);
   MicroSeconds micro_seconds =  (MicroSeconds)now.tv_nsec / 1000ULL +
                                now.tv_sec * 1000000ULL;
   return micro_seconds;

+ 38 - 37
source/src/ports/generic_networkhandler.c

@@ -19,11 +19,11 @@
 #include "typedefs.h"
 #include "trace.h"
 #include "opener_error.h"
-#include "socket_timer.h"
 #include "encap.h"
 #include "ciptcpipinterface.h"
+#include "opener_user_conf.h"
 
-SocketTimer g_timestamps[MAX_NO_OF_TCP_SOCKETS];
+#define MAX_NO_OF_TCP_SOCKETS 10
 
 /** @brief handle any connection request coming in the TCP server socket.
  *
@@ -64,7 +64,7 @@ EipStatus NetworkHandlerInitialize(void) {
     return kEipStatusError;
   }
 
-  SocketTimerArrayInitialize(g_timestamps, MAX_NO_OF_TCP_SOCKETS);
+  SocketTimerArrayInitialize(g_timestamps, OPENER_NUMBER_OF_SUPPORTED_SESSIONS);
 
   /* clear the master an temp sets */
   FD_ZERO(&master_socket);
@@ -238,11 +238,10 @@ void IApp_CloseSocket_udp(int socket_handle) {
 }
 
 void IApp_CloseSocket_tcp(int socket_handle) {
-  SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps,
-                                                             MAX_NO_OF_TCP_SOCKETS,
-                                                             socket_handle);
-  if(NULL != socket_timer) {
-    SocketTimerClear(socket_timer);
+  SocketTimer *socket_timer = NULL;
+  while(NULL != (socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps, OPENER_NUMBER_OF_SUPPORTED_SESSIONS, socket_handle)) )
+  {
+	SocketTimerClear(socket_timer);
   }
   CloseSocket(socket_handle);
 }
@@ -276,19 +275,26 @@ void CheckAndHandleTcpListenerSocket(void) {
       FreeErrorMessage(error_message);
       return;
     }
+    OPENER_TRACE_INFO(">>> network handler: accepting new TCP socket: %d \n", new_socket);
+
+    SocketTimer *socket_timer = SocketTimerArrayGetEmptySocketTimer(
+          g_timestamps,
+		  OPENER_NUMBER_OF_SUPPORTED_SESSIONS);
+
+    OPENER_TRACE_INFO("Current time stamp: %ld\n", g_actual_time);
+    for(size_t i = 0; i < OPENER_NUMBER_OF_SUPPORTED_SESSIONS;i++) {
+    	OPENER_TRACE_INFO("Socket: %d - Last Update: %ld\n", g_timestamps[i].socket, g_timestamps[i].last_update);
+    }
+
+    OPENER_ASSERT(socket_timer != NULL);
 
     FD_SET(new_socket, &master_socket);
     /* add newfd to master set */
     if (new_socket > highest_socket_handle) {
+      OPENER_TRACE_INFO("New highest socket: %d", new_socket);
       highest_socket_handle = new_socket;
     }
 
-    SocketTimer *socket_timer = SocketTimerArrayGetEmptySocketTimer(
-      g_timestamps,
-      MAX_NO_OF_TCP_SOCKETS);
-    SocketTimerSetSocket(socket_timer, new_socket);
-    SocketTimerSetLastUpdate(socket_timer, g_actual_time);
-
     OPENER_TRACE_STATE("networkhandler: opened new TCP connection on fd %d\n",
                        new_socket);
   }
@@ -329,7 +335,7 @@ EipStatus NetworkHandlerProcessOnce(void) {
     CheckAndHandleUdpGlobalBroadcastSocket();
     CheckAndHandleConsumingUdpSockets();
 
-    for (int socket = 0; socket <= highest_socket_handle; socket++) {
+    for (size_t socket = 0; socket <= highest_socket_handle; socket++) {
       if ( true == CheckSocketSet(socket) ) {
         /* if it is still checked it is a TCP receive */
         if ( kEipStatusError == HandleDataOnTcpSocket(socket) ) /* if error */
@@ -337,9 +343,8 @@ EipStatus NetworkHandlerProcessOnce(void) {
           CloseSocket(socket);
           CloseSession(socket); /* clean up session and close the socket */
         }
-        CheckEncapsulationInactivity(socket);
-
       }
+      CheckEncapsulationInactivity(socket);
     }
   }
 
@@ -517,7 +522,7 @@ EipStatus SendUdpData(struct sockaddr_in *address,
 }
 
 EipStatus HandleDataOnTcpSocket(int socket) {
-  //OPENER_TRACE_INFO("Entering HandleDataOnTcpSocket\n");
+  OPENER_TRACE_INFO("Entering HandleDataOnTcpSocket for socket: %d\n", socket);
   int remaining_bytes = 0;
   long data_sent = PC_OPENER_ETHERNET_BUFFER_SIZE;
 
@@ -532,9 +537,8 @@ EipStatus HandleDataOnTcpSocket(int socket) {
                                    0); /*TODO we may have to set the socket to a non blocking socket */
 
   SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps,
-                                                             MAX_NO_OF_TCP_SOCKETS,
+		  OPENER_NUMBER_OF_SUPPORTED_SESSIONS,
                                                              socket);
-  SocketTimerSetLastUpdate(socket_timer, g_actual_time);
   if (number_of_read_bytes == 0) {
     int error_code = GetSocketErrorNumber();
     char *error_message = GetErrorMessage(error_code);
@@ -572,10 +576,7 @@ EipStatus HandleDataOnTcpSocket(int socket) {
         data_sent);
       number_of_read_bytes = recv(socket, &g_ethernet_communication_buffer[0],
                                   data_sent, 0);
-      SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps,
-                                                                 MAX_NO_OF_TCP_SOCKETS,
-                                                                 socket);
-      SocketTimerSetLastUpdate(socket_timer, g_actual_time);
+
       if (number_of_read_bytes == 0) /* got error or connection closed by client */
       {
         int error_code = GetSocketErrorNumber();
@@ -604,14 +605,14 @@ EipStatus HandleDataOnTcpSocket(int socket) {
         data_sent = data_size;
       }
     } while (0 < data_size);
-    assert(0 <= data_size);
+    SocketTimerSetLastUpdate(socket_timer, g_actual_time);
+    OPENER_ASSERT(0 <= data_size);
     return kEipStatusOk;
   }
 
   number_of_read_bytes = recv(socket, &g_ethernet_communication_buffer[4],
                               data_size, 0);
 
-  SocketTimerSetLastUpdate(socket_timer, g_actual_time);
   if (number_of_read_bytes == 0) /* got error or connection closed by client */
   {
     int error_code = GetSocketErrorNumber();
@@ -652,7 +653,7 @@ EipStatus HandleDataOnTcpSocket(int socket) {
       socket, g_ethernet_communication_buffer, data_size, &remaining_bytes,
       &sender_address);
     SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps,
-                                                               MAX_NO_OF_TCP_SOCKETS,
+    		OPENER_NUMBER_OF_SUPPORTED_SESSIONS,
                                                                socket);
     if(NULL != socket_timer) {
       SocketTimerSetLastUpdate(socket_timer, g_actual_time);
@@ -672,7 +673,7 @@ EipStatus HandleDataOnTcpSocket(int socket) {
       data_sent = send(socket, (char *) &g_ethernet_communication_buffer[0],
                        number_of_read_bytes, 0);
       SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps,
-                                                                 MAX_NO_OF_TCP_SOCKETS,
+    		  OPENER_NUMBER_OF_SUPPORTED_SESSIONS,
                                                                  socket);
       SocketTimerSetLastUpdate(socket_timer, g_actual_time);
       if (data_sent != number_of_read_bytes) {
@@ -701,11 +702,9 @@ EipStatus HandleDataOnTcpSocket(int socket) {
 int CreateUdpSocket(UdpCommuncationDirection communication_direction,
                     struct sockaddr_in *socket_data) {
   struct sockaddr_in peer_address;
-  int new_socket;
-
-  socklen_t peer_address_length;
+  int new_socket = kEipInvalidSocket;
 
-  peer_address_length = sizeof(struct sockaddr_in);
+  socklen_t peer_address_length = sizeof(struct sockaddr_in);
   /* create a new UDP socket */
   if ( ( new_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) == -1 ) {
     int error_code = GetSocketErrorNumber();
@@ -786,6 +785,7 @@ int CreateUdpSocket(UdpCommuncationDirection communication_direction,
   /* add new socket to the master list                                             */
   FD_SET(new_socket, &master_socket);
   if (new_socket > highest_socket_handle) {
+	OPENER_TRACE_INFO("New highest socket: %d", new_socket);
     highest_socket_handle = new_socket;
   }
   return new_socket;
@@ -846,12 +846,12 @@ void CheckAndHandleConsumingUdpSockets(void) {
 }
 
 
-void CloseSocket(int socket_handle) {
-
+void CloseSocket(const int socket_handle) {
   OPENER_TRACE_INFO("networkhandler: closing socket %d\n", socket_handle);
+
   if (kEipInvalidSocket != socket_handle) {
-    FD_CLR(socket_handle, &master_socket);
-    CloseSocketPlatform(socket_handle);
+      FD_CLR(socket_handle, &master_socket);
+      CloseSocketPlatform(socket_handle);
   }
   OPENER_TRACE_INFO("networkhandler: closing socket done %d\n", socket_handle);
 }
@@ -876,9 +876,10 @@ void CheckEncapsulationInactivity(int socket_handle) {
 
   if (0 < g_encapsulation_inactivity_timeout) {
     SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer(g_timestamps,
-                                                               MAX_NO_OF_TCP_SOCKETS,
+    		OPENER_NUMBER_OF_SUPPORTED_SESSIONS,
                                                                socket_handle);
 
+    OPENER_TRACE_INFO("Check socket %d - socket timer: %p\n", socket_handle, socket_timer);
     if(NULL != socket_timer) {
       MilliSeconds diffms = g_actual_time - SocketTimerGetLastUpdate(
         socket_timer);

+ 2 - 1
source/src/ports/generic_networkhandler.h

@@ -25,8 +25,9 @@
 #include "cipconnectionmanager.h"
 #include "networkhandler.h"
 #include "appcontype.h"
+#include "socket_timer.h"
 
-#define MAX_NO_OF_TCP_SOCKETS 10
+SocketTimer g_timestamps[OPENER_NUMBER_OF_SUPPORTED_SESSIONS];
 
 /* values needed from the connection manager */
 extern ConnectionObject *g_active_connection_list;

+ 4 - 0
source/src/ports/socket_timer.c

@@ -6,14 +6,18 @@
 
 #include "socket_timer.h"
 
+#include "trace.h"
+
 void SocketTimerSetSocket(SocketTimer *const socket_timer, const int socket) {
   socket_timer->socket = socket;
+  OPENER_TRACE_INFO("Adds socket %d to socket timers\n", socket);
 }
 
 void SocketTimerSetLastUpdate(SocketTimer *const socket_timer,
                               const MilliSeconds actual_time) {
   if (NULL != socket_timer) {
     socket_timer->last_update = actual_time;
+    OPENER_TRACE_INFO("Sets time stamp for socket %d\n", socket_timer->socket);
   }
 }