wmbusmeters/src/wmbus_rawtty.cc

173 wiersze
5.0 KiB
C++
Czysty Zwykły widok Historia

2019-10-15 12:32:32 +00:00
/*
2020-01-27 08:29:40 +00:00
Copyright (C) 2019-2020 Fredrik Öhrström
2019-10-15 12:32:32 +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-10-25 07:21:26 +00:00
#include"wmbus_common_implementation.h"
2020-01-27 08:29:40 +00:00
#include"wmbus_utils.h"
2019-10-15 12:32:32 +00:00
#include"serial.h"
#include<assert.h>
#include<pthread.h>
#include<semaphore.h>
2020-05-11 09:59:47 +00:00
#include<errno.h>
2019-10-15 12:32:32 +00:00
#include<unistd.h>
using namespace std;
2020-01-27 08:29:40 +00:00
struct WMBusRawTTY : public virtual WMBusCommonImplementation
2019-10-15 12:32:32 +00:00
{
bool ping();
string getDeviceId();
string getDeviceUniqueId();
2019-10-15 12:32:32 +00:00
LinkModeSet getLinkModes();
2020-08-01 19:56:46 +00:00
void deviceReset();
void deviceSetLinkModes(LinkModeSet lms);
2019-10-15 12:32:32 +00:00
LinkModeSet supportedLinkModes() { return Any_bit; }
int numConcurrentLinkModes() { return 0; }
bool canSetLinkModes(LinkModeSet desired_modes) { return true; }
void processSerialData();
void simulate() { }
2021-08-29 18:26:06 +00:00
WMBusRawTTY(string bus_alias, shared_ptr<SerialDevice> serial, shared_ptr<SerialCommunicationManager> manager);
2019-10-15 12:32:32 +00:00
~WMBusRawTTY() { }
private:
2020-08-01 19:56:46 +00:00
2019-10-15 12:32:32 +00:00
vector<uchar> read_buffer_;
LinkModeSet link_modes_;
vector<uchar> received_payload_;
};
shared_ptr<WMBus> openRawTTY(Detected detected,
shared_ptr<SerialCommunicationManager> manager,
shared_ptr<SerialDevice> serial_override)
2019-10-15 12:32:32 +00:00
{
2021-08-29 18:26:06 +00:00
string bus_alias = detected.specified_device.bus_alias;
string device = detected.found_file;
int bps = detected.found_bps;
assert(device != "");
2019-11-03 15:31:30 +00:00
if (serial_override)
{
2021-08-29 18:26:06 +00:00
WMBusRawTTY *imp = new WMBusRawTTY(bus_alias, serial_override, manager);
imp->markAsNoLongerSerial();
return shared_ptr<WMBus>(imp);
2019-11-03 15:31:30 +00:00
}
auto serial = manager->createSerialDeviceTTY(device.c_str(), bps, PARITY::NONE, "rawtty");
2021-08-29 18:26:06 +00:00
WMBusRawTTY *imp = new WMBusRawTTY(bus_alias, serial, manager);
return shared_ptr<WMBus>(imp);
2019-10-15 12:32:32 +00:00
}
2021-08-29 18:26:06 +00:00
WMBusRawTTY::WMBusRawTTY(string bus_alias, shared_ptr<SerialDevice> serial, shared_ptr<SerialCommunicationManager> manager) :
WMBusCommonImplementation(bus_alias, DEVICE_RAWTTY, manager, serial, true)
2019-10-15 12:32:32 +00:00
{
2020-08-01 19:56:46 +00:00
reset();
2019-10-15 12:32:32 +00:00
}
bool WMBusRawTTY::ping()
{
return true;
}
string WMBusRawTTY::getDeviceId()
2019-10-15 12:32:32 +00:00
{
return "?";
2019-10-15 12:32:32 +00:00
}
string WMBusRawTTY::getDeviceUniqueId()
{
return "?";
}
2019-10-15 12:32:32 +00:00
LinkModeSet WMBusRawTTY::getLinkModes() {
return link_modes_;
}
2020-08-01 19:56:46 +00:00
void WMBusRawTTY::deviceReset()
2019-10-15 12:32:32 +00:00
{
}
2020-08-01 19:56:46 +00:00
void WMBusRawTTY::deviceSetLinkModes(LinkModeSet lms)
2019-10-15 12:32:32 +00:00
{
}
void WMBusRawTTY::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-10-15 12:32:32 +00:00
read_buffer_.insert(read_buffer_.end(), data.begin(), data.end());
2019-11-03 21:00:18 +00:00
size_t frame_length;
int payload_len, payload_offset;
for (;;)
{
2020-03-22 17:43:09 +00:00
FrameStatus status = checkWMBusFrame(read_buffer_, &frame_length, &payload_len, &payload_offset);
2019-10-15 12:32:32 +00:00
if (status == PartialFrame)
{
// Partial frame, stop eating.
break;
}
2019-11-03 21:00:18 +00:00
if (status == ErrorInFrame)
{
verbose("(rawtty) protocol error in message received!\n");
string msg = bin2hex(read_buffer_);
debug("(rawtty) protocol error \"%s\"\n", msg.c_str());
read_buffer_.clear();
break;
}
2019-11-03 21:00:18 +00:00
if (status == FullFrame)
{
vector<uchar> payload;
if (payload_len > 0)
{
uchar l = payload_len;
payload.insert(payload.end(), &l, &l+1); // Re-insert the len byte.
payload.insert(payload.end(), read_buffer_.begin()+payload_offset, read_buffer_.begin()+payload_offset+payload_len);
}
read_buffer_.erase(read_buffer_.begin(), read_buffer_.begin()+frame_length);
AboutTelegram about("", 0, FrameType::WMBUS);
2020-10-14 18:59:14 +00:00
handleTelegram(about, payload);
2019-11-03 22:43:29 +00:00
}
2019-10-15 12:32:32 +00:00
}
}
AccessCheck detectRAWTTY(Detected *detected, shared_ptr<SerialCommunicationManager> manager)
2019-10-15 12:32:32 +00:00
{
string tty = detected->specified_device.file;
int bps = atoi(detected->specified_device.bps.c_str());
2019-10-15 12:32:32 +00:00
// Since we do not know how to talk to the other end, it might not
// even respond. The only thing we can do is to try to open the serial device.
2021-02-13 14:58:38 +00:00
auto serial = manager->createSerialDeviceTTY(tty.c_str(), bps, PARITY::NONE, "detect rawtty");
2020-05-09 19:56:37 +00:00
AccessCheck rc = serial->open(false);
if (rc != AccessCheck::AccessOK) return AccessCheck::NotThere;
2019-10-15 12:32:32 +00:00
serial->close();
detected->setAsFound("", WMBusDeviceType::DEVICE_RAWTTY, false, bps,
detected->specified_device.linkmodes);
2020-05-09 19:56:37 +00:00
return AccessCheck::AccessOK;
2019-10-15 12:32:32 +00:00
}