00001 /*! \file WaspXBeeCore.h 00002 \brief Library for managing the XBee modules 00003 00004 Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. 00005 http://www.libelium.com 00006 00007 This program is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation, either version 2.1 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 00020 Version: 0.2 00021 00022 Design: David Gascón 00023 00024 Implementation: Alberto Bielsa 00025 00026 */ 00027 00028 /*! \def WaspXBeeCore_h 00029 \brief The library flag 00030 00031 */ 00032 #ifndef WaspXBeeCore_h 00033 #define WaspXBeeCore_h 00034 00035 /****************************************************************************** 00036 * Includes 00037 ******************************************************************************/ 00038 00039 #include <string.h> 00040 #include <stdint.h> 00041 #include <stdlib.h> 00042 #include "WConstants.h" 00043 #include "WaspXBee.h" 00044 #include "WaspUSB.h" 00045 #include "pins_waspmote.h" 00046 #include <inttypes.h> 00047 00048 #ifndef __WASPXBEECONSTANTS_H__ 00049 #include "WaspXBeeConstants.h" 00050 #endif 00051 00052 /****************************************************************************** 00053 * Definitions & Declarations 00054 ******************************************************************************/ 00055 00056 //! Structure : used for storing the information returned by other nodes when a Node Discovery is performed 00057 /*! 00058 */ 00059 typedef struct Node 00060 { 00061 //! Structure Variable : 16b Network Address 00062 /*! 00063 */ 00064 uint8_t MY[2]; 00065 00066 //! Structure Variable : 32b Lower Mac Source 00067 /*! 00068 */ 00069 uint8_t SH[4]; 00070 00071 //! Structure Variable : 32b Higher Mac Source 00072 /*! 00073 */ 00074 uint8_t SL[4]; 00075 00076 //! Structure Variable : Node Identifier 00077 /*! 00078 */ 00079 char NI[20]; 00080 00081 //! Structure Variable : Parent 16b Network Address (ZigBee) 00082 /*! 00083 */ 00084 uint8_t PMY[2]; 00085 00086 //! Structure Variable : Device Type: 0=End 1=Router 2=Coord (ZigBee) 00087 /*! 00088 */ 00089 uint8_t DT; 00090 00091 //! Structure Variable : Status: Reserved (ZigBee) 00092 /*! 00093 */ 00094 uint8_t ST; 00095 00096 //! Structure Variable : Profile ID (ZigBee) 00097 /*! 00098 */ 00099 uint8_t PID[2]; 00100 00101 //! Structure Variable : Manufacturer ID (ZigBee) 00102 /*! 00103 */ 00104 uint8_t MID[2]; 00105 00106 //! Structure Variable : Receive Signal Strength Indicator 00107 /*! 00108 */ 00109 uint8_t RSSI; 00110 }; 00111 00112 //! Structure : used for storing the information needed to send or receive a packet, such as the addresses and data 00113 /*! 00114 */ 00115 typedef struct packetXBee 00116 { 00117 private: 00118 public: 00119 /************ IN ***********/ 00120 //! Structure Variable : 32b Lower Mac Destination 00121 /*! 00122 */ 00123 uint8_t macDL[4]; 00124 00125 //! Structure Variable : 32b Higher Mac Destination 00126 /*! 00127 */ 00128 uint8_t macDH[4]; 00129 00130 //! Structure Variable : Sending Mode -> 0=unicast ; 1=broadcast ; 2=cluster ; 3=synchronization 00131 /*! 00132 */ 00133 uint8_t mode; 00134 00135 //! Structure Variable : Address Type -> 0=16B ; 1=64B 00136 /*! 00137 */ 00138 uint8_t address_type; 00139 00140 //! Structure Variable : 16b Network Address Destination 00141 /*! 00142 */ 00143 uint8_t naD[2]; 00144 00145 //! Structure Variable : Data. All the data here, even when it is > payload 00146 /*! 00147 */ 00148 char data[MAX_DATA]; 00149 00150 //! Structure Variable : Real data length. 00151 /*! 00152 */ 00153 uint16_t data_length; 00154 00155 //! Structure Variable : Fragment length. Used to send each fragment 00156 /*! 00157 */ 00158 uint16_t frag_length; 00159 00160 //! Structure Variable : Source Endpoint (ZigBee) 00161 /*! 00162 */ 00163 uint8_t SD; 00164 00165 //! Structure Variable : Destination Endpoint (ZigBee) 00166 /*! 00167 */ 00168 uint8_t DE; 00169 00170 //! Structure Variable : Cluster Identifier (ZigBee) 00171 /*! 00172 */ 00173 uint8_t CID[2]; 00174 00175 //! Structure Variable : Profile Identifier (ZigBee) 00176 /*! 00177 */ 00178 uint8_t PID[2]; 00179 00180 //! Structure Variable : Specifies if Network Address is known -> 0=unknown net address ; 1=known net address 00181 /*! 00182 */ 00183 uint8_t MY_known; 00184 00185 //! Structure Variable : Sending options (depends on the XBee module) 00186 /*! 00187 */ 00188 uint8_t opt; 00189 00190 /******** APLICATION *******/ 00191 00192 //! Structure Variable : Application Level ID 00193 /*! 00194 */ 00195 uint8_t packetID; 00196 00197 //! Structure Variable : 32b Lower Mac Source 00198 /*! 00199 */ 00200 uint8_t macSL[4]; 00201 00202 //! Structure Variable : 32b Higher Mac Source 00203 /*! 00204 */ 00205 uint8_t macSH[4]; 00206 00207 //! Structure Variable : 16b Network Address Source 00208 /*! 00209 */ 00210 uint8_t naS[2]; 00211 00212 //! Structure Variable : 32b Lower Mac Origin Source 00213 /*! 00214 */ 00215 uint8_t macOL[4]; 00216 00217 //! Structure Variable : 32b Higher Mac Origin Source 00218 /*! 00219 */ 00220 uint8_t macOH[4]; 00221 00222 //! Structure Variable : 16b Network Address origin 00223 /*! 00224 */ 00225 uint8_t naO[2]; 00226 00227 //! Structure Variable : Node Identifier Origin. To use in transmission, it must finish in "#". 00228 /*! 00229 */ 00230 char niO[21]; 00231 00232 //! Structure Variable : Receive Signal Strength Indicator 00233 /*! 00234 */ 00235 uint8_t RSSI; 00236 00237 //! Structure Variable : Address Source Type 00238 /*! 00239 */ 00240 uint8_t address_typeS; 00241 00242 //! Structure Variable : Source Type ID 00243 /*! 00244 */ 00245 uint8_t typeSourceID; 00246 00247 //! Structure Variable : Fragment number for ordering the global packet 00248 /*! 00249 */ 00250 uint8_t numFragment; 00251 00252 //! Structure Variable : Specifies if it is the last fragment of a packet 00253 /*! 00254 */ 00255 uint8_t endFragment; 00256 00257 //! Structure Variable : Specifies the time when the first fragment of the packet was received 00258 /*! 00259 */ 00260 long time; 00261 00262 00263 /******** OUT **************/ 00264 00265 //! Structure Variable : Delivery Status 00266 /*! 00267 */ 00268 uint8_t deliv_status; 00269 00270 //! Structure Variable : Discovery Status 00271 /*! 00272 */ 00273 uint8_t discov_status; 00274 00275 //! Structure Variable : Network Address where the packet has been set 00276 /*! 00277 */ 00278 uint8_t true_naD[2]; 00279 00280 //! Structure Variable : Retries needed to send the packet 00281 /*! 00282 */ 00283 uint8_t retries; 00284 }; 00285 00286 //! Structure : used for storing the received fragments 00287 /*! 00288 */ 00289 typedef struct matrix 00290 { 00291 private: 00292 public: 00293 00294 //! Structure Variable : Fragment data 00295 /*! 00296 */ 00297 char data[DATA_MATRIX]; 00298 00299 //! Structure Variable : Fragment data length 00300 /*! 00301 */ 00302 uint16_t frag_length; 00303 00304 //! Structure Variable : Fragment number for ordering the packet 00305 /*! 00306 */ 00307 uint8_t numFragment; 00308 00309 //! Structure Variable : Specifies if it is the last fragment 00310 /*! 00311 */ 00312 uint8_t endFragment; 00313 }; 00314 00315 00316 //! Structure : used for storing the needed information about the received packets 00317 /*! 00318 */ 00319 typedef struct index 00320 { 00321 private: 00322 public: 00323 00324 //! Structure Variable : Application Level ID 00325 /*! 00326 */ 00327 uint8_t packetID; 00328 00329 //! Structure Variable : 32b Lower Mac Source 00330 /*! 00331 */ 00332 uint8_t macSL[4]; 00333 00334 //! Structure Variable : 32b Higher Mac Source 00335 /*! 00336 */ 00337 uint8_t macSH[4]; 00338 00339 //! Structure Variable : 16b Network Address Source 00340 /*! 00341 */ 00342 uint8_t naS[2]; 00343 00344 //! Structure Variable : 32b Higher Mac Origin Source 00345 /*! 00346 */ 00347 uint8_t macOH[4]; 00348 00349 //! Structure Variable : 32b Lower Mac Origin Source 00350 /*! 00351 */ 00352 uint8_t macOL[4]; 00353 00354 //! Structure Variable : 16b Network Address origin 00355 /*! 00356 */ 00357 uint8_t naO[2]; 00358 00359 //! Structure Variable : Node Identifier Origin. To use in transmission, it must finish in "#". 00360 /*! 00361 */ 00362 char niO[20]; 00363 00364 //! Structure Variable : Source Type ID -> 0=NetAdrress ; 1=MacAddress ; 2=NodeIdentifier 00365 /*! 00366 */ 00367 uint8_t typeSourceID; 00368 00369 //! Structure Variable : Source Address Type -> 0=16B ; 1=64B 00370 /*! 00371 */ 00372 uint8_t address_typeS; 00373 00374 //! Structure Variable : Sending Mode -> 0=unicast ; 1=broadcast ; 2=cluster ; 3=synchronization 00375 /*! 00376 */ 00377 uint8_t mode; 00378 00379 //! Structure Variable : Source Endpoint 00380 /*! 00381 */ 00382 uint8_t SD; 00383 00384 //! Structure Variable : Destination Endpoint 00385 /*! 00386 */ 00387 uint8_t DE; 00388 00389 //! Structure Variable : Cluster Identifier 00390 /*! 00391 */ 00392 uint8_t CID[2]; 00393 00394 //! Structure Variable : Profile Identifier 00395 /*! 00396 */ 00397 uint8_t PID[2]; 00398 00399 //! Structure Variable : Sending Options (depends on the XBee module) 00400 /*! 00401 */ 00402 uint8_t opt; 00403 00404 //! Structure Variable : Receive Signal Strength Indicator 00405 /*! 00406 */ 00407 uint16_t RSSI; 00408 00409 //! Structure Variable : Time in miliseconds at the first fragment was received 00410 /*! 00411 */ 00412 long time; 00413 00414 //! Structure Variable : Specifies if the global packet is complete -> 1=Complete ; 0=Uncomplete ; -1=Empty 00415 /*! 00416 */ 00417 uint8_t complete; 00418 00419 //! Structure Variable : Specifies the total number of fragments that are expected -> -1=Empty 00420 /*! 00421 */ 00422 uint8_t totalFragments; 00423 00424 //! Structure Variable : Specifies the number of fragments received till now. -1=Empty 00425 /*! 00426 */ 00427 uint8_t recFragments; 00428 00429 //! Structure Variable : Real Data Length 00430 /*! 00431 */ 00432 uint16_t data_length; 00433 }; 00434 00435 00436 /****************************************************************************** 00437 * Class 00438 ******************************************************************************/ 00439 00440 //! WaspXBeeCore Class 00441 /*! 00442 WaspXBeeCore Class defines all the variables and functions used for managing the XBee modules. In this library, all the common functions are defined to be used in the other XBee libraries 00443 */ 00444 class WaspXBeeCore 00445 { 00446 public: 00447 00448 //! class constructor 00449 /*! 00450 It does nothing 00451 \param void 00452 \return void 00453 */ 00454 WaspXBeeCore(){}; 00455 00456 //! It initializes the necessary variables 00457 /*! 00458 It initalizes all the necessary variables 00459 \param uint8_t protocol_used : specifies the protocol used in the XBee module (depends on the XBee module) 00460 \param uint8_t frequency : specifies the frequency used in the XBee module (depends on the XBee module) 00461 \param uint8_t model_used : specifies the XBee model used (depends on the XBee module) 00462 \return void 00463 */ 00464 void init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used); 00465 00466 //! It gets the own lower 32b MAC 00467 /*! 00468 It stores in global 'sourceMacLow' variable the own lower 32b MAC 00469 \return '0' on success, '1' otherwise 00470 */ 00471 uint8_t getOwnMacLow(); 00472 00473 //! It gets the own higher 32b MAC 00474 /*! 00475 It stores in global 'sourceMacHigh' variable the own higher 32b MAC 00476 \return '0' on success, '1' otherwise 00477 */ 00478 uint8_t getOwnMacHigh(); 00479 00480 //! It gets the own MAC 00481 /*! 00482 It stores in global 'sourceMacHigh' and 'sourceMacLow' variables the 64b MAC 00483 \return '0' on success, '1' otherwise 00484 */ 00485 uint8_t getOwnMac(); 00486 00487 //! It sets the own 16b Network Address 00488 /*! 00489 \param uint8_t NA_H : higher Network Address byte (range [0x00-0xFF]) 00490 \param uint8_t NA_L : lower Network Address byte (range [0x00-0xFF]) 00491 \return '0' on success, '1' otherwise 00492 */ 00493 uint8_t setOwnNetAddress(uint8_t NA_H, uint8_t NA_L); 00494 00495 //! It gets the 16b Network Address 00496 /*! 00497 It stores in global 'sourceNA' the 16b Network Address 00498 \return '0' on success, '1' otherwise 00499 */ 00500 uint8_t getOwnNetAddress(); 00501 00502 //! It sets the baudrate 00503 /*! 00504 \param uint8_t baud_rate : the baudrate to set the XBee to (range [0-5]) 00505 \return '0' on success, '1' otherwise 00506 */ 00507 uint8_t setBaudrate(uint8_t baud_rate); 00508 00509 //! It sets API enabled or disabled 00510 /*! 00511 \param uint8_t api_value : the API mode (range [0-2]) 00512 \return '0' on success, '1' otherwise 00513 */ 00514 uint8_t setAPI(uint8_t api_value); 00515 00516 //! It sets API options 00517 /*! 00518 \param uint8_t api_options : the API options (range [0-1]) 00519 \return '0' on success, '1' otherwise 00520 */ 00521 uint8_t setAPIoptions(uint8_t api_options); 00522 00523 //! It sets the PAN ID 00524 /*! 00525 \param uint8_t* PANID : the PAN ID (64b - ZigBee ; 16b - Other protocols) 00526 \return '0' on success, '1' otherwise 00527 */ 00528 uint8_t setPAN(uint8_t* PANID); 00529 00530 //! It gets the PAN ID 00531 /*! 00532 It stores in global 'PAN_ID' the PAN ID 00533 \return '0' on success, '1' otherwise 00534 */ 00535 uint8_t getPAN(); 00536 00537 //! It sets the sleep mode 00538 /*! 00539 \param uint8_t sleep : the sleep mode (range [0-5]) 00540 \return '0' on success, '1' otherwise 00541 */ 00542 uint8_t setSleepMode(uint8_t sleep); 00543 00544 //! It gets the sleep mode 00545 /*! 00546 It stores in global 'sleepMode' the sleep mode 00547 \return '0' on success, '1' otherwise 00548 */ 00549 uint8_t getSleepMode(); 00550 00551 //! It sets the time awake before sleeping 00552 /*! 00553 \param uint8_t* awake : the time awake before sleeping (range depends on the XBee module) 00554 \return '0' on success, '1' otherwise 00555 */ 00556 uint8_t setAwakeTime(uint8_t* awake); 00557 00558 //! It sets time the module is slept 00559 /*! 00560 \param uint8_t* sleep : the module is slept (range depends on the XBee module) 00561 \return '0' on success, '1' otherwise 00562 */ 00563 uint8_t setSleepTime(uint8_t* sleep); 00564 00565 //! It sets the channel frequency where module is working on 00566 /*! 00567 \param uint8_t _channel : the channel frequency where module is working on (range depends on the XBee module) 00568 \return '0' on success, '1' otherwise 00569 */ 00570 uint8_t setChannel(uint8_t _channel); 00571 00572 //! It gets the channel frequency where module is working on 00573 /*! 00574 It stores in global 'channel' the channel frequency where module is working on 00575 \return '0' on success, '1' otherwise 00576 */ 00577 uint8_t getChannel(); 00578 00579 //! It sets the Node Identifier 00580 /*! 00581 \param char* node : the NI must be a 20 character max string 00582 \return '0' on success, '1' otherwise 00583 */ 00584 uint8_t setNodeIdentifier(char* node); 00585 00586 //! It gets the Node Identifier 00587 /*! 00588 It stores in global 'nodeID' the Node Identifier 00589 \return '0' on success, '1' otherwise 00590 */ 00591 uint8_t getNodeIdentifier(); 00592 00593 //! It scans for brothers in the same channel and same PAN ID 00594 /*! 00595 It stores the given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable 00596 It stores in global "totalScannedBrothers" the number of found brothers 00597 \return '0' on success, '1' otherwise 00598 */ 00599 uint8_t scanNetwork(); 00600 00601 //! It scans for a brother in the same channel and same PAN ID 00602 /*! 00603 It stores the given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable 00604 It stores in global "totalScannedBrothers" the number of found brothers 00605 \param char* node : 20-byte max string containing NI of the node to search 00606 \return '0' on success, '1' otherwise 00607 */ 00608 uint8_t scanNetwork(char* node); 00609 00610 //! It sets the time the Node Discovery is scanning 00611 /*! 00612 \param uint8_t* time : the time the Node Discovery is scanning (range [0x01-0xFC]) 00613 \return '0' on success, '1' otherwise 00614 */ 00615 uint8_t setScanningTime(uint8_t* time); 00616 00617 //! It gets the time the Node Discovery is scanning 00618 /*! 00619 It stores in global 'scanTime' the time the Node Discovery is scanning 00620 \return '0' on success, '1' otherwise 00621 */ 00622 uint8_t getScanningTime(); 00623 00624 //! It sets the options for the network discovery command 00625 /*! 00626 \param uint8_t options : the options for the network discovery command (range [0x00-0x03]) 00627 \return '0' on success, '1' otherwise 00628 */ 00629 uint8_t setDiscoveryOptions(uint8_t options); 00630 00631 //! It gets the options for the network discovery command 00632 /*! 00633 It stores in global 'discoveryOptions' the options for the network discovery command 00634 \return '0' on success, '1' otherwise 00635 */ 00636 uint8_t getDiscoveryOptions(); 00637 00638 //! It performs a quick search of a specific node. Depending on the XBee module it stores different information 00639 /*! 00640 It stores the given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable 00641 It stores in global "totalScannedBrothers" the number of found brothers 00642 \param char* node : 20-byte max string containing NI of the node to search 00643 \param struct packetXBee* paq : a packetXBee structure to store the node information 00644 \return '0' on success, '1' otherwise 00645 */ 00646 uint8_t nodeSearch(char* node, struct packetXBee* paq); 00647 00648 //! It sets the list of channels to scan when performing an energy scan 00649 /*! 00650 \param uint8_t channel_H : higher channel list byte (range [0x00-0xFF]) 00651 \param uint8_t channel_L : lower channel list byte (range [0x00-0xFF]) 00652 \return '0' on success, '1' otherwise 00653 */ 00654 uint8_t setScanningChannels(uint8_t channel_H, uint8_t channel_L); 00655 00656 //! It gets the list of channels to scan when performing an energy scan 00657 /*! 00658 It stores in global 'scanChannels' the list of channels to scan when performing an energy scan 00659 \return '0' on success, '1' otherwise 00660 */ 00661 uint8_t getScanningChannels(); 00662 00663 //! It sets the time the Energy Scan will be performed 00664 /*! 00665 It stores the energy on each channel in global 'energyChannel' variable 00666 \param uint8_t duration : time the energy scan will be performed (range [0-6]) 00667 \return '0' on success, '1' otherwise 00668 */ 00669 uint8_t setDurationEnergyChannels(uint8_t duration); 00670 00671 //! It gets the time the Energy Scan will be performed 00672 /*! 00673 It stores in global 'timeEnergyChannel' the time the Energy Scan will be performed 00674 \return '0' on success, '1' otherwise 00675 */ 00676 uint8_t getDurationEnergyChannels(); 00677 00678 //! It sets the link key to use in the 128b AES algorithm 00679 /*! 00680 \param char* key : the 128-bit AES encryption key (range [0-0xFFFFFFFFFFFFFFFF]) 00681 \return '0' on success, '1' otherwise 00682 */ 00683 uint8_t setLinkKey(char* key); 00684 00685 //! It sets the encryption mode ON/OFF 00686 /*! 00687 \param uint8_t mode : the encryption mode (range [0-1]) 00688 \return '0' on success, '1' otherwise 00689 */ 00690 uint8_t encryptionMode(uint8_t mode); 00691 00692 //! It sets the power level at which RF module transmits 00693 /*! 00694 \param uint8_t mode : power level at which RF module transmits (depends on the XBee module - range [0-4]) 00695 \return '0' on success, '1' otherwise 00696 */ 00697 uint8_t setPowerLevel(uint8_t value); 00698 00699 //! It gets the Received Signal Strength Indicator 00700 /*! 00701 It stores in global 'valueRSSI' the Received Signal Strength Indicator 00702 \return '0' on success, '1' otherwise 00703 */ 00704 uint8_t getRSSI(); 00705 00706 //! It gets the Hardware Version 00707 /*! 00708 It stores in global 'hardVersion' the Hardware Version 00709 \return '0' on success, '1' otherwise 00710 */ 00711 uint8_t getHardVersion(); 00712 00713 //! It gets the Software Version 00714 /*! 00715 It stores in global 'softVersion' the Software Version 00716 \return '0' on success, '1' otherwise 00717 */ 00718 uint8_t getSoftVersion(); 00719 00720 //! It sets the RSSI time 00721 /*! 00722 \param uint8_t time : amount of time to do the PWM (range [0x00-0xFF]) 00723 \return '0' on success, '1' otherwise 00724 */ 00725 uint8_t setRSSItime(uint8_t time); 00726 00727 //! It gets the RSSI time 00728 /*! 00729 It stores in global 'timeRSSI' the RSSI time 00730 \return '0' on success, '1' otherwise 00731 */ 00732 uint8_t getRSSItime(); 00733 00734 //! It writes the parameters changed into non-volatil memory, being applied when the XBee is set OFF 00735 /*! 00736 \return '0' on success, '1' otherwise 00737 */ 00738 uint8_t writeValues(); 00739 00740 //! It writes the parameters changed into non-volatil memory and applies them immediately 00741 /*! 00742 \return '0' on success, '1' otherwise 00743 */ 00744 uint8_t applyChanges(); 00745 00746 //! It resets the XBee firmware 00747 /*! 00748 \return '0' on success, '1' otherwise 00749 */ 00750 uint8_t reset(); 00751 00752 //! It resets the XBee parameters to factory defaults 00753 /*! 00754 \return '0' on success, '1' otherwise 00755 */ 00756 uint8_t resetDefaults(); 00757 00758 //! It sets the sleep options 00759 /*! 00760 \param uint8_t soption : the sleep options (range depends on the XBee module) 00761 \return '0' on success, '1' otherwise 00762 */ 00763 uint8_t setSleepOptions(uint8_t soption); 00764 00765 //! It gets the sleep options 00766 /*! 00767 It stores in global 'sleepOptions' the sleep options 00768 \return '0' on success, '1' otherwise 00769 */ 00770 uint8_t getSleepOptions(); 00771 00772 //! It sends a packet to others XBee modules 00773 /*! 00774 \param struct packetXBee* packet : it is filled with the information needed to be able to send the packet 00775 \return '0' on success, '1' otherwise 00776 */ 00777 uint8_t sendXBee(struct packetXBee* packet); 00778 00779 //! It sends a packet to others XBee modules 00780 /*! 00781 \param uint8_t* address : address where to send the packet to 00782 \param char* data : data to send 00783 \return '0' on success, '1' if error, '-1' if no memory 00784 */ 00785 int8_t send(uint8_t* address, char* data); 00786 00787 //! It sends a packet to others XBee modules 00788 /*! 00789 \param char* address : address where to send the packet to 00790 \param char* data : data to send 00791 \return '0' on success, '1' otherwise 00792 */ 00793 int8_t send(char* address, char* data); 00794 00795 //! It treats the data from XBee UART 00796 /*! 00797 \return '0' on success, '1' otherwise 00798 */ 00799 int8_t treatData(); 00800 00801 //! It frees the outcoming data buffer in the XBee 00802 /*! 00803 \return '0' on success, '1' otherwise 00804 */ 00805 uint8_t freeXBee(); 00806 00807 //! It synchronizes two nodes in a PAN 00808 /*! 00809 \param struct packetXBee* paq : it is filled with the information needed to be able to synchronize the two nodes 00810 \return '0' on success, '1' otherwise 00811 */ 00812 uint8_t synchronization(struct packetXBee* paq); 00813 00814 //! The user introduces an AT command within a string and the function executes it without knowing its meaning 00815 /*! 00816 \param char* atcommand : the command to execute. It must finish with a '#' 00817 \return '0' on success, '1' otherwise 00818 */ 00819 uint8_t sendCommandAT(char *atcommand); 00820 00821 //! It connects XBee, activating switch and opens the UART 00822 /*! 00823 \return '0' on success, '1' otherwise 00824 */ 00825 uint8_t ON(); 00826 00827 //! It disconnects XBee, switching it off and closing the UART 00828 /*! 00829 \return '0' on success, '1' otherwise 00830 */ 00831 uint8_t OFF(); 00832 00833 //! It sets the XBee to sleep, asserting PIN 9 00834 /*! 00835 \return '0' on success, '1' otherwise 00836 */ 00837 uint8_t sleep(); 00838 00839 //! It wakes up the XBee, de-asserting PIN 9 00840 /*! 00841 \return '0' on success, '1' otherwise 00842 */ 00843 uint8_t wake(); 00844 00845 //! It sets the destination parameters, such as the receiver address and the data to send 00846 /*! 00847 \param packetXBee* paq : a packetXBee structure where some parameters should have been filled before calling this function. After this call, this structure is filled with the corresponding address and data 00848 \param uint8_t* address : the receiver MAC 00849 \param char* data : the data to send 00850 \param uint8_t type : origin identification type (using this function call, it only can be used MAC_TYPE) 00851 \param uint8_t off_type : DATA_ABSOLUTE or DATA_OFFSET. It specifies if 'data' are absolute or if they must be added at the end of the packet 00852 \return '1' on success 00853 */ 00854 uint8_t setDestinationParams(packetXBee* paq, uint8_t* address, char* data, uint8_t type, uint8_t off_type); 00855 00856 //! It sets the destination parameters, such as the receiver address and the data to send 00857 /*! 00858 \param packetXBee* paq : a packetXBee structure where some parameters should have been filled before calling this function. After this call, this structure is filled with the corresponding address and data 00859 \param uint8_t* address : the receiver MAC 00860 \param int data : the data to send 00861 \param uint8_t type : origin identification type (using this function call, it only can be used MAC_TYPE) 00862 \param uint8_t off_type : DATA_ABSOLUTE or DATA_OFFSET. It specifies if 'data' are absolute or if they must be added at the end of the packet 00863 \return '1' on success 00864 */ 00865 uint8_t setDestinationParams(packetXBee* paq, uint8_t* address, int data, uint8_t type, uint8_t off_type); 00866 00867 //! It sets the destination parameters, such as the receiver address and the data to send 00868 /*! 00869 \param packetXBee* paq : a packetXBee structure where some parameters should have been filled before calling this function. After this call, this structure is filled with the corresponding address and data 00870 \param char* address : the receiver MAC 00871 \param char* data : the data to send 00872 \param uint8_t type : origin identification type (MAC_TYPE,MY_TYPE or NI_TYPE) 00873 \param uint8_t off_type : DATA_ABSOLUTE or DATA_OFFSET. It specifies if 'data' are absolute or if they must be added at the end of the packet 00874 \return '1' on success 00875 */ 00876 uint8_t setDestinationParams(packetXBee* paq, char* address, char* data, uint8_t type, uint8_t off_type); 00877 00878 //! It sets the destination parameters, such as the receiver address and the data to send 00879 /*! 00880 \param packetXBee* paq : a packetXBee structure where some parameters should have been filled before calling this function. After this call, this structure is filled with the corresponding address and data 00881 \param char* address : the receiver MAC 00882 \param int data : the data to send 00883 \param uint8_t type : origin identification type (MAC_TYPE,MY_TYPE or NI_TYPE) 00884 \param uint8_t off_type : DATA_ABSOLUTE or DATA_OFFSET. It specifies if 'data' are absolute or if they must be added at the end of the packet 00885 \return '1' on success 00886 */ 00887 uint8_t setDestinationParams(packetXBee* paq, char* address, int data, uint8_t type, uint8_t off_type); 00888 00889 //! It sets the origin parameters, such as the sender address 00890 /*! 00891 It gets the origin identification from the node. 00892 \param packetXBee* paq : a packetXBee structure where some parameters should have been filled before calling this function. After this call, this structure is filled with the corresponding address and data 00893 \param uint8_t type : origin identification type (MAC_TYPE,MY_TYPE or NI_TYPE) 00894 \return '1' on success 00895 */ 00896 uint8_t setOriginParams(packetXBee* paq, uint8_t type); 00897 00898 //! It sets the origin parameters, such as the sender address 00899 /*! 00900 \param packetXBee* paq : a packetXBee structure where some parameters should have been filled before calling this function. After this call, this structure is filled with the corresponding address and data 00901 \param char* address : origin identification (Netowrk Address, MAC Address or Node Identifier) 00902 \param uint8_t type : origin identification type (MAC_TYPE,MY_TYPE or NI_TYPE) 00903 \return '1' on success 00904 */ 00905 uint8_t setOriginParams(packetXBee* paq, char* address, uint8_t type); 00906 00907 //! It clears the variable 'command' 00908 /*! 00909 */ 00910 void clearCommand(); 00911 00912 //! Variable : it stores if the XBee module is ON or OFF (0-1) 00913 /*! 00914 */ 00915 uint8_t XBee_ON; 00916 00917 //! Variable : 32b Lower Mac Source 00918 /*! 00919 */ 00920 uint8_t sourceMacLow[4]; 00921 00922 //! Variable : 32b Higher Mac Source 00923 /*! 00924 */ 00925 uint8_t sourceMacHigh[4]; 00926 00927 //! Variable : 16b Network Address 00928 /*! 00929 */ 00930 uint8_t sourceNA[2]; 00931 00932 //! Variable : Baudrate, speed used to communicate with the XBee module (0-5) 00933 /*! 00934 */ 00935 uint8_t baudrate; 00936 00937 //! Variable : Api value selected (0-2) 00938 /*! 00939 */ 00940 uint8_t apiValue; 00941 00942 //! Variable : 64b PAN ID 00943 /*! 00944 */ 00945 uint8_t PAN_ID[8]; 00946 00947 //! Variable : Current Sleep Mode (0-5) 00948 /*! 00949 */ 00950 uint8_t sleepMode; 00951 00952 //! Variable : bidimensional array for storing the received fragments 00953 /*! 00954 */ 00955 matrix *packet_fragments[MAX_FINISH_PACKETS][MAX_FRAG_PACKETS]; 00956 00957 //! Variable : the number of fragments received 00958 /*! 00959 */ 00960 uint8_t totalFragmentsReceived; 00961 00962 //! Variable : array for storing the information related with each global packet, being able to order the different fragments 00963 /*! 00964 */ 00965 index *pendingFragments[MAX_FINISH_PACKETS]; 00966 00967 //! Variable : number of packets pending of being treated 00968 /*! 00969 */ 00970 uint8_t pendingPackets; 00971 00972 //! Variable : array for storing the packets received completely 00973 /*! 00974 */ 00975 packetXBee *packet_finished[MAX_FINISH_PACKETS]; 00976 00977 //! Variable : real number of complete received packets 00978 /*! 00979 */ 00980 uint8_t totalPacketsReceived; 00981 00982 //! Variable : indicates the position in 'packet_finished' array of each packet 00983 /*! 00984 */ 00985 uint8_t pos; 00986 00987 //! Variable : array for storing the information answered by the nodes when a Node Discovery is performe 00988 /*! 00989 */ 00990 Node scannedBrothers[MAX_BROTHERS]; 00991 00992 //! Variable : number of brothers found 00993 /*! 00994 */ 00995 int8_t totalScannedBrothers; 00996 00997 //! Variable : time to be idle before start sleeping 00998 /*! 00999 */ 01000 uint8_t awakeTime[3]; 01001 01002 //! Variable : Cyclic sleeping time 01003 /*! 01004 */ 01005 uint8_t sleepTime[3]; 01006 01007 //! Variable : Channel frequency where the module is currently working on 01008 /*! 01009 */ 01010 uint8_t channel; 01011 01012 //! Variable : Node Identifier 01013 /*! 01014 */ 01015 char nodeID[20]; 01016 01017 //! Variable : time meanwhile the Node Discovery is scanning 01018 /*! 01019 */ 01020 uint8_t scanTime[2]; 01021 01022 //! Variable : options for the network discovery command 01023 /*! 01024 */ 01025 uint8_t discoveryOptions; 01026 01027 //! Variable : list of channels to scan when performing an energy scan 01028 /*! 01029 */ 01030 uint8_t scanChannels[2]; 01031 01032 //! Variable : energy found on each channel 01033 /*! 01034 */ 01035 uint8_t energyChannel[20]; 01036 01037 //! Variable : time the Energy Scan is going to be performed 01038 /*! 01039 */ 01040 uint8_t timeEnergyChannel; 01041 01042 //! Variable : 128b AES Link key 01043 /*! 01044 */ 01045 char linkKey[16]; 01046 01047 //! Variable : encryption mode (ON/OFF) (0-1) 01048 /*! 01049 */ 01050 uint8_t encryptMode; 01051 01052 //! Variable : power level at which the RF transmits 01053 /*! 01054 */ 01055 uint8_t powerLevel; 01056 01057 //! Variable : time meanwhile the PWM output is active after receiving a packet 01058 /*! 01059 */ 01060 uint8_t timeRSSI; 01061 01062 //! Variable : software Version 01063 /*! 01064 */ 01065 uint8_t softVersion[2]; 01066 01067 //! Variable : hardware Version 01068 /*! 01069 */ 01070 uint8_t hardVersion[2]; 01071 01072 //! Variable : received Signal Strength Indicator 01073 /*! 01074 */ 01075 uint8_t valueRSSI[2]; 01076 01077 //! Variable : sleep Options 01078 /*! 01079 */ 01080 uint8_t sleepOptions; 01081 01082 //! Variable : max number of hops a packet can travel 01083 /*! 01084 */ 01085 uint8_t hops; 01086 01087 //! Variable : Answer received after executing "sendCommandAT" function 01088 /*! 01089 */ 01090 uint8_t commandAT[100]; 01091 01092 //! Variable : It stores the last Modem Status indication received 01093 /*! 01094 */ 01095 uint8_t modem_status; 01096 01097 //! Variable : It specifies the replacement ploicty to implement (FIFO, LIFO or OUT) 01098 /*! 01099 */ 01100 uint8_t replacementPolicy; 01101 01102 //! Variable : It stores if the last call to an AT command has generated an error 01103 /*! 01104 */ 01105 int8_t error_AT; 01106 01107 //! Variable : It stores if the last received packet has generated an error 01108 /*! 01109 */ 01110 int8_t error_RX; 01111 01112 //! Variable : It stores if the last sent packet has generated an error 01113 /*! 01114 */ 01115 int8_t error_TX; 01116 01117 protected: 01118 01119 01120 //! It reads a packet from other XBee module 01121 /*! 01122 It should be called when data is available from the XBee. If the available data is not a packet it will handle it and will return the appropriate message 01123 \return '0' on success, '1' when error, '-1' when no more memory is available 01124 */ 01125 int8_t readXBee(uint8_t* data); 01126 01127 //! It sends a packet to other XBee modules 01128 /*! 01129 \param struct packetXBee* packet : the function gets the needed information to send the packet from it 01130 \return '0' on success, '1' otherwise 01131 */ 01132 uint8_t sendXBeePriv(struct packetXBee* packet); 01133 01134 //! It generates the API frame to send to the XBee module 01135 /*! 01136 \param const char* data : the string that contains part of the API frame 01137 \param uint8_t param : input parameter to set using the AT command 01138 \return void 01139 */ 01140 void gen_data(const char* data, uint8_t param); 01141 01142 //! It generates the API frame to send to the XBee module 01143 /*! 01144 \param const char* data : the string that contains part of the API frame 01145 \return void 01146 */ 01147 void gen_data(const char* data); 01148 01149 //! It generates the API frame to send to the XBee module 01150 /*! 01151 \param const char* data : the string that contains part of the API frame 01152 \param uint8_t param1 : higher part of the input parameter to set using the AT command 01153 \param uint8_t param2 : lower part of the input parameter to set using the AT command 01154 \return void 01155 */ 01156 void gen_data(const char* data, uint8_t param1, uint8_t param2); 01157 01158 //! It generates the API frame to send to the XBee module 01159 /*! 01160 \param const char* data : the string that contains part of the API frame 01161 \param uint8_t* param : input parameter to set using the AT command 01162 \return void 01163 */ 01164 void gen_data(const char* data, uint8_t* param); 01165 01166 //! It generates the API frame to send to the XBee module 01167 /*! 01168 \param const char* data : the string that contains part of the API frame 01169 \param char* param : input parameter to set using the AT command 01170 \return void 01171 */ 01172 void gen_data(const char* data, char* param); 01173 01174 //! It generates the checksum API frame to send to the XBee module 01175 /*! 01176 \param const char* data : the string that contains part of the API frame 01177 \return the checksum generated 01178 */ 01179 uint8_t gen_checksum(const char* data); 01180 01181 //! It sends the API frame stored in 'command' variable to the XBee module 01182 /*! 01183 \param const char* data : the string that contains part of the API frame 01184 \return '0' if no error, '1' if error 01185 */ 01186 uint8_t gen_send(const char* data); 01187 01188 //! It generates the API frame when a TX is done 01189 /*! 01190 \param struct packetXBee* _packet : packet for storing the data to send 01191 \param uint8_t* TX_array : array for storing the data 01192 \param uint8_t start_pos: start position 01193 */ 01194 void gen_frame(struct packetXBee* _packet, uint8_t* TX_array, uint8_t start_pos); 01195 01196 //! It generates the frame using eschaped characters 01197 /*! 01198 \param struct packetXBee* _packet : packet for storing the data to send 01199 \param uint8_t* TX_array : array for storing the data 01200 \param uint8_t &protect: variable used for storing if some protected character 01201 \param uint8_t type: variable used for knowing the frame length 01202 */ 01203 void gen_frame_ap2(struct packetXBee* _packet, uint8_t* TX_array, uint8_t &protect, uint8_t type); 01204 01205 //! It parses the answer received by the XBee module, calling the appropriate function 01206 /*! 01207 \param uint8_t* frame : an array that contains the API frame that is expected to receive answer from if it is an AT command 01208 \return '0' if no error, '1' if error 01209 */ 01210 int8_t parse_message(uint8_t* frame); 01211 01212 //! It generates the correct API frame from an eschaped one 01213 /*! 01214 \param uint8_t* data_in : the string that contains the eschaped API frame AT command 01215 \param uint16_t end : the end of the frame 01216 \param uint16_t start : the start of the frame 01217 \return '0' if no error, '1' if error 01218 */ 01219 void des_esc(uint8_t* data_in, uint16_t end, uint16_t start); 01220 01221 //! It parses the AT command answer received by the XBee module 01222 /*! 01223 \param uint8_t* data_in : the string that contains the eschaped API frame AT command 01224 \param uint8_t* frame : an array that contains the API frame that is expected to receive answer from if it is an AT command 01225 \param uint16_t end : the end of the frame 01226 \param uint16_t start : the start of the frame 01227 \return '0' if no error, '1' if error 01228 */ 01229 uint8_t atCommandResponse(uint8_t* data_in, uint8_t* frame, uint16_t end, uint16_t start); 01230 01231 //! It parses the Modem Status message received by the XBee module 01232 /*! 01233 \param uint8_t* data_in : the string that contains the eschaped API frame AT command 01234 \param uint16_t end : the end of the frame 01235 \param uint16_t start : the start of the frame 01236 \return '0' if no error, '1' if error 01237 */ 01238 uint8_t modemStatusResponse(uint8_t* data_in, uint16_t end, uint16_t start); 01239 01240 //! It parses the TX Status message received by the XBee module 01241 /*! 01242 \param uint8_t* ByteIN : array to store the received answer 01243 \return '0' if no error, '1' if error 01244 */ 01245 uint8_t txStatusResponse(uint8_t* ByteIN); 01246 01247 //! It parses the ZB TX Status message received by the XBee module 01248 /*! 01249 \param uint8_t* ByteIN : array to store the received answer 01250 \return '0' if no error, '1' if error 01251 */ 01252 uint8_t txZBStatusResponse(uint8_t* ByteIN); 01253 01254 //! It parses the RX Data message received by the XBee module 01255 /*! 01256 \param uint8_t* data_in : the string that contains the eschaped API frame AT command 01257 \param uint16_t end : the end of the frame 01258 \param uint16_t start : the start of the frame 01259 \return '0' if no error, '1' if error 01260 */ 01261 int8_t rxData(uint8_t* data_in, uint16_t end, uint16_t start); 01262 01263 //! It parses the ND message received by the XBee module 01264 /*! 01265 It stores in 'scannedBrothers' variable the data extracted from the answer 01266 */ 01267 void treatScan(); 01268 01269 //! It checks the checksum is good 01270 /*! 01271 \param uint8_t* data_in : the string that contains the eschaped API frame AT command 01272 \param uint16_t end : the end of the frame 01273 \param uint16_t start : the start of the frame 01274 \return '0' if no error, '1' if error 01275 */ 01276 uint8_t checkChecksum(uint8_t* data_in, uint16_t end, uint16_t start); 01277 01278 //! It frees a position in index array 01279 /*! 01280 */ 01281 void freeIndex(); 01282 01283 //! It frees index array and matrix 01284 /*! 01285 */ 01286 void freeAll(); 01287 01288 //! It gets the next index where store the finished packet 01289 /*! 01290 \return the index where store the packet 01291 */ 01292 uint8_t getFinishIndex(); 01293 01294 //! It clears the finished packets array 01295 /*! 01296 */ 01297 void clearFinishArray(); 01298 01299 //! It gets the index in 'packet_finished' where store the new packet, according to a FIFO policy 01300 /*! 01301 \return the index where store the packet 01302 */ 01303 uint8_t getIndexFIFO(); 01304 01305 //! It gets the index in 'packet_finished' where store the new packet, according to a LIFO policy 01306 /*! 01307 \return the index where store the packet 01308 */ 01309 uint8_t getIndexLIFO(); 01310 01311 //! It frees the index array and the matrix row corresponding to the position is sent as an input parameter 01312 /*! 01313 \param uint8_t position : the position to free 01314 */ 01315 void freeIndexMatrix(uint8_t position); 01316 01317 //! Variable : protocol used (depends on de the XBee module) 01318 /*! 01319 */ 01320 uint8_t protocol; 01321 01322 //! Variable : frequency used (depends on de the XBee module) 01323 /*! 01324 */ 01325 uint8_t freq; 01326 01327 //! Variable : model used (depends on de the XBee module) 01328 /*! 01329 */ 01330 uint8_t model; 01331 01332 //! Variable : internal variable used to store the data length 01333 /*! 01334 */ 01335 uint16_t data_length; 01336 01337 //! Variable : it stores the data received in each frame 01338 /*! 01339 */ 01340 uint8_t data[50]; 01341 01342 //! Variable : internal counter 01343 /*! 01344 */ 01345 uint16_t it; 01346 01347 //! Variable : byte for starting getting the data 01348 /*! 01349 */ 01350 uint16_t start; 01351 01352 //! Variable : byte for stopping getting the data 01353 /*! 01354 */ 01355 uint16_t finish; 01356 01357 //! Variable : address type 01358 /*! 01359 */ 01360 uint8_t add_type; 01361 01362 //! Variable : sending mode 01363 /*! 01364 */ 01365 uint8_t mode; 01366 01367 //! Variable : fragment length 01368 /*! 01369 */ 01370 uint16_t frag_length; 01371 01372 //! Variable : time used to manage timeouts 01373 /*! 01374 */ 01375 long TIME1; 01376 01377 //! Variable : array to store the AT commands 01378 /*! 01379 */ 01380 uint8_t command[30]; 01381 01382 //! Variable : index data to make a packet from many different data 01383 /*! 01384 */ 01385 uint16_t data_ind; 01386 01387 //! Variable : delivery packet status 01388 /*! 01389 */ 01390 uint8_t delivery_status; 01391 01392 //! Variable : discovery process status 01393 /*! 01394 */ 01395 uint8_t discovery_status; 01396 01397 //! Variable : true 16b Network Address where the packet has been sent 01398 /*! 01399 */ 01400 uint8_t true_naD[2]; 01401 01402 //! Variable : retries done during the sending 01403 /*! 01404 */ 01405 uint8_t retries_sending; 01406 01407 //! Variable : specifies the next index where storing the next received fragment 01408 /*! 01409 */ 01410 uint8_t nextIndex1; 01411 01412 //! Variable : flag to indicate if a frame was truncated 01413 /*! 01414 */ 01415 uint8_t frameNext; 01416 01417 //! Variable : flag to indicate if the variable 'nextIndex1' has been modified during the last execution of 'readXBee' 01418 /*! 01419 */ 01420 uint8_t indexNotModified; 01421 01422 //! Variable : specifies if APS encryption is enabled or disabled 01423 /*! 01424 */ 01425 uint8_t apsEncryption; 01426 }; 01427 01428 extern WaspXBeeCore xbee; 01429 01430 #endif 01431 01432 01433 01434 01435 01436 01437 01438 01439 01440 01441 01442 01443 01444 01445 01446 01447
1.5.6