sampleapplication.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*******************************************************************************
  2. * Copyright (c) 2012, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include "opener_api.h"
  10. #include "appcontype.h"
  11. #include "trace.h"
  12. #include "cipidentity.h"
  13. #include "ciptcpipinterface.h"
  14. #include "cipqos.h"
  15. #define DEMO_APP_INPUT_ASSEMBLY_NUM 100 //0x064
  16. #define DEMO_APP_OUTPUT_ASSEMBLY_NUM 150 //0x096
  17. #define DEMO_APP_CONFIG_ASSEMBLY_NUM 151 //0x097
  18. #define DEMO_APP_HEARTBEAT_INPUT_ONLY_ASSEMBLY_NUM 152 //0x098
  19. #define DEMO_APP_HEARTBEAT_LISTEN_ONLY_ASSEMBLY_NUM 153 //0x099
  20. #define DEMO_APP_EXPLICT_ASSEMBLY_NUM 154 //0x09A
  21. /* global variables for demo application (4 assembly data fields) ************/
  22. EipUint8 g_assembly_data064[32]; /* Input */
  23. EipUint8 g_assembly_data096[32]; /* Output */
  24. EipUint8 g_assembly_data097[10]; /* Config */
  25. EipUint8 g_assembly_data09A[32]; /* Explicit */
  26. EipStatus ApplicationInitialization(void) {
  27. /* create 3 assembly object instances*/
  28. /*INPUT*/
  29. CreateAssemblyObject( DEMO_APP_INPUT_ASSEMBLY_NUM, g_assembly_data064,
  30. sizeof(g_assembly_data064) );
  31. /*OUTPUT*/
  32. CreateAssemblyObject( DEMO_APP_OUTPUT_ASSEMBLY_NUM, g_assembly_data096,
  33. sizeof(g_assembly_data096) );
  34. /*CONFIG*/
  35. CreateAssemblyObject( DEMO_APP_CONFIG_ASSEMBLY_NUM, g_assembly_data097,
  36. sizeof(g_assembly_data097) );
  37. /*Heart-beat output assembly for Input only connections */
  38. CreateAssemblyObject(DEMO_APP_HEARTBEAT_INPUT_ONLY_ASSEMBLY_NUM, NULL, 0);
  39. /*Heart-beat output assembly for Listen only connections */
  40. CreateAssemblyObject(DEMO_APP_HEARTBEAT_LISTEN_ONLY_ASSEMBLY_NUM, NULL, 0);
  41. /* assembly for explicit messaging */
  42. CreateAssemblyObject( DEMO_APP_EXPLICT_ASSEMBLY_NUM, g_assembly_data09A,
  43. sizeof(g_assembly_data09A) );
  44. ConfigureExclusiveOwnerConnectionPoint(0, DEMO_APP_OUTPUT_ASSEMBLY_NUM,
  45. DEMO_APP_INPUT_ASSEMBLY_NUM,
  46. DEMO_APP_CONFIG_ASSEMBLY_NUM);
  47. ConfigureInputOnlyConnectionPoint(0,
  48. DEMO_APP_HEARTBEAT_INPUT_ONLY_ASSEMBLY_NUM,
  49. DEMO_APP_INPUT_ASSEMBLY_NUM,
  50. DEMO_APP_CONFIG_ASSEMBLY_NUM);
  51. ConfigureListenOnlyConnectionPoint(0,
  52. DEMO_APP_HEARTBEAT_LISTEN_ONLY_ASSEMBLY_NUM,
  53. DEMO_APP_INPUT_ASSEMBLY_NUM,
  54. DEMO_APP_CONFIG_ASSEMBLY_NUM);
  55. return kEipStatusOk;
  56. }
  57. void HandleApplication(void) {
  58. /* check if application needs to trigger an connection */
  59. }
  60. void CheckIoConnectionEvent(unsigned int output_assembly_id,
  61. unsigned int input_assembly_id,
  62. IoConnectionEvent io_connection_event) {
  63. /* maintain a correct output state according to the connection state*/
  64. (void) output_assembly_id; /* suppress compiler warning */
  65. (void) input_assembly_id; /* suppress compiler warning */
  66. (void) io_connection_event; /* suppress compiler warning */
  67. }
  68. EipStatus AfterAssemblyDataReceived(CipInstance *instance) {
  69. EipStatus status = kEipStatusOk;
  70. /*handle the data received e.g., update outputs of the device */
  71. switch (instance->instance_number) {
  72. case DEMO_APP_OUTPUT_ASSEMBLY_NUM:
  73. /* Data for the output assembly has been received.
  74. * Mirror it to the inputs */
  75. memcpy( &g_assembly_data064[0], &g_assembly_data096[0],
  76. sizeof(g_assembly_data064) );
  77. break;
  78. case DEMO_APP_EXPLICT_ASSEMBLY_NUM:
  79. /* do something interesting with the new data from
  80. * the explicit set-data-attribute message */
  81. break;
  82. case DEMO_APP_CONFIG_ASSEMBLY_NUM:
  83. /* Add here code to handle configuration data and check if it is ok
  84. * The demo application does not handle config data.
  85. * However in order to pass the test we accept any data given.
  86. * EIP_ERROR
  87. */
  88. status = kEipStatusOk;
  89. break;
  90. default:
  91. OPENER_TRACE_INFO(
  92. "Unknown assembly instance ind AfterAssemblyDataReceived");
  93. break;
  94. }
  95. return status;
  96. }
  97. EipBool8 BeforeAssemblyDataSend(CipInstance *pa_pstInstance) {
  98. /*update data to be sent e.g., read inputs of the device */
  99. /*In this sample app we mirror the data from out to inputs on data receive
  100. * therefore we need nothing to do here. Just return true to inform that
  101. * the data is new.
  102. */
  103. if (pa_pstInstance->instance_number == DEMO_APP_EXPLICT_ASSEMBLY_NUM) {
  104. /* do something interesting with the existing data
  105. * for the explicit get-data-attribute message */
  106. }
  107. return true;
  108. }
  109. EipStatus ResetDevice(void) {
  110. /* add reset code here*/
  111. CloseAllConnections();
  112. CipQosUpdateUsedSetQosValues();
  113. return kEipStatusOk;
  114. }
  115. EipStatus ResetDeviceToInitialConfiguration(void) {
  116. /*rest the parameters */
  117. g_tcpip.encapsulation_inactivity_timeout = 120;
  118. CipQosResetAttributesToDefaultValues();
  119. /*than perform device reset*/
  120. ResetDevice();
  121. return kEipStatusOk;
  122. }
  123. void *
  124. CipCalloc(size_t number_of_elements,
  125. size_t size_of_element) {
  126. return calloc(number_of_elements, size_of_element);
  127. }
  128. void CipFree(void *data) {
  129. free(data);
  130. }
  131. void RunIdleChanged(EipUint32 run_idle_value) {
  132. OPENER_TRACE_INFO("Run/Idle handler triggered\n");
  133. if( (0x0001 & run_idle_value) == 1 ) {
  134. CipIdentitySetExtendedDeviceStatus(kAtLeastOneIoConnectionInRunMode);
  135. } else {
  136. CipIdentitySetExtendedDeviceStatus(
  137. kAtLeastOneIoConnectionEstablishedAllInIdleMode);
  138. }
  139. (void) run_idle_value;
  140. }