PageRenderTime 70ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/WaspXBeeCore.cpp

https://github.com/mencey/waspmote-api
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

  1. /*
  2. * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
  3. * http://www.libelium.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 2.1 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Version: 0.13
  17. * Design: David Gascón
  18. * Implementation: Alberto Bielsa
  19. */
  20. #ifndef __WPROGRAM_H__
  21. #include "WaspClasses.h"
  22. #endif
  23. #ifndef __WASPXBEECONSTANTS_H__
  24. #include "WaspXBeeConstants.h"
  25. #endif
  26. char get_own_mac_low[] = "7E00040852534C06";
  27. char get_own_mac_high[] = "7E0004085253480A";
  28. char set_own_net_address[] = "7E000608524D59000000";
  29. char get_own_net_address[] = "7E000408524D59FF";
  30. char set_baudrate[] = "7E0005085242440000";
  31. char set_api_mode[] = "7E0005085241500000";
  32. char set_api_options[] = "7E00050852414F0000";
  33. char set_pan[] = "7E000608524944000000";
  34. char set_pan_zb[] = "7E000C08524944000000000000000000";
  35. char get_pan[] = "7E00040852494418";
  36. char set_sleep_mode_xbee[] = "7E00050852534D0000";
  37. char get_sleep_mode_xbee[] = "7E00040852534D05";
  38. char set_awake_time[] = "7E000608525354000000";
  39. char set_awake_time_DM[] = "7E00070852535400000000";
  40. char set_sleep_time[] = "7E000608525350000000";
  41. char set_sleep_time_DM[] = "7E00070852535000000000";
  42. char set_channel[] = "7E0005085243480000";
  43. char get_channel[] = "7E0004085243481A";
  44. char get_NI[] = "7E000408524E490E";
  45. char set_scanning_time[] = "7E000508524E540000";
  46. char set_scanning_time_DM[] = "7E000608524E54000000";
  47. char get_scanning_time[] = "7E000408524E5403";
  48. char set_discov_options[] = "7E000508524E4F0000";
  49. char get_discov_options[] = "7E000408524E4F08";
  50. char write_values[] = "7E000408525752FC";
  51. char set_scanning_channel[] = "7E000608525343000000";
  52. char get_scanning_channel[] = "7E0004085253430F";
  53. char get_duration_energy[] = "7E0004085253440E";
  54. char set_link_key[] = "7E001408524B590000000000000000000000000000000000";
  55. char set_encryption[] = "7E0005085245450000";
  56. char set_power_level[] = "7E00050852504C0000";
  57. char get_RSSI[] = "7E0004085244421F";
  58. char get_hard_version[] = "7E00040852485607";
  59. char get_soft_version[] = "7E000408525652FD";
  60. char set_RSSI_time[] = "7E0005085252500000";
  61. char get_RSSI_time[] = "7E00040852525003";
  62. char apply_changes[] = "7E00040852414321";
  63. char reset_xbee[] = "7E0004085246520D";
  64. char reset_defaults_xbee[] = "7E0004085252450E";
  65. char set_sleep_options_xbee[] = "7E00050852534F0000";
  66. char get_sleep_options_xbee[] = "7E00040852534F03";
  67. char scan_network[] = "7E000408524E4413";
  68. extern char set_duration_energy[];
  69. extern char set_duration_energy_ZB[];
  70. /*
  71. Function: Initializes all the global variables that will be used later
  72. Returns: Nothing
  73. Parameters:
  74. protocol_used: Protocol the XBee is using
  75. frequency: Frequency the XBee is running on
  76. model_used: Model of XBee used
  77. */
  78. void WaspXBeeCore::init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used)
  79. {
  80. protocol=protocol_used;
  81. freq=frequency;
  82. model=model_used;
  83. totalFragmentsReceived=0;
  84. pendingPackets=0;
  85. pos=0;
  86. discoveryOptions=0x00;
  87. if(protocol==XBEE_802_15_4)
  88. {
  89. awakeTime[0]=AWAKE_TIME_802_15_4_H;
  90. awakeTime[1]=AWAKE_TIME_802_15_4_L;
  91. sleepTime[0]=SLEEP_TIME_802_15_4_H;
  92. sleepTime[1]=SLEEP_TIME_802_15_4_L;
  93. scanTime[0]=SCAN_TIME_802_15_4;
  94. scanChannels[0]=SCAN_CHANNELS_802_15_4_H;
  95. scanChannels[1]=SCAN_CHANNELS_802_15_4_L;
  96. encryptMode=ENCRYPT_MODE_802_15_4;
  97. powerLevel=POWER_LEVEL_802_15_4;
  98. timeRSSI=TIME_RSSI_802_15_4;
  99. sleepOptions=SLEEP_OPTIONS_802_15_4;
  100. }
  101. if(protocol==ZIGBEE)
  102. {
  103. awakeTime[0]=AWAKE_TIME_ZIGBEE_H;
  104. awakeTime[1]=AWAKE_TIME_ZIGBEE_L;
  105. sleepTime[0]=SLEEP_TIME_ZIGBEE_H;
  106. sleepTime[1]=SLEEP_TIME_ZIGBEE_L;
  107. scanTime[0]=SCAN_TIME_ZIGBEE;
  108. scanChannels[0]=SCAN_CHANNELS_ZIGBEE_H;
  109. scanChannels[1]=SCAN_CHANNELS_ZIGBEE_L;
  110. timeEnergyChannel=TIME_ENERGY_CHANNEL_ZIGBEE;
  111. encryptMode=ENCRYPT_MODE_ZIGBEE;
  112. powerLevel=POWER_LEVEL_ZIGBEE;
  113. timeRSSI=TIME_RSSI_ZIGBEE;
  114. sleepOptions=SLEEP_OPTIONS_ZIGBEE;
  115. }
  116. if(protocol==DIGIMESH)
  117. {
  118. awakeTime[0]=AWAKE_TIME_DIGIMESH_H;
  119. awakeTime[1]=AWAKE_TIME_DIGIMESH_M;
  120. awakeTime[2]=AWAKE_TIME_DIGIMESH_L;
  121. sleepTime[0]=SLEEP_TIME_DIGIMESH_H;
  122. sleepTime[1]=SLEEP_TIME_DIGIMESH_M;
  123. sleepTime[2]=SLEEP_TIME_DIGIMESH_L;
  124. scanTime[0]=SCAN_TIME_DIGIMESH_H;
  125. scanTime[1]=SCAN_TIME_DIGIMESH_L;
  126. encryptMode=ENCRYPT_MODE_DIGIMESH;
  127. powerLevel=POWER_LEVEL_DIGIMESH;
  128. timeRSSI=TIME_RSSI_DIGIMESH;
  129. sleepOptions=SLEEP_OPTIONS_DIGIMESH;
  130. }
  131. data_length=0;
  132. it=0;
  133. start=0;
  134. finish=0;
  135. add_type=0;
  136. mode=0;
  137. frag_length=0;
  138. TIME1=0;
  139. nextIndex1=0;
  140. frameNext=0;
  141. replacementPolicy=XBEE_OUT;
  142. indexNotModified=1;
  143. error_AT=2;
  144. error_RX=2;
  145. error_TX=2;
  146. clearFinishArray();
  147. clearCommand();
  148. apsEncryption=0;
  149. }
  150. /*
  151. Function: Get the 32 lower bits of my MAC address
  152. Returns: Integer that determines if there has been any error
  153. error=2 --> The command has not been executed
  154. error=1 --> There has been an error while executing the command
  155. error=0 --> The command has been executed with no errors
  156. Values: When it is executed stores the returned value by SL command in the global
  157. "sourceMacLow[4]" variable
  158. */
  159. uint8_t WaspXBeeCore::getOwnMacLow()
  160. {
  161. int8_t error=2;
  162. error_AT=2;
  163. gen_data(get_own_mac_low);
  164. error=gen_send(get_own_mac_low);
  165. if(error==0)
  166. {
  167. for(it=0;it<4;it++)
  168. {
  169. sourceMacLow[it]=data[it];
  170. }
  171. }
  172. return error;
  173. }
  174. /*
  175. Function: Get the 32 higher bits of my MAC address
  176. Returns: Integer that determines if there has been any error
  177. error=2 --> The command has not been executed
  178. error=1 --> There has been an error while executing the command
  179. error=0 --> The command has been executed with no errors
  180. Values: When it is executed stores the returned value by SH in the global
  181. "sourceMacHigh[4]" variable
  182. */
  183. uint8_t WaspXBeeCore::getOwnMacHigh()
  184. {
  185. int8_t error=2;
  186. error_AT=2;
  187. gen_data(get_own_mac_high);
  188. error=gen_send(get_own_mac_high);
  189. if(error==0)
  190. {
  191. for(it=0;it<4;it++)
  192. {
  193. sourceMacHigh[it]=data[it];
  194. }
  195. }
  196. return error;
  197. }
  198. /*
  199. Function: Get the 64 bits of my MAC address
  200. Returns: Integer that determines if there has been any error
  201. error=2 --> The command has not been executed
  202. error=1 --> There has been an error while executing the command
  203. error=0 --> The command has been executed with no errors
  204. Values: Executes functions getOwnMacLow() and getOwnMacHigh()
  205. */
  206. uint8_t WaspXBeeCore::getOwnMac()
  207. {
  208. int8_t error=2;
  209. error=getOwnMacLow();
  210. if(error==0)
  211. {
  212. error=getOwnMacHigh();
  213. }
  214. return error;
  215. }
  216. /*
  217. Function: Set the 16b network address
  218. Returns: Integer that determines if there has been any error
  219. error=2 --> The command has not been executed
  220. error=1 --> There has been an error while executing the command
  221. error=0 --> The command has been executed with no errors
  222. error=-1 --> Forbidden command in this protocol
  223. Parameters:
  224. NA_H : Higher byte of Network Address (0x00-0xFF)
  225. NA_L : Lower byte of Network Address (0x00-0xFF)
  226. Values: Stores in global "sourceNA[2]" variable the 16b address set by the user
  227. */
  228. uint8_t WaspXBeeCore::setOwnNetAddress(uint8_t NA_H, uint8_t NA_L)
  229. {
  230. int8_t error=2;
  231. if(protocol==XBEE_802_15_4)
  232. {
  233. error_AT=2;
  234. gen_data(set_own_net_address,NA_H,NA_L);
  235. gen_checksum(set_own_net_address);
  236. error=gen_send(set_own_net_address);
  237. }
  238. else
  239. {
  240. error_AT=-1;
  241. error=-1;
  242. }
  243. if(!error)
  244. {
  245. sourceNA[0]=NA_H;
  246. sourceNA[1]=NA_L;
  247. }
  248. return error;
  249. }
  250. /*
  251. Function: Get the 16b network address
  252. Returns: Integer that determines if there has been any error
  253. error=2 --> The command has not been executed
  254. error=1 --> There has been an error while executing the command
  255. error=0 --> The command has been executed with no errors
  256. error=-1 --> Forbidden command in this protocol
  257. Values: Stores in global "sourceNA[2]" variable the returned 16b network address
  258. by MY command
  259. */
  260. uint8_t WaspXBeeCore::getOwnNetAddress()
  261. {
  262. int8_t error=2;
  263. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  264. {
  265. error_AT=2;
  266. gen_data(get_own_net_address);
  267. error=gen_send(get_own_net_address);
  268. }
  269. else
  270. {
  271. error_AT=-1;
  272. error=-1;
  273. }
  274. if(!error)
  275. {
  276. sourceNA[0]=data[0];
  277. sourceNA[1]=data[1];
  278. }
  279. return error;
  280. }
  281. /*
  282. Function: Set Baudrate to use
  283. Returns: Integer that determines if there has been any error
  284. error=2 --> The command has not been executed
  285. error=1 --> There has been an error while executing the command
  286. error=0 --> The command has been executed with no errors
  287. error=-1 --> Forbidden command in this protocol
  288. Parameters:
  289. baud_rate: integer that contains the baudrate
  290. Values: Stores in global "baudrate" variable the baudrate
  291. */
  292. uint8_t WaspXBeeCore::setBaudrate(uint8_t baud_rate)
  293. {
  294. int8_t error=2;
  295. error_AT=2;
  296. gen_data(set_baudrate,baud_rate);
  297. gen_checksum(set_baudrate);
  298. error=gen_send(set_baudrate);
  299. if(!error)
  300. {
  301. baudrate=baud_rate;
  302. }
  303. return error;
  304. }
  305. /*
  306. Function: Set API values
  307. Returns: Integer that determines if there has been any error
  308. error=2 --> The command has not been executed
  309. error=1 --> There has been an error while executing the command
  310. error=0 --> The command has been executed with no errors
  311. error=-1 --> Forbidden command in this protocol
  312. Parameters:
  313. api_value: integer that contains the api value
  314. Values: Stores in global "apiValue" variable the baudrate
  315. */
  316. uint8_t WaspXBeeCore::setAPI(uint8_t api_value)
  317. {
  318. int8_t error=2;
  319. error_AT=2;
  320. gen_data(set_api_mode,api_value);
  321. gen_checksum(set_api_mode);
  322. error=gen_send(set_api_mode);
  323. if(!error)
  324. {
  325. apiValue=api_value;
  326. }
  327. return error;
  328. }
  329. /*
  330. Function: Set API options. Enable ZIgBee Application Layer Addressing
  331. Returns: Integer that determines if there has been any error
  332. error=2 --> The command has not been executed
  333. error=1 --> There has been an error while executing the command
  334. error=0 --> The command has been executed with no errors
  335. error=-1 --> Forbidden command in this protocol
  336. Parameters:
  337. api_options: integer that contains the baudrate
  338. */
  339. uint8_t WaspXBeeCore::setAPIoptions(uint8_t api_options)
  340. {
  341. int8_t error=2;
  342. if( (protocol!=XBEE_802_15_4) )
  343. {
  344. error_AT=2;
  345. gen_data(set_api_options,api_options);
  346. gen_checksum(set_api_options);
  347. error=gen_send(set_api_options);
  348. }
  349. else
  350. {
  351. error_AT=-1;
  352. error=-1;
  353. }
  354. return error;
  355. }
  356. /*
  357. Function: Set the network identifier
  358. Returns: Integer that determines if there has been any error
  359. error=2 --> The command has not been executed
  360. error=1 --> There has been an error while executing the command
  361. error=0 --> The command has been executed with no errors
  362. Parameters:
  363. PANID: Array of integers than contains the 16b or 64b PAN ID
  364. Values: Stores in global "PAN_ID" variable the recent set PAN ID value
  365. */
  366. uint8_t WaspXBeeCore::setPAN(uint8_t* PANID)
  367. {
  368. int8_t error=2;
  369. if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  370. {
  371. error_AT=2;
  372. gen_data(set_pan,PANID);
  373. gen_checksum(set_pan);
  374. error=gen_send(set_pan);
  375. }
  376. if(protocol==ZIGBEE)
  377. {
  378. error_AT=2;
  379. gen_data(set_pan_zb,PANID);
  380. gen_checksum(set_pan_zb);
  381. error=gen_send(set_pan_zb);
  382. }
  383. if(!error)
  384. {
  385. if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  386. {
  387. for(it=0;it<2;it++)
  388. {
  389. PAN_ID[it]=PANID[it];
  390. }
  391. }
  392. if(protocol==ZIGBEE)
  393. {
  394. for(it=0;it<8;it++)
  395. {
  396. PAN_ID[it]=PANID[it];
  397. }
  398. }
  399. }
  400. return error;
  401. }
  402. /*
  403. Function: Get Network ID
  404. Returns: Integer that determines if there has been any error
  405. error=2 --> The command has not been executed
  406. error=1 --> There has been an error while executing the command
  407. error=0 --> The command has been executed with no errors
  408. Values: Stores in global "error" variable any error happened while execution
  409. Stores in global "PAN_ID" variable the 16b or 64b network PAN ID
  410. */
  411. uint8_t WaspXBeeCore::getPAN()
  412. {
  413. int8_t error=2;
  414. error_AT=2;
  415. gen_data(get_pan);
  416. if( protocol==ZIGBEE ) error=gen_send(get_pan);
  417. else error=gen_send(get_pan);
  418. if(!error)
  419. {
  420. if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  421. {
  422. for(it=0;it<2;it++)
  423. {
  424. PAN_ID[it]=data[it];
  425. delay(20);
  426. }
  427. }
  428. if(protocol==ZIGBEE)
  429. {
  430. for(it=0;it<8;it++)
  431. {
  432. PAN_ID[it]=data[it];
  433. delay(20);
  434. }
  435. }
  436. }
  437. return error;
  438. }
  439. /*
  440. Function: Set the module to the sleep mode specified.
  441. Returns: Integer that determines if there has been any error
  442. error=2 --> The command has not been executed
  443. error=1 --> There has been an error while executing the command
  444. error=0 --> The command has been executed with no errors
  445. Values: Stores the returned value by SM command in the global "sleepMode" variable
  446. Parameters:
  447. sleep: Defines the sleep mode to use by the XBee (0-5)
  448. */
  449. uint8_t WaspXBeeCore::setSleepMode(uint8_t sleep)
  450. {
  451. int8_t error=2;
  452. error_AT=2;
  453. gen_data(set_sleep_mode_xbee,sleep);
  454. gen_checksum(set_sleep_mode_xbee);
  455. error=gen_send(set_sleep_mode_xbee);
  456. if(!error)
  457. {
  458. sleepMode=sleep;
  459. }
  460. return error;
  461. }
  462. /*
  463. Function: Get the XBee mode
  464. Returns: Integer that determines if there has been any error
  465. error=2 --> The command has not been executed
  466. error=1 --> There has been an error while executing the command
  467. error=0 --> The command has been executed with no errors
  468. Values: Stores the XBee mode in the global "sleepMode" variable
  469. */
  470. uint8_t WaspXBeeCore::getSleepMode()
  471. {
  472. int8_t error=2;
  473. error_AT=2;
  474. gen_data(get_sleep_mode_xbee);
  475. error=gen_send(get_sleep_mode_xbee);
  476. if(error==0)
  477. {
  478. sleepMode=data[0];
  479. }
  480. return error;
  481. }
  482. /*
  483. Function: Set the time the module has to be idle before start sleeping
  484. Returns: Integer that determines if there has been any error
  485. error=2 --> The command has not been executed
  486. error=1 --> There has been an error while executing the command
  487. error=0 --> The command has been executed with no errors
  488. Values: Change the ST parameter in XBee module
  489. Stores in global "awakeTime" the value of this time
  490. Parameters:
  491. awake: Array of integers that specifies the time to be awake before sleep
  492. */
  493. uint8_t WaspXBeeCore::setAwakeTime(uint8_t* awake)
  494. {
  495. int8_t error=2;
  496. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
  497. {
  498. error_AT=2;
  499. gen_data(set_awake_time,awake);
  500. gen_checksum(set_awake_time);
  501. error=gen_send(set_awake_time);
  502. }
  503. if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
  504. {
  505. error_AT=2;
  506. gen_data(set_awake_time_DM,awake);
  507. gen_checksum(set_awake_time_DM);
  508. error=gen_send(set_awake_time_DM);
  509. }
  510. if(!error)
  511. {
  512. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
  513. {
  514. awakeTime[0]=awake[0];
  515. awakeTime[1]=awake[1];
  516. }
  517. if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
  518. {
  519. awakeTime[0]=awake[0];
  520. awakeTime[1]=awake[1];
  521. awakeTime[2]=awake[2];
  522. }
  523. }
  524. return error;
  525. }
  526. /*
  527. Function: Set the cyclic sleeping time of the node
  528. Returns: Integer that determines if there has been any error
  529. error=2 --> The command has not been executed
  530. error=1 --> There has been an error while executing the command
  531. error=0 --> The command has been executed with no errors
  532. Values: Change the SP parameter in the XBee module
  533. Stores in global "sleepTime" the value of this time
  534. Parameters:
  535. sleep: Array of Integers that specifies the amount of time the module spends sleeping
  536. */
  537. uint8_t WaspXBeeCore::setSleepTime(uint8_t* sleep)
  538. {
  539. int8_t error=2;
  540. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
  541. {
  542. error_AT=2;
  543. gen_data(set_sleep_time,sleep);
  544. gen_checksum(set_sleep_time);
  545. error=gen_send(set_sleep_time);
  546. }
  547. if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
  548. {
  549. error_AT=2;
  550. gen_data(set_sleep_time_DM,sleep);
  551. gen_checksum(set_sleep_time_DM);
  552. error=gen_send(set_sleep_time_DM);
  553. }
  554. if(!error)
  555. {
  556. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_868) )
  557. {
  558. sleepTime[0]=sleep[0];
  559. sleepTime[1]=sleep[1];
  560. }
  561. if( (protocol==DIGIMESH) || (protocol==XBEE_900) )
  562. {
  563. sleepTime[0]=sleep[0];
  564. sleepTime[1]=sleep[1];
  565. sleepTime[2]=sleep[2];
  566. }
  567. }
  568. return error;
  569. }
  570. /*
  571. Function: Set the channel frequency where the module is going to work
  572. Returns: Integer that determines if there has been any error
  573. error=2 --> The command has not been executed
  574. error=1 --> There has been an error while executing the command
  575. error=0 --> The command has been executed with no errors
  576. error=-1 --> Forbidden command for this protocol
  577. Values: Stores the selected channel in the global "channel" variable
  578. Parameters:
  579. _channel: Channel used to transmit (0x0B-0x1A)
  580. */
  581. uint8_t WaspXBeeCore::setChannel(uint8_t _channel)
  582. {
  583. int8_t error=2;
  584. if( (protocol==XBEE_802_15_4) || (protocol==DIGIMESH) || (protocol==XBEE_900) )
  585. {
  586. error_AT=2;
  587. gen_data(set_channel,_channel);
  588. gen_checksum(set_channel);
  589. error=gen_send(set_channel);
  590. }
  591. else
  592. {
  593. error_AT=-1;
  594. error=-1;
  595. }
  596. if(!error)
  597. {
  598. channel=_channel;
  599. }
  600. return error;
  601. }
  602. /*
  603. Function: Get the actual frequency channel
  604. Returns: Integer that determines if there has been any error
  605. error=2 --> The command has not been executed
  606. error=1 --> There has been an error while executing the command
  607. error=0 --> The command has been executed with no errors
  608. Values: Stores the frequency channel in the global "channel" variable
  609. */
  610. uint8_t WaspXBeeCore::getChannel()
  611. {
  612. int8_t error=2;
  613. error_AT=2;
  614. gen_data(get_channel);
  615. error=gen_send(get_channel);
  616. if(!error)
  617. {
  618. channel=data[0];
  619. }
  620. return error;
  621. }
  622. /*
  623. Function: Set the Node Indentifier
  624. Returns: Integer that determines if there has been any error
  625. error=2 --> The command has not been executed
  626. error=1 --> There has been an error while executing the command
  627. error=0 --> The command has been executed with no errors
  628. Values: Change the NI to the selected in the function
  629. The NI must be a 20 character max string
  630. Stores the given NI in the global "nodeID" variable
  631. Parameters:
  632. node: string that specifies the node indentifier
  633. */
  634. uint8_t WaspXBeeCore::setNodeIdentifier(char* node)
  635. {
  636. uint8_t* NI = (uint8_t*) calloc(30,sizeof(uint8_t)); //{0x7E, 0x00, 0x00, 0x08, 0x52, 0x4E, 0x49, 0x02};
  637. NI[0]=0x7E;
  638. NI[1]=0x00;
  639. NI[3]=0x08;
  640. NI[4]=0x52;
  641. NI[5]=0x4E;
  642. NI[6]=0x49;
  643. uint8_t num_NI;
  644. int8_t error=2;
  645. uint8_t* ByteIN = (uint8_t*) calloc(20,sizeof(uint8_t));
  646. uint8_t counter=0;
  647. uint8_t counter3=0;
  648. uint8_t est=1;
  649. uint8_t frame_ID=NI[4];
  650. uint8_t end=0;
  651. uint16_t interval=WAIT_TIME;
  652. uint8_t checksum=0;
  653. uint8_t numberBytes=9;
  654. uint8_t status=0;
  655. uint8_t undesired=0;
  656. it=0;
  657. error_AT=2;
  658. while( (node[it]!='\0') )
  659. {
  660. NI[it+7]=uint8_t(node[it]);
  661. it++;
  662. }
  663. NI[2]=4+it;
  664. for(it=3;it<(7+(NI[2]-4));it++)
  665. {
  666. checksum=checksum+NI[it];
  667. }
  668. while( (checksum>255))
  669. {
  670. checksum=checksum-256;
  671. }
  672. checksum=255-checksum;
  673. NI[7+NI[2]-4]=checksum;
  674. while(counter<(8+NI[2]-4))
  675. {
  676. XBee.print(NI[counter], BYTE);
  677. counter++;
  678. }
  679. counter=0;
  680. clearCommand();
  681. command[5]=0x4E;
  682. command[6]=0x49;
  683. error=parse_message(command);
  684. if(error==0)
  685. {
  686. for(it=0;it<NI[2]-4;it++)
  687. {
  688. nodeID[it]=node[it];
  689. }
  690. }
  691. free(NI);
  692. free(ByteIN);
  693. NI=NULL;
  694. ByteIN=NULL;
  695. return error;
  696. }
  697. /*
  698. Function: Get the Node Identifier
  699. Returns: Integer that determines if there has been any error
  700. error=2 --> The command has not been executed
  701. error=1 --> There has been an error while executing the command
  702. error=0 --> The command has been executed with no errors
  703. Values: Stores the NI in the global "nodeID" variable
  704. */
  705. uint8_t WaspXBeeCore::getNodeIdentifier()
  706. {
  707. int8_t error=2;
  708. error_AT=2;
  709. gen_data(get_NI);
  710. error=gen_send(get_NI);
  711. if(!error)
  712. {
  713. for(it=0;it<data_length;it++)
  714. {
  715. nodeID[it]=char(data[it]);
  716. }
  717. }
  718. return error;
  719. }
  720. /*
  721. Function: Scans for brothers in the same channel and same PAN ID
  722. Returns: Integer that determines if there has been any error
  723. error=2 --> The command has not been executed
  724. error=1 --> There has been an error while executing the command
  725. error=0 --> The command has been executed with no errors
  726. Values: Stores given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable
  727. Stores in global "totalScannedBrothers" the number of found brothers
  728. */
  729. uint8_t WaspXBeeCore::scanNetwork()
  730. {
  731. uint8_t error=2;
  732. error_AT=2;
  733. totalScannedBrothers=0;
  734. gen_data(scan_network);
  735. error=gen_send(scan_network);
  736. return error;
  737. }
  738. /*
  739. Function: Scans for brothers in the same channel and same PAN ID
  740. Returns: Integer that determines if there has been any error
  741. error=2 --> The command has not been executed
  742. error=1 --> There has been an error while executing the command
  743. error=0 --> The command has been executed with no errors
  744. Values: Stores given info (SH,SL,MY,RSSI,NI) in global array "scannedBrothers" variable
  745. Stores in global "totalScannedBrothers" the number of founded brothers
  746. Parameters:
  747. node: 20-byte max string containing NI of the node to search
  748. */
  749. uint8_t WaspXBeeCore::scanNetwork(char* node)
  750. {
  751. uint8_t* ND = (uint8_t*) calloc(30,sizeof(uint8_t)); //{0x7E, 0x00, 0x04, 0x08, 0x52, 0x4E, 0x44, 0x13};
  752. ND[0]=0x7E;
  753. ND[1]=0x00;
  754. ND[3]=0x08;
  755. ND[4]=0x52;
  756. ND[5]=0x4E;
  757. ND[6]=0x44;
  758. uint8_t num_ND;
  759. int8_t error=2;
  760. uint8_t* ByteIN = (uint8_t*) calloc(20,sizeof(uint8_t));
  761. uint8_t cont=1;
  762. uint8_t cont2=0;
  763. uint8_t counter=0;
  764. uint8_t counter3=0;
  765. uint8_t est=1;
  766. uint8_t frame_ID=ND[4];
  767. uint8_t end=0;
  768. uint16_t interval=WAIT_TIME2;
  769. uint16_t length=0;
  770. uint16_t aux=0;
  771. uint8_t existsNI=0;
  772. uint8_t length_NI=0;
  773. uint16_t checksum=0;
  774. uint8_t finish=0;
  775. error_AT=2;
  776. totalScannedBrothers=0;
  777. if( (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  778. {
  779. interval=14000;
  780. }
  781. it=0;
  782. while( (node[it]!='\0') )
  783. {
  784. ND[it+7]=uint8_t(node[it]);
  785. it++;
  786. }
  787. ND[2]=4+it;
  788. for(it=3;it<(7+(ND[2]-4));it++)
  789. {
  790. checksum=checksum+ND[it];
  791. }
  792. while( (checksum>255))
  793. {
  794. checksum=checksum-256;
  795. }
  796. checksum=255-checksum;
  797. ND[7+ND[2]-4]=checksum;
  798. while(counter<(8+ND[2]-4))
  799. {
  800. XBee.print(ND[counter], BYTE);
  801. counter++;
  802. }
  803. counter=0;
  804. clearCommand();
  805. command[5]=ND[5];
  806. command[6]=ND[6];
  807. error=parse_message(command);
  808. free(ND);
  809. free(ByteIN);
  810. ND=NULL;
  811. ByteIN=NULL;
  812. return error;
  813. }
  814. /*
  815. Function: Defines the amount of time the scanNetwork() function is scanning
  816. Returns: Integer that determines if there has been any error
  817. error=2 --> The command has not been executed
  818. error=1 --> There has been an error while executing the command
  819. error=0 --> The command has been executed with no errors
  820. Values: Changes the NT command
  821. Stores in global "scanTime" variable the recent set time
  822. Parameters:
  823. time: amount of time ND is scanning for brothers (0x01-0xFC)
  824. */
  825. uint8_t WaspXBeeCore::setScanningTime(uint8_t* time)
  826. {
  827. int8_t error=2;
  828. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_900) )
  829. {
  830. error_AT=2;
  831. gen_data(set_scanning_time,time);
  832. gen_checksum(set_scanning_time);
  833. error=gen_send(set_scanning_time);
  834. }
  835. if( (protocol==DIGIMESH) || (protocol==XBEE_868) )
  836. {
  837. error_AT=2;
  838. gen_data(set_scanning_time_DM,time);
  839. gen_checksum(set_scanning_time_DM);
  840. error=gen_send(set_scanning_time_DM);
  841. }
  842. if(!error)
  843. {
  844. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) || (protocol==XBEE_900) )
  845. {
  846. scanTime[0]=time[0];
  847. }
  848. if( (protocol==DIGIMESH) || (protocol==XBEE_868) )
  849. {
  850. scanTime[0]=time[0];
  851. scanTime[1]=time[1];
  852. }
  853. }
  854. return error;
  855. }
  856. /*
  857. Function: Get the Scanning Time
  858. Returns: Integer that determines if there has been any error
  859. error=2 --> The command has not been executed
  860. error=1 --> There has been an error while executing the command
  861. error=0 --> The command has been executed with no errors
  862. Values: Stores in global "error" variable any error happened while execution
  863. Stores in global "scanTime" the value of scanning time
  864. */
  865. uint8_t WaspXBeeCore::getScanningTime()
  866. {
  867. int8_t error=2;
  868. error_AT=2;
  869. gen_data(get_scanning_time);
  870. if( (protocol==DIGIMESH) || (protocol==XBEE_868) || (protocol==ZIGBEE) || (protocol==XBEE_900) ) error=gen_send(get_scanning_time);
  871. else error=gen_send(get_scanning_time);
  872. if(!error)
  873. {
  874. if( (protocol==XBEE_802_15_4) )
  875. {
  876. scanTime[0]=data[0];
  877. }
  878. if( (protocol==ZIGBEE) || (protocol==XBEE_900) )
  879. {
  880. scanTime[0]=data[1];
  881. }
  882. if( (protocol==DIGIMESH) || (protocol==XBEE_868) )
  883. {
  884. scanTime[0]=data[0];
  885. scanTime[1]=data[1];
  886. }
  887. }
  888. return error;
  889. }
  890. /*
  891. Function: Set the options value for the network discovery command
  892. Returns: Integer that determines if there has been any error
  893. error=2 --> The command has not been executed
  894. error=1 --> There has been an error while executing the command
  895. error=0 --> The command has been executed with no errors
  896. Values: Change the NO command
  897. Parameters:
  898. options: chosen option (0x00-0x03)
  899. */
  900. uint8_t WaspXBeeCore::setDiscoveryOptions(uint8_t options)
  901. {
  902. int8_t error=2;
  903. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  904. {
  905. error_AT=2;
  906. gen_data(set_discov_options,options);
  907. gen_checksum(set_discov_options);
  908. error=gen_send(set_discov_options);
  909. }
  910. else
  911. {
  912. error_AT=-1;
  913. error=-1;
  914. }
  915. if(!error)
  916. {
  917. discoveryOptions=options;
  918. }
  919. return error;
  920. }
  921. /*
  922. Function: Get the options value for the network discovery command
  923. Returns: Integer that determines if there has been any error
  924. error=2 --> The command has not been executed
  925. error=1 --> There has been an error while executing the command
  926. error=0 --> The command has been executed with no errors
  927. Values: Executes the NO command. Stores in global "discoveryOptions" variable the options
  928. */
  929. uint8_t WaspXBeeCore::getDiscoveryOptions()
  930. {
  931. int8_t error=2;
  932. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  933. {
  934. error_AT=2;
  935. gen_data(get_discov_options);
  936. error=gen_send(get_discov_options);
  937. }
  938. else
  939. {
  940. error_AT=-1;
  941. error=-1;
  942. }
  943. if(error==0)
  944. {
  945. discoveryOptions=data[0];
  946. }
  947. return error;
  948. }
  949. /*
  950. Function: Performs a quick search.
  951. 802.15.4 : It keeps in DL the MY of the looked up NI brother
  952. ZIGBEE : Stores in global "paquete" naD,macDH,macDL from the searched device
  953. DIGIMESH: Stores in global "paquete" macDH,macDL from the searched device
  954. Returns: Integer that determines if there has been any error
  955. error=2 --> The command has not been executed
  956. error=1 --> There has been an error while executing the command
  957. error=0 --> The command has been executed with no errors
  958. Values: Executes DN command.
  959. Parameters:
  960. node: string that specifies the NI that identifies the searched brother
  961. length: length of that NI (0-20)
  962. */
  963. uint8_t WaspXBeeCore::nodeSearch(char* node, struct packetXBee* paq)
  964. {
  965. uint8_t* DN = (uint8_t*) calloc(30,sizeof(uint8_t)); //{0x7E, 0x00, 0x00, 0x08, 0x52, 0x44, 0x4E, 0xE3};
  966. DN[0]=0x7E;
  967. DN[1]=0x00;
  968. DN[3]=0x08;
  969. DN[4]=0x52;
  970. DN[5]=0x44;
  971. DN[6]=0x4E;
  972. uint8_t num_DN;
  973. int8_t error=2;
  974. uint8_t* ByteIN = (uint8_t*) calloc(25,sizeof(uint8_t));
  975. uint8_t counter=0;
  976. uint8_t counter3=0;
  977. uint8_t est=1;
  978. uint8_t frame_ID=DN[4];
  979. uint8_t end=0;
  980. uint16_t interval=2000;
  981. uint8_t checksum=0;
  982. uint8_t status=0;
  983. uint8_t undesired=0;
  984. error_AT=2;
  985. if(protocol==DIGIMESH)
  986. {
  987. interval=14000;
  988. }
  989. it=0;
  990. while( (node[it]!='\0') )
  991. {
  992. DN[it+7]=uint8_t(node[it]);
  993. it++;
  994. }
  995. DN[2]=4+it;
  996. for(it=3;it<(7+(DN[2]-4));it++)
  997. {
  998. checksum=checksum+DN[it];
  999. }
  1000. while( (checksum>255))
  1001. {
  1002. checksum=checksum-256;
  1003. }
  1004. checksum=255-checksum;
  1005. DN[7+DN[2]-4]=checksum;
  1006. while(counter<(8+DN[2]-4))
  1007. {
  1008. XBee.print(DN[counter], BYTE);
  1009. counter++;
  1010. }
  1011. counter=0;
  1012. clearCommand();
  1013. command[5]=0x44;
  1014. command[6]=0x4E;
  1015. error=parse_message(command);
  1016. if(error==0)
  1017. {
  1018. if( (protocol==ZIGBEE) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  1019. {
  1020. for(it=0;it<2;it++)
  1021. {
  1022. paq->naD[it]=data[it];
  1023. }
  1024. for(it=0;it<4;it++)
  1025. {
  1026. paq->macDH[it]=data[it+2];
  1027. }
  1028. for(it=0;it<4;it++)
  1029. {
  1030. paq->macDL[it]=data[it+6];
  1031. }
  1032. }
  1033. if(protocol==DIGIMESH)
  1034. {
  1035. for(it=0;it<4;it++)
  1036. {
  1037. paq->macDH[it]=data[it];
  1038. }
  1039. for(it=0;it<4;it++)
  1040. {
  1041. paq->macDL[it]=data[it+4];
  1042. }
  1043. }
  1044. }
  1045. free(DN);
  1046. free(ByteIN);
  1047. DN=NULL;
  1048. ByteIN=NULL;
  1049. return error;
  1050. }
  1051. /*
  1052. Function: Write the current parameters to a non volatil memory
  1053. Returns: Integer that determines if there has been any error
  1054. error=2 --> The command has not been executed
  1055. error=1 --> There has been an error while executing the command
  1056. error=0 --> The command has been executed with no errors
  1057. Values: Executes the WR command
  1058. */
  1059. uint8_t WaspXBeeCore::writeValues()
  1060. {
  1061. int8_t error=2;
  1062. error_AT=2;
  1063. gen_data(write_values);
  1064. error=gen_send(write_values);
  1065. return error;
  1066. }
  1067. /*
  1068. Function: Specifies the list of channels to scan when performing an energy scan
  1069. Returns: Integer that determines if there has been any error
  1070. error=2 --> The command has not been executed
  1071. error=1 --> There has been an error while executing the command
  1072. error=0 --> The command has been executed with no errors
  1073. error=-1 --> Forbidden command for this protocol
  1074. Values: Change the SC command. Stores in global "scanChannels" variable the list of channels
  1075. Parameters:
  1076. channel_H: higher byte of list of channels (0x00-0xFF)
  1077. channel_L: lower byte of list of channels (0x00-0xFF
  1078. */
  1079. uint8_t WaspXBeeCore::setScanningChannels(uint8_t channel_H, uint8_t channel_L)
  1080. {
  1081. int8_t error=2;
  1082. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  1083. {
  1084. error_AT=2;
  1085. gen_data(set_scanning_channel,channel_H,channel_L);
  1086. gen_checksum(set_scanning_channel);
  1087. error=gen_send(set_scanning_channel);
  1088. }
  1089. else
  1090. {
  1091. error_AT=-1;
  1092. error=-1;
  1093. }
  1094. if(error==0)
  1095. {
  1096. scanChannels[0]=channel_H;
  1097. scanChannels[1]=channel_L;
  1098. }
  1099. return error;
  1100. }
  1101. /*
  1102. Function: Get the list of channels to scan when performing an energy scan
  1103. Returns: Integer that determines if there has been any error
  1104. error=2 --> The command has not been executed
  1105. error=1 --> There has been an error while executing the command
  1106. error=0 --> The command has been executed with no errors
  1107. error=-1 --> Forbidden command for this protocol
  1108. Values: Stores in global "scanChannels" variable the scanning channel list
  1109. */
  1110. uint8_t WaspXBeeCore::getScanningChannels()
  1111. {
  1112. int8_t error=2;
  1113. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  1114. {
  1115. error_AT=2;
  1116. gen_data(get_scanning_channel);
  1117. error=gen_send(get_scanning_channel);
  1118. }
  1119. else
  1120. {
  1121. error_AT=-1;
  1122. error=-1;
  1123. }
  1124. if(error==0)
  1125. {
  1126. for(it=0;it<2;it++)
  1127. {
  1128. scanChannels[it]=data[it];
  1129. }
  1130. }
  1131. return error;
  1132. }
  1133. /*
  1134. Function: It sets the time the energy scan will be performed
  1135. Returns: Integer that determines if there has been any error
  1136. error=2 --> The command has not been executed
  1137. error=1 --> There has been an error while executing the command
  1138. error=0 --> The command has been executed with no errors
  1139. error=-1 --> Forbidden command for this protocol
  1140. Values: Change the ED command. Stores in global "energyChannel" variable the energy in each channel
  1141. Parameters:
  1142. duration: amount of time that the energy scan will be performed (0-6)
  1143. */
  1144. uint8_t WaspXBeeCore::setDurationEnergyChannels(uint8_t duration)
  1145. {
  1146. int8_t error=2;
  1147. if( (protocol==XBEE_802_15_4) )
  1148. {
  1149. error_AT=2;
  1150. gen_data(set_duration_energy,duration);
  1151. gen_checksum(set_duration_energy);
  1152. error=gen_send(set_duration_energy);
  1153. }
  1154. else if( (protocol==ZIGBEE) )
  1155. {
  1156. error_AT=2;
  1157. gen_data(set_duration_energy_ZB,duration);
  1158. gen_checksum(set_duration_energy_ZB);
  1159. error=gen_send(set_duration_energy_ZB);
  1160. }
  1161. else
  1162. {
  1163. error_AT=-1;
  1164. error=-1;
  1165. }
  1166. if(error==0)
  1167. {
  1168. if(protocol==XBEE_802_15_4)
  1169. {
  1170. for(it=0;it<data_length;it++)
  1171. {
  1172. energyChannel[it]=data[it];
  1173. }
  1174. }
  1175. if(protocol==ZIGBEE)
  1176. {
  1177. timeEnergyChannel=data[0];
  1178. }
  1179. }
  1180. return error;
  1181. }
  1182. /*
  1183. Function: It gets the time the energy scan will be performed
  1184. Returns: Integer that determines if there has been any error
  1185. error=2 --> The command has not been executed
  1186. error=1 --> There has been an error while executing the command
  1187. error=0 --> The command has been executed with no errors
  1188. error=-1 --> Forbidden command for this protocol
  1189. Values: Change the SD command. Stores in global "timeEnergyChannel" variable the time the energy
  1190. scan will be performed
  1191. */
  1192. uint8_t WaspXBeeCore::getDurationEnergyChannels()
  1193. {
  1194. int8_t error=2;
  1195. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  1196. {
  1197. error_AT=2;
  1198. gen_data(get_duration_energy);
  1199. error=gen_send(get_duration_energy);
  1200. }
  1201. else
  1202. {
  1203. error_AT=-1;
  1204. error=-1;
  1205. }
  1206. if(!error)
  1207. {
  1208. timeEnergyChannel=data[0];
  1209. }
  1210. return error;
  1211. }
  1212. /*
  1213. Function: Sets the encryption key to be used in the 128b AES algorithm
  1214. Returns: Integer that determines if there has been any error
  1215. error=2 --> The command has not been executed
  1216. error=1 --> There has been an error while executing the command
  1217. error=0 --> The command has been executed with no errors
  1218. Values: Change the KY command. Stores in global "linkKey" variable the key has been set
  1219. Parameters:
  1220. key: 16 byte array of chars that specifies the 128b AES key
  1221. */
  1222. uint8_t WaspXBeeCore::setLinkKey(char* key)
  1223. {
  1224. int8_t error=2;
  1225. error_AT=2;
  1226. gen_data(set_link_key,key);
  1227. gen_checksum(set_link_key);
  1228. error=gen_send(set_link_key);
  1229. if(!error)
  1230. {
  1231. for(it=0;it<16;it++)
  1232. {
  1233. linkKey[it]=char(key[it]);
  1234. }
  1235. }
  1236. return error;
  1237. }
  1238. /*
  1239. Function: Sets the encryption mode on/off
  1240. Returns: Integer that determines if there has been any error
  1241. error=2 --> The command has not been executed
  1242. error=1 --> There has been an error while executing the command
  1243. error=0 --> The command has been executed with no errors
  1244. Values: Change the EE command. Stores in global "encryptMode" variable the encryption mode
  1245. Parameters:
  1246. mode: on/off the encryption mode (1/0)
  1247. */
  1248. uint8_t WaspXBeeCore::encryptionMode(uint8_t mode)
  1249. {
  1250. int8_t error=2;
  1251. error_AT=2;
  1252. gen_data(set_encryption,mode);
  1253. gen_checksum(set_encryption);
  1254. error=gen_send(set_encryption);
  1255. if(!error)
  1256. {
  1257. encryptMode=mode;
  1258. }
  1259. return error;
  1260. }
  1261. /*
  1262. Function: Select the power level at which the RF module transmits
  1263. Returns: Integer that determines if there has been any error
  1264. error=2 --> The command has not been executed
  1265. error=1 --> There has been an error while executing the command
  1266. error=0 --> The command has been executed with no errors
  1267. Values: Change the PL command. Stores in global "powerLevel" the power level at which RF tx
  1268. Parameters:
  1269. value: power level of transmission (0-4)
  1270. */
  1271. uint8_t WaspXBeeCore::setPowerLevel(uint8_t value)
  1272. {
  1273. int8_t error=2;
  1274. if(protocol!=XBEE_900)
  1275. {
  1276. error_AT=2;
  1277. gen_data(set_power_level,value);
  1278. gen_checksum(set_power_level);
  1279. error=gen_send(set_power_level);
  1280. }
  1281. if(!error)
  1282. {
  1283. powerLevel=value;
  1284. }
  1285. return error;
  1286. }
  1287. /*
  1288. Function: Get the Received Signal Strength Indicator of the last received packet
  1289. Returns: Returns: Integer that determines if there has been any error
  1290. error=2 --> The command has not been executed
  1291. error=1 --> There has been an error while executing the command
  1292. error=0 --> The command has been executed with no errors
  1293. Values: Stores in global "valueRSSI" variable the RSSI value of last received packet
  1294. */
  1295. uint8_t WaspXBeeCore::getRSSI()
  1296. {
  1297. int8_t error=2;
  1298. uint8_t* ByteIN = (uint8_t*) calloc(40,sizeof(uint8_t));
  1299. uint8_t i=0;
  1300. if( (protocol == XBEE_802_15_4 ) || (protocol==ZIGBEE) )
  1301. {
  1302. error_AT=2;
  1303. gen_data(get_RSSI);
  1304. error=gen_send(get_RSSI);
  1305. }
  1306. else if( (protocol== DIGIMESH) || (protocol==XBEE_868) || (protocol==XBEE_900) )
  1307. {
  1308. delay(2000);
  1309. XBee.print("+++");
  1310. delay(2000);
  1311. XBee.flush();
  1312. XBee.println("atdb");
  1313. delay(1000);
  1314. error_AT=2;
  1315. while(XBee.available()>0)
  1316. {
  1317. ByteIN[i]=XBee.read();
  1318. error=0;
  1319. i++;
  1320. error_AT=0;
  1321. }
  1322. i=0;
  1323. XBee.println("atcn");
  1324. delay(1000);
  1325. valueRSSI[0]=Utils.str2hex(ByteIN);
  1326. }
  1327. if(error==0)
  1328. {
  1329. if( (protocol==XBEE_802_15_4) || (protocol==ZIGBEE) )
  1330. {
  1331. valueRSSI[0]=data[0];
  1332. }
  1333. }
  1334. free(ByteIN);
  1335. ByteIN=NULL;
  1336. return error;
  1337. }
  1338. /*
  1339. Function: Get the Harware Version
  1340. Returns: Returns: Integer that determines if there has been any error
  1341. error=2 --> The command has not been executed
  1342. error=1 --> There has been an error while executing the command
  1343. error=0 --> The command has been executed with no errors
  1344. Values: Stores in global "hardVersion" variable the Hardware Version
  1345. */
  1346. uint8_t WaspXBeeCore::getHardVersion()
  1347. {
  1348. int8_t error=2;
  1349. error_AT=2;
  1350. gen_data(get_hard_version);
  1351. error=gen_send(get_hard_version);
  1352. if(!error)
  1353. {
  1354. hardVersion[0]=data[0];
  1355. hardVersion[1]=data[1];
  1356. }
  1357. return error;
  1358. }
  1359. /*
  1360. Function: Get the version of the firmware
  1361. Returns: Integer that determines if there has been any error
  1362. error=2 --> The command has not been executed
  1363. error=1 --> There has been an error while executing the command
  1364. error=0 --> The command has been executed with no errors
  1365. Values: Stores in global "softVersion" variable the firmware version
  1366. */
  1367. uint8_t WaspXBeeCore::getSoftVersion()
  1368. {
  1369. int8_t error=2;
  1370. error_AT=2;
  1371. gen_data(get_soft_version);
  1372. error=gen_send(get_soft_version);
  1373. if(error==0)
  1374. {
  1375. softVersion[0]=data[0];
  1376. softVersion[1]=data[1];
  1377. }
  1378. return error;
  1379. }
  1380. /*
  1381. Function: Set the RSSI time
  1382. Returns: Integer that determines if there has been any error
  1383. error=2 --> The command has not been executed
  1384. error=1 --> There has been an error while executing the command
  1385. error=0 --> The command has been executed with no errors
  1386. Values: Change the RP command. Stores in global "timeRSSI" variable the RSSI time
  1387. Parameters:
  1388. time: amount of time to do the pwm (0x00-0xFF)
  1389. */
  1390. uint8_t WaspXBeeCore::setRSSItime(uint8_t time)
  1391. {
  1392. int8_t error=2;
  1393. error_AT=2;
  1394. gen_data(set_RSSI_time,time);
  1395. gen_checksum(set_RSSI_time);
  1396. error=gen_send(set_RSSI_time);
  1397. if(!error)
  1398. {
  1399. timeRSSI=time;
  1400. }
  1401. return error;
  1402. }
  1403. /*
  1404. Function: Get the RSSI time
  1405. Returns: Integer that determines if there has been any error
  1406. error=2 --> The command has not been executed
  1407. error=1 --> There has been an error while executing the command
  1408. error=0 --> The command has been executed with no errors
  1409. Values: Stores in global "timeRSSI" variable the RSSI time
  1410. */
  1411. uint8_t WaspXBeeCore::getRSSItime()
  1412. {
  1413. int8_t error=2;
  1414. error_AT=2;
  1415. gen_data(get_RSSI_time);
  1416. error=gen_send(get_RSSI_time);
  1417. if(!error)
  1418. {
  1419. timeRSSI=data[0];
  1420. }
  1421. return error;
  1422. }
  1423. /*
  1424. Function: Immediately applies new settings without exiting command mode
  1425. Returns: Integer that determines if there has been any error
  1426. error=2 --> The command has not been executed
  1427. error=1 --> There has been an error while executing the command
  1428. error=0 --> The command has been executed with no errors
  1429. Values: Executes the AC command
  1430. */
  1431. uint8_t WaspXBeeCore::applyChanges()
  1432. {
  1433. int8_t error=2;
  1434. error_AT=2;
  1435. gen_data(apply_changes);
  1436. error=gen_send(apply_changes);
  1437. return error;
  1438. }
  1439. /*
  1440. Function: Reset the XBee Firmware
  1441. Returns: Integer that determines if there has been any error
  1442. error=2 --> The command has not been executed
  1443. error=1 --> There has been an error while executing the command
  1444. error=0 --> The command has been executed with no errors
  1445. Values: Executes the FR command
  1446. */
  1447. uint8_t WaspXBeeCore::reset()
  1448. {
  1449. int8_t error=2;
  1450. error_AT=2;
  1451. gen_data(reset_xbee);
  1452. error=gen_send(reset_xbee);
  1453. return error;
  1454. }
  1455. /*
  1456. Function: Set the parameteres to the factory defaults
  1457. Returns: Integer that determines if there has been any error
  1458. error=2 --> The command has not been executed
  1459. error=1 --> There has been an error while executing the command
  1460. error=0 --> The command has been executed with no errors
  1461. Values: Executes the RE command
  1462. */
  1463. uint8_t WaspXBeeCore::resetDefaults()
  1464. {
  1465. int8_t error=2;
  1466. error_AT=2;
  1467. gen_data(reset_defaults_xbee);
  1468. error=gen_send(reset_defaults_xbee);
  1469. return error;
  1470. }
  1471. /*
  1472. Function: Configure options for sleep
  1473. Returns: Integer that determines if there has been any error
  1474. error=2 --> The command has not been executed
  1475. error=1 --> There has been an error while executing the command
  1476. error=0 --> The command has been executed with no errors
  1477. error=-1 --> Forbidden command for this protocol
  1478. Values: Change the SO command. Stores in global "sleepOptions" variable the options
  1479. Parameters:
  1480. soption: options for sleep (0x00-0xFF)
  1481. */
  1482. uint8_t WaspXBeeCore::setSleepOptions(uint8_t soption)
  1483. {
  1484. int8_t error=2;
  1485. if( (protocol==ZIGBEE) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  1486. {
  1487. error_AT=2;
  1488. gen_data(set_sleep_options_xbee,soption);
  1489. gen_checksum(set_sleep_options_xbee);
  1490. error=gen_send(set_sleep_options_xbee);
  1491. }
  1492. else
  1493. {
  1494. error_AT=-1;
  1495. error=-1;
  1496. }
  1497. if(!error)
  1498. {
  1499. sleepOptions=soption;
  1500. }
  1501. return error;
  1502. }
  1503. /*
  1504. Function: Reads the options for sleep
  1505. Returns: Integer that determines if there has been any error
  1506. error=2 --> The command has not been executed
  1507. error=1 --> There has been an error while executing the command
  1508. error=0 --> The command has been executed with no errors
  1509. error=-1 --> Forbidden command for this protocol
  1510. Values: Executes the SO command. Stores in global "sleepOptions" variable the options
  1511. */
  1512. uint8_t WaspXBeeCore::getSleepOptions()
  1513. {
  1514. int8_t error=2;
  1515. if( (protocol==ZIGBEE) || (protocol==DIGIMESH) || (protocol==XBEE_900) || (protocol==XBEE_868) )
  1516. {
  1517. error_AT=2;
  1518. gen_data(get_sleep_options_xbee);
  1519. error=gen_send(get_sleep_options_xbee);
  1520. }
  1521. else
  1522. {
  1523. error_AT=-1;
  1524. error=-1;
  1525. }
  1526. if(!error)
  1527. {
  1528. sleepOptions=data[0];
  1529. }
  1530. return error;
  1531. }
  1532. /*
  1533. Function: Transparent function. The user introduces an AT command within a string and the function executes it without knowing its meaning
  1534. Returns: Integer that determines if there has been any error
  1535. error=2 --> The command has not been executed
  1536. error=1 --> There has been an error while executing the command
  1537. error=0 --> The command has been executed with no errors
  1538. Parameters:
  1539. atcommand : String to specify the AT command to execute
  1540. */
  1541. uint8_t WaspXBeeCore::sendCommandAT(char* atcommand)
  1542. {
  1543. uint8_t* AT = (uint8_t*) calloc(30,sizeof(uint8_t));// {0x7E, 0x00, 0x00, 0x08, 0x52, 0x00, 0x00, 0x00};
  1544. AT[0]=0x7E;
  1545. AT[1]=0x00;
  1546. AT[3]=0x08;
  1547. AT[4]=0x52;
  1548. uint8_t num_AT=0;
  1549. int8_t error=2;
  1550. uint8_t it2=0;
  1551. uint8_t* ByteIN = (uint8_t*) calloc(120,sizeof(uint8_t));
  1552. uint8_t counter=0;
  1553. uint8_t counter3=0;
  1554. uint8_t est=1;
  1555. uint8_t frame_ID=AT[4];
  1556. uint8_t end=0;
  1557. uint16_t interval=WAIT_TIME;
  1558. uint8_t checksum=0;
  1559. uint16_t length=0;
  1560. it=0;
  1561. error_AT=2;
  1562. while( atcommand[it2]!='#' )
  1563. {
  1564. if( it>=2 )
  1565. {
  1566. if( atcommand[it2+1]!='#' )
  1567. {
  1568. AT[it+5]=Utils.converter(atcommand[2*(it-1)],atcommand[2*(it-1)+1]);
  1569. it2+=2;
  1570. }
  1571. else
  1572. {
  1573. switch( atcommand[it2] )
  1574. {
  1575. case '0': AT[it+5]=0;
  1576. break;
  1577. case '1': AT[it+5]=1;
  1578. break;
  1579. case '2': AT[it+5]=2;
  1580. break;
  1581. case '3': AT[it+5]=3;
  1582. break;
  1583. case '4': AT[it+5]=4;
  1584. break;
  1585. case '5': AT[it+5]=5;
  1586. break;
  1587. case '6': AT[it+5]=6;
  1588. break;
  1589. case '7': AT[it+5]=7;
  1590. break;
  1591. case '8': AT[it+5]=8;
  1592. break;
  1593. case '9': AT[it+5]=9;
  1594. break;
  1595. case 'A': AT[it+5]='A';
  1596. break;
  1597. case 'B': AT[it+5]='B';
  1598. break;
  1599. case 'C': AT[it+5]='C';
  1600. break;
  1601. case 'D': AT[it+5]='D';
  1602. break;
  1603. case 'E': AT[it+5]='E';
  1604. break;
  1605. case 'F': AT[it+5]='F';
  1606. break;
  1607. }
  1608. it2++;
  1609. }
  1610. }
  1611. else
  1612. {
  1613. AT[it+5]=atcommand[it];
  1614. it2++;
  1615. }
  1616. it++;
  1617. }
  1618. length=it;
  1619. AT[2]=2+length;
  1620. for(it=3;it<(5+length);it++)
  1621. {
  1622. checksum=checksum+AT[it];
  1623. }
  1624. while( (checksum>255))
  1625. {
  1626. checksum=checksum-256;
  1627. }
  1628. checksum=255-checksum;
  1629. AT[5+length]=checksum;
  1630. while(counter<(6+length))
  1631. {
  1632. XBee.print(AT[counter], BYTE);
  1633. counter++;
  1634. }
  1635. counter=0;
  1636. clearCommand();
  1637. command[5]=AT[5];
  1638. command[6]=AT[6];
  1639. data_length=0;
  1640. error=parse_message(command);
  1641. if(error==0)
  1642. {
  1643. if(data_length>0)
  1644. {
  1645. for(it=0;it<data_length;it++)
  1646. {
  1647. commandAT[it]=data[it];
  1648. delay(20);
  1649. }
  1650. }
  1651. else
  1652. {
  1653. commandAT[0]=0x4F;
  1654. commandAT[1]=0x4B;
  1655. }
  1656. }
  1657. free(AT);
  1658. AT=NULL;
  1659. free(ByteIN);
  1660. ByteIN=NULL;
  1661. return error;
  1662. }
  1663. /*
  1664. Function: Connect XBee, activating switch in Waspmote
  1665. Returns: Integer that determines if there has been any error
  1666. error=2 --> The command has not been executed
  1667. error=1 --> There has been an error while executing the command
  1668. error=0 --> The command has been executed with no errors
  1669. */
  1670. uint8_t WaspXBeeCore::ON()
  1671. {
  1672. uint8_t error=2;
  1673. XBee.begin();
  1674. XBee.setMode(XBEE_ON);
  1675. if( protocol== ZIGBEE || protocol==XBEE_868 ) delay(500);
  1676. else delay(50);
  1677. error=0;
  1678. XBee_ON=1;
  1679. return error;
  1680. }
  1681. /*
  1682. Function: disconnects XBee, switching it off and closing the UART
  1683. Returns: Integer that determines if there has been any error
  1684. error=2 --> The command has not been executed
  1685. error=1 --> There has been an error while executing the command
  1686. error=0 --> The command has been executed with no errors
  1687. */
  1688. uint8_t WaspXBeeCore::OFF()
  1689. {
  1690. uint8_t error=2;
  1691. XBee.close();
  1692. XBee.setMode(XBEE_OFF);
  1693. error=0;
  1694. XBee_ON=0;
  1695. return error;
  1696. }
  1697. /*
  1698. Function: Set XBee to sleep, asserting PIN 9
  1699. Returns: Integer that determines if there has been any error
  1700. error=2 --> The command has not been executed
  1701. error=1 --> There has been an error while executing the command
  1702. error=0 --> The command has been executed with no errors
  1703. */
  1704. uint8_t WaspXBeeCore::sleep()
  1705. {
  1706. uint8_t error=2;
  1707. pinMode(XBEE_SLEEP, OUTPUT);
  1708. digitalWrite(XBEE_SLEEP,HIGH);
  1709. XBee.close();
  1710. error=0;
  1711. return error;
  1712. }
  1713. /*
  1714. Function: Wake up XBee, de-asserting PIN 9
  1715. Returns: Integer that determines if there has been any error
  1716. error=2 --> The command has not been executed
  1717. error=1 --> There has been an error while executing the command
  1718. error=0 --> The command has been executed with no errors
  1719. */
  1720. uint8_t WaspXBeeCore::wake()
  1721. {
  1722. uint8_t error=2;
  1723. pinMode(XBEE_SLEEP, OUTPUT);
  1724. digitalWrite(XBEE_SLEEP,LOW);
  1725. XBee.begin();
  1726. delay(50);
  1727. error=0;
  1728. return error;
  1729. }
  1730. /*
  1731. Function: Send a packet from one XBee to another XBee in API mode
  1732. Returns: Integer that determines if there has been any error
  1733. error=2 --> The command has not been executed
  1734. error=1 --> There has been an error while executing the command
  1735. error=0 --> The command has been executed …

Large files files are truncated, but you can click here to view the full file