/** Checks for new bytes available from the wireless radio * and processes all that are available. */ void processBytesFromRadio() { uint8 i, n; uint8 XDATA *rcv; rcv = radioLinkRxCurrentPacket(); if(rcv != NULL) { n = rcv[0]; // payload length for(i = 1; i <= n && radioLinkTxAvailable(); i++) { processByte(rcv[i]); } radioLinkRxDoneWithPacket(); } }
uint8 radioComTxAvailable(void) { if (sendSignalsSoon) { // We want to send the control signals ASAP, but have not yet been able to // queue a packet for them. Return 0 because we don't want to accept any // more data bytes until we queue up those control signals. This is part of // the plan to ensure that everything is processed in the right order. return 0; } else { // Assumption: If txBytesLoaded is non-zero, radioLinkTxAvailable will be non-zero, // so the subtraction below does not overflow. // Assumption: The multiplication below does not overflow ever. return radioLinkTxAvailable()*RADIO_LINK_PAYLOAD_SIZE - txBytesLoaded; } }
void radioComTxService(void) { if (radioLinkResetPacketReceived) { // The other device has sent us a reset packet, which means it has been // reset. We should send the state of the control signals to it. radioLinkResetPacketReceived = 0; sendSignalsSoon = 1; } if (sendSignalsSoon) { // We want to send the control signals ASAP. // NOTE: The if statement below could probably be moved to radioComTxControlSignals() // and then you could add the assumption here that txBytesLoaded is 0 // (we are not in the middle of populating a data packet). if (txBytesLoaded != 0) { // There is normal data that needs to be sent before the control signals, // so send it now. radioComSendDataNow(); } if (radioLinkTxAvailable()) { radioComSendControlSignalsNow(); } } else { // We don't need to send control signals ASAP, so we use the normal policy // for sending data: only send a non-full packet if the number of packets // queued in the lower level drops below the TX_QUEUE_THRESHOLD. if (txBytesLoaded != 0 && radioLinkTxQueued() <= TX_QUEUE_THRESHOLD) { radioComSendDataNow(); } } }