The goal of this software is to automatically generate C/C++ code which reads and writes GOOSE and Sampled Value packets. Any valid IEC 61850 Substation Configuration Description (SCD) file, describing GOOSE and/or SV communications, can be used as the input. The output code is lightweight and platform-independent, so it can run on a variety of devices, including low-cost microcontrollers. It\'s ideal for rapid-prototyping new protection and control systems that require communications. This mbed project is a simple example of this functionality. Other code: https://github.com/stevenblair/rapid61850 Project homepage: http://personal.strath.ac.uk/steven.m.blair/

Committer:
sblair
Date:
Fri Oct 07 13:48:18 2011 +0000
Revision:
1:9399d44c2b1a
Parent:
0:230c10b228ea

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sblair 1:9399d44c2b1a 1 /**
sblair 1:9399d44c2b1a 2 * Rapid-prototyping protection schemes with IEC 61850
sblair 1:9399d44c2b1a 3 *
sblair 1:9399d44c2b1a 4 * Copyright (c) 2011 Steven Blair
sblair 1:9399d44c2b1a 5 *
sblair 1:9399d44c2b1a 6 * This program is free software; you can redistribute it and/or
sblair 1:9399d44c2b1a 7 * modify it under the terms of the GNU General Public License
sblair 1:9399d44c2b1a 8 * as published by the Free Software Foundation; either version 2
sblair 1:9399d44c2b1a 9 * of the License, or (at your option) any later version.
sblair 1:9399d44c2b1a 10
sblair 1:9399d44c2b1a 11 * This program is distributed in the hope that it will be useful,
sblair 1:9399d44c2b1a 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sblair 1:9399d44c2b1a 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
sblair 1:9399d44c2b1a 14 * GNU General Public License for more details.
sblair 1:9399d44c2b1a 15
sblair 1:9399d44c2b1a 16 * You should have received a copy of the GNU General Public License
sblair 1:9399d44c2b1a 17 * along with this program; if not, write to the Free Software
sblair 1:9399d44c2b1a 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
sblair 1:9399d44c2b1a 19 */
sblair 1:9399d44c2b1a 20
sblair 0:230c10b228ea 21 #ifndef CTYPES_H
sblair 0:230c10b228ea 22 #define CTYPES_H
sblair 0:230c10b228ea 23
sblair 0:230c10b228ea 24 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
sblair 0:230c10b228ea 25 extern "C" {
sblair 0:230c10b228ea 26 #endif
sblair 0:230c10b228ea 27
sblair 0:230c10b228ea 28 #include <string.h>
sblair 0:230c10b228ea 29
sblair 1:9399d44c2b1a 30 #define LITTLE_ENDIAN 1
sblair 0:230c10b228ea 31
sblair 0:230c10b228ea 32 // platform-specific data types to conform to SV type sizes (Table 14 in IEC 61850-9-2)
sblair 1:9399d44c2b1a 33 #define CTYPE_BOOLEAN unsigned char
sblair 1:9399d44c2b1a 34 #define CTYPE_INT8 char
sblair 1:9399d44c2b1a 35 #define CTYPE_INT16 short
sblair 1:9399d44c2b1a 36 #define CTYPE_INT32 int
sblair 1:9399d44c2b1a 37 #define CTYPE_INT8U unsigned char
sblair 1:9399d44c2b1a 38 #define CTYPE_INT16U unsigned short
sblair 1:9399d44c2b1a 39 #define CTYPE_INT32U unsigned int
sblair 1:9399d44c2b1a 40 #define CTYPE_ENUM CTYPE_INT32
sblair 1:9399d44c2b1a 41 #define CTYPE_FLOAT32 float
sblair 1:9399d44c2b1a 42 #define CTYPE_FLOAT64 double
sblair 1:9399d44c2b1a 43 #define CTYPE_VISSTRING255 char *
sblair 1:9399d44c2b1a 44 #define CTYPE_DBPOS int
sblair 1:9399d44c2b1a 45 #define CTYPE_QUALITY CTYPE_INT16U
sblair 1:9399d44c2b1a 46 #define CTYPE_TIMESTAMP long long
sblair 0:230c10b228ea 47
sblair 0:230c10b228ea 48 // sampled value dataset type sizes, in bytes
sblair 1:9399d44c2b1a 49 #define SV_GET_LENGTH_FLOAT32 4
sblair 1:9399d44c2b1a 50 #define SV_GET_LENGTH_FLOAT64 8
sblair 1:9399d44c2b1a 51 #define SV_GET_LENGTH_TIMESTAMP 8
sblair 1:9399d44c2b1a 52 #define SV_GET_LENGTH_INT16 2
sblair 1:9399d44c2b1a 53 #define SV_GET_LENGTH_INT32 4
sblair 1:9399d44c2b1a 54 #define SV_GET_LENGTH_INT16U 2
sblair 1:9399d44c2b1a 55 #define SV_GET_LENGTH_INT32U 4
sblair 1:9399d44c2b1a 56 #define SV_GET_LENGTH_VISSTRING255 35
sblair 1:9399d44c2b1a 57 #define SV_GET_LENGTH_BOOLEAN 1
sblair 1:9399d44c2b1a 58 #define SV_GET_LENGTH_ENUM 4
sblair 1:9399d44c2b1a 59 #define SV_GET_LENGTH_QUALITY 4
sblair 1:9399d44c2b1a 60 #define SV_GET_LENGTH_DBPOS 4
sblair 0:230c10b228ea 61
sblair 0:230c10b228ea 62 // BER datatype sizes, which are dependent on the actual data
sblair 1:9399d44c2b1a 63 #define BER_GET_LENGTH_CTYPE_FLOAT32(x) (SV_GET_LENGTH_FLOAT32 + 1) // + 1 byte for number of exponent bits
sblair 1:9399d44c2b1a 64 #define BER_GET_LENGTH_CTYPE_FLOAT64(x) (SV_GET_LENGTH_FLOAT64 + 1) // + 1 byte for number of exponent bits
sblair 1:9399d44c2b1a 65 #define BER_GET_LENGTH_CTYPE_TIMESTAMP(x) (SV_GET_LENGTH_TIMESTAMP)
sblair 1:9399d44c2b1a 66 #define BER_GET_LENGTH_CTYPE_INT16(x) (ber_integer_length((x), SV_GET_LENGTH_INT16))
sblair 1:9399d44c2b1a 67 #define BER_GET_LENGTH_CTYPE_INT32(x) (ber_integer_length((x), SV_GET_LENGTH_INT32))
sblair 1:9399d44c2b1a 68 #define BER_GET_LENGTH_CTYPE_INT16U(x) (ber_integer_length((x), SV_GET_LENGTH_INT16U))
sblair 1:9399d44c2b1a 69 #define BER_GET_LENGTH_CTYPE_INT32U(x) (ber_integer_length((x), SV_GET_LENGTH_INT32U))
sblair 1:9399d44c2b1a 70 #define BER_GET_LENGTH_CTYPE_VISSTRING255(x) (SV_GET_LENGTH_VISSTRING255)
sblair 1:9399d44c2b1a 71 #define BER_GET_LENGTH_CTYPE_BOOLEAN(x) (SV_GET_LENGTH_BOOLEAN)
sblair 1:9399d44c2b1a 72 #define BER_GET_LENGTH_CTYPE_ENUM(x) (ber_integer_length((x), SV_GET_LENGTH_ENUM))
sblair 1:9399d44c2b1a 73 #define BER_GET_LENGTH_CTYPE_QUALITY(x) (/*SV_GET_LENGTH_QUALITY*/2 + 1) // + 1 byte for padding
sblair 1:9399d44c2b1a 74 #define BER_GET_LENGTH_CTYPE_DBPOS(x) (SV_GET_LENGTH_DBPOS)
sblair 0:230c10b228ea 75
sblair 0:230c10b228ea 76
sblair 1:9399d44c2b1a 77 #define ASN1_TAG_ARRAY 0x81
sblair 1:9399d44c2b1a 78 #define ASN1_TAG_STRUCTURE 0x82
sblair 1:9399d44c2b1a 79 #define ASN1_TAG_BOOLEAN 0x83
sblair 1:9399d44c2b1a 80 #define ASN1_TAG_BIT_STRING 0x84
sblair 1:9399d44c2b1a 81 #define ASN1_TAG_INTEGER 0x85
sblair 1:9399d44c2b1a 82 #define ASN1_TAG_UNSIGNED 0x86
sblair 1:9399d44c2b1a 83 #define ASN1_TAG_FLOATING_POINT 0x87
sblair 1:9399d44c2b1a 84 #define ASN1_TAG_REAL 0x88
sblair 1:9399d44c2b1a 85 #define ASN1_TAG_OCTET_STRING 0x89
sblair 1:9399d44c2b1a 86 #define ASN1_TAG_VISIBLE_STRING 0x8A
sblair 0:230c10b228ea 87
sblair 1:9399d44c2b1a 88 #define QUALITY_UNUSED_BITS 3 // total_bits (16) - used_bits (13) = 3
sblair 0:230c10b228ea 89
sblair 0:230c10b228ea 90 int ber_integer_length(void *value, int maxLength);
sblair 0:230c10b228ea 91 int ber_encode_integer(unsigned char *bufDst, void *value, int maxLength);
sblair 0:230c10b228ea 92 void ber_decode_integer(unsigned char *buf, int length, void *value, int maxLength);
sblair 0:230c10b228ea 93
sblair 0:230c10b228ea 94 void reversememcpy(unsigned char *dst, const unsigned char *src, unsigned int len);
sblair 0:230c10b228ea 95 void netmemcpy(void *dst, const void *src, unsigned int len);
sblair 0:230c10b228ea 96 void hostmemcpy(void *dst, const void *src, unsigned int len);
sblair 0:230c10b228ea 97
sblair 0:230c10b228ea 98 void setTimestamp(CTYPE_TIMESTAMP *t);
sblair 0:230c10b228ea 99 void gse_sv_packet_filter(unsigned char *buf, int len);
sblair 0:230c10b228ea 100
sblair 0:230c10b228ea 101 struct UtcTime {
sblair 1:9399d44c2b1a 102 unsigned char bytes[8]; // format: ssssssssssssssssssssssssssssssssffffffffffffffffffffffffqqqqqqqq
sblair 0:230c10b228ea 103 };
sblair 0:230c10b228ea 104
sblair 0:230c10b228ea 105 extern unsigned char LOCAL_MAC_ADDRESS[];
sblair 0:230c10b228ea 106
sblair 0:230c10b228ea 107 struct ethHeaderData {
sblair 1:9399d44c2b1a 108 unsigned char destMACAddress[6]; // stored in big-endian format
sblair 1:9399d44c2b1a 109 CTYPE_INT16U APPID;
sblair 1:9399d44c2b1a 110 CTYPE_INT16U VLAN_ID;
sblair 1:9399d44c2b1a 111 CTYPE_INT16U VLAN_PRIORITY;
sblair 0:230c10b228ea 112 };
sblair 0:230c10b228ea 113
sblair 0:230c10b228ea 114 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
sblair 0:230c10b228ea 115 }
sblair 0:230c10b228ea 116 #endif
sblair 0:230c10b228ea 117
sblair 0:230c10b228ea 118 #endif
sblair 0:230c10b228ea 119