Skip to content

Commit fb76d7e

Browse files
committed
Channel status handling and some utility functions
1 parent 3217ddb commit fb76d7e

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

src/BaseClasses/ANTPLUS_BaseProfile.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,26 @@ void BaseProfile::pushChannelConfig() {
6262

6363
void BaseProfile::openChannel() {
6464
OpenChannel oc = OpenChannel(_channel);
65+
_channelStatus = CHANNEL_STATUS_SEARCHING;
6566
_router->send(oc);
6667
}
6768

6869
void BaseProfile::closeChannel() {
6970
CloseChannel cc = CloseChannel(_channel);
71+
_channelStatus = CHANNEL_STATUS_ASSIGNED;
7072
_router->send(cc);
7173
}
7274

7375
void BaseProfile::onChannelEventResponse(ChannelEventResponse& msg) {
7476
uint8_t event = msg.getCode();
75-
// TODO maybe define an explicit state enum?
77+
7678
switch (event) {
7779
case STATUS_EVENT_CHANNEL_CLOSED:
78-
_channelStatus = STATUS_EVENT_CHANNEL_CLOSED;
79-
break;
8080
case STATUS_EVENT_RX_FAIL_GO_TO_SEARCH:
81-
_channelStatus = STATUS_EVENT_RX_FAIL_GO_TO_SEARCH;
81+
_channelStatus = CHANNEL_STATUS_ASSIGNED;
8282
break;
8383
}
84+
8485
_onChannelEvent.call(msg);
8586
}
8687

@@ -110,11 +111,19 @@ void BaseProfile::onBurstTransferData(BurstTransferData& msg) {
110111
_onDataPage.call(msg);
111112
}
112113

114+
void BaseProfile::onChannelStatus(ChannelStatus& msg) {
115+
_channelStatus = msg.getChannelState();
116+
}
117+
113118
void BaseProfile::checkProfileStatus() {
114119
if (!getDeviceNumber() || !getTransmissionType()) {
115120
RequestMessage rm = RequestMessage(CHANNEL_ID, _channel);
116121
send(rm);
117122
}
123+
if (_channelStatus == CHANNEL_STATUS_SEARCHING) {
124+
RequestMessage rm = RequestMessage(CHANNEL_STATUS, _channel);
125+
send(rm);
126+
}
118127
}
119128

120129
void BaseProfile::send(AntRequest& msg) {
@@ -127,4 +136,8 @@ uint8_t BaseProfile::getTransmissionType() {
127136

128137
uint16_t BaseProfile::getDeviceNumber() {
129138
return _deviceNumber;
139+
}
140+
141+
void BaseProfile::loop() {
142+
_router->loop();
130143
}

src/BaseClasses/ANTPLUS_BaseProfile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class BaseProfile {
7474
virtual void onBurstTransferData(BurstTransferData& msg);
7575
virtual void onChannelEventResponse(ChannelEventResponse& msg);
7676
virtual void onChannelIdResponse(ChannelIdResponse& msg);
77+
virtual void onChannelStatus(ChannelStatus& msg);
7778
void setRouter(AntPlusRouter* router);
7879
void setChannelNumber(uint8_t channel);
7980
// TODO this should probably have the whole message passed in so
@@ -90,6 +91,7 @@ class BaseProfile {
9091
void pushChannelConfig();
9192
void openChannel();
9293
void closeChannel();
94+
void loop();
9395
private:
9496
void checkProfileStatus();
9597
AntPlusRouter* _router;

src/BaseClasses/ANTPLUS_BaseSlaveProfile.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,16 @@ BaseSlaveProfile::BaseSlaveProfile(uint16_t deviceNumber) : BaseProfile(deviceNu
1212
BaseSlaveProfile::BaseSlaveProfile(uint16_t deviceNumber, uint8_t transmissionType) : BaseProfile(deviceNumber, transmissionType) {
1313

1414
}
15+
16+
void BaseSlaveProfile::searchForDevices(uint16_t* buffer) {
17+
// TODO
18+
}
19+
20+
uint8_t BaseSlaveProfile::waitForPair() {
21+
// TODO add a timeout in the event packed was dropped?
22+
while (getChannelStatus() == CHANNEL_STATUS_SEARCHING) {
23+
// If a event saying we disconnected happened then we will check it, otherwise wait for data to come in
24+
loop();
25+
}
26+
return getChannelStatus();
27+
}

src/BaseClasses/ANTPLUS_BaseSlaveProfile.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ class BaseSlaveProfile : public BaseProfile {
99
BaseSlaveProfile(uint16_t deviceNumber);
1010
BaseSlaveProfile(uint16_t deviceNumber, uint8_t transmissionType);
1111

12-
// TODO add a search method
12+
/**
13+
* Pass in a pointer to an array uint16_t[ANTPLUS_MAX_CHANNELS_POSSIBLE]
14+
*
15+
* Any non-0 entry after the function returned is a nearby device
16+
*/
17+
void searchForDevices(uint16_t* buffer);
18+
/**
19+
* Will return once the device either pairs or timesout its search
20+
*/
21+
uint8_t waitForPair();
1322

1423
private:
1524
};

src/MainClasses/ANTPLUS_AntPlusRouter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ void AntPlusRouter::onChannelIdResponse(ChannelIdResponse& msg) {
172172
}
173173

174174
void AntPlusRouter::onChannelStatus(ChannelStatus& msg) {
175-
// TODO
175+
uint8_t channel = msg.getChannelNumber();
176+
if (_profiles[channel]) {
177+
_profiles[channel]->onChannelStatus(msg);
178+
}
176179
}
177180

178181
void AntPlusRouter::onEncryptionModeParameters(EncryptionModeParameters& msg) {

0 commit comments

Comments
 (0)