Merge pull request #50 from chester4444/CULsupported

CULsupported
pull/52/head
Fredrik Öhrström 2019-11-23 12:35:27 +01:00 zatwierdzone przez GitHub
commit 4af4d0e49e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
8 zmienionych plików z 376 dodań i 7 usunięć

Wyświetl plik

@ -113,6 +113,7 @@ METER_OBJS:=\
$(BUILD)/wmbus.o \
$(BUILD)/wmbus_amb8465.o \
$(BUILD)/wmbus_im871a.o \
$(BUILD)/wmbus_cul.o \
$(BUILD)/wmbus_rtlwmbus.o \
$(BUILD)/wmbus_simulator.o \
$(BUILD)/wmbus_rawtty.o \

Wyświetl plik

@ -131,7 +131,7 @@ auto, to have wmbusmeters look for the links /dev/im871a, /dev/amb8465, /dev/rfm
/dev/ttyUSB0:amb8465, if you have an amb8465 dongle assigned to ttyUSB0. Other suffixes are im871a,rfmrx2.
/dev/ttyUSB0, to have wmbusmeters auto-detect amb8465 or im871a.
/dev/ttyUSB0, to have wmbusmeters auto-detect amb8465, im871a or CUL device.
/dev/ttyUSB0:38400, to have wmbusmeters set the baud rate to 38400 and listen for raw wmbus telegrams.
@ -166,6 +166,7 @@ IMST 871a
Amber 8465
BMeters RFM-RX2
rtl_sdr|rtl_wmbus
CUL family
Supported water meters:
Kamstrup Multical 21 (multical21)

Wyświetl plik

