;;; -*- Mode:LISP; Package:KERMIT; Readtable:CL; Base:10 -*-

;;;Lambda SDU serial stuff

;;;By default, pick this serial port

(defvar *sdu-serial-default-device-name* "SDU-SERIAL-B")

;;;Modify these to (re)init serial port on open with parameters:

(defvar *sdu-serial-xon-xoff-p* T
  "If non-NIL, serial I/O uses software (Xon/Xoff) flow control")

(defvar *sdu-serial-ascii-p* NIL
  "If non-NIL, serial I/O does remote Ascii <--> local LISPM character conversion")

(defvar *sdu-serial-default-baud-rate* 9600.
  "Baud rate to set in SDU-SERIAL-OPEN")

;;;Make a list of device names and suitable open forms:

(defun gather-sdu-serial-devices()
  (loop for p in fs:*pathname-host-list*
	when (typep p 'si:sdu-serial-b-shared-device)
	collect `(,(send p :name) (sdu-serial-open ,(send p :name)))))

(defvar *sdu-serial-device-alist*
	(gather-sdu-serial-devices)
  "An a-list of SDU-SERIAL-B-SHARED-DEVICE type devices.
The CADR of each entry is a form suitable for opening the associated device.")

;;;This is the open function:

(defun sdu-serial-open (&optional
			(name *sdu-serial-default-device-name*)
			(new-baud-rate *sdu-serial-default-baud-rate*))
  (unless (member name *sdu-serial-device-alist* :test #'string-equal :key #'car)
    (cerror "Proceed to open ~s anyway"
	    "~s is not the name of a known serial port shared device / pathname host" name))
  (let ((device (make-pathname :host name)))
    (check-type device si:shared-device-pathname)
    (open device
	  :flavor-and-init-options
	  (list
	    (si:combined-sdu-serial-stream-flavor :ascii    *sdu-serial-ascii-p*
						  :xon-xoff *sdu-serial-xon-xoff-p*)
	    :input-buffer-size (* 3 si:page-size)
	    :output-buffer-size (* 2 si:page-size)
	    :baud-rate new-baud-rate))))
