allow :smsc and :device params to sms module

main
Daniel Barlow 2022-09-16 17:13:43 +01:00
parent 02ea16374c
commit 501312d600
2 changed files with 32 additions and 28 deletions

View File

@ -1,12 +1,18 @@
(local json (require :dkjson))
(local unistd (require :posix.unistd))
(local sms (require :sms))
(local sms
((. (require :sms) :new)
{
:smsc "+447958879879"
:device "/dev/serial/by-id/usb-HUAWEI_HUAWEI_HiLink-if00-port0"
:verbose true
}))
(fn send-sms [body]
(print :send-sms body)
(sms.send "+447000123456" body)
"OK")
(sms:send "447000123456" body)
"Sent")
(fn handle [s]
(let [(m pos err) (json.decode s 1 nil)]

40
sms.fnl
View File

@ -18,8 +18,6 @@
(assert false (.. "expected " ,(view expr) " = " e#
", actual " a#)))))
(test= (+ 5 4) 9)
(fn escape-readably [s]
(s:gsub "%c" (fn [x] (string.format "\\u{%.3x}" (string.byte x)))))
@ -46,9 +44,6 @@
(test= (unicode->gsm "abc123") "abc123")
(test= (unicode->gsm "abc{123}") "abc\x1b(123\x1b)")
;(print (escape-readably(unicode-to-gsm "hello")))
;(print (escape-readably(unicode-to-gsm "{he@llo}€")))
(fn expect [fd pattern fail-pattern]
(let [b (read fd 1024)]
(if (> (# b) 0)
@ -147,8 +142,18 @@
(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)
(fn send-message [{: fd} number body]
(let [pdu (message->pdu number (unicode->gsm body))
payload (.. "00" pdu)]
(doto fd
(tx (.. "AT+CMGS=" (string.format "%d" (/ (# pdu) 2)) "\r\n"))
(expect ">" "ERROR")
(tx payload)
(tx "\026\r\n")
(expect "OK"))))
(fn new-sender [{: device : smsc : verbose}]
(let [fd (fcntl.open device posix.O_RDWR)
termios (tcgetattr fd)]
(tset termios :cflag (bor termios.cflag CLOCAL CREAD))
(tset termios :lflag (band termios.lflag
@ -157,24 +162,17 @@
(bnot ECHOE)
(bnot ISIG)))
(tset termios :oflag (band termios.oflag ( bnot OPOST)))
(tcsetattr fd 0 termios)
(tcdrain fd)
(let [pdu (message->pdu number (unicode->gsm body))
payload (.. "00" pdu)]
(doto fd
(tcsetattr 0 termios)
(tcdrain)
(command "AT")
(command "AT&F") ; revert to defaults
(command "ATE0") ; disable command echo
(command "AT+CMEE=1") ;print CME errors
(command "AT+CSCA=\"+447958879879\",145\r\n") ;set SMSC
(command "AT+CMGF=0") ;SMS PDU mode
(command (.. "AT+CSCA=\"" smsc "\",145\r\n")) ;set SMSC
(command "AT+CMGF=0")) ;SMS PDU mode
(tx (.. "AT+CMGS=" (string.format "%d" (/ (# pdu) 2)) "\r\n"))
(expect ">" "ERROR")
(tx payload)
(tx "\026\r\n")
(expect "OK")))))
{:send send-message :device device :smsc smsc :fd fd}))
{ :send send-sms }
{ :new new-sender :verbose false }