PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/UniversalGsmDevice.php

https://bitbucket.org/prusol/phpgsmdevice
PHP | 634 lines | 323 code | 150 blank | 161 comment | 65 complexity | 09c012a7dcee00bbba875d03d1acf2a2 MD5 | raw file
  1. <?php
  2. require_once 'GsmDevice.php';
  3. require_once 'DeviceInfoInterface.php';
  4. require_once 'MessageInterface.php';
  5. /*
  6. * To change this template, choose Tools | Templates
  7. * and open the template in the editor.
  8. */
  9. /**
  10. * This is example driver implementation.
  11. * As far as we know it's fully compatible with:
  12. * 1) Huawei E156G
  13. *
  14. * @author Piotr Rusoł <prusol@bitbucket.org>
  15. * @thanks Tomasz Mikulski <tmikulski@bitbucket.org>
  16. * @license http://www.gnu.org/licenses/gpl-2.0.txt GPLv2
  17. */
  18. class UniversalGsmDevice extends GsmDevice implements DeviceInfoInterface, MessageInterface{
  19. //put your code here
  20. public static $DEBUG = false;
  21. private $ASCII_CR = null;
  22. private $ASCII_LF = null;
  23. private $ASCII_SUB = null;
  24. private $serial = null;
  25. /**
  26. *
  27. * @param string $devPath
  28. * @param int $bRate
  29. * @param int $chrLen
  30. * @param string $parity
  31. * @param int $bitStop
  32. * @param string $flowCon
  33. */
  34. function __construct(
  35. $devPath = '',
  36. $bRate = 9600,
  37. $chrLen = 8,
  38. $parity = 'none',
  39. $bitStop = 1,
  40. $flowCon = "xon/xoff"
  41. ) {
  42. $this->serial = new phpSerial();
  43. $this->serial->deviceSet($devPath);
  44. $this->serial->confBaudRate($bRate);
  45. $this->serial->confCharacterLength($chrLen);
  46. $this->serial->confParity($parity);
  47. $this->serial->confStopBits($bitStop);
  48. $this->serial->confFlowControl($flowCon);
  49. $this->ASCII_CR = chr(13);
  50. $this->ASCII_LF = chr(10);
  51. $this->ASCII_SUB = chr(26);
  52. }
  53. function __destruct() {
  54. $this->serial->deviceClose();
  55. }
  56. public function open(){
  57. return $this->serial->deviceOpen();
  58. }
  59. public function close(){
  60. return $this->serial->deviceClose();
  61. }
  62. public function reconnect() {
  63. $this->serial->deviceClose();
  64. return $this->serial->deviceOpen();
  65. }
  66. /**
  67. * DONE
  68. *
  69. * @return string
  70. * @throws Exception
  71. */
  72. public function getManufacturer(){
  73. $this->serial->sendMessage(self::GET_MANUFACTURER. $this->ASCII_CR);
  74. if(self::$DEBUG){
  75. $result = $this->serial->readPort();
  76. print_r(bin2hex($result));
  77. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  78. return;
  79. }
  80. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
  81. if(end($result) == 'OK'){
  82. return $result[0];
  83. }
  84. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  85. }
  86. /**
  87. * DONE
  88. *
  89. * @return string
  90. * @throws Exception
  91. */
  92. public function getModel(){
  93. $this->serial->sendMessage(self::GET_MODEL. $this->ASCII_CR);
  94. if(self::$DEBUG){
  95. $result = $this->serial->readPort();
  96. print_r(bin2hex($result));
  97. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  98. return;
  99. }
  100. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
  101. if(end($result) == 'OK'){
  102. return $result[0];
  103. }
  104. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  105. }
  106. /**
  107. * DONE
  108. *
  109. * @return string
  110. * @throws Exception
  111. */
  112. public function getSoftwareVersion(){
  113. $this->serial->sendMessage(self::GET_SOFT_VERSION. $this->ASCII_CR);
  114. if(self::$DEBUG){
  115. $result = $this->serial->readPort();
  116. print_r(bin2hex($result));
  117. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  118. return;
  119. }
  120. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
  121. if(end($result) == 'OK'){
  122. return $result[0];
  123. }
  124. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  125. }
  126. /**
  127. * DONE
  128. *
  129. * @return string
  130. * @throws Exception
  131. */
  132. public function getImei(){
  133. $this->serial->sendMessage(self::GET_IMEI. $this->ASCII_CR);
  134. if(self::$DEBUG){
  135. $result = $this->serial->readPort();
  136. print_r(bin2hex($result));
  137. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  138. return;
  139. }
  140. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
  141. if(end($result) == 'OK'){
  142. return $result[0];
  143. }
  144. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  145. }
  146. /**
  147. * DONE
  148. *
  149. * @return string
  150. * @throws Exception
  151. */
  152. public function getImsi(){
  153. $this->serial->sendMessage(self::GET_IMSI. $this->ASCII_CR);
  154. if(self::$DEBUG){
  155. $result = $this->serial->readPort();
  156. print_r(bin2hex($result));
  157. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  158. return;
  159. }
  160. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
  161. if(end($result) == 'OK'){
  162. return $result[0];
  163. }
  164. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  165. }
  166. /**
  167. * UNDER IMPLEMENTATION
  168. *
  169. * @return type
  170. * @throws Exception
  171. */
  172. public function getMsisdn(){
  173. /*
  174. * Need more implementation.
  175. * Returns OK with +CME ERROR:25 +CNUM: "A","B",C where
  176. * A = 'empty string' - could be ISDN number
  177. * B = '517875816' - phone number
  178. * C = '129' - could be phone number type
  179. */
  180. $this->serial->sendMessage(self::GET_MSISDN. $this->ASCII_CR);
  181. if(self::$DEBUG){
  182. $result = $this->serial->readPort();
  183. print_r(bin2hex($result));
  184. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 3), trim($result)));
  185. return;
  186. }
  187. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 3), trim($this->serial->readPort()));
  188. if(end($result) == 'OK'){
  189. return $result[0];
  190. }
  191. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  192. }
  193. /**
  194. * DONE
  195. *
  196. * @return int
  197. * @throws Exception
  198. */
  199. public function getActivityStatus(){
  200. $this->serial->sendMessage(self::GET_ACTIVITY_STATUS. $this->ASCII_CR);
  201. if(self::$DEBUG){
  202. $result = $this->serial->readPort();
  203. print_r(bin2hex($result));
  204. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  205. return;
  206. }
  207. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+CPAS :', '', $this->serial->readPort())));
  208. if(end($result) == 'OK'){
  209. return $result[0];
  210. }
  211. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  212. }
  213. /**
  214. * DONE
  215. *
  216. * @return array
  217. * @throws Exception
  218. */
  219. public function getCapabilities(){
  220. $this->serial->sendMessage(self::GET_CAPABILITIES. $this->ASCII_CR);
  221. if(self::$DEBUG){
  222. $result = $this->serial->readPort();
  223. print_r(bin2hex($result));
  224. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  225. return;
  226. }
  227. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+GCAP: ', '', $this->serial->readPort())));
  228. if(end($result) == 'OK'){
  229. return explode(',', str_replace('+', '', $result[0]));
  230. }
  231. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  232. }
  233. /**
  234. * DONE
  235. *
  236. * @return DateTime
  237. * @throws Exception
  238. */
  239. public function getDateTime(){
  240. $this->serial->sendMessage(self::GET_DATE_TIME. $this->ASCII_CR);
  241. if(self::$DEBUG){
  242. $result = $this->serial->readPort();
  243. print_r(bin2hex($result));
  244. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  245. return;
  246. }
  247. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+CCLK: ', '', $this->serial->readPort())));
  248. if(end($result) == 'OK'){
  249. return DateTime::createFromFormat(self::DATE_TIME_FORMAT, $result[0]);
  250. }
  251. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  252. }
  253. /**
  254. * DONE
  255. *
  256. * @return string
  257. * @throws Exception
  258. */
  259. public function getSignalQuality(){
  260. $this->serial->sendMessage(self::GET_SIGNAL_QUALITY. $this->ASCII_CR);
  261. if(self::$DEBUG){
  262. $result = $this->serial->readPort();
  263. print_r(bin2hex($result));
  264. print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
  265. return;
  266. }
  267. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+CSQ: ', '', $this->serial->readPort())));
  268. if(end($result) == 'OK'){
  269. return $result[0];
  270. }
  271. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  272. }
  273. /**
  274. * DONE
  275. *
  276. * @return type
  277. */
  278. public function getStoredOperators(){
  279. $this->serial->sendMessage(self::GET_STORED_OPERATORS. $this->ASCII_CR);
  280. $result = $this->serial->readPort();
  281. $result = str_replace('+COPN: ', '', trim($result));
  282. $result = str_replace('"', '', $result);
  283. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), $result);
  284. if(end($result) == 'OK'){
  285. $result = explode($this->ASCII_CR.$this->ASCII_LF, $result[0]);
  286. $out = array();
  287. foreach($result as $opt) {
  288. list($optName, $optValue) = explode(',', $opt);
  289. $out[$optName] = $optValue;
  290. }
  291. return $out;
  292. }
  293. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  294. }
  295. /**
  296. *
  297. * NOT IMPLEMENTED YET
  298. * @return type
  299. */
  300. public function getCurrentOperator(){
  301. $this->serial->sendMessage(self::GET_CURRENT_OPERATOR. $this->ASCII_CR);
  302. $result = $this->serial->readPort();
  303. return $result;
  304. }
  305. /**
  306. * NOT IMPLEMENTED YET
  307. * @return type
  308. */
  309. public function getAvailableOperators(){
  310. $this->serial->sendMessage(self::GET_AVAILABLE_OPERATORS. $this->ASCII_CR);
  311. $result = $this->serial->readPort();
  312. return $result;
  313. }
  314. /**
  315. * NOT IMPLEMENTED YET
  316. * @return type
  317. */
  318. public function getCurrentMessageFormat(){
  319. $this->serial->sendMessage(self::GET_CURRENT_MESSAGE_FORMAT. $this->ASCII_CR);
  320. $result = $this->serial->readPort();
  321. return $result;
  322. }
  323. /**
  324. * NOT IMPLEMENTED YET
  325. * @return type
  326. */
  327. public function getAvailableMessageFormat(){
  328. $this->serial->sendMessage(self::GET_AVAILABLE_MESSAGE_FORMAT. $this->ASCII_CR);
  329. $result = $this->serial->readPort();
  330. return $result;
  331. }
  332. /**
  333. *
  334. * DONE
  335. *
  336. * @return type
  337. */
  338. public function getAvailableMessageCharSet(){
  339. $search = array(
  340. $this->ASCII_CR.$this->ASCII_LF,
  341. '+CSCS: ',
  342. '(',
  343. ')',
  344. '"'
  345. );
  346. $this->serial->sendMessage(self::GET_AVAILABLE_MESSAGE_CHAR_SETS. $this->ASCII_CR);
  347. $result = $this->serial->readPort();
  348. $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result));
  349. if(end($result) == 'OK'){
  350. //return str_replace($search, '', $result[0]);
  351. return explode(',', str_replace($search, '', $result[0]));
  352. }
  353. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  354. }
  355. /**
  356. * DONE
  357. *
  358. * @param DateTime $dt
  359. * @return type
  360. */
  361. public function setDateTime(DateTime $dt = null){
  362. if(!isset($dt)) { $dt = new DateTime('now'); }
  363. $this->serial->sendMessage(self::SET_DATE_TIME . '"' . $dt->format(self::DATE_TIME_FORMAT) . '"' . $this->ASCII_CR);
  364. if($result = trim($this->serial->readPort()) != 'OK'){
  365. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  366. }
  367. }
  368. /**
  369. * DONE
  370. *
  371. * @param type $format
  372. * @return type
  373. */
  374. public function setMessageFormat($format){
  375. switch ($format){
  376. case self::MESSAGE_PDU_FORMAT:
  377. break;
  378. case self::MESSAGE_TEXT_FORMAT:
  379. break;
  380. default :
  381. throw new InvalidArgumentException(__METHOD__ . '##' . $format);
  382. break;
  383. }
  384. $this->serial->sendMessage(self::SET_MESSAGE_FORMAT.$format.$this->ASCII_CR);
  385. if(($result = trim($this->serial->readPort())) != 'OK'){
  386. throw new Exception(__METHOD__ . '##' . $result);
  387. }
  388. return $result;
  389. }
  390. /**
  391. * TO REVIEW
  392. *
  393. * @param type $status
  394. * @return type
  395. * @throws InvalidArgumentException
  396. */
  397. public function getMessages($status){
  398. $regExp = null;
  399. $mode = null;
  400. if($status == self::MESSAGE_STATUS_PDU_ALL ||
  401. $status == self::MESSAGE_STATUS_PDU_READ ||
  402. $status == self::MESSAGE_STATUS_PDU_SENT ||
  403. $status == self::MESSAGE_STATUS_PDU_UNREAD ||
  404. $status == self::MESSAGE_STATUS_PDU_UNSENT
  405. ){
  406. print_r('PDU');
  407. //return;
  408. $this->setMessageFormat(self::MESSAGE_PDU_FORMAT);
  409. $regExp = '/^\\+CMGL: (\d*),(\d*),(.*),(\d*)([\w|\b|\s]*)/m';
  410. $mode = 'PDU';
  411. }elseif ($status == (self::MESSAGE_STATUS_TEXT_ALL ||
  412. $status == self::MESSAGE_STATUS_TEXT_READ ||
  413. $status == self::MESSAGE_STATUS_TEXT_SENT ||
  414. $status == self::MESSAGE_STATUS_TEXT_UNREAD ||
  415. $status == self::MESSAGE_STATUS_TEXT_UNSENT)
  416. ) {
  417. print_r('TEXT');
  418. //return;
  419. $this->setMessageFormat(self::MESSAGE_TEXT_FORMAT);
  420. $regExp = '/^\\+CMGL: (\d*),"([\w|\b|\s]*)","([\w|\b|\s|\\+]*)",([\w|\b|\s]*),([\w|\b|\s]*)/m';
  421. $mode = 'TEXT';
  422. } else {
  423. throw new InvalidArgumentException($status);
  424. }
  425. $this->serial->sendMessage(self::GET_MESSAGES . $status . $this->ASCII_CR . $this->ASCII_LF , 1);
  426. $result = explode(str_repeat($this->ASCII_CR . $this->ASCII_LF , 2), trim($this->serial->readPort()));
  427. if(end($result) != 'OK'){
  428. throw new Exception(__METHOD__ . '##' . implode(' ', $result));
  429. }
  430. $msgs = array();
  431. preg_match_all($regExp, $result[0], $msgs, PREG_SET_ORDER);
  432. return $msgs;
  433. $out = array();
  434. foreach ($msgs as $message){
  435. if($mode == 'PDU'){
  436. list($all, $msgNr, $msgStatus, $dstNum, $field, $msgBody) = $message;
  437. $out[$msgNr] = array($msgStatus, $dstNum, trim($field), trim($msgBody));
  438. } else {
  439. list($all, $msgNr, $msgStatus, $dstNum, $field, $msgBody) = $message;
  440. $out[$msgNr] = array($msgStatus, $dstNum, trim($field), trim($msgBody));
  441. }
  442. }
  443. return $out;
  444. }
  445. /**
  446. * NOT IMPLEMENTED YET
  447. *
  448. * @param type $dNumber
  449. * @param type $body
  450. * @return type
  451. */
  452. public function writeMessageToMem($dNumber, $body){
  453. $this->serial->sendMessage(self::WRITE_MESSAGE_TO_MEM.'"'.$dNumber.'"'.$this->ASCII_CR);
  454. $this->serial->sendMessage($body.$this->ASCII_SUB);
  455. $result = $this->serial->readPort();
  456. return $result;
  457. }
  458. /**
  459. * NOT IMPLEMENTED YET
  460. *
  461. * @param type $dNumber
  462. * @param type $body
  463. * @return type
  464. */
  465. public function sendMessage($dNumber, $body){
  466. $this->serial->sendMessage(self::SEND_MESSAGE.'"'.$dNumber.'"'.$this->ASCII_CR);
  467. $this->serial->sendMessage($body). $this->ASCII_SUB;
  468. $result = $this->serial->readPort();
  469. return $result;
  470. }
  471. /**
  472. * NOT IMPLEMENTED YET
  473. *
  474. * @param type $id
  475. * @param type $dNumber
  476. * @return type
  477. */
  478. public function sendMessageFromMem($id, $dNumber){
  479. $this->serial->sendMessage(self::SEND_MESSAGE_FROM_MEM.$id.','.$dNumber.$this->ASCII_CR);
  480. $result = $this->serial->readPort();
  481. return $result;
  482. }
  483. }
  484. ?>