Rapid-prototyping protection schemes with IEC 61850

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 and the Raspberry Pi. It's ideal for rapid-prototyping new power system protection, control, and automation systems that require communications.

This readme file describes how to set up the software, and its basic use.

The code is meant to be a proof of concept, and some parts are experimental. It has not been tested on many SCD files. Some features may be incomplete.

Features

You can read more about the motivation and benefits of the project here.

Installation

This process has been tested on Windows and Ubuntu, but other Linux flavours and OS X should work too. Most steps only need to be completed once.

The software requires Eclipse, with the Eclipse Modeling Framework (EMF). Java Emitter Templates (JET) is needed for development, but not to run the code. It's easiest to start with the version of Eclipse that comes with the Modeling Tools bundle (see here: http://www.eclipse.org/downloads/). (If you are planning on using the Python or Java interfaces on Windows, it is best to use the 32-bit versions of Eclipse, and the JDK.)

There are two source code trees: emf (in Java), and c (obviously written in C). Each should be a separate project in Eclipse. The Java emf project directory is organised as follows:

EMF import process

  1. Start Eclipse, with the Workspace set to the root of the repository directory, e.g., /home/user/rapid61850 on Linux.
  2. Create an "EMF Project" called "emf", at the location of the repository code.
  3. Select "XML Schema" as the Model Importer type. Select all the IEC 61850 XML Schema documents in the emf/model directory.
  4. Select the three root packages that are imported (although, only scl is used). Click "Finish". This will re-generate some files in emf/model: scl.ecore, lcoordinates.ecore, lmaintenance.ecore, and SCL.genmodel.
  5. Create a new project of type "Convert Projects to JET Projects", and select the emf project. For the emf project, go to Project Properties > JET Settings, and set Template Containers to "templates", and Source Container to "src". You can delete the rapid61850/emf/rapid61850/templates directory (but not the rapid61850/emf/templates or rapid61850/emf/src/rapid61850/templates directories) that was created before JET was configured correctly.
  6. Open SCL.genmodel and right-click on the root of the model tree. Select "Show Properties View" and ensure that "Compliance Level" is set to "6.0". Right-click on the root again and select "Generate Model Code". This should re-generate the model implementation files (in the emf/src/ch directory), and set up the project properly for using the generated code.
  7. Two additional JAR libraries must be included for the project to compile. In the Project Properties for emf, go to Java Build Path > Libraries. Click on "Add External JARs..." and find com.ibm.icu_4.4.2.v20110823.jar and org.eclipse.emf.query_1.2.100.v200903190031.jar (or similar versions). These should be located in the "plugins" directory within the Eclipse installation.

C code project example

An example SCD file and a main.c file are provided. Many of the other C files are generated automatically. For the C code to compile with Eclipse, you should:

Using the code with a new SCD file

First, open the file Main.java. In the Main class, set the value of SCD_FILENAME to the filename of the SCD file. The SCD file should be in the same directory as the Main.java file. Run the Java project to generate the C implementation. If the SCD parser complains, ensure that the first two lines of the SCD file exactly match those from the example scd.xml in the repository. It's usually best to refresh the C project in Eclipse, to ensure that Eclipse knows about the new or modified files.

A basic C main() function will look something like:

#include "iec61850.h"

int length = 0;
unsigned char buffer[2048] = {0};

int main() {
    initialise_iec61850();                                         // initialise all data structures

    // send GOOSE packet
    E1Q1SB1.S1.C1.TVTRa_1.Vol.instMag.f = 1.024;                   // set a value that appears in the dataset used by the "ItlPositions" GOOSE Control
    length = E1Q1SB1.S1.C1.LN0.ItlPositions.send(buffer, 1, 512);  // generate a goose packet, and store the bytes in "buffer"
    send_ethernet_packet(buffer, length);                          // platform-specific call to send an Ethernet packet


    // in another IED...


    // receive GOOSE or SV packet
    length = recv_ethernet_packet(buffer);                         // platform-specific call to receive an Ethernet packet
    gse_sv_packet_filter(buffer, length);                          // deals with any GOOSE or SV dataset that is able to be processed

    // read value that was updated by the packet (it will equal 1.024)
    float inputValue = D1Q1SB4.S1.C1.RSYNa_1.gse_inputs_ItlPositions.E1Q1SB1_C1_Positions.C1_TVTR_1_Vol_instMag.f;

    return 0;
}

