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/
svDecodePacket.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 "svDecodeBasic.h" |
sblair | 0:230c10b228ea | 22 | #include "ied.h" |
sblair | 0:230c10b228ea | 23 | #include "svDecode.h" |
sblair | 0:230c10b228ea | 24 | #include "svPacketData.h" |
sblair | 0:230c10b228ea | 25 | #include "decodePacket.h" |
sblair | 0:230c10b228ea | 26 | #include <stddef.h> |
sblair | 0:230c10b228ea | 27 | |
sblair | 0:230c10b228ea | 28 | |
sblair | 0:230c10b228ea | 29 | void svDecodeASDU(unsigned char *buf, int len, int noASDU) { |
sblair | 1:9399d44c2b1a | 30 | unsigned char tag; // assumes only one byte is used |
sblair | 1:9399d44c2b1a | 31 | int lengthFieldSize; |
sblair | 1:9399d44c2b1a | 32 | int lengthValue; |
sblair | 1:9399d44c2b1a | 33 | int offsetForNonSequence; |
sblair | 1:9399d44c2b1a | 34 | unsigned char *svID = NULL; |
sblair | 1:9399d44c2b1a | 35 | int svIDLength = 0; |
sblair | 0:230c10b228ea | 36 | |
sblair | 1:9399d44c2b1a | 37 | int i = 0; |
sblair | 1:9399d44c2b1a | 38 | for (i = 0; i < len;) { |
sblair | 1:9399d44c2b1a | 39 | tag = (unsigned char) buf[i]; |
sblair | 1:9399d44c2b1a | 40 | lengthFieldSize = getLengthFieldSize((unsigned char) buf[i + 1]); |
sblair | 1:9399d44c2b1a | 41 | lengthValue = decodeLength((unsigned char *) &buf[i + 1]); |
sblair | 1:9399d44c2b1a | 42 | offsetForNonSequence = 1 + lengthFieldSize + lengthValue; |
sblair | 0:230c10b228ea | 43 | |
sblair | 1:9399d44c2b1a | 44 | //printf("\ttag: %x, noASDU: %u, lengthFieldSize: %i, lengthValue: %i, offset: %i\n", tag, noASDU, lengthFieldSize, lengthValue, offsetForNonSequence); |
sblair | 0:230c10b228ea | 45 | |
sblair | 1:9399d44c2b1a | 46 | switch (tag) { |
sblair | 1:9399d44c2b1a | 47 | case 0x80: |
sblair | 1:9399d44c2b1a | 48 | svID = &buf[i + 1 + lengthFieldSize]; |
sblair | 1:9399d44c2b1a | 49 | svIDLength = lengthValue; |
sblair | 1:9399d44c2b1a | 50 | break; |
sblair | 1:9399d44c2b1a | 51 | case 0x81: |
sblair | 0:230c10b228ea | 52 | |
sblair | 1:9399d44c2b1a | 53 | break; |
sblair | 1:9399d44c2b1a | 54 | case 0x82: |
sblair | 1:9399d44c2b1a | 55 | // TODO: may be useful to store smpCnt value |
sblair | 1:9399d44c2b1a | 56 | break; |
sblair | 1:9399d44c2b1a | 57 | case 0x83: |
sblair | 0:230c10b228ea | 58 | |
sblair | 1:9399d44c2b1a | 59 | break; |
sblair | 1:9399d44c2b1a | 60 | case 0x84: |
sblair | 0:230c10b228ea | 61 | |
sblair | 1:9399d44c2b1a | 62 | break; |
sblair | 1:9399d44c2b1a | 63 | case 0x85: |
sblair | 0:230c10b228ea | 64 | |
sblair | 1:9399d44c2b1a | 65 | break; |
sblair | 1:9399d44c2b1a | 66 | case 0x86: |
sblair | 0:230c10b228ea | 67 | |
sblair | 1:9399d44c2b1a | 68 | break; |
sblair | 1:9399d44c2b1a | 69 | case 0x87: |
sblair | 1:9399d44c2b1a | 70 | if (svID != NULL) { |
sblair | 1:9399d44c2b1a | 71 | svDecodeDataset(&buf[i + 1 + lengthFieldSize], lengthValue, noASDU, svID, svIDLength); |
sblair | 1:9399d44c2b1a | 72 | } |
sblair | 1:9399d44c2b1a | 73 | break; |
sblair | 1:9399d44c2b1a | 74 | default: |
sblair | 1:9399d44c2b1a | 75 | break; |
sblair | 1:9399d44c2b1a | 76 | } |
sblair | 0:230c10b228ea | 77 | |
sblair | 1:9399d44c2b1a | 78 | i += offsetForNonSequence; |
sblair | 1:9399d44c2b1a | 79 | } |
sblair | 0:230c10b228ea | 80 | } |
sblair | 0:230c10b228ea | 81 | |
sblair | 0:230c10b228ea | 82 | void svDecodeAPDU(unsigned char *buf, int len) { |
sblair | 1:9399d44c2b1a | 83 | unsigned char tag = (unsigned char) buf[0]; // assumes only one byte is used |
sblair | 1:9399d44c2b1a | 84 | int lengthFieldSize = getLengthFieldSize((unsigned char) buf[1]); |
sblair | 1:9399d44c2b1a | 85 | int lengthValue = decodeLength((unsigned char *) &buf[1]); |
sblair | 1:9399d44c2b1a | 86 | int offsetForSequence = 1 + lengthFieldSize; |
sblair | 1:9399d44c2b1a | 87 | int offsetForNonSequence = 1 + lengthFieldSize + lengthValue; |
sblair | 1:9399d44c2b1a | 88 | //static unsigned int noASDU = 0; |
sblair | 1:9399d44c2b1a | 89 | static unsigned int ASDU = 0; |
sblair | 0:230c10b228ea | 90 | |
sblair | 1:9399d44c2b1a | 91 | //printf("tag: %x, noASDU: %u, lengthFieldSize: %i, lengthValue: %i, offset: %i\n", tag, noASDU, lengthFieldSize, lengthValue, offsetForNonSequence); |
sblair | 0:230c10b228ea | 92 | |
sblair | 1:9399d44c2b1a | 93 | switch (tag) { |
sblair | 1:9399d44c2b1a | 94 | case 0x60: |
sblair | 1:9399d44c2b1a | 95 | svDecodeAPDU(&buf[offsetForSequence], lengthValue); |
sblair | 1:9399d44c2b1a | 96 | break; |
sblair | 1:9399d44c2b1a | 97 | case 0x80: |
sblair | 1:9399d44c2b1a | 98 | //noASDU = (unsigned int) buf[1 + lengthFieldSize]; // assuming noASDU is < 126 |
sblair | 1:9399d44c2b1a | 99 | ASDU = 0; |
sblair | 1:9399d44c2b1a | 100 | svDecodeAPDU(&buf[offsetForNonSequence], len - offsetForNonSequence); |
sblair | 1:9399d44c2b1a | 101 | break; |
sblair | 1:9399d44c2b1a | 102 | case 0xA2: |
sblair | 1:9399d44c2b1a | 103 | svDecodeAPDU(&buf[offsetForSequence], lengthValue); |
sblair | 1:9399d44c2b1a | 104 | break; |
sblair | 1:9399d44c2b1a | 105 | case 0x30: |
sblair | 1:9399d44c2b1a | 106 | svDecodeASDU(&buf[offsetForSequence], lengthValue, ASDU++); |
sblair | 0:230c10b228ea | 107 | |
sblair | 1:9399d44c2b1a | 108 | svDecodeAPDU(&buf[offsetForNonSequence], len - offsetForNonSequence); |
sblair | 1:9399d44c2b1a | 109 | break; |
sblair | 1:9399d44c2b1a | 110 | default: |
sblair | 1:9399d44c2b1a | 111 | break; |
sblair | 1:9399d44c2b1a | 112 | } |
sblair | 0:230c10b228ea | 113 | } |
sblair | 0:230c10b228ea | 114 | |
sblair | 0:230c10b228ea | 115 | void svDecode(unsigned char *buf, int len) { |
sblair | 1:9399d44c2b1a | 116 | unsigned short APDULength = ((buf[21] << 8) | buf[22]) - 8; // must use length in PDU because total bytes (len) may contain CRC |
sblair | 0:230c10b228ea | 117 | |
sblair | 1:9399d44c2b1a | 118 | svDecodeAPDU(&buf[26], APDULength); // cuts out frame header (fixed size of 26 bytes before start of APDU) |
sblair | 0:230c10b228ea | 119 | } |