1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#!/usr/bin/env tclsh
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
# text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2025 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################
source [file join [file dirname [info script]] .. brltty-prologue.tcl]
set serialProperties [dict create]
set devicePath "/dev/ttyS0"
proc getSerialPropertyNames {} {
global serialProperties
return [dict keys $serialProperties]
}
proc isSerialPropertyDefined {name} {
return [lcontain [getSerialPropertyNames] $name]
}
proc getSerialPropertyLabel {name} {
global serialProperties
return [dict get $serialProperties $name label]
}
proc getSerialPropertyChoices {name} {
global serialProperties
return [dict get $serialProperties $name choices]
}
proc getSerialPropertyOptions {name} {
global serialProperties
return [dict get $serialProperties $name options]
}
proc getSerialPropertyDefault {name} {
global serialProperties
return [dict get $serialProperties $name default]
}
proc getSerialPropertySetting {name} {
global serialProperties
return [dict get $serialProperties $name setting]
}
proc verifySerialPropertyChoice {name value} {
if {![lcontain [getSerialPropertyChoices $name] $value]} {
return -code error "unknown serial [getSerialPropertyLabel $name]: $value"
}
}
proc setSerialPropertyField {name field value} {
global serialProperties
dict set serialProperties $name $field $value
}
proc defineSerialProperty {name label default args} {
if {[isSerialPropertyDefined $name]} {
return -code error "serial property already defined: $name"
}
global serialProperties
dict set serialProperties $name [dict create label $label choices $args options [dict create]]
verifySerialPropertyChoice $name $default
setSerialPropertyField $name default $default
setSerialPropertyField $name setting $default
}
proc addSerialOption {option property args} {
if {![isSerialPropertyDefined $property]} {
return -code error "serial property not defined: $property"
}
global serialProperties
foreach value $args {
verifySerialPropertyChoice $property $value
}
dict set serialProperties $property options $option $args
}
defineSerialProperty lineMode "mode" local local modem
defineSerialProperty lineBaud "baud" 38400 50 75 110 134 150 200 300 600 1200 1800 2400 4800 9600 19200 38400 57600 115200 230400 460800 500000 576000 921600 1000000 1152000 1500000 2000000 2500000 3000000 3500000 4000000
defineSerialProperty flowControl "flow control" no no hardware software
defineSerialProperty dataParity "parity" none none odd even mark space
defineSerialProperty dataBits "data bits" 8 5 6 7 8
defineSerialProperty stopBits "stop bits" 1 1 2
addSerialOption clocal lineMode local
addSerialOption crtscts flowControl hardware
addSerialOption ixon flowControl software
addSerialOption parenb dataParity odd even mark space
addSerialOption parodd dataParity odd mark
addSerialOption cmspar dataParity mark space
addSerialOption cstopb stopBits 2
proc logSerialConfiguration {} {
global devicePath
set log "serial cofiguration: $devicePath"
foreach name [getSerialPropertyNames] {
append log ", [getSerialPropertySetting $name]"
append log " [getSerialPropertyLabel $name]"
}
logMessage task $log
}
proc configureSerialProperty {name value} {
verifySerialPropertyChoice $name $value
setSerialPropertyField $name setting $value
}
proc verifySerialDevice {path} {
foreach {test word} {exists "found" readable "readable" writable "writable"} {
if {![file $test $path]} {
semanticError "device not $word: $path"
}
}
if {![string equal [file type $path] characterSpecial]} {
semanticError "not a serial device: $path"
}
}
proc configureSerialDevice {{options {-g}}} {
global devicePath
set command [list]
lappend command stty -F $devicePath
eval lappend command $options
logNote "command: [join $command " "]"
lvarpush command exec
lappend command 2>@ stderr
if {[catch $command response] != 0} {
exit 9
}
return $response
}
proc addSettingUsage {linesVariable label specification default} {
upvar 1 $linesVariable lines
lappend lines "To set the $label, specify $specification (the default is $default)."
}
proc getArgumentsUsageSummary {} {
set lines [list]
global devicePath
addSettingUsage lines "port" "the absolute path to the corresponding serial device" $devicePath
foreach name [getSerialPropertyNames] {
set label [getSerialPropertyLabel $name]
set choices [getSerialPropertyChoices $name]
set default [getSerialPropertyDefault $name]
addSettingUsage lines $label [formatChoicesPhrase $choices] $default
}
return $lines
}
proc parseCommandLine {} {
set optionDefinitions {
}
processProgramArguments optionValues $optionDefinitions argumentValues "\[setting ...\]" getArgumentsUsageSummary
return $argumentValues
}
proc getRequestedConfiguration {} {
set configuration [list raw -echo -echoe -echok -echonl]
lappend configuration [getSerialPropertySetting lineBaud]
lappend configuration cs[getSerialPropertySetting dataBits]
foreach property [getSerialPropertyNames] {
set setting [getSerialPropertySetting $property]
set options [getSerialPropertyOptions $property]
foreach option [dict keys $options] {
if {![lcontain [dict get $options $option] $setting]} {
set option "-$option"
}
lappend configuration $option
}
}
return $configuration
}
foreach argument [parseCommandLine] {
if {[cequal [file pathtype $argument] absolute]} {
set devicePath $argument
} else {
set found 0
foreach name [getSerialPropertyNames] {
if {[lcontain [getSerialPropertyChoices $name] $argument]} {
set found 1
configureSerialProperty $name $argument
break
}
}
if {!$found} {
syntaxError "unrecognized serial property: $argument"
}
}
}
verifySerialDevice $devicePath
logSerialConfiguration
set originalConfiguration [configureSerialDevice]
set requestedConfiguration [getRequestedConfiguration]
try {
configureSerialDevice $requestedConfiguration
try {
withChannel deviceChannel [open $devicePath {RDWR NOCTTY NONBLOCK}] {
fconfigure $deviceChannel -buffering none -translation binary
fileevent $deviceChannel readable {
foreach byte [split [read $deviceChannel] ""] {
scan $byte %c number
puts -nonewline stdout [format "%02x " $number]
}
flush stdout
}
fileevent stdin readable {
if {[set length [gets stdin line]] == 0} {
set exitStatus 0
} elseif {$length > 0} {
puts -nonewline $deviceChannel [subst -nocommands -novariables $line]
flush $deviceChannel
} elseif {[eof stdin]} {
set exitStatus 0
}
}
vwait exitStatus
}
} trap {POSIX} {problem} {
semanticError $message
}
} finally {
configureSerialDevice $originalConfiguration
}
exit $exitStatus
|