BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
XBeeFrame.php
Go to the documentation of this file.
1 <?php
2 
11 abstract class _XBeeFrameBase {
12  const DEFAULT_START_BYTE = 0x7e, DEFAULT_FRAME_ID = 0x01,
13  REMOTE_API_ID = 0x17, LOCAL_API_ID = 0x08,
14  QUEUED_API_ID = 0x09, TX_API_ID = 0x10, TX_EXPLICIT_API_ID = 0x11;
15 
18 
23  protected function _XBeeFrameBase() {
26  }
27 
33  protected function _assembleFrame() {
34  $this -> setFrame(
35  $this -> getStartByte() .
36  $this -> _getFramelength($this -> getCmdData()) .
37  $this -> getCmdData() .
38  $this -> _calcChecksum($this -> getCmdData())
39  );
40  //echo 'Assembled: ';print_r($this -> _unpackBytes($this -> getFrame())); //debug
41  }
42 
49  protected function _calcChecksum($data) {
50  $checksum = 0;
51  for ($i = 0; $i < strlen($data); $i++) {
52  $checksum += ord($data[$i]);
53  }
54  $checksum = $checksum & 0xFF;
55  $checksum = 0xFF - $checksum;
56  $checksum = chr($checksum);
57  return $checksum;
58  }
59 
66  protected function _getFramelength($data) {
67  $length = strlen($data);
68  $length = sprintf("%04x", $length);
69  $length = $this -> _packBytes($length);
70  return $length;
71  }
72 
79  protected function _hexstr($hex) {
80  $string = '';
81  for ($i=0; $i < strlen($hex); $i+=2) {
82  $string .= chr(hexdec($hex[$i] . $hex[$i+1]));
83  }
84  return $string;
85  }
86 
93  protected function _strhex($str) {
94  $hex = '';
95  for ($i=0; $i < strlen($str); $i+=2) {
96  $hex .= dechex(ord($str[$i])) . dechex(ord($str[$i+1]));
97  }
98  return $hex;
99  }
100 
107  protected function _packBytes($data) {
108  return pack('H*', $data);
109  }
110 
117  protected function _unpackBytes($data) {
118  return unpack('H*', $data);
119  }
120 
127  public function setFrame($frame) {
128  $this -> frame = $frame;
129  }
130 
136  public function getFrame() {
137  return $this -> frame;
138  }
139 
146  public function setFrameId($frameId) {
147  $this -> frameId = $frameId;
148  }
149 
155  public function getFrameId() {
156  return $this -> frameId;
157  }
158 
164  public function setApiId($apiId) {
165  $this -> apiId = $apiId;
166  }
167 
173  public function getApiId() {
174  return $this -> apiId;
175  }
176 
183  public function setCmdData($cmdData) {
184  $this -> cmdData = $this -> _packBytes($cmdData);
185  }
186 
192  public function getCmdData() {
193  return $this -> cmdData;
194  }
195 
201  public function setStartByte($startByte) {
202  $this -> startByte = $this -> _packBytes($startByte);
203  }
204 
210  public function getStartByte() {
211  return $this -> startByte;
212  }
213 
219  public function setAddress16($address16) {
220  $this->address16 = $address16;
221  }
222 
228  public function getAddress16() {
229  return $this->address16;
230  }
231 
237  public function setAddress64($address64) {
238  $this->address64 = $address64;
239  }
240 
246  public function getAddress64() {
247  return $this->address64;
248  }
249 
255  public function setOptions($options) {
256  $this->options = $options;
257  }
258 
264  public function getOptions() {
265  return $this->options;
266  }
267 
273  public function setCmd($cmd) {
274  $this -> cmd = $cmd;
275  }
276 
282  public function getCmd() {
283  return $this -> cmd;
284  }
285 
291  public function setValue($val) {
292  $this -> val = $val;
293  }
294 
300  public function getValue() {
301  return $this -> val;
302  }
303 }
304 
310 class XBeeFrame extends _XBeeFrameBase {
311  public function XBeeFrame() {
312  parent::_XBeeFrameBase();
313  }
314 
323  public function remoteAtCommand($address16, $cmd, $val, $address64 = '0000000000000000', $options = '02') {
325  $this -> setAddress16($address16);
326  $this -> setAddress64($address64);
327  $this -> setOptions($options);
328  $this -> setCmd($this -> _strhex($cmd));
329  $this -> setValue($val);
330 
331  $this -> setCmdData(
332  $this -> getApiId() .
333  $this -> getFrameId() .
334  $this -> getAddress64() .
335  $this ->getAddress16() .
336  $this -> getOptions() .
337  $this -> getCmd() .
338  $this -> getValue()
339  );
340  $this -> _assembleFrame();
341  }
342 
350  public function localAtCommand($cmd, $val = '') {
352  $this -> setCmd($this ->_strhex($cmd));
353  $this -> setCmdData(
354  $this -> getApiId() .
355  $this -> getFrameId() .
356  $this -> getCmd() .
357  $this -> getValue()
358  );
359  $this -> _assembleFrame();
360  }
361 
365  public function queuedAtCommand() {
367  trigger_error('queued_at not implemented', E_USER_ERROR);
368  }
369 
373  public function txCommand() {
375  trigger_error('tx not implemented', E_USER_ERROR);
376  }
377 
381  public function txExplicityCommand() {
383  trigger_error('tx_explicit not implemented', E_USER_ERROR);
384  }
385 
386 }
387 
394  const REMOTE_RESPONSE_ID = '97', LOCAL_RESPONSE_ID = '88' ; RX16 = '81';
396  protected $status_bytes = array();
397 
403  public function XBeeResponse($response) {
404  parent::_XBeeFrameBase();
405 
406  $this->status_byte = array('00' => 'OK','01' => 'Error','02'=> 'Invalid Command', '03' => 'Invalid Parameter', '04' => 'No Response' );
407  $this -> _parse($response);
408 
409  if ($this -> getApiId() === XBeeResponse::REMOTE_RESPONSE_ID) {
410  $this -> _parseRemoteAt();
411  } else if ($this -> getApiId() === XBeeResponse::RX16) {
412  $this -> _parseRX16();
413  } else if ($this -> getApiId() === XBeeResponse::LOCAL_RESPONSE_ID) {
414  $this -> _parseLocalAt();
415  } else {
416  trigger_error('Could not determine response type or response type is not implemented.', E_USER_WARNING);
417  }
418  /* debug
419  echo '</br>';echo 'Response:';print_r($response);echo '</br>';
420  echo ' apiId:';print_r($this->getApiId());echo '</br>';echo ' frameId:';print_r($this->getFrameId());echo '</br>';
421  echo ' add64:';print_r($this->getAddress64());echo '</br>';echo ' add16:';print_r($this->getAddress16());echo '</br>';
422  echo ' DB:';print_r($this->getSignalStrength());echo '</br>';echo ' NI:';print_r($this->getNodeId());echo '</br>';
423  echo ' CMD:';print_r($this->getCmd());echo '</br>';echo ' Status:';print_r($this->getStatus());echo '</br>';
424  echo ' isOk:';print_r($this->isOk());echo '</br>';*/
425  }
426 
433  private function _parse($response) {
434  $length = substr($response, 0, 2);
435  $checksum = substr($response, -1);
436  $cmdData = substr($response, 2, -1);
437  $apiId = substr($cmdData, 0, 1);
438  $frameId = substr($cmdData, 1, 1);
439  $calculatedChecksum = $this -> _calcChecksum($cmdData);
440  $calculatedLength = $this -> _getFramelength($cmdData);
441 
442  $packedChecksum = $checksum; //pack for comparison
443  $packedLength = $length; //pack for comparison
444 
445  if ($packedChecksum === $calculatedChecksum && $packedLength === $calculatedLength) {
446  $this -> setApiId($apiId);
447  $cmdData = $this->_unpackBytes($cmdData);
448  $cmdData=$cmdData[1];
449  $this -> setCmdData($cmdData);
450  $this -> setFrameId($frameId);
451  $this -> setFrame($response);
452  } else {
453  trigger_error('Checksum or length check failed.', E_USER_WARNING);
454  }
455  }
456 
462  private function _parseRemoteAt() {
463  //A valid remote frame looks like this:
464  //<apiId1> <frameId1> <address64,8> <address16,8> <command,2> <status,2>
465 
466  $cmdData = $this->getCmdData();
467 
468  $cmd = substr($cmdData, 24, 4);
469  $cmd = $this->_hexstr($cmd);
470 
471  $frameId = substr($cmdData, 2, 2);
472  $status = substr($cmdData, 4, 2);
473  $address64 = substr($cmdData, 4, 16);
474  $address16 = substr($cmdData, 20, 4);
475  $signalStrength = substr($cmdData, 30, 2);
476 
477  $this->_setSignalStrength($signalStrength);
478  $this->setAddress16($address16);
479  $this->setAddress64($address64);
480  $this->_setCmd($cmd);
481  $this->_setStatus($status);
482  $this->setFrameId($frameId);
483  }
484 
490  private function _parseLocalAt() {
491  //A valid local frame looks like this:
492  //<api_id1> <frameId1> <command2> <status2> <add16> <add64> <DB> <NI> <NULL>
493  $cmdData = $this->getCmdData();
494 
495  $cmd = substr($cmdData, 4, 6);
496  $cmd = $this->_hexstr($cmd);
497  $frameId = substr($cmdData, 2, 2);
498  $status = substr($cmdData, 8, 2);
499  $address64 = substr($cmdData, 14, 16);
500  $address16 = substr($cmdData, 10, 4);
501  $signalStrength = substr($cmdData, 30, 2);
502  $nodeId = $this->_hexstr(substr($cmdData, 32, -2));
503 
504  $this -> _setNodeId($nodeId);
505  $this->_setSignalStrength($signalStrength);
506  $this->setAddress16($address16);
507  $this->setAddress64($address64);
508  $this->_setCmd($cmd);
509  $this->_setStatus($status);
510  $this->setFrameId($frameId);
511  }
512 
519  private function _parseRX16() {
520  //A valid local frame looks like this:
521  //<api_id1> <MYID 2b> <RSSI><Options><payload>
522  $cmdData = $this->getCmdData();
523  $myid = substr($cmdData,2,4);
524  $rssi = substr($cmdData,6,2);
525  $payload =substr($cmdData,8);
526 
527 
528  $this->_setSignalStrength($rssi);
529  $this->setAddress16($myid);
530  $this->_setCmd($payload);
531  }
537  public function getSignalStrength() {
538  return $this -> signalStrength;
539  }
540 
546  private function _setSignalStrength($strength) {
547  $this->signalStrength = $strength;
548  }
549 
555  public function getNodeId() {
556  return $this->nodeId;
557  }
558 
564  private function _setNodeId($nodeId) {
565  $this->nodeId = $nodeId;
566  }
567 
573  private function _setStatus($status) {
574  $this->status = $status;
575  }
576 
588  public function getStatus() {
589  return $this->status;
590  }
591 
597  public function isOk() {
598  if ($this->getStatus()=='00') {
599  return TRUE;
600  } else {
601  return FALSE;
602  }
603  }
604 
611  private function _setCmd($cmd) {
612  $this->cmd = $cmd;
613  }
614 
620  public function getCmd() {
621  return $this->cmd;
622  }
623 }
624 ?>
_unpackBytes($data)
Definition: XBeeFrame.php:117
setOptions($options)
Definition: XBeeFrame.php:255
txExplicityCommand()
Definition: XBeeFrame.php:381
const DEFAULT_START_BYTE
Definition: XBeeFrame.php:12
const DEFAULT_FRAME_ID
Definition: XBeeFrame.php:12
XBeeResponse($response)
Definition: XBeeFrame.php:403
remoteAtCommand($address16, $cmd, $val, $address64= '0000000000000000', $options= '02')
Definition: XBeeFrame.php:323
const LOCAL_API_ID
Definition: XBeeFrame.php:13
setAddress16($address16)
Definition: XBeeFrame.php:219
const TX_EXPLICIT_API_ID
Definition: XBeeFrame.php:14
setStartByte($startByte)
Definition: XBeeFrame.php:201
queuedAtCommand()
Definition: XBeeFrame.php:365
setFrameId($frameId)
Definition: XBeeFrame.php:146
_packBytes($data)
Definition: XBeeFrame.php:107
const REMOTE_API_ID
Definition: XBeeFrame.php:13
_getFramelength($data)
Definition: XBeeFrame.php:66
setCmdData($cmdData)
Definition: XBeeFrame.php:183
setFrame($frame)
Definition: XBeeFrame.php:127
const REMOTE_RESPONSE_ID
Definition: XBeeFrame.php:394
setApiId($apiId)
Definition: XBeeFrame.php:164
const TX_API_ID
Definition: XBeeFrame.php:14
setAddress64($address64)
Definition: XBeeFrame.php:237
const QUEUED_API_ID
Definition: XBeeFrame.php:14
localAtCommand($cmd, $val= '')
Definition: XBeeFrame.php:350
const LOCAL_RESPONSE_ID
Definition: XBeeFrame.php:394
_calcChecksum($data)
Definition: XBeeFrame.php:49