Initial commit.

pull/5/head
weetmuts 2017-08-09 12:00:11 +02:00
rodzic 425ab12b15
commit 9ae2a5ec25
16 zmienionych plików z 2193 dodań i 1 usunięć

54
Makefile 100644
Wyświetl plik

@ -0,0 +1,54 @@
# To compile for Raspberry PI ARM:
# make HOST=arm
#
# To build with debug information:
# make DEBUG=true
# make DEBUG=true HOST=arm
ifeq "$(HOST)" "arm"
CXX=arm-linux-gnueabihf-g++
STRIP=arm-linux-gnueabihf-strip
BUILD=build_arm
else
CXX=clang++
STRIP=strip
#CXX=g++
BUILD=build
endif
ifeq "$(DEBUG)" "true"
DEBUG_FLAGS=-O0 -g
STRIP_BINARY=
BUILD:=$(BUILD)_debug
else
DEBUG_FLAGS=-Os
STRIP_BINARY=$(STRIP) $(BUILD)/wmbusmeters
endif
$(shell mkdir -p $(BUILD))
CXXFLAGS := $(DEBUG_FLAGS) -Wall -fmessage-length=0 -std=c++11 -Wno-unused-function "-DWMBUSMETERS_VERSION=\"0.1\""
$(BUILD)/%.o: %.cc $(wildcard %.h)
$(CXX) $(CXXFLAGS) $< -c -o $@
METERS_OBJS:=\
$(BUILD)/main.o \
$(BUILD)/util.o \
$(BUILD)/aes.o \
$(BUILD)/serial.o \
$(BUILD)/wmbus.o \
$(BUILD)/wmbus_im871a.o \
$(BUILD)/meters.o \
$(BUILD)/meter_multical21.o
all: $(BUILD)/wmbusmeters
$(STRIP_BINARY)
$(BUILD)/wmbusmeters: $(METERS_OBJS)
$(CXX) -o $(BUILD)/wmbusmeters $(METERS_OBJS) -lpthread
clean:
rm -f build/* build_arm/* build_debug/* build_arm_debug/* *~

Wyświetl plik

@ -1,2 +1,45 @@
# wmbusmeters
Read the wireless mbus protocol to acquire utility meter readings.
The program receives and decodes C1 telegrams
(using the wireless mbus protocol) to acquire
utility meter readings. No configuration file
exists, you change main.cc, recompile and run
to output the values you are interested in,
typically to log and be processed by another tool.
Builds and runs on GNU/Linux:
make
./build/wmbusmeters
./build/wmbusmeters --verbose
make HOST=arm
./build_arm/wmbusmeters
make DEBUG=true
./build_debug/wmbusmeters
make DEBUG=true HOST=arm
./build_arm_debug/wmbusmeters
(After you insert the im871A USB stick, do:
chown me:me /dev/ttyUSB0
to avoid having to run the program as root.)
Currently only supports the USB stick receiver im871A
and the water meter Multical21. The source code is modular
and it should be relatively straightforward to add
more receivers (Amber anyone?) and meters.
Good documents on the wireless mbus protocol:
https://www.infineon.com/dgdl/TDA5340_AN_WMBus_v1.0.pdf
http://fastforward.ag/downloads/docu/FAST_EnergyCam-Protocol-wirelessMBUS.pdf
The AES source code is copied from:
https://github.com/kokke/tiny-AES128-C
The following two other github projects were of great help:
https://github.com/ffcrg/ecpiww
https://github.com/tobiasrask/wmbus-client
Code can print total water consumption! But everything else is
missing. CRC checks anyone? :-) Don't rely on these measurements
for anything really important!

597
aes.cc 100644
Wyświetl plik

@ -0,0 +1,597 @@
// Source from https://github.com/kokke/tiny-AES128-C
// Public Domain / CC0 / Unlicense
/*
This is an implementation of the AES algorithm, specifically ECB and CBC mode.
Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
The implementation is verified against the test vectors in:
National Institute of Standards and Technology Special Publication 800-38A 2001 ED
ECB-AES128
----------
plain-text:
6bc1bee22e409f96e93d7e117393172a
ae2d8a571e03ac9c9eb76fac45af8e51
30c81c46a35ce411e5fbc1191a0a52ef
f69f2445df4f9b17ad2b417be66c3710
key:
2b7e151628aed2a6abf7158809cf4f3c
resulting cipher
3ad77bb40d7a3660a89ecaf32466ef97
f5d3d58503b9699de785895a96fdbaaf
43b1cd7f598ece23881b00e3ed030688
7b0c785e27e8ad3f8223207104725dd4
NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
You should pad the end of the string with zeros if this is not the case.
For AES192/256 the block size is proportionally larger.
*/
/*****************************************************************************/
/* Includes: */
/*****************************************************************************/
#include <stdint.h>
#include <string.h> // CBC mode, for memset
#include "aes.h"
/*****************************************************************************/
/* Defines: */
/*****************************************************************************/
// The number of columns comprising a state in AES. This is a constant in AES. Value=4
#define Nb 4
#define BLOCKLEN 16 //Block length in bytes AES is 128b block only
#if defined(AES256) && (AES256 == 1)
#define Nk 8
#define KEYLEN 32
#define Nr 14
#define keyExpSize 240
#elif defined(AES192) && (AES192 == 1)
#define Nk 6
#define KEYLEN 24
#define Nr 12
#define keyExpSize 208
#else
#define Nk 4 // The number of 32 bit words in a key.
#define KEYLEN 16 // Key length in bytes
#define Nr 10 // The number of rounds in AES Cipher.
#define keyExpSize 176
#endif
// jcallan@github points out that declaring Multiply as a function
// reduces code size considerably with the Keil ARM compiler.
// See this link for more information: https://github.com/kokke/tiny-AES128-C/pull/3
#ifndef MULTIPLY_AS_A_FUNCTION
#define MULTIPLY_AS_A_FUNCTION 0
#endif
/*****************************************************************************/
/* Private variables: */
/*****************************************************************************/
// state - array holding the intermediate results during decryption.
typedef uint8_t state_t[4][4];
static state_t* state;
// The array that stores the round keys.
static uint8_t RoundKey[keyExpSize];
// The Key input to the AES Program
static const uint8_t* Key;
#if defined(CBC) && CBC
// Initial Vector used only for CBC mode
static uint8_t* Iv;
#endif
// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
// The numbers below can be computed dynamically trading ROM for RAM -
// This can be useful in (embedded) bootloader applications, where ROM is often limited.
static const uint8_t sbox[256] = {
//0 1 2 3 4 5 6 7 8 9 A B C D E F
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
static const uint8_t rsbox[256] = {
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
// The round constant word array, Rcon[i], contains the values given by
// x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
static const uint8_t Rcon[11] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
/*
* Jordan Goulder points out in PR #12 (https://github.com/kokke/tiny-AES128-C/pull/12),
* that you can remove most of the elements in the Rcon array, because they are unused.
*
* From Wikipedia's article on the Rijndael key schedule @ https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon
*
* "Only the first some of these constants are actually used – up to rcon[10] for AES-128 (as 11 round keys are needed),
* up to rcon[8] for AES-192, up to rcon[7] for AES-256. rcon[0] is not used in AES algorithm."
*
* ... which is why the full array below has been 'disabled' below.
*/
#if 0
static const uint8_t Rcon[256] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d };
#endif
/*****************************************************************************/
/* Private functions: */
/*****************************************************************************/
static uint8_t getSBoxValue(uint8_t num)
{
return sbox[num];
}
static uint8_t getSBoxInvert(uint8_t num)
{
return rsbox[num];
}
// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
static void KeyExpansion(void)
{
uint32_t i, k;
uint8_t tempa[4]; // Used for the column/row operations
// The first round key is the key itself.
for (i = 0; i < Nk; ++i)
{
RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
}
// All other round keys are found from the previous round keys.
//i == Nk
for (; i < Nb * (Nr + 1); ++i)
{
{
tempa[0]=RoundKey[(i-1) * 4 + 0];
tempa[1]=RoundKey[(i-1) * 4 + 1];
tempa[2]=RoundKey[(i-1) * 4 + 2];
tempa[3]=RoundKey[(i-1) * 4 + 3];
}
if (i % Nk == 0)
{
// This function shifts the 4 bytes in a word to the left once.
// [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
// Function RotWord()
{
k = tempa[0];
tempa[0] = tempa[1];
tempa[1] = tempa[2];
tempa[2] = tempa[3];
tempa[3] = k;
}
// SubWord() is a function that takes a four-byte input word and
// applies the S-box to each of the four bytes to produce an output word.
// Function Subword()
{
tempa[0] = getSBoxValue(tempa[0]);
tempa[1] = getSBoxValue(tempa[1]);
tempa[2] = getSBoxValue(tempa[2]);
tempa[3] = getSBoxValue(tempa[3]);
}
tempa[0] = tempa[0] ^ Rcon[i/Nk];
}
#if defined(AES256) && (AES256 == 1)
if (i % Nk == 4)
{
// Function Subword()
{
tempa[0] = getSBoxValue(tempa[0]);
tempa[1] = getSBoxValue(tempa[1]);
tempa[2] = getSBoxValue(tempa[2]);
tempa[3] = getSBoxValue(tempa[3]);
}
}
#endif
RoundKey[i * 4 + 0] = RoundKey[(i - Nk) * 4 + 0] ^ tempa[0];
RoundKey[i * 4 + 1] = RoundKey[(i - Nk) * 4 + 1] ^ tempa[1];
RoundKey[i * 4 + 2] = RoundKey[(i - Nk) * 4 + 2] ^ tempa[2];
RoundKey[i * 4 + 3] = RoundKey[(i - Nk) * 4 + 3] ^ tempa[3];
}
}
// This function adds the round key to state.
// The round key is added to the state by an XOR function.
static void AddRoundKey(uint8_t round)
{
uint8_t i,j;
for (i=0;i<4;++i)
{
for (j = 0; j < 4; ++j)
{
(*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j];
}
}
}
// The SubBytes Function Substitutes the values in the
// state matrix with values in an S-box.
static void SubBytes(void)
{
uint8_t i, j;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
(*state)[j][i] = getSBoxValue((*state)[j][i]);
}
}
}
// The ShiftRows() function shifts the rows in the state to the left.
// Each row is shifted with different offset.
// Offset = Row number. So the first row is not shifted.
static void ShiftRows(void)
{
uint8_t temp;
// Rotate first row 1 columns to left
temp = (*state)[0][1];
(*state)[0][1] = (*state)[1][1];
(*state)[1][1] = (*state)[2][1];
(*state)[2][1] = (*state)[3][1];
(*state)[3][1] = temp;
// Rotate second row 2 columns to left
temp = (*state)[0][2];
(*state)[0][2] = (*state)[2][2];
(*state)[2][2] = temp;
temp = (*state)[1][2];
(*state)[1][2] = (*state)[3][2];
(*state)[3][2] = temp;
// Rotate third row 3 columns to left
temp = (*state)[0][3];
(*state)[0][3] = (*state)[3][3];
(*state)[3][3] = (*state)[2][3];
(*state)[2][3] = (*state)[1][3];
(*state)[1][3] = temp;
}
static uint8_t xtime(uint8_t x)
{
return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
}
// MixColumns function mixes the columns of the state matrix
static void MixColumns(void)
{
uint8_t i;
uint8_t Tmp,Tm,t;
for (i = 0; i < 4; ++i)
{
t = (*state)[i][0];
Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
}
}
// Multiply is used to multiply numbers in the field GF(2^8)
#if MULTIPLY_AS_A_FUNCTION
static uint8_t Multiply(uint8_t x, uint8_t y)
{
return (((y & 1) * x) ^
((y>>1 & 1) * xtime(x)) ^
((y>>2 & 1) * xtime(xtime(x))) ^
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
((y>>4 & 1) * xtime(xtime(xtime(xtime(x))))));
}
#else
#define Multiply(x, y) \
( ((y & 1) * x) ^ \
((y>>1 & 1) * xtime(x)) ^ \
((y>>2 & 1) * xtime(xtime(x))) ^ \
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \
#endif
// MixColumns function mixes the columns of the state matrix.
// The method used to multiply may be difficult to understand for the inexperienced.
// Please use the references to gain more information.
static void InvMixColumns(void)
{
int i;
uint8_t a, b, c, d;
for (i = 0; i < 4; ++i)
{
a = (*state)[i][0];
b = (*state)[i][1];
c = (*state)[i][2];
d = (*state)[i][3];
(*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
(*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
(*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
(*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
}
}
// The SubBytes Function Substitutes the values in the
// state matrix with values in an S-box.
static void InvSubBytes(void)
{
uint8_t i,j;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
(*state)[j][i] = getSBoxInvert((*state)[j][i]);
}
}
}
static void InvShiftRows(void)
{
uint8_t temp;
// Rotate first row 1 columns to right
temp = (*state)[3][1];
(*state)[3][1] = (*state)[2][1];
(*state)[2][1] = (*state)[1][1];
(*state)[1][1] = (*state)[0][1];
(*state)[0][1] = temp;
// Rotate second row 2 columns to right
temp = (*state)[0][2];
(*state)[0][2] = (*state)[2][2];
(*state)[2][2] = temp;
temp = (*state)[1][2];
(*state)[1][2] = (*state)[3][2];
(*state)[3][2] = temp;
// Rotate third row 3 columns to right
temp = (*state)[0][3];
(*state)[0][3] = (*state)[1][3];
(*state)[1][3] = (*state)[2][3];
(*state)[2][3] = (*state)[3][3];
(*state)[3][3] = temp;
}
// Cipher is the main function that encrypts the PlainText.
static void Cipher(void)
{
uint8_t round = 0;
// Add the First round key to the state before starting the rounds.
AddRoundKey(0);
// There will be Nr rounds.
// The first Nr-1 rounds are identical.
// These Nr-1 rounds are executed in the loop below.
for (round = 1; round < Nr; ++round)
{
SubBytes();
ShiftRows();
MixColumns();
AddRoundKey(round);
}
// The last round is given below.
// The MixColumns function is not here in the last round.
SubBytes();
ShiftRows();
AddRoundKey(Nr);
}
static void InvCipher(void)
{
uint8_t round=0;
// Add the First round key to the state before starting the rounds.
AddRoundKey(Nr);
// There will be Nr rounds.
// The first Nr-1 rounds are identical.
// These Nr-1 rounds are executed in the loop below.
for (round = (Nr - 1); round > 0; --round)
{
InvShiftRows();
InvSubBytes();
AddRoundKey(round);
InvMixColumns();
}
// The last round is given below.
// The MixColumns function is not here in the last round.
InvShiftRows();
InvSubBytes();
AddRoundKey(0);
}
/*****************************************************************************/
/* Public functions: */
/*****************************************************************************/
#if defined(ECB) && (ECB == 1)
void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output, const uint32_t length)
{
// Copy input to output, and work in-memory on output
memcpy(output, input, length);
state = (state_t*)output;
Key = key;
KeyExpansion();
// The next function call encrypts the PlainText with the Key using AES algorithm.
Cipher();
}
void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
{
// Copy input to output, and work in-memory on output
memcpy(output, input, length);
state = (state_t*)output;
// The KeyExpansion routine must be called before encryption.
Key = key;
KeyExpansion();
InvCipher();
}
#endif // #if defined(ECB) && (ECB == 1)
#if defined(CBC) && (CBC == 1)
static void XorWithIv(uint8_t* buf)
{
uint8_t i;
for (i = 0; i < BLOCKLEN; ++i) //WAS for(i = 0; i < KEYLEN; ++i) but the block in AES is always 128bit so 16 bytes!
{
buf[i] ^= Iv[i];
}
}
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
{
uintptr_t i;
uint8_t extra = length % BLOCKLEN; /* Remaining bytes in the last non-full block */
// Skip the key expansion if key is passed as 0
if (0 != key)
{
Key = key;
KeyExpansion();
}
if (iv != 0)
{
Iv = (uint8_t*)iv;
}
for (i = 0; i < length; i += BLOCKLEN)
{
XorWithIv(input);
memcpy(output, input, BLOCKLEN);
state = (state_t*)output;
Cipher();
Iv = output;
input += BLOCKLEN;
output += BLOCKLEN;
//printf("Step %d - %d", i/16, i);
}
if (extra)
{
memcpy(output, input, extra);
state = (state_t*)output;
Cipher();
}
}
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
{
uintptr_t i;
uint8_t extra = length % BLOCKLEN; /* Remaining bytes in the last non-full block */
// Skip the key expansion if key is passed as 0
if (0 != key)
{
Key = key;
KeyExpansion();
}
// If iv is passed as 0, we continue to encrypt without re-setting the Iv
if (iv != 0)
{
Iv = (uint8_t*)iv;
}
for (i = 0; i < length; i += BLOCKLEN)
{
memcpy(output, input, BLOCKLEN);
state = (state_t*)output;
InvCipher();
XorWithIv(output);
Iv = input;
input += BLOCKLEN;
output += BLOCKLEN;
}
if (extra)
{
memcpy(output, input, extra);
state = (state_t*)output;
InvCipher();
}
}
#endif // #if defined(CBC) && (CBC == 1)

43
aes.h 100644
Wyświetl plik

@ -0,0 +1,43 @@
// Source from https://github.com/kokke/tiny-AES128-C
// Public Domain / CC0 / Unlicense
#ifndef _AES_H_
#define _AES_H_
#include <stdint.h>
// #define the macros below to 1/0 to enable/disable the mode of operation.
//
// CBC enables AES encryption in CBC-mode of operation.
// ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously.
// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
#ifndef CBC
#define CBC 1
#endif
#ifndef ECB
#define ECB 1
#endif
#define AES128 1
//#define AES192 1
//#define AES256 1
#if defined(ECB) && (ECB == 1)
void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length);
void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length);
#endif // #if defined(ECB) && (ECB == !)
#if defined(CBC) && (CBC == 1)
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv);
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv);
#endif // #if defined(CBC) && (CBC == 1)
#endif //_AES_H_

