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/
gseDecodePacket.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 "gseDecodeBasic.h" |
sblair | 0:230c10b228ea | 22 | #include "ied.h" |
sblair | 0:230c10b228ea | 23 | #include "gseDecode.h" |
sblair | 0:230c10b228ea | 24 | #include "gsePacketData.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 | unsigned char datasetName[65] = {0}; // maximum length of 65 bytes |
sblair | 0:230c10b228ea | 30 | CTYPE_INT16U datasetNameLength = 0; |
sblair | 0:230c10b228ea | 31 | |
sblair | 0:230c10b228ea | 32 | |
sblair | 0:230c10b228ea | 33 | void gseDecodePDU(unsigned char *buf) { |
sblair | 0:230c10b228ea | 34 | unsigned char tag = (unsigned char) buf[0]; // assumes only one byte is used |
sblair | 0:230c10b228ea | 35 | CTYPE_INT16U lengthFieldSize = getLengthFieldSize((unsigned char) buf[1]); |
sblair | 0:230c10b228ea | 36 | CTYPE_INT16U lengthValue = decodeLength((unsigned char *) &buf[1]); |
sblair | 0:230c10b228ea | 37 | CTYPE_INT16U offsetForSequence = 1 + lengthFieldSize; |
sblair | 0:230c10b228ea | 38 | CTYPE_INT16U offsetForNonSequence = 1 + lengthFieldSize + lengthValue; |
sblair | 0:230c10b228ea | 39 | |
sblair | 0:230c10b228ea | 40 | //printf("tag: %x, lengthFieldSize: %i, lengthValue: %i, offset: %i\n", tag, lengthFieldSize, lengthValue, offsetForNonSequence); |
sblair | 0:230c10b228ea | 41 | |
sblair | 0:230c10b228ea | 42 | switch (tag) { |
sblair | 0:230c10b228ea | 43 | case 0x61: |
sblair | 0:230c10b228ea | 44 | gseDecodePDU(&buf[offsetForSequence]); |
sblair | 0:230c10b228ea | 45 | break; |
sblair | 0:230c10b228ea | 46 | case 0x82: |
sblair | 0:230c10b228ea | 47 | // save dataset name |
sblair | 0:230c10b228ea | 48 | memcpy(datasetName, &buf[offsetForSequence], lengthValue); |
sblair | 0:230c10b228ea | 49 | datasetNameLength = lengthValue; |
sblair | 0:230c10b228ea | 50 | //printf("%s, %d\n", datasetName, datasetNameLength); |
sblair | 0:230c10b228ea | 51 | //fflush(stdout); |
sblair | 0:230c10b228ea | 52 | |
sblair | 0:230c10b228ea | 53 | gseDecodePDU(&buf[offsetForNonSequence]); |
sblair | 0:230c10b228ea | 54 | break; |
sblair | 0:230c10b228ea | 55 | case 0xAB: |
sblair | 0:230c10b228ea | 56 | gseDecodeDataset(&buf[offsetForSequence], lengthValue, datasetName, datasetNameLength); |
sblair | 0:230c10b228ea | 57 | return; |
sblair | 0:230c10b228ea | 58 | //break; |
sblair | 0:230c10b228ea | 59 | default: |
sblair | 0:230c10b228ea | 60 | gseDecodePDU(&buf[offsetForNonSequence]); |
sblair | 0:230c10b228ea | 61 | break; |
sblair | 0:230c10b228ea | 62 | } |
sblair | 0:230c10b228ea | 63 | } |
sblair | 0:230c10b228ea | 64 | |
sblair | 0:230c10b228ea | 65 | void gseDecode(unsigned char *buf, int len) { |
sblair | 0:230c10b228ea | 66 | gseDecodePDU(&buf[26]); // cuts out frame header (fixed size of 26 bytes before start of APDU) |
sblair | 0:230c10b228ea | 67 | } |