2019-11-22 17:50:55 +00:00
|
|
|
/*
|
2020-01-27 08:29:40 +00:00
|
|
|
Copyright (C) 2019-2020 Fredrik Öhrström
|
2019-11-22 17:50:55 +00:00
|
|
|
|
|
|
|
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"
|
2020-01-27 08:29:40 +00:00
|
|
|
#include"wmbus_utils.h"
|
2019-11-22 17:50:55 +00:00
|
|
|
#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>
|
2020-05-11 09:59:47 +00:00
|
|
|
#include<errno.h>
|
2019-11-22 17:50:55 +00:00
|
|
|
#include<sys/stat.h>
|
|
|
|
#include<sys/types.h>
|
|
|
|
#include<unistd.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2020-02-03 17:20:03 +00:00
|
|
|
#define SET_LINK_MODE 1
|
|
|
|
#define SET_X01_MODE 2
|
|
|
|
|
2020-01-27 08:29:40 +00:00
|
|
|
struct WMBusCUL : public virtual WMBusCommonImplementation
|
2019-11-22 17:50:55 +00:00
|
|
|
{
|
|
|
|
bool ping();
|
|
|
|
uint32_t getDeviceId();
|
|
|
|
LinkModeSet getLinkModes();
|
2020-08-01 19:56:46 +00:00
|
|
|
void deviceReset();
|
|
|
|
void deviceSetLinkModes(LinkModeSet lms);
|
2019-11-22 17:50:55 +00:00
|
|
|
LinkModeSet supportedLinkModes() {
|
|
|
|
return
|
|
|
|
C1_bit |
|
2020-02-03 17:20:03 +00:00
|
|
|
S1_bit |
|
2019-11-22 17:50:55 +00:00
|
|
|
T1_bit;
|
|
|
|
}
|
2020-02-02 19:49:26 +00:00
|
|
|
int numConcurrentLinkModes() { return 1; }
|
2019-11-22 17:50:55 +00:00
|
|
|
bool canSetLinkModes(LinkModeSet lms)
|
|
|
|
{
|
2020-02-02 19:49:26 +00:00
|
|
|
if (0 == countSetBits(lms.bits())) return false;
|
2019-11-22 17:50:55 +00:00
|
|
|
if (!supportedLinkModes().supports(lms)) return false;
|
2020-02-02 19:49:26 +00:00
|
|
|
// Ok, the supplied link modes are compatible,
|
|
|
|
// but im871a can only listen to one at a time.
|
|
|
|
return 1 == countSetBits(lms.bits());
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
void processSerialData();
|
|
|
|
void simulate();
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
WMBusCUL(shared_ptr<SerialDevice> serial, shared_ptr<SerialCommunicationManager> manager);
|
2019-11-22 17:50:55 +00:00
|
|
|
~WMBusCUL() { }
|
|
|
|
|
|
|
|
private:
|
2020-08-01 19:56:46 +00:00
|
|
|
|
2020-02-02 19:49:26 +00:00
|
|
|
LinkModeSet link_modes_ {};
|
2019-11-22 17:50:55 +00:00
|
|
|
vector<uchar> read_buffer_;
|
|
|
|
vector<uchar> received_payload_;
|
2020-02-03 17:20:03 +00:00
|
|
|
string sent_command_;
|
|
|
|
string received_response_;
|
|
|
|
|
2019-11-22 17:50:55 +00:00
|
|
|
FrameStatus checkCULFrame(vector<uchar> &data,
|
2020-02-15 20:18:21 +00:00
|
|
|
size_t *hex_frame_length,
|
|
|
|
vector<uchar> &payload);
|
2019-11-22 17:50:55 +00:00
|
|
|
|
|
|
|
string setup_;
|
|
|
|
};
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
shared_ptr<WMBus> openCUL(string device, shared_ptr<SerialCommunicationManager> manager, shared_ptr<SerialDevice> serial_override)
|
2019-11-22 17:50:55 +00:00
|
|
|
{
|
|
|
|
if (serial_override)
|
|
|
|
{
|
2020-09-21 19:55:21 +00:00
|
|
|
WMBusCUL *imp = new WMBusCUL(serial_override, manager);
|
|
|
|
return shared_ptr<WMBus>(imp);
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto serial = manager->createSerialDeviceTTY(device.c_str(), 38400);
|
2020-09-21 19:55:21 +00:00
|
|
|
WMBusCUL *imp = new WMBusCUL(serial, manager);
|
|
|
|
return shared_ptr<WMBus>(imp);
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
WMBusCUL::WMBusCUL(shared_ptr<SerialDevice> serial, shared_ptr<SerialCommunicationManager> manager) :
|
|
|
|
WMBusCommonImplementation(DEVICE_CUL, manager, serial)
|
2019-11-22 17:50:55 +00:00
|
|
|
{
|
2020-08-01 19:56:46 +00:00
|
|
|
reset();
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WMBusCUL::ping()
|
|
|
|
{
|
|
|
|
verbose("(cul) ping\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t WMBusCUL::getDeviceId()
|
|
|
|
{
|
|
|
|
verbose("(cul) getDeviceId\n");
|
|
|
|
return 0x11111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkModeSet WMBusCUL::getLinkModes()
|
|
|
|
{
|
2020-02-02 19:49:26 +00:00
|
|
|
return link_modes_;
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 19:56:46 +00:00
|
|
|
void WMBusCUL::deviceReset()
|
|
|
|
{
|
|
|
|
// No device specific settings needed right now.
|
|
|
|
// The common code in wmbus.cc reset()
|
|
|
|
// will open the serial device and potentially
|
|
|
|
// set the link modes properly.
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMBusCUL::deviceSetLinkModes(LinkModeSet lms)
|
2019-11-22 17:50:55 +00:00
|
|
|
{
|
2020-08-01 19:56:46 +00:00
|
|
|
if (serial()->readonly()) return; // Feeding from stdin or file.
|
2020-02-15 20:18:21 +00:00
|
|
|
|
2020-02-02 19:49:26 +00:00
|
|
|
if (!canSetLinkModes(lms))
|
|
|
|
{
|
|
|
|
string modes = lms.hr();
|
|
|
|
error("(cul) setting link mode(s) %s is not supported\n", modes.c_str());
|
|
|
|
}
|
2019-11-22 17:50:55 +00:00
|
|
|
// 'brc' command: b - wmbus, r - receive, c - c mode (with t)
|
|
|
|
vector<uchar> msg(5);
|
|
|
|
msg[0] = 'b';
|
|
|
|
msg[1] = 'r';
|
2020-02-02 19:49:26 +00:00
|
|
|
if (lms.has(LinkMode::C1)) {
|
|
|
|
msg[2] = 'c';
|
|
|
|
} else if (lms.has(LinkMode::S1)) {
|
|
|
|
msg[2] = 's';
|
|
|
|
} else if (lms.has(LinkMode::T1)) {
|
|
|
|
msg[2] = 't';
|
|
|
|
}
|
2019-11-22 17:50:55 +00:00
|
|
|
msg[3] = 0xa;
|
|
|
|
msg[4] = 0xd;
|
2020-01-27 08:29:40 +00:00
|
|
|
|
2020-02-02 19:49:26 +00:00
|
|
|
verbose("(cul) set link mode %c\n", msg[2]);
|
2020-02-03 17:20:03 +00:00
|
|
|
sent_command_ = string(&msg[0], &msg[3]);
|
|
|
|
received_response_ = "";
|
2020-02-04 13:46:48 +00:00
|
|
|
bool sent = serial()->send(msg);
|
2020-02-02 19:49:26 +00:00
|
|
|
|
2020-02-03 17:20:03 +00:00
|
|
|
if (sent) waitForResponse();
|
|
|
|
|
|
|
|
sent_command_ = "";
|
|
|
|
debug("(cul) received \"%s\"", received_response_.c_str());
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
if (lms.has(LinkMode::C1)) {
|
|
|
|
if (received_response_ != "CMODE") ok = false;
|
|
|
|
} else if (lms.has(LinkMode::S1)) {
|
|
|
|
if (received_response_ != "SMODE") ok = false;
|
|
|
|
} else if (lms.has(LinkMode::T1)) {
|
|
|
|
if (received_response_ != "TMODE") ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
string modes = lms.hr();
|
|
|
|
error("(cul) setting link mode(s) %s is not supported for this cul device!\n", modes.c_str());
|
2020-02-02 19:49:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:50:55 +00:00
|
|
|
// X01 - start the receiver
|
|
|
|
msg[0] = 'X';
|
|
|
|
msg[1] = '0';
|
|
|
|
msg[2] = '1';
|
|
|
|
msg[3] = 0xa;
|
|
|
|
msg[4] = 0xd;
|
2020-01-27 08:29:40 +00:00
|
|
|
|
2020-02-02 19:49:26 +00:00
|
|
|
sent = serial()->send(msg);
|
2020-02-03 17:20:03 +00:00
|
|
|
|
|
|
|
// Any response here, or does it silently move into listening mode?
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:50:55 +00:00
|
|
|
void WMBusCUL::simulate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-03 17:20:03 +00:00
|
|
|
string expectedResponses(vector<uchar> &data)
|
|
|
|
{
|
|
|
|
string safe = safeString(data);
|
|
|
|
if (safe.find("CMODE") != string::npos) return "CMODE";
|
|
|
|
if (safe.find("TMODE") != string::npos) return "TMODE";
|
|
|
|
if (safe.find("SMODE") != string::npos) return "SMODE";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:50:55 +00:00
|
|
|
void WMBusCUL::processSerialData()
|
|
|
|
{
|
|
|
|
vector<uchar> data;
|
|
|
|
|
|
|
|
// Receive and accumulated serial data until a full frame has been received.
|
2020-08-01 19:56:46 +00:00
|
|
|
serial()->receive(&data);
|
2019-11-22 17:50:55 +00:00
|
|
|
read_buffer_.insert(read_buffer_.end(), data.begin(), data.end());
|
|
|
|
|
|
|
|
size_t frame_length;
|
2020-02-15 20:18:21 +00:00
|
|
|
vector<uchar> payload;
|
2019-11-22 17:50:55 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2020-02-15 20:18:21 +00:00
|
|
|
FrameStatus status = checkCULFrame(read_buffer_, &frame_length, payload);
|
2019-11-22 17:50:55 +00:00
|
|
|
|
|
|
|
if (status == PartialFrame)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status == TextAndNotFrame)
|
|
|
|
{
|
|
|
|
// The buffer has already been printed by serial cmd.
|
2020-02-03 17:20:03 +00:00
|
|
|
if (sent_command_ != "")
|
|
|
|
{
|
|
|
|
string r = expectedResponses(read_buffer_);
|
|
|
|
if (r != "")
|
|
|
|
{
|
|
|
|
received_response_ = r;
|
|
|
|
sem_post(&command_wait_);
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 17:50:55 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
read_buffer_.erase(read_buffer_.begin(), read_buffer_.begin()+frame_length);
|
|
|
|
|
2020-01-27 08:29:40 +00:00
|
|
|
handleTelegram(payload);
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FrameStatus WMBusCUL::checkCULFrame(vector<uchar> &data,
|
2020-02-03 17:20:03 +00:00
|
|
|
size_t *hex_frame_length,
|
2020-02-15 20:18:21 +00:00
|
|
|
vector<uchar> &payload)
|
2019-11-22 17:50:55 +00:00
|
|
|
{
|
|
|
|
if (data.size() == 0) return PartialFrame;
|
2020-02-06 18:01:48 +00:00
|
|
|
|
2020-02-15 20:18:21 +00:00
|
|
|
if (isDebugEnabled())
|
|
|
|
{
|
|
|
|
string s = safeString(data);
|
|
|
|
debug("(cul) checkCULFrame \"%s\"\n", s.c_str());
|
|
|
|
}
|
2020-02-06 18:01:48 +00:00
|
|
|
|
2019-11-22 17:50:55 +00:00
|
|
|
size_t eolp = 0;
|
|
|
|
// Look for end of line
|
|
|
|
for (; eolp < data.size(); ++eolp) {
|
2020-02-15 20:18:21 +00:00
|
|
|
if (data[eolp] == '\n') break; // Expect CRLF, look for LF ('\n')
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
2020-02-06 18:01:48 +00:00
|
|
|
if (eolp >= data.size())
|
|
|
|
{
|
|
|
|
debug("(cul) no eol found yet, partial frame\n");
|
|
|
|
return PartialFrame;
|
|
|
|
}
|
2020-02-15 20:18:21 +00:00
|
|
|
eolp++; // Point to byte after CRLF.
|
2020-02-16 19:23:47 +00:00
|
|
|
// Normally it is CRLF, but enable code to handle single LF as well.
|
|
|
|
int eof_len = data[eolp-2] == '\r' ? 2 : 1;
|
|
|
|
// If it was a CRLF then eof_len == 2, else it is 1.
|
2020-02-03 17:20:03 +00:00
|
|
|
if (data[0] != 'b')
|
|
|
|
{
|
2019-11-22 17:50:55 +00:00
|
|
|
// C1 and T1 telegrams should start with a 'b'
|
2020-02-15 20:18:21 +00:00
|
|
|
debug("(cul) no leading 'b' so it is text and no frame\n");
|
2020-02-03 17:20:03 +00:00
|
|
|
return TextAndNotFrame;
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:18:21 +00:00
|
|
|
if (data[1] == 'Y')
|
2020-02-03 17:20:03 +00:00
|
|
|
{
|
2020-02-15 20:18:21 +00:00
|
|
|
// C1 telegram in frame format B
|
|
|
|
// bY..44............<CR><LF>
|
|
|
|
*hex_frame_length = eolp;
|
|
|
|
vector<uchar> hex;
|
2020-02-26 10:02:47 +00:00
|
|
|
// If reception is started with X01, then there are no RSSI bytes.
|
|
|
|
// If started with X21, then there are two RSSI bytes (4 hex digits at the end).
|
|
|
|
// Now we always start with X01.
|
2020-02-18 19:48:00 +00:00
|
|
|
hex.insert(hex.end(), data.begin()+2, data.begin()+eolp-eof_len); // Remove CRLF
|
2020-02-15 20:18:21 +00:00
|
|
|
payload.clear();
|
|
|
|
bool ok = hex2bin(hex, &payload);
|
|
|
|
if (!ok)
|
|
|
|
{
|
2020-02-16 19:23:47 +00:00
|
|
|
string s = safeString(hex);
|
|
|
|
debug("(cul) bad hex \"%s\"\n", s.c_str());
|
2020-02-15 20:18:21 +00:00
|
|
|
warning("(cul) warning: the hex string is not proper! Ignoring telegram!\n");
|
|
|
|
return ErrorInFrame;
|
|
|
|
}
|
|
|
|
ok = trimCRCsFrameFormatB(payload);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
warning("(cul) dll C1 (frame b) crcs failed check! Ignoring telegram!\n");
|
|
|
|
return ErrorInFrame;
|
|
|
|
}
|
|
|
|
debug("(cul) received full C1 frame\n");
|
|
|
|
return FullFrame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// T1 telegram in frame format A
|
|
|
|
// b..44..............<CR><LF>
|
|
|
|
*hex_frame_length = eolp;
|
|
|
|
vector<uchar> hex;
|
2020-02-26 10:02:47 +00:00
|
|
|
// If reception is started with X01, then there are no RSSI bytes.
|
|
|
|
// If started with X21, then there are two RSSI bytes (4 hex digits at the end).
|
|
|
|
// Now we always start with X01.
|
2020-02-16 19:23:47 +00:00
|
|
|
hex.insert(hex.end(), data.begin()+1, data.begin()+eolp-eof_len); // Remove CRLF
|
2020-02-15 20:18:21 +00:00
|
|
|
payload.clear();
|
|
|
|
bool ok = hex2bin(hex, &payload);
|
|
|
|
if (!ok)
|
|
|
|
{
|
2020-02-16 19:23:47 +00:00
|
|
|
string s = safeString(hex);
|
|
|
|
debug("(cul) bad hex \"%s\"\n", s.c_str());
|
2020-02-15 20:18:21 +00:00
|
|
|
warning("(cul) warning: the hex string is not proper! Ignoring telegram!\n");
|
|
|
|
return ErrorInFrame;
|
|
|
|
}
|
|
|
|
ok = trimCRCsFrameFormatA(payload);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
warning("(cul) dll T1 (frame a) crcs failed check! Ignoring telegram!\n");
|
|
|
|
return ErrorInFrame;
|
|
|
|
}
|
|
|
|
debug("(cul) received full T1 frame\n");
|
|
|
|
return FullFrame;
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
AccessCheck detectCUL(string device, Detected *detected, shared_ptr<SerialCommunicationManager> manager)
|
2019-11-22 17:50:55 +00:00
|
|
|
{
|
|
|
|
// Talk to the device and expect a very specific answer.
|
|
|
|
auto serial = manager->createSerialDeviceTTY(device.c_str(), 38400);
|
2020-08-30 19:33:48 +00:00
|
|
|
serial->doNotUseCallbacks();
|
2020-05-09 19:56:37 +00:00
|
|
|
AccessCheck rc = serial->open(false);
|
|
|
|
if (rc != AccessCheck::AccessOK) return AccessCheck::NotThere;
|
2019-11-22 17:50:55 +00:00
|
|
|
|
|
|
|
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);
|
2020-01-27 08:29:40 +00:00
|
|
|
|
2019-11-22 17:50:55 +00:00
|
|
|
if (data[0] != '?') {
|
|
|
|
// no CUL device detected
|
|
|
|
serial->close();
|
2020-05-09 19:56:37 +00:00
|
|
|
return AccessCheck::NotThere;
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-08-30 19:33:48 +00:00
|
|
|
|
|
|
|
detected->set(WMBusDeviceType::DEVICE_CUL, 38400, false);
|
|
|
|
|
2020-05-09 19:56:37 +00:00
|
|
|
return AccessCheck::AccessOK;
|
2019-11-22 17:50:55 +00:00
|
|
|
}
|