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/
gseEncodeBasic.c@1:9399d44c2b1a, 2011-10-07 (annotated)
- Committer:
- sblair
- Date:
- Fri Oct 07 13:48:18 2011 +0000
- Revision:
- 1:9399d44c2b1a
- Parent:
- 0:230c10b228ea
Who changed what in which revision?
User | Revision | Line number | New 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 GSE_BER_ENCODE_BASIC_C |
sblair | 0:230c10b228ea | 22 | #define GSE_BER_ENCODE_BASIC_C |
sblair | 0:230c10b228ea | 23 | |
sblair | 0:230c10b228ea | 24 | #include "ctypes.h" |
sblair | 0:230c10b228ea | 25 | #include "datatypes.h" |
sblair | 0:230c10b228ea | 26 | #include "ied.h" |
sblair | 0:230c10b228ea | 27 | #include "gseEncodeBasic.h" |
sblair | 0:230c10b228ea | 28 | |
sblair | 0:230c10b228ea | 29 | #include <stdlib.h> |
sblair | 0:230c10b228ea | 30 | #include <stdio.h> |
sblair | 0:230c10b228ea | 31 | |
sblair | 0:230c10b228ea | 32 | // BER encoding of basic types |
sblair | 0:230c10b228ea | 33 | int BER_ENCODE_CTYPE_FLOAT32(unsigned char *buf, CTYPE_FLOAT32 *value) { |
sblair | 1:9399d44c2b1a | 34 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 35 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_FLOAT32(value); |
sblair | 0:230c10b228ea | 36 | |
sblair | 1:9399d44c2b1a | 37 | buf[offset++] = ASN1_TAG_FLOATING_POINT; |
sblair | 1:9399d44c2b1a | 38 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 39 | |
sblair | 1:9399d44c2b1a | 40 | buf[offset++] = 0x08; // bits for exponent |
sblair | 1:9399d44c2b1a | 41 | netmemcpy(&buf[offset], value, len - 1); |
sblair | 0:230c10b228ea | 42 | |
sblair | 1:9399d44c2b1a | 43 | return offset + len - 1; |
sblair | 0:230c10b228ea | 44 | } |
sblair | 0:230c10b228ea | 45 | int BER_ENCODE_CTYPE_FLOAT64(unsigned char *buf, CTYPE_FLOAT64 *value) { |
sblair | 1:9399d44c2b1a | 46 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 47 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_FLOAT64(value); |
sblair | 0:230c10b228ea | 48 | |
sblair | 1:9399d44c2b1a | 49 | buf[offset++] = ASN1_TAG_FLOATING_POINT; |
sblair | 1:9399d44c2b1a | 50 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 51 | |
sblair | 1:9399d44c2b1a | 52 | buf[offset++] = 0x0B; // bits for exponent |
sblair | 1:9399d44c2b1a | 53 | netmemcpy(&buf[offset], value, len - 1); |
sblair | 0:230c10b228ea | 54 | |
sblair | 1:9399d44c2b1a | 55 | return offset + len - 1; |
sblair | 0:230c10b228ea | 56 | } |
sblair | 0:230c10b228ea | 57 | int BER_ENCODE_CTYPE_QUALITY(unsigned char *buf, CTYPE_QUALITY *value) { |
sblair | 1:9399d44c2b1a | 58 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 59 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_QUALITY(value); |
sblair | 0:230c10b228ea | 60 | |
sblair | 1:9399d44c2b1a | 61 | buf[offset++] = ASN1_TAG_BIT_STRING; |
sblair | 1:9399d44c2b1a | 62 | offset += encodeLength(&buf[offset], len - 1); |
sblair | 0:230c10b228ea | 63 | |
sblair | 1:9399d44c2b1a | 64 | buf[offset++] = QUALITY_UNUSED_BITS; // number of unused bits |
sblair | 1:9399d44c2b1a | 65 | netmemcpy(&buf[offset], value, len - 1); |
sblair | 0:230c10b228ea | 66 | |
sblair | 1:9399d44c2b1a | 67 | return offset + len - 1; |
sblair | 0:230c10b228ea | 68 | } |
sblair | 0:230c10b228ea | 69 | int BER_ENCODE_CTYPE_TIMESTAMP(unsigned char *buf, CTYPE_TIMESTAMP *value) { |
sblair | 1:9399d44c2b1a | 70 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 71 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_TIMESTAMP(value); |
sblair | 0:230c10b228ea | 72 | |
sblair | 1:9399d44c2b1a | 73 | buf[offset++] = ASN1_TAG_OCTET_STRING; |
sblair | 1:9399d44c2b1a | 74 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 75 | |
sblair | 1:9399d44c2b1a | 76 | memcpy(&buf[offset], value, len); //TODO should be memcpy, because already in big-endian? |
sblair | 0:230c10b228ea | 77 | |
sblair | 1:9399d44c2b1a | 78 | return offset + len; |
sblair | 0:230c10b228ea | 79 | } |
sblair | 1:9399d44c2b1a | 80 | int BER_ENCODE_CTYPE_ENUM(unsigned char *buf, CTYPE_ENUM *value) { // assuming enum is an int - allows any enum type to be used |
sblair | 1:9399d44c2b1a | 81 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 82 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_ENUM(value); |
sblair | 0:230c10b228ea | 83 | |
sblair | 1:9399d44c2b1a | 84 | buf[offset++] = ASN1_TAG_INTEGER; |
sblair | 1:9399d44c2b1a | 85 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 86 | |
sblair | 1:9399d44c2b1a | 87 | ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT32U); |
sblair | 1:9399d44c2b1a | 88 | //netmemcpy(&buf[offset], value, len); |
sblair | 0:230c10b228ea | 89 | |
sblair | 1:9399d44c2b1a | 90 | return offset + len; |
sblair | 0:230c10b228ea | 91 | } |
sblair | 0:230c10b228ea | 92 | int BER_ENCODE_CTYPE_INT16(unsigned char *buf, CTYPE_INT16 *value) { |
sblair | 1:9399d44c2b1a | 93 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 94 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT16(value); |
sblair | 0:230c10b228ea | 95 | |
sblair | 1:9399d44c2b1a | 96 | buf[offset++] = ASN1_TAG_INTEGER; |
sblair | 1:9399d44c2b1a | 97 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 98 | |
sblair | 1:9399d44c2b1a | 99 | ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT16); |
sblair | 1:9399d44c2b1a | 100 | //netmemcpy(&buf[offset], value, len); |
sblair | 0:230c10b228ea | 101 | |
sblair | 1:9399d44c2b1a | 102 | return offset + len; |
sblair | 0:230c10b228ea | 103 | } |
sblair | 0:230c10b228ea | 104 | int BER_ENCODE_CTYPE_INT32(unsigned char *buf, CTYPE_INT32 *value) { |
sblair | 1:9399d44c2b1a | 105 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 106 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT32(value); |
sblair | 0:230c10b228ea | 107 | |
sblair | 1:9399d44c2b1a | 108 | buf[offset++] = ASN1_TAG_INTEGER; |
sblair | 1:9399d44c2b1a | 109 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 110 | |
sblair | 1:9399d44c2b1a | 111 | printf("INT32 value: %u, len: %i, total: %i\n", *value, len, offset + len); |
sblair | 1:9399d44c2b1a | 112 | fflush(stdout); |
sblair | 1:9399d44c2b1a | 113 | ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT32); |
sblair | 1:9399d44c2b1a | 114 | //netmemcpy(&buf[offset], value, len); |
sblair | 0:230c10b228ea | 115 | |
sblair | 1:9399d44c2b1a | 116 | return offset + len; |
sblair | 0:230c10b228ea | 117 | } |
sblair | 0:230c10b228ea | 118 | int BER_ENCODE_CTYPE_INT16U(unsigned char *buf, CTYPE_INT16U *value) { |
sblair | 1:9399d44c2b1a | 119 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 120 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT16U(value); |
sblair | 0:230c10b228ea | 121 | |
sblair | 1:9399d44c2b1a | 122 | buf[offset++] = ASN1_TAG_UNSIGNED; |
sblair | 1:9399d44c2b1a | 123 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 124 | |
sblair | 1:9399d44c2b1a | 125 | printf("INT16U value: %u, len: %i, total: %i\n", *value, len, offset + len); |
sblair | 1:9399d44c2b1a | 126 | fflush(stdout); |
sblair | 1:9399d44c2b1a | 127 | ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT16U); |
sblair | 1:9399d44c2b1a | 128 | //netmemcpy(&buf[offset], value, len); |
sblair | 0:230c10b228ea | 129 | |
sblair | 1:9399d44c2b1a | 130 | return offset + len; |
sblair | 0:230c10b228ea | 131 | } |
sblair | 0:230c10b228ea | 132 | int BER_ENCODE_CTYPE_INT32U(unsigned char *buf, CTYPE_INT32U *value) { |
sblair | 1:9399d44c2b1a | 133 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 134 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT32U(value); |
sblair | 0:230c10b228ea | 135 | |
sblair | 1:9399d44c2b1a | 136 | buf[offset++] = ASN1_TAG_UNSIGNED; |
sblair | 1:9399d44c2b1a | 137 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 138 | |
sblair | 1:9399d44c2b1a | 139 | ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT32U); |
sblair | 0:230c10b228ea | 140 | |
sblair | 1:9399d44c2b1a | 141 | printf("INT32U value: %u, len: %i, total: %i\n", *value, len, offset + len); |
sblair | 1:9399d44c2b1a | 142 | fflush(stdout); |
sblair | 1:9399d44c2b1a | 143 | //netmemcpy(&buf[offset], value, len); |
sblair | 0:230c10b228ea | 144 | |
sblair | 1:9399d44c2b1a | 145 | return offset + len; |
sblair | 0:230c10b228ea | 146 | } |
sblair | 0:230c10b228ea | 147 | int BER_ENCODE_CTYPE_VISSTRING255(unsigned char *buf, CTYPE_VISSTRING255 *value) { |
sblair | 1:9399d44c2b1a | 148 | netmemcpy(buf, value, BER_GET_LENGTH_CTYPE_VISSTRING255(value)); |
sblair | 0:230c10b228ea | 149 | |
sblair | 1:9399d44c2b1a | 150 | return BER_GET_LENGTH_CTYPE_VISSTRING255(value); |
sblair | 0:230c10b228ea | 151 | } |
sblair | 0:230c10b228ea | 152 | int BER_ENCODE_CTYPE_BOOLEAN(unsigned char *buf, CTYPE_BOOLEAN *value) { |
sblair | 1:9399d44c2b1a | 153 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 154 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_BOOLEAN(value); |
sblair | 0:230c10b228ea | 155 | |
sblair | 1:9399d44c2b1a | 156 | buf[offset++] = ASN1_TAG_BOOLEAN; |
sblair | 1:9399d44c2b1a | 157 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 158 | |
sblair | 1:9399d44c2b1a | 159 | netmemcpy(&buf[offset], value, len); |
sblair | 0:230c10b228ea | 160 | |
sblair | 1:9399d44c2b1a | 161 | return offset + len; |
sblair | 0:230c10b228ea | 162 | } |
sblair | 0:230c10b228ea | 163 | int BER_ENCODE_CTYPE_DBPOS(unsigned char *buf, CTYPE_DBPOS *value) { |
sblair | 1:9399d44c2b1a | 164 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 165 | CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_DBPOS(value); |
sblair | 0:230c10b228ea | 166 | |
sblair | 1:9399d44c2b1a | 167 | buf[offset++] = 0x85; |
sblair | 1:9399d44c2b1a | 168 | offset += encodeLength(&buf[offset], len); |
sblair | 0:230c10b228ea | 169 | |
sblair | 1:9399d44c2b1a | 170 | netmemcpy(&buf[offset], value, len); //TODO should be memcpy, because already in big-endian? |
sblair | 0:230c10b228ea | 171 | |
sblair | 1:9399d44c2b1a | 172 | return offset + len; |
sblair | 0:230c10b228ea | 173 | } |
sblair | 0:230c10b228ea | 174 | |
sblair | 0:230c10b228ea | 175 | #endif |