/WaspXBeeCore.cpp
C++ | 5619 lines | 4512 code | 344 blank | 763 comment | 716 complexity | 1a38e9b24757c429f426623e786533f9 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
- * http://www.libelium.com
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 2.1 of the License, or
- * (at your option) any later version.
-
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
-
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * Version: 0.13
- * Design: David Gascón
- * Implementation: Alberto Bielsa
- */
-
- #ifndef __WPROGRAM_H__
- #include "WaspClasses.h"
- #endif
- #ifndef __WASPXBEECONSTANTS_H__
- #include "WaspXBeeConstants.h"
- #endif
- char get_own_mac_low[] = "7E00040852534C06";
- char get_own_mac_high[] = "7E0004085253480A";
- char set_own_net_address[] = "7E000608524D59000000";
- char get_own_net_address[] = "7E000408524D59FF";
- char set_baudrate[] = "7E0005085242440000";
- char set_api_mode[] = "7E0005085241500000";
- char set_api_options[] = "7E00050852414F0000";
- char set_pan[] = "7E000608524944000000";
- char set_pan_zb[] = "7E000C08524944000000000000000000";
- char get_pan[] = "7E00040852494418";
- char set_sleep_mode_xbee[] = "7E00050852534D0000";
- char get_sleep_mode_xbee[] = "7E00040852534D05";
- char set_awake_time[] = "7E000608525354000000";
- char set_awake_time_DM[] = "7E00070852535400000000";
- char set_sleep_time[] = "7E000608525350000000";
- char set_sleep_time_DM[] = "7E00070852535000000000";
- char set_channel[] = "7E0005085243480000";
- char get_channel[] = "7E0004085243481A";
- char get_NI[] = "7E000408524E490E";
- char set_scanning_time[] = "7E000508524E540000";
- char set_scanning_time_DM[] = "7E000608524E54000000";
- char get_scanning_time[] = "7E000408524E5403";
- char set_discov_options[] = "7E000508524E4F0000";
- char get_discov_options[] = "7E000408524E4F08";
- char write_values[] = "7E000408525752FC";
- char set_scanning_channel[] = "7E000608525343000000";
- char get_scanning_channel[] = "7E0004085253430F";
- char get_duration_energy[] = "7E0004085253440E";
- char set_link_key[] = "7E001408524B590000000000000000000000000000000000";
- char set_encryption[] = "7E0005085245450000";
- char set_power_level[] = "7E00050852504C0000";
- char get_RSSI[] = "7E0004085244421F";
- char get_hard_version[] = "7E00040852485607";
- char get_soft_version[] = "7E000408525652FD";
- char set_RSSI_time[] = "7E0005085252500000";
- char get_RSSI_time[] = "7E00040852525003";
- char apply_changes[] = "7E00040852414321";
- char reset_xbee[] = "7E0004085246520D";
- char reset_defaults_xbee[] = "7E0004085252450E";
- char set_sleep_options_xbee[] = "7E00050852534F0000";
- char get_sleep_options_xbee[] = "7E00040852534F03";
- char scan_network[] = "7E000408524E4413";
- extern char set_duration_energy[];
- extern char set_duration_energy_ZB[];
- /*
- Function: Initializes all the global variables that will be used later
- Returns: Nothing
- Parameters:
- protocol_used: Protocol the XBee is using
- frequency: Frequency the XBee is running on
- model_used: Model of XBee used
- */
- void WaspXBeeCore::init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used)
- {
- protocol=protocol_used;
- freq=frequency;
- model=model_used;
- totalFragmentsReceived=0;
- pendingPackets=0;
- pos=0;
- discoveryOptions=0x00;
- if(protocol==XBEE_802_15_4)
- {
- awakeTime[0]=AWAKE_TIME_802_15_4_H;
- awakeTime[1]=AWAKE_TIME_802_15_4_L;
- sleepTime[0]=SLEEP_TIME_802_15_4_H;
- sleepTime[1]=SLEEP_TIME_802_15_4_L;
- scanTime[0]=SCAN_TIME_802_15_4;
- scanChannels[0]=SCAN_CHANNELS_802_15_4_H;
- scanChannels[1]=SCAN_CHANNELS_802_15_4_L;
- encryptMode=ENCRYPT_MODE_802_15_4;
- powerLevel=POWER_LEVEL_802_15_4;
- timeRSSI=TIME_RSSI_802_15_4;
- sleepOptions=SLEEP_OPTIONS_802_15_4;
- }
- if(protocol==ZIGBEE)
- {
- awakeTime[0]=AWAKE_TIME_ZIGBEE_H;
- awakeTime[1]=AWAKE_TIME_ZIGBEE_L;
- sleepTime[0]=SLEEP_TIME_ZIGBEE_H;
- sleepTime[1]=SLEEP_TIME_ZIGBEE_L;
- scanTime[0]=SCAN_TIME_ZIGBEE;
- scanChannels[0]=SCAN_CHANNELS_ZIGBEE_H;
- scanChannels[1]=SCAN_CHANNELS_ZIGBEE_L;
- timeEnergyChannel=TIME_ENERGY_CHANNEL_ZIGBEE;
- encryptMode=ENCRYPT_MODE_ZIGBEE;
- powerLevel=POWER_LEVEL_ZIGBEE;
- timeRSSI=TIME_RSSI_ZIGBEE;
- sleepOptions=SLEEP_OPTIONS_ZIGBEE;
- }
- if(protocol==DIGIMESH)
- {
- awakeTime[0]=AWAKE_TIME_DIGIMESH_H;
- awakeTime[1]=AWAKE_TIME_DIGIMESH_M;
- awakeTime[2]=AWAKE_TIME_DIGIMESH_L;
- sleepTime[0]=SLEEP_TIME_DIGIMESH_H;
- sleepTime[1]=SLEEP_TIME_DIGIMESH_M;
- sleepTime[2]=SLEEP_TIME_DIGIMESH_L;
- scanTime[0]=SCAN_TIME_DIGIMESH_H;
- scanTime[1]=SCAN_TIME_DIGIMESH_L;
- encryptMode=ENCRYPT_MODE_DIGIMESH;
- powerLevel=POWER_LEVEL_DIGIMESH;
- timeRSSI=TIME_RSSI_DIGIMESH;
- sleepOptions=SLEEP_OPTIONS_DIGIMESH;
- }
-
- data_length=0;
- it=0;
- start=0;
- finish=0;
- add_type=0;
- mode=0;
- frag_length=0;
- TIME1=0;
- nextIndex1=0;
- frameNext=0;
- replacementPolicy=XBEE_OUT;
- indexNotModified=1;
- error_AT=2;
- error_RX=2;
- error_TX=2;
- clearFinishArray();
- clearCommand();
- apsEncryption=0;
- }
- /*
- Function: Get the 32 lower bits of my MAC address
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: When it is executed stores the returned value by SL command in the global
- "sourceMacLow[4]" variable
- */
- uint8_t WaspXBeeCore::getOwnMacLow()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_own_mac_low);
- error=gen_send(get_own_mac_low);
-
- if(error==0)
- {
- for(it=0;it<4;it++)
- {
- sourceMacLow[it]=data[it];
- }
- }
- return error;
- }
- /*
- Function: Get the 32 higher bits of my MAC address
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: When it is executed stores the returned value by SH in the global
- "sourceMacHigh[4]" variable
- */
- uint8_t WaspXBeeCore::getOwnMacHigh()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_own_mac_high);
- error=gen_send(get_own_mac_high);
-
- if(error==0)
- {
- for(it=0;it<4;it++)
- {
- sourceMacHigh[it]=data[it];
- }
- }
- return error;
- }
- /*
- Function: Get the 64 bits of my MAC address
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes functions getOwnMacLow() and getOwnMacHigh()
- */
- uint8_t WaspXBeeCore::getOwnMac()
- {
- int8_t error=2;
- error=getOwnMacLow();
- if(error==0)
- {
- error=getOwnMacHigh();
- }
- return error;
- }
- /*
- Function: Set the 16b network address
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command in this protocol
- Parameters:
- NA_H : Higher byte of Network Address (0x00-0xFF)
- NA_L : Lower byte of Network Address (0x00-0xFF)
- Values: Stores in global "sourceNA[2]" variable the 16b address set by the user
- */
- uint8_t WaspXBeeCore::setOwnNetAddress(uint8_t NA_H, uint8_t NA_L)
- {
- int8_t error=2;
- if(protocol==XBEE_802_15_4)
- {
- error_AT=2;
- gen_data(set_own_net_address,NA_H,NA_L);
- gen_checksum(set_own_net_address);
- error=gen_send(set_own_net_address);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
-
- if(!error)
- {
- sourceNA[0]=NA_H;
- sourceNA[1]=NA_L;
- }
- return error;
- }
- /*
- Function: Get the 16b network address
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command in this protocol
- Values: Stores in global "sourceNA[2]" variable the returned 16b network address
- by MY command
- */
- uint8_t WaspXBeeCore::getOwnNetAddress()
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(get_own_net_address);
- error=gen_send(get_own_net_address);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
-
- if(!error)
- {
- sourceNA[0]=data[0];
- sourceNA[1]=data[1];
- }
- return error;
- }
- /*
- Function: Set Baudrate to use
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command in this protocol
- Parameters:
- baud_rate: integer that contains the baudrate
- Values: Stores in global "baudrate" variable the baudrate
- */
- uint8_t WaspXBeeCore::setBaudrate(uint8_t baud_rate)
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(set_baudrate,baud_rate);
- gen_checksum(set_baudrate);
- error=gen_send(set_baudrate);
-
- if(!error)
- {
- baudrate=baud_rate;
- }
- return error;
- }
- /*
- Function: Set API values
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command in this protocol
- Parameters:
- api_value: integer that contains the api value
- Values: Stores in global "apiValue" variable the baudrate
- */
- uint8_t WaspXBeeCore::setAPI(uint8_t api_value)
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(set_api_mode,api_value);
- gen_checksum(set_api_mode);
- error=gen_send(set_api_mode);
-
- if(!error)
- {
- apiValue=api_value;
- }
- return error;
- }
- /*
- Function: Set API options. Enable ZIgBee Application Layer Addressing
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command in this protocol
- Parameters:
- api_options: integer that contains the baudrate
- */
- uint8_t WaspXBeeCore::setAPIoptions(uint8_t api_options)
- {
- int8_t error=2;
-
- if( (protocol!=XBEE_802_15_4) )
- {
- error_AT=2;
- gen_data(set_api_options,api_options);
- gen_checksum(set_api_options);
- error=gen_send(set_api_options);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- return error;
- }
- /*
- Function: Set the network identifier
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Parameters:
- PANID: Array of integers than contains the 16b or 64b PAN ID
- Values: Stores in global "PAN_ID" variable the recent set PAN ID value
- */
- uint8_t WaspXBeeCore::setPAN(uint8_t* PANID)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- error_AT=2;
- gen_data(set_pan,PANID);
- gen_checksum(set_pan);
- error=gen_send(set_pan);
- }
-
- if(protocol==ZIGBEE)
- {
- error_AT=2;
- gen_data(set_pan_zb,PANID);
- gen_checksum(set_pan_zb);
- error=gen_send(set_pan_zb);
- }
- if(!error)
- {
- if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- for(it=0;it<2;it++)
- {
- PAN_ID[it]=PANID[it];
- }
- }
- if(protocol==ZIGBEE)
- {
- for(it=0;it<8;it++)
- {
- PAN_ID[it]=PANID[it];
- }
- }
- }
- return error;
- }
- /*
- Function: Get Network ID
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores in global "error" variable any error happened while execution
- Stores in global "PAN_ID" variable the 16b or 64b network PAN ID
- */
- uint8_t WaspXBeeCore::getPAN()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_pan);
- if( protocol==ZIGBEE ) error=gen_send(get_pan);
- else error=gen_send(get_pan);
-
- if(!error)
- {
- if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- for(it=0;it<2;it++)
- {
- PAN_ID[it]=data[it];
- delay(20);
- }
- }
- if(protocol==ZIGBEE)
- {
- for(it=0;it<8;it++)
- {
- PAN_ID[it]=data[it];
- delay(20);
- }
- }
- }
- return error;
- }
- /*
- Function: Set the module to the sleep mode specified.
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores the returned value by SM command in the global "sleepMode" variable
- Parameters:
- sleep: Defines the sleep mode to use by the XBee (0-5)
- */
- uint8_t WaspXBeeCore::setSleepMode(uint8_t sleep)
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(set_sleep_mode_xbee,sleep);
- gen_checksum(set_sleep_mode_xbee);
- error=gen_send(set_sleep_mode_xbee);
-
- if(!error)
- {
- sleepMode=sleep;
- }
- return error;
- }
- /*
- Function: Get the XBee mode
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores the XBee mode in the global "sleepMode" variable
- */
- uint8_t WaspXBeeCore::getSleepMode()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_sleep_mode_xbee);
- error=gen_send(get_sleep_mode_xbee);
-
- if(error==0)
- {
- sleepMode=data[0];
- }
- return error;
- }
- /*
- Function: Set the time the module has to be idle before start sleeping
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the ST parameter in XBee module
- Stores in global "awakeTime" the value of this time
- Parameters:
- awake: Array of integers that specifies the time to be awake before sleep
- */
- uint8_t WaspXBeeCore::setAwakeTime(uint8_t* awake)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
- {
- error_AT=2;
- gen_data(set_awake_time,awake);
- gen_checksum(set_awake_time);
- error=gen_send(set_awake_time);
- }
-
- if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
- {
- error_AT=2;
- gen_data(set_awake_time_DM,awake);
- gen_checksum(set_awake_time_DM);
- error=gen_send(set_awake_time_DM);
- }
-
- if(!error)
- {
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
- {
- awakeTime[0]=awake[0];
- awakeTime[1]=awake[1];
- }
- if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
- {
- awakeTime[0]=awake[0];
- awakeTime[1]=awake[1];
- awakeTime[2]=awake[2];
- }
- }
- return error;
- }
- /*
- Function: Set the cyclic sleeping time of the node
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the SP parameter in the XBee module
- Stores in global "sleepTime" the value of this time
- Parameters:
- sleep: Array of Integers that specifies the amount of time the module spends sleeping
- */
- uint8_t WaspXBeeCore::setSleepTime(uint8_t* sleep)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
- {
- error_AT=2;
- gen_data(set_sleep_time,sleep);
- gen_checksum(set_sleep_time);
- error=gen_send(set_sleep_time);
- }
-
- if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
- {
- error_AT=2;
- gen_data(set_sleep_time_DM,sleep);
- gen_checksum(set_sleep_time_DM);
- error=gen_send(set_sleep_time_DM);
- }
-
- if(!error)
- {
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
- {
- sleepTime[0]=sleep[0];
- sleepTime[1]=sleep[1];
- }
- if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
- {
- sleepTime[0]=sleep[0];
- sleepTime[1]=sleep[1];
- sleepTime[2]=sleep[2];
- }
- }
- return error;
- }
- /*
- Function: Set the channel frequency where the module is going to work
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Stores the selected channel in the global "channel" variable
- Parameters:
- _channel: Channel used to transmit (0x0B-0x1A)
- */
- uint8_t WaspXBeeCore::setChannel(uint8_t _channel)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) )
- {
- error_AT=2;
- gen_data(set_channel,_channel);
- gen_checksum(set_channel);
- error=gen_send(set_channel);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(!error)
- {
- channel=_channel;
- }
- return error;
- }
- /*
- Function: Get the actual frequency channel
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores the frequency channel in the global "channel" variable
- */
- uint8_t WaspXBeeCore::getChannel()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_channel);
- error=gen_send(get_channel);
-
- if(!error)
- {
- channel=data[0];
- }
- return error;
- }
- /*
- Function: Set the Node Indentifier
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the NI to the selected in the function
- The NI must be a 20 character max string
- Stores the given NI in the global "nodeID" variable
- Parameters:
- node: string that specifies the node indentifier
- */
- uint8_t WaspXBeeCore::setNodeIdentifier(char* node)
- {
- uint8_t* NI = (uint8_t*) calloc(30,sizeof(uint8_t)); //{0x7E, 0x00, 0x00, 0x08, 0x52, 0x4E, 0x49, 0x02};
- NI[0]=0x7E;
- NI[1]=0x00;
- NI[3]=0x08;
- NI[4]=0x52;
- NI[5]=0x4E;
- NI[6]=0x49;
- uint8_t num_NI;
- int8_t error=2;
- uint8_t* ByteIN = (uint8_t*) calloc(20,sizeof(uint8_t));
-
- uint8_t counter=0;
- uint8_t counter3=0;
- uint8_t est=1;
- uint8_t frame_ID=NI[4];
- uint8_t end=0;
- uint16_t interval=WAIT_TIME;
- uint8_t checksum=0;
- uint8_t numberBytes=9;
- uint8_t status=0;
- uint8_t undesired=0;
- it=0;
- error_AT=2;
- while( (node[it]!='\0') )
- {
- NI[it+7]=uint8_t(node[it]);
- it++;
- }
- NI[2]=4+it;
- for(it=3;it<(7+(NI[2]-4));it++)
- {
- checksum=checksum+NI[it];
- }
- while( (checksum>255))
- {
- checksum=checksum-256;
- }
- checksum=255-checksum;
- NI[7+NI[2]-4]=checksum;
- while(counter<(8+NI[2]-4))
- {
- XBee.print(NI[counter], BYTE);
- counter++;
- }
- counter=0;
- clearCommand();
- command[5]=0x4E;
- command[6]=0x49;
- error=parse_message(command);
- if(error==0)
- {
- for(it=0;it<NI[2]-4;it++)
- {
- nodeID[it]=node[it];
- }
- }
- free(NI);
- free(ByteIN);
- NI=NULL;
- ByteIN=NULL;
- return error;
- }
- /*
- Function: Get the Node Identifier
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores the NI in the global "nodeID" variable
- */
- uint8_t WaspXBeeCore::getNodeIdentifier()
- {
- int8_t error=2;
- error_AT=2;
- gen_data(get_NI);
- error=gen_send(get_NI);
-
- if(!error)
- {
- for(it=0;it<data_length;it++)
- {
- nodeID[it]=char(data[it]);
- }
- }
- return error;
- }
- /*
- Function: Scans for brothers in the same channel and same PAN ID
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable
- Stores in global "totalScannedBrothers" the number of found brothers
- */
- uint8_t WaspXBeeCore::scanNetwork()
- {
- uint8_t error=2;
-
- error_AT=2;
- totalScannedBrothers=0;
- gen_data(scan_network);
- error=gen_send(scan_network);
- return error;
- }
- /*
- Function: Scans for brothers in the same channel and same PAN ID
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable
- Stores in global "totalScannedBrothers" the number of founded brothers
- Parameters:
- node: 20-byte max string containing NI of the node to search
- */
- uint8_t WaspXBeeCore::scanNetwork(char* node)
- {
- uint8_t* ND = (uint8_t*) calloc(30,sizeof(uint8_t)); //{0x7E, 0x00, 0x04, 0x08, 0x52, 0x4E, 0x44, 0x13};
- ND[0]=0x7E;
- ND[1]=0x00;
- ND[3]=0x08;
- ND[4]=0x52;
- ND[5]=0x4E;
- ND[6]=0x44;
- uint8_t num_ND;
- int8_t error=2;
- uint8_t* ByteIN = (uint8_t*) calloc(20,sizeof(uint8_t));
-
- uint8_t cont=1;
- uint8_t cont2=0;
- uint8_t counter=0;
- uint8_t counter3=0;
- uint8_t est=1;
- uint8_t frame_ID=ND[4];
- uint8_t end=0;
- uint16_t interval=WAIT_TIME2;
- uint16_t length=0;
- uint16_t aux=0;
- uint8_t existsNI=0;
- uint8_t length_NI=0;
- uint16_t checksum=0;
- uint8_t finish=0;
-
- error_AT=2;
- totalScannedBrothers=0;
- if( (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- interval=14000;
- }
- it=0;
- while( (node[it]!='\0') )
- {
- ND[it+7]=uint8_t(node[it]);
- it++;
- }
- ND[2]=4+it;
- for(it=3;it<(7+(ND[2]-4));it++)
- {
- checksum=checksum+ND[it];
- }
- while( (checksum>255))
- {
- checksum=checksum-256;
- }
- checksum=255-checksum;
- ND[7+ND[2]-4]=checksum;
- while(counter<(8+ND[2]-4))
- {
- XBee.print(ND[counter], BYTE);
- counter++;
- }
- counter=0;
- clearCommand();
- command[5]=ND[5];
- command[6]=ND[6];
- error=parse_message(command);
-
- free(ND);
- free(ByteIN);
- ND=NULL;
- ByteIN=NULL;
- return error;
- }
- /*
- Function: Defines the amount of time the scanNetwork() function is scanning
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Changes the NT command
- Stores in global "scanTime" variable the recent set time
- Parameters:
- time: amount of time ND is scanning for brothers (0x01-0xFC)
- */
- uint8_t WaspXBeeCore::setScanningTime(uint8_t* time)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_900) )
- {
- error_AT=2;
- gen_data(set_scanning_time,time);
- gen_checksum(set_scanning_time);
- error=gen_send(set_scanning_time);
- }
-
- if( (protocol==DIGIMESH) || (protocol==XBEE_868) )
- {
- error_AT=2;
- gen_data(set_scanning_time_DM,time);
- gen_checksum(set_scanning_time_DM);
- error=gen_send(set_scanning_time_DM);
- }
-
-
- if(!error)
- {
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_900) )
- {
- scanTime[0]=time[0];
- }
- if( (protocol==DIGIMESH) || (protocol==XBEE_868) )
- {
- scanTime[0]=time[0];
- scanTime[1]=time[1];
- }
- }
- return error;
- }
- /*
- Function: Get the Scanning Time
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores in global "error" variable any error happened while execution
- Stores in global "scanTime" the value of scanning time
- */
- uint8_t WaspXBeeCore::getScanningTime()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_scanning_time);
- if( (protocol==DIGIMESH) || (protocol==XBEE_868) || (protocol==ZIGBEE) || (protocol==XBEE_900) ) error=gen_send(get_scanning_time);
- else error=gen_send(get_scanning_time);
-
- if(!error)
- {
- if( (protocol==XBEE_802_15_4) )
- {
- scanTime[0]=data[0];
- }
- if( (protocol==ZIGBEE) || (protocol==XBEE_900) )
- {
- scanTime[0]=data[1];
- }
- if( (protocol==DIGIMESH) || (protocol==XBEE_868) )
- {
- scanTime[0]=data[0];
- scanTime[1]=data[1];
- }
- }
- return error;
- }
- /*
- Function: Set the options value for the network discovery command
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the NO command
- Parameters:
- options: chosen option (0x00-0x03)
- */
- uint8_t WaspXBeeCore::setDiscoveryOptions(uint8_t options)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(set_discov_options,options);
- gen_checksum(set_discov_options);
- error=gen_send(set_discov_options);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(!error)
- {
- discoveryOptions=options;
- }
- return error;
- }
- /*
- Function: Get the options value for the network discovery command
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes the NO command. Stores in global "discoveryOptions" variable the options
- */
- uint8_t WaspXBeeCore::getDiscoveryOptions()
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(get_discov_options);
- error=gen_send(get_discov_options);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(error==0)
- {
- discoveryOptions=data[0];
- }
- return error;
- }
- /*
- Function: Performs a quick search.
- 802.15.4 : It keeps in DL the MY of the looked up NI brother
- ZIGBEE : Stores in global "paquete" naD,macDH,macDL from the searched device
- DIGIMESH: Stores in global "paquete" macDH,macDL from the searched device
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes DN command.
- Parameters:
- node: string that specifies the NI that identifies the searched brother
- length: length of that NI (0-20)
- */
- uint8_t WaspXBeeCore::nodeSearch(char* node, struct packetXBee* paq)
- {
- uint8_t* DN = (uint8_t*) calloc(30,sizeof(uint8_t)); //{0x7E, 0x00, 0x00, 0x08, 0x52, 0x44, 0x4E, 0xE3};
- DN[0]=0x7E;
- DN[1]=0x00;
- DN[3]=0x08;
- DN[4]=0x52;
- DN[5]=0x44;
- DN[6]=0x4E;
- uint8_t num_DN;
- int8_t error=2;
- uint8_t* ByteIN = (uint8_t*) calloc(25,sizeof(uint8_t));
-
- uint8_t counter=0;
- uint8_t counter3=0;
- uint8_t est=1;
- uint8_t frame_ID=DN[4];
- uint8_t end=0;
- uint16_t interval=2000;
- uint8_t checksum=0;
- uint8_t status=0;
- uint8_t undesired=0;
- error_AT=2;
- if(protocol==DIGIMESH)
- {
- interval=14000;
- }
- it=0;
- while( (node[it]!='\0') )
- {
- DN[it+7]=uint8_t(node[it]);
- it++;
- }
- DN[2]=4+it;
- for(it=3;it<(7+(DN[2]-4));it++)
- {
- checksum=checksum+DN[it];
- }
- while( (checksum>255))
- {
- checksum=checksum-256;
- }
- checksum=255-checksum;
- DN[7+DN[2]-4]=checksum;
- while(counter<(8+DN[2]-4))
- {
- XBee.print(DN[counter], BYTE);
- counter++;
- }
-
- counter=0;
- clearCommand();
- command[5]=0x44;
- command[6]=0x4E;
- error=parse_message(command);
-
- if(error==0)
- {
- if( (protocol==ZIGBEE) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- for(it=0;it<2;it++)
- {
- paq->naD[it]=data[it];
- }
- for(it=0;it<4;it++)
- {
- paq->macDH[it]=data[it+2];
- }
- for(it=0;it<4;it++)
- {
- paq->macDL[it]=data[it+6];
- }
- }
- if(protocol==DIGIMESH)
- {
- for(it=0;it<4;it++)
- {
- paq->macDH[it]=data[it];
- }
- for(it=0;it<4;it++)
- {
- paq->macDL[it]=data[it+4];
- }
- }
- }
- free(DN);
- free(ByteIN);
- DN=NULL;
- ByteIN=NULL;
- return error;
- }
- /*
- Function: Write the current parameters to a non volatil memory
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes the WR command
- */
- uint8_t WaspXBeeCore::writeValues()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(write_values);
- error=gen_send(write_values);
-
- return error;
- }
- /*
- Function: Specifies the list of channels to scan when performing an energy scan
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Change the SC command. Stores in global "scanChannels" variable the list of channels
- Parameters:
- channel_H: higher byte of list of channels (0x00-0xFF)
- channel_L: lower byte of list of channels (0x00-0xFF
- */
- uint8_t WaspXBeeCore::setScanningChannels(uint8_t channel_H, uint8_t channel_L)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(set_scanning_channel,channel_H,channel_L);
- gen_checksum(set_scanning_channel);
- error=gen_send(set_scanning_channel);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(error==0)
- {
- scanChannels[0]=channel_H;
- scanChannels[1]=channel_L;
- }
- return error;
- }
- /*
- Function: Get the list of channels to scan when performing an energy scan
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Stores in global "scanChannels" variable the scanning channel list
- */
- uint8_t WaspXBeeCore::getScanningChannels()
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(get_scanning_channel);
- error=gen_send(get_scanning_channel);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(error==0)
- {
- for(it=0;it<2;it++)
- {
- scanChannels[it]=data[it];
- }
- }
- return error;
- }
- /*
- Function: It sets the time the energy scan will be performed
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Change the ED command. Stores in global "energyChannel" variable the energy in each channel
- Parameters:
- duration: amount of time that the energy scan will be performed (0-6)
- */
- uint8_t WaspXBeeCore::setDurationEnergyChannels(uint8_t duration)
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) )
- {
- error_AT=2;
- gen_data(set_duration_energy,duration);
- gen_checksum(set_duration_energy);
- error=gen_send(set_duration_energy);
- }
- else if( (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(set_duration_energy_ZB,duration);
- gen_checksum(set_duration_energy_ZB);
- error=gen_send(set_duration_energy_ZB);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
-
- if(error==0)
- {
- if(protocol==XBEE_802_15_4)
- {
- for(it=0;it<data_length;it++)
- {
- energyChannel[it]=data[it];
- }
- }
- if(protocol==ZIGBEE)
- {
- timeEnergyChannel=data[0];
- }
- }
- return error;
- }
- /*
- Function: It gets the time the energy scan will be performed
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Change the SD command. Stores in global "timeEnergyChannel" variable the time the energy
- scan will be performed
- */
- uint8_t WaspXBeeCore::getDurationEnergyChannels()
- {
- int8_t error=2;
-
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(get_duration_energy);
- error=gen_send(get_duration_energy);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(!error)
- {
- timeEnergyChannel=data[0];
- }
- return error;
- }
- /*
- Function: Sets the encryption key to be used in the 128b AES algorithm
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the KY command. Stores in global "linkKey" variable the key has been set
- Parameters:
- key: 16 byte array of chars that specifies the 128b AES key
- */
- uint8_t WaspXBeeCore::setLinkKey(char* key)
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(set_link_key,key);
- gen_checksum(set_link_key);
- error=gen_send(set_link_key);
-
- if(!error)
- {
- for(it=0;it<16;it++)
- {
- linkKey[it]=char(key[it]);
- }
- }
- return error;
- }
- /*
- Function: Sets the encryption mode on/off
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the EE command. Stores in global "encryptMode" variable the encryption mode
- Parameters:
- mode: on/off the encryption mode (1/0)
- */
- uint8_t WaspXBeeCore::encryptionMode(uint8_t mode)
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(set_encryption,mode);
- gen_checksum(set_encryption);
- error=gen_send(set_encryption);
- if(!error)
- {
- encryptMode=mode;
- }
- return error;
- }
- /*
- Function: Select the power level at which the RF module transmits
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the PL command. Stores in global "powerLevel" the power level at which RF tx
- Parameters:
- value: power level of transmission (0-4)
- */
- uint8_t WaspXBeeCore::setPowerLevel(uint8_t value)
- {
- int8_t error=2;
-
- if(protocol!=XBEE_900)
- {
- error_AT=2;
- gen_data(set_power_level,value);
- gen_checksum(set_power_level);
- error=gen_send(set_power_level);
- }
- if(!error)
- {
- powerLevel=value;
- }
- return error;
- }
- /*
- Function: Get the Received Signal Strength Indicator of the last received packet
- Returns: Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores in global "valueRSSI" variable the RSSI value of last received packet
- */
- uint8_t WaspXBeeCore::getRSSI()
- {
- int8_t error=2;
- uint8_t* ByteIN = (uint8_t*) calloc(40,sizeof(uint8_t));
- uint8_t i=0;
- if( (protocol == XBEE_802_15_4 ) || (protocol==ZIGBEE) )
- {
- error_AT=2;
- gen_data(get_RSSI);
- error=gen_send(get_RSSI);
- }
- else if( (protocol== DIGIMESH) || (protocol==XBEE_868) || (protocol==XBEE_900) )
- {
- delay(2000);
- XBee.print("+++");
- delay(2000);
- XBee.flush();
- XBee.println("atdb");
- delay(1000);
- error_AT=2;
- while(XBee.available()>0)
- {
- ByteIN[i]=XBee.read();
- error=0;
- i++;
- error_AT=0;
- }
- i=0;
- XBee.println("atcn");
- delay(1000);
- valueRSSI[0]=Utils.str2hex(ByteIN);
- }
- if(error==0)
- {
- if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
- {
- valueRSSI[0]=data[0];
- }
- }
- free(ByteIN);
- ByteIN=NULL;
- return error;
- }
- /*
- Function: Get the Harware Version
- Returns: Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores in global "hardVersion" variable the Hardware Version
- */
- uint8_t WaspXBeeCore::getHardVersion()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_hard_version);
- error=gen_send(get_hard_version);
- if(!error)
- {
- hardVersion[0]=data[0];
- hardVersion[1]=data[1];
- }
- return error;
- }
- /*
- Function: Get the version of the firmware
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores in global "softVersion" variable the firmware version
- */
- uint8_t WaspXBeeCore::getSoftVersion()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_soft_version);
- error=gen_send(get_soft_version);
- if(error==0)
- {
- softVersion[0]=data[0];
- softVersion[1]=data[1];
- }
- return error;
- }
- /*
- Function: Set the RSSI time
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Change the RP command. Stores in global "timeRSSI" variable the RSSI time
- Parameters:
- time: amount of time to do the pwm (0x00-0xFF)
- */
- uint8_t WaspXBeeCore::setRSSItime(uint8_t time)
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(set_RSSI_time,time);
- gen_checksum(set_RSSI_time);
- error=gen_send(set_RSSI_time);
- if(!error)
- {
- timeRSSI=time;
- }
- return error;
- }
- /*
- Function: Get the RSSI time
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Stores in global "timeRSSI" variable the RSSI time
- */
- uint8_t WaspXBeeCore::getRSSItime()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(get_RSSI_time);
- error=gen_send(get_RSSI_time);
- if(!error)
- {
- timeRSSI=data[0];
- }
- return error;
- }
- /*
- Function: Immediately applies new settings without exiting command mode
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes the AC command
- */
- uint8_t WaspXBeeCore::applyChanges()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(apply_changes);
- error=gen_send(apply_changes);
- return error;
- }
- /*
- Function: Reset the XBee Firmware
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes the FR command
- */
- uint8_t WaspXBeeCore::reset()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(reset_xbee);
- error=gen_send(reset_xbee);
- return error;
- }
- /*
- Function: Set the parameteres to the factory defaults
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Values: Executes the RE command
- */
- uint8_t WaspXBeeCore::resetDefaults()
- {
- int8_t error=2;
-
- error_AT=2;
- gen_data(reset_defaults_xbee);
- error=gen_send(reset_defaults_xbee);
- return error;
- }
- /*
- Function: Configure options for sleep
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Change the SO command. Stores in global "sleepOptions" variable the options
- Parameters:
- soption: options for sleep (0x00-0xFF)
- */
- uint8_t WaspXBeeCore::setSleepOptions(uint8_t soption)
- {
- int8_t error=2;
-
- if( (protocol==ZIGBEE) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- error_AT=2;
- gen_data(set_sleep_options_xbee,soption);
- gen_checksum(set_sleep_options_xbee);
- error=gen_send(set_sleep_options_xbee);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(!error)
- {
- sleepOptions=soption;
- }
- return error;
- }
- /*
- Function: Reads the options for sleep
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- error=-1 --> Forbidden command for this protocol
- Values: Executes the SO command. Stores in global "sleepOptions" variable the options
- */
- uint8_t WaspXBeeCore::getSleepOptions()
- {
- int8_t error=2;
-
- if( (protocol==ZIGBEE) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
- {
- error_AT=2;
- gen_data(get_sleep_options_xbee);
- error=gen_send(get_sleep_options_xbee);
- }
- else
- {
- error_AT=-1;
- error=-1;
- }
- if(!error)
- {
- sleepOptions=data[0];
- }
- return error;
- }
- /*
- Function: Transparent function. The user introduces an AT command within a string and the function executes it without knowing its meaning
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- Parameters:
- atcommand : String to specify the AT command to execute
- */
- uint8_t WaspXBeeCore::sendCommandAT(char* atcommand)
- {
- uint8_t* AT = (uint8_t*) calloc(30,sizeof(uint8_t));// {0x7E, 0x00, 0x00, 0x08, 0x52, 0x00, 0x00, 0x00};
- AT[0]=0x7E;
- AT[1]=0x00;
- AT[3]=0x08;
- AT[4]=0x52;
- uint8_t num_AT=0;
- int8_t error=2;
- uint8_t it2=0;
-
- uint8_t* ByteIN = (uint8_t*) calloc(120,sizeof(uint8_t));
- uint8_t counter=0;
- uint8_t counter3=0;
- uint8_t est=1;
- uint8_t frame_ID=AT[4];
- uint8_t end=0;
- uint16_t interval=WAIT_TIME;
- uint8_t checksum=0;
- uint16_t length=0;
-
- it=0;
- error_AT=2;
- while( atcommand[it2]!='#' )
- {
- if( it>=2 )
- {
- if( atcommand[it2+1]!='#' )
- {
- AT[it+5]=Utils.converter(atcommand[2*(it-1)],atcommand[2*(it-1)+1]);
- it2+=2;
- }
- else
- {
- switch( atcommand[it2] )
- {
- case '0': AT[it+5]=0;
- break;
- case '1': AT[it+5]=1;
- break;
- case '2': AT[it+5]=2;
- break;
- case '3': AT[it+5]=3;
- break;
- case '4': AT[it+5]=4;
- break;
- case '5': AT[it+5]=5;
- break;
- case '6': AT[it+5]=6;
- break;
- case '7': AT[it+5]=7;
- break;
- case '8': AT[it+5]=8;
- break;
- case '9': AT[it+5]=9;
- break;
- case 'A': AT[it+5]='A';
- break;
- case 'B': AT[it+5]='B';
- break;
- case 'C': AT[it+5]='C';
- break;
- case 'D': AT[it+5]='D';
- break;
- case 'E': AT[it+5]='E';
- break;
- case 'F': AT[it+5]='F';
- break;
- }
- it2++;
- }
- }
- else
- {
- AT[it+5]=atcommand[it];
- it2++;
- }
- it++;
- }
- length=it;
-
- AT[2]=2+length;
- for(it=3;it<(5+length);it++)
- {
- checksum=checksum+AT[it];
- }
- while( (checksum>255))
- {
- checksum=checksum-256;
- }
- checksum=255-checksum;
- AT[5+length]=checksum;
- while(counter<(6+length))
- {
- XBee.print(AT[counter], BYTE);
- counter++;
- }
- counter=0;
- clearCommand();
- command[5]=AT[5];
- command[6]=AT[6];
- data_length=0;
- error=parse_message(command);
- if(error==0)
- {
- if(data_length>0)
- {
- for(it=0;it<data_length;it++)
- {
- commandAT[it]=data[it];
- delay(20);
- }
- }
- else
- {
- commandAT[0]=0x4F;
- commandAT[1]=0x4B;
- }
- }
-
- free(AT);
- AT=NULL;
- free(ByteIN);
- ByteIN=NULL;
- return error;
- }
- /*
- Function: Connect XBee, activating switch in Waspmote
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- */
- uint8_t WaspXBeeCore::ON()
- {
- uint8_t error=2;
- XBee.begin();
- XBee.setMode(XBEE_ON);
- if( protocol== ZIGBEE || protocol==XBEE_868 ) delay(500);
- else delay(50);
- error=0;
- XBee_ON=1;
- return error;
- }
- /*
- Function: disconnects XBee, switching it off and closing the UART
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- */
- uint8_t WaspXBeeCore::OFF()
- {
- uint8_t error=2;
- XBee.close();
- XBee.setMode(XBEE_OFF);
- error=0;
- XBee_ON=0;
- return error;
- }
- /*
- Function: Set XBee to sleep, asserting PIN 9
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- */
- uint8_t WaspXBeeCore::sleep()
- {
- uint8_t error=2;
- pinMode(XBEE_SLEEP, OUTPUT);
- digitalWrite(XBEE_SLEEP,HIGH);
- XBee.close();
- error=0;
- return error;
- }
- /*
- Function: Wake up XBee, de-asserting PIN 9
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed with no errors
- */
- uint8_t WaspXBeeCore::wake()
- {
- uint8_t error=2;
- pinMode(XBEE_SLEEP, OUTPUT);
- digitalWrite(XBEE_SLEEP,LOW);
- XBee.begin();
- delay(50);
- error=0;
- return error;
- }
- /*
- Function: Send a packet from one XBee to another XBee in API mode
- Returns: Integer that determines if there has been any error
- error=2 --> The command has not been executed
- error=1 --> There has been an error while executing the command
- error=0 --> The command has been executed …
Large files files are truncated, but you can click here to view the full file