00001 /* 00002 * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. 00003 * http://www.libelium.com 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU Lesser General Public License as published by 00007 * the Free Software Foundation, either version 2.1 of the License, or 00008 * (at your option) any later version. 00009 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU Lesser General Public License for more details. 00014 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 * 00018 * Version: 0.2 00019 * Design: David Gascón 00020 * Implementation: Alberto Bielsa 00021 */ 00022 00023 00024 #ifndef __WPROGRAM_H__ 00025 // #include <WProgram.h> 00026 #include <WaspClasses.h> 00027 #endif 00028 00029 void WaspXBee802::init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used) 00030 { 00031 protocol=protocol_used; 00032 freq=frequency; 00033 model=model_used; 00034 00035 totalFragmentsReceived=0; 00036 pendingPackets=0; 00037 pos=0; 00038 discoveryOptions=0x00; 00039 awakeTime[0]=AWAKE_TIME_802_15_4_H; 00040 awakeTime[1]=AWAKE_TIME_802_15_4_L; 00041 sleepTime[0]=SLEEP_TIME_802_15_4_H; 00042 sleepTime[1]=SLEEP_TIME_802_15_4_L; 00043 scanTime[0]=SCAN_TIME_802_15_4; 00044 scanChannels[0]=SCAN_CHANNELS_802_15_4_H; 00045 scanChannels[1]=SCAN_CHANNELS_802_15_4_L; 00046 encryptMode=ENCRYPT_MODE_802_15_4; 00047 powerLevel=POWER_LEVEL_802_15_4; 00048 timeRSSI=TIME_RSSI_802_15_4; 00049 sleepOptions=SLEEP_OPTIONS_802_15_4; 00050 retries=0; 00051 delaySlots=0; 00052 macMode=0; 00053 energyThreshold=0x2C; 00054 counterCCA[0]=0x00; 00055 counterCCA[1]=0x00; 00056 counterACK[0]=0x00; 00057 counterACK[1]=0x00; 00058 00059 counter=0; 00060 data_length=0; 00061 it=0; 00062 start=0; 00063 finish=0; 00064 add_type=0; 00065 mode=0; 00066 frag_length=0; 00067 TIME1=0; 00068 nextIndex1=0; 00069 frameNext=0; 00070 replacementPolicy=XBEE_OUT; 00071 indexNotModified=1; 00072 error_AT=2; 00073 error_RX=2; 00074 error_TX=2; 00075 clearFinishArray(); 00076 clearCommand(); 00077 } 00078 00079 00080 /* 00081 Function: Set the maximum number of retries to execute in addition to the 00082 three retries defined in the 802.15.4 protocol 00083 Returns: Integer that determines if there has been any error 00084 error=2 --> The command has not been executed 00085 error=1 --> There has been an error while executing the command 00086 error=0 --> The command has been executed with no errors 00087 Values: Change the RR command 00088 Parameters: 00089 retry: number of retries (0-6) 00090 */ 00091 uint8_t WaspXBee802::setRetries(uint8_t retry) 00092 { 00093 int8_t error=2; 00094 00095 error_AT=2; 00096 gen_data(set_retries_802,retry); 00097 gen_checksum(set_retries_802); 00098 error=gen_send(set_retries_802); 00099 00100 00101 if(!error) 00102 { 00103 retries=retry; 00104 } 00105 return error; 00106 } 00107 00108 /* 00109 Function: Get the retries that specifies the RR command 00110 Returns: Integer that determines if there has been any error 00111 error=2 --> The command has not been executed 00112 error=1 --> There has been an error while executing the command 00113 error=0 --> The command has been executed with no errors 00114 Values: Stores in global "retries" variable the number of retries 00115 */ 00116 uint8_t WaspXBee802::getRetries() 00117 { 00118 int8_t error=2; 00119 00120 error_AT=2; 00121 gen_data(get_retries_802); 00122 error=gen_send(get_retries_802); 00123 00124 00125 if(!error) 00126 { 00127 retries=data[0]; 00128 } 00129 return error; 00130 } 00131 00132 /* 00133 Function: Set the minimun value of the back-off exponent in CSMA/CA 00134 Returns: Integer that determines if there has been any error 00135 error=2 --> The command has not been executed 00136 error=1 --> There has been an error while executing the command 00137 error=0 --> The command has been executed with no errors 00138 Values: Change the RN command 00139 Parameters: 00140 exponent: value of the back-off exponential (0-3) 00141 */ 00142 uint8_t WaspXBee802::setDelaySlots(uint8_t exponent) 00143 { 00144 int8_t error=2; 00145 00146 error_AT=2; 00147 gen_data(set_delay_slots_802,exponent); 00148 gen_checksum(set_delay_slots_802); 00149 error=gen_send(set_delay_slots_802); 00150 00151 if(!error) 00152 { 00153 delaySlots=exponent; 00154 } 00155 00156 return error; 00157 } 00158 00159 /* 00160 Function: Get the minimum value of the back-off exponent in CSMA/CA 00161 Returns: Integer that determines if there has been any error 00162 error=2 --> The command has not been executed 00163 error=1 --> There has been an error while executing the command 00164 error=0 --> The command has been executed with no errors 00165 Values: Stores in global "delaySlots" variable the back-off exponent 00166 */ 00167 uint8_t WaspXBee802::getDelaySlots() 00168 { 00169 int8_t error=2; 00170 00171 error_AT=2; 00172 gen_data(get_delay_slots_802); 00173 error=gen_send(get_delay_slots_802); 00174 00175 if(!error) 00176 { 00177 delaySlots=data[0]; 00178 } 00179 return error; 00180 } 00181 00182 /* 00183 Function: Set the Mac Mode, choosen between the 4 options (0/1/2/3) 00184 Returns: Integer that determines if there has been any error 00185 error=2 --> The command has not been executed 00186 error=1 --> There has been an error while executing the command 00187 error=0 --> The command has been executed with no errors 00188 Values: Change the MM command 00189 Parameters: 00190 mac: set the mac mode to use (0-3) 00191 */ 00192 uint8_t WaspXBee802::setMacMode(uint8_t mac) 00193 { 00194 int8_t error=2; 00195 00196 error_AT=2; 00197 gen_data(set_mac_mode_802,mac); 00198 gen_checksum(set_mac_mode_802); 00199 error=gen_send(set_mac_mode_802); 00200 00201 if(!error) 00202 { 00203 macMode=mac; 00204 } 00205 return error; 00206 } 00207 00208 /* 00209 Function: Get the Mac Mode 00210 Returns: Integer that determines if there has been any error 00211 error=2 --> The command has not been executed 00212 error=1 --> There has been an error while executing the command 00213 error=0 --> The command has been executed with no errors 00214 Values: Stores in global "macMode" variable the Mac Mode 00215 */ 00216 uint8_t WaspXBee802::getMacMode() 00217 { 00218 int8_t error=2; 00219 00220 error_AT=2; 00221 gen_data(get_mac_mode_802); 00222 error=gen_send(get_mac_mode_802); 00223 00224 if(!error) 00225 { 00226 macMode=data[0]; 00227 } 00228 return error; 00229 } 00230 00231 /* 00232 Function: Set the CA threshold in the CCA process to detect energy on the channel 00233 Returns: Integer that determines if there has been any error 00234 error=2 --> The command has not been executed 00235 error=1 --> There has been an error while executing the command 00236 error=0 --> The command has been executed with no errors 00237 Values: Change the CA command 00238 Parameters: 00239 threshold: CA threshold in the CCA process (0x00-0x50) 00240 */ 00241 uint8_t WaspXBee802::setEnergyThreshold(uint8_t threshold) 00242 { 00243 int8_t error=2; 00244 00245 error_AT=2; 00246 gen_data(set_energy_thres_802,threshold); 00247 gen_checksum(set_energy_thres_802); 00248 error=gen_send(set_energy_thres_802); 00249 00250 if(!error) 00251 { 00252 energyThreshold=threshold; 00253 } 00254 return error; 00255 } 00256 00257 /* 00258 Function: Get the Energy Threshold used in the CCA process 00259 Returns: Integer that determines if there has been any error 00260 error=2 --> The command has not been executed 00261 error=1 --> There has been an error while executing the command 00262 error=0 --> The command has been executed with no errors 00263 Values: Stores in global "energyThreshold" variable any error happened while execution 00264 */ 00265 uint8_t WaspXBee802::getEnergyThreshold() 00266 { 00267 int8_t error=2; 00268 00269 error_AT=2; 00270 gen_data(get_energy_thres_802); 00271 error=gen_send(get_energy_thres_802); 00272 00273 if(!error) 00274 { 00275 energyThreshold=data[0]; 00276 } 00277 return error; 00278 } 00279 00280 /* 00281 Function: It gets the number of times too much energy has been found on the channel 00282 Returns: Integer that determines if there has been any error 00283 error=2 --> The command has not been executed 00284 error=1 --> There has been an error while executing the command 00285 error=0 --> The command has been executed with no errors 00286 Values: Stores in global "counterCCA" variable number of times too much energy has been found 00287 */ 00288 uint8_t WaspXBee802::getCCAcounter() 00289 { 00290 int8_t error=2; 00291 00292 error_AT=2; 00293 gen_data(get_CCA_802); 00294 error=gen_send(get_CCA_802); 00295 00296 if(!error) 00297 { 00298 counterCCA[0]=data[0]; 00299 delay(20); 00300 counterCCA[1]=data[1]; 00301 delay(20); 00302 } 00303 return error; 00304 } 00305 00306 /* 00307 Function: Reset the CCA counter 00308 Returns: Integer that determines if there has been any error 00309 error=2 --> The command has not been executed 00310 error=1 --> There has been an error while executing the command 00311 error=0 --> The command has been executed with no errors 00312 Values: Change the EC command 00313 */ 00314 uint8_t WaspXBee802::resetCCAcounter() 00315 { 00316 int8_t error=2; 00317 00318 error_AT=2; 00319 gen_data(reset_CCA_802); 00320 error=gen_send(reset_CCA_802); 00321 00322 if(!error) 00323 { 00324 counterCCA[0]=0; 00325 counterCCA[1]=0; 00326 } 00327 return error; 00328 } 00329 00330 /* 00331 Function: Get the number of times there has been an ACK failure 00332 Returns: Integer that determines if there has been any error 00333 error=2 --> The command has not been executed 00334 error=1 --> There has been an error while executing the command 00335 error=0 --> The command has been executed with no errors 00336 Values: Stores in global "counterACK" variable the number of times there has been an ACK failure 00337 */ 00338 uint8_t WaspXBee802::getACKcounter() 00339 { 00340 int8_t error=2; 00341 00342 error_AT=2; 00343 gen_data(get_ACK_802); 00344 error=gen_send(get_ACK_802); 00345 00346 if(!error) 00347 { 00348 counterACK[0]=data[0]; 00349 delay(20); 00350 counterACK[1]=data[1]; 00351 delay(20); 00352 } 00353 return error; 00354 } 00355 00356 /* 00357 Function: Reset the ACK counter 00358 Returns: Integer that determines if there has been any error 00359 error=2 --> The command has not been executed 00360 error=1 --> There has been an error while executing the command 00361 error=0 --> The command has been executed with no errors 00362 Values: Change the EA command 00363 */ 00364 uint8_t WaspXBee802::resetACKcounter() 00365 { 00366 int8_t error=2; 00367 00368 error_AT=2; 00369 gen_data(reset_ACK_802); 00370 error=gen_send(reset_ACK_802); 00371 00372 if(!error) 00373 { 00374 counterACK[0]=0; 00375 counterACK[1]=0; 00376 } 00377 return error; 00378 } 00379 00380 WaspXBee802 xbee802 = WaspXBee802();
1.5.6