Skip to content
Browse files

initial import

  • Loading branch information...
0 parents commit 8fa7bb783a78ec8166734c62fbf90ae360ad2594 @gonzalo123 committed Mar 12, 2011
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);
+ }
+ }
+
+}
37 Sms/Dummy.php
@@ -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;
+ }
+}
76 Sms/Http.php
@@ -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);
+ }
+ }
+}
13 Sms/Interface.php
@@ -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);
+}
473 Sms/Serial.php
@@ -0,0 +1,473 @@
+<?php
+
+/**
+ * Based on: phpSerial (Rémy Sanchez <thenux@gmail.com>)
+ * http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html
+ * I've done a change in readport function, triggering only with a list of
+ * valid outputs. Windows support of the original class has been removed
+ *
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
+ * USE IT AT YOUR OWN RISKS !
+ *
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * @author Rémy Sanchez <thenux@gmail.com>
+ *
+ * @copyright under GPL 2 licence
+ */
+class Sms_Serial implements Sms_Interface
+{
+ const SERIAL_DEVICE_NOTSET = 0;
+ const SERIAL_DEVICE_SET = 1;
+ const SERIAL_DEVICE_OPENED = 2;
+
+ private $_device = null;
+ private $_dHandle = null;
+ private $_dState = self::SERIAL_DEVICE_NOTSET;
+ private $_buffer = "";
+ private $_os = "";
+ /**
+ * Constructor. Perform some checks about the OS and setserial
+ *
+ * @return Sms_Serial
+ */
+ function __construct()
+ {
+ setlocale(LC_ALL, "en_US");
+
+ $sysname = php_uname();
+
+ if (substr($sysname, 0, 5) === "Linux") {
+ $this->_os = "linux";
+
+ if ($this->_exec("stty --version") === 0) {
+ register_shutdown_function(array($this, "deviceClose"));
+ } else {
+ trigger_error("No stty availible, unable to run.", E_USER_ERROR);
+ }
+ } else {
+ trigger_error("Host OS is must be linux, unable tu run.", E_USER_ERROR);
+ exit();
+ }
+ }
+
+ /**
+ * Device set function : used to set the device name/address.
+ * -> linux : use the device address, like /dev/ttyS0
+ *
+ * @param string $device the name of the device to be used
+ * @return bool
+ */
+ function deviceSet($device)
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_OPENED) {
+ if ($this->_os === "linux") {
+ if (preg_match("@^COM(\d+):?$@i", $device, $matches)) {
+ $device = "/dev/ttyS" . ($matches[1] - 1);
+ }
+
+ if ($this->_exec("stty -F " . $device) === 0) {
+ $this->_device = $device;
+ $this->_dState = self::SERIAL_DEVICE_SET;
+ return true;
+ }
+ }
+ trigger_error("Specified serial port is not valid", E_USER_WARNING);
+ return false;
+ } else {
+ trigger_error("You must close your device before to set an other one", E_USER_WARNING);
+ return false;
+ }
+ }
+
+ /**
+ * Opens the device for reading and/or writing.
+ *
+ * @param string $mode Opening mode : same parameter as fopen()
+ * @return bool
+ */
+ function deviceOpen($mode = "r+b")
+ {
+ if ($this->_dState === self::SERIAL_DEVICE_OPENED) {
+ trigger_error("The device is already opened", E_USER_NOTICE);
+ return true;
+ }
+
+ if ($this->_dState === self::SERIAL_DEVICE_NOTSET) {
+ trigger_error("The device must be set before to be open", E_USER_WARNING);
+ return false;
+ }
+
+ if (!preg_match("@^[raw]\+?b?$@", $mode)) {
+ trigger_error("Invalid opening mode : " . $mode . ". Use fopen() modes.", E_USER_WARNING);
+ return false;
+ }
+
+ $this->_dHandle = @fopen($this->_device, $mode);
+
+ if ($this->_dHandle !== false) {
+ stream_set_blocking($this->_dHandle, 0);
+ $this->_dState = self::SERIAL_DEVICE_OPENED;
+ return true;
+ }
+
+ $this->_dHandle = null;
+ trigger_error("Unable to open the device", E_USER_WARNING);
+ return false;
+ }
+
+ /**
+ * Closes the device
+ *
+ * @return bool
+ */
+ function deviceClose()
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_OPENED) {
+ return true;
+ }
+
+ if (fclose($this->_dHandle)) {
+ $this->_dHandle = null;
+ $this->_dState = self::SERIAL_DEVICE_SET;
+ return true;
+ }
+
+ trigger_error("Unable to close the device", E_USER_ERROR);
+ return false;
+ }
+
+ //
+ // OPEN/CLOSE DEVICE SECTION -- {STOP}
+ //
+ //
+ // CONFIGURE SECTION -- {START}
+ //
+ /**
+ * Configure the Baud Rate
+ * Possible rates : 110, 150, 300, 600, 1200, 2400, 4800, 9600, 38400,
+ * 57600 and 115200.
+ *
+ * @param int $rate the rate to set the port in
+ * @return bool
+ */
+ function confBaudRate($rate)
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_SET) {
+ trigger_error("Unable to set the baud rate : the device is either not set or opened", E_USER_WARNING);
+ return false;
+ }
+
+ $validBauds = array(
+ 110 => 11,
+ 150 => 15,
+ 300 => 30,
+ 600 => 60,
+ 1200 => 12,
+ 2400 => 24,
+ 4800 => 48,
+ 9600 => 96,
+ 19200 => 19,
+ 38400 => 38400,
+ 57600 => 57600,
+ 115200 => 115200
+ );
+
+ if (isset($validBauds[$rate])) {
+ if ($this->_os === "linux") {
+ $ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);
+ } else {
+ return false;
+ }
+
+ if ($ret !== 0) {
+ trigger_error("Unable to set baud rate: " . $out[1], E_USER_WARNING);
+ return false;
+ }
+ }
+ }
+
+ /**
+ * Configure parity.
+ * Modes : odd, even, none
+ *
+ * @param string $parity one of the modes
+ * @return bool
+ */
+ function confParity($parity)
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_SET) {
+ trigger_error("Unable to set parity : the device is either not set or opened", E_USER_WARNING);
+ return false;
+ }
+
+ $args = array(
+ "none" => "-parenb",
+ "odd" => "parenb parodd",
+ "even" => "parenb -parodd",
+ );
+
+ if (!isset($args[$parity])) {
+ trigger_error("Parity mode not supported", E_USER_WARNING);
+ return false;
+ }
+
+ if ($this->_os === "linux") {
+ $ret = $this->_exec("stty -F " . $this->_device . " " . $args[$parity], $out);
+ }
+
+ if ($ret === 0) {
+ return true;
+ }
+
+ trigger_error("Unable to set parity : " . $out[1], E_USER_WARNING);
+ return false;
+ }
+
+ /**
+ * Sets the length of a character.
+ *
+ * @param int $int length of a character (5 <= length <= 8)
+ * @return bool
+ */
+ function confCharacterLength($int)
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_SET) {
+ trigger_error("Unable to set length of a character : the device is either not set or opened", E_USER_WARNING);
+ return false;
+ }
+
+ $int = (int) $int;
+ if ($int < 5)
+ $int = 5;
+ elseif ($int > 8)
+ $int = 8;
+
+ if ($this->_os === "linux") {
+ $ret = $this->_exec("stty -F " . $this->_device . " cs" . $int, $out);
+ }
+
+ if ($ret === 0) {
+ return true;
+ }
+
+ trigger_error("Unable to set character length : " . $out[1], E_USER_WARNING);
+ return false;
+ }
+
+ /**
+ * Sets the length of stop bits.
+ *
+ * @param float $length the length of a stop bit. It must be either 1,
+ * 1.5 or 2. 1.5 is not supported under linux and on some computers.
+ * @return bool
+ */
+ function confStopBits($length)
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_SET) {
+ trigger_error("Unable to set the length of a stop bit : the device is either not set or opened", E_USER_WARNING);
+ return false;
+ }
+
+ if ($length != 1 and $length != 2 and $length != 1.5 and !($length == 1.5 and $this->_os === "linux")) {
+ trigger_error("Specified stop bit length is invalid", E_USER_WARNING);
+ return false;
+ }
+
+ if ($this->_os === "linux") {
+ $ret = $this->_exec("stty -F " . $this->_device . " " . (($length == 1) ? "-" : "") . "cstopb", $out);
+ }
+
+ if ($ret === 0) {
+ return true;
+ }
+
+ trigger_error("Unable to set stop bit length : " . $out[1], E_USER_WARNING);
+ return false;
+ }
+
+ /**
+ * Configures the flow control
+ *
+ * @param string $mode Set the flow control mode. Availible modes :
+ * -> "none" : no flow control
+ * -> "rts/cts" : use RTS/CTS handshaking
+ * -> "xon/xoff" : use XON/XOFF protocol
+ * @return bool
+ */
+ function confFlowControl($mode)
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_SET) {
+ trigger_error("Unable to set flow control mode : the device is either not set or opened", E_USER_WARNING);
+ return false;
+ }
+
+ $linuxModes = array(
+ "none" => "clocal -crtscts -ixon -ixoff",
+ "rts/cts" => "-clocal crtscts -ixon -ixoff",
+ "xon/xoff" => "-clocal -crtscts ixon ixoff"
+ );
+
+ if ($mode !== "none" and $mode !== "rts/cts" and $mode !== "xon/xoff") {
+ trigger_error("Invalid flow control mode specified", E_USER_ERROR);
+ return false;
+ }
+
+ if ($this->_os === "linux") {
+ $ret = $this->_exec("stty -F " . $this->_device . " " . $linuxModes[$mode], $out);
+ }
+
+ if ($ret === 0) {
+ return true;
+ } else {
+ trigger_error("Unable to set flow control : " . $out[1], E_USER_ERROR);
+ return false;
+ }
+ }
+
+ /**
+ * Sets a setserial parameter (cf man setserial)
+ * NO MORE USEFUL !
+ * -> No longer supported
+ * -> Only use it if you need it
+ *
+ * @param string $param parameter name
+ * @param string $arg parameter value
+ * @return bool
+ */
+ function setSetserialFlag($param, $arg = "")
+ {
+ if (!$this->_ckOpened())
+ return false;
+
+ $return = exec("setserial " . $this->_device . " " . $param . " " . $arg . " 2>&1");
+
+ if ($return{0} === "I") {
+ trigger_error("setserial: Invalid flag", E_USER_WARNING);
+ return false;
+ } elseif ($return{0} === "/") {
+ trigger_error("setserial: Error with device file", E_USER_WARNING);
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Sends a string to the device
+ *
+ * @param string $str string to be sent to the device
+ * @param float $waitForReply time to wait for the reply (in seconds)
+ */
+ function sendMessage($str, $waitForReply = 0.1)
+ {
+ $this->_buffer .= $str;
+ $this->flush();
+
+ usleep((int) ($waitForReply * 1000000));
+ }
+
+ private $_validOutputs = array();
+ public function setValidOutputs($validOutputs)
+ {
+ $this->_validOutputs = $validOutputs;
+ }
+
+ /**
+ * Reads the port until no new datas are availible, then return the content.
+ *
+ * @pararm int $count number of characters to be read (will stop before
+ * if less characters are in the buffer)
+ * @return string
+ */
+ function readPort()
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_OPENED) {
+ trigger_error("Device must be opened to read it", E_USER_WARNING);
+ return false;
+ }
+ if ($this->_os === "linux") {
+ $last = null;
+ $buffer = array();
+
+ if ($this->_dHandle) {
+ $_buffer = "";
+ while (!in_array($last, $this->_validOutputs)) {
+ $bit = fread($this->_dHandle, 1);
+ if ($bit == "\n") {
+ $last = strtoupper(trim(strtoupper($_buffer)));
+ $buffer[] = $_buffer;
+ $_buffer = "";
+ } else {
+ $_buffer .= $bit;
+
+ }
+ }
+ return array($last, $buffer);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Flushes the output buffer
+ *
+ * @return bool
+ */
+ private function flush()
+ {
+ if (!$this->_ckOpened())
+ return false;
+
+ if (fwrite($this->_dHandle, $this->_buffer) !== false) {
+ $this->_buffer = "";
+ return true;
+ } else {
+ $this->_buffer = "";
+ trigger_error("Error while sending message", E_USER_WARNING);
+ return false;
+ }
+ }
+
+ private function _ckOpened()
+ {
+ if ($this->_dState !== self::SERIAL_DEVICE_OPENED) {
+ trigger_error("Device must be opened", E_USER_WARNING);
+ return false;
+ }
+
+ return true;
+ }
+
+ private function _ckClosed()
+ {
+ if ($this->_dState !== SERIAL_DEVICE_CLOSED) {
+ trigger_error("Device must be closed", E_USER_WARNING);
+ return false;
+ }
+
+ return true;
+ }
+
+ private function _exec($cmd, &$out = null)
+ {
+ $desc = array(
+ 1 => array("pipe", "w"),
+ 2 => array("pipe", "w")
+ );
+
+ $proc = proc_open($cmd, $desc, $pipes);
+
+ $ret = stream_get_contents($pipes[1]);
+ $err = stream_get_contents($pipes[2]);
+
+ fclose($pipes[1]);
+ fclose($pipes[2]);
+
+ $retVal = proc_close($proc);
+
+ if (func_num_args() == 2)
+ $out = array($ret, $err);
+ return $retVal;
+ }
+
+}
16 testDummy.php
@@ -0,0 +1,16 @@
+<?php
+
+require_once('Sms.php');
+require_once('Sms/Interface.php');
+require_once('Sms/Dummy.php');
+
+$pin = 1234;
+
+$serial = new Sms_Dummy;
+
+if (Sms::factory($serial)->insertPin($pin)
+ ->sendSMS(555987654, "test Hi")) {
+ echo "SMS sent\n";
+} else {
+ echo "SMS not Sent\n";
+}
43 testHttp.php
@@ -0,0 +1,43 @@
+<?php
+require_once('Sms.php');
+require_once('Sms/Interface.php');
+require_once('Sms/Http.php');
+
+$serialEternetConverterIP = '192.168.1.10';
+$serialEternetConverterPort = 1113;
+$pin = 1234;
+
+try {
+ $sms = Sms::factory(new Sms_Http($serialEternetConverterIP, $serialEternetConverterPort));
+ $sms->insertPin($pin);
+
+ if ($sms->sendSMS(555987654, "test Hi")) {
+ echo "SMS Sent\n";
+ } else {
+ echo "Sent Error\n";
+ }
+
+ // Now read inbox
+ foreach ($sms->readInbox() as $in) {
+ echo"tlfn: {$in['tlfn']} date: {$in['date']} {$in['hour']}\n{$in['msg']}\n";
+
+ // now delete sms
+ if ($sms->deleteSms($in['id'])) {
+ echo "SMS Deleted\n";
+ }
+ }
+} catch (Exception $e) {
+ switch ($e->getCode()) {
+ case Sms::EXCEPTION_NO_PIN:
+ echo "PIN Not set\n";
+ break;
+ case Sms::EXCEPTION_PIN_ERROR:
+ echo "PIN Incorrect\n";
+ break;
+ case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:
+ echo "Service Not implemented\n";
+ break;
+ default:
+ echo $e->getMessage();
+ }
+}
46 testSerial.php
@@ -0,0 +1,46 @@
+<?php
+require_once('Sms.php');
+require_once('Sms/Interface.php');
+require_once('Sms/Serial.php');
+
+$pin = 1234;
+
+try {
+ $serial = new Sms_Serial;
+ $serial->deviceSet("/dev/ttyS0");
+ $serial->confBaudRate(9600);
+ $serial->confParity('none');
+ $serial->confCharacterLength(8);
+
+ $sms = Sms::factory($serial)->insertPin($pin);
+
+ if ($sms->sendSMS(555987654, "test Hi")) {
+ echo "SMS sent\n";
+ } else {
+ echo "Sent Error\n";
+ }
+
+ // Now read inbox
+ foreach ($sms->readInbox() as $in) {
+ echo"tlfn: {$in['tlfn']} date: {$in['date']} {$in['hour']}\n{$in['msg']}\n";
+
+ // now delete sms
+ if ($sms->deleteSms($in['id'])) {
+ echo "SMS Deleted\n";
+ }
+ }
+} catch (Exception $e) {
+ switch ($e->getCode()) {
+ case Sms::EXCEPTION_NO_PIN:
+ echo "PIN Not set\n";
+ break;
+ case Sms::EXCEPTION_PIN_ERROR:
+ echo "PIN Incorrect\n";
+ break;
+ case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:
+ echo "Service Not implemented\n";
+ break;
+ default:
+ echo $e->getMessage();
+ }
+}

0 comments on commit 8fa7bb7

Please sign in to comment.
Something went wrong with that request. Please try again.