commit 4ca8ddcf9fed51d399ad5f41d3b8ec3bedc619ec
parent 3bb143b6f4a7e88cc04337d674b7c0b4c9c0a6de
Author: Daniel Barlow <dan@telent.net>
Date: Thu, 15 Sep 2022 22:17:18 +0100
add fn "command" which combines tx and expect
... also has better (any) error handling
Diffstat:
| M | sms.fnl | | | 42 | ++++++++++++++++-------------------------- |
1 file changed, 16 insertions(+), 26 deletions(-)
diff --git a/sms.fnl b/sms.fnl
@@ -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 ">")