You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.5 KiB
108 lines
2.5 KiB
5 years ago
|
#!/usr/bin/expect -f
|
||
|
|
||
|
proc setup {} {
|
||
|
set timeout 1
|
||
|
exp_internal 0
|
||
|
log_user 0
|
||
|
set ::env(DISPLAY) :0
|
||
|
spawn sh -c "screen -wipe"
|
||
|
spawn sh -c "screen -rx dialer || screen -S dialer /dev/ttyUSB2 115200"
|
||
|
sleep 0.3
|
||
|
set modem_pid $spawn_id
|
||
|
sleep 0.2
|
||
|
|
||
|
# Set audio routing
|
||
|
messagemodem $modem_pid "AT+QDAI=1,0,0,2,0,1,1,1;\r" 1
|
||
|
# Setup text message to immediatly show text response on +CMT recvs
|
||
|
messagemodem $modem_pid "AT+CNMI=1,2,0,1,0;\r" 1
|
||
|
# Set message format as PDU
|
||
|
messagemodem $modem_pid "AT+CMGF=0;\r" 1
|
||
|
|
||
|
return $modem_pid
|
||
|
}
|
||
|
|
||
|
proc messagemodem {modem_pid message fatal} {
|
||
|
send -i $modem_pid $message
|
||
|
expect {
|
||
|
-i $modem_pid "OK" {
|
||
|
return 1
|
||
|
}
|
||
|
}
|
||
|
if {$fatal} {
|
||
|
puts "Couldn't run command on modem: $message"
|
||
|
exit
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
set modem_pid [setup]
|
||
|
sleep 0.2
|
||
|
|
||
|
# E.g. consume the thing
|
||
|
expect -i $modem_pid eof
|
||
|
|
||
|
proc writenumber {number} {
|
||
|
exec sh -c "echo '$number' > /tmp/sxmo_incomingcall"
|
||
|
puts "Ring for incoming call: <$number>"
|
||
|
spawn sxmo_setpineled blue 0
|
||
|
sleep 0.3
|
||
|
}
|
||
|
|
||
|
proc ring {modem_pid} {
|
||
|
send -i $modem_pid "AT+CLCC\r"
|
||
|
expect {
|
||
|
-i $modem_pid -re {\n\+CLCC: 4,.+\+(\d+)} { writenumber $expect_out(1,string) }
|
||
|
-i $modem_pid -re {\n\+CLCC: 3} { writenumber "UNKNOWN" }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
proc textmsg {modem_pid pdutext} {
|
||
|
puts "New text msg - pdu text $pdutext"
|
||
|
spawn sxmo_pdudecode $pdutext
|
||
|
wait
|
||
|
expect -i $spawn_id -re {Date: ([\-0-9]+)\r\nNumber: \+?([^\n]+)\r\nMessage:\r\n(.+)$} { puts "Matched ok"; }
|
||
|
set date $expect_out(1,string)
|
||
|
set number $expect_out(2,string)
|
||
|
set msg [string trim $expect_out(3,string)]
|
||
|
|
||
|
puts "<date: $date>"
|
||
|
puts "<number: $number>"
|
||
|
puts "<msg: $msg>"
|
||
|
|
||
|
spawn date "+%Y/%m/%d %H:%M:%S" -d "@$date"
|
||
|
wait
|
||
|
expect
|
||
|
set prettydate [string trim $expect_out(buffer)]
|
||
|
|
||
|
set USER $::env(USER)
|
||
|
spawn mkdir -p "/home/$USER/.sxmo/$number"
|
||
|
wait
|
||
|
set fp "/home/$USER/.sxmo/$number/log.txt"
|
||
|
|
||
|
set logf [open $fp a]
|
||
|
puts $logf "$number at \[$prettydate\]:\n$msg\n"
|
||
|
close $logf
|
||
|
}
|
||
|
|
||
|
proc nocarrier {modem_pid} {
|
||
|
spawn sxmo_setpineled blue 0
|
||
|
exec sh -c "rm -f /tmp/sxmo_incomingcall"
|
||
|
puts "No carrier"
|
||
|
}
|
||
|
|
||
|
while 1 {
|
||
|
set is_ringing 0
|
||
|
set timeout 2
|
||
|
expect {
|
||
|
-i $modem_pid -re {\nRING\r} {ring $modem_pid; set is_ringing 1}
|
||
|
-i $modem_pid -re {\nNO CARRIER\r} {nocarrier $modem_pid; set is_ringing 0}
|
||
|
-i $modem_pid -re {\n\+CMT: \S+\r\n([0-9A-F]+)} {textmsg $modem_pid $expect_out(1,string)} }
|
||
|
if { $is_ringing } {
|
||
|
spawn sxmo_setpineled blue 1
|
||
|
} else {
|
||
|
spawn sxmo_setpineled blue 0
|
||
|
}
|
||
|
|
||
|
sleep 2
|
||
|
}
|