/UniversalGsmDevice.php
PHP | 634 lines | 323 code | 150 blank | 161 comment | 65 complexity | 09c012a7dcee00bbba875d03d1acf2a2 MD5 | raw file
- <?php
- require_once 'GsmDevice.php';
- require_once 'DeviceInfoInterface.php';
- require_once 'MessageInterface.php';
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * This is example driver implementation.
- * As far as we know it's fully compatible with:
- * 1) Huawei E156G
- *
- * @author Piotr Rusoł <prusol@bitbucket.org>
- * @thanks Tomasz Mikulski <tmikulski@bitbucket.org>
- * @license http://www.gnu.org/licenses/gpl-2.0.txt GPLv2
- */
- class UniversalGsmDevice extends GsmDevice implements DeviceInfoInterface, MessageInterface{
- //put your code here
- public static $DEBUG = false;
- private $ASCII_CR = null;
- private $ASCII_LF = null;
- private $ASCII_SUB = null;
- private $serial = null;
- /**
- *
- * @param string $devPath
- * @param int $bRate
- * @param int $chrLen
- * @param string $parity
- * @param int $bitStop
- * @param string $flowCon
- */
- function __construct(
- $devPath = '',
- $bRate = 9600,
- $chrLen = 8,
- $parity = 'none',
- $bitStop = 1,
- $flowCon = "xon/xoff"
- ) {
- $this->serial = new phpSerial();
- $this->serial->deviceSet($devPath);
- $this->serial->confBaudRate($bRate);
- $this->serial->confCharacterLength($chrLen);
- $this->serial->confParity($parity);
- $this->serial->confStopBits($bitStop);
- $this->serial->confFlowControl($flowCon);
- $this->ASCII_CR = chr(13);
- $this->ASCII_LF = chr(10);
- $this->ASCII_SUB = chr(26);
- }
- function __destruct() {
- $this->serial->deviceClose();
- }
- public function open(){
- return $this->serial->deviceOpen();
- }
- public function close(){
- return $this->serial->deviceClose();
- }
- public function reconnect() {
- $this->serial->deviceClose();
- return $this->serial->deviceOpen();
- }
- /**
- * DONE
- *
- * @return string
- * @throws Exception
- */
- public function getManufacturer(){
- $this->serial->sendMessage(self::GET_MANUFACTURER. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
- if(end($result) == 'OK'){
- return $result[0];
- }
-
-
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return string
- * @throws Exception
- */
- public function getModel(){
- $this->serial->sendMessage(self::GET_MODEL. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return string
- * @throws Exception
- */
- public function getSoftwareVersion(){
- $this->serial->sendMessage(self::GET_SOFT_VERSION. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return string
- * @throws Exception
- */
- public function getImei(){
- $this->serial->sendMessage(self::GET_IMEI. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return string
- * @throws Exception
- */
- public function getImsi(){
- $this->serial->sendMessage(self::GET_IMSI. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($this->serial->readPort()));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * UNDER IMPLEMENTATION
- *
- * @return type
- * @throws Exception
- */
- public function getMsisdn(){
- /*
- * Need more implementation.
- * Returns OK with +CME ERROR:25 +CNUM: "A","B",C where
- * A = 'empty string' - could be ISDN number
- * B = '517875816' - phone number
- * C = '129' - could be phone number type
- */
- $this->serial->sendMessage(self::GET_MSISDN. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 3), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 3), trim($this->serial->readPort()));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return int
- * @throws Exception
- */
- public function getActivityStatus(){
- $this->serial->sendMessage(self::GET_ACTIVITY_STATUS. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+CPAS :', '', $this->serial->readPort())));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return array
- * @throws Exception
- */
- public function getCapabilities(){
- $this->serial->sendMessage(self::GET_CAPABILITIES. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+GCAP: ', '', $this->serial->readPort())));
- if(end($result) == 'OK'){
- return explode(',', str_replace('+', '', $result[0]));
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return DateTime
- * @throws Exception
- */
- public function getDateTime(){
- $this->serial->sendMessage(self::GET_DATE_TIME. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+CCLK: ', '', $this->serial->readPort())));
- if(end($result) == 'OK'){
- return DateTime::createFromFormat(self::DATE_TIME_FORMAT, $result[0]);
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return string
- * @throws Exception
- */
- public function getSignalQuality(){
- $this->serial->sendMessage(self::GET_SIGNAL_QUALITY. $this->ASCII_CR);
- if(self::$DEBUG){
- $result = $this->serial->readPort();
- print_r(bin2hex($result));
- print_r(explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result)));
- return;
- }
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim(str_replace('+CSQ: ', '', $this->serial->readPort())));
- if(end($result) == 'OK'){
- return $result[0];
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @return type
- */
- public function getStoredOperators(){
- $this->serial->sendMessage(self::GET_STORED_OPERATORS. $this->ASCII_CR);
- $result = $this->serial->readPort();
- $result = str_replace('+COPN: ', '', trim($result));
- $result = str_replace('"', '', $result);
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), $result);
- if(end($result) == 'OK'){
-
- $result = explode($this->ASCII_CR.$this->ASCII_LF, $result[0]);
- $out = array();
-
- foreach($result as $opt) {
-
- list($optName, $optValue) = explode(',', $opt);
- $out[$optName] = $optValue;
-
- }
- return $out;
- }
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- *
- * NOT IMPLEMENTED YET
- * @return type
- */
- public function getCurrentOperator(){
- $this->serial->sendMessage(self::GET_CURRENT_OPERATOR. $this->ASCII_CR);
- $result = $this->serial->readPort();
- return $result;
- }
- /**
- * NOT IMPLEMENTED YET
- * @return type
- */
- public function getAvailableOperators(){
- $this->serial->sendMessage(self::GET_AVAILABLE_OPERATORS. $this->ASCII_CR);
- $result = $this->serial->readPort();
- return $result;
- }
- /**
- * NOT IMPLEMENTED YET
- * @return type
- */
- public function getCurrentMessageFormat(){
- $this->serial->sendMessage(self::GET_CURRENT_MESSAGE_FORMAT. $this->ASCII_CR);
- $result = $this->serial->readPort();
- return $result;
- }
- /**
- * NOT IMPLEMENTED YET
- * @return type
- */
- public function getAvailableMessageFormat(){
- $this->serial->sendMessage(self::GET_AVAILABLE_MESSAGE_FORMAT. $this->ASCII_CR);
- $result = $this->serial->readPort();
- return $result;
- }
- /**
- *
- * DONE
- *
- * @return type
- */
- public function getAvailableMessageCharSet(){
-
- $search = array(
-
- $this->ASCII_CR.$this->ASCII_LF,
- '+CSCS: ',
- '(',
- ')',
- '"'
- );
-
- $this->serial->sendMessage(self::GET_AVAILABLE_MESSAGE_CHAR_SETS. $this->ASCII_CR);
- $result = $this->serial->readPort();
- $result = explode(str_repeat($this->ASCII_CR.$this->ASCII_LF, 2), trim($result));
-
- if(end($result) == 'OK'){
- //return str_replace($search, '', $result[0]);
- return explode(',', str_replace($search, '', $result[0]));
-
- }
-
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- /**
- * DONE
- *
- * @param DateTime $dt
- * @return type
- */
- public function setDateTime(DateTime $dt = null){
- if(!isset($dt)) { $dt = new DateTime('now'); }
-
- $this->serial->sendMessage(self::SET_DATE_TIME . '"' . $dt->format(self::DATE_TIME_FORMAT) . '"' . $this->ASCII_CR);
-
- if($result = trim($this->serial->readPort()) != 'OK'){
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
-
- }
- /**
- * DONE
- *
- * @param type $format
- * @return type
- */
- public function setMessageFormat($format){
- switch ($format){
- case self::MESSAGE_PDU_FORMAT:
- break;
-
- case self::MESSAGE_TEXT_FORMAT:
- break;
-
- default :
- throw new InvalidArgumentException(__METHOD__ . '##' . $format);
- break;
- }
-
- $this->serial->sendMessage(self::SET_MESSAGE_FORMAT.$format.$this->ASCII_CR);
-
- if(($result = trim($this->serial->readPort())) != 'OK'){
- throw new Exception(__METHOD__ . '##' . $result);
- }
- return $result;
- }
- /**
- * TO REVIEW
- *
- * @param type $status
- * @return type
- * @throws InvalidArgumentException
- */
- public function getMessages($status){
-
- $regExp = null;
- $mode = null;
-
- if($status == self::MESSAGE_STATUS_PDU_ALL ||
- $status == self::MESSAGE_STATUS_PDU_READ ||
- $status == self::MESSAGE_STATUS_PDU_SENT ||
- $status == self::MESSAGE_STATUS_PDU_UNREAD ||
- $status == self::MESSAGE_STATUS_PDU_UNSENT
- ){
- print_r('PDU');
- //return;
- $this->setMessageFormat(self::MESSAGE_PDU_FORMAT);
- $regExp = '/^\\+CMGL: (\d*),(\d*),(.*),(\d*)([\w|\b|\s]*)/m';
- $mode = 'PDU';
- }elseif ($status == (self::MESSAGE_STATUS_TEXT_ALL ||
- $status == self::MESSAGE_STATUS_TEXT_READ ||
- $status == self::MESSAGE_STATUS_TEXT_SENT ||
- $status == self::MESSAGE_STATUS_TEXT_UNREAD ||
- $status == self::MESSAGE_STATUS_TEXT_UNSENT)
- ) {
- print_r('TEXT');
- //return;
- $this->setMessageFormat(self::MESSAGE_TEXT_FORMAT);
- $regExp = '/^\\+CMGL: (\d*),"([\w|\b|\s]*)","([\w|\b|\s|\\+]*)",([\w|\b|\s]*),([\w|\b|\s]*)/m';
- $mode = 'TEXT';
- } else {
- throw new InvalidArgumentException($status);
- }
- $this->serial->sendMessage(self::GET_MESSAGES . $status . $this->ASCII_CR . $this->ASCII_LF , 1);
- $result = explode(str_repeat($this->ASCII_CR . $this->ASCII_LF , 2), trim($this->serial->readPort()));
- if(end($result) != 'OK'){
- throw new Exception(__METHOD__ . '##' . implode(' ', $result));
- }
- $msgs = array();
-
- preg_match_all($regExp, $result[0], $msgs, PREG_SET_ORDER);
- return $msgs;
- $out = array();
-
- foreach ($msgs as $message){
- if($mode == 'PDU'){
- list($all, $msgNr, $msgStatus, $dstNum, $field, $msgBody) = $message;
- $out[$msgNr] = array($msgStatus, $dstNum, trim($field), trim($msgBody));
- } else {
- list($all, $msgNr, $msgStatus, $dstNum, $field, $msgBody) = $message;
- $out[$msgNr] = array($msgStatus, $dstNum, trim($field), trim($msgBody));
- }
- }
-
- return $out;
- }
- /**
- * NOT IMPLEMENTED YET
- *
- * @param type $dNumber
- * @param type $body
- * @return type
- */
- public function writeMessageToMem($dNumber, $body){
- $this->serial->sendMessage(self::WRITE_MESSAGE_TO_MEM.'"'.$dNumber.'"'.$this->ASCII_CR);
- $this->serial->sendMessage($body.$this->ASCII_SUB);
- $result = $this->serial->readPort();
- return $result;
- }
- /**
- * NOT IMPLEMENTED YET
- *
- * @param type $dNumber
- * @param type $body
- * @return type
- */
- public function sendMessage($dNumber, $body){
- $this->serial->sendMessage(self::SEND_MESSAGE.'"'.$dNumber.'"'.$this->ASCII_CR);
- $this->serial->sendMessage($body). $this->ASCII_SUB;
- $result = $this->serial->readPort();
- return $result;
- }
- /**
- * NOT IMPLEMENTED YET
- *
- * @param type $id
- * @param type $dNumber
- * @return type
- */
- public function sendMessageFromMem($id, $dNumber){
- $this->serial->sendMessage(self::SEND_MESSAGE_FROM_MEM.$id.','.$dNumber.$this->ASCII_CR);
- $result = $this->serial->readPort();
- return $result;
- }
-
- }
- ?>