libBB.expect
changeset 4 fda11de1826e
parent 3 99a41e0476b1
child 5 f41d45fe7ae9
equal deleted inserted replaced
3:99a41e0476b1 4:fda11de1826e
     1 #!/usr/bin/expect -f
       
     2 #======================================================================
       
     3 #                    L I B B B . E X P E C T 
       
     4 #                    doc: Mon Mar  8 02:57:35 2004
       
     5 #                    dlm: Thu Nov 19 14:48:52 2009
       
     6 #                    (c) 2004 A.M. Thurnherr
       
     7 #                    uE-Info: 41 54 NIL 0 0 72 2 2 8 NIL ofnI
       
     8 #======================================================================
       
     9 
       
    10 # HISTORY:
       
    11 # Mar  8, 2004: - incepted (NBP0402)
       
    12 # Mar 15, 2004: - released
       
    13 # Mar 18, 2004: - improved download-error handling
       
    14 # 		- BUG: set correct color in toggle_instrument
       
    15 # 		- new reset_two_instruments
       
    16 # Mar 20, 2004: - increased instrument-toggle timeout to 2s
       
    17 # 		- cleaned-up get_deployment_name
       
    18 #		- added save_hardware_config to upload_file (just before CS)
       
    19 #		- renamed continue_logging to resume_logging
       
    20 # Mar 21, 2004: - debugged user-interaction routines
       
    21 # 		- generally increased timeouts
       
    22 # Apr  4, 2004: - added comments
       
    23 # Jun 15, 2004: - changed \n to \r for BB150 (CLIVAR P02)
       
    24 #		- started adaptation to BB150
       
    25 # Jun 18, 2004: - BUG: could not handle station 008 (illegal octal digit)
       
    26 # Jun 27, 2004: - doubled wait_for_startup timeouts
       
    27 # Sep 28, 2006: - added sleep 1 to wait_for_startup (marked AMARYLIS)
       
    28 # Oct 30, 2006: - improved error messages
       
    29 # Nov  6, 2006: - added ensure_sleep
       
    30 #		- BUG: input_int had interpreted 010 as octal
       
    31 # Nov 15, 2006: - added next_speed
       
    32 # Nov 17, 2006: - added get_serial_number
       
    33 # 		- added save_parameters
       
    34 # Oct 22, 2007: - removed erroneous line of code (that did nothing)
       
    35 # 		- changed expect target in next_speed()
       
    36 # Oct 23, 2007: - cosmetics
       
    37 # Nov 19, 2008: - added load_factory_defaults()
       
    38 #		- added print_version()
       
    39 # Oct 12, 2009: - BUG: load_factory_defaults errmesg used non-existent variable
       
    40 # Nov 19, 2009: - get_deployment_name needed a delay (sleep 1) for the
       
    41 #		  instruments sent to the DIMES cruise
       
    42 
       
    43 # NOTE: after having written this, I am not much of a fan of TCL and and
       
    44 #	expect(1) syntax any more...
       
    45 
       
    46 # PS0 ON BB150 NOTE:
       
    47 #	I AM NOT SURE ABOUT THE FOLLOWING ANY MORE. THE CURRENT CODE
       
    48 #	MAY THEREFORE NOT RUN WITH BB150s!!!
       
    49 #  PS0 does not produce any output on the BB150 unless the factory 
       
    50 #  defaults are in effect. (I have not checked exactly which user defaults
       
    51 #  cause this behaviour.) Therefore, every time before PS0 is sent
       
    52 #  to the instrument, the current values are stored, the factory
       
    53 #  defaults restored, PS0 is sent, and then the user values
       
    54 #  are restored. For safety, I removed PS0 from save_hardware_config
       
    55 #  code for the BB150; because it would be really bad if the factory
       
    56 #  defaults would be restored immediately before pinging starts...
       
    57 #  The behaviour was detected on CPU firmware 5.52.
       
    58 
       
    59 set NEXT_SPEED	\002;	# ^B				# bbabble commands
       
    60 set BREAK 	\003;	# ^C
       
    61 set HELP	\010;	# ^H
       
    62 set TOGGLE	\024;	# ^T
       
    63 set DNLOAD	\030;	# ^X
       
    64 
       
    65 set cid -1;						# current instrument
       
    66 
       
    67 proc print_version {} {
       
    68 	send_user "acquire V1.4beta\n"
       
    69 }
       
    70 
       
    71 proc set_color {{cid -1}} {				# set instrument color
       
    72 	if {$cid == 0} {
       
    73 		send_user [exec tput setaf 1];
       
    74 	} elseif {$cid == 1} {
       
    75 		send_user [exec tput setaf 4];
       
    76 	} else {
       
    77 		send_user [exec tput setaf 0];
       
    78 	}
       
    79 }
       
    80 
       
    81 proc error {msg} {					# print error & exit
       
    82 	global cid;
       
    83 	set_color $cid;
       
    84 	send_error "\nError: $msg\n";
       
    85 	set_color;
       
    86 	exit 1;
       
    87 }
       
    88 
       
    89 proc gobble_prompts {} {				# gobble old prompts
       
    90 	set continue_flushing 1;
       
    91 	while {$continue_flushing > 0} {
       
    92 		expect {
       
    93 			timeout {set continue_flushing 0;}
       
    94 			-timeout 0 ">";
       
    95 		}
       
    96 	}
       
    97 }
       
    98 
       
    99 proc BREAK {} {						# send BREAK
       
   100 	global BREAK;
       
   101 
       
   102 	gobble_prompts;
       
   103 	send $BREAK;					# Wakeup Instrument
       
   104 	expect {
       
   105 		timeout {error "Can't wake Instrument"}
       
   106 		-timeout 8 "RD Instruments (c)"
       
   107 	}
       
   108 	expect {					# gobble prompt as well
       
   109 		timeout {error "Can't get wakeup prompt"}
       
   110 		-timeout 3 ">"
       
   111 	}
       
   112 }
       
   113 
       
   114 proc no_error_BREAK {} {				# send BREAK without errcheck
       
   115 	global BREAK;
       
   116 
       
   117 	gobble_prompts;
       
   118 	send $BREAK;
       
   119 	expect -timeout 8 "RD Instruments (c)"
       
   120 	expect -timeout 3 ">"
       
   121 }
       
   122 
       
   123 proc next_speed {} {					# select next baudrate
       
   124 	global NEXT_SPEED;
       
   125 	
       
   126 	send $NEXT_SPEED;
       
   127 	expect {
       
   128 		timeout {error "Cannot change speed"}
       
   129 		-timeout 5 ": end"
       
   130 	}
       
   131 	sleep 1
       
   132 }
       
   133 
       
   134 proc toggle_instrument {} {				# select other instrument
       
   135 	global cid TOGGLE;
       
   136 	send $TOGGLE;					# switch
       
   137 	expect {
       
   138 		timeout {error "Can't switch instruments"}
       
   139 		-timeout 3 "Instrument"
       
   140 	}
       
   141 	if {$cid == 0} {set cid 1} else {set cid 0}
       
   142 }
       
   143 
       
   144 proc select_master {{toggle_first 1}} {			# select master & check S/N
       
   145 	global HELP cid master_sn slave_sn;
       
   146 
       
   147 	if {$toggle_first} toggle_instrument;
       
   148 	gobble_prompts;
       
   149 	send "\r"; wait_for_prompt;			# save current value
       
   150 	send "PS0\r";
       
   151 	expect {
       
   152 		timeout {error "Can't determine serial number"}
       
   153 		-timeout 8 -re "Instrument S/N:  $master_sn.*>|Xducer Ser #:  $master_sn.*>|PS0...>" {}
       
   154 		-timeout 8 -re "Instrument S/N:  $slave_sn.*>" {
       
   155 			toggle_instrument;
       
   156 			send "PS0\r";
       
   157 			expect {
       
   158 				timeout {error "Can't determine serial number"}
       
   159 				-timeout 8 -re "Instrument S/N:  $master_sn.*>|Xducer Ser #:  $master_sn.*>|PS0...>" {}
       
   160 				-timeout 8 -re "Instrument S/N:  $slave_sn.*>" {
       
   161 					error "Can't switch to master"
       
   162 				}
       
   163 			}
       
   164 		}
       
   165 	}
       
   166 	set cid 0;
       
   167 }
       
   168 
       
   169 proc select_slave {{toggle_first 1}} {			# select slave & check S/N
       
   170 	global HELP cid master_sn slave_sn;
       
   171 
       
   172 	if {$toggle_first} toggle_instrument;
       
   173 	gobble_prompts;
       
   174 	send "\r"; wait_for_prompt;			# save current value
       
   175 	send "PS0\r";
       
   176 	expect {
       
   177 		timeout {error "Can't determine master/slave"}
       
   178 		-timeout 8 -re "Instrument S/N:  $slave_sn.*>" {}
       
   179 		-timeout 8 -re "Instrument S/N:  $master_sn.*>|Xducer Ser #:  $master_sn.*>|PS0...>" {
       
   180 			toggle_instrument;
       
   181 			send "PS0\r";
       
   182 			expect {
       
   183 				timeout {error "Can't determine master/slave"}
       
   184 				-timeout 8 -re "Instrument S/N:  $slave_sn.*>" {}
       
   185 				-timeout 8 -re "Instrument S/N:  $master_sn.*>|Xducer Ser #:  $master_sn.*>|PS0...>" {
       
   186 					error "Can't switch to slave"
       
   187 				}
       
   188 			}
       
   189 		}
       
   190 	}
       
   191 	set cid 1;
       
   192 }
       
   193 
       
   194 proc wait_for_startup {} {			# wait for bbabble startup
       
   195 	global cid;
       
   196 	expect {
       
   197 		timeout {error "bbabble failed to start up"}
       
   198 		-timeout 10 "device" {
       
   199 			error "No such device: cables plugged in???"
       
   200 		}
       
   201 		-timeout 10 "Instrument 0" {}
       
   202 	}
       
   203 	set cid 0;
       
   204 	sleep 1;				# AMARYLIS
       
   205 }
       
   206 
       
   207 proc reset_instrument {} {			# reset regardless of state
       
   208 	no_error_BREAK;
       
   209 	send "\r";				# send NOP command
       
   210 	expect {
       
   211 		timeout {
       
   212 			send_error ".";
       
   213 			BREAK;			# try again
       
   214 		}
       
   215 		-timeout 5 ">";			# instrument reacted
       
   216 	}
       
   217 }
       
   218 
       
   219 proc reset_two_instruments {} {			# reset regardless of state
       
   220 	set ok 0;
       
   221 ###	toggle_instrument;
       
   222 	while {$ok < 2} {
       
   223 		no_error_BREAK;
       
   224 		send "\r"; expect {
       
   225 			timeout {
       
   226 				set ok 0;
       
   227 				send_error "!";
       
   228 			}
       
   229 			-timeout 5 ">" {incr ok}
       
   230 		}
       
   231 		toggle_instrument;
       
   232 	}
       
   233 	select_master;
       
   234 }
       
   235 
       
   236 proc wait_for_prompt {{tout 5}} {		# wait for prompt
       
   237 	expect {
       
   238 		timeout {error "Can't get prompt"}
       
   239 		-timeout $tout "ERR" {
       
   240 			error "Instrument returned error $expect_out(buffer)";
       
   241 		}
       
   242 		-timeout $tout ">"
       
   243 	}
       
   244 }
       
   245 
       
   246 proc put_to_sleep {} {				# send instrument to sleep
       
   247 	gobble_prompts;
       
   248 	send "\r"; wait_for_prompt;
       
   249 	send "CZ\r";
       
   250 	expect {
       
   251 		timeout {error "Can't put Instrument to sleep"}
       
   252 		-timeout 5 -re "Powering Down|POWERING DOWN"
       
   253 	}
       
   254 }
       
   255 
       
   256 proc save_parameters {} {			# save parameters
       
   257 	gobble_prompts;
       
   258 	send "\r"; wait_for_prompt;
       
   259 	send "CK\r";
       
   260 	expect {
       
   261 		timeout {error "Can't put Instrument to sleep"}
       
   262 		-timeout 5 -re "USER defaults"
       
   263 	}
       
   264 }
       
   265 
       
   266 proc ensure_sleep {} {				# send instrument to sleep...
       
   267 	gobble_prompts;				# unless already sleeping
       
   268 	send "CZ\r";
       
   269 	expect {
       
   270 		timeout {error "Can't put Instrument to sleep"}
       
   271 		-timeout 5 -re "Powering Down|POWERING DOWN"
       
   272 	}
       
   273 }
       
   274 
       
   275 proc start_download {} {			# start downloading
       
   276 	global DNLOAD;
       
   277 
       
   278 	gobble_prompts;
       
   279 	send "\r"; wait_for_prompt;
       
   280 	send $DNLOAD;
       
   281 	expect {
       
   282 		timeout {error "Can't start download (memory empty?)"}
       
   283 		-timeout 10 "Receiving:"
       
   284 	}
       
   285 }
       
   286 
       
   287 proc wait_for_download_finish {stn tout} {	# wait for timeout to finish
       
   288 	global BREAK TOGGLE;
       
   289 	expect {
       
   290 		timeout {error "Download did not finish ok"}
       
   291 		-timeout $tout "Transfer complete" {}
       
   292 		-timeout $tout "ABNORMALLY" {
       
   293 			send $BREAK; send $TOGGLE; send $BREAK;
       
   294 			error "Download did not finish ok";
       
   295 		}
       
   296 	}
       
   297 	expect -timeout 5 ">";
       
   298 
       
   299 	global cid master_download_filename slave_download_filename;
       
   300 	global master_deployment_name_fmt slave_deployment_name_fmt;
       
   301 	if {$cid == 0} {
       
   302 		rename_master_download_file [format $master_deployment_name_fmt $stn]000.000;
       
   303 	} else {
       
   304 		rename_slave_download_file [format $slave_deployment_name_fmt $stn]000.000;
       
   305         }
       
   306 }
       
   307 
       
   308 proc list_dir {} {				# list directory
       
   309 	global cid;
       
   310 	gobble_prompts;
       
   311 	send "\r"; wait_for_prompt;
       
   312 	send "RR\r";
       
   313 	expect {
       
   314 		-timeout 1 "ERR" {		# BB150
       
   315 			wait_for_prompt;
       
   316 			send "RS\r";
       
   317 			log_user 1;
       
   318 		}
       
   319 		-timeout 5 "Recorder Directory:";
       
   320 	}
       
   321 	set_color $cid; log_user 1; 
       
   322 	wait_for_prompt;
       
   323 	log_user 0; set_color;
       
   324 }
       
   325 
       
   326 proc erase_memory {} {				# erase memory
       
   327 	gobble_prompts;
       
   328 	send "\r"; wait_for_prompt;
       
   329 	send "RE ErAsE\r";
       
   330 	expect {
       
   331 		timeout {error "Can't erase recorder"}
       
   332 		-timeout 5 "Erasing..." {	# BB150
       
   333 			sleep 3;
       
   334 			wait_for_prompt;
       
   335 			return;
       
   336 		}
       
   337 		-timeout 5 "Recorder erased."	# Workhorse
       
   338 	}
       
   339 	wait_for_prompt;
       
   340 }
       
   341 
       
   342 proc save_hardware_config {filename} {		# NB: logging continues in ...
       
   343 	gobble_prompts;				# ... file given as parameter
       
   344 	send "\r"; wait_for_prompt;
       
   345 	send "TT?\r";				# BB150
       
   346 	expect -timeout 1 "ERR" {
       
   347 		wait_for_prompt;
       
   348 		set time [exec date +%y/%m/%d,%H:%M:%S];
       
   349 		set pipe [open "| tr -d \\\r \
       
   350 				| sed -e s@Time\\ Set.*@LOGGING\\ COMPUTER:\\ $time@ \
       
   351 				      -e /^>/d \
       
   352 				> $filename" w];
       
   353 		log_file; log_file -a -open $pipe;
       
   354 		send "T?\r"; wait_for_prompt;
       
   355 		send "RA?\r"; wait_for_prompt;
       
   356 		send "RS?\r"; wait_for_prompt;
       
   357 		send "W?\r"; wait_for_prompt;
       
   358 		send "B?\r"; wait_for_prompt;
       
   359 		send "E?\r"; wait_for_prompt;
       
   360 		send "CG?\r"; wait_for_prompt;
       
   361 		send "CL?\r"; wait_for_prompt;
       
   362 		send "CP?\r"; wait_for_prompt;
       
   363 		send "CQ?\r"; wait_for_prompt;
       
   364 		send "CT?\r"; wait_for_prompt;
       
   365 		send "CX?\r"; wait_for_prompt;
       
   366 		send "PS3\r"; wait_for_prompt;
       
   367 		return;
       
   368 	}
       
   369 	set time [exec date +%Y/%m/%d,%H:%M:%S];
       
   370 	set pipe [open "| tr -d \\\r \
       
   371 			| sed -e s/^Press\\ any\\ key\\ to\\ continue\\ *// \
       
   372 			      -e s@Time\\ Set.*@LOGGING\\ COMPUTER:\\ $time@ \
       
   373 			> $filename" w];
       
   374 	log_file;
       
   375 	log_file -a -open $pipe;
       
   376 	send "TT?\r"; wait_for_prompt;
       
   377 	send "RF\r"; wait_for_prompt;
       
   378 	send "RR\r"; wait_for_prompt;
       
   379 	send "PS0\r"; wait_for_prompt;
       
   380 	send "PS3\r"; wait_for_prompt;
       
   381 	send "AC\r"; wait_for_prompt;
       
   382 	send "RN?\r"; wait_for_prompt;
       
   383 	send "DEPLOY?\r";
       
   384 	expect {
       
   385 		timeout {error "Can't get DEPLOY? output"}
       
   386 		-timeout 10 "Press any key to continue"
       
   387 	}
       
   388 	send " "; wait_for_prompt;
       
   389 }
       
   390 
       
   391 proc start_logging {{filename "bbabble.log"}} {	# write UNIX conformant log file
       
   392 	set pipe [open "| tr -d \\\r \
       
   393 			> $filename" w];
       
   394 	log_file -a -open $pipe;
       
   395 }	
       
   396 	
       
   397 proc resume_logging {{filename "bbabble.log"}} {# append to UNIX conf. log file
       
   398 	set pipe [open "| tr -d \\\r \
       
   399 			>> $filename" w];
       
   400 	log_file;
       
   401 	log_file -a -open $pipe;
       
   402 }
       
   403 
       
   404 proc memory_empty {} {				# check whether memory is empty
       
   405 	gobble_prompts;
       
   406 	send "\r"; wait_for_prompt;
       
   407 	send "RA\r";
       
   408 	expect {
       
   409 		timeout {error "Can't get number of deployments"};
       
   410 		-timeout 3 -re {[1-9]} {
       
   411 			wait_for_prompt;
       
   412 			return 0;
       
   413 		}
       
   414 		-timeout 3 ">" {
       
   415 			return 1;
       
   416 		}
       
   417 	}
       
   418 }
       
   419 			
       
   420 proc set_instrument_clock {} {			# set instrument clock
       
   421 	gobble_prompts;
       
   422 	send "\r"; wait_for_prompt;
       
   423 	send "TT?\r";				# check for BB150
       
   424 	expect {
       
   425 		-timeout 1 "ERR" {
       
   426 			gobble_prompts;
       
   427 			set time [exec date +%y/%m/%d,%H:%M:%S];
       
   428 			send "TS$time\r";
       
   429 			wait_for_prompt;
       
   430 			return;
       
   431 		}
       
   432 	}
       
   433 	set time [exec date +%Y/%m/%d,%H:%M:%S];
       
   434 	send "TT$time\r";
       
   435 	wait_for_prompt;
       
   436 }
       
   437 
       
   438 proc upload_file {cfn lfn hwfn} {		# upload command file
       
   439 	gobble_prompts;				# logging stops in this routine
       
   440 	send "\r"; wait_for_prompt;
       
   441 	set cf [open $cfn "r"];
       
   442 	set lf [open $lfn "w"];
       
   443 	while {[gets $cf line] >= 0} {
       
   444 		regsub {[ 	]*;.*} $line {} line;
       
   445 		if {![regexp {^[ 	]*$} $line]} {
       
   446 			puts -nonewline $lf "$line ... ";
       
   447 			if {$line == "CS" || $line == "cs"} {
       
   448 				send_error "!";
       
   449 				save_hardware_config $hwfn;
       
   450 				send "$line\r";
       
   451 				expect {
       
   452 					timeout {};
       
   453 					-timeout 3 expect ">" {
       
   454 						error "Got prompt after CS command";
       
   455 					}
       
   456 				}
       
   457 				puts $lf "INSTRUMENT PINGING";
       
   458 				close $cf; close $lf;
       
   459 				log_file;
       
   460 				return;
       
   461 			}
       
   462 			send "$line\r";
       
   463 			send_error ".";
       
   464 			expect {
       
   465 				timeout {error "Can't get prompt after sending $line"};
       
   466 				-timeout 5 "ERR" {
       
   467 					error "Sending command $line returned $expect_out(buffer)";
       
   468 				}
       
   469 				-timeout 5 ">" {
       
   470 					puts $lf "OK";
       
   471 				}
       
   472 			}
       
   473 		}
       
   474 	}
       
   475 	close $cf; close $lf;
       
   476 }
       
   477 
       
   478 proc load_factory_defaults {} {				# set default params
       
   479 	gobble_prompts;
       
   480 	send "\r"; wait_for_prompt;
       
   481 	send "CR1\r";
       
   482 	expect {
       
   483 		timeout {error "Can't get prompt"};
       
   484 		-timeout 5 "ERR" {
       
   485 			error "command CR1 returned $expect_out(buffer)";
       
   486 		}
       
   487 		-timeout 5 ">"
       
   488         }
       
   489 }
       
   490 
       
   491 proc set_deployment_name {{dn UNSET}} {			# set deployment name
       
   492 	gobble_prompts;
       
   493 	send "\r"; wait_for_prompt;
       
   494 	send "RN$dn\r";
       
   495 	expect -timeout 1 "ERR";			# BB150
       
   496 	wait_for_prompt;
       
   497 }
       
   498 
       
   499 proc get_deployment_name {} {				# get deployment name
       
   500 	global stn_format master_deployment_name_fmt;
       
   501 	gobble_prompts;
       
   502 	send "\r"; wait_for_prompt;
       
   503 	send "RN?\r"; sleep 1;
       
   504 	expect {
       
   505 		timeout {error "Can't get deployment name";}
       
   506 		-timeout 1 "ERR" {			# BB150
       
   507 			if {![file isfile .last_stn]} {
       
   508 				error "Can't find .last_stn";
       
   509 			}
       
   510 			set stn [exec cat .last_stn];
       
   511 			set dn [format $master_deployment_name_fmt $stn];
       
   512 			wait_for_prompt;
       
   513 			return $dn;			
       
   514 		}
       
   515 		-timeout 5 -re {Current deployment name = ([^\r]*)}
       
   516 	}
       
   517 	set dn $expect_out(1,string);
       
   518 	wait_for_prompt;
       
   519 	return $dn;
       
   520 }
       
   521 	
       
   522 proc get_serial_number {} {				# get serial number
       
   523 	gobble_prompts;
       
   524 	send "\r"; wait_for_prompt;
       
   525 	send "PS0\r";
       
   526 	expect {
       
   527 		timeout {error "Can't get serial number";}
       
   528 		-timeout 5 -re {Instrument S/N: *([0-9]*)}
       
   529 	}
       
   530 	set sn $expect_out(1,string);
       
   531 	wait_for_prompt;
       
   532 	return $sn;
       
   533 }
       
   534 	
       
   535 #======================================================================
       
   536 # User-Interation Routines
       
   537 #======================================================================
       
   538 
       
   539 proc affirm {prompt {to -1} {timeout_return 0}} {	# yes/no question with timeout
       
   540 	send_user $prompt;				# and default answer
       
   541 	while {1} {
       
   542 		expect_user {
       
   543 			timeout {
       
   544 				if {$timeout_return == 0} {
       
   545 					send_user "N\n";
       
   546 				} else {
       
   547 					send_user "Y\n";
       
   548 				}
       
   549 				return $timeout_return;
       
   550 			}
       
   551 			-timeout $to -re "^y.*|^Y.*" 	{return 1;}
       
   552 			-timeout $to -re "^n.*|^N.*"	{return 0;}
       
   553 			-re {..*} {send_user "Please answer Y or N..."}
       
   554 		}
       
   555 	}
       
   556 }
       
   557 
       
   558 proc input_int {prompt} {				# input an integer
       
   559 	while {1} {
       
   560 		send_user $prompt;
       
   561 		expect_user {
       
   562 			-timeout -1 -re {^[0-9]+\n} {
       
   563 				regsub {^0*} $expect_out(0,string) {} decimal_val;
       
   564 				return $decimal_val;
       
   565 			}
       
   566 			-timeout -1 -re {.+} {send_user "only integers, please!\n"}
       
   567 		}
       
   568 	}
       
   569 }