master_example.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * master_example.c
  3. */
  4. #include "iec_include.h"
  5. #include "master_example.h"
  6. static bool running = false;
  7. /* Callback handler to log sent or received messages (optional) */
  8. static void
  9. rawMessageHandler (void* parameter, uint8_t* msg, int msgSize, bool sent)
  10. {
  11. if (sent)
  12. printf("SEND: ");
  13. else
  14. printf("RCVD: ");
  15. int i;
  16. for (i = 0; i < msgSize; i++) {
  17. printf("%02x ", msg[i]);
  18. }
  19. printf("\n");
  20. }
  21. static bool
  22. asduReceivedHandler (void* parameter, int address, CS101_ASDU asdu)
  23. {
  24. printf("SLAVE %i: RECVD ASDU type: %s(%i) elements: %i\n",
  25. address,
  26. TypeID_toString(CS101_ASDU_getTypeID(asdu)),
  27. CS101_ASDU_getTypeID(asdu),
  28. CS101_ASDU_getNumberOfElements(asdu));
  29. if (CS101_ASDU_getTypeID(asdu) == M_ME_TE_1) {
  30. printf(" measured scaled values with CP56Time2a timestamp (M_ME_TE_1):\n");
  31. int i;
  32. for (i = 0; i < CS101_ASDU_getNumberOfElements(asdu); i++) {
  33. MeasuredValueScaledWithCP56Time2a io =
  34. (MeasuredValueScaledWithCP56Time2a) CS101_ASDU_getElement(asdu, i);
  35. if (io != NULL) {
  36. printf(" IOA: %i value: %i\n",
  37. InformationObject_getObjectAddress((InformationObject) io),
  38. MeasuredValueScaled_getValue((MeasuredValueScaled) io)
  39. );
  40. MeasuredValueScaledWithCP56Time2a_destroy(io);
  41. }
  42. else {
  43. printf(" invalid object!\n");
  44. }
  45. }
  46. }
  47. else if (CS101_ASDU_getTypeID(asdu) == M_SP_NA_1) {
  48. printf(" single point information (M_SP_NA_1):\n");
  49. int i;
  50. for (i = 0; i < CS101_ASDU_getNumberOfElements(asdu); i++) {
  51. SinglePointInformation io =
  52. (SinglePointInformation) CS101_ASDU_getElement(asdu, i);
  53. if (io != NULL) {
  54. printf(" IOA: %i value: %i\n",
  55. InformationObject_getObjectAddress((InformationObject) io),
  56. SinglePointInformation_getValue((SinglePointInformation) io)
  57. );
  58. SinglePointInformation_destroy(io);
  59. }
  60. else {
  61. printf(" invalid object!\n");
  62. }
  63. }
  64. }
  65. else if (CS101_ASDU_getTypeID(asdu) == M_EP_TD_1) {
  66. printf(" event of protection equipment (M_EP_TD_1):\n");
  67. int i;
  68. for (i = 0; i < CS101_ASDU_getNumberOfElements(asdu); i++) {
  69. EventOfProtectionEquipmentWithCP56Time2a epe = (EventOfProtectionEquipmentWithCP56Time2a)
  70. CS101_ASDU_getElement(asdu, i);
  71. if (epe != NULL) {
  72. SingleEvent singleEvent = EventOfProtectionEquipmentWithCP56Time2a_getEvent(epe);
  73. printf(" IOA: %i state: %i QDQ: %i\n",
  74. InformationObject_getObjectAddress((InformationObject) epe),
  75. SingleEvent_getEventState(singleEvent),
  76. SingleEvent_getQDP(singleEvent)
  77. );
  78. EventOfProtectionEquipmentWithCP56Time2a_destroy(epe);
  79. }
  80. else {
  81. printf(" invalid object!\n");
  82. }
  83. }
  84. }
  85. return true;
  86. }
  87. static void
  88. linkLayerStateChanged(void* parameter, int address, LinkLayerState state)
  89. {
  90. printf("Link layer state changed for slave %i: ", address);
  91. switch (state) {
  92. case LL_STATE_IDLE:
  93. printf("IDLE\n");
  94. break;
  95. case LL_STATE_ERROR:
  96. printf("ERROR\n");
  97. break;
  98. case LL_STATE_BUSY:
  99. printf("BUSY\n");
  100. break;
  101. case LL_STATE_AVAILABLE:
  102. printf("AVAILABLE\n");
  103. break;
  104. }
  105. }
  106. void iec_master_test(void const* arg)
  107. {
  108. const char* serialPort = "/dev/ttyS1";
  109. SerialPort port = SerialPort_create(serialPort, 9600, 8, 'E', 1);
  110. CS101_Master master = CS101_Master_create(port, NULL, NULL, IEC60870_LINK_LAYER_UNBALANCED);
  111. LinkLayerParameters llParams = CS101_Master_getLinkLayerParameters(master);
  112. llParams->addressLength = 1;
  113. /* Setting the callback handler for generic ASDUs */
  114. CS101_Master_setASDUReceivedHandler(master, asduReceivedHandler, NULL);
  115. /* set callback handler for link layer state changes */
  116. CS101_Master_setLinkLayerStateChanged(master, linkLayerStateChanged, NULL);
  117. /* log messages */
  118. CS101_Master_setRawMessageHandler(master, rawMessageHandler, NULL);
  119. CS101_Master_addSlave(master, 1);
  120. // CS101_Master_addSlave(master, 2);
  121. running = true;
  122. int cycleCounter = 0;
  123. while (running) {
  124. CS101_Master_pollSingleSlave(master, 1);
  125. CS101_Master_run(master);
  126. // CS101_Master_pollSingleSlave(master, 2);
  127. // CS101_Master_run(master);
  128. if (cycleCounter == 10) {
  129. /* Send a general interrogation to a specific slave */
  130. if (CS101_Master_isChannelReady(master, 1)) {
  131. CS101_Master_useSlaveAddress(master, 1);
  132. CS101_Master_sendInterrogationCommand(master, CS101_COT_ACTIVATION, 1, IEC60870_QOI_STATION);
  133. CS101_Master_run(master);
  134. }
  135. else
  136. cycleCounter--;
  137. }
  138. if (cycleCounter == 30) {
  139. /* Send a read request */
  140. if (CS101_Master_isChannelReady(master, 1)) {
  141. CS101_Master_useSlaveAddress(master, 1);
  142. CS101_Master_sendReadCommand(master, 1, 102);
  143. CS101_Master_run(master);
  144. }
  145. else
  146. cycleCounter--;
  147. }
  148. if (cycleCounter == 50) {
  149. if (CS101_Master_isChannelReady(master, 1)) {
  150. printf("Send control command C_SC_NA_1\n");
  151. InformationObject sc = (InformationObject)
  152. SingleCommand_create(NULL, 5000, true, false, 0);
  153. CS101_Master_useSlaveAddress(master, 2);
  154. CS101_Master_sendProcessCommand(master, CS101_COT_ACTIVATION, 1, sc);
  155. CS101_Master_run(master);
  156. InformationObject_destroy(sc);
  157. }
  158. else
  159. cycleCounter--;
  160. }
  161. if (cycleCounter == 80) {
  162. /* Send clock synchronization command */
  163. if (CS101_Master_isChannelReady(master, 1)) {
  164. struct sCP56Time2a newTime;
  165. CP56Time2a_createFromMsTimestamp(&newTime, Hal_getTimeInMs());
  166. printf("Send time sync command\n");
  167. /* Use broadcast address */
  168. CS101_Master_useSlaveAddress(master, 255);
  169. CS101_Master_sendClockSyncCommand(master, 1, &newTime);
  170. CS101_Master_run(master);
  171. }
  172. else
  173. cycleCounter--;
  174. }
  175. delay_ms(100);
  176. cycleCounter++;
  177. }
  178. CS101_Master_destroy(master);
  179. }