iec61850_common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * iec61850_common.h
  3. *
  4. * Copyright 2013-2019 Michael Zillgith
  5. *
  6. * This file is part of libIEC61850.
  7. *
  8. * libIEC61850 is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * libIEC61850 is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with libIEC61850. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * See COPYING file for the complete license text.
  22. */
  23. #ifndef IEC61850_COMMON_H_
  24. #define IEC61850_COMMON_H_
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #include "libiec61850_common_api.h"
  29. #include "logging_api.h"
  30. #include "linked_list.h"
  31. /**
  32. * @defgroup iec61850_common_api_group IEC 61850 API common parts
  33. */
  34. /**@{*/
  35. /** IEC 61850 edition 1 */
  36. #define IEC_61850_EDITION_1 0
  37. /** IEC 61850 edition 2 */
  38. #define IEC_61850_EDITION_2 1
  39. /** IEC 61850 edition 2.1 */
  40. #define IEC_61850_EDITION_2_1 2
  41. /** PhyComAddress type contains Ethernet address and VLAN attributes */
  42. typedef struct {
  43. uint8_t vlanPriority;
  44. uint16_t vlanId;
  45. uint16_t appId;
  46. uint8_t dstAddress[6];
  47. } PhyComAddress;
  48. /** IEC 61850 ACSI classes */
  49. typedef enum {
  50. ACSI_CLASS_DATA_OBJECT,
  51. ACSI_CLASS_DATA_SET,
  52. ACSI_CLASS_BRCB,
  53. ACSI_CLASS_URCB,
  54. ACSI_CLASS_LCB,
  55. ACSI_CLASS_LOG,
  56. ACSI_CLASS_SGCB,
  57. ACSI_CLASS_GoCB,
  58. ACSI_CLASS_GsCB,
  59. ACSI_CLASS_MSVCB,
  60. ACSI_CLASS_USVCB
  61. } ACSIClass;
  62. /**
  63. * \brief Control model (represented by "ctlModel" attribute)
  64. */
  65. typedef enum {
  66. /**
  67. * No support for control functions. Control object only support status information.
  68. */
  69. CONTROL_MODEL_STATUS_ONLY = 0,
  70. /**
  71. * Direct control with normal security: Supports Operate, TimeActivatedOperate (optional),
  72. * and Cancel (optional).
  73. */
  74. CONTROL_MODEL_DIRECT_NORMAL = 1,
  75. /**
  76. * Select before operate (SBO) with normal security: Supports Select, Operate, TimeActivatedOperate (optional),
  77. * and Cancel (optional).
  78. */
  79. CONTROL_MODEL_SBO_NORMAL = 2,
  80. /**
  81. * Direct control with enhanced security (enhanced security includes the CommandTermination service)
  82. */
  83. CONTROL_MODEL_DIRECT_ENHANCED = 3,
  84. /**
  85. * Select before operate (SBO) with enhanced security (enhanced security includes the CommandTermination service)
  86. */
  87. CONTROL_MODEL_SBO_ENHANCED = 4
  88. } ControlModel;
  89. /**
  90. * @defgroup TRIGGER_OPTIONS Trigger options (bit values combinable)
  91. *
  92. * @{
  93. */
  94. /** Report will be triggered when data changes */
  95. #define TRG_OPT_DATA_CHANGED 1
  96. /** Report will be triggered when quality changes */
  97. #define TRG_OPT_QUALITY_CHANGED 2
  98. /** Report will be triggered when data is updated */
  99. #define TRG_OPT_DATA_UPDATE 4
  100. /** Report will be triggered periodically */
  101. #define TRG_OPT_INTEGRITY 8
  102. /** Report will be triggered by GI (general interrogation) request */
  103. #define TRG_OPT_GI 16
  104. /** Report will be triggered only on rising edge (transient variable */
  105. #define TRG_OPT_TRANSIENT 128
  106. /** @} */
  107. /**
  108. * @defgroup REPORT_OPTIONS Report options (bit values combinable)
  109. *
  110. * @{
  111. */
  112. /** Report contains sequence number */
  113. #define RPT_OPT_SEQ_NUM 1
  114. /** Report contains a report timestamp */
  115. #define RPT_OPT_TIME_STAMP 2
  116. /** Report contains reason for inclusion value for each included data set member */
  117. #define RPT_OPT_REASON_FOR_INCLUSION 4
  118. /** Report contains data set object reference */
  119. #define RPT_OPT_DATA_SET 8
  120. /** Report contains data reference for each included data set member */
  121. #define RPT_OPT_DATA_REFERENCE 16
  122. /** Report contains buffer overflow flag */
  123. #define RPT_OPT_BUFFER_OVERFLOW 32
  124. /** Report contains entry id */
  125. #define RPT_OPT_ENTRY_ID 64
  126. /** Report contains configuration revision */
  127. #define RPT_OPT_CONF_REV 128
  128. /** @} */
  129. /**
  130. * @defgroup ORIGINATOR_CATEGORIES Originator categories (orCat)
  131. *
  132. * @{
  133. */
  134. /** Not supported - should not be used */
  135. #define CONTROL_ORCAT_NOT_SUPPORTED 0
  136. /** Control operation issued from an operator using a client located at bay level */
  137. #define CONTROL_ORCAT_BAY_CONTROL 1
  138. /** Control operation issued from an operator using a client located at station level */
  139. #define CONTROL_ORCAT_STATION_CONTROL 2
  140. /** Control operation from a remote operator outside the substation (for example network control center) */
  141. #define CONTROL_ORCAT_REMOTE_CONTROL 3
  142. /** Control operation issued from an automatic function at bay level */
  143. #define CONTROL_ORCAT_AUTOMATIC_BAY 4
  144. /** Control operation issued from an automatic function at station level */
  145. #define CONTROL_ORCAT_AUTOMATIC_STATION 5
  146. /** Control operation issued from a automatic function outside of the substation */
  147. #define CONTROL_ORCAT_AUTOMATIC_REMOTE 6
  148. /** Control operation issued from a maintenance/service tool */
  149. #define CONTROL_ORCAT_MAINTENANCE 7
  150. /** Status change occurred without control action (for example external trip of a circuit breaker or failure inside the breaker) */
  151. #define CONTROL_ORCAT_PROCESS 8
  152. /** @} */
  153. /**
  154. * @defgroup CONTROL_ADD_CAUSE Definition for addCause type - used in control models
  155. *
  156. * @{
  157. */
  158. /** AddCause - additional cause information for control model errors */
  159. typedef enum {
  160. ADD_CAUSE_UNKNOWN = 0,
  161. ADD_CAUSE_NOT_SUPPORTED = 1,
  162. ADD_CAUSE_BLOCKED_BY_SWITCHING_HIERARCHY = 2,
  163. ADD_CAUSE_SELECT_FAILED = 3,
  164. ADD_CAUSE_INVALID_POSITION = 4,
  165. ADD_CAUSE_POSITION_REACHED = 5,
  166. ADD_CAUSE_PARAMETER_CHANGE_IN_EXECUTION = 6,
  167. ADD_CAUSE_STEP_LIMIT = 7,
  168. ADD_CAUSE_BLOCKED_BY_MODE = 8,
  169. ADD_CAUSE_BLOCKED_BY_PROCESS = 9,
  170. ADD_CAUSE_BLOCKED_BY_INTERLOCKING = 10,
  171. ADD_CAUSE_BLOCKED_BY_SYNCHROCHECK = 11,
  172. ADD_CAUSE_COMMAND_ALREADY_IN_EXECUTION = 12,
  173. ADD_CAUSE_BLOCKED_BY_HEALTH = 13,
  174. ADD_CAUSE_1_OF_N_CONTROL = 14,
  175. ADD_CAUSE_ABORTION_BY_CANCEL = 15,
  176. ADD_CAUSE_TIME_LIMIT_OVER = 16,
  177. ADD_CAUSE_ABORTION_BY_TRIP = 17,
  178. ADD_CAUSE_OBJECT_NOT_SELECTED = 18,
  179. ADD_CAUSE_OBJECT_ALREADY_SELECTED = 19,
  180. ADD_CAUSE_NO_ACCESS_AUTHORITY = 20,
  181. ADD_CAUSE_ENDED_WITH_OVERSHOOT = 21,
  182. ADD_CAUSE_ABORTION_DUE_TO_DEVIATION = 22,
  183. ADD_CAUSE_ABORTION_BY_COMMUNICATION_LOSS = 23,
  184. ADD_CAUSE_ABORTION_BY_COMMAND = 24,
  185. ADD_CAUSE_NONE = 25,
  186. ADD_CAUSE_INCONSISTENT_PARAMETERS = 26,
  187. ADD_CAUSE_LOCKED_BY_OTHER_CLIENT = 27
  188. } ControlAddCause;
  189. /** @} */
  190. /**
  191. * @defgroup CONTROL_LAST_APPL_ERROR Definition for LastAppError error type - used in control models
  192. *
  193. * @{
  194. */
  195. typedef enum {
  196. CONTROL_ERROR_NO_ERROR = 0,
  197. CONTROL_ERROR_UNKNOWN = 1,
  198. CONTROL_ERROR_TIMEOUT_TEST = 2,
  199. CONTROL_ERROR_OPERATOR_TEST = 3
  200. } ControlLastApplError;
  201. /** @} */
  202. /**
  203. * @defgroup FUNCTIONAL_CONSTRAINTS Definitions and functions related to functional constraints (FCs)
  204. *
  205. * @{
  206. */
  207. /** FCs (Functional constraints) according to IEC 61850-7-2 */
  208. typedef enum eFunctionalConstraint {
  209. /** Status information */
  210. IEC61850_FC_ST = 0,
  211. /** Measurands - analog values */
  212. IEC61850_FC_MX = 1,
  213. /** Setpoint */
  214. IEC61850_FC_SP = 2,
  215. /** Substitution */
  216. IEC61850_FC_SV = 3,
  217. /** Configuration */
  218. IEC61850_FC_CF = 4,
  219. /** Description */
  220. IEC61850_FC_DC = 5,
  221. /** Setting group */
  222. IEC61850_FC_SG = 6,
  223. /** Setting group editable */
  224. IEC61850_FC_SE = 7,
  225. /** Service response / Service tracking */
  226. IEC61850_FC_SR = 8,
  227. /** Operate received */
  228. IEC61850_FC_OR = 9,
  229. /** Blocking */
  230. IEC61850_FC_BL = 10,
  231. /** Extended definition */
  232. IEC61850_FC_EX = 11,
  233. /** Control */
  234. IEC61850_FC_CO = 12,
  235. /** Unicast SV */
  236. IEC61850_FC_US = 13,
  237. /** Multicast SV */
  238. IEC61850_FC_MS = 14,
  239. /** Unbuffered report */
  240. IEC61850_FC_RP = 15,
  241. /** Buffered report */
  242. IEC61850_FC_BR = 16,
  243. /** Log control blocks */
  244. IEC61850_FC_LG = 17,
  245. /** Goose control blocks */
  246. IEC61850_FC_GO = 18,
  247. /** All FCs - wildcard value */
  248. IEC61850_FC_ALL = 99,
  249. IEC61850_FC_NONE = -1
  250. } FunctionalConstraint;
  251. /**extern "C" {
  252. * \brief convert a function constraint to a static string
  253. */
  254. LIB61850_API char*
  255. FunctionalConstraint_toString(FunctionalConstraint fc);
  256. /**
  257. * \brief parse a string treated as a functional constraint representation
  258. */
  259. LIB61850_API FunctionalConstraint
  260. FunctionalConstraint_fromString(const char* fcString);
  261. /** @} */
  262. /**
  263. * @defgroup QUALITY Definitions and functions related to data attribute quality
  264. *
  265. * @{
  266. */
  267. typedef uint16_t Quality;
  268. typedef uint16_t Validity;
  269. #define QUALITY_VALIDITY_GOOD 0
  270. #define QUALITY_VALIDITY_INVALID 2
  271. #define QUALITY_VALIDITY_RESERVED 1
  272. #define QUALITY_VALIDITY_QUESTIONABLE 3
  273. #define QUALITY_DETAIL_OVERFLOW 4
  274. #define QUALITY_DETAIL_OUT_OF_RANGE 8
  275. #define QUALITY_DETAIL_BAD_REFERENCE 16
  276. #define QUALITY_DETAIL_OSCILLATORY 32
  277. #define QUALITY_DETAIL_FAILURE 64
  278. #define QUALITY_DETAIL_OLD_DATA 128
  279. #define QUALITY_DETAIL_INCONSISTENT 256
  280. #define QUALITY_DETAIL_INACCURATE 512
  281. #define QUALITY_SOURCE_SUBSTITUTED 1024
  282. #define QUALITY_TEST 2048
  283. #define QUALITY_OPERATOR_BLOCKED 4096
  284. #define QUALITY_DERIVED 8192
  285. LIB61850_API Validity
  286. Quality_getValidity(Quality* self);
  287. LIB61850_API void
  288. Quality_setValidity(Quality* self, Validity validity);
  289. LIB61850_API void
  290. Quality_setFlag(Quality* self, int flag);
  291. LIB61850_API void
  292. Quality_unsetFlag(Quality* self, int flag);
  293. LIB61850_API bool
  294. Quality_isFlagSet(Quality* self, int flag);
  295. LIB61850_API Quality
  296. Quality_fromMmsValue(const MmsValue* mmsValue);
  297. LIB61850_API MmsValue*
  298. Quality_toMmsValue(Quality* self, MmsValue* mmsValue);
  299. /** @} */
  300. /**
  301. * @defgroup DBPOS Definitions and functions related to IEC 61850 Dbpos (a CODED ENUM) data type
  302. *
  303. * @{
  304. */
  305. typedef enum {
  306. DBPOS_INTERMEDIATE_STATE = 0,
  307. DBPOS_OFF = 1,
  308. DBPOS_ON = 2,
  309. DBPOS_BAD_STATE = 3
  310. } Dbpos;
  311. /**
  312. * \brief convert MMS bit string to Dbpos enumeration type
  313. *
  314. * \param mmsValue the MmsValue instance representing the Dbpos value
  315. *
  316. * \return the corresponding Dbpos value
  317. */
  318. LIB61850_API Dbpos
  319. Dbpos_fromMmsValue(const MmsValue* mmsValue);
  320. /**
  321. * \brief conver Dbpos to MMS bit string
  322. *
  323. * \param mmsValue the MmsValue instance representing a Dbpos value or NULL to create a new MmsValue instance
  324. * \param a Dbpos value
  325. *
  326. * \return the corresponding MmsValue instance
  327. */
  328. LIB61850_API MmsValue*
  329. Dbpos_toMmsValue(MmsValue* mmsValue, Dbpos dbpos);
  330. /** @} */
  331. /**
  332. * @defgroup TIMESTAMP Definitions and functions related to IEC 61850 Timestamp (UTC Time) data type
  333. *
  334. * @{
  335. */
  336. typedef union {
  337. uint8_t val[8];
  338. } Timestamp;
  339. LIB61850_API Timestamp*
  340. Timestamp_create(void);
  341. LIB61850_API Timestamp*
  342. Timestamp_createFromByteArray(const uint8_t* byteArray);
  343. LIB61850_API void
  344. Timestamp_destroy(Timestamp* self);
  345. LIB61850_API void
  346. Timestamp_clearFlags(Timestamp* self);
  347. LIB61850_API uint32_t
  348. Timestamp_getTimeInSeconds(Timestamp* self);
  349. LIB61850_API msSinceEpoch
  350. Timestamp_getTimeInMs(Timestamp* self);
  351. LIB61850_API nsSinceEpoch
  352. Timestamp_getTimeInNs(Timestamp* self);
  353. LIB61850_API bool
  354. Timestamp_isLeapSecondKnown(Timestamp* self);
  355. LIB61850_API void
  356. Timestamp_setLeapSecondKnown(Timestamp* self, bool value);
  357. LIB61850_API bool
  358. Timestamp_hasClockFailure(Timestamp* self);
  359. LIB61850_API void
  360. Timestamp_setClockFailure(Timestamp* self, bool value);
  361. LIB61850_API bool
  362. Timestamp_isClockNotSynchronized(Timestamp* self);
  363. LIB61850_API void
  364. Timestamp_setClockNotSynchronized(Timestamp* self, bool value);
  365. LIB61850_API int
  366. Timestamp_getSubsecondPrecision(Timestamp* self);
  367. LIB61850_API void
  368. Timestamp_setFractionOfSecondPart(Timestamp* self, uint32_t fractionOfSecond);
  369. LIB61850_API uint32_t
  370. Timestamp_getFractionOfSecondPart(Timestamp* self);
  371. LIB61850_API float
  372. Timestamp_getFractionOfSecond(Timestamp* self);
  373. /**
  374. * \brief Set the subsecond precision value of the time stamp
  375. *
  376. * \param subsecondPrecision the number of significant bits of the fractionOfSecond part of the time stamp
  377. */
  378. LIB61850_API void
  379. Timestamp_setSubsecondPrecision(Timestamp* self, int subsecondPrecision);
  380. /**
  381. * \brief Set the time in seconds
  382. *
  383. * NOTE: the fractionOfSecond part is set to zero
  384. * NOTE: the subSecondPrecision is not touched
  385. *
  386. * \param self the Timestamp instance
  387. * \param secondsSinceEpoch the seconds since unix epoch (unix timestamp)
  388. */
  389. LIB61850_API void
  390. Timestamp_setTimeInSeconds(Timestamp* self, uint32_t secondsSinceEpoch);
  391. /**
  392. * \brief Set the time in milliseconds
  393. *
  394. * NOTE: the subSecondPrecision is not touched
  395. *
  396. * \param self the Timestamp instance
  397. * \param msTime the milliseconds since unix epoch
  398. */
  399. LIB61850_API void
  400. Timestamp_setTimeInMilliseconds(Timestamp* self, msSinceEpoch msTime);
  401. /**
  402. * \brief Set the time in nanoseconds
  403. *
  404. * NOTE: the subSecondPrecision is not touched
  405. *
  406. * \param self the Timestamp instance
  407. * \param msTime the nanoseconds since unix epoch
  408. */
  409. LIB61850_API void
  410. Timestamp_setTimeInNanoseconds(Timestamp* self, nsSinceEpoch nsTime);
  411. LIB61850_API void
  412. Timestamp_setByMmsUtcTime(Timestamp* self, const MmsValue* mmsValue);
  413. /**
  414. * \brief Set an MmsValue instance of type UTCTime to the timestamp value
  415. *
  416. * \param self the Timestamp instance
  417. * \param mmsValue the mmsValue instance, if NULL a new instance will be created
  418. */
  419. LIB61850_API MmsValue*
  420. Timestamp_toMmsValue(Timestamp* self, MmsValue* mmsValue);
  421. /**
  422. * \brief Get the Timestamp value from an MmsValue instance of type MMS_UTC_TIME
  423. *
  424. * \param self the Timestamp instance or NULL to create a new instance
  425. * \param mmsValue the mmsValue instance of type MMS_UTC_TIME
  426. *
  427. * \return the updated Timestamp value or NULL in case of an error
  428. */
  429. LIB61850_API Timestamp*
  430. Timestamp_fromMmsValue(Timestamp* self, MmsValue* mmsValue);
  431. /**
  432. * \brief Get the version of the library as string
  433. *
  434. * \return the version of the library (e.g. "1.2.2")
  435. */
  436. LIB61850_API char*
  437. LibIEC61850_getVersionString(void);
  438. /** @} */
  439. /**@}*/
  440. #ifdef __cplusplus
  441. }
  442. #endif
  443. #endif /* IEC61850_COMMON_H_ */