62
main.cc 100644
Wyświetl plik

@ -0,0 +1,62 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"util.h"
#include"serial.h"
#include"wmbus.h"
#include"meters.h"
#include"aes.h"
#include<string.h>
using namespace std;
int main(int argc, char **argv)
{
if (argc > 1) {
if (!strcmp(argv[1], "--verbose")) {
verboseEnabled(true);
} else {
printf("wmbusmeters version: " WMBUSMETERS_VERSION "\n\n");
printf("Usage: wmbusmeters [--verbose]\n");
exit(0);
}
}
auto manager = createSerialCommunicationManager();
onExit(call(manager,stop));
auto wmbus = openIM871A("/dev/ttyUSB0", manager);
wmbus->setLinkMode(C1a);
if (wmbus->getLinkMode()!=C1a) error("Could not set link mode to C1a\n");
auto meter = createMultical21(wmbus, "MyHouseWater", "12345678", "00112233445566778899AABBCCDDEEFF");
meter->onUpdate([meter](){
printf("%s\t%s\t% 3.3f m3\t%s\n",
meter->name().c_str(),
meter->id().c_str(),
meter->totalWaterConsumption(),
meter->datetimeOfUpdate().c_str());
});
manager->waitForStop();
}

227
meter_multical21.cc 100644
Wyświetl plik