The data structures used for generating GOOSE and SV packets are stored within LN0. GOOSE packets are generated by calling the appropriate send(buffer, statusChange, timeAllowedToLive) function, where statusChange should be 1 if any value in the dataset has changed, and where timeAllowedToLive is the time in milliseconds for the receiver to wait for the next re-transmission. SV packets are sent by calling the update(buffer) function, which returns 0 if the next ASDU was written, but other ASDUs are free. It returns the size of the packet when all ASDUs have been written (and buffer contains the packet data). Clearly, a real implementation might include the use of platform-specific timers, interrupts and callbacks, where needed.

The generated C code implements all IEDs specified in the SCD file. You can use the code to emulate the communications between several IEDs, or just use one IED's implementation.

Set the local MAC address

In ctypes.h, set LOCAL_MAC_ADDRESS_VALUE to the local network interface's MAC address. This must be done for each physical device.

Callbacks after a dataset is decoded

Callbacks should be set up in the form:

void SVcallbackFunction(CTYPE_INT16U smpCnt) {
    ;
}

void GSEcallbackFunction(CTYPE_INT32U timeAllowedToLive, CTYPE_TIMESTAMP T, CTYPE_INT32U stNum, CTYPE_INT32U sqNum) {
    ;
}

//...

D1Q1SB4.S1.C1.exampleMMXU_1.sv_inputs_rmxuCB.datasetDecodeDone = &SVcallbackFunction;
D1Q1SB4.S1.C1.RSYNa_1.gse_inputs_ItlPositions.datasetDecodeDone = &GSEcallbackFunction;

where D1Q1SB4.S1.C1.exampleMMXU_1 is a Logical Node defined in datatypes.h (and ied.h). rmxuCB is the name of the SampledValueControl, in a different IED, which sent the SV packets. After being initialised, the callback function will be executed after this dataset is successfully decoded, to allow the LN to deal with the new data. For example, by default, only one packet of data is saved for each GSE or SV Control - and is overwritten when a new packet arrives. Therefore, it may be useful to use the callback to log the data to a separate memory buffer.

Fixed-length GOOSE encoding

To enable fixed-length GOOSE encoding, in ctypes.h set the value of GOOSE_FIXED_SIZE to 1. Otherwise, it should have a value of 0. This can only be enabled globally for all GOOSE encoding, rather than on a per Control basis.

Platform-specific options

All platform-specific options can be edited in ctypes.h or ctypes.c. For example, for a big endian platform, change:

#define LITTLE_ENDIAN       1

to:

#define LITTLE_ENDIAN       0

All CTYPE_* definitions must map to local datatypes of the correct size and sign.

In ctypes.c, the basic library function memcpy() is used to copy bytes in order (according to platform endianness), and reversememcpy() copies the bytes of multi-byte data types in reverse order (for converting between endianness). Although these will work, they can be replaced with platform-specific alternatives for better performance.

The value of TIMESTAMP_SUPPORTED should be set to 0, unless generating timestamps has been implemented for your platform. An implementation for Windows has been included by default.

Real-time compression of Sampled Values data

This library can be used to compress Sampled Values data, in real-time, to significantly reduce bandwidth requirements and reduce overall latency. To avoid affecting the master branch for this project, the code is available in the compress branch (which will eventually be merged into the main code branch).

