Debian Codegraph @

drivers/reboot-laurel

raw
1 #!/usr/bin/expect
2 ###############################################################################
3 # reboot-laurel -- Reboot a system attached to a Laurel DPM meter.
4 #
5 # This script provides standalone control for the Laurel DPM ammeter when
6 # setup to control power to a target using the alarm relays. This is actually
7 # a creative misuse of the meter. We are actually sending commands to the
8 # meter that trigger alarm relays to be "always on" or "always off".
9 #
10 # In addition to controlling power to a target, the meter will report the
11 # current ammeter reading using the 'value' command.
12 #
13 # Usage:
14 # reboot-laurel <ttydev> <on|off|reset|value>
15 #
16 # Copyright (c) 2007, Wind River Systems
17 # Author: James Puderer <[email protected]>
18 #
19 ###############################################################################
20
21 # This script depends on the ckermit package.
22
23 ##### Configuration #####
24 set tty_line /dev/ttyUSB0
25 set tty_baud 9600
26 set reset_delay 5000
27 #########################
28 log_user 0
29
30 # Triggers alarm relay when over a large negative amperage (always true)
31 set command(relay1_on) "*1F386800000\r"
32 set command(relay2_on) "*1F389800000\r"
33
34 # Triggers alarm relay when under a large positive amperage (always false)
35 set command(relay1_off) "*1F3867FFFFF\r"
36 set command(relay2_off) "*1F3897FFFFF\r"
37
38 set command(get_value) "*1B1\r"
39
40 proc usage { } {
41 global argv0
42 puts stderr "Usage: $argv0 <ttydev> <on|off|reset|value>\n"
43 exit 1
44 }
45
46 proc connect { tty_line tty_baud } {
47 global spawn_id
48
49 spawn kermit -Y
50 expect {
51 "C-Kermit>" { }
52 timeout { error "Timeout starting kermit" }
53 }
54 send "set line $tty_line\n"
55 send "set speed $tty_baud\n"
56 expect {
57 "$tty_baud bps" { }
58 "Sorry, device is in use" { error "Serial device is in use" }
59 timeout { error "Kermit timed out" }
60 }
61 send "set flow none\n"
62 send "set carrier-watch off\n"
63 send "c\n"
64 expect {
65 "Port already in use" { error "Port already in use" }
66 "\n-------*\n" { }
67 timeout { error "Kermit timed out" }
68 }
69 }
70
71 proc disconnect { } {
72 global spawn_id
73 close
74 }
75
76 proc on { } {
77 global spawn_id command
78 send $command(relay1_on)
79 after 100
80 send $command(relay2_on)
81 after 100
82 }
83
84 proc off { } {
85 global spawn_id command
86 send $command(relay1_off)
87 after 100
88 send $command(relay2_off)
89 after 100
90 }
91
92 proc reset { } {
93 global spawn_id reset_delay command
94 send $command(relay1_off)
95 after 100
96 send $command(relay2_off)
97 after $reset_delay
98 send $command(relay1_on)
99 after 100
100 send $command(relay2_on)
101 after 100
102 }
103
104 proc value { } {
105 global spawn_id command
106 set timeout 1
107 expect "*"
108 send $command(get_value)
109 expect {
110 -re {[-| ][0-9][0-9]\.[0-9][0-9][0-9]} { return $expect_out(0,string) }
111 }
112 }
113
114
115 # Handle command line
116 if {[llength $argv] < 2} {
117 usage
118 }
119 set tty_line [lindex $argv 0]
120 set action [string tolower [lindex $argv 1]]
121
122 if { ($action == "on") || ($action == "off") || ($action == "reset") } {
123 connect $tty_line $tty_baud
124 $action
125 disconnect
126 } elseif { $action == "value" } {
127 connect $tty_line $tty_baud
128 puts [$action]
129 disconnect
130 } else {
131 usage
132 }
133