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.

Dependencies:   BufferedArray

Dependents:   MBEDminiproject

Files at this revision

API Documentation at this revision

Comitter:
yangcq88517
Date:
Thu Oct 22 20:02:11 2015 +0000
Parent:
0:837e6c48e90d
Child:
2:700dc65ca3b1
Commit message:
bug fix

Changed in this revision

Core/APIFrame.cpp Show annotated file Show diff for this revision Revisions of this file
Core/APIFrame.h Show annotated file Show diff for this revision Revisions of this file
Core/BufferedArray.cpp Show annotated file Show diff for this revision Revisions of this file
Core/CoreAPI.cpp Show annotated file Show diff for this revision Revisions of this file
Core/CoreAPI.h Show annotated file Show diff for this revision Revisions of this file
Core/ISerial.h Show annotated file Show diff for this revision Revisions of this file
--- a/Core/APIFrame.cpp	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/APIFrame.cpp	Thu Oct 22 20:02:11 2015 +0000
@@ -7,8 +7,10 @@
 APIFrame::APIFrame(APIFrame * frame)
     :BufferedArray(frame)
 {
-    this->checkSum = frame->checkSum;
-    this->isVerify = frame->isVerify;
+    if (frame != NULL) {
+        this->checkSum = frame->checkSum;
+        this->isVerify = frame->isVerify;
+    }
 }
 
 char APIFrame::getFrameType()
@@ -16,7 +18,10 @@
     return data[0];
 }
 
-void APIFrame::setFrameType(char identifier) {}
+void APIFrame::setFrameType(char identifier)
+{
+    data[0] = identifier;
+}
 
 void APIFrame::allocate(int length)
 {
--- a/Core/APIFrame.h	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/APIFrame.h	Thu Oct 22 20:02:11 2015 +0000
@@ -3,14 +3,14 @@
 
 #include "BufferedArray.h"
 
+/// An API frame is the structured data sent and received through the serial interface of the radio module when it is configured in API or API escaped operating modes. API frames are used to communicate with the module or with other modules in the network.
 class APIFrame : public BufferedArray
 {
 private:
+    /// Checksum value for the API fram.
     char checkSum;
 
-    /// <summary>
-    /// a state to indicate whether this packet's checksum is verified while process
-    /// </summary>
+    /// A state to indicate whether this packet's checksum is verified while process.
     bool isVerify;
 
 public:
@@ -48,7 +48,8 @@
 
     APIFrame(APIFrame * frame);
 
-    /**
+    /** Get the API frame type.
+    *
     * @returns
     *    Tx64_Request =0x00,
     *    Tx16_Request =0x01,
@@ -114,12 +115,23 @@
     */
     void sets(int position, const char * value, int offset, int length);
 
+    /** Get checksum.
+    * @returns the checksum value
+    */
     char getCheckSum();
 
+    /** Set checksum.
+    * @param value checksum value
+    */
     void setCheckSum(char value);
 
+    /** Check is the API frame's checksum is verified.
+    * @returns true checksum match,
+    *          false checksum not match
+    */
     bool verifyChecksum();
 
+    /// Calculate the checksum value.
     void calculateChecksum();
 };
 
--- a/Core/BufferedArray.cpp	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/BufferedArray.cpp	Thu Oct 22 20:02:11 2015 +0000
@@ -64,7 +64,8 @@
         return;
 
     if (length > max) {
-        delete[] data;
+        if (data != NULL)
+            delete[] data;
         data = new char[length];
     }
 
--- a/Core/CoreAPI.cpp	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/CoreAPI.cpp	Thu Oct 22 20:02:11 2015 +0000
@@ -9,19 +9,17 @@
     this->serial = serial;
     isEscapeMode = escape;
     msg = new APIFrame(INITIAL_FRAME_LENGTH);
+    isChecksum = false;
     isRunning = false;
-    isChecksum = false;
 }
 
 CoreAPI::~CoreAPI()
 {
-    delete msg;
-}
-
+    if (msg != NULL)
+        delete msg;
 
-void CoreAPI::setVerifyChecksum(bool isCheck)
-{
-    isChecksum = isCheck;
+    if (serial != NULL)
+        delete serial;
 }
 
 void CoreAPI::start()
@@ -42,6 +40,11 @@
     }
 }
 
