-
Notifications
You must be signed in to change notification settings - Fork 0
/
BleDeviceTable.cpp
105 lines (86 loc) · 3.96 KB
/
BleDeviceTable.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
#include "BleStar.h"
// Common reset methods
void BleStar::resetBleDeviceTable() {
for (int i = 0; i < _maxConnectionsAsCentral + 2; i++) {
BleDeviceTable * bleDevice = &bleDeviceTable[i];
bleDevice->peerName = NULL;
bleDevice->isConnected = false;
bleDevice->index = i;
bleDevice->tempReceiveBuffer = new MessageBuilder(MAX_BLE_CHUNK_LENGTH + 1);
resetReceiveMessage(bleDevice);
resetSendMessage(bleDevice);
}
Log.v("bleDeviceTable has been reset");
}
void BleStar::recalculateNumberOfBleConnections() {
_numberOfBleConnections = 0;
for (int i = BLE_PERIPHERAL_INDEX; i < _numberOfBleCentralConnections + 2; i++) {
if (bleDeviceTable[i].isConnected) { _numberOfBleConnections++; }
}
}
// Common receive methods
void BleStar::resetReceiveMessage(BleDeviceTable * bleDevice) {
bleDevice->receiveState = AWAITING_NEW_MESSAGE;
bleDevice->tempReceiveBuffer->reset();
bleDevice->isUsingTempReceiveBuffer = true;
bleDevice->receiveBuffer = bleDevice->tempReceiveBuffer;
if (bleDevice->messageBeingReceived != NULL) {
bleDevice->messageBeingReceived->setMessageType(MESSAGE_TYPE_NONE);
bleDevice->messageBeingReceived->getMessageBuilder()->reset();
}
bleDevice->messageBeingReceived = NULL;
bleDevice->receiveChunkInProgress = 0;
bleDevice->indexWithinReceiveChunk = 0;
bleDevice->receiveChunksExpected = 0;
bleDevice->chunkResendIndex = 0;
bleDevice->receiveAttempts = 0;
bleDevice->lastreceivedTime = 0;
bleDevice->lastResendRequestTime = 0;
for (int j = 0; j < CHUNK_FLAG_TABLE_CAPACITY + 1; j++) { bleDevice->receivedChunkFlags[j] = 0xFF; }
}
boolean BleStar::getChunkReceived(BleDeviceTable * bleDevice, int chunkNumber) {
return ((bleDevice->receivedChunkFlags[chunkNumber / 8] & bitSetArray[chunkNumber % 8]) > 0);
}
void BleStar::setChunkReceived(BleDeviceTable * bleDevice, int chunkNumber) {
bleDevice->receivedChunkFlags[chunkNumber / 8] = bleDevice->receivedChunkFlags[chunkNumber / 8] & bitResetArray[chunkNumber % 8];
}
boolean BleStar::getAllChunksReceived(BleDeviceTable * bleDevice) {
for (int i = 0; i < bleDevice->receiveChunksExpected; i++) {
if ((bleDevice->receivedChunkFlags[i / 8] & bitSetArray[i % 8]) !=0 ) { return false; }
}
return (true);
}
void BleStar::resetSendMessage(BleDeviceTable * bleDevice) {
bleDevice->sendBuffer = NULL;
bleDevice->sendChunkResendRequested = false;
if (bleDevice->messageBeingSent != NULL) {
bleDevice->messageBeingSent->setMessageType(MESSAGE_TYPE_NONE);
bleDevice->messageBeingSent = NULL;
}
for (int j = 0; j < CHUNK_FLAG_TABLE_CAPACITY + 1; j++) { bleDevice->sentChunkFlags[j] = 0x00; }
}
boolean BleStar::getChunkNeedsToBeSent(BleDeviceTable * bleDevice, int chunkNumber) {
return ((bleDevice->receivedChunkFlags[chunkNumber / 8] & bitSetArray[chunkNumber % 8]) > 0);
}
void BleStar::setChunkNotSent(BleDeviceTable * bleDevice, int chunkNumber) {
bleDevice->sentChunkFlags[chunkNumber / 8] = bleDevice->sentChunkFlags[chunkNumber / 8] | bitSetArray[chunkNumber % 8];
}
void BleStar::setChunkSent(BleDeviceTable * bleDevice, int chunkNumber) {
bleDevice->sentChunkFlags[chunkNumber / 8] = bleDevice->sentChunkFlags[chunkNumber / 8] & bitResetArray[chunkNumber % 8];
}
boolean BleStar::getAllChunksSent(BleDeviceTable * bleDevice) {
for (int i = 0; i < bleDevice->receiveChunksExpected; i++) {
if ((bleDevice->sentChunkFlags[i / 8] & bitSetArray[i % 8]) != 0 ) { return false; }
}
return (true);
}
int BleStar::getBleDeviceIndex(char * deviceName) {
int bleDeviceIndex = ERROR_BLE_DEVICE_NA;
for (int i = BLE_PERIPHERAL_INDEX; i < MAX_CENTRAL_CONNECTIONS + 2; i++) {
if (bleDeviceTable[i].peerName == deviceName) { bleDeviceIndex = i; break; }
}
if (bleDeviceIndex == -1) { return ERROR_BLE_DEVICE_NA; }
if (bleDeviceIndex == BLE_THIS_DEVICE_INDEX) { return ERROR_BLE_DEVICE_SAME; }
if (!bleDeviceTable[bleDeviceIndex].isConnected) { return ERROR_BLE_DEVICE_NOT_CONNECTED; }
return (bleDeviceIndex);
}