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/
gseDecodeBasic.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 | #include "ctypes.h" |
sblair | 0:230c10b228ea | 22 | #include "datatypes.h" |
sblair | 0:230c10b228ea | 23 | #include "ied.h" |
sblair | 0:230c10b228ea | 24 | #include "gseDecodeBasic.h" |
sblair | 0:230c10b228ea | 25 | #include <string.h> |
sblair | 0:230c10b228ea | 26 | |
sblair | 0:230c10b228ea | 27 | // GSE decoding of basic types |
sblair | 0:230c10b228ea | 28 | int BER_DECODE_CTYPE_FLOAT32(unsigned char *buf, CTYPE_FLOAT32 *value) { |
sblair | 1:9399d44c2b1a | 29 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 30 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 31 | |
sblair | 1:9399d44c2b1a | 32 | if (buf[offset++] == 0x87) { |
sblair | 1:9399d44c2b1a | 33 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 34 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 35 | |
sblair | 1:9399d44c2b1a | 36 | // check for 8 bits for exponent |
sblair | 1:9399d44c2b1a | 37 | if (buf[offset++] == 0x08) { |
sblair | 1:9399d44c2b1a | 38 | netmemcpy(value, &buf[offset], len - 1); |
sblair | 1:9399d44c2b1a | 39 | } |
sblair | 1:9399d44c2b1a | 40 | } |
sblair | 0:230c10b228ea | 41 | |
sblair | 1:9399d44c2b1a | 42 | return offset + len - 1; |
sblair | 0:230c10b228ea | 43 | } |
sblair | 0:230c10b228ea | 44 | |
sblair | 0:230c10b228ea | 45 | // GSE decoding of basic types |
sblair | 0:230c10b228ea | 46 | int BER_DECODE_CTYPE_FLOAT64(unsigned char *buf, CTYPE_FLOAT64 *value) { |
sblair | 1:9399d44c2b1a | 47 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 48 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 49 | |
sblair | 1:9399d44c2b1a | 50 | if (buf[offset++] == 0x87) { |
sblair | 1:9399d44c2b1a | 51 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 52 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 53 | |
sblair | 1:9399d44c2b1a | 54 | // check for 11 bits for exponent |
sblair | 1:9399d44c2b1a | 55 | if (buf[offset++] == 0x0B) { |
sblair | 1:9399d44c2b1a | 56 | netmemcpy(value, &buf[offset], len - 1); |
sblair | 1:9399d44c2b1a | 57 | } |
sblair | 1:9399d44c2b1a | 58 | } |
sblair | 0:230c10b228ea | 59 | |
sblair | 1:9399d44c2b1a | 60 | return offset + len - 1; |
sblair | 0:230c10b228ea | 61 | } |
sblair | 0:230c10b228ea | 62 | int BER_DECODE_CTYPE_QUALITY(unsigned char *buf, CTYPE_QUALITY *value) { |
sblair | 1:9399d44c2b1a | 63 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 64 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 65 | |
sblair | 1:9399d44c2b1a | 66 | if (buf[offset++] == 0x85) { |
sblair | 1:9399d44c2b1a | 67 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 68 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 69 | |
sblair | 1:9399d44c2b1a | 70 | netmemcpy(value, buf, len); //TODO check if memcpy should be used here, and elsewhere |
sblair | 1:9399d44c2b1a | 71 | } |
sblair | 0:230c10b228ea | 72 | |
sblair | 1:9399d44c2b1a | 73 | return offset + len; |
sblair | 0:230c10b228ea | 74 | } |
sblair | 0:230c10b228ea | 75 | int BER_DECODE_CTYPE_TIMESTAMP(unsigned char *buf, CTYPE_TIMESTAMP *value) { |
sblair | 1:9399d44c2b1a | 76 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 77 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 78 | |
sblair | 1:9399d44c2b1a | 79 | if (buf[offset++] == 0x89) { |
sblair | 1:9399d44c2b1a | 80 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 81 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 82 | |
sblair | 1:9399d44c2b1a | 83 | netmemcpy(value, buf, len); |
sblair | 1:9399d44c2b1a | 84 | } |
sblair | 0:230c10b228ea | 85 | |
sblair | 1:9399d44c2b1a | 86 | return offset + len; |
sblair | 0:230c10b228ea | 87 | } |
sblair | 1:9399d44c2b1a | 88 | int BER_DECODE_CTYPE_ENUM(unsigned char *buf, CTYPE_ENUM *value) { // assuming enum is an int - allows any enum type to be used |
sblair | 1:9399d44c2b1a | 89 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 90 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 91 | |
sblair | 1:9399d44c2b1a | 92 | if (buf[offset++] == ASN1_TAG_UNSIGNED) { |
sblair | 1:9399d44c2b1a | 93 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 94 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 95 | |
sblair | 1:9399d44c2b1a | 96 | ber_decode_integer(&buf[offset], len, value, SV_GET_LENGTH_INT32U); |
sblair | 1:9399d44c2b1a | 97 | } |
sblair | 0:230c10b228ea | 98 | |
sblair | 1:9399d44c2b1a | 99 | return offset + len; |
sblair | 0:230c10b228ea | 100 | } |
sblair | 0:230c10b228ea | 101 | int BER_DECODE_CTYPE_INT16(unsigned char *buf, CTYPE_INT16 *value) { |
sblair | 1:9399d44c2b1a | 102 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 103 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 104 | |
sblair | 1:9399d44c2b1a | 105 | if (buf[offset++] == ASN1_TAG_INTEGER) { |
sblair | 1:9399d44c2b1a | 106 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 107 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 108 | |
sblair | 1:9399d44c2b1a | 109 | ber_decode_integer(&buf[offset], len, value, SV_GET_LENGTH_INT16); |
sblair | 1:9399d44c2b1a | 110 | } |
sblair | 0:230c10b228ea | 111 | |
sblair | 1:9399d44c2b1a | 112 | return offset + len; |
sblair | 0:230c10b228ea | 113 | } |
sblair | 0:230c10b228ea | 114 | int BER_DECODE_CTYPE_INT32(unsigned char *buf, CTYPE_INT32 *value) { |
sblair | 1:9399d44c2b1a | 115 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 116 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 117 | |
sblair | 1:9399d44c2b1a | 118 | if (buf[offset++] == ASN1_TAG_INTEGER) { |
sblair | 1:9399d44c2b1a | 119 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 120 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 121 | |
sblair | 1:9399d44c2b1a | 122 | ber_decode_integer(&buf[offset], len, value, SV_GET_LENGTH_INT32); |
sblair | 1:9399d44c2b1a | 123 | } |
sblair | 0:230c10b228ea | 124 | |
sblair | 1:9399d44c2b1a | 125 | return offset + len; |
sblair | 0:230c10b228ea | 126 | } |
sblair | 0:230c10b228ea | 127 | int BER_DECODE_CTYPE_INT16U(unsigned char *buf, CTYPE_INT16U *value) { |
sblair | 1:9399d44c2b1a | 128 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 129 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 130 | |
sblair | 1:9399d44c2b1a | 131 | if (buf[offset++] == ASN1_TAG_UNSIGNED) { |
sblair | 1:9399d44c2b1a | 132 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 133 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 134 | |
sblair | 1:9399d44c2b1a | 135 | ber_decode_integer(&buf[offset], len, value, SV_GET_LENGTH_INT16U); |
sblair | 1:9399d44c2b1a | 136 | } |
sblair | 0:230c10b228ea | 137 | |
sblair | 1:9399d44c2b1a | 138 | return offset + len; |
sblair | 0:230c10b228ea | 139 | } |
sblair | 0:230c10b228ea | 140 | int BER_DECODE_CTYPE_INT32U(unsigned char *buf, CTYPE_INT32U *value) { |
sblair | 1:9399d44c2b1a | 141 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 142 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 143 | |
sblair | 1:9399d44c2b1a | 144 | if (buf[offset++] == ASN1_TAG_UNSIGNED) { |
sblair | 1:9399d44c2b1a | 145 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 146 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 147 | |
sblair | 1:9399d44c2b1a | 148 | ber_decode_integer(&buf[offset], len, value, SV_GET_LENGTH_INT32U); |
sblair | 1:9399d44c2b1a | 149 | } |
sblair | 0:230c10b228ea | 150 | |
sblair | 1:9399d44c2b1a | 151 | return offset + len; |
sblair | 0:230c10b228ea | 152 | } |
sblair | 0:230c10b228ea | 153 | int BER_DECODE_CTYPE_VISSTRING255(unsigned char *buf, CTYPE_VISSTRING255 *value) { |
sblair | 1:9399d44c2b1a | 154 | netmemcpy(value, buf, SV_GET_LENGTH_VISSTRING255); |
sblair | 0:230c10b228ea | 155 | |
sblair | 1:9399d44c2b1a | 156 | return SV_GET_LENGTH_VISSTRING255; |
sblair | 0:230c10b228ea | 157 | } |
sblair | 0:230c10b228ea | 158 | int BER_DECODE_CTYPE_BOOLEAN(unsigned char *buf, CTYPE_BOOLEAN *value) { |
sblair | 1:9399d44c2b1a | 159 | CTYPE_INT16U offset = 0; |
sblair | 1:9399d44c2b1a | 160 | CTYPE_INT16U len = 0; |
sblair | 0:230c10b228ea | 161 | |
sblair | 1:9399d44c2b1a | 162 | if (buf[offset++] == ASN1_TAG_BOOLEAN) { |
sblair | 1:9399d44c2b1a | 163 | len += decodeLength(&buf[offset]); |
sblair | 1:9399d44c2b1a | 164 | offset += getLengthFieldSize(buf[offset]); |
sblair | 0:230c10b228ea | 165 | |
sblair | 1:9399d44c2b1a | 166 | netmemcpy(value, buf, len); |
sblair | 1:9399d44c2b1a | 167 | } |
sblair | 0:230c10b228ea | 168 | |
sblair | 1:9399d44c2b1a | 169 | return offset + len; |
sblair | 0:230c10b228ea | 170 | } |
sblair | 0:230c10b228ea | 171 | int BER_DECODE_CTYPE_DBPOS(unsigned char *buf, CTYPE_DBPOS *value) { |
sblair | 1:9399d44c2b1a | 172 | netmemcpy(value, buf, SV_GET_LENGTH_DBPOS); |
sblair | 0:230c10b228ea | 173 | |
sblair | 1:9399d44c2b1a | 174 | return SV_GET_LENGTH_DBPOS; |
sblair | 0:230c10b228ea | 175 | } |