@ -212,6 +212,12 @@ bool startUsingCommandline(Configuration *config)
std::move(serial_override));
break;
}
case DEVICE_CUL:
{
verbose("(cul) on %s\n", settings.devicefile.c_str());
wmbus = openCUL(settings.devicefile, manager.get(), std::move(serial_override));
break;
}
case DEVICE_UNKNOWN:
warning("No wmbus device found! Exiting!\n");
if (config->daemon) {

Wyświetl plik

@ -346,6 +346,11 @@ void MeterMultical21::processContent(Telegram *t)
hex2bin("02FF2004134413A1015B8101E7FF0F", &format_bytes);
debug("(%s) using hard coded format for hash 61eb\n", meter_name.c_str());
}
else if (format_signature == 0xd2f7)
{
hex2bin("02FF2004134413615B5167", &format_bytes);
debug("(%s) using hard coded format for hash d2f7\n", meter_name.c_str());
}
else
{
verbose("(%s) ignoring telegram since format signature hash 0x%02x is yet unknown.\n",

Wyświetl plik

@ -412,6 +412,7 @@ bool detectIM871A(string device, SerialCommunicationManager *handler);
bool detectAMB8465(string device, SerialCommunicationManager *handler);
bool detectRawTTY(string device, int baud, SerialCommunicationManager *handler);
bool detectRTLSDR(string device, SerialCommunicationManager *handler);
bool detectCUL(string device, SerialCommunicationManager *handler);
Detected detectAuto(string devicefile,
string suffix,
@ -478,13 +479,27 @@ Detected detectAuto(string devicefile,
}
}
if (detectCUL("/dev/ttyUSB0", handler))
{
return { DEVICE_CUL, "/dev/ttyUSB0" };
}
else
{
AccessCheck ac = checkIfExistsAndSameGroup("/dev/ttyUSB0");
if (ac == AccessCheck::NotSameGroup)
{
// The device exists but we cannot read it!
error("You are not in the same group as the device CUL\n");
}
}
// We could not auto-detect any device.
return { DEVICE_UNKNOWN, "", false };
}
Detected detectImstOrAmber(string devicefile,
string suffix,
SerialCommunicationManager *handler)
Detected detectImstAmberCul(string devicefile,
string suffix,
SerialCommunicationManager *handler)
{
// If im87a is tested first, a delay of 1s must be inserted
// before amb8465 is tested, lest it will not respond properly.
@ -505,6 +520,12 @@ Detected detectImstOrAmber(string devicefile,
{
return { DEVICE_IM871A, devicefile, false };
}
// Talk CUL with it...
// assumes this device is configured for 38400 bps, which seems to be the default.
if (detectCUL(devicefile, handler))
{
return { DEVICE_CUL, devicefile, false };
}
// We could not auto-detect either.
return { DEVICE_UNKNOWN, "", false };
@ -573,6 +594,7 @@ Detected detectWMBusDeviceSetting(string devicefile,
if (suffix == "im871a") return { DEVICE_IM871A, devicefile, 0, override_tty };
if (suffix == "rfmrx2") return { DEVICE_RFMRX2, devicefile, 0, override_tty };
if (suffix == "rtlwmbus") return { DEVICE_RTLWMBUS, devicefile, 0, override_tty };
if (suffix == "cul") return { DEVICE_CUL, devicefile, 0, override_tty };
if (suffix == "simulation") return { DEVICE_SIMULATOR, devicefile, 0, override_tty };
// If the suffix is a number, then assume that it is a baud rate.
@ -588,8 +610,8 @@ Detected detectWMBusDeviceSetting(string devicefile,
// Ok, we are left with a single /dev/ttyUSB0 lets talk to it
// to figure out what is connected to it. We currently only
// know how to detect Imst or Amber dongles.
return detectImstOrAmber(devicefile, suffix, handler);
// know how to detect Imst, Amber or CUL dongles.
return detectImstAmberCul(devicefile, suffix, handler);
}

Wyświetl plik

@ -202,7 +202,7 @@ struct WMBus {
virtual ~WMBus() = 0;
};
#define LIST_OF_MBUS_DEVICES X(DEVICE_IM871A)X(DEVICE_AMB8465)X(DEVICE_RFMRX2)X(DEVICE_SIMULATOR)X(DEVICE_RTLWMBUS)X(DEVICE_RAWTTY)X(DEVICE_UNKNOWN)
#define LIST_OF_MBUS_DEVICES X(DEVICE_CUL)X(DEVICE_IM871A)X(DEVICE_AMB8465)X(DEVICE_RFMRX2)X(DEVICE_SIMULATOR)X(DEVICE_RTLWMBUS)X(DEVICE_RAWTTY)X(DEVICE_UNKNOWN)
enum MBusDeviceType {
#define X(name) name,
@ -232,6 +232,8 @@ unique_ptr<WMBus> openRawTTY(string device, int baudrate, SerialCommunicationMan
unique_ptr<SerialDevice> serial_override);
unique_ptr<WMBus> openRTLWMBUS(string device, SerialCommunicationManager *manager, std::function<void()> on_exit,
unique_ptr<SerialDevice> serial_override);
unique_ptr<WMBus> openCUL(string device, SerialCommunicationManager *manager,
unique_ptr<SerialDevice> serial_override);
unique_ptr<WMBus> openSimulator(string file, SerialCommunicationManager *manager,
unique_ptr<SerialDevice> serial_override);

328
src/wmbus_cul.cc 100644
Wyświetl plik

@ -0,0 +1,328 @@
/*
Copyright (C) 2019 Fredrik Öhrström
This program 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
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"wmbus.h"
#include"wmbus_cul.h"
#include"serial.h"
#include<assert.h>
#include<fcntl.h>
#include<grp.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#include<sys/errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
enum FrameStatus { PartialFrame, FullFrame, ErrorInFrame, TextAndNotFrame };
struct WMBusCUL : public WMBus
{
bool ping();
uint32_t getDeviceId();
LinkModeSet getLinkModes();
void setLinkModes(LinkModeSet lms);
LinkModeSet supportedLinkModes() {
return
C1_bit |
T1_bit;
}
int numConcurrentLinkModes() { return 2; }
bool canSetLinkModes(LinkModeSet lms)
{
if (!supportedLinkModes().supports(lms)) return false;
// The cul listens to both modes always.
return true;
}
void onTelegram(function<void(Telegram*)> cb);
void processSerialData();
SerialDevice *serial() { return serial_.get(); }
void simulate();
WMBusCUL(unique_ptr<SerialDevice> serial, SerialCommunicationManager *manager);
~WMBusCUL() { }
private:
unique_ptr<SerialDevice> serial_;
SerialCommunicationManager *manager_;
vector<uchar> read_buffer_;
vector<uchar> received_payload_;
vector<function<void(Telegram*)>> telegram_listeners_;
FrameStatus checkCULFrame(vector<uchar> &data,
size_t *hex_frame_length,
int *hex_payload_len_out,
int *hex_payload_offset);
void handleMessage(vector<uchar> &frame);
string setup_;
};
unique_ptr<WMBus> openCUL(string device, SerialCommunicationManager *manager, unique_ptr<SerialDevice> serial_override)
{
if (serial_override)
{
WMBusCUL *imp = new WMBusCUL(std::move(serial_override), manager);
return unique_ptr<WMBus>(imp);
}
auto serial = manager->createSerialDeviceTTY(device.c_str(), 38400);
WMBusCUL *imp = new WMBusCUL(std::move(serial), manager);
return unique_ptr<WMBus>(imp);
}
WMBusCUL::WMBusCUL(unique_ptr<SerialDevice> serial, SerialCommunicationManager *manager) :
serial_(std::move(serial)), manager_(manager)
{
manager_->listenTo(serial_.get(),call(this,processSerialData));
serial_->open(true);
}
bool WMBusCUL::ping()
{
verbose("(cul) ping\n");
return true;
}
uint32_t WMBusCUL::getDeviceId()
{
verbose("(cul) getDeviceId\n");
return 0x11111111;
}
LinkModeSet WMBusCUL::getLinkModes()
{
return Any_bit;
}
void WMBusCUL::setLinkModes(LinkModeSet lm)
{
verbose("(cul) setLinkModes\n");
// 'brc' command: b - wmbus, r - receive, c - c mode (with t)
vector<uchar> msg(5);
msg[0] = 'b';
msg[1] = 'r';
msg[2] = 'c';
msg[3] = 0xa;
msg[4] = 0xd;
serial()->send(msg);
usleep(1000*100);
// TODO: CUL should answer with "CMODE" - check this
// X01 - start the receiver
msg[0] = 'X';
msg[1] = '0';
msg[2] = '1';
msg[3] = 0xa;
msg[4] = 0xd;
serial()->send(msg);
usleep(1000*100);
}
void WMBusCUL::onTelegram(function<void(Telegram*)> cb) {
telegram_listeners_.push_back(cb);
}
void WMBusCUL::simulate()
{
}
void WMBusCUL::processSerialData()
{
vector<uchar> data;
//verbose("(cul) processSerialData 1\n");
// Receive and accumulated serial data until a full frame has been received.
serial_->receive(&data);
read_buffer_.insert(read_buffer_.end(), data.begin(), data.end());
size_t frame_length;
int hex_payload_len, hex_payload_offset;
//verbose("(cul) processSerialData 2\n");
for (;;)
{
FrameStatus status = checkCULFrame(read_buffer_, &frame_length, &hex_payload_len, &hex_payload_offset);
if (status == PartialFrame)
{
break;
}
if (status == TextAndNotFrame)
{
// The buffer has already been printed by serial cmd.
read_buffer_.clear();
break;
}
if (status == ErrorInFrame)
{
debug("(cul) error in received message.\n");
string msg = bin2hex(read_buffer_);
read_buffer_.clear();
break;
}
if (status == FullFrame)
{
vector<uchar> payload;
if (hex_payload_len > 0)
{
vector<uchar> hex;
hex.insert(hex.end(), read_buffer_.begin()+hex_payload_offset, read_buffer_.begin()+hex_payload_offset+hex_payload_len);
bool ok = hex2bin(hex, &payload);
if (!ok)
{
if (hex.size() % 2 == 1)
{
payload.clear();
warning("(cul) warning: the hex string is not an even multiple of two! Dropping last char.\n");
hex.pop_back();
ok = hex2bin(hex, &payload);
}
if (!ok)
{
warning("(cul) warning: the hex string contains bad characters! Decode stopped partway.\n");
}
}
}
read_buffer_.erase(read_buffer_.begin(), read_buffer_.begin()+frame_length);
handleMessage(payload);
}
}
}
void WMBusCUL::handleMessage(vector<uchar> &frame)
{
Telegram t;
bool ok = t.parse(frame);
if (ok)
{
bool handled = false;
for (auto f : telegram_listeners_)
{
Telegram copy = t;
if (f) f(&copy);
if (copy.handled) handled = true;
}
if (isVerboseEnabled() && !handled)
{
verbose("(cul) telegram ignored by all configured meters!\n");
}
}
}
FrameStatus WMBusCUL::checkCULFrame(vector<uchar> &data,
size_t *hex_frame_length,
int *hex_payload_len_out,
int *hex_payload_offset)
{
if (data.size() == 0) return PartialFrame;
size_t eolp = 0;
// Look for end of line
for (; eolp < data.size(); ++eolp) {
if (data[eolp] == '\n') break;
}
if (eolp >= data.size()) return PartialFrame;
// We got a full line, but if it is too short, then
// there is something wrong. Discard the data.
if (data.size() < 10) return ErrorInFrame;
if (data[0] != 'b') {
// C1 and T1 telegrams should start with a 'b'
return ErrorInFrame;
}
if (data[1] != 'Y') {
verbose("(cul) T1 telegrams currently not supported\n");
return ErrorInFrame;
}
// we received a full C1 frame, TODO check len
// skip the crc bytes adjusting the length byte by 2
data[3] -= 2;
// remove 8: 2 ('bY') + 4 (CRC) + 2 (CRLF) and start at 2
*hex_frame_length = data.size();
*hex_payload_len_out = data.size()-8;
*hex_payload_offset = 2;
debug("(cul) got full frame\n");
return FullFrame;
}
bool detectCUL(string device, SerialCommunicationManager *manager)
{
// Talk to the device and expect a very specific answer.
auto serial = manager->createSerialDeviceTTY(device.c_str(), 38400);
bool ok = serial->open(false);
if (!ok) return false;
vector<uchar> data;
// send '-'+CRLF -> should be an unsupported command for CUL
// it should respond with "? (- is unknown) Use one of ..."
vector<uchar> crlf(3);
crlf[0] = '-';
crlf[1] = 0x0d;
crlf[2] = 0x0a;
serial->send(crlf);
usleep(1000*100);
serial->receive(&data);
if (data[0] != '?') {
// no CUL device detected
serial->close();
return false;
}
data.clear();
// get the version string: "V 1.67 nanoCUL868" or similar
vector<uchar> msg(3);
msg[0] = CMD_GET_VERSION;
msg[1] = 0x0a;
msg[2] = 0x0d;
verbose("(cul) are you there?\n");
serial->send(msg);
// Wait for 200ms so that the USB stick have time to prepare a response.
usleep(1000*200);
serial->receive(&data);
string strC(data.begin(), data.end());
verbose("CUL answered: %s", strC.c_str());
// TODO: check version string somehow
serial->close();
return true;
}

4
src/wmbus_cul.h 100644
Wyświetl plik

@ -0,0 +1,4 @@
// Defines for the CUL family
#define CMD_GET_VERSION 'V'