XBee and XBee-PRO ZigBee RF modules provide cost-effective wireless connectivity to electronic devices. They are interoperable with other ZigBee PRO feature set devices, including devices from other vendors.
Revision 3:6b205ec8624b, committed 2015-10-26
- Comitter:
- yangcq88517
- Date:
- Mon Oct 26 18:04:12 2015 +0000
- Parent:
- 2:700dc65ca3b1
- Child:
- 4:a0f1fba6c2fb
- Commit message:
- bug fix add new functions
Changed in this revision
--- a/Core/APIFrame.cpp Thu Oct 22 22:22:12 2015 +0000 +++ b/Core/APIFrame.cpp Mon Oct 26 18:04:12 2015 +0000 @@ -13,7 +13,7 @@ } } -char APIFrame::getFrameType() +int APIFrame::getFrameType() { return data[0]; }
--- a/Core/APIFrame.h Thu Oct 22 22:22:12 2015 +0000 +++ b/Core/APIFrame.h Mon Oct 26 18:04:12 2015 +0000 @@ -79,7 +79,7 @@ * Device_Authenticated_Indicator = 0xA2, * Many_to_One_Route_Request_Indicator = 0xA3, */ - char getFrameType(); + int getFrameType(); void setFrameType(char identifier);
--- a/Core/CoreAPI.cpp Thu Oct 22 22:22:12 2015 +0000 +++ b/Core/CoreAPI.cpp Mon Oct 26 18:04:12 2015 +0000 @@ -286,4 +286,294 @@ if (manyToOneRouteIndicator.convert(msg)) return &manyToOneRouteIndicator; else return NULL; -} \ No newline at end of file +} + +XBeeTxStatusIndicator * CoreAPI::sendXBeeTx16(Address * remoteAddress, OptionsBase * option, const char * payload, int offset, int length) +{ + if (!isRunning) + return NULL; + + waitFrameID++; + if (waitFrameID == 0) + waitFrameID = 0x01; + + msg->rewind(); + msg->set(APIFrame::Tx16_Request); + msg->set(waitFrameID); + msg->set(remoteAddress->getNetworkAddress() >> 8); + msg->set(remoteAddress->getNetworkAddress()); + msg->set(option->getValue()); + msg->sets(payload, offset, length); + + send(msg); + + timer.start(); + for (int i = 0; i < 3; i++) { + timer.reset(); + while (!serial->peek()) { + if (timer.read_ms() > 1000) { + timer.stop(); + return NULL; + } + } + + getResponse(); + + if (xBeeTxStatusIndicator.convert(msg) && xBeeTxStatusIndicator.getFrameID() == waitFrameID) { + timer.stop(); + return &xBeeTxStatusIndicator; + } + } + timer.stop(); + return NULL; +} + +XBeeTxStatusIndicator * CoreAPI::sendXBeeTx64(Address * remoteAddress, OptionsBase * option, const char * payload, int offset, int length) +{ + if (!isRunning) + return NULL; + + waitFrameID++; + if (waitFrameID == 0) + waitFrameID = 0x01; + + msg->rewind(); + msg->set(APIFrame::Tx64_Request); + msg->set(waitFrameID); + msg->sets(remoteAddress->getAddressValue(), 0, 8); + msg->set(option->getValue()); + msg->sets(payload, offset, length); + + send(msg); + + timer.start(); + for (int i = 0; i < 3; i++) { + timer.reset(); + while (!serial->peek()) { + if (timer.read_ms() > 1000) { + timer.stop(); + return NULL; + } + } + + getResponse(); + + if (xBeeTxStatusIndicator.convert(msg) && xBeeTxStatusIndicator.getFrameID() == waitFrameID) { + timer.stop(); + return &xBeeTxStatusIndicator; + } + } + timer.stop(); + return NULL; +} + +ATCommandIndicator * CoreAPI::sendATCommand(const char * command, bool applyChange, const char * parameter, int offset, int length) +{ + waitFrameID++; + if (waitFrameID == 0) + waitFrameID = 0x01; + + msg->rewind(); + if (applyChange) + msg->set(APIFrame::AT_Command); + else msg->set(APIFrame::AT_Command_Queue_Parameter_Value); + msg->set(waitFrameID); + msg->sets(command, 0 , 2); + if (parameter != NULL) + msg->sets(parameter, offset, length); + + send(msg); + + timer.start(); + for (int i = 0; i < 3; i++) { + timer.reset(); + while (!serial->peek()) { + if (timer.read_ms() > 1000) { + timer.stop(); + return NULL; + } + } + + getResponse(); + + if (aTCommandIndicator.convert(msg) && aTCommandIndicator.getFrameID() == waitFrameID) { + timer.stop(); + return &aTCommandIndicator; + } + } + timer.stop(); + return NULL; +} + +RemoteCommandIndicator * CoreAPI::sendRemoteATCommand(Address * remoteAddress, const char * command, OptionsBase * transmitOptions, const char * parameter, int parameterOffset, int parameterLength) +{ + waitFrameID++; + if (waitFrameID == 0) + waitFrameID = 0x01; + + msg->rewind(); + msg->set(APIFrame::Remote_Command_Request); + msg->set(waitFrameID); + + msg->sets(remoteAddress->getAddressValue(), 0, 10); + msg->set(transmitOptions->getValue()); + msg->sets(command, 0, 2); + + if (parameter != NULL) + msg->sets(parameter, parameterOffset, parameterLength); + + send(msg); + + timer.start(); + for (int i = 0; i < 3; i++) { + timer.reset(); + while (!serial->peek()) { + if (timer.read_ms() > 1000) { + timer.stop(); + return NULL; + } + } + + getResponse(); + + if (remoteCommandIndicator.convert(msg) && remoteCommandIndicator.getFrameID() == waitFrameID) { + timer.stop(); + return &remoteCommandIndicator; + } + } + timer.stop(); + return NULL; +} + +ZigBeeTxStatusIndicator * CoreAPI::sendZigBeeTx(Address * remoteAddress, OptionsBase * option, const char * payload, int offset, int length) +{ + waitFrameID++; + if (waitFrameID == 0) + waitFrameID = 0x01; + + msg->rewind(); + msg->set(APIFrame::ZigBee_Transmit_Request); + msg->set(waitFrameID); + msg->sets(remoteAddress->getAddressValue(), 0, 10); + msg->set(0x00); + msg->set(option->getValue()); + msg->sets(payload, offset, length); + + send(msg); + + timer.start(); + for (int i = 0; i < 3; i++) { + timer.reset(); + while (!serial->peek()) { + if (timer.read_ms() > 1000) { + timer.stop(); + return NULL; + } + } + + getResponse(); + + if (zigBeeTxStatusIndicator.convert(msg) && zigBeeTxStatusIndicator.getFrameID() == waitFrameID) { + timer.stop(); + return &zigBeeTxStatusIndicator; + } + } + timer.stop(); + return NULL; +} + +ZigBeeTxStatusIndicator * CoreAPI::sendZigBeeExplicitTx(ExplicitAddress * remoteAddress, OptionsBase * option, const char * payload, int offset, int length) +{ + waitFrameID++; + if (waitFrameID == 0) + waitFrameID = 0x01; + + msg->rewind(); + msg->set(APIFrame::Explicit_Addressing_ZigBee_Command_Frame); + msg->set(waitFrameID); + msg->sets(remoteAddress->getAddressValue(), 0, 10); + msg->sets(remoteAddress->getExplicitValue(), 0 , 6); + msg->set(0x00); + msg->set(option->getValue()); + msg->sets(payload, offset, length); + + send(msg); + + timer.start(); + for (int i = 0; i < 3; i++) { + timer.reset(); + while (!serial->peek()) { + if (timer.read_ms() > 1000) { + timer.stop(); + return NULL; + } + } + + getResponse(); + + if (zigBeeTxStatusIndicator.convert(msg) && zigBeeTxStatusIndicator.getFrameID() == waitFrameID) { + timer.stop(); + return &zigBeeTxStatusIndicator; + } + } + timer.stop(); + return NULL; +} + +ATCommandIndicator * CoreAPI::setPinFunction(Pin * pin, char function) +{ + return sendATCommand(pin->getCommand(), true, &function, 0 , 1); +} + +ATCommandIndicator * CoreAPI::setIODetection(Pin * pins, int size) +{ + return sendATCommand(ATCommands::Digital_IO_Change_Detection, true, Pin::IOChangeDetectionConfiguration(pins, size)); +} + +RemoteCommandIndicator * CoreAPI::setRemotePinFunction(Address * remoteAddress, Pin * pin, char function) +{ + return sendRemoteATCommand(remoteAddress, pin->getCommand(), &RemoteCommandOptions::ApplyChanges, &function, 0 , 1); +} + +RemoteCommandIndicator * CoreAPI::setRemoteIODetection(Address * remoteAddress, Pin * pins, int size) +{ + return sendRemoteATCommand(remoteAddress, ATCommands::Digital_IO_Change_Detection, &RemoteCommandOptions::ApplyChanges, Pin::IOChangeDetectionConfiguration(pins, size)); +} + +bool CoreAPI::forceXBeeLocalIOSample() +{ + ATCommandIndicator * re = sendATCommand(ATCommands::Force_Sample, true); + + if (re == NULL) + return false; + + if (re->getCommandStatus() != 0x00) + return false; + + return true; +} + +IOSamples * CoreAPI::forceZigBeeLocalIOSample() +{ + ATCommandIndicator * re = sendATCommand(ATCommands::Force_Sample, true); + + if (re == NULL) + return NULL; + + return IOSampleDecoder::ZigBeeSamplesParse(re->getParameter()); +} + +IOSamples * CoreAPI::forceXBeeRemoteIOSample(Address * remote) +{ + RemoteCommandIndicator * re = sendRemoteATCommand(remote, ATCommands::Force_Sample, &OptionsBase::DEFAULT); + + return IOSampleDecoder::XBeeSamplesParse(re->getParameter()); +} + +IOSamples * CoreAPI::forceZigBeeRemoteIOSample(Address * remote) +{ + RemoteCommandIndicator * re = sendRemoteATCommand(remote, ATCommands::Force_Sample, &OptionsBase::DEFAULT); + + return IOSampleDecoder::ZigBeeSamplesParse(re->getParameter()); +} +
--- a/Core/CoreAPI.h Thu Oct 22 22:22:12 2015 +0000 +++ b/Core/CoreAPI.h Mon Oct 26 18:04:12 2015 +0000 @@ -47,6 +47,9 @@ bool isEscapeMode; bool isRunning; bool isChecksum; + Timer timer; + + char waitFrameID; APIFrame * msg; XBeeRx64Indicator xBeeRx64Indicator; @@ -77,13 +80,13 @@ * @param data one byte [0x00-0xFF] */ void writeByte(char data); - + /// Processing API frame. void packetProcess(); - + /// Get the next avaliable API frame length. int getLength(); - + /// Read the next avaliable API frame data. void readPayLoad(int length); @@ -152,85 +155,132 @@ * @returns a API frame, NULL means data not avaliable. */ ATCommandIndicator * getATCommand(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ ModemStatusIndicator * getModemStatus(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ ZigBeeTxStatusIndicator * getZigBeeTxStatus(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ ZigBeeRxIndicator * getZigBeeRx(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ ZigBeeExplicitRxIndicator * getZigBeeExplicitRx(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ ZigBeeIOSampleIndicator * getZigBeeIOSample(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ SensorReadIndicator * getSensorRead(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ NodeIdentificationIndicator * getNodeIdentification(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ RemoteCommandIndicator * getRemoteCommand(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ RouteRecordIndicator * getRouteRecord(); - + /** Read the next avaliable API frame. * * @returns a API frame, NULL means data not avaliable. */ ManyToOneRouteIndicator * getManyToOneRoute(); + + XBeeTxStatusIndicator * sendXBeeTx16(Address * remoteAddress, OptionsBase * option, const char * payload, int offset, int length); + + XBeeTxStatusIndicator * sendXBeeTx64(Address * remoteAddress, OptionsBase * option, const char * payload, int offset, int length); + + ATCommandIndicator * sendATCommand(const char * command, bool applyChange, const char * parameter = NULL, int offset = 0, int length = 0); + + RemoteCommandIndicator * sendRemoteATCommand(Address * remoteAddress, const char * command, OptionsBase * transmitOptions, const char * parameter = NULL, int parameterOffset = 0, int parameterLength = 0); + + ZigBeeTxStatusIndicator * sendZigBeeTx(Address * remoteAddress, OptionsBase * option, const char * payload, int offset, int length); + + ZigBeeTxStatusIndicator * sendZigBeeExplicitTx(ExplicitAddress * remoteAddress, OptionsBase * option, const char * payload, int offset, int length); + + /** + * @param function + *DISABLED = 0x00, + *RESERVED_FOR_PIN_SPECIFIC_ALTERNATE_FUNCTIONALITIES = 0x01, + *ANALOG_INPUT_SINGLE_ENDED = 0x02, + *DIGITAL_INPUT_MONITORED = 0x03, + *DIGITAL_OUTPUT_DEFAULT_LOW = 0x04, + *DIGITAL_OUTPUT_DEFAULT_HIGH = 0x05, + *ALTERNATE_FUNCTIONALITIES_WHERE_APPLICABLE = 0x06//0x06~0x09 + */ + ATCommandIndicator * setPinFunction(Pin * pin, char function); + + ATCommandIndicator * setIODetection(Pin * pins, int size); + + /** + * @param function + *DISABLED = 0x00, + *RESERVED_FOR_PIN_SPECIFIC_ALTERNATE_FUNCTIONALITIES = 0x01, + *ANALOG_INPUT_SINGLE_ENDED = 0x02, + *DIGITAL_INPUT_MONITORED = 0x03, + *DIGITAL_OUTPUT_DEFAULT_LOW = 0x04, + *DIGITAL_OUTPUT_DEFAULT_HIGH = 0x05, + *ALTERNATE_FUNCTIONALITIES_WHERE_APPLICABLE = 0x06//0x06~0x09 + */ + RemoteCommandIndicator * setRemotePinFunction(Address * remoteAddress, Pin * pin, char function); + + RemoteCommandIndicator * setRemoteIODetection(Address * remoteAddress, Pin * pins, int size); + + /// <summary> + /// The command will immediately return an "OK" response. The data will follow in the normal API format for DIO data event. + /// </summary> + /// <returns>true if the command is "OK", false if no IO is enabled.</returns> + bool forceXBeeLocalIOSample(); + + /// <summary> + /// Return 1 IO sample from the local module. + /// </summary> + /// <returns></returns> + IOSamples * forceZigBeeLocalIOSample(); + + /// <summary> + /// Return 1 IO sample only, Samples before TX (IT) does not affect. + /// </summary> + /// <param name="remote"Remote address of the device></param> + /// <returns></returns> + IOSamples * forceXBeeRemoteIOSample(Address * remote); + + /// <summary> + /// Return 1 IO sample only. + /// </summary> + /// <param name="remote">Remote address of the device</param> + /// <returns></returns> + IOSamples * forceZigBeeRemoteIOSample(Address * remote); }; - - - - - - - - - - - - - - - - - - - -#endif \ No newline at end of file +#endif
--- a/Type/ATCommands.cpp Thu Oct 22 22:22:12 2015 +0000 +++ b/Type/ATCommands.cpp Mon Oct 26 18:04:12 2015 +0000 @@ -12,4 +12,6 @@ char * ATCommands::Node_Identifier = "NI"; -char * ATCommands::Node_Join_Time = "NJ"; \ No newline at end of file +char * ATCommands::Node_Join_Time = "NJ"; + +char * ATCommands::Force_Sample = "IS"; \ No newline at end of file
--- a/Type/ATCommands.h Thu Oct 22 22:22:12 2015 +0000 +++ b/Type/ATCommands.h Mon Oct 26 18:04:12 2015 +0000 @@ -17,6 +17,8 @@ static char * Node_Identifier; static char * Node_Join_Time; + + static char * Force_Sample; }; #endif \ No newline at end of file