wmbusmeters/src/wmbus_utils.h

86 wiersze
3.2 KiB
C
Czysty Zwykły widok Historia

2018-04-01 06:53:37 +00:00
/*
2020-01-27 08:29:40 +00:00
Copyright (C) 2020 Fredrik Öhrström
2018-04-01 06:53:37 +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/>.
*/
#ifndef WMBUS_UTILS_H
#define WMBUS_UTILS_H
2020-01-27 08:29:40 +00:00
#include "util.h"
#include "wmbus.h"
#include<semaphore.h>
2020-01-27 08:29:40 +00:00
bool decrypt_ELL_AES_CTR(Telegram *t, vector<uchar> &frame, vector<uchar>::iterator &pos, vector<uchar> &aeskey);
bool decrypt_TPL_AES_CBC_IV(Telegram *t, vector<uchar> &frame, vector<uchar>::iterator &pos, vector<uchar> &aeskey);
bool decrypt_TPL_AES_CBC_NO_IV(Telegram *t, vector<uchar> &frame, vector<uchar>::iterator &pos, vector<uchar> &aeskey);
string frameTypeKamstrupC1(int ft);
2020-01-27 08:29:40 +00:00
struct WMBusCommonImplementation : public virtual WMBus
{
WMBusCommonImplementation(WMBusDeviceType t, shared_ptr<SerialCommunicationManager> manager, shared_ptr<SerialDevice> serial_override);
~WMBusCommonImplementation();
2020-01-27 08:29:40 +00:00
2020-02-06 12:14:46 +00:00
WMBusDeviceType type();
2020-01-27 08:29:40 +00:00
void onTelegram(function<bool(vector<uchar>)> cb);
bool handleTelegram(vector<uchar> frame);
2020-08-01 19:56:46 +00:00
void checkStatus();
bool isWorking();
2020-08-01 19:56:46 +00:00
void setTimeout(int seconds, std::string expected_activity);
2020-09-25 21:56:50 +00:00
void setResetInterval(int seconds);
2020-08-01 19:56:46 +00:00
void setLinkModes(LinkModeSet lms);
virtual void processSerialData() = 0;
void disconnectedFromDevice();
2020-08-01 19:56:46 +00:00
bool reset();
SerialDevice *serial() { if (serial_) return serial_.get(); else return NULL; }
string device() { if (serial_) return serial_->device(); else return "?"; }
bool waitForResponse();
2020-08-01 19:56:46 +00:00
protected:
shared_ptr<SerialCommunicationManager> manager_;
2020-08-01 19:56:46 +00:00
void protocolErrorDetected();
void resetProtocolErrorCount();
bool areLinkModesConfigured();
// Device specific set link modes implementation.
virtual void deviceSetLinkModes(LinkModeSet lms) = 0;
// Device specific reset code, apart from serial->open and setLinkModes.
virtual void deviceReset() = 0;
LinkModeSet protectedGetLinkModes(); // Used to read private link_modes_ in subclass.
2020-02-06 12:14:46 +00:00
private:
bool is_working_ {};
2020-02-06 12:14:46 +00:00
vector<function<bool(vector<uchar>)>> telegram_listeners_;
// vector<shared_ptr<Meter>> *meters_;
2020-02-06 12:14:46 +00:00
WMBusDeviceType type_ {};
2020-08-01 19:56:46 +00:00
int protocol_error_count_ {};
time_t timeout_ {}; // If longer silence than timeout, then reset dongle! It might have hanged!
string expected_activity_ {}; // During which times should we care about timeouts?
time_t last_received_ {}; // When as the last telegram reception?
time_t last_reset_ {}; // When did we last attempt a reset of the dongle?
2020-09-25 21:56:50 +00:00
int reset_timeout_ {}; // When set to 24*3600 reset the device once every 24 hours.
2020-08-01 19:56:46 +00:00
bool link_modes_configured_ {};
LinkModeSet link_modes_ {};
shared_ptr<SerialDevice> serial_;
protected:
sem_t command_wait_;
2020-01-27 08:29:40 +00:00
};
#endif