add fn "command" which combines tx and expect
... also has better (any) error handling
This commit is contained in:
parent
3bb143b6f4
commit
4ca8ddcf9f
42
sms.fnl
42
sms.fnl
@ -33,16 +33,22 @@
|
|||||||
;(print (escape-for-logging (unicode-to-gsm "{he@llo}€")))
|
;(print (escape-for-logging (unicode-to-gsm "{he@llo}€")))
|
||||||
|
|
||||||
|
|
||||||
(fn expect [fd pattern]
|
(fn expect [fd pattern fail-pattern]
|
||||||
(let [b (read fd 1024)]
|
(let [b (read fd 1024)]
|
||||||
(if (> (# b) 0)
|
(if (> (# b) 0)
|
||||||
(do
|
(do
|
||||||
(print (.. "<<< " (escape-for-logging b)))
|
(print (.. "<<< " (escape-for-logging b)))
|
||||||
(if (string.find b pattern)
|
(if (string.find b pattern)
|
||||||
(do (print "found" pattern) true)
|
(do (print "found" pattern) true)
|
||||||
|
(and fail-pattern (string.find b fail-pattern))
|
||||||
|
(error (.. "Expected " pattern ", got " (escape-for-logging b)))
|
||||||
(expect fd pattern)))
|
(expect fd pattern)))
|
||||||
nil)))
|
nil)))
|
||||||
|
|
||||||
|
(fn command [fd s]
|
||||||
|
(tx fd (.. s "\r\n"))
|
||||||
|
(expect fd "OK" "ERROR"))
|
||||||
|
|
||||||
(fn send-sms [number body]
|
(fn send-sms [number body]
|
||||||
(let [fd (fcntl.open "/dev/serial/by-id/usb-HUAWEI_HUAWEI_HiLink-if00-port0" posix.O_RDWR)
|
(let [fd (fcntl.open "/dev/serial/by-id/usb-HUAWEI_HUAWEI_HiLink-if00-port0" posix.O_RDWR)
|
||||||
termios (tcgetattr fd)]
|
termios (tcgetattr fd)]
|
||||||
@ -54,33 +60,17 @@
|
|||||||
(bnot ISIG)))
|
(bnot ISIG)))
|
||||||
(tset termios :oflag (band termios.oflag ( bnot OPOST)))
|
(tset termios :oflag (band termios.oflag ( bnot OPOST)))
|
||||||
(tcsetattr fd 0 termios)
|
(tcsetattr fd 0 termios)
|
||||||
;; opt->c_cc[VMIN] = 1;
|
|
||||||
;; opt->c_cc[VTIME] = 10;
|
|
||||||
(tcdrain fd)
|
(tcdrain fd)
|
||||||
(doto fd
|
(doto fd
|
||||||
(tx "AT\r\n")
|
(command "AT")
|
||||||
(expect "OK")
|
(command "AT&F") ; revert to defaults
|
||||||
|
(command "ATE0") ; disable command echo
|
||||||
(tx "AT&F\r\n")
|
(command "AT+CMEE=1") ;print CME errors
|
||||||
(expect "OK")
|
(command "AT+CMGF=1") ;SMS text mode (vs. PDU mode)
|
||||||
|
(command "AT+CSCS=\"GSM\"") ; default character set
|
||||||
(tx "ATE0\r\n") ;disable command echo
|
(command "AT+CSMP=17,12,0,0") ; message valid for 12*5 minutes
|
||||||
(expect "OK")
|
(command "AT+CSCA=\"+447958879879\",145\r\n") ;set SMSC
|
||||||
|
|
||||||
(tx "AT+CMEE=1\r\n") ;print CME errors
|
|
||||||
(expect "OK")
|
|
||||||
|
|
||||||
(tx "AT+CMGF=1\r\n") ;SMS text mode (not PDU mode)
|
|
||||||
(expect "OK")
|
|
||||||
|
|
||||||
(tx "AT+CSCS=\"GSM\"\r\n") ; default character set
|
|
||||||
(expect "OK")
|
|
||||||
|
|
||||||
(tx "AT+CSMP=17,12,0,0\r\n") ; message valid for 12*5 minutes
|
|
||||||
(expect "OK")
|
|
||||||
|
|
||||||
(tx "AT+CSCA=\"+447958879879\",145\r\n") ;set SMSC
|
|
||||||
(expect "OK")
|
|
||||||
|
|
||||||
(tx (.. "AT+CMGS=\"" number "\"\r\n"))
|
(tx (.. "AT+CMGS=\"" number "\"\r\n"))
|
||||||
(expect ">")
|
(expect ">")
|
||||||
|
Loading…
Reference in New Issue
Block a user