cipidentity.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*******************************************************************************
  2. * Copyright (c) 2009, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. /**
  7. * @file cipidentity.c
  8. *
  9. * CIP Identity Object
  10. * ===================
  11. *
  12. * Implemented Attributes
  13. * ----------------------
  14. * - Attribute 1: VendorID
  15. * - Attribute 2: Device Type
  16. * - Attribute 3: Product Code
  17. * - Attribute 4: Revision
  18. * - Attribute 5: Status
  19. * - Attribute 6: Serial Number
  20. * - Attribute 7: Product Name
  21. *
  22. * Implemented Services
  23. * --------------------
  24. */
  25. #include <string.h>
  26. #include "cipidentity.h"
  27. #include "opener_user_conf.h"
  28. #include "cipcommon.h"
  29. #include "cipmessagerouter.h"
  30. #include "ciperror.h"
  31. #include "endianconv.h"
  32. #include "opener_api.h"
  33. /* attributes in CIP Identity Object */
  34. EipUint16 vendor_id_ = OPENER_DEVICE_VENDOR_ID; /**< Attribute 1: Vendor ID */
  35. EipUint16 device_type_ = OPENER_DEVICE_TYPE; /**< Attribute 2: Device Type */
  36. EipUint16 product_code_ = OPENER_DEVICE_PRODUCT_CODE; /**< Attribute 3: Product Code */
  37. CipRevision revision_ = { OPENER_DEVICE_MAJOR_REVISION,
  38. OPENER_DEVICE_MINOR_REVISION }; /**< Attribute 4: Revision / USINT Major, USINT Minor */
  39. EipUint16 status_ = 0; /**< Attribute 5: Status */
  40. EipUint32 serial_number_ = 0; /**< Attribute 6: Serial Number, has to be set prior to OpENer initialization */
  41. CipShortString product_name_ = { sizeof(OPENER_DEVICE_NAME) - 1,
  42. OPENER_DEVICE_NAME }; /**< Attribute 7: Product Name */
  43. /** Private functions, sets the devices serial number
  44. * @param serial_number The serial number of the device
  45. */
  46. void SetDeviceSerialNumber(const EipUint32 serial_number) {
  47. serial_number_ = serial_number;
  48. }
  49. /** @brief Private function, sets the devices status
  50. * @param status The serial number of the device
  51. */
  52. void SetDeviceStatus(const EipUint16 status) {
  53. status_ = status;
  54. }
  55. /** @brief Reset service
  56. *
  57. * @param instance
  58. * @param message_router_request
  59. * @param message_router_response
  60. * @returns Currently always kEipOkSend is returned
  61. */
  62. static EipStatus Reset(CipInstance *instance,
  63. /* pointer to instance*/
  64. CipMessageRouterRequest *message_router_request,
  65. /* pointer to message router request*/
  66. CipMessageRouterResponse *message_router_response,
  67. struct sockaddr *originator_address) { /* pointer to message router response*/
  68. (void) instance;
  69. EipStatus eip_status = kEipStatusOkSend;
  70. message_router_response->reply_service = (0x80
  71. | message_router_request->service);
  72. message_router_response->size_of_additional_status = 0;
  73. message_router_response->general_status = kCipErrorSuccess;
  74. if (message_router_request->request_path_size == 1) {
  75. switch (message_router_request->data[0]) {
  76. case 0: /* Reset type 0 -> emulate device reset / Power cycle */
  77. if ( kEipStatusError == ResetDevice() ) {
  78. message_router_response->general_status = kCipErrorInvalidParameter;
  79. }
  80. break;
  81. case 1: /* Reset type 1 -> reset to device settings */
  82. if ( kEipStatusError == ResetDeviceToInitialConfiguration() ) {
  83. message_router_response->general_status = kCipErrorInvalidParameter;
  84. }
  85. break;
  86. /* case 2: Not supported Reset type 2 -> Return to factory defaults except communications parameters */
  87. default:
  88. message_router_response->general_status = kCipErrorInvalidParameter;
  89. break;
  90. }
  91. } else /*TODO: Should be if (pa_stMRRequest->DataLength == 0)*/
  92. {
  93. /* The same behavior as if the data value given would be 0
  94. emulate device reset */
  95. if ( kEipStatusError == ResetDevice() ) {
  96. message_router_response->general_status = kCipErrorInvalidParameter;
  97. } else {
  98. /* eip_status = EIP_OK; */
  99. }
  100. }
  101. message_router_response->data_length = 0;
  102. return eip_status;
  103. }
  104. void InitializeCipIdentiy(CipClass *class) {
  105. CipClass *meta_class = class->class_instance.cip_class;
  106. InsertAttribute( (CipInstance *) class, 1, kCipUint,
  107. (void *) &class->revision,
  108. kGetableSingleAndAll ); /* revision */
  109. InsertAttribute( (CipInstance *) class, 2, kCipUint,
  110. (void *) &class->number_of_instances, kGetableSingleAndAll ); /* largest instance number */
  111. InsertAttribute( (CipInstance *) class, 3, kCipUint,
  112. (void *) &class->number_of_instances, kGetAttributeSingle ); /* number of instances currently existing*/
  113. InsertAttribute( (CipInstance *) class, 4, kCipUint, (void *) &kCipUintZero,
  114. kNotSetOrGetable ); /* optional attribute list - default = 0 */
  115. InsertAttribute( (CipInstance *) class, 5, kCipUint, (void *) &kCipUintZero,
  116. kNotSetOrGetable ); /* optional service list - default = 0 */
  117. InsertAttribute( (CipInstance *) class, 6, kCipUint,
  118. (void *) &meta_class->highest_attribute_number,
  119. kGetableSingleAndAll ); /* max class attribute number*/
  120. InsertAttribute( (CipInstance *) class, 7, kCipUint,
  121. (void *) &class->highest_attribute_number,
  122. kGetableSingleAndAll ); /* max instance attribute number*/
  123. }
  124. EipStatus CipIdentityInit() {
  125. CipClass *class = CreateCipClass(kIdentityClassCode, 0, /* # of non-default class attributes */
  126. 7, /* # highest class attribute number*/
  127. 2, /* # of class services*/
  128. 7, /* # of instance attributes*/
  129. 7, /* # highest instance attribute number*/
  130. 3, /* # of instance services*/
  131. 1, /* # of instances*/
  132. "identity", /* # class name (for debug)*/
  133. 1, /* # class revision*/
  134. &InitializeCipIdentiy); /* # function pointer for initialization*/
  135. if (class == 0) {
  136. return kEipStatusError;
  137. }
  138. CipInstance *instance = GetCipInstance(class, 1);
  139. InsertAttribute(instance, 1, kCipUint, &vendor_id_, kGetableSingleAndAll);
  140. InsertAttribute(instance, 2, kCipUint, &device_type_, kGetableSingleAndAll);
  141. InsertAttribute(instance, 3, kCipUint, &product_code_, kGetableSingleAndAll);
  142. InsertAttribute(instance, 4, kCipUsintUsint, &revision_,
  143. kGetableSingleAndAll);
  144. InsertAttribute(instance, 5, kCipWord, &status_, kGetableSingleAndAll);
  145. InsertAttribute(instance, 6, kCipUdint, &serial_number_,
  146. kGetableSingleAndAll);
  147. InsertAttribute(instance, 7, kCipShortString, &product_name_,
  148. kGetableSingleAndAll);
  149. InsertService(class, kGetAttributeSingle, &GetAttributeSingle,
  150. "GetAttributeSingle");
  151. InsertService(class, kGetAttributeAll, &GetAttributeAll, "GetAttributeAll");
  152. InsertService(class, kReset, &Reset, "Reset");
  153. return kEipStatusOk;
  154. }