add fn "command" which combines tx and expect

... also has better (any) error handling
main
Daniel Barlow 2022-09-15 22:17:18 +01:00
parent 3bb143b6f4
commit 4ca8ddcf9f
1 changed files with 16 additions and 26 deletions

42
sms.fnl
View File

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