sv_publisher.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * sv_publisher.h
  3. *
  4. * Copyright 2016-2022 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 LIBIEC61850_SRC_SAMPLED_VALUES_SV_PUBLISHER_H_
  24. #define LIBIEC61850_SRC_SAMPLED_VALUES_SV_PUBLISHER_H_
  25. #include "iec61850_common.h"
  26. #include "r_session.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #ifndef GOOSE_SV_COMM_PARAMETERS
  31. #define GOOSE_SV_COMM_PARAMETERS
  32. typedef struct sCommParameters {
  33. uint8_t vlanPriority;
  34. uint16_t vlanId;
  35. uint16_t appId;
  36. uint8_t dstAddress[6];
  37. } CommParameters;
  38. #endif
  39. /**
  40. * \defgroup sv_publisher_api_group IEC 61850 Sampled Values (SV) publisher API
  41. */
  42. /**@{*/
  43. #define IEC61850_SV_SMPSYNC_NOT_SYNCHRONIZED 0
  44. #define IEC61850_SV_SMPSYNC_SYNCED_UNSPEC_LOCAL_CLOCK 1
  45. #define IEC61850_SV_SMPSYNC_SYNCED_GLOBAL_CLOCK 2
  46. #define IEC61850_SV_SMPMOD_PER_NOMINAL_PERIOD 0
  47. #define IEC61850_SV_SMPMOD_SAMPLES_PER_SECOND 1
  48. #define IEC61850_SV_SMPMOD_SECONDS_PER_SAMPLE 2
  49. /**
  50. * \brief An opaque type representing an IEC 61850-9-2 Sampled Values publisher.
  51. */
  52. typedef struct sSVPublisher* SVPublisher;
  53. /**
  54. * \brief An opaque type representing an IEC 61850-9-2 Sampled Values Application Service Data Unit (ASDU).
  55. */
  56. typedef struct sSVPublisher_ASDU* SVPublisher_ASDU;
  57. /**
  58. * \brief Create a new IEC61850-9-2 Sampled Values publisher.
  59. *
  60. * NOTE: VLAN tagging is enabled when calling this constructor. To disable VLAN tagging
  61. * use \ref SVPublisher_createEx instead.
  62. *
  63. * \param[in] interfaceId the name of the interface over which the SV publisher should send SV packets.
  64. * \param[in] parameters optional parameters for setting VLAN options and destination MAC address. Use NULL for default values.
  65. * \return the new SV publisher instance.
  66. */
  67. LIB61850_API SVPublisher
  68. SVPublisher_create(CommParameters* parameters, const char* interfaceId);
  69. /**
  70. * \brief Create a new IEC61850-9-2 Sampled Values publisher.
  71. *
  72. * \param[in] interfaceId the name of the interface over which the SV publisher should send SV packets.
  73. * \param[in] parameters optional parameters for setting VLAN options and destination MAC address. Use NULL for default values.
  74. * \param[in] useVlanTags enable(true)/disable(false) VLAN tagging
  75. * \return the new SV publisher instance.
  76. */
  77. LIB61850_API SVPublisher
  78. SVPublisher_createEx(CommParameters* parameters, const char* interfaceId, bool useVlanTag);
  79. /**
  80. * \brief Create a new SVPublisher instance for R-SMV
  81. *
  82. * \param session R-session protocol instance to use
  83. * \param appId the appID value to use
  84. *
  85. * \return the new SVPublisher instance
  86. */
  87. LIB61850_API SVPublisher
  88. SVPublisher_createRemote(RSession session, uint16_t appId);
  89. /**
  90. * \brief Create an Application Service Data Unit (ASDU) and add it to an existing Sampled Values publisher.
  91. *
  92. * \param[in] svID
  93. * \param[in] datset
  94. * \param[in] confRev Configuration revision number. Should be incremented each time that the configuration of the logical device changes.
  95. * \return the new ASDU instance.
  96. */
  97. LIB61850_API SVPublisher_ASDU
  98. SVPublisher_addASDU(SVPublisher self, const char* svID, const char* datset, uint32_t confRev);
  99. /**
  100. * \brief Prepare the publisher for publishing.
  101. *
  102. * This method must be called before SVPublisher_publish().
  103. *
  104. * \param[in] self the Sampled Values publisher instance.
  105. */
  106. LIB61850_API void
  107. SVPublisher_setupComplete(SVPublisher self);
  108. /**
  109. * \brief Publish all registered ASDUs
  110. *
  111. * \param[in] self the Sampled Values publisher instance.
  112. */
  113. LIB61850_API void
  114. SVPublisher_publish(SVPublisher self);
  115. /**
  116. * \brief Destroy an IEC61850-9-2 Sampled Values instance.
  117. *
  118. * \param[in] self the Sampled Values publisher instance.
  119. */
  120. LIB61850_API void
  121. SVPublisher_destroy(SVPublisher self);
  122. /**
  123. * \addtogroup sv_publisher_asdu_group Values Application Service Data Unit (ASDU)
  124. * @{
  125. */
  126. /**
  127. * \brief Reset the internal data buffer of an ASDU.
  128. *
  129. * All data elements added by SVPublisher_ASDU_add*() functions are removed.
  130. * SVPublisher_setupComplete() must be called afterwards.
  131. *
  132. * \param[in] self the Sampled Values ASDU instance.
  133. */
  134. LIB61850_API void
  135. SVPublisher_ASDU_resetBuffer(SVPublisher_ASDU self);
  136. /**
  137. * \brief Reserve memory for a signed 8-bit integer in the ASDU.
  138. *
  139. * \param[in] self the Sampled Values ASDU instance.
  140. * \return the offset in bytes within the ASDU data block.
  141. */
  142. LIB61850_API int
  143. SVPublisher_ASDU_addINT8(SVPublisher_ASDU self);
  144. /**
  145. * \brief Set the value of a 8-bit integer in the ASDU.
  146. *
  147. * \param[in] self the Sampled Values ASDU instance.
  148. * \param[in] index The offset within the data block of the ASDU in bytes.
  149. * \param[in] value The value which should be set.
  150. */
  151. LIB61850_API void
  152. SVPublisher_ASDU_setINT8(SVPublisher_ASDU self, int index, int8_t value);
  153. /**
  154. * \brief Reserve memory for a signed 32-bit integer in the ASDU.
  155. *
  156. * \param[in] self the Sampled Values ASDU instance.
  157. * \return the offset in bytes within the ASDU data block.
  158. */
  159. LIB61850_API int
  160. SVPublisher_ASDU_addINT32(SVPublisher_ASDU self);
  161. /**
  162. * \brief Set the value of a 32-bit integer in the ASDU.
  163. *
  164. * \param[in] self the Sampled Values ASDU instance.
  165. * \param[in] index The offset within the data block of the ASDU in bytes.
  166. * \param[in] value The value which should be set.
  167. */
  168. LIB61850_API void
  169. SVPublisher_ASDU_setINT32(SVPublisher_ASDU self, int index, int32_t value);
  170. /**
  171. * \brief Reserve memory for a signed 64-bit integer in the ASDU.
  172. *
  173. * \param[in] self the Sampled Values ASDU instance.
  174. * \return the offset in bytes of the new element within the ASDU data block.
  175. */
  176. LIB61850_API int
  177. SVPublisher_ASDU_addINT64(SVPublisher_ASDU self);
  178. /**
  179. * \brief Set the value of a 64-bit integer in the ASDU.
  180. *
  181. * \param[in] self the Sampled Values ASDU instance.
  182. * \param[in] index The offset within the data block of the ASDU in bytes.
  183. * \param[in] value The value which should be set.
  184. */
  185. LIB61850_API void
  186. SVPublisher_ASDU_setINT64(SVPublisher_ASDU self, int index, int64_t value);
  187. /**
  188. * \brief Reserve memory for a single precision floating point number in the ASDU.
  189. *
  190. * \param[in] self the Sampled Values ASDU instance.
  191. * \return the offset in bytes of the new element within the ASDU data block.
  192. */
  193. LIB61850_API int
  194. SVPublisher_ASDU_addFLOAT(SVPublisher_ASDU self);
  195. /**
  196. * \brief Set the value of a single precision floating point number in the ASDU.
  197. *
  198. * \param[in] self the Sampled Values ASDU instance.
  199. * \param[in] index The offset within the data block of the ASDU in bytes.
  200. * \param[in] value The value which should be set.
  201. */
  202. LIB61850_API void
  203. SVPublisher_ASDU_setFLOAT(SVPublisher_ASDU self, int index, float value);
  204. /**
  205. * \brief Reserve memory for a double precision floating point number in the ASDU.
  206. *
  207. * \param[in] self the Sampled Values ASDU instance.
  208. * \return the offset in bytes of the new element within the ASDU data block.
  209. */
  210. LIB61850_API int
  211. SVPublisher_ASDU_addFLOAT64(SVPublisher_ASDU self);
  212. /**
  213. * \brief Set the value of a double precision floating pointer number in the ASDU.
  214. *
  215. * \param[in] self the Sampled Values ASDU instance.
  216. * \param[in] index The offset within the data block of the ASDU in bytes.
  217. * \param[in] value The value which should be set.
  218. */
  219. LIB61850_API void
  220. SVPublisher_ASDU_setFLOAT64(SVPublisher_ASDU self, int index, double value);
  221. /**
  222. * \brief Reserve memory for a 64 bit time stamp in the ASDU
  223. *
  224. * \param[in] self the Sampled Values ASDU instance.
  225. * \return the offset in bytes of the new element within the ASDU data block.
  226. */
  227. LIB61850_API int
  228. SVPublisher_ASDU_addTimestamp(SVPublisher_ASDU self);
  229. /**
  230. * \brief Set the value of a 64 bit time stamp in the ASDU.
  231. *
  232. * \param[in] self the Sampled Values ASDU instance.
  233. * \param[in] index The offset within the data block of the ASDU in bytes.
  234. * \param[in] value The value which should be set.
  235. */
  236. LIB61850_API void
  237. SVPublisher_ASDU_setTimestamp(SVPublisher_ASDU self, int index, Timestamp value);
  238. /**
  239. * \brief Reserve memory for a quality value in the ASDU
  240. *
  241. * NOTE: Quality is encoded as BITSTRING (4 byte)
  242. *
  243. * \param[in] self the Sampled Values ASDU instance.
  244. * \return the offset in bytes of the new element within the ASDU data block.
  245. */
  246. LIB61850_API int
  247. SVPublisher_ASDU_addQuality(SVPublisher_ASDU self);
  248. /**
  249. * \brief Set the value of a quality attribute in the ASDU.
  250. *
  251. * \param[in] self the Sampled Values ASDU instance.
  252. * \param[in] index The offset within the data block of the ASDU in bytes.
  253. * \param[in] value The value which should be set.
  254. */
  255. LIB61850_API void
  256. SVPublisher_ASDU_setQuality(SVPublisher_ASDU self, int index, Quality value);
  257. /**
  258. * \brief Set the sample count attribute of the ASDU.
  259. *
  260. * \param[in] self the Sampled Values ASDU instance.
  261. * \param[in] value the new value of the attribute.
  262. */
  263. LIB61850_API void
  264. SVPublisher_ASDU_setSmpCnt(SVPublisher_ASDU self, uint16_t value);
  265. /**
  266. * \brief Get the sample count attribute of the ASDU.
  267. *
  268. * \param[in] self the Sampled Values ASDU instance.
  269. */
  270. LIB61850_API uint16_t
  271. SVPublisher_ASDU_getSmpCnt(SVPublisher_ASDU self);
  272. /**
  273. * \brief Increment the sample count attribute of the ASDU.
  274. *
  275. * The parameter SmpCnt shall contain the values of a counter, which is incremented each time a new sample of the analogue value is taken.
  276. * The sample values shall be kept in the right order.
  277. * If the counter is used to indicate time consistency of various sampled values, the counter shall be reset by an external synchronization event.
  278. *
  279. * \param[in] self the Sampled Values ASDU instance.
  280. */
  281. LIB61850_API void
  282. SVPublisher_ASDU_increaseSmpCnt(SVPublisher_ASDU self);
  283. /**
  284. * \brief Set the roll-over (wrap) limit for the sample counter. When reaching the limit the
  285. * sample counter will be reset to 0 (default is no limit)
  286. *
  287. * \param[in] self the Sampled Values ASDU instance.
  288. * \param[in] value the new sample counter limit
  289. */
  290. LIB61850_API void
  291. SVPublisher_ASDU_setSmpCntWrap(SVPublisher_ASDU self, uint16_t value);
  292. /**
  293. * \brief Enables the transmission of refresh time attribute of the ASDU
  294. *
  295. * The refresh time is the time when the data in put into the sampled values buffer
  296. *
  297. * \param[in] self the Sampled Values ASDU instance.
  298. */
  299. LIB61850_API void
  300. SVPublisher_ASDU_enableRefrTm(SVPublisher_ASDU self);
  301. /**
  302. * \brief Set the refresh time attribute of the ASDU with nanosecond resolution
  303. *
  304. * \param[in] self the Sampled Values ASDU instance.
  305. * \param[in] refrTmNs the refresh time value with nanoseconds resolution.
  306. */
  307. LIB61850_API void
  308. SVPublisher_ASDU_setRefrTmNs(SVPublisher_ASDU self, nsSinceEpoch refrTmNs);
  309. /**
  310. * \brief Set the refresh time attribute of the ASDU with millisecond resolution
  311. *
  312. * \param[in] self the Sampled Values ASDU instance.
  313. * \param[in] refrTmNs the refresh time value with with milliseconds resolution.
  314. */
  315. LIB61850_API void
  316. SVPublisher_ASDU_setRefrTm(SVPublisher_ASDU self, msSinceEpoch refrTm);
  317. /**
  318. * \brief Set the refresh time attribute of the ASDU
  319. *
  320. * NOTE: Using this function you can control the time quality flags and the
  321. * sub second precision (number of valid bits) of the RefrTm value.
  322. *
  323. * \param[in] self the Sampled Values ASDU instance.
  324. * \param[in] refrTm the refresh time value
  325. */
  326. LIB61850_API void
  327. SVPublisher_ASDU_setRefrTmByTimestamp(SVPublisher_ASDU self, Timestamp* refrTm);
  328. /**
  329. * \brief Set the sample mode attribute of the ASDU.
  330. *
  331. * The attribute SmpMod shall specify if the sample rate is defined in units of samples per nominal period, samples per second or seconds per sample.
  332. * If it is missing, the default value is samples per period.
  333. *
  334. * NOTE: Function has to be called before calling \ref SVPublisher_setupComplete
  335. *
  336. * \param[in] self the Sampled Values ASDU instance.
  337. * \param[in] smpMod one of IEC61850_SV_SMPMOD_PER_NOMINAL_PERIOD, IEC61850_SV_SMPMOD_SAMPLES_PER_SECOND or IEC61850_SV_SMPMOD_SECONDS_PER_SAMPLE
  338. */
  339. LIB61850_API void
  340. SVPublisher_ASDU_setSmpMod(SVPublisher_ASDU self, uint8_t smpMod);
  341. /**
  342. * \brief Set the sample rate attribute of the ASDU.
  343. *
  344. * The attribute SmpRate shall specify the sample rate.
  345. * The value shall be interpreted depending on the value of the SmpMod attribute.
  346. *
  347. * NOTE: Function has to be called before calling \ref SVPublisher_setupComplete
  348. *
  349. * \param[in] self the Sampled Values ASDU instance.
  350. * \param[in] smpRate Amount of samples (default per nominal period, see SmpMod).
  351. */
  352. LIB61850_API void
  353. SVPublisher_ASDU_setSmpRate(SVPublisher_ASDU self, uint16_t smpRate);
  354. /**
  355. * \brief Set the clock synchronization information
  356. *
  357. * Default value is not synchronized (0).
  358. * Possible values are:
  359. * 0 = SV are not synchronized by an external clock signal.
  360. * 1 = SV are synchronized by a clock signal from an unspecified local area clock.
  361. * 2 = SV are synchronized by a global area clock signal (time traceable).
  362. * 5 to 254 = SV are synchronized by a clock signal from a local area clock identified by this value.
  363. * 3;4;255 = Reserved values – Do not use.
  364. *
  365. * \param[in] self the Sampled Values ASDU instance.
  366. * \param[in] smpSynch the clock synchronization state
  367. */
  368. LIB61850_API void
  369. SVPublisher_ASDU_setSmpSynch(SVPublisher_ASDU self, uint16_t smpSynch);
  370. /**@} @}*/
  371. #ifndef DEPRECATED
  372. #if defined(__GNUC__) || defined(__clang__)
  373. #define DEPRECATED __attribute__((deprecated))
  374. #else
  375. #define DEPRECATED
  376. #endif
  377. #endif
  378. /**
  379. * \addtogroup sv_publisher_deprecated_api_group Deprecated API
  380. * \ingroup sv_publisher_api_group IEC 61850 Sampled Values (SV) publisher API
  381. * \deprecated
  382. * @{
  383. */
  384. typedef struct sSVPublisher* SampledValuesPublisher;
  385. typedef struct sSV_ASDU* SV_ASDU;
  386. LIB61850_API DEPRECATED SVPublisher
  387. SampledValuesPublisher_create(CommParameters* parameters, const char* interfaceId);
  388. LIB61850_API DEPRECATED SVPublisher_ASDU
  389. SampledValuesPublisher_addASDU(SVPublisher self, char* svID, char* datset, uint32_t confRev);
  390. LIB61850_API DEPRECATED void
  391. SampledValuesPublisher_setupComplete(SVPublisher self);
  392. LIB61850_API DEPRECATED void
  393. SampledValuesPublisher_publish(SVPublisher self);
  394. LIB61850_API DEPRECATED void
  395. SampledValuesPublisher_destroy(SVPublisher self);
  396. LIB61850_API DEPRECATED void
  397. SV_ASDU_resetBuffer(SVPublisher_ASDU self);
  398. LIB61850_API DEPRECATED int
  399. SV_ASDU_addINT8(SVPublisher_ASDU self);
  400. LIB61850_API DEPRECATED void
  401. SV_ASDU_setINT8(SVPublisher_ASDU self, int index, int8_t value);
  402. LIB61850_API DEPRECATED int
  403. SV_ASDU_addINT32(SVPublisher_ASDU self);
  404. LIB61850_API DEPRECATED void
  405. SV_ASDU_setINT32(SVPublisher_ASDU self, int index, int32_t value);
  406. LIB61850_API DEPRECATED int
  407. SV_ASDU_addINT64(SVPublisher_ASDU self);
  408. LIB61850_API DEPRECATED void
  409. SV_ASDU_setINT64(SVPublisher_ASDU self, int index, int64_t value);
  410. LIB61850_API DEPRECATED int
  411. SV_ASDU_addFLOAT(SVPublisher_ASDU self);
  412. LIB61850_API DEPRECATED void
  413. SV_ASDU_setFLOAT(SVPublisher_ASDU self, int index, float value);
  414. LIB61850_API DEPRECATED int
  415. SV_ASDU_addFLOAT64(SVPublisher_ASDU self);
  416. LIB61850_API DEPRECATED void
  417. SV_ASDU_setFLOAT64(SVPublisher_ASDU self, int index, double value);
  418. LIB61850_API DEPRECATED void
  419. SV_ASDU_setSmpCnt(SVPublisher_ASDU self, uint16_t value);
  420. LIB61850_API DEPRECATED uint16_t
  421. SV_ASDU_getSmpCnt(SVPublisher_ASDU self);
  422. LIB61850_API DEPRECATED void
  423. SV_ASDU_increaseSmpCnt(SVPublisher_ASDU self);
  424. LIB61850_API DEPRECATED void
  425. SV_ASDU_setRefrTm(SVPublisher_ASDU self, uint64_t refrTm);
  426. LIB61850_API DEPRECATED void
  427. SV_ASDU_setSmpMod(SVPublisher_ASDU self, uint8_t smpMod);
  428. LIB61850_API DEPRECATED void
  429. SV_ASDU_setSmpRate(SVPublisher_ASDU self, uint16_t smpRate);
  430. /**@}*/
  431. #ifdef __cplusplus
  432. }
  433. #endif
  434. #endif /* LIBIEC61850_SRC_SAMPLED_VALUES_SV_PUBLISHER_H_ */