PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/callnotifier/EDSS1/Parser.php

https://github.com/cweiske/auerswald-callnotifier
PHP | 94 lines | 69 code | 13 blank | 12 comment | 15 complexity | a28e6b211ffb2c421028aeadc0967b5b MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. namespace callnotifier;
  3. class EDSS1_Parser
  4. {
  5. const PARAM = 0;
  6. const PARAMLENGTH = 1;
  7. const PARAMVAL = 2;
  8. public function parse($bytes)
  9. {
  10. $m = new EDSS1_Message();
  11. $m->sapi = ord($bytes{0}) >> 2;
  12. $m->callResponse = (int) ((ord($bytes{0}) & 2) == 2);
  13. $m->tei = ord($bytes{1}) >> 1;
  14. $curpos = 4;
  15. list($curpos, $cCallRef, $crLen) = $this->readLengthData($bytes, ++$curpos);
  16. if ($crLen == 0xFF) {
  17. return $m;
  18. }
  19. if ($crLen > 0) {
  20. $m->callRefType = ord($cCallRef{0}) >> 7;
  21. $nCallRef = ord($cCallRef{0}) & 127;
  22. if ($crLen > 1) {
  23. $nCallRef = ord($cCallRef{1}) + ($nCallRef << 8);
  24. if ($crLen > 2) {
  25. $nCallRef = ord($cCallRef{2}) + ($nCallRef << 8);
  26. }
  27. }
  28. $m->callRef = $nCallRef;
  29. }
  30. $m->type = ord($bytes{++$curpos});
  31. $complete = false;
  32. do {
  33. //parameter type
  34. $curbit = $bytes{++$curpos};
  35. if ($curbit == "\xFF" && $bytes{$curpos + 1} == "\n") {
  36. $complete = true;
  37. break;
  38. }
  39. $paramType = ord($curbit);
  40. $param = $this->getParameterByType($paramType);
  41. $m->parameters[] = $param;
  42. //parameter length
  43. $curbit = $bytes{++$curpos};
  44. $param->length = ord($curbit);
  45. //parameter data
  46. $param->setData(substr($bytes, $curpos + 1, $param->length));
  47. $curpos += $param->length;
  48. } while ($curpos < strlen($bytes) - 1);
  49. return $m;
  50. }
  51. /**
  52. * Read a datablock preceded with a length byte.
  53. *
  54. * @return array Array with new cursor position, data and data length
  55. */
  56. public function readLengthData($bytes, $curpos)
  57. {
  58. //var_dump('old' . $curpos);
  59. $length = ord($bytes{$curpos});
  60. if ($length != 0xFF) {
  61. $data = substr($bytes, $curpos + 1, $length);
  62. } else {
  63. $data = null;
  64. }
  65. return array($curpos + $length, $data, $length);
  66. }
  67. /**
  68. * @param integer $type Parameter type ID
  69. */
  70. public function getParameterByType($type)
  71. {
  72. $supported = array(0x28, 0x2C, 0x4C, 0x6C, 0x70);
  73. if (!in_array($type, $supported)) {
  74. return new EDSS1_Parameter($type);
  75. }
  76. $typeHex = sprintf('%02X', $type);
  77. $class = 'callnotifier\EDSS1_Parameter_' . $typeHex;
  78. return new $class($type);
  79. }
  80. }
  81. ?>