-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAM43Client.cpp
300 lines (262 loc) · 8.9 KB
/
AM43Client.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <Arduino.h>
#include "AM43Client.h"
#ifdef USE_NIMBLE
#include "NimBLEDevice.h"
#else
#include "BLEDevice.h"
#endif
BLEUUID serviceUUID(AM43_SERVICE_UUID);
BLEUUID charUUID(AM43_CHAR_UUID);
uint8_t startPacket[5] = {0x00, 0xff, 0x00, 0x00, 0x9a};
std::vector<uint8_t> startPkt(startPacket, startPacket + sizeof(startPacket)/sizeof(startPacket[0]));
AM43Client::AM43Client(BLEAdvertisedDevice *d)
:AM43Client(d, AM43_DEFAULT_PIN)
{}
AM43Client::AM43Client(BLEAdvertisedDevice *d, uint16_t pin) {
this->m_Device = d;
this->m_Connected = false;
this->m_Disconnected = false;
this->m_LoggedIn = false;
this->m_Pin = pin;
this->m_BatteryPercent = 0xff;
this->m_ClientCallbacks = nullptr;
this->m_CurrentQuery = 1;
this->m_CommandQueued = false;
}
void AM43Client::onConnect(BLEClient* pclient) {
this->m_Connected = true;
if (this->m_ClientCallbacks != nullptr)
this->m_ClientCallbacks->onConnect(this);
}
void AM43Client::onDisconnect(BLEClient* pclient) {
this->m_Connected = false;
this->m_Disconnected = true;
if (this->m_ClientCallbacks != nullptr)
this->m_ClientCallbacks->onDisconnect(this);
}
void AM43Client::setClientCallbacks(AM43Callbacks *callbacks) {
this->m_ClientCallbacks = callbacks;
}
String AM43Client::deviceString() {
return String(this->m_Device->getName().c_str()) + " " + String(this->m_Device->getAddress().toString().c_str());
}
void AM43Client::myNotifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
if (length < 1 || pData[0] != 0x9a) return;
#if 0
Serial.printf("%s: 0x", deviceString().c_str());
for (int i = 0; i < length ; i++) {
if (pData[i]<0x10) Serial.print("0");
Serial.print(pData[i], HEX);
}
Serial.println();
#endif
switch (pData[1]) {
case AM43_COMMAND_GET_BATTERY: {
this->m_BatteryPercent = pData[7];
if (this->m_ClientCallbacks != nullptr)
this->m_ClientCallbacks->onBatteryLevel(this->m_BatteryPercent);
break;
}
case AM43_COMMAND_SET_POSITION: {
if (pData[3] != AM43_RESPONSE_ACK) {
Serial.printf("[%s] Position command nack! (%d)\r\n", deviceString().c_str(), pData[3]);
}
break;
}
case AM43_NOTIFY_POSITION: {
this->m_OpenLevel = pData[4];
if (this->m_ClientCallbacks != nullptr)
this->m_ClientCallbacks->onPosition(this->m_OpenLevel);
break;
}
case AM43_COMMAND_GET_POSITION: {
/*
* Bytes in this packet are:
* 3: Configuration flags, bits are:
* 1: direction
* 2: operation mode
* 3: top limit set
* 4: bottom limit set
* 5: has light sensor
* 4: Speed setting
* 5: Current position
* 6,7: Shade length.
* 8: Roller diameter.
* 9: Roller type.
*/
this->m_OpenLevel = pData[5];
if (this->m_ClientCallbacks != nullptr)
this->m_ClientCallbacks->onPosition(this->m_OpenLevel);
break;
}
case AM43_COMMAND_GET_LIGHT: {
this->m_LightLevel = ((float)pData[4]*100/9);
if (this->m_ClientCallbacks != nullptr)
this->m_ClientCallbacks->onLightLevel(this->m_LightLevel);
break;
}
case AM43_COMMAND_LOGIN: {
if (pData[3] == AM43_RESPONSE_ACK) {
this->m_LoggedIn = true;
Serial.printf("[%s] Pin ok\r\n", deviceString().c_str());
// Trigger a fetch in 1s;
this->m_LastUpdate = millis() - AM43_UPDATE_INTERVAL + 1000;
} else if (pData[3] == AM43_RESPONSE_NACK) {
Serial.printf("[%s] Pin incorrect\r\n", deviceString().c_str());
this->m_LoggedIn = false;
}
break;
}
case AM43_COMMAND_MOVE: {
if (pData[3] == AM43_RESPONSE_ACK) {
Serial.printf("[%s] Move ok\r\n", deviceString().c_str());
break;
} else if (pData[3] == AM43_RESPONSE_NACK) {
Serial.printf("[%s] Move nack\r\n", deviceString().c_str());
break;
}
}
case AM43_REPLY_UNKNOWN2:
case AM43_REPLY_UNKNOWN1: {
Serial.printf("[%s] Unknown reply: ", deviceString().c_str());
for (int i = 0; i < length ; i++) {
if (pData[i]<0x10) Serial.print("0");
Serial.print(pData[i], HEX);
}
Serial.println();
break;
}
default: {
Serial.printf("[%s] Unknown notify data for characteristic %s: 0x", deviceString().c_str(), pBLERemoteCharacteristic->getUUID().toString().c_str());
for (int i = 0; i < length ; i++) {
if (pData[i]<0x10) Serial.print("0");
Serial.print(pData[i], HEX);
}
Serial.println();
}
}
}
void AM43Client::sendGetBatteryRequest() {
std::vector<uint8_t> data{0x1};
this->sendCommand(AM43_COMMAND_GET_BATTERY, data);
}
void AM43Client::sendGetLightRequest() {
std::vector<uint8_t> data{0x1};
this->sendCommand(AM43_COMMAND_GET_LIGHT, data);
}
void AM43Client::sendGetPositionRequest() {
std::vector<uint8_t> data{0x1};
this->sendCommand(AM43_COMMAND_GET_POSITION, data);
}
void AM43Client::sendPin() {
std::vector<uint8_t> data{((uint16_t)this->m_Pin & 0xff00)>>8, ((uint16_t)this->m_Pin & 0xff)};
this->sendCommand(AM43_COMMAND_LOGIN, data);
}
void AM43Client::open() {
std::vector<uint8_t> data{0xDD};
this->sendCommand(AM43_COMMAND_MOVE, data);
}
void AM43Client::stop() {
std::vector<uint8_t> data{0xCC};
this->sendCommand(AM43_COMMAND_MOVE, data);
}
void AM43Client::close() {
std::vector<uint8_t> data{0xEE};
this->sendCommand(AM43_COMMAND_MOVE, data);
}
void AM43Client::setPosition(uint8_t pos) {
std::vector<uint8_t> data{pos};
this->sendCommand(AM43_COMMAND_SET_POSITION, data);
}
void AM43Client::setCommandQueued(boolean status) {
this->m_CommandQueued = status;
}
void AM43Client::update() {
if (millis() - this->m_LastUpdate > AM43_UPDATE_INTERVAL) {
if (!this->m_LoggedIn) {
this->sendPin();
return;
}
if (AM43_UPDATE_BATTERY == 1 && this->m_CurrentQuery == AM43_UPDATE_BATTERY)
this->sendGetBatteryRequest();
else if (AM43_UPDATE_POSITION == 1 && this->m_CurrentQuery == AM43_UPDATE_BATTERY + AM43_UPDATE_POSITION)
this->sendGetPositionRequest();
else if (AM43_UPDATE_BATTERY == 1 && this->m_CurrentQuery == AM43_UPDATE_BATTERY + AM43_UPDATE_POSITION + AM43_UPDATE_LIGHT)
this->sendGetLightRequest();
this->m_CurrentQuery = this->m_CurrentQuery + 1 > AM43_UPDATE_BATTERY + AM43_UPDATE_POSITION + AM43_UPDATE_LIGHT ? 1 : this->m_CurrentQuery + 1;
this->m_LastUpdate = millis();
}
}
byte checksum(std::vector<uint8_t> data) {
uint8_t checksum = 0;
for (int i = 0; i < data.size() ; i++)
checksum = checksum ^ data[i];
checksum = checksum ^ 0xff;
return checksum;
}
void AM43Client::sendCommand(uint8_t command, std::vector<uint8_t> data) {
std::vector<uint8_t> sendData;
for (int i=0; i < startPkt.size(); i++)
sendData.push_back(startPkt[i]);
sendData.push_back(command);
sendData.push_back((char)data.size());
sendData.insert(sendData.end(), data.begin(), data.end());
sendData.push_back(checksum(sendData));
Serial.printf("[%s] AM43 Send: ", deviceString().c_str());
for (int i = 0; i < sendData.size() ; i++) {
if (sendData[i]<0x10) Serial.print("0");
Serial.print(sendData[i], HEX);
}
Serial.println();
m_Char->writeValue(&sendData[0], sendData.size(), false);
}
boolean AM43Client::connectToServer(notify_callback callback) {
Serial.printf("Attempting to connect to: %s ", this->m_Device->getAddress().toString().c_str());
unsigned long connectStart = millis();
this->m_DoConnect = false;
this->m_Client = BLEDevice::createClient();
this->m_Client->setClientCallbacks(this);
this->m_Connected = false;
// Connect to the remote BLE Server.
Serial.print("...");
#ifdef USE_NIMBLE
if (this->m_Client->connect(this->m_Device, false)) {
#else
if (this->m_Client->connect(this->m_Device)) {
#endif
Serial.println(" - Connected to server");
} else {
Serial.println(" - Failed to connect.");
this->m_Disconnected = true;
this->m_Connected = false;
return false;
}
#ifdef USE_NIMBLE
this->m_Client->discoverAttributes();
#endif
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = m_Client->getService(AM43_SERVICE_UUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
this->m_Client->disconnect();
return false;
}
// Obtain a reference to the characteristic in the service of the remote BLE server.
m_Char = pRemoteService->getCharacteristic(AM43_CHAR_UUID);
if (m_Char == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
this->m_Client->disconnect();
return false;
}
if(this->m_Char->canNotify())
this->m_Char->registerForNotify(callback);
this->m_Connected = true;
Serial.printf("Connect took %dms\r\n", millis()-connectStart);
return true;
}
void AM43Client::disconnectFromServer() {
this->m_Client->disconnect();
}