An mbed implementation of IEC 61850-9-2LE Sample Values. Creating using the rapid61850 library, available at: https://github.com/stevenblair/rapid61850.

Dependencies:   mbed

An mbed implementation of IEC 61850-9-2LE Sample Values. Creating using the rapid61850 library, available at: https://github.com/stevenblair/rapid61850.

Committer:
sblair
Date:
Tue Oct 02 21:31:05 2012 +0000
Revision:
0:f09b7bb8bcce
converted library to folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sblair 0:f09b7bb8bcce 1 /**
sblair 0:f09b7bb8bcce 2 * Rapid-prototyping protection schemes with IEC 61850
sblair 0:f09b7bb8bcce 3 *
sblair 0:f09b7bb8bcce 4 * Copyright (c) 2012 Steven Blair
sblair 0:f09b7bb8bcce 5 *
sblair 0:f09b7bb8bcce 6 * This program is free software; you can redistribute it and/or
sblair 0:f09b7bb8bcce 7 * modify it under the terms of the GNU General Public License
sblair 0:f09b7bb8bcce 8 * as published by the Free Software Foundation; either version 2
sblair 0:f09b7bb8bcce 9 * of the License, or (at your option) any later version.
sblair 0:f09b7bb8bcce 10
sblair 0:f09b7bb8bcce 11 * This program is distributed in the hope that it will be useful,
sblair 0:f09b7bb8bcce 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sblair 0:f09b7bb8bcce 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
sblair 0:f09b7bb8bcce 14 * GNU General Public License for more details.
sblair 0:f09b7bb8bcce 15
sblair 0:f09b7bb8bcce 16 * You should have received a copy of the GNU General Public License
sblair 0:f09b7bb8bcce 17 * along with this program; if not, write to the Free Software
sblair 0:f09b7bb8bcce 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
sblair 0:f09b7bb8bcce 19 */
sblair 0:f09b7bb8bcce 20
sblair 0:f09b7bb8bcce 21 #ifndef GSE_BER_ENCODE_BASIC_C
sblair 0:f09b7bb8bcce 22 #define GSE_BER_ENCODE_BASIC_C
sblair 0:f09b7bb8bcce 23
sblair 0:f09b7bb8bcce 24 #include "ctypes.h"
sblair 0:f09b7bb8bcce 25 #include "datatypes.h"
sblair 0:f09b7bb8bcce 26 #include "ied.h"
sblair 0:f09b7bb8bcce 27 #include "gseEncodeBasic.h"
sblair 0:f09b7bb8bcce 28
sblair 0:f09b7bb8bcce 29 // BER encoding of basic types
sblair 0:f09b7bb8bcce 30 int BER_ENCODE_CTYPE_FLOAT32(unsigned char *buf, CTYPE_FLOAT32 *value) {
sblair 0:f09b7bb8bcce 31 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 32 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_FLOAT32(value);
sblair 0:f09b7bb8bcce 33
sblair 0:f09b7bb8bcce 34 buf[offset++] = ASN1_TAG_FLOATING_POINT;
sblair 0:f09b7bb8bcce 35 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 36
sblair 0:f09b7bb8bcce 37 buf[offset++] = 0x08; // bits for exponent
sblair 0:f09b7bb8bcce 38 netmemcpy(&buf[offset], value, len - 1);
sblair 0:f09b7bb8bcce 39
sblair 0:f09b7bb8bcce 40 return offset + len - 1;
sblair 0:f09b7bb8bcce 41 }
sblair 0:f09b7bb8bcce 42 int BER_ENCODE_CTYPE_FLOAT64(unsigned char *buf, CTYPE_FLOAT64 *value) {
sblair 0:f09b7bb8bcce 43 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 44 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_FLOAT64(value);
sblair 0:f09b7bb8bcce 45
sblair 0:f09b7bb8bcce 46 buf[offset++] = ASN1_TAG_FLOATING_POINT;
sblair 0:f09b7bb8bcce 47 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 48
sblair 0:f09b7bb8bcce 49 buf[offset++] = 0x0B; // bits for exponent
sblair 0:f09b7bb8bcce 50 netmemcpy(&buf[offset], value, len - 1);
sblair 0:f09b7bb8bcce 51
sblair 0:f09b7bb8bcce 52 return offset + len - 1;
sblair 0:f09b7bb8bcce 53 }
sblair 0:f09b7bb8bcce 54
sblair 0:f09b7bb8bcce 55 int BER_ENCODE_CTYPE_QUALITY(unsigned char *buf, CTYPE_QUALITY *value) {
sblair 0:f09b7bb8bcce 56 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 57 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_QUALITY(value);
sblair 0:f09b7bb8bcce 58
sblair 0:f09b7bb8bcce 59 buf[offset++] = ASN1_TAG_BIT_STRING;
sblair 0:f09b7bb8bcce 60 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 61
sblair 0:f09b7bb8bcce 62 buf[offset++] = QUALITY_UNUSED_BITS; // number of unused bits
sblair 0:f09b7bb8bcce 63 netmemcpy(&buf[offset], value, len - 1);
sblair 0:f09b7bb8bcce 64
sblair 0:f09b7bb8bcce 65 return offset + len - 1;
sblair 0:f09b7bb8bcce 66 }
sblair 0:f09b7bb8bcce 67 int BER_ENCODE_CTYPE_TIMESTAMP(unsigned char *buf, CTYPE_TIMESTAMP *value) {
sblair 0:f09b7bb8bcce 68 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 69 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_TIMESTAMP(value);
sblair 0:f09b7bb8bcce 70
sblair 0:f09b7bb8bcce 71 buf[offset++] = ASN1_TAG_OCTET_STRING;
sblair 0:f09b7bb8bcce 72 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 73
sblair 0:f09b7bb8bcce 74 memcpy(&buf[offset], value, len);
sblair 0:f09b7bb8bcce 75
sblair 0:f09b7bb8bcce 76 return offset + len;
sblair 0:f09b7bb8bcce 77 }
sblair 0:f09b7bb8bcce 78 int BER_ENCODE_CTYPE_ENUM(unsigned char *buf, CTYPE_ENUM *value) {
sblair 0:f09b7bb8bcce 79 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 80 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_ENUM(value);
sblair 0:f09b7bb8bcce 81
sblair 0:f09b7bb8bcce 82 buf[offset++] = ASN1_TAG_INTEGER;
sblair 0:f09b7bb8bcce 83 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 84
sblair 0:f09b7bb8bcce 85 #if GOOSE_FIXED_SIZE == 1
sblair 0:f09b7bb8bcce 86 ber_encode_integer_fixed_size(&buf[offset], value, SV_GET_LENGTH_INT8);
sblair 0:f09b7bb8bcce 87 #else
sblair 0:f09b7bb8bcce 88 ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT32U); // assuming enum is an int - allows any enum type to be used
sblair 0:f09b7bb8bcce 89 #endif
sblair 0:f09b7bb8bcce 90
sblair 0:f09b7bb8bcce 91 return offset + len;
sblair 0:f09b7bb8bcce 92 }
sblair 0:f09b7bb8bcce 93 int BER_ENCODE_CTYPE_INT8(unsigned char *buf, CTYPE_INT8 *value) {
sblair 0:f09b7bb8bcce 94 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 95 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT8(value);
sblair 0:f09b7bb8bcce 96
sblair 0:f09b7bb8bcce 97 buf[offset++] = ASN1_TAG_INTEGER;
sblair 0:f09b7bb8bcce 98 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 99
sblair 0:f09b7bb8bcce 100 #if GOOSE_FIXED_SIZE == 1
sblair 0:f09b7bb8bcce 101 ber_encode_integer_fixed_size(&buf[offset], value, SV_GET_LENGTH_INT8);
sblair 0:f09b7bb8bcce 102 #else
sblair 0:f09b7bb8bcce 103 ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT8);
sblair 0:f09b7bb8bcce 104 #endif
sblair 0:f09b7bb8bcce 105
sblair 0:f09b7bb8bcce 106 return offset + len;
sblair 0:f09b7bb8bcce 107 }
sblair 0:f09b7bb8bcce 108 int BER_ENCODE_CTYPE_INT16(unsigned char *buf, CTYPE_INT16 *value) {
sblair 0:f09b7bb8bcce 109 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 110 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT16(value);
sblair 0:f09b7bb8bcce 111
sblair 0:f09b7bb8bcce 112 buf[offset++] = ASN1_TAG_INTEGER;
sblair 0:f09b7bb8bcce 113 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 114
sblair 0:f09b7bb8bcce 115 #if GOOSE_FIXED_SIZE == 1
sblair 0:f09b7bb8bcce 116 ber_encode_integer_fixed_size(&buf[offset], value, SV_GET_LENGTH_INT16);
sblair 0:f09b7bb8bcce 117 #else
sblair 0:f09b7bb8bcce 118 ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT16);
sblair 0:f09b7bb8bcce 119 #endif
sblair 0:f09b7bb8bcce 120
sblair 0:f09b7bb8bcce 121 return offset + len;
sblair 0:f09b7bb8bcce 122 }
sblair 0:f09b7bb8bcce 123 int BER_ENCODE_CTYPE_INT32(unsigned char *buf, CTYPE_INT32 *value) {
sblair 0:f09b7bb8bcce 124 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 125 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT32(value);
sblair 0:f09b7bb8bcce 126
sblair 0:f09b7bb8bcce 127 buf[offset++] = ASN1_TAG_INTEGER;
sblair 0:f09b7bb8bcce 128 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 129
sblair 0:f09b7bb8bcce 130 #if GOOSE_FIXED_SIZE == 1
sblair 0:f09b7bb8bcce 131 ber_encode_integer_fixed_size(&buf[offset], value, SV_GET_LENGTH_INT32);
sblair 0:f09b7bb8bcce 132 #else
sblair 0:f09b7bb8bcce 133 ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT32);
sblair 0:f09b7bb8bcce 134 #endif
sblair 0:f09b7bb8bcce 135
sblair 0:f09b7bb8bcce 136 return offset + len;
sblair 0:f09b7bb8bcce 137 }
sblair 0:f09b7bb8bcce 138 int BER_ENCODE_CTYPE_INT16U(unsigned char *buf, CTYPE_INT16U *value) {
sblair 0:f09b7bb8bcce 139 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 140 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT16U(value);
sblair 0:f09b7bb8bcce 141
sblair 0:f09b7bb8bcce 142 buf[offset++] = ASN1_TAG_UNSIGNED;
sblair 0:f09b7bb8bcce 143 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 144
sblair 0:f09b7bb8bcce 145 #if GOOSE_FIXED_SIZE == 1
sblair 0:f09b7bb8bcce 146 ber_encode_integer_fixed_size(&buf[offset], value, SV_GET_LENGTH_INT16U);
sblair 0:f09b7bb8bcce 147 #else
sblair 0:f09b7bb8bcce 148 ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT16U);
sblair 0:f09b7bb8bcce 149 #endif
sblair 0:f09b7bb8bcce 150
sblair 0:f09b7bb8bcce 151 return offset + len;
sblair 0:f09b7bb8bcce 152 }
sblair 0:f09b7bb8bcce 153 int BER_ENCODE_CTYPE_INT32U(unsigned char *buf, CTYPE_INT32U *value) {
sblair 0:f09b7bb8bcce 154 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 155 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_INT32U(value);
sblair 0:f09b7bb8bcce 156
sblair 0:f09b7bb8bcce 157 buf[offset++] = ASN1_TAG_UNSIGNED;
sblair 0:f09b7bb8bcce 158 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 159
sblair 0:f09b7bb8bcce 160 #if GOOSE_FIXED_SIZE == 1
sblair 0:f09b7bb8bcce 161 ber_encode_integer_fixed_size(&buf[offset], value, SV_GET_LENGTH_INT32U);
sblair 0:f09b7bb8bcce 162 #else
sblair 0:f09b7bb8bcce 163 ber_encode_integer(&buf[offset], value, SV_GET_LENGTH_INT32U);
sblair 0:f09b7bb8bcce 164 #endif
sblair 0:f09b7bb8bcce 165
sblair 0:f09b7bb8bcce 166 return offset + len;
sblair 0:f09b7bb8bcce 167 }
sblair 0:f09b7bb8bcce 168 int BER_ENCODE_CTYPE_VISSTRING255(unsigned char *buf, CTYPE_VISSTRING255 *value) {
sblair 0:f09b7bb8bcce 169 netmemcpy(buf, value, BER_GET_LENGTH_CTYPE_VISSTRING255(value));
sblair 0:f09b7bb8bcce 170
sblair 0:f09b7bb8bcce 171 return BER_GET_LENGTH_CTYPE_VISSTRING255(value);
sblair 0:f09b7bb8bcce 172 }
sblair 0:f09b7bb8bcce 173 int BER_ENCODE_CTYPE_BOOLEAN(unsigned char *buf, CTYPE_BOOLEAN *value) {
sblair 0:f09b7bb8bcce 174 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 175 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_BOOLEAN(value);
sblair 0:f09b7bb8bcce 176
sblair 0:f09b7bb8bcce 177 buf[offset++] = ASN1_TAG_BOOLEAN;
sblair 0:f09b7bb8bcce 178 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 179
sblair 0:f09b7bb8bcce 180 netmemcpy(&buf[offset], value, len);
sblair 0:f09b7bb8bcce 181
sblair 0:f09b7bb8bcce 182 return offset + len;
sblair 0:f09b7bb8bcce 183 }
sblair 0:f09b7bb8bcce 184 int BER_ENCODE_CTYPE_DBPOS(unsigned char *buf, CTYPE_DBPOS *value) {
sblair 0:f09b7bb8bcce 185 CTYPE_INT16U offset = 0;
sblair 0:f09b7bb8bcce 186 CTYPE_INT16U len = BER_GET_LENGTH_CTYPE_DBPOS(value);
sblair 0:f09b7bb8bcce 187
sblair 0:f09b7bb8bcce 188 buf[offset++] = 0x85;
sblair 0:f09b7bb8bcce 189 offset += encodeLength(&buf[offset], len);
sblair 0:f09b7bb8bcce 190
sblair 0:f09b7bb8bcce 191 netmemcpy(&buf[offset], value, len);
sblair 0:f09b7bb8bcce 192
sblair 0:f09b7bb8bcce 193 return offset + len;
sblair 0:f09b7bb8bcce 194 }
sblair 0:f09b7bb8bcce 195
sblair 0:f09b7bb8bcce 196 #endif