From 1e09bd52f4b3c16aae888152f99c8b9b50a36c02 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 5 Aug 2011 01:28:47 +0200 Subject: [PATCH] added first draft od SML support --- misc/controller/vzlogger/src/main.c | 3 +- misc/controller/vzlogger/src/protocols/sml.c | 115 +++++++++++++++++++ misc/controller/vzlogger/src/protocols/sml.h | 46 ++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 misc/controller/vzlogger/src/protocols/sml.c create mode 100644 misc/controller/vzlogger/src/protocols/sml.h diff --git a/misc/controller/vzlogger/src/main.c b/misc/controller/vzlogger/src/main.c index 19ee1d1..393fd86 100644 --- a/misc/controller/vzlogger/src/main.c +++ b/misc/controller/vzlogger/src/main.c @@ -49,9 +49,10 @@ */ static protocol_t protocols[] = { {"1wire", "Dallas 1-Wire sensors (via OWFS)", onewire_get, onewire_init, onewire_close, MODE_SENSOR}, - {"obis", "Plaintext OBIS", obis_get, obis_init, obis_close, MODE_SENSOR}, +// {"obis", "Plaintext OBIS", obis_get, obis_init, obis_close, MODE_SENSOR}, {"random", "Random walk", random_get, random_init, random_close, MODE_SENSOR}, {"rawS0", "S0 on RS232", rawS0_get, rawS0_init, rawS0_close, MODE_METER}, +// {"sml", "Smart Meter Language", sml_get, sml_init, sml_close, MODE_SENSOR}, // {"fluksousb", "FluksoUSB board", flukso_get, flukso_init, flukso_close, MODE_SENSOR}, {NULL} /* stop condition for iterator */ }; diff --git a/misc/controller/vzlogger/src/protocols/sml.c b/misc/controller/vzlogger/src/protocols/sml.c new file mode 100644 index 0000000..45d91a8 --- /dev/null +++ b/misc/controller/vzlogger/src/protocols/sml.c @@ -0,0 +1,115 @@ +/** + * Wrapper around libsml + * + * @package vzlogger + * @copyright Copyright (c) 2011, The volkszaehler.org project + * @copyright Copyright (c) 2011, DAI-Labor, TU-Berlin + * @license http://www.gnu.org/licenses/gpl.txt GNU Public License + * @author Steffen Vogel + * @author Juri Glass + * @author Mathias Runge + * @author Nadim El Sayed + */ +/* + * This file is part of volkzaehler.org + * + * volkzaehler.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * volkzaehler.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with volkszaehler.org. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +int sml_init(char *device) { + sml_state_t *state; + + /* initialize handle */ + state = malloc(sizeof(sml_state_t)); + + /* this example assumes that a EDL21 meter sending SML messages via a + * serial device. Adjust as needed. */ + int fd = serial_port_open(device); + + if (fd > 0) { + // start thread + + /* listen on the serial device, this call is blocking */ + sml_transport_listen(fd, &transport_receiver); + close(fd); + } +} + +void sml_close(void *handle) { + +} + +reading_t sml_get(void *handle) { + +} + +void sml_transport_receiver(unsigned char *buffer, size_t buffer_len) { + // the buffer contains the whole message, with transport escape sequences. + // these escape sequences are stripped here. + sml_file *file = sml_file_parse(buffer + 8, buffer_len - 16); + // the sml file is parsed now + + // read here some values .. + + // this prints some information about the file + sml_file_print(file); + + // free the malloc'd memory + sml_file_free(file); +} + +int sml_open_port(char *device) { + int bits; + struct termios config; + memset(&config, 0, sizeof(config)); + + int fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); + if (fd < 0) { + printf("error: open(%s): %s\n", device, strerror(errno)); + return -1; + } + + // set RTS + ioctl(fd, TIOCMGET, &bits); + bits |= TIOCM_RTS; + ioctl(fd, TIOCMSET, &bits); + + tcgetattr( fd, &config ) ; + + // set 8-N-1 + config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + config.c_oflag &= ~OPOST; + config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + config.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB); + config.c_cflag |= CS8; + + // set speed to 9600 baud + cfsetispeed( &config, B9600); + cfsetospeed( &config, B9600); + + tcsetattr(fd, TCSANOW, &config); + return fd; +} diff --git a/misc/controller/vzlogger/src/protocols/sml.h b/misc/controller/vzlogger/src/protocols/sml.h new file mode 100644 index 0000000..fcfe3dd --- /dev/null +++ b/misc/controller/vzlogger/src/protocols/sml.h @@ -0,0 +1,46 @@ +/** + * Wrapper around libsml + * + * @package vzlogger + * @copyright Copyright (c) 2011, The volkszaehler.org project + * @copyright Copyright (c) 2011, DAI-Labor, TU-Berlin + * @license http://www.gnu.org/licenses/gpl.txt GNU Public License + * @author Steffen Vogel + * @author Juri Glass + * @author Mathias Runge + * @author Nadim El Sayed + */ +/* + * This file is part of volkzaehler.org + * + * volkzaehler.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * volkzaehler.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with volkszaehler.org. If not, see . + */ + +#ifndef _SML_H_ +#define _SML_H_ + +#include "../protocol.h" + +typedef struct { + int fd; + // termios etc.. +} sml_state_t; + +void * sml_init(char *port); +void sml_close(void *handle); +reading_t sml_get(void *handle); +int sml_open_port(char *device); +void sml_transport_receiver(unsigned char *buffer, size_t buffer_len); + +#endif /* _SML_H_ */