Permalink
Browse files

initial import

  • Loading branch information...
gonzalo123 committed Mar 12, 2011
0 parents commit 8fa7bb783a78ec8166734c62fbf90ae360ad2594
Showing with 922 additions and 0 deletions.
  1. +3 −0 README
  2. +215 −0 Sms.php
  3. +37 −0 Sms/Dummy.php
  4. +76 −0 Sms/Http.php
  5. +13 −0 Sms/Interface.php
  6. +473 −0 Sms/Serial.php
  7. +16 −0 testDummy.php
  8. +43 −0 testHttp.php
  9. +46 −0 testSerial.php
3 README
@@ -0,0 +1,3 @@
+SMS sender/receiver script
+
+
215 Sms.php
@@ -0,0 +1,215 @@
+<?php
+
+/**
+ * GSM Modem AT Send/receive
+ * Adapter is loader via dependency injection
+ *
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
+ * USE IT AT YOUR OWN RISKS !
+ *
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * @copyright under GPL 2 licence
+ */
+class Sms
+{
+ private $_serial;
+ protected $_pinOK = false;
+
+ const EXCEPTION_PIN_ERROR = 1;
+ const EXCEPTION_NO_PIN = 2;
+ const EXCEPTION_SERVICE_NOT_IMPLEMENTED = 3;
+ /**
+ * Factory. Creates new instance. Dependency injections with the type os Modem
+ * valid serial resources:
+ * Sms_Serial: GSM modem conected via seria interface
+ * Sms_Http: GSM modem conected via seria/ethernet converter
+ * Sms_Dummy: Mock for testing
+ *
+ * @param Sms_Interface $serial
+ * @return Sms
+ */
+ public static function factory($serial)
+ {
+ if (!($serial instanceof Sms_Serial ||
+ $serial instanceof Sms_Http ||
+ $serial instanceof Sms_Dummy
+ )) {
+ throw new Exception("NOT IMPLEMENTED", self::EXCEPTION_SERVICE_NOT_IMPLEMENTED);
+ }
+
+ $serial->setValidOutputs(array(
+ 'OK',
+ 'ERROR',
+ '+CPIN: SIM PIN',
+ '+CPIN: READY'
+ ));
+
+ return new self($serial);
+ }
+
+ protected function __construct($serial)
+ {
+ $this->_serial = $serial;
+ }
+
+ private function readPort($returnBufffer = false)
+ {
+ list($last, $buffer) = $this->_serial->readPort();
+ if ($returnBufffer) {
+ return $buffer;
+ } else {
+ return strtoupper($last);
+ }
+ }
+
+ private function sendMessage($msg)
+ {
+ $this->_serial->sendMessage($msg);
+ }
+
+ private function deviceOpen()
+ {
+ $this->_serial->deviceOpen();
+ }
+
+ private function deviceClose()
+ {
+ $this->_serial->deviceClose();
+ }
+
+ /**
+ * Delete selected id from SMS SIM
+ *
+ * @param unknown_type $id
+ * @return unknown
+ */
+ public function deleteSms($id)
+ {
+ $this->deviceOpen();
+ $this->sendMessage("AT+CMGD={$id}\r");
+ $out = $this->readPort();
+ $this->deviceClose();
+ if ($out == 'OK') {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Sends a SMS to a selected tlfn
+ * @param Integer $tlfn
+ * @param String $text
+ * @return Boolean
+ */
+ public function sendSMS($tlfn, $text)
+ {
+ if ($this->_pinOK) {
+ $text = substr($text, 0, 160);
+ $this->deviceOpen();
+ $this->sendMessage("AT+CMGS=\"{$tlfn}\"\r{$text}" . chr(26));
+ $out = $this->readPort();
+
+ $this->deviceClose();
+ if ($out == 'OK') {
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ throw new Exception("Please insert the PIN", self::EXCEPTION_NO_PIN);
+ }
+ }
+
+ public function isPinOk()
+ {
+ return $this->_pinOK;
+ }
+
+ /**
+ * Inserts the pin number.
+ * first checks if PIN is set. If it's set nothing happens
+ * @param Integer $pin
+ * @return Sms
+ */
+ public function insertPin($pin)
+ {
+ $this->deviceOpen();
+
+ $this->sendMessage("AT+CPIN?\r");
+ $out = $this->readPort();
+ $this->deviceClose();
+
+ if ($out == "+CPIN: SIM PIN") {
+ $this->deviceOpen();
+ $this->sendMessage("AT+CPIN={$pin}\r");
+ $out = $this->readPort();
+ $this->deviceClose();
+ // I don't know why but I need to wait a few seconds until
+ // start sending SMS. Only after the first PIN
+ sleep(20);
+ }
+
+ switch ($out) {
+ case "+CPIN: READY":
+ case "OK":
+ $this->_pinOK = true;
+ break;
+ }
+
+ if ($this->_pinOK === true) {
+ return $this;
+ } else {
+ throw new Exception("PIN ERROR ({$out})", self::EXCEPTION_PIN_ERROR);
+ }
+ }
+
+ const ALL = "ALL";
+ const UNREAD = "REC UNREAD";
+ /**
+ * Read Inbox
+ *
+ * @param String $mode ALL | UNREAD
+ * @return Array
+ */
+ public function readInbox($mode=self::ALL)
+ {
+ $inbox = $return = array();
+ if ($this->_pinOK) {
+ $this->deviceOpen();
+ $this->sendMessage("AT+CMGF=1\r");
+ $out = $this->readPort();
+ if ($out == 'OK') {
+ $this->sendMessage("AT+CMGL=\"{$mode}\"\r");
+ $inbox = $this->readPort(true);
+ }
+ $this->deviceClose();
+ if (count($inbox) > 2) {
+ array_pop($inbox);
+ array_pop($inbox);
+ $arr = explode("+CMGL:", implode("\n", $inbox));
+
+ for ($i = 1; $i < count($arr); $i++) {
+ $arrItem = explode("\n", $arr[$i], 2);
+
+ // Header
+ $headArr = explode(",", $arrItem[0]);
+
+ $fromTlfn = str_replace('"', null, $headArr[2]);
+ $id = $headArr[0];
+ $date = $headArr[4];
+ $hour = $headArr[5];
+
+ // txt
+ $txt = $arrItem[1];
+
+ $return[] = array('id' => $id, 'tlfn' => $fromTlfn, 'msg' => $txt, 'date' => $date, 'hour' => $hour);
+ }
+ }
+ return $return;
+ } else {
+ throw new Exception("Please insert the PIN", self::EXCEPTION_NO_PIN);
+ }
+ }
+
+}
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * GSM Modem AT Dummy interface
+ *
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
+ * USE IT AT YOUR OWN RISKS !
+ *
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * @copyright under GPL 2 licence
+ */
+class Sms_Dummy implements Sms_Interface
+{
+ public function deviceOpen()
+ {
+ }
+
+ public function deviceClose()
+ {
+ }
+
+ public function sendMessage($msg)
+ {
+ }
+
+ public function readPort()
+ {
+ return array("OK", array());
+ }
+
+ private $_validOutputs = array();
+
+ public function setValidOutputs($validOutputs)
+ {
+ $this->_validOutputs = $validOutputs;
+ }
+}
@@ -0,0 +1,76 @@
+<?php
+/**
+ * GSM Modem AT Interface with a serial-ethernet adapter
+ *
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
+ * USE IT AT YOUR OWN RISKS !
+ *
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * @copyright under GPL 2 licence
+ */
+class Sms_Http implements Sms_Interface
+{
+ private $_host;
+ private $_port;
+
+ function __construct($host=null, $port=null)
+ {
+ $this->_host = $host;
+ $this->_port = $port;
+ }
+
+ public function confHost($host)
+ {
+ $this->_host = $host;
+ }
+
+ public function confPort($port)
+ {
+ $this->_port = $port;
+ }
+
+ private $_socket;
+
+ public function deviceOpen()
+ {
+ $this->_socket = @fsockopen($this->_host, $this->_port, $errno, $errstr, 30);
+
+ if (!$this->_socket) {
+ throw new Exception("SOCKET ERROR");
+ } else {
+ socket_set_timeout ($this->_socket, 10);
+ }
+ }
+
+ public function deviceClose()
+ {
+ fclose($this->_socket);
+ }
+
+ public function sendMessage($msg)
+ {
+ fwrite($this->_socket, $msg);
+ }
+
+ private $_validOutputs = array();
+
+ public function setValidOutputs($validOutputs)
+ {
+ $this->_validOutputs = $validOutputs;
+ }
+
+ public function readPort()
+ {
+ $last = null;
+ $buffer = array();
+
+ if ($this->_socket) {
+ while (!in_array($last, $this->_validOutputs)) {
+ $_buffer = trim(fgets($this->_socket));
+ $last = strtoupper($_buffer);
+ $buffer[] = $_buffer;
+ }
+ return array($last, $buffer);
+ }
+ }
+}
@@ -0,0 +1,13 @@
+<?php
+interface Sms_Interface
+{
+ public function deviceOpen();
+
+ public function deviceClose();
+
+ public function sendMessage($msg);
+
+ public function readPort();
+
+ public function setValidOutputs($validOutputs);
+}
Oops, something went wrong.

0 comments on commit 8fa7bb7

Please sign in to comment.