The example provided uses the -9-2LE dataset format, and is aimed at relatively high sampling rates (using the recommendations in IEC 61869-9). The code is based on the LEx2_compression.scd SCD file along with a manual implementation of the compression encoding and decoding functions in compress.c. A main C file main_SV_compression.c ca be used to test the method, along with other open source compression algorthims for comparison (these other algorithms are not specifically designed to operate SV datasets and are therefore not feasible for real-time applications).

You can read more about the method and typical results in this paper: http://strathprints.strath.ac.uk/57710/1/Blair_etal_AMPS2016_Real_time_compression_of_IEC_61869_9_sampled_value_data.pdf

Using the HTTP and JSON interface

This functionality is experimental. Some data types and ACSI services have not been fully tested yet. Support for Windows and Linux has been verified; OS X should work too. The JSON interface can run on an embedded (i.e., non-POSIX) platform, but an alternative web server is necessary.

An "index" of the data model provided by rapid61850 is generated automatically. This fully exposes the data model, including all metadata (such as data types and functional constraints), at run-time. A JavaScript object notation (JSON) data format, with an interface which is exposed via HTTP (or HTTPS), has been provided for implementing the IEC 61850-7-2 abstract communication service interface (ACSI).

Mongoose, which is embedded in the repository and has an open source GPL 2 license, provides a simple and lightweight web server. As with the rest of rapid61850, the JSON interface "implements" all IEDs specified in the SCD file. By default, a new thread is spawned for each IED; this allows multiple IEDs to be tested together from a single application. (Note: no locking has been implemented for the data model, but different IEDs should not modify each other's data directly.) As well as the HTTP server for each IED, there is a basic facility for an HTTP client, for IEDs to perform GET and POST operations on other IEDs - whether local or remote.

API details

There are examples of how to use each command from C code in main_json.c. JSON prettification (formatting with whitespace) can be enabled at compile-time.

Either . or / can be used to separate items in the object reference, but the separator between the Logical Device and the object reference must be /. All URLs are case-sensitive.

Associate

Explicitly creates an ACSI association, using the IP address and port of the requester as the ID. ACSI_AUTO_ASSOCIATE can be set to 1 to automatically associate any client that requests data. Alternatively, this command can be completely ignored for convenience. If using HTTP authentication (with SSL), the username and password act as the authentication parameters for the association.

HTTP GET with: /associate

Release

Releases an ACSI association.

HTTP GET with: /release

Abort

Aborts an ACSI association.

HTTP GET with: /abort

Get value

Returns the value of the specified element and all sub-elements, if applicable. The is effectively the default response for the server; if no Logical Device is specified, then all data for the entire IED will be returned.

HTTP GET with: /<LD>/<ObjectRef>


Example: GET http://localhost:8001/C1/LN0.NamPlt.configRev

Returns: {"configRev":"Rev 3.45"}


Example: GET http://localhost:8001/C1/exampleRMXU_1.AmpLocPhsB

Returns: {"AmpLocPhsB":{"instMag":{"f":1.024000},"q":0}}

Get definition

Returns the data definition of the specified element.

HTTP GET with: /definition/<LD>/<ObjectRef>


Example: GET http://localhost:8001/definition/C1/LN0.NamPlt.configRev

Returns: {"name":"configRev","type":"VisString255","FC":"DC"}


Example: GET http://localhost:8001/definition/C1/exampleRMXU_1.AmpLocPhsB

Returns: {"name":"AmpLocPhsB","type":"simpleSAV","CDC":"SAV"}

Get directory

Returns the definition of the full hierarchy, starting from the specified element. I.e., does the same as "get definition", except that it recursively seeks out all leaf nodes.

HTTP GET with: /directory/<LD>/<ObjectRef>


Example: GET http://localhost:8001/directory/C1/LN0.NamPlt.configRev

Returns:

{
    "name" : "configRev",
    "type" : "VisString255",
    "FC" : "DC",
    "items" : []
}

Example: GET http://localhost:8001/directory/C1/exampleRMXU_1.AmpLocPhsB

Returns:

{
    "name" : "AmpLocPhsB",
    "type" : "simpleSAV",
    "CDC" : "SAV",
    "items" : [
        {
            "name" : "instMag",
            "type" : "myAnalogValue",
            "FC" : "MX",
            "items" : [
                {
                    "name" : "f",
                    "type" : "FLOAT32",
                    "items" : []
                }
            ]
        },
        {
            "name" : "q",
            "type" : "Quality",
            "FC" : "MX",
            "qchg" : true,
            "items" : []
        }
   ]
}

Set value

Attempts to set the value of the specified element.

HTTP POST with: /<LD>/<ObjectRef> and with the new data value encoded as a string in the message body


Example: GET http://localhost:8001/C1/exampleMMXU_1.A.phsA.cVal.mag.f, 123.456

Returns: ok if successful

Building the JSON interface code

Two JSON examples are provided in the repository:

  1. A generic testing application, using scd.xml with main_json.c, and
  2. A simplified multi-vendor IED monitoring application, using ied_monitor.xml with main_json_ied_monitor.c.

To run the IED monitoring application:

  1. Generate C code for the ied_monitor.xml SCD file included in the repository. You may need to alter the JSON_WEB_SERVER_START_PORT variable in SCDCodeGenerator.java to control the TCP port range used for web server instances.
  2. In the C project build settings, add "${workspace_loc:/${ProjName}/src}" as an include path. This ensures the JSON code can access the other header files.
  3. Ensure that the *.c files in the c/src/json directory and main_json_ied_monitor.c are included in the build, and that the other main*.c files are not included.
  4. In ctypes.h, set JSON_INTERFACE to 1.
  5. Build and run the C project. On Windows, this step can take a long time, depending on the size of the data model.
  6. Open a web browser and go to http://localhost:8001/C1 to confirm that the web server is working.

Using SSL to encrypt all connections

  1. Install OpenSSL for your operating system.
  2. In the C project build settings:
    1. define the symbol USE_SSL
    2. link to the library ssleay32
  3. Link with SSL library
    1. Copy the file ssleay32.dll from the OpenSSL bin directory (e.g., "C:\OpenSSL-Win32\bin" on Windows) to the c/Lib directory in the repository.
    2. Rename the DLL to libssleay32.a.
  4. Ensure that the SSL certificate (ssl_cert.pem) is in the appropriate directory: typically at the root of the C directory if running from Eclipse. WARNING: the included certificate file is for testing only. Generate or purchase a new certificate for production purposes.
  5. If you wish to use HTTP authentication, set USE_HTTP_AUTH to 1 in json.h. Create your password file called htpasswd.txt, in the same directory as the SSL certificate. Mongoose (as well as various web sites) can be used to help create the MD5 hash: see main_json.c for an example.

Using the Python or Java interfaces

So far, this readme has described how to use the native C/C++ interface. It's also possible to use SWIG to automatically generate wrappers for high-level languages from C/C++ header files. At the moment, Python and Java interfaces on Windows and Linux have been tested, but other languages (such as C#, Lua, Perl, Ruby, etc.) should work too. (You can also use the high-level interface from C/C++ too, but not alongside the native interface.)

Four C files, with filenames interface*, are generated along with the rest of the GOOSE/SV code. These files, and the SWIG interface file rapid61850.i, are used as the input to SWIG. They contain functions to start a (platform-dependent) network interface using winpcap/libpcap, and functions to send GOOSE or SV packets using that network interface. All of the interaction with pcap is done in C, and is hidden by the interface given to SWIG.

Note that this interface can also be used within a C/C++ application - this is shown in the example main.c file, if HIGH_LEVEL_INTERFACE is defined as 1. If your are not using this high-level interface, and are using the plain C interface, you may need to exclude the two interface*.c files from the build in Eclipse.

Building on Windows

If using MinGW as the C compiler (as described above), this process is significantly simpler if the 32-bit versions of Eclipse and the JDK are used. The following instructions assume this. It's also assumed that the Python or Java application exists within a directory at the same level as the emf and c directories.

The following subsections explain how to change the compiler settings for the c project to generate a dynamic library, instead of an executable. This differs for Python and Java. It may be helpful to create different build configurations in Eclipse if you need to use more than one of the C/C++, Python, or Java interfaces. You may also need to exclude the existing main.c file from any Python or Java builds.

Python interface C compiler settings

Java interface C compiler settings

Building on Linux

On Linux, it's easier to create the Python or Java interface with the Terminal, rather than with Eclipse. The steps below assume Ubuntu (and have only been tested on 11.10 64-bit), so it may differ on other distributions. Note that because the following scripts compile all .c files, the example `main.c` files may need to be deleted from the C source code directory.

Install the following packages:

sudo apt-get install libpcap-dev
sudo apt-get install swig
sudo apt-get install build-essential
sudo apt-get install python2.7         # other Python versions should be ok
sudo apt-get install openjdk-6-jdk

Open a Terminal at the rapid61850/c/src directory.

Python

# attempt to clean up any previous files
rm *.o *.so *_wrap.c rapid61850.py rapid61850.pyc

# run SWIG, output goes in current directory
swig -python rapid61850.i

# compile and link the C library
gcc -fPIC -c *.c -I/usr/include/python2.7
gcc -shared *.o -lpcap -o _rapid61850.so

# run Python. sudo is needed for the network interface
sudo python2.7

# example Python program:
>>> import rapid61850
>>> rapid61850.start()
>>> print rapid61850.interface_gse_send_D1Q1SB4_C1_MMXUResult(1, 512)
332
>>> exit()

Java

# attempt to clean up any previous files
rm *.o *.so *_wrap.c java/*.class java/*.java

mkdir java    # only needed once

# run SWIG, and put the .java files (there will be a lot) in the "java" sub-directory
swig -java -outdir java rapid61850.i

# compile and link the C library
gcc -fPIC -c *.c -I/usr/lib/jvm/java-6-openjdk/include -I/usr/lib/jvm/java-6-openjdk/include/linux
ld -G *.o -lpcap -o librapid61850.so

# compile all .java files, including the sample program
javac -d java/ java/*.java ../../java_interface/Main.java

# run the sample Java program. sudo is needed for the network interface
cd java
sudo java -Djava.library.path=/home/steven/rapid61850/c/src/ Main    # this path must be set correctly

Software documentation

Validation

SCDValidator.java performs semantic validation of the SCD file, prior to code generation. It checks for the following constraints:

As shown in Main.java, the validation process is separate from the code generation process. Therefore, it's possible to reuse the validation process in other software, if needed.

The validation process extensively uses the EMF Model Query framework for searching and filtering SCD data. It uses a SQL-like syntax. For example, to find all IEDs in the SCD document object root:

public void checkForDuplicateNames(DocumentRoot root) {
    // describe a condition: is the object an IED (called `TIED` in EMF)?
    final EObjectCondition isIED = new EObjectTypeRelationCondition(
        SclPackage.eINSTANCE.getTIED()
    );

    // build and execute a query
    IQueryResult iedResult = new SELECT(
        new FROM(root),
        new WHERE(isIED)
    ).execute();

    // loop through results
    for (Object o : iedResult) {
        TIED ied = (TIED) o;
        String iedName = ied.getName();

        // ...
    }
}

Augmented IEC 61850-6 SCL model

SCDAdditionalMappings.java uses hash maps to explicitly link parts of the Java representation of the SCL model. This greatly simplifies the code generation process. (In the SCL, these links are implicit and are achieved by string-matching.) Each mapping is as follows:

Code generation

The following UML class diagram illustrates how a generic representation of a C file is used by the code generation process:

Java Emitter Template (JET) files (CSourceTemplate and CHeaderTemplate) are used to define the generic structure of C source and header files. This approach allows several header files, which specify function prototypes, to be generated automatically from the CSource objects.

Notes and possible features