+void CoreAPI::setVerifyChecksum(bool isCheck)
+{
+    isChecksum = isCheck;
+}
+
 void CoreAPI::send(APIFrame * request)
 {
     if (!isRunning)
@@ -65,7 +68,7 @@
 {
     int value = serial->readByte();
 
-    if (isEscapeMode)
+    if (isEscapeMode && value == ESCAPED)
         return serial->readByte() ^ 0x20;
 
     return value;
@@ -86,9 +89,14 @@
 
 APIFrame * CoreAPI::getResponse()
 {
-    if (readByte() != KEY)
+    if (!isRunning)
         return NULL;
 
+    if (!serial->peek())
+        return NULL;
+
+    while (serial->readByte() != KEY);
+
     int length = getLength();
 
     msg->allocate(length);
--- a/Core/CoreAPI.h	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/CoreAPI.h	Thu Oct 22 20:02:11 2015 +0000
@@ -16,7 +16,6 @@
 #include "ZigBeeExplicitTxRequest.h"
 #include "ZigBeeTxRequest.h"
 
-
 #include "XBeeRx64Indicator.h"
 #include "XBeeRx16Indicator.h"
 #include "XBeeRx64IOSampleIndicator.h"
@@ -34,6 +33,7 @@
 #include "RouteRecordIndicator.h"
 #include "ManyToOneRouteIndicator.h"
 
+/// The core API class which responseable for processing frame data, but not the serial operation.
 class CoreAPI
 {
 private:
@@ -68,22 +68,23 @@
 
 protected:
 
-    /// <summary>
-    /// read one byte payload, which allready handle the escape char, if less than 0 means error occured
-    /// </summary>
-    /// <returns></returns>
+    /** Read one byte payload, which allready handle the escape char, if less than 0 means error occured
+    * @returns if less than 0 means error occured.
+    */
     int readByte();
 
-    /// <summary>
-    /// write one byte to the payload, which allready handle the escape char
-    /// </summary>
-    /// <param name="data"></param>
+    /** Write one byte to the payload, which allready handle the escape char.
+    * @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);
 
 public:
@@ -91,59 +92,125 @@
 
     ~CoreAPI();
 
-    /// <summary>
-    /// get or set whether to verify receive packet's checksum
-    /// </summary>
+    /** Set whether to verify checksum during receiving, default is not verify.
+    *
+    * @param isCheck true only to process API frame when checksum matches.
+    *                false ignore the checksum.
+    */
     void setVerifyChecksum(bool isCheck);
 
-    /// <summary>
-    /// to start send and process response, must be called before any function
-    /// </summary>
+    /// Start send and process response, must call this method before starting processing data.
     void start();
 
-    /// <summary>
-    /// stop so the serial port can be used for other purpose
-    /// </summary>
+    /// Stop the serial port.
     void stop();
 
-    /// <summary>
-    /// a general function to send frame out, do not process response
-    /// </summary>
-    /// <param name="request"></param>
+    /** A general function to send frame out
+    *
+    * @param request any API frame
+    */
     void send(APIFrame * request);
 
+    /** Read the next avaliable API frame, and the type of fram can be retrieved from getFrameType().
+    *
+    * @returns a API frame, NULL means data not avaliable.
+    */
     APIFrame * getResponse();
 
+    /** Read the next avaliable API frame.
+    *
+    * @returns a API frame, NULL means data not avaliable.
+    */
     XBeeRx64Indicator * getXBeeRx64();
 
+    /** Read the next avaliable API frame.
+    *
+    * @returns a API frame, NULL means data not avaliable.
+    */
     XBeeRx16Indicator * getXBeeRx16();
 
+    /** Read the next avaliable API frame.
+    *
+    * @returns a API frame, NULL means data not avaliable.
+    */
     XBeeRx64IOSampleIndicator * getXBeeRx64IOSample();
 
+    /** Read the next avaliable API frame.
+    *
+    * @returns a API frame, NULL means data not avaliable.
+    */
     XBeeRx16IOSampleIndicator * getXBeeRx16IOSample();
 
+    /** Read the next avaliable API frame.
+    *
+    * @returns a API frame, NULL means data not avaliable.
+    */
     XBeeTxStatusIndicator * getXBeeTxStatus();
 
+    /** Read the next avaliable API frame.
+    *
+    * @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();
 };
 
--- a/Core/ISerial.h	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/ISerial.h	Thu Oct 22 20:02:11 2015 +0000
@@ -1,26 +1,31 @@
 #ifndef UK_AC_HERTS_SMARTLAB_XBEE_ISerial
 #define UK_AC_HERTS_SMARTLAB_XBEE_ISerial
 
+/// Serial data interface.
 class ISerial
 {
 public:
-    /// <summary>
-    /// if success return non zero, -1 means something is wrong
-    /// </summary>
-    /// <returns></returns>
+    /** Read one byte data from the serila port.
+    * @returns if success return non zero [0x00-0xFF], -1 means something is wrong
+    */
     virtual int readByte() = 0;
 
+    /** write one byte data to the serila port.
+    * @param data [0x00-0xFF]
+    */
     virtual void writeByte(char data) = 0;
 
-    /// <summary>
-    /// check if the serial port is already open
-    /// </summary>
-    /// <returns></returns>
+    /// Check if the serial port is already open.
     virtual bool isOpen() = 0;
 
+    /// Open the serila port.
     virtual void open() = 0;
 
+    /// Close the serila port.
     virtual void close() = 0;
+    
+    /// Check if data is avaliable to read. 
+    virtual bool peek() = 0;
 };
 
 #endif
\ No newline at end of file