From 501312d600fb63700bfad3a87b9508eee5354614 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Fri, 16 Sep 2022 17:13:43 +0100 Subject: [PATCH] allow :smsc and :device params to sms module --- handler.fnl | 12 +++++++++--- sms.fnl | 48 +++++++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/handler.fnl b/handler.fnl index 1560e7a..4f0ace2 100644 --- a/handler.fnl +++ b/handler.fnl @@ -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)] diff --git a/sms.fnl b/sms.fnl index 827b250..2aa9726 100644 --- a/sms.fnl +++ b/sms.fnl @@ -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) + (doto fd + (tcsetattr 0 termios) + (tcdrain) - (tcdrain fd) + (command "AT") + (command "AT&F") ; revert to defaults + (command "ATE0") ; disable command echo + (command "AT+CMEE=1") ;print CME errors + (command (.. "AT+CSCA=\"" smsc "\",145\r\n")) ;set SMSC + (command "AT+CMGF=0")) ;SMS PDU mode - (let [pdu (message->pdu number (unicode->gsm body)) - payload (.. "00" pdu)] - (doto fd - (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 + {:send send-message :device device :smsc smsc :fd fd})) - (tx (.. "AT+CMGS=" (string.format "%d" (/ (# pdu) 2)) "\r\n")) - (expect ">" "ERROR") - (tx payload) - (tx "\026\r\n") - (expect "OK"))))) - -{ :send send-sms } +{ :new new-sender :verbose false }