cipclass3connection.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*******************************************************************************
  2. * Copyright (c) 2011, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <string.h>
  7. #include "cipclass3connection.h"
  8. #include "encap.h"
  9. /**** Global variables ****/
  10. extern CipConnectionObject explicit_connection_object_pool[
  11. OPENER_CIP_NUM_EXPLICIT_CONNS];
  12. CipConnectionObject *GetFreeExplicitConnection(void);
  13. void Class3ConnectionTimeoutHandler(CipConnectionObject *connection_object) {
  14. CheckForTimedOutConnectionsAndCloseTCPConnections(connection_object,
  15. CloseSessionBySessionHandle);
  16. CloseConnection(connection_object);
  17. }
  18. /**** Implementation ****/
  19. CipError EstablishClass3Connection(
  20. CipConnectionObject *RESTRICT const connection_object,
  21. EipUint16 *const extended_error) {
  22. CipError cip_error = kCipErrorSuccess;
  23. CipConnectionObject *explicit_connection = GetFreeExplicitConnection();
  24. if (NULL == explicit_connection) {
  25. cip_error = kCipErrorConnectionFailure;
  26. *extended_error =
  27. kConnectionManagerExtendedStatusCodeErrorNoMoreConnectionsAvailable;
  28. } else {
  29. ConnectionObjectDeepCopy(explicit_connection, connection_object);
  30. ConnectionObjectGeneralConfiguration(explicit_connection);
  31. ConnectionObjectSetInstanceType(explicit_connection,
  32. kConnectionObjectInstanceTypeExplicitMessaging);
  33. /* set the connection call backs */
  34. explicit_connection->connection_close_function =
  35. CloseConnection;
  36. /* explicit connection have to be closed on time out*/
  37. explicit_connection->connection_timeout_function =
  38. Class3ConnectionTimeoutHandler;
  39. AddNewActiveConnection(explicit_connection);
  40. }
  41. return cip_error;
  42. }
  43. /** @brief Searches and returns a free explicit connection slot
  44. *
  45. * @return Free explicit connection slot, or NULL if no slot is free
  46. */
  47. CipConnectionObject *GetFreeExplicitConnection(void) {
  48. for (size_t i = 0; i < OPENER_CIP_NUM_EXPLICIT_CONNS; ++i) {
  49. if (ConnectionObjectGetState(&(explicit_connection_object_pool[i]) ) ==
  50. kConnectionObjectStateNonExistent) {
  51. return &(explicit_connection_object_pool[i]);
  52. }
  53. }
  54. return NULL;
  55. }
  56. void InitializeClass3ConnectionData(void) {
  57. memset( explicit_connection_object_pool, 0,
  58. OPENER_CIP_NUM_EXPLICIT_CONNS * sizeof(CipConnectionObject) );
  59. }
  60. EipStatus CipClass3ConnectionObjectStateEstablishedHandler(
  61. CipConnectionObject *RESTRICT const connection_object,
  62. ConnectionObjectState new_state) {
  63. switch(new_state) {
  64. case kConnectionObjectStateNonExistent:
  65. ConnectionObjectInitializeEmpty(connection_object);
  66. ConnectionObjectSetState(connection_object, new_state);
  67. return kEipStatusOk;
  68. default: return kEipStatusError;
  69. }
  70. }
  71. EipStatus CipClass3ConnectionObjectStateNonExistentHandler(
  72. CipConnectionObject *RESTRICT const connection_object,
  73. ConnectionObjectState new_state) {
  74. switch(new_state) {
  75. case kConnectionObjectStateEstablished:
  76. ConnectionObjectSetState(connection_object, new_state);
  77. return kEipStatusOk;
  78. default: return kEipStatusError;
  79. }
  80. }