opportunistic-xbee/xbee_wrapper.cpp

62 lines
1.3 KiB
C++

#include "xbee_wrapper.h"
XBeeWrapper::XBeeWrapper(){ xbee = XBee(); };
void XBeeWrapper::begin(const 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 ){
Rx16Response res = Rx16Response();
xbee.getResponse().getRx16Response(res);
// extract data and propagate error
if( pkt.read(res.getData(), res.getDataLength()) != PKTREAD_OK)
return XBWRECV_ERROR;
} else {
Rx64Response res = Rx64Response();
xbee.getResponse().getRx64Response(res);
// extract data and propagate error
if( pkt.read(res.getData(), res.getDataLength()) != PKTREAD_OK)
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 = malloc(6 * sizeof(uint8_t));
size_t payload_size = pkt.write(payload);
// send
Tx64Request tx = Tx64Request(bcast, payload, payload_size);
xbee.send(tx);
free(payload);
return XBWSEND_OK;
};