iec61850_common.h 14 KB

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