forked from SzczepanLeon/wmbus-drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_izar.h
executable file
·157 lines (133 loc) · 5.22 KB
/
driver_izar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Based on: https://github.com/wmbusmeters/wmbusmeters/blob/master/src/driver_izar.cc
Copyright (C) 2019 Jacek Tomasiak (gpl-3.0-or-later)
Copyright (C) 2020-2022 Fredrik Öhrström (gpl-3.0-or-later)
Copyright (C) 2021 Vincent Privat (gpl-3.0-or-later)
*/
#pragma once
#include "driver.h"
#define GET_BIT(var, pos) ((var >> pos) & 0x01)
struct Izar: Driver
{
Izar(std::string key = "") : Driver(std::string("izar"), key) {};
virtual esphome::optional<std::map<std::string, double>> get_values(std::vector<unsigned char> &telegram) override {
std::map<std::string, double> ret_val{};
uint8_t decrypted[64] = {0};
if ((this->decrypt(reinterpret_cast<uint8_t*>(telegram.data()), telegram.size(), decrypted)) > 0) {
add_to_map(ret_val, "total_water_m3", this->get_total_water_m3(decrypted));
add_to_map(ret_val, "last_month_total_water_m3", this->get_last_month_total_water_m3(decrypted));
add_to_map(ret_val, "current_month_total_water_l", this->get_current_month_total_water_l(decrypted));
}
add_to_map(ret_val, "transmit_period_s", this->get_transmit_period_s(telegram));
add_to_map(ret_val, "remaining_battery_life_y", this->get_remaining_battery_life_y(telegram));
add_to_map(ret_val, "current_alarms", this->get_current_alarms(telegram));
add_to_map(ret_val, "previous_alarms", this->get_previous_alarms(telegram));
if (ret_val.size() > 0) {
return ret_val;
}
else {
return {};
}
};
private:
esphome::optional<double> get_current_alarms(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint16_t alarms = 0;
alarms |= (telegram[11] >> 7) << 1; // general_alarm
alarms |= (telegram[12] >> 7) << 2; // leakage_currently
alarms |= (telegram[12] >> 5 & 0x1) << 3; // meter_blocked
alarms |= (telegram[13] >> 7) << 4; // back_flow
alarms |= (telegram[13] >> 6 & 0x1) << 5; // underflow
alarms |= (telegram[13] >> 5 & 0x1) << 6; // overflow
alarms |= (telegram[13] >> 4 & 0x1) << 7; // submarine
alarms |= (telegram[13] >> 3 & 0x1) << 8; // sensor_fraud_currently
alarms |= (telegram[13] >> 1 & 0x1) << 9; // mechanical_fraud_currently
ret_val = (double)alarms;
return ret_val;
};
esphome::optional<double> get_previous_alarms(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint16_t alarms = 0;
alarms |= (telegram[12] >> 6 & 0x1) << 2; // leakage_previously
alarms |= (telegram[13] >> 2 & 0x1) << 8; // sensor_fraud_previously
alarms |= (telegram[13] & 0x1) << 9; // mechanical_fraud_previously
ret_val = (double)alarms;
return ret_val;
};
esphome::optional<double> get_remaining_battery_life_y(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
ret_val = (telegram[12] & 0x1F) / 2.0;
return ret_val;
};
esphome::optional<double> get_transmit_period_s(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
ret_val = 1 << ((telegram[11] & 0x0F) + 2);
return ret_val;
};
esphome::optional<double> get_total_water_m3(uint8_t *decrypted) {
esphome::optional<double> ret_val{};
ret_val = (this->uintFromBytesLittleEndian(decrypted + 1)) / 1000.0;
return ret_val;
};
esphome::optional<double> get_last_month_total_water_m3(uint8_t *decrypted) {
esphome::optional<double> ret_val{};
ret_val = (this->uintFromBytesLittleEndian(decrypted + 5)) / 1000.0;
return ret_val;
};
esphome::optional<double> get_current_month_total_water_l(uint8_t *decrypted) {
esphome::optional<double> ret_val{};
ret_val = this->uintFromBytesLittleEndian(decrypted + 1) - this->uintFromBytesLittleEndian(decrypted + 5);
return ret_val;
};
const uint8_t decoder[64] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0x03,0xFF,0x01,0x02,0xFF,
0xFF,0xFF,0xFF,0x07,0xFF,0xFF,0x00,0xFF,
0xFF,0x05,0x06,0xFF,0x04,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0x0B,0xFF,0x09,0x0A,0xFF,
0xFF,0x0F,0xFF,0xFF,0x08,0xFF,0xFF,0xFF,
0xFF,0x0D,0x0E,0xFF,0x0C,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
uint32_t uintFromBytes(uint8_t* data) {
uint32_t result = data[0] << 24;
result += data[1] << 16;
result += data[2] << 8;
result += data[3];
return result;
}
uint32_t uintFromBytesLittleEndian(uint8_t* data) {
uint32_t result = data[3] << 24;
result += data[2] << 16;
result += data[1] << 8;
result += data[0];
return result;
}
uint32_t hashShiftKey(uint32_t key) {
for (uint8_t i = 0; i < 8; i++) {
uint8_t bit = GET_BIT(key, 1) ^ GET_BIT(key, 2) ^
GET_BIT(key, 11) ^ GET_BIT(key, 31);
key <<= 1;
key |= bit;
}
return key;
}
uint8_t decrypt(uint8_t* encoded, uint8_t len, uint8_t* decoded) {
if (len < 15) {
return 0;
}
uint32_t key = 0xdfd109e8;
key ^= uintFromBytes(encoded + 2);
key ^= uintFromBytes(encoded + 6);
key ^= uintFromBytes(encoded + 10);
const uint8_t size = len - 15;
for (uint8_t i = 0; i < size; i++) {
key = hashShiftKey(key);
decoded[i] = encoded[i + 15] ^ (key & 0xFF);
}
if (decoded[0] != 0x4B) {
return 0;
}
return size;
}
};