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

Preparing Identity object to have a state attribute

Also some code refactoring

Signed-off-by: Martin Melik Merkumians <melik-merkumians@acin.tuwien.ac.at>
Martin Melik Merkumians 6 лет назад
Родитель
Сommit
39de8ef146
2 измененных файлов с 34 добавлено и 27 удалено
  1. 2 0
      source/src/cip/cipidentity.c
  2. 32 27
      source/src/enet_encap/encap.c

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

@@ -19,6 +19,7 @@
  * - Attribute 5: Status
  * - Attribute 6: Serial Number
  * - Attribute 7: Product Name
+ * - Attribute 8: State
  *
  * Implemented Services
  * --------------------
@@ -47,6 +48,7 @@ CipWord status_ = 0; /**< Attribute 5: Status */
 CipUdint serial_number_ = 0; /**< Attribute 6: Serial Number, has to be set prior to OpENer initialization */
 CipShortString product_name_ = { sizeof(OPENER_DEVICE_NAME) - 1,
                                  OPENER_DEVICE_NAME }; /**< Attribute 7: Product Name */
+CipUsint g_state = 255;
 
 /** Private functions, sets the devices serial number
  * @param serial_number The serial number of the device

+ 32 - 27
source/src/enet_encap/encap.c

@@ -30,6 +30,7 @@ extern CipRevision revision_;
 extern CipWord status_;
 extern CipUdint serial_number_;
 extern CipShortString product_name_;
+extern CipUsint g_state;
 
 /* IP address data taken from TCPIPInterfaceObject*/
 extern CipTcpIpNetworkInterfaceConfiguration interface_configuration_;
@@ -431,32 +432,15 @@ void HandleReceivedListIdentityCommandUdp(const int socket,
   }
 }
 
-void EncapsulateListIdentityResponseMessage(
-  const EncapsulationData *const receive_data,
-  ENIPMessage *const outgoing_message) {
-
-
-  const CipUint kEncapsulationCommandListIdentityCommandSpecificLength =
-    sizeof(CipUint) + sizeof(CipInt) + sizeof(CipUint) + sizeof(CipUdint) +
-    8 *
-    sizeof(CipUsint) + sizeof(CipUint) + sizeof(CipUint) + sizeof(CipUint) + 2 *
-    sizeof(CipUsint) + sizeof(CipWord) + sizeof(CipUdint) +
-    sizeof(
-      CipUsint) + product_name_.length + sizeof(CipUsint);
-  const CipUint kEncapsulationCommandListIdentityLength =
-    kEncapsulationCommandListIdentityCommandSpecificLength + sizeof(CipUint) +
-    sizeof(CipUint)
-    + sizeof(CipUint);               /* Last element is item count */
-
-  GenerateEncapsulationHeader(receive_data,
-                              kEncapsulationCommandListIdentityLength,
-                              0,   /* Session handle will be ignored by receiver */
-                              kEncapsulationProtocolSuccess,
-                              outgoing_message);
-
-  outgoing_message->used_message_length += AddIntToMessage(1,
-                                                           &outgoing_message->current_message_position); /* Item count: one item */
+CipUint ListIdentityGetCipIdentityItemLength() {
+  return sizeof(CipUint) + sizeof(CipInt) + sizeof(CipUint) +
+         sizeof(CipUdint) + 8 * sizeof(CipUsint) + sizeof(CipUint) +
+         sizeof(CipUint) + sizeof(CipUint) + 2 * sizeof(CipUsint) +
+         sizeof(CipWord) + sizeof(CipUdint) + sizeof(CipUsint) +
+         product_name_.length + sizeof(CipUsint);
+}
 
+void EncodeListIdentityCipIdentityItem(ENIPMessage *const outgoing_message) {
   /* Item ID*/
   const CipUint kItemIDCipIdentity = 0x0C;
   outgoing_message->used_message_length += AddIntToMessage(
@@ -464,7 +448,7 @@ void EncapsulateListIdentityResponseMessage(
     &outgoing_message->current_message_position);
 
   outgoing_message->used_message_length += AddIntToMessage(
-    kEncapsulationCommandListIdentityCommandSpecificLength,
+    ListIdentityGetCipIdentityItemLength(),
     &outgoing_message->current_message_position);
 
   outgoing_message->used_message_length += AddIntToMessage(
@@ -503,11 +487,32 @@ void EncapsulateListIdentityResponseMessage(
   outgoing_message->current_message_position += product_name_.length;
   outgoing_message->used_message_length += product_name_.length;
 
-  *outgoing_message->current_message_position++ = 0xFF;
+  *outgoing_message->current_message_position++ = g_state;
   outgoing_message->used_message_length++;
 
 }
 
+void EncapsulateListIdentityResponseMessage(
+  const EncapsulationData *const receive_data,
+  ENIPMessage *const outgoing_message) {
+
+  const CipUint kEncapsulationCommandListIdentityLength =
+    ListIdentityGetCipIdentityItemLength() + sizeof(CipUint) +
+    sizeof(CipUint)
+    + sizeof(CipUint);               /* Last element is item count */
+
+  GenerateEncapsulationHeader(receive_data,
+                              kEncapsulationCommandListIdentityLength,
+                              0,   /* Session handle will be ignored by receiver */
+                              kEncapsulationProtocolSuccess,
+                              outgoing_message);
+
+  outgoing_message->used_message_length += AddIntToMessage(1,
+                                                           &outgoing_message->current_message_position); /* Item count: one item */
+  EncodeListIdentityCipIdentityItem(outgoing_message);
+
+}
+
 void DetermineDelayTime(const EipByte *const buffer_start,
                         DelayedEncapsulationMessage *const delayed_message_buffer)
 {