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

added callback for informing the application on connection state changes

Alois Zoitl 16 лет назад
Родитель
Сommit
7a1b1dd5ef
4 измененных файлов с 152 добавлено и 101 удалено
  1. 21 4
      src/cip/cipconnectionmanager.c
  2. 5 0
      src/cip/ciptypes.h
  3. 12 0
      src/opener_api.h
  4. 114 97
      src/ports/platform-pc/main.c

+ 21 - 4
src/cip/cipconnectionmanager.c

@@ -750,6 +750,12 @@ ForwardClose(S_CIP_Instance *pa_pstInstance, S_CIP_MR_Request * pa_MRRequest,
               && (pstRunner->OriginatorVendorID == OriginatorVendorID)
               && (pstRunner->OriginatorSerialNumber == OriginatorSerialNr))
             { /* found the corresponding connection object -> close it */
+              if (enConnTypeExplicit != pstRunner->m_eInstanceType)
+                {
+                  IApp_IOConnectionEvent(
+                      pstRunner->ConnectionPath.ConnectionPoint[0],
+                      pstRunner->ConnectionPath.ConnectionPoint[1], enTimedOut);
+                }
               closeConnection(pstRunner);
               nConnectionStatus = CIP_CON_MGR_SUCCESS;
               break;
@@ -843,6 +849,15 @@ manageConnections(void)
                 {
                   /* we have a timed out connection perform watchdog time out action*/
                   OPENER_TRACE_INFO(">>>>>>>>>>Connection timed out\n");
+                  if (enConnTypeExplicit != pstRunner->m_eInstanceType)
+                    {
+                      IApp_IOConnectionEvent(
+                          pstRunner->ConnectionPath.ConnectionPoint[0],
+                          pstRunner->ConnectionPath.ConnectionPoint[1],
+                          enTimedOut);
+
+                    }
+
                   switch (pstRunner->WatchdogTimeoutAction)
                     {
                   case enWatchdogTransitionToTimedOut:
@@ -1414,10 +1429,10 @@ establishIOConnction(S_CIP_ConnectionObject *pa_pstConnObjData,
       return CIP_ERROR_CONNECTION_FAILURE;
     }
 
-  O2TConnectionType = (pstIOConnObj->O_to_T_NetworkConnectionParameter
-      & 0x6000) >> 13;
-  T2OConnectionType = (pstIOConnObj->T_to_O_NetworkConnectionParameter
-      & 0x6000) >> 13;
+  O2TConnectionType
+      = (pstIOConnObj->O_to_T_NetworkConnectionParameter & 0x6000) >> 13;
+  T2OConnectionType
+      = (pstIOConnObj->T_to_O_NetworkConnectionParameter & 0x6000) >> 13;
 
   generalConnectionConfiguration(pstIOConnObj);
 
@@ -1576,6 +1591,8 @@ establishIOConnction(S_CIP_ConnectionObject *pa_pstConnObjData,
 
     }
   addNewActiveConnection(pstIOConnObj);
+  IApp_IOConnectionEvent(pstIOConnObj->ConnectionPath.ConnectionPoint[0],
+      pstIOConnObj->ConnectionPath.ConnectionPoint[1], enTimedOut);
   return EIP_OK;
 }
 

+ 5 - 0
src/cip/ciptypes.h

@@ -82,6 +82,11 @@ typedef enum
 #define CIP_UNCONNECTED_SEND		  0x52
 #define CIP_GET_CONNECTION_OWNER	0x5A
 
+typedef enum
+{
+  enOpened, enTimedOut, enClosed
+} EIOConnectionEvent;
+
 /* typedefinition to general CIP structure */
 typedef struct
 {

+ 12 - 0
src/opener_api.h

@@ -310,6 +310,18 @@ closeSession(int pa_nSocket);
 EIP_STATUS
 IApp_Init(void);
 
+
+/** \ingroup CIP_CALLBACK_API
+ * \brief Inform the application on changes occurred for a connection
+ *
+ * @param pa_unOutputAssembly the output assembly connection point of the connection
+ * @param pa_unInputAssembly the input assembly connection point of the connection
+ * @param pa_eIOConnectionEvent information on the change occurred
+ */
+void
+IApp_IOConnectionEvent(unsigned int pa_unOutputAssembly,
+    unsigned int pa_unInputAssembly, EIOConnectionEvent pa_eIOConnectionEvent);
+
 /** \ingroup CIP_CALLBACK_API 
  * \brief Call back function to inform application on received data for an assembly object.
  * 

+ 114 - 97
src/ports/platform-pc/main.c

@@ -23,101 +23,118 @@ EIP_UINT8 g_assemblydata3[10]; /* Config */
 
 extern int newfd;
 
-int main(int argc, char *arg[])
-  {
-    EIP_UINT8 acMyMACAddress[6];
-
-    if (argc != 12)
-      { printf("Wrong number of command line parameters!\n");
-        printf("The correct command line parameters are:\n");
-        printf("./opener ipaddress subnetmask gateway domainname hostaddress macaddress\n");
-        printf("    e.g. ./opener 192.168.0.2 255.255.255.0 192.168.0.1 test.com testdevice 00 15 C5 BF D0 87\n");      
-        exit(0);
-      }
-    else
-      {
-        /* fetch Internet address info from the platform */
-        configureNetworkInterface(arg[1], arg[2], arg[3]);
-        configureDomainName(arg[4]);
-        configureHostName(arg[5]);
-
-        acMyMACAddress[0] = (EIP_UINT8)strtoul(arg[6], NULL, 16);
-        acMyMACAddress[1] = (EIP_UINT8)strtoul(arg[7], NULL, 16);
-        acMyMACAddress[2] = (EIP_UINT8)strtoul(arg[8], NULL, 16);
-        acMyMACAddress[3] = (EIP_UINT8)strtoul(arg[9], NULL, 16);
-        acMyMACAddress[4] = (EIP_UINT8)strtoul(arg[10], NULL, 16);
-        acMyMACAddress[5] = (EIP_UINT8)strtoul(arg[11], NULL, 16);
-        configureMACAddress(acMyMACAddress);
-      }
-    
-    /*for a real device the serial number should be unique per device */
-    setDeviceSerialNumber(123456789);
-
-    /* Setup the CIP Layer */
-    CIP_Init();
-
-    Start_NetworkHandler(); /* here is the select loop implemented */
-    return -1;
-  }
-
-EIP_STATUS IApp_Init(void)
-  {
-    /* create 3 assembly object instances*/
-    /*INPUT*/
-    createAssemblyObject(1, &g_assemblydata[0], sizeof(g_assemblydata));
-
-    /*OUTPUT*/
-    createAssemblyObject(2, &g_assemblydata2[0], sizeof(g_assemblydata2));
-
-    /*CONFIG*/
-    createAssemblyObject(3, &g_assemblydata3[0], sizeof(g_assemblydata3));
-
-    /*Heart-beat output assembly for Input only connections */
-    createAssemblyObject(4, 0, 0);
-
-    /*Heart-beat output assembly for Listen only connections */
-    createAssemblyObject(5, 0, 0);
-
-
-    configureExclusiveOwnerConnectionPoint(0, 2, 1, 3);
-    configureInputOnlyConnectionPoint(0, 4, 1, 3);
-    configureListenOnlyConnectionPoint(0, 5, 1, 3);
-
-    return EIP_OK;
-  }
-
-EIP_STATUS IApp_AfterAssemblyDataReceived(S_CIP_Instance *pa_pstInstance)
-  {
-    /*handle the data received e.g., update outputs of the device */
-
-    return EIP_OK;
-  }
-
-bool IApp_BeforeAssemblyDataSend(S_CIP_Instance *pa_pstInstance)
-  {
-    /*update data to be sent e.g., read inputs of the device */
-    return true;
-  }
-
-EIP_STATUS IApp_ResetDevice(void)
-  {
-    /* add reset code here*/
-    return EIP_OK;
-  }
-
-EIP_STATUS IApp_ResetDeviceToInitialConfiguration(void)
-  {
-    /*rest the parameters and than perform device reset*/
-    return EIP_OK;
-  }
-
-void *IApp_CipCalloc(unsigned pa_nNumberOfElements, unsigned pa_nSizeOfElement)
-  {
-    return calloc(pa_nNumberOfElements, pa_nSizeOfElement);
-  }
-
-void IApp_RunIdleChanged(EIP_UINT32 pa_nRunIdleValue)
-  {
-    (void)pa_nRunIdleValue;
-  }
+int
+main(int argc, char *arg[])
+{
+  EIP_UINT8 acMyMACAddress[6];
+
+  if (argc != 12)
+    {
+      printf("Wrong number of command line parameters!\n");
+      printf("The correct command line parameters are:\n");
+      printf(
+          "./opener ipaddress subnetmask gateway domainname hostaddress macaddress\n");
+      printf(
+          "    e.g. ./opener 192.168.0.2 255.255.255.0 192.168.0.1 test.com testdevice 00 15 C5 BF D0 87\n");
+      exit(0);
+    }
+  else
+    {
+      /* fetch Internet address info from the platform */
+      configureNetworkInterface(arg[1], arg[2], arg[3]);
+      configureDomainName(arg[4]);
+      configureHostName(arg[5]);
+
+      acMyMACAddress[0] = (EIP_UINT8) strtoul(arg[6], NULL, 16);
+      acMyMACAddress[1] = (EIP_UINT8) strtoul(arg[7], NULL, 16);
+      acMyMACAddress[2] = (EIP_UINT8) strtoul(arg[8], NULL, 16);
+      acMyMACAddress[3] = (EIP_UINT8) strtoul(arg[9], NULL, 16);
+      acMyMACAddress[4] = (EIP_UINT8) strtoul(arg[10], NULL, 16);
+      acMyMACAddress[5] = (EIP_UINT8) strtoul(arg[11], NULL, 16);
+      configureMACAddress(acMyMACAddress);
+    }
+
+  /*for a real device the serial number should be unique per device */
+  setDeviceSerialNumber(123456789);
+
+  /* Setup the CIP Layer */
+  CIP_Init();
+
+  Start_NetworkHandler(); /* here is the select loop implemented */
+  return -1;
+}
+
+EIP_STATUS
+IApp_Init(void)
+{
+  /* create 3 assembly object instances*/
+  /*INPUT*/
+  createAssemblyObject(1, &g_assemblydata[0], sizeof(g_assemblydata));
+
+  /*OUTPUT*/
+  createAssemblyObject(2, &g_assemblydata2[0], sizeof(g_assemblydata2));
+
+  /*CONFIG*/
+  createAssemblyObject(3, &g_assemblydata3[0], sizeof(g_assemblydata3));
+
+  /*Heart-beat output assembly for Input only connections */
+  createAssemblyObject(4, 0, 0);
+
+  /*Heart-beat output assembly for Listen only connections */
+  createAssemblyObject(5, 0, 0);
+
+  configureExclusiveOwnerConnectionPoint(0, 2, 1, 3);
+  configureInputOnlyConnectionPoint(0, 4, 1, 3);
+  configureListenOnlyConnectionPoint(0, 5, 1, 3);
+
+  return EIP_OK;
+}
+
+void
+IApp_IOConnectionEvent(unsigned int pa_unOutputAssembly,
+    unsigned int pa_unInputAssembly, EIOConnectionEvent pa_eIOConnectionEvent)
+{
+  /* maintain a correct output state according to the connection state*/
+}
+
+EIP_STATUS
+IApp_AfterAssemblyDataReceived(S_CIP_Instance *pa_pstInstance)
+{
+  /*handle the data received e.g., update outputs of the device */
+
+  return EIP_OK;
+}
+
+bool
+IApp_BeforeAssemblyDataSend(S_CIP_Instance *pa_pstInstance)
+{
+  /*update data to be sent e.g., read inputs of the device */
+  return true;
+}
+
+EIP_STATUS
+IApp_ResetDevice(void)
+{
+  /* add reset code here*/
+  return EIP_OK;
+}
+
+EIP_STATUS
+IApp_ResetDeviceToInitialConfiguration(void)
+{
+  /*rest the parameters and than perform device reset*/
+  return EIP_OK;
+}
+
+void *
+IApp_CipCalloc(unsigned pa_nNumberOfElements, unsigned pa_nSizeOfElement)
+{
+  return calloc(pa_nNumberOfElements, pa_nSizeOfElement);
+}
+
+void
+IApp_RunIdleChanged(EIP_UINT32 pa_nRunIdleValue)
+{
+  (void) pa_nRunIdleValue;
+}