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/>.
|
|
|
|
*/
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
#ifndef WMBUS_UTILS_H
|
|
|
|
#define WMBUS_UTILS_H
|
|
|
|
|
2020-01-27 08:29:40 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "wmbus.h"
|
|
|
|
|
2020-09-25 19:47:21 +00:00
|
|
|
#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);
|
2020-01-27 15:53:18 +00:00
|
|
|
bool decrypt_TPL_AES_CBC_NO_IV(Telegram *t, vector<uchar> &frame, vector<uchar>::iterator &pos, vector<uchar> &aeskey);
|
2018-03-05 10:29:25 +00:00
|
|
|
string frameTypeKamstrupC1(int ft);
|
|
|
|
|
2020-01-27 08:29:40 +00:00
|
|
|
struct WMBusCommonImplementation : public virtual WMBus
|
|
|
|
{
|
2020-09-21 19:55:21 +00:00
|
|
|
WMBusCommonImplementation(WMBusDeviceType t, shared_ptr<SerialCommunicationManager> manager, shared_ptr<SerialDevice> serial_override);
|
2020-08-30 19:33:48 +00:00
|
|
|
~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();
|
2020-08-30 19:33:48 +00:00
|
|
|
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);
|
2020-09-25 19:58:09 +00:00
|
|
|
virtual void processSerialData() = 0;
|
2020-08-30 19:33:48 +00:00
|
|
|
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 "?"; }
|
2020-09-25 22:10:34 +00:00
|
|
|
bool waitForResponse();
|
2020-08-01 19:56:46 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
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:
|
|
|
|
|
2020-08-30 19:33:48 +00:00
|
|
|
bool is_working_ {};
|
2020-02-06 12:14:46 +00:00
|
|
|
vector<function<bool(vector<uchar>)>> telegram_listeners_;
|
2020-09-21 19:55:21 +00:00
|
|
|
// 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_ {};
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
shared_ptr<SerialDevice> serial_;
|
2020-09-25 19:47:21 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
sem_t command_wait_;
|
2020-01-27 08:29:40 +00:00
|
|
|
};
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
#endif
|