2018-12-03 09:54:53 +00:00
|
|
|
#include "xbee_wrapper.h"
|
|
|
|
|
|
|
|
|
|
|
|
XBeeWrapper::XBeeWrapper(){ xbee = XBee(); };
|
|
|
|
|
|
|
|
|
|
|
|
void XBeeWrapper::begin(unsigned long baud){
|
|
|
|
xbee.setSerial(Serial1);
|
|
|
|
Serial1.begin(baud);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t XBeeWrapper::receive(Packet& pkt){
|
|
|
|
xbee.readPacket();
|
|
|
|
|
|
|
|
if( !xbee.getResponse().isAvailable() )
|
|
|
|
return XBWRECV_NONE;
|
|
|
|
|
|
|
|
uint8_t apiId = xbee.getResponse().getApiId();
|
|
|
|
if( apiId != RX_16_RESPONSE && apiId != RX_64_RESPONSE )
|
|
|
|
return XBWRECV_NONE;
|
|
|
|
|
|
|
|
if( apiId == RX_16_RESPONSE ){
|
|
|
|
|
2018-12-03 14:29:56 +00:00
|
|
|
Rx16Response res = Rx16Response();
|
2018-12-03 09:54:53 +00:00
|
|
|
xbee.getResponse().getRx16Response(res);
|
|
|
|
|
|
|
|
// extract data and propagate error
|
2018-12-03 14:29:56 +00:00
|
|
|
if( pkt.read(res.getData(), res.getDataLength()) != PKTREAD_OK)
|
2018-12-03 09:54:53 +00:00
|
|
|
return XBWRECV_ERROR;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2018-12-03 14:29:56 +00:00
|
|
|
Rx64Response res = Rx64Response();
|
2018-12-03 09:54:53 +00:00
|
|
|
xbee.getResponse().getRx64Response(res);
|
|
|
|
|
|
|
|
// extract data and propagate error
|
2018-12-03 14:29:56 +00:00
|
|
|
if( pkt.read(res.getData(), res.getDataLength()) != PKTREAD_OK)
|
2018-12-03 09:54:53 +00:00
|
|
|
return XBWRECV_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return XBWRECV_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t XBeeWrapper::broadcast(Packet& pkt){
|
|
|
|
// build broadcast addr
|
|
|
|
XBeeAddress64 bcast = XBeeAddress64(0x00000000, 0x0000FFFF);
|
|
|
|
|
|
|
|
// build payload from packet
|
|
|
|
uint8_t payload[PROTO_SIZE];
|
|
|
|
size_t payload_size = pkt.write(payload);
|
|
|
|
|
|
|
|
// send
|
|
|
|
Tx64Request tx = Tx64Request(bcast, payload, payload_size);
|
|
|
|
xbee.send(tx);
|
|
|
|
|
|
|
|
// // check status
|
|
|
|
// TxStatusResponse txStatus = TxStatusResponse();
|
|
|
|
// xbee.readPacket();
|
|
|
|
// if( xbee.getResponse().isAvailable() ){
|
|
|
|
// if( xbee.getResponse().getApiId() == TX_STATUS_RESPONSE ){
|
|
|
|
// xbee.getResponse().getTxStatusResponse(txStatus);
|
|
|
|
// return txStatus.isSuccess() ? XBWSEND_OK : XBWSEND_ERROR;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
return XBWSEND_OK;
|
|
|
|
};
|