Skip to content

Commit a46fbd2

Browse files
Add response specific callbacks
This adds a callback for each type of response that exists. Whenever a respons is received, the corresponding callback is called. When it is not defined, the onOtherResponse callback is called instead.
1 parent 2ec69c5 commit a46fbd2

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

XBee.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,65 @@ void XBeeWithCallbacks::loop() {
16021602
readPacket();
16031603
if (getResponse().isAvailable()) {
16041604
_onResponse.call(getResponse());
1605+
1606+
bool called = false;
1607+
1608+
uint8_t id = getResponse().getApiId();
1609+
1610+
if (id == ZB_TX_STATUS_RESPONSE) {
1611+
ZBTxStatusResponse response;
1612+
getResponse().getZBTxStatusResponse(response);
1613+
called = _onZBTxStatusResponse.call(response);
1614+
} else if (id == ZB_RX_RESPONSE) {
1615+
ZBRxResponse response;
1616+
getResponse().getZBRxResponse(response);
1617+
called = _onZBRxResponse.call(response);
1618+
} else if (id == ZB_EXPLICIT_RX_RESPONSE) {
1619+
ZBExplicitRxResponse response;
1620+
getResponse().getZBExplicitRxResponse(response);
1621+
called = _onZBExplicitRxResponse.call(response);
1622+
} else if (id == ZB_IO_SAMPLE_RESPONSE) {
1623+
ZBRxIoSampleResponse response;
1624+
getResponse().getZBRxIoSampleResponse(response);
1625+
called = _onZBRxIoSampleResponse.call(response);
1626+
} else if (id == TX_STATUS_RESPONSE) {
1627+
TxStatusResponse response;
1628+
getResponse().getTxStatusResponse(response);
1629+
called = _onTxStatusResponse.call(response);
1630+
} else if (id == RX_16_RESPONSE) {
1631+
Rx16Response response;
1632+
getResponse().getRx16Response(response);
1633+
called = _onRx16Response.call(response);
1634+
} else if (id == RX_64_RESPONSE) {
1635+
Rx64Response response;
1636+
getResponse().getRx64Response(response);
1637+
called = _onRx64Response.call(response);
1638+
} else if (id == RX_16_IO_RESPONSE) {
1639+
Rx16IoSampleResponse response;
1640+
getResponse().getRx16IoSampleResponse(response);
1641+
called = _onRx16IoSampleResponse.call(response);
1642+
} else if (id == RX_64_IO_RESPONSE) {
1643+
Rx64IoSampleResponse response;
1644+
getResponse().getRx64IoSampleResponse(response);
1645+
called = _onRx64IoSampleResponse.call(response);
1646+
} else if (id == MODEM_STATUS_RESPONSE) {
1647+
ModemStatusResponse response;
1648+
getResponse().getModemStatusResponse(response);
1649+
called = _onModemStatusResponse.call(response);
1650+
} else if (id == AT_COMMAND_RESPONSE) {
1651+
AtCommandResponse response;
1652+
getResponse().getAtCommandResponse(response);
1653+
called = _onAtCommandResponse.call(response);
1654+
} else if (id == REMOTE_AT_COMMAND_RESPONSE) {
1655+
RemoteAtCommandResponse response;
1656+
getResponse().getRemoteAtCommandResponse(response);
1657+
called = _onRemoteAtCommandResponse.call(response);
1658+
}
1659+
1660+
if (!called)
1661+
_onOtherResponse.call(getResponse());
1662+
1663+
16051664
} else if (getResponse().isError()) {
16061665
_onPacketError.call(getResponse().getErrorCode());
16071666
}

XBee.h

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,20 +808,54 @@ class XBeeWithCallbacks : public XBee {
808808
/**
809809
* Register a packet error callback. It is called whenever an
810810
* error occurs in the packet reading process. Arguments to the
811-
* callback will be the error code and the data parameter.
812-
* while registering the callback.
811+
* callback will be the error code (as returned by
812+
* XBeeResponse::getErrorCode()) and the data parameter. while
813+
* registering the callback.
813814
*/
814815
void onPacketError(void (*func)(uint8_t, uintptr_t), uintptr_t data = 0) { _onPacketError.set(func, data); }
815816

816817
/**
817818
* Register a response received callback. It is called whenever
818-
* a response was succesfully received.
819+
* a response was succesfully received, before a response
820+
* specific callback (or onOtherResponse) below is called.
819821
*
820822
* Arguments to the callback will be the received response and
821823
* the data parameter passed while registering the callback.
822824
*/
823825
void onResponse(void (*func)(XBeeResponse&, uintptr_t), uintptr_t data = 0) { _onResponse.set(func, data); }
824826

827+
/**
828+
* Register an other response received callback. It is called
829+
* whenever a response was succesfully received, but no response
830+
* specific callback was registered using the functions below
831+
* (after the onResponse callback is called).
832+
*
833+
* Arguments to the callback will be the received response and
834+
* the data parameter passed while registering the callback.
835+
*/
836+
void onOtherResponse(void (*func)(XBeeResponse&, uintptr_t), uintptr_t data = 0) { _onOtherResponse.set(func, data); }
837+
838+
// These functions register a response specific callback. They
839+
// are called whenever a response of the appropriate type was
840+
// succesfully received (after the onResponse callback is
841+
// called).
842+
//
843+
// Arguments to the callback will be the received response
844+
// (already converted to the appropriate type) and the data
845+
// parameter passed while registering the callback.
846+
void onZBTxStatusResponse(void (*func)(ZBTxStatusResponse&, uintptr_t), uintptr_t data = 0) { _onZBTxStatusResponse.set(func, data); }
847+
void onZBRxResponse(void (*func)(ZBRxResponse&, uintptr_t), uintptr_t data = 0) { _onZBRxResponse.set(func, data); }
848+
void onZBExplicitRxResponse(void (*func)(ZBExplicitRxResponse&, uintptr_t), uintptr_t data = 0) { _onZBExplicitRxResponse.set(func, data); }
849+
void onZBRxIoSampleResponse(void (*func)(ZBRxIoSampleResponse&, uintptr_t), uintptr_t data = 0) { _onZBRxIoSampleResponse.set(func, data); }
850+
void onTxStatusResponse(void (*func)(TxStatusResponse&, uintptr_t), uintptr_t data = 0) { _onTxStatusResponse.set(func, data); }
851+
void onRx16Response(void (*func)(Rx16Response&, uintptr_t), uintptr_t data = 0) { _onRx16Response.set(func, data); }
852+
void onRx64Response(void (*func)(Rx64Response&, uintptr_t), uintptr_t data = 0) { _onRx64Response.set(func, data); }
853+
void onRx16IoSampleResponse(void (*func)(Rx16IoSampleResponse&, uintptr_t), uintptr_t data = 0) { _onRx16IoSampleResponse.set(func, data); }
854+
void onRx64IoSampleResponse(void (*func)(Rx64IoSampleResponse&, uintptr_t), uintptr_t data = 0) { _onRx64IoSampleResponse.set(func, data); }
855+
void onModemStatusResponse(void (*func)(ModemStatusResponse&, uintptr_t), uintptr_t data = 0) { _onModemStatusResponse.set(func, data); }
856+
void onAtCommandResponse(void (*func)(AtCommandResponse&, uintptr_t), uintptr_t data = 0) { _onAtCommandResponse.set(func, data); }
857+
void onRemoteAtCommandResponse(void (*func)(RemoteAtCommandResponse&, uintptr_t), uintptr_t data = 0) { _onRemoteAtCommandResponse.set(func, data); }
858+
825859
/**
826860
* Regularly call this method, which ensures that the serial
827861
* buffer is processed and the appropriate callbacks are called.
@@ -846,6 +880,19 @@ class XBeeWithCallbacks : public XBee {
846880

847881
Callback<uint8_t> _onPacketError;
848882
Callback<XBeeResponse&> _onResponse;
883+
Callback<XBeeResponse&> _onOtherResponse;
884+
Callback<ZBTxStatusResponse&> _onZBTxStatusResponse;
885+
Callback<ZBRxResponse&> _onZBRxResponse;
886+
Callback<ZBExplicitRxResponse&> _onZBExplicitRxResponse;
887+
Callback<ZBRxIoSampleResponse&> _onZBRxIoSampleResponse;
888+
Callback<TxStatusResponse&> _onTxStatusResponse;
889+
Callback<Rx16Response&> _onRx16Response;
890+
Callback<Rx64Response&> _onRx64Response;
891+
Callback<Rx16IoSampleResponse&> _onRx16IoSampleResponse;
892+
Callback<Rx64IoSampleResponse&> _onRx64IoSampleResponse;
893+
Callback<ModemStatusResponse&> _onModemStatusResponse;
894+
Callback<AtCommandResponse&> _onAtCommandResponse;
895+
Callback<RemoteAtCommandResponse&> _onRemoteAtCommandResponse;
849896
};
850897

851898
/**

0 commit comments

Comments
 (0)