blob: 4c1e9e296ea27da666c89940f2ac7cbaccd58248 [file] [log] [blame]
mbligh7e176f62007-12-17 17:58:08 +00001#!/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 <James.Puderer@windriver.com>
18#
19###############################################################################
20
21# This script depends on the ckermit package.
22
23##### Configuration #####
24set tty_line /dev/ttyUSB0
25set tty_baud 9600
26set reset_delay 5000
27#########################
28log_user 0
29
30# Triggers alarm relay when over a large negative amperage (always true)
31set command(relay1_on) "*1F386800000\r"
32set command(relay2_on) "*1F389800000\r"
33
34# Triggers alarm relay when under a large positive amperage (always false)
35set command(relay1_off) "*1F3867FFFFF\r"
36set command(relay2_off) "*1F3897FFFFF\r"
37
38set command(get_value) "*1B1\r"
39
40proc usage { } {
41 global argv0
42 puts stderr "Usage: $argv0 <ttydev> <on|off|reset|value>\n"
43 exit 1
44}
45
46proc 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
71proc disconnect { } {
72 global spawn_id
73 close
74}
75
76proc on { } {
77 global spawn_id command
78 send $command(relay1_on)
79 after 100
80 send $command(relay2_on)
81 after 100
82}
83
84proc off { } {
85 global spawn_id command
86 send $command(relay1_off)
87 after 100
88 send $command(relay2_off)
89 after 100
90}
91
92proc 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
104proc 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
116if {[llength $argv] < 2} {
117 usage
118}
119set tty_line [lindex $argv 0]
120set action [string tolower [lindex $argv 1]]
121
122if { ($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