@ -0,0 +1,227 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"aes.h"
#include"meters.h"
#include"wmbus.h"
#include"util.h"
#include<memory.h>
#include<stdio.h>
#include<string>
#include<time.h>
#include<vector>
using namespace std;
struct MeterMultical21 : public Meter {
MeterMultical21(WMBus *bus, const char *name, const char *id, const char *key);
string id();
string name();
float totalWaterConsumption();
string datetimeOfUpdate();
void onUpdate(function<void()> cb);
private:
void handleTelegram(Telegram*t);
float processContent(vector<uchar> &d);
float total_water_consumption_;
time_t datetime_of_update_;
string name_;
vector<uchar> id_;
vector<uchar> key_;
WMBus *bus_;
function<void()> on_update_;
};
MeterMultical21::MeterMultical21(WMBus *bus, const char *name, const char *id, const char *key) :
total_water_consumption_(0), name_(name), bus_(bus)
{
hex2bin(id, &id_);
hex2bin(key, &key_);
bus_->onTelegram(calll(this,handleTelegram,Telegram*));
}
string MeterMultical21::id()
{
return bin2hex(id_);
}
string MeterMultical21::name()
{
return name_;
}
void MeterMultical21::onUpdate(function<void()> cb)
{
on_update_ = cb;
}
float MeterMultical21::totalWaterConsumption()
{
return total_water_consumption_;
}
string MeterMultical21::datetimeOfUpdate()
{
char datetime[20];
memset(datetime, 0, sizeof(datetime));
strftime(datetime, 20, "%Y-%m-%d %H:%M.%S", localtime(&datetime_of_update_));
return string(datetime);
}
Meter *createMultical21(WMBus *bus, const char *name, const char *id, const char *key) {
return new MeterMultical21(bus,name,id,key);
}
void MeterMultical21::handleTelegram(Telegram *t) {
if (t->a_field_address[3] != id_[3] ||
t->a_field_address[2] != id_[2] ||
t->a_field_address[1] != id_[1] ||
t->a_field_address[0] != id_[0]) {
verbose("Ignoring meter %02x %02x %02x %02x \n",
t->a_field_address[3], t->a_field_address[2], t->a_field_address[1], t->a_field_address[0]);
return;
} else {
verbose("Update from meter %02x %02x %02x %02x!\n",
t->a_field_address[3], t->a_field_address[2], t->a_field_address[1], t->a_field_address[0]);
}
int cc_field = t->payload[0];
//int acc = t->payload[1];
uchar sn[4];
sn[0] = t->payload[2];
sn[1] = t->payload[3];
sn[2] = t->payload[4];
sn[3] = t->payload[5];
vector<uchar> content;
content.insert(content.end(), t->payload.begin()+6, t->payload.end());
while (content.size() < 16) { content.push_back(0); }
uchar iv[16];
int i=0;
// M-field
iv[i++] = t->m_field>>8; iv[i++] = t->m_field&255;
// A-field
for (int j=0; j<6; ++j) { iv[i++] = t->a_field[j]; }
// CC-field
iv[i++] = cc_field;
// SN-field
for (int j=0; j<4; ++j) { iv[i++] = sn[j]; }
// FN
iv[i++] = 0; iv[i++] = 0;
// BC
iv[i++] = 0;
vector<uchar> ivv(iv, iv+16);
string s = bin2hex(ivv);
verbose("IV %s\n", s.c_str());
uchar back[16];
AES_ECB_encrypt(iv, &key_[0], back, 16);
uchar decrypt[16];
xorit(back, &content[0], decrypt, 16);
vector<uchar> dec(decrypt, decrypt+16);
string answer = bin2hex(dec);
// The total water consumption data is inside the first 16 bytes for
// both short and full frames. So this code does not even bother
// to decrypt later bytes! Its aes-ctr mode, so add 1 to IV to
// decrypt next 16 bytes, add 2 to IV to decrypt the next next 16 bytes
// and so on and so forth...
//
// There is supposedly more interesting data, like the water temperature
// and warning flags, like leak, burst, dry etc.
verbose("Decrypted >%s<\n", answer.c_str());
total_water_consumption_ = processContent(dec);
datetime_of_update_ = time(NULL);
if (on_update_) on_update_();
}
float MeterMultical21::processContent(vector<uchar> &c) {
float consumption = 0.0;
/* int crc0 = c[0];
int crc1 = c[1]; */
int frame_type = c[2];
if (frame_type == 0x79) {
verbose("Short frame\n");
/* int ecrc0 = c[3];
int ecrc1 = c[4];
int ecrc2 = c[5];
int ecrc3 = c[6];
int rec1val0 = c[7];
int rec1val1 = c[8];*/
int rec2val0 = c[9];
int rec2val1 = c[10];
int rec2val2 = c[11];
int rec2val3 = c[12];
/* int rec3val0 = c[13];
int rec3val1 = c[14];
int rec3val2 = c[15];
int rec3val3 = c[16];*/
int tmp = rec2val3*256*256*256 + rec2val2*256*256 + rec2val1*256 + rec2val0;
// This scale factor 1000, is dependent on the meter device.
// The full frame has info on this, here it is hardcoded to
// the domestic meter setting.
consumption = ((float)tmp) / ((float)1000);
} else
if (frame_type == 0x78) {
verbose("Full frame\n");
/* int rec1dif = c[3];
int rec1vif = c[4];
int rec1vife = c[5];
int rec1val0 = c[6];
int rec1val1 = c[7];
*/
/*int rec2dif = c[8];
int rec2vif = c[9];*/
int rec2val0 = c[10];
int rec2val1 = c[11];
int rec2val2 = c[12];
int rec2val3 = c[13];
/*
int rec3dif = c[14];
int rec3vif = c[15];
int rec3val0 = c[16];
int rec3val1 = c[17];
int rec3val2 = c[18];
int rec3val3 = c[19];
*/
int tmp = rec2val3*256*256*256 + rec2val2*256*256 + rec2val1*256 + rec2val0;
consumption = ((float)tmp) / ((float)1000);
} else {
printf("Unknown frame %02x\n", frame_type);
}
return consumption;
}

26
meters.cc 100644
Wyświetl plik

@ -0,0 +1,26 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"meters.h"
#include"wmbus.h"
#include<string>
#include<vector>

45
meters.h 100644
Wyświetl plik

@ -0,0 +1,45 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef METER_H_
#define METER_H_
#include"util.h"
#include"wmbus.h"
#include<string>
#include<vector>
using namespace std;
typedef unsigned char uchar;
struct Meter {
virtual string id() = 0;
virtual string name() = 0;
virtual float totalWaterConsumption() = 0;
virtual string datetimeOfUpdate() = 0;
virtual void onUpdate(function<void()> cb) = 0;
};
Meter *createMultical21(WMBus *bus, const char *name, const char *id, const char *key);
#endif

289
serial.cc 100644
Wyświetl plik

@ -0,0 +1,289 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"util.h"
#include"serial.h"
#include <fcntl.h>
#include <functional>
#include <memory.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
static int openSerialTTY(const char *tty, int baud_rate);
struct SerialDeviceTTY;
struct SerialCommunicationManagerImp : public SerialCommunicationManager {
SerialCommunicationManagerImp();
SerialDevice *createSerialDeviceTTY(string dev, int baud_rate);
void listenTo(SerialDevice *sd, function<void()> cb);
void stop();
void waitForStop();
bool isRunning();
void opened(SerialDeviceTTY *sd);
private:
void *eventLoop();
static void *startLoop(void *);
bool running_;
pthread_t thread_;
int max_fd_;
vector<SerialDevice*> devices_;
};
struct SerialDeviceImp : public SerialDevice {
private:
function<void()> on_data_;
friend struct SerialCommunicationManagerImp;
};
struct SerialDeviceTTY : public SerialDeviceImp {
SerialDeviceTTY(string device, int baud_rate, SerialCommunicationManagerImp *manager);
bool open();
void close();
bool send(vector<uchar> &data);
int receive(vector<uchar> *data);
int fd() { return fd_; }
SerialCommunicationManager *manager() { return manager_; }
private:
string device_;
int baud_rate_;
int fd_;
pthread_mutex_t write_lock_ = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t read_lock_ = PTHREAD_MUTEX_INITIALIZER;
SerialCommunicationManagerImp *manager_;
};
SerialDeviceTTY::SerialDeviceTTY(string device, int baud_rate,
SerialCommunicationManagerImp *manager) {
device_ = device;
baud_rate_ = baud_rate;
manager_ = manager;
}
bool SerialDeviceTTY::open() {
fd_ = openSerialTTY(device_.c_str(), baud_rate_);
if (fd_ == -1) {
error("Could not open %s with %d baud N81\n", device_.c_str(), baud_rate_);
}
manager_->opened(this);
return true;
}
void SerialDeviceTTY::close() {
::close(fd_);
}
bool SerialDeviceTTY::send(vector<uchar> &data)
{
if (data.size() == 0) return true;
pthread_mutex_lock(&write_lock_);
bool rc = true;
int n = data.size();
int written = 0;
while (true) {
int nw = write(fd_, &data[written], n-written);
if (nw > 0) written += nw;
if (nw < 0) {
if (errno==EINTR) continue;
rc = false;
goto end;
}
if (written == n) break;
}
end:
pthread_mutex_unlock(&write_lock_);
return rc;
}
int SerialDeviceTTY::receive(vector<uchar> *data)
{
pthread_mutex_lock(&read_lock_);
data->clear();
int available = 0;
int num_read = 0;
ioctl(fd_, FIONREAD, &available);
if (!available) goto end;
data->resize(available);
while (true) {
int nr = read(fd_, &((*data)[num_read]), available-num_read);
if (nr > 0) num_read += nr;
if (nr < 0) {
if (errno==EINTR) continue;
goto end;
}
if (num_read == available) break;
}
end:
pthread_mutex_unlock(&read_lock_);
return num_read;
}
SerialCommunicationManagerImp::SerialCommunicationManagerImp()
{
running_ = true;
max_fd_ = 0;
pthread_create(&thread_, NULL, startLoop, this);
//running_ = (rc == 0);
}
void *SerialCommunicationManagerImp::startLoop(void *a) {
auto t = (SerialCommunicationManagerImp*)a;
return t->eventLoop();
}
SerialDevice *SerialCommunicationManagerImp::createSerialDeviceTTY(string device, int baud_rate) {
SerialDevice *sd = new SerialDeviceTTY(device, baud_rate, this);
return sd;
}
void SerialCommunicationManagerImp::listenTo(SerialDevice *sd, function<void()> cb) {
SerialDeviceImp *si = dynamic_cast<SerialDeviceImp*>(sd);
si->on_data_ = cb;
}
void SerialCommunicationManagerImp::stop()
{
running_ = false;
}
void SerialCommunicationManagerImp::waitForStop()
{
while (running_) { usleep(1000*1000);}
pthread_kill(thread_, SIGUSR1);
pthread_join(thread_, NULL);
}
bool SerialCommunicationManagerImp::isRunning()
{
return running_;
}
void SerialCommunicationManagerImp::opened(SerialDeviceTTY *sd) {
max_fd_ = max(sd->fd(), max_fd_);
devices_.push_back(sd);
pthread_kill(thread_, SIGUSR1);
}
void *SerialCommunicationManagerImp::eventLoop() {
fd_set readfds;
while (running_) {
FD_ZERO(&readfds);
for (SerialDevice *d : devices_) {
FD_SET(d->fd(), &readfds);
}
int activity = select(max_fd_+1 , &readfds , NULL , NULL , NULL);
if (!running_) break;
if (activity < 0 && errno!=EINTR) {
error("Error after select! errno=%d\n", errno);
}
if (activity > 0) {
for (SerialDevice *d : devices_) {
if (FD_ISSET(d->fd(), &readfds)) {
SerialDeviceImp *si = dynamic_cast<SerialDeviceImp*>(d);
if (si->on_data_) si->on_data_();
}
}
}
}
verbose("Event loop stopped!\n");
return NULL;
}
SerialCommunicationManager *createSerialCommunicationManager()
{
return new SerialCommunicationManagerImp();
}
static int openSerialTTY(const char *tty, int baud_rate) {
int rc = 0;
speed_t speed = 0;
struct termios tios;
int fd = open(tty, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
if (fd == -1) {
usleep(1000*1000);
fd = open(tty, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
if (fd == -1) goto err;
}
switch (baud_rate) {
case 9600: speed = B9600; break;
case 19200: speed = B19200; break;
case 38400: speed = B38400; break;
case 57600: speed = B57600; break;
case 115200: speed = B115200;break;
default:
goto err;
}
memset(&tios, 0, sizeof(tios));
rc = cfsetispeed(&tios, speed);
if (rc < 0) goto err;
rc = cfsetospeed(&tios, speed);
if (rc < 0) goto err;
tios.c_cflag |= (CREAD | CLOCAL);
tios.c_cflag &= ~CSIZE;
tios.c_cflag |= CS8;
tios.c_cflag &=~ CSTOPB;
tios.c_cflag &=~ PARENB;
tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tios.c_iflag &= ~INPCK;
tios.c_iflag &= ~(IXON | IXOFF | IXANY);
tios.c_oflag &=~ OPOST;
tios.c_cc[VMIN] = 0;
tios.c_cc[VTIME] = 0;
rc = tcsetattr(fd, TCSANOW, &tios);
if (rc < 0) goto err;
return fd;
err:
if (fd != -1) close(fd);
return -1;
}

53
serial.h 100644
Wyświetl plik

@ -0,0 +1,53 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef SERIAL_H_
#define SERIAL_H_
#include"util.h"
#include<functional>
#include<string>
#include<vector>
using namespace std;
struct SerialCommunicationManager;
struct SerialDevice {
virtual bool open() = 0;
virtual void close() = 0;
virtual bool send(vector<uchar> &data) = 0;
virtual int receive(vector<uchar> *data) = 0;
virtual int fd() = 0;
virtual SerialCommunicationManager *manager() = 0;
};
struct SerialCommunicationManager {
virtual SerialDevice *createSerialDeviceTTY(string dev, int baud_rate) = 0;
virtual void listenTo(SerialDevice *sd, function<void()> cb) = 0;
virtual void stop() = 0;
virtual void waitForStop() = 0;
virtual bool isRunning() = 0;
};
SerialCommunicationManager *createSerialCommunicationManager();
#endif

128
util.cc 100644
Wyświetl plik

@ -0,0 +1,128 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"util.h"
#include<functional>
#include<signal.h>
#include<stdarg.h>
#include<stddef.h>
#include<string>
using namespace std;
function<void()> exit_handler;
void exitHandler(int signum)
{
if (exit_handler) exit_handler();
}
void doNothing(int signum) {
}
void onExit(function<void()> cb)
{
exit_handler = cb;
struct sigaction new_action, old_action;
new_action.sa_handler = exitHandler;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction (SIGINT, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) sigaction(SIGINT, &new_action, NULL);
sigaction (SIGHUP, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) sigaction (SIGHUP, &new_action, NULL);
sigaction (SIGTERM, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) sigaction (SIGTERM, &new_action, NULL);
new_action.sa_handler = doNothing;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction (SIGUSR1, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) sigaction(SIGUSR1, &new_action, NULL);
}
int char2int(char input)
{
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
return -1;
}
void hex2bin(const char* src, vector<uchar> *target)
{
if (!src) return;
while(*src && src[1]) {
if (*src == ' ') {
src++;
} else {
target->push_back(char2int(*src)*16 + char2int(src[1]));
src += 2;
}
}
}
char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B','C','D','E','F'};
std::string bin2hex(vector<uchar> &target) {
std::string str;
for (size_t i = 0; i < target.size(); ++i) {
const char ch = target[i];
str.append(&hex[(ch & 0xF0) >> 4], 1);
str.append(&hex[ch & 0xF], 1);
}
return str;
}
void xorit(uchar *srca, uchar *srcb, uchar *dest, int len)
{
for (int i=0; i<len; ++i) { dest[i] = srca[i]^srcb[i]; }
}
void error(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exitHandler(0);
exit(1);
}
bool verbose_enabled_ = false;
void verboseEnabled(bool b) {
verbose_enabled_ = b;
}
void verbose(const char* fmt, ...) {
if (verbose_enabled_) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
}

47
util.h 100644
Wyświetl plik

@ -0,0 +1,47 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef UTIL_H
#define UTIL_H
#include<signal.h>
#include<functional>
#include<vector>
void onExit(std::function<void()> cb);
typedef unsigned char uchar;
#define call(A,B) ([A](){A->B();})
#define calll(A,B,T) ([A](T t){A->B(t);})
void hex2bin(const char* src, std::vector<uchar> *target);
std::string bin2hex(std::vector<uchar> &target);
void xorit(uchar *srca, uchar *srcb, uchar *dest, int len);
void error(const char* fmt, ...);
void verbose(const char* fmt, ...);
void verboseEnabled(bool b);
#endif

28
wmbus.cc 100644
Wyświetl plik

@ -0,0 +1,28 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"wmbus.h"
const char *LinkModeNames[] = {
#define X(name) #name ,
LIST_OF_LINK_MODES
#undef X
};

69
wmbus.h 100644
Wyświetl plik

@ -0,0 +1,69 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef WMBUS_H
#define WMBUS_H
#include"serial.h"
#include"util.h"
#include<inttypes.h>
#define LIST_OF_LINK_MODES X(S1)X(S1m)X(S2)X(T1)X(T2)X(R2)X(C1a)X(C1b)X(C2a)X(C2b)\
X(N1A)X(N2A)X(N1B)X(N2B)X(N1C)X(N2C)X(N1D)X(N2D)X(N1E)X(N2E)X(N1F)X(N2F)X(UNKNOWN_LINKMODE)
enum LinkMode {
#define X(name) name,
LIST_OF_LINK_MODES
#undef X
};
using namespace std;
extern const char *LinkModeNames[];
struct Telegram {
int c_field; // 1 byte (0x44=telegram, no response expected!)
int m_field; // Manufacturer 2 bytes
vector<uchar> a_field; // A field 6 bytes
// The 6 a field bytes are composed of:
vector<uchar> a_field_address; // Address in BCD = 8 decimal 00000000...99999999 digits.
int a_field_version; // 1 byte
int a_field_device_type; // 1 byte
int ci_field; // 1 byte
vector<uchar> payload; // All payload data after the ci field byte.
// The id as written on the physical meter device.
string id() { return bin2hex(a_field_address); }
};
struct WMBus {
virtual bool ping() = 0;
virtual uint32_t getDeviceId() = 0;
virtual LinkMode getLinkMode() = 0;
virtual void setLinkMode(LinkMode lm) = 0;
virtual void onTelegram(function<void(Telegram*)> cb) = 0;
virtual SerialDevice *serial() = 0;
};
WMBus *openIM871A(string device, SerialCommunicationManager *handler);
#endif

421
wmbus_im871a.cc 100644
Wyświetl plik

@ -0,0 +1,421 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"wmbus.h"
#include"wmbus_im871a.h"
#include"serial.h"
#include<assert.h>
#include<pthread.h>
#include<semaphore.h>
using namespace std;
enum FrameStatus { PartialFrame, FullFrame, ErrorInFrame };
struct WMBusIM871A : public WMBus {
bool ping();
uint32_t getDeviceId();
LinkMode getLinkMode();
void setLinkMode(LinkMode lm);
void onTelegram(function<void(Telegram*)> cb);
void processMessage();
SerialDevice *serial() { return serial_; }
WMBusIM871A(SerialDevice *serial, SerialCommunicationManager *manager);
private:
SerialDevice *serial_;
SerialCommunicationManager *manager_;
vector<uchar> read_buffer_;
pthread_mutex_t command_lock_ = PTHREAD_MUTEX_INITIALIZER;
sem_t command_wait_;
int sent_command_;
int received_command_;
vector<uchar> received_payload_;
function<void(Telegram*)> on_telegram;
void waitForResponse();
FrameStatus checkFrame(vector<uchar> &data,
size_t *frame_length, int *endpoint_out, int *msgid_out, int *payload_len_out);
void handleDevMgmt(int msgid, vector<uchar> &payload);
void handleRadioLink(int msgid, vector<uchar> &payload);
void handleRadioLinkTest(int msgid, vector<uchar> &payload);
void handleHWTest(int msgid, vector<uchar> &payload);
};
WMBus *openIM871A(string device, SerialCommunicationManager *manager)
{
SerialDevice *serial = manager->createSerialDeviceTTY(device.c_str(), 57600);
WMBusIM871A *imp = new WMBusIM871A(serial, manager);
return imp;
}
WMBusIM871A::WMBusIM871A(SerialDevice *serial, SerialCommunicationManager *manager) :
serial_(serial), manager_(manager)
{
sem_init(&command_wait_, 0, 0);
manager_->listenTo(serial_,call(this,processMessage));
serial_->open();
}
bool WMBusIM871A::ping() {
pthread_mutex_lock(&command_lock_);
vector<uchar> msg(4);
msg[0] = WMBUS_SERIAL_SOF;
msg[1] = DEVMGMT_ID;
msg[2] = DEVMGMT_MSG_PING_REQ;
msg[3] = 0;
sent_command_ = DEVMGMT_MSG_PING_REQ;
serial()->send(msg);
waitForResponse();
pthread_mutex_unlock(&command_lock_);
return true;
}
uint32_t WMBusIM871A::getDeviceId() {
pthread_mutex_lock(&command_lock_);
vector<uchar> msg(4);
msg[0] = WMBUS_SERIAL_SOF;
msg[1] = DEVMGMT_ID;
msg[2] = DEVMGMT_MSG_GET_DEVICEINFO_REQ;
msg[3] = 0;
sent_command_ = DEVMGMT_MSG_GET_DEVICEINFO_REQ;
serial()->send(msg);
waitForResponse();
uint32_t id = 0;
if (received_command_ == DEVMGMT_MSG_GET_DEVICEINFO_RSP) {
verbose("Module Type %02x\n", received_payload_[0]);
verbose( "Device Mode %02x\n", received_payload_[1]);
verbose( "Firmware version %02x\n", received_payload_[2]);
verbose( "HCI Protocol version %02x\n", received_payload_[3]);
id = received_payload_[4] << 24 |
received_payload_[5] << 16 |
received_payload_[6] << 8 |
received_payload_[7];
verbose( "Device id %08x\n", id);
}
pthread_mutex_unlock(&command_lock_);
return id;
}
LinkMode WMBusIM871A::getLinkMode() {
pthread_mutex_lock(&command_lock_);
vector<uchar> msg(4);
msg[0] = WMBUS_SERIAL_SOF;
msg[1] = DEVMGMT_ID;
sent_command_ = msg[2] = DEVMGMT_MSG_GET_CONFIG_REQ;
msg[3] = 0;
serial()->send(msg);
waitForResponse();
LinkMode lm = UNKNOWN_LINKMODE;
if (received_command_ == DEVMGMT_MSG_GET_CONFIG_RSP) {
verbose( "Receivd config size %zu\n", received_payload_.size());
int iff1 = received_payload_[0];
bool has_device_mode = (iff1&1)==1;
bool has_link_mode = (iff1&2)==2;
bool has_wmbus_c_field = (iff1&4)==4;
bool has_wmbus_man_id = (iff1&8)==8;
bool has_wmbus_device_id = (iff1&16)==16;
bool has_wmbus_version = (iff1&32)==32;
bool has_wmbus_device_type = (iff1&64)==64;
bool has_radio_channel = (iff1&128)==128;
int offset = 1;
if (has_device_mode) {
verbose( "Device mode %02x\n", received_payload_[offset]);
offset++;
}
if (has_link_mode) {
verbose( "Link mode %02x\n", received_payload_[offset]);
lm = (LinkMode)(int)received_payload_[offset];
offset++;
}
if (has_wmbus_c_field) {
verbose( "WMBus C-field %02x\n", received_payload_[offset]);
offset++;
}
if (has_wmbus_man_id) {
verbose( "WMBus Manufacture id %02x%02x\n", received_payload_[offset+1], received_payload_[offset+0]);
offset+=2;
}
if (has_wmbus_device_id) {
verbose( "WMBus Device id %02x%02x%02x%02x\n", received_payload_[offset+3], received_payload_[offset+2],
received_payload_[offset+1], received_payload_[offset+0]);
offset+=4;
}
if (has_wmbus_version) {
verbose( "WMBus Version %02x\n", received_payload_[offset]);
offset++;
}
if (has_wmbus_device_type) {
verbose( "WMBus Device Type %02x\n", received_payload_[offset]);
offset++;
}
if (has_radio_channel) {
verbose( "Radio channel %02x\n", received_payload_[offset]);
offset++;
}
int iff2 = received_payload_[offset];
offset++;
bool has_radio_power_level = (iff2&1)==1;
bool has_radio_data_rate = (iff2&2)==2;
bool has_radio_rx_window = (iff2&4)==4;
bool has_auto_power_saving = (iff2&8)==8;
bool has_auto_rssi_attachment = (iff2&16)==16;
bool has_auto_rx_timestamp_attachment = (iff2&32)==32;
bool has_led_control = (iff2&64)==64;
bool has_rtc_control = (iff2&128)==128;
if (has_radio_power_level) {
verbose( "Radio power level %02x\n", received_payload_[offset]);
offset++;
}
if (has_radio_data_rate) {
verbose( "Radio data rate %02x\n", received_payload_[offset]);
offset++;
}
if (has_radio_rx_window) {
verbose( "Radio rx window %02x\n", received_payload_[offset]);
offset++;
}
if (has_auto_power_saving) {
verbose( "Auto power saving %02x\n", received_payload_[offset]);
offset++;
}
if (has_auto_rssi_attachment) {
verbose( "Auto RSSI attachment %02x\n", received_payload_[offset]);
offset++;
}
if (has_auto_rx_timestamp_attachment) {
verbose( "Auto rx timestamp attachment %02x\n", received_payload_[offset]);
offset++;
}
if (has_led_control) {
verbose( "LED control %02x\n", received_payload_[offset]);
offset++;
}
if (has_rtc_control) {
verbose( "RTC control %02x\n", received_payload_[offset]);
offset++;
}
}
pthread_mutex_unlock(&command_lock_);
return lm;
}
void WMBusIM871A::setLinkMode(LinkMode lm) {
pthread_mutex_lock(&command_lock_);
vector<uchar> msg(8);
msg[0] = WMBUS_SERIAL_SOF;
msg[1] = DEVMGMT_ID;
sent_command_ = msg[2] = DEVMGMT_MSG_SET_CONFIG_REQ;
msg[3] = 3; // Len
msg[4] = 0; // Temporary
msg[5] = 2; // iff1 bits: Set Radio Mode only
msg[6] = (int)lm; // C1a
msg[7] = 0; // iff2 bits: Set nothing
verbose( "SET CONFIG REQ to USB im871a\n\n");
serial()->send(msg);
waitForResponse();
pthread_mutex_unlock(&command_lock_);
}
void WMBusIM871A::onTelegram(function<void(Telegram*)> cb) {
on_telegram = cb;
}
void WMBusIM871A::waitForResponse() {
while (manager_->isRunning()) {
int rc = sem_wait(&command_wait_);
if (rc==0) break;
if (rc==-1) {
if (errno==EINTR) continue;
break;
}
}
}
FrameStatus WMBusIM871A::checkFrame(vector<uchar> &data,
size_t *frame_length, int *endpoint_out, int *msgid_out, int *payload_len_out)
{
if (data.size() == 0) return PartialFrame;
if (data[0] != 0xa5) return ErrorInFrame;
int ctrlbits = (data[1] & 0xf0) >> 4;
if (ctrlbits & 1) return ErrorInFrame; // Bit 1 is reserved, we do not expect it....
bool has_timestamp = ((ctrlbits&2)==2);
bool has_rssi = ((ctrlbits&4)==4);
bool has_crc16 = ((ctrlbits&8)==8);
int endpoint = data[1] & 0x0f;
if (endpoint != DEVMGMT_ID &&
endpoint != RADIOLINK_ID &&
endpoint != RADIOLINKTEST_ID &&
endpoint != HWTEST_ID) return ErrorInFrame;
*endpoint_out = endpoint;
int msgid = data[2];
if (endpoint == DEVMGMT_ID && (msgid<1 || msgid>0x27)) return ErrorInFrame;
if (endpoint == RADIOLINK_ID && (msgid<1 || msgid>0x05)) return ErrorInFrame;
if (endpoint == RADIOLINKTEST_ID && (msgid<1 || msgid>0x07)) return ErrorInFrame; // Are 5 and 6 disallowed?
if (endpoint == HWTEST_ID && (msgid<1 || msgid>0x02)) return ErrorInFrame;
*msgid_out = msgid;
int payload_len = data[3];
*payload_len_out = payload_len;
*frame_length = 4+payload_len+(has_timestamp?4:0)+(has_rssi?1:0)+(has_crc16?2:0);
if (data.size() < *frame_length) return PartialFrame;
return FullFrame;
}
void WMBusIM871A::processMessage() {
vector<uchar> data;
serial_->receive(&data);
read_buffer_.insert(read_buffer_.end(), data.begin(), data.end());
size_t frame_length;
int endpoint;
int msgid;
int payload_len;
FrameStatus status = checkFrame(read_buffer_, &frame_length, &endpoint, &msgid, &payload_len);
if (status == ErrorInFrame) {
verbose( "Protocol error in message received from iM871A!\n");
read_buffer_.clear();
} else
if (status == FullFrame) {
vector<uchar> payload;
if (payload_len > 0) {
payload.insert(payload.begin(), read_buffer_.begin()+4, read_buffer_.begin()+payload_len);
}
read_buffer_.erase(read_buffer_.begin(), read_buffer_.begin()+frame_length);
switch (endpoint) {
case DEVMGMT_ID: handleDevMgmt(msgid, payload); break;
case RADIOLINK_ID: handleRadioLink(msgid, payload); break;
case RADIOLINKTEST_ID: handleRadioLinkTest(msgid, payload); break;
case HWTEST_ID: handleHWTest(msgid, payload); break;
}
}
}
void WMBusIM871A::handleDevMgmt(int msgid, vector<uchar> &payload)
{
switch (msgid) {
case DEVMGMT_MSG_PING_RSP: // 0x02
verbose("Received ping response.\n");
received_command_ = msgid;
sem_post(&command_wait_);
break;
case DEVMGMT_MSG_SET_CONFIG_RSP: // 0x04
verbose("Received set device config response.\n");
received_command_ = msgid;
received_payload_.clear();
received_payload_.insert(received_payload_.end(), payload.begin(), payload.end());
sem_post(&command_wait_);
break;
case DEVMGMT_MSG_GET_CONFIG_RSP: // 0x06
verbose("Received get device config response.\n");
received_command_ = msgid;
received_payload_.clear();
received_payload_.insert(received_payload_.end(), payload.begin(), payload.end());
sem_post(&command_wait_);
break;
case DEVMGMT_MSG_GET_DEVICEINFO_RSP: // 0x10
verbose("Received device info response.\n");
received_command_ = msgid;
received_payload_.clear();
received_payload_.insert(received_payload_.end(), payload.begin(), payload.end());
sem_post(&command_wait_);
break;
default:
verbose("Unhandled device management message %d\n", msgid);
}
}
void WMBusIM871A::handleRadioLink(int msgid, vector<uchar> &payload)
{
switch (msgid) {
case RADIOLINK_MSG_WMBUSMSG_IND: // 0x03
{
Telegram t;
verbose("Radiolink received (%zu bytes): ", payload.size());
for (auto c : payload) verbose("%02x ", c);
verbose("\n");
t.c_field = payload[0];
t.m_field = payload[1]<<8 | payload[2];
t.a_field.resize(6);
t.a_field_address.resize(4);
for (int i=0; i<6; ++i) {
t.a_field[i] = payload[3+i];
if (i<4) { t.a_field_address[i] = payload[3+3-i]; }
}
t.a_field_version = payload[3+4];
t.a_field_device_type=payload[3+5];
t.ci_field=payload[9];
t.payload.clear();
t.payload.insert(t.payload.end(), payload.begin()+10, payload.end());
if (on_telegram) on_telegram(&t);
}
break;
default:
verbose("Unhandled radio link message %d\n", msgid);
}
}
void WMBusIM871A::handleRadioLinkTest(int msgid, vector<uchar> &payload)
{
switch (msgid) {
default:
verbose("Unhandled radio link test message %d\n", msgid);
}
}
void WMBusIM871A::handleHWTest(int msgid, vector<uchar> &payload)
{
switch (msgid) {
default:
verbose("Unhandled hw test message %d\n", msgid);
}
}

60
wmbus_im871a.h 100644
Wyświetl plik

@ -0,0 +1,60 @@
// Definitions and source code imported from WMBus_HCI_Spec_V1_6.pdf
// Found here: https://wireless-solutions.de/products/gateways/wirelessadapter.html
#define WMBUS_SERIAL_SOF 0xA5
#define DEVMGMT_ID 0x01
#define RADIOLINK_ID 0x02
#define RADIOLINKTEST_ID 0x03
#define HWTEST_ID 0x04
#define DEVMGMT_MSG_PING_REQ 0x01
#define DEVMGMT_MSG_PING_RSP 0x02
#define DEVMGMT_MSG_SET_CONFIG_REQ 0x03
#define DEVMGMT_MSG_SET_CONFIG_RSP 0x04
#define DEVMGMT_MSG_GET_CONFIG_REQ 0x05
#define DEVMGMT_MSG_GET_CONFIG_RSP 0x06
#define DEVMGMT_MSG_RESET_REQ 0x07
#define DEVMGMT_MSG_RESET_RSP 0x08
#define DEVMGMT_MSG_FACTORY_RESET_REQ 0x09
#define DEVMGMT_MSG_FACTORY_RESET_RSP 0x0A
#define DEVMGMT_MSG_GET_OPMODE_REQ 0x0B
#define DEVMGMT_MSG_GET_OPMODE_RSP 0x0C
#define DEVMGMT_MSG_SET_OPMODE_REQ 0x0D
#define DEVMGMT_MSG_SET_OPMODE_RSP 0x0E
#define DEVMGMT_MSG_GET_DEVICEINFO_REQ 0x0F
#define DEVMGMT_MSG_GET_DEVICEINFO_RSP 0x10
#define DEVMGMT_MSG_GET_SYSSTATUS_REQ 0x11
#define DEVMGMT_MSG_GET_SYSSTATUS_RSP 0x12
#define DEVMGMT_MSG_GET_FWINFO_REQ 0x13
#define DEVMGMT_MSG_GET_FWINFO_RSP 0x14
#define DEVMGMT_MSG_GET_RTC_REQ 0x19
#define DEVMGMT_MSG_GET_RTC_RSP 0x1A
#define DEVMGMT_MSG_SET_RTC_REQ 0x1B
#define DEVMGMT_MSG_SET_RTC_RSP 0x1C
#define DEVMGMT_MSG_ENTER_LPM_REQ 0x1D
#define DEVMGMT_MSG_ENTER_LPM_RSP 0x1E
#define DEVMGMT_MSG_SET_AES_ENCKEY_REQ 0x21
#define DEVMGMT_MSG_SET_AES_ENCKEY_RSP 0x22
#define DEVMGMT_MSG_ENABLE_AES_ENCKEY_REQ 0x23
#define DEVMGMT_MSG_ENABLE_AES_ENCKEY_RSP 0x24
#define DEVMGMT_MSG_SET_AES_DECKEY_REQ 0x25
#define DEVMGMT_MSG_SET_AES_DECKEY_RSP 0x26
#define DEVMGMT_MSG_AES_DEC_ERROR_IND 0x27
#define RADIOLINK_MSG_WMBUSMSG_REQ 0x01
#define RADIOLINK_MSG_WMBUSMSG_RSP 0x02
#define RADIOLINK_MSG_WMBUSMSG_IND 0x03
#define RADIOLINK_MSG_DATA_REQ 0x04
#define RADIOLINK_MSG_DATA_RSP 0x05
#define RADIOLINKTEST_MSG_START_REQ 0x01
#define RADIOLINKTEST_MSG_START_RSP 0x02
#define RADIOLINKTEST_MSG_STOP_REQ 0x03
#define RADIOLINKTEST_MSG_STOP_RSP 0x04
#define RADIOLINKTEST_MSG_STATUS_IND 0x07
#define HWTEST_MSG_RADIOTEST_REQ 0x01
#define HWTEST_MSG_RADIOTEST_RSP 0x02