2004-06-21 22:39:25 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
2006-09-15 14:55:39 +00:00
|
|
|
|
* (c) 2004 Laurent Vivier <Laurent@lvivier.info>
|
2004-06-21 22:39:25 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-25 11:16:51 +00:00
|
|
|
|
#include <stdio.h>
|
2004-06-21 22:39:25 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2005-11-08 02:06:40 +00:00
|
|
|
|
#include <macos/types.h>
|
|
|
|
|
#include <macos/devices.h>
|
|
|
|
|
#include <macos/serial.h>
|
|
|
|
|
|
2004-06-21 22:39:25 +00:00
|
|
|
|
#include "misc.h"
|
2004-06-22 22:05:24 +00:00
|
|
|
|
#include "head.h"
|
2005-10-06 21:03:19 +00:00
|
|
|
|
#include "driver.h"
|
2005-11-27 22:31:07 +00:00
|
|
|
|
#include "config.h"
|
2004-06-22 22:05:24 +00:00
|
|
|
|
|
2005-08-30 08:03:50 +00:00
|
|
|
|
static short out_refnum0 = -1;
|
|
|
|
|
static short out_refnum1 = -1;
|
|
|
|
|
#ifdef USE_CLI
|
|
|
|
|
static short in_refnum0 = -1;
|
|
|
|
|
static short in_refnum1 = -1;
|
|
|
|
|
#endif
|
2004-06-22 22:59:20 +00:00
|
|
|
|
|
|
|
|
|
#if USE_BUFFER
|
|
|
|
|
#define BUFFER_LEN 80
|
2004-06-22 22:05:24 +00:00
|
|
|
|
static char buffer[256];
|
|
|
|
|
static int buff_len;
|
2004-06-22 22:59:20 +00:00
|
|
|
|
#endif
|
2004-06-21 22:39:25 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Technical Note TN1119 "Serial Port Apocrypha"
|
|
|
|
|
*
|
|
|
|
|
* from http://developer.apple.com/technotes/tn/tn1119.html
|
|
|
|
|
*
|
|
|
|
|
* Inside Macintosh, Devices, "Using the Serial Driver" :
|
|
|
|
|
*
|
|
|
|
|
* http://developer.apple.com/documentation/mac/Devices/Devices-315.html
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-22 22:05:24 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* ".AOut" Serial port A (modem) output
|
|
|
|
|
* ".AIn" Serial port A (modem) input
|
|
|
|
|
* ".BOut" Serial port B (printer) output
|
|
|
|
|
* ".BIn" Serial port B (printer) input
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-21 22:39:25 +00:00
|
|
|
|
int setserial(short refNum, unsigned int bitrate, unsigned int datasize,
|
|
|
|
|
int parity, int stopbits)
|
|
|
|
|
{
|
|
|
|
|
CntrlParam param;
|
|
|
|
|
short seropts;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
switch(bitrate)
|
|
|
|
|
{
|
|
|
|
|
case 150:
|
|
|
|
|
seropts = baud150;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 300:
|
|
|
|
|
seropts = baud300;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 600:
|
|
|
|
|
seropts = baud600;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1200:
|
|
|
|
|
seropts = baud1200;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1800:
|
|
|
|
|
seropts = baud1800;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2400:
|
|
|
|
|
seropts = baud2400;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3600:
|
|
|
|
|
seropts = baud3600;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4800:
|
|
|
|
|
seropts = baud3600;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7200:
|
|
|
|
|
seropts = baud7200;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 9600:
|
|
|
|
|
seropts = baud9600;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 14400:
|
|
|
|
|
seropts = baud14400;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 19200:
|
|
|
|
|
seropts = baud19200;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 28800:
|
|
|
|
|
seropts = baud28800;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 38400:
|
|
|
|
|
seropts = baud38400;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 57600:
|
|
|
|
|
seropts = baud57600;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
seropts = baud9600;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(datasize)
|
|
|
|
|
{
|
|
|
|
|
case 5:
|
|
|
|
|
seropts |= data5;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
|
seropts |= data6;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
|
seropts |= data7;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
|
seropts |= data8;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
seropts |= data8;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(parity)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
seropts |= noParity;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
seropts |= oddParity;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
seropts |= evenParity;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(stopbits)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
seropts |= stop10;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
seropts |= stop15;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
seropts |= stop20;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
param.csCode = kSERDConfiguration;
|
|
|
|
|
param.csParam[0] = seropts;
|
|
|
|
|
param.ioCRefNum = refNum;
|
|
|
|
|
param.ioVRefNum = 0;
|
|
|
|
|
param.ioCompletion = 0;
|
|
|
|
|
res = PBControlSync((ParmBlkPtr)¶m);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:05:24 +00:00
|
|
|
|
void serial_put(char c)
|
|
|
|
|
{
|
2004-06-22 22:59:20 +00:00
|
|
|
|
#if USE_BUFFER
|
2004-06-22 22:05:24 +00:00
|
|
|
|
buffer[buff_len++] = c;
|
|
|
|
|
|
|
|
|
|
if ( c == '\n' )
|
|
|
|
|
{
|
|
|
|
|
/* add '\r' and flush buffer */
|
|
|
|
|
|
|
|
|
|
buffer[buff_len++] = '\r';
|
|
|
|
|
|
|
|
|
|
goto flush;
|
|
|
|
|
}
|
|
|
|
|
/* if buffer is full (BUFFER_LEN - 1), flush it
|
|
|
|
|
* we take BUFFER_LEN - 1 to have enough room
|
|
|
|
|
* if we need to add '\r' on '\n'
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (buff_len == BUFFER_LEN - 1)
|
|
|
|
|
goto flush;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
flush:
|
2005-08-30 08:03:50 +00:00
|
|
|
|
if (out_refnum0 != -1)
|
|
|
|
|
write(out_refnum0, buffer, buff_len);
|
|
|
|
|
if (out_refnum1 != -1)
|
|
|
|
|
write(out_refnum1, buffer, buff_len);
|
2004-06-22 22:05:24 +00:00
|
|
|
|
buff_len = 0;
|
2004-06-22 22:59:20 +00:00
|
|
|
|
#else
|
|
|
|
|
if ( c == '\n' )
|
|
|
|
|
{
|
2005-08-30 08:03:50 +00:00
|
|
|
|
if (out_refnum0 != -1)
|
|
|
|
|
write(out_refnum0, "\n\r", 2);
|
|
|
|
|
if (out_refnum1 != -1)
|
|
|
|
|
write(out_refnum1, "\n\r", 2);
|
2004-06-22 22:59:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2004-06-25 11:16:51 +00:00
|
|
|
|
{
|
2005-08-30 08:03:50 +00:00
|
|
|
|
if (out_refnum0 != -1)
|
|
|
|
|
write(out_refnum0, &c, 1);
|
|
|
|
|
if (out_refnum1 != -1)
|
|
|
|
|
write(out_refnum1, &c, 1);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
2004-06-22 22:59:20 +00:00
|
|
|
|
#endif
|
2004-06-22 22:05:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void serial_init(emile_l2_header_t* info)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
2005-11-27 22:31:07 +00:00
|
|
|
|
int bitrate, parity, datasize, stopbits;
|
2004-06-22 22:05:24 +00:00
|
|
|
|
|
2005-11-27 22:31:07 +00:00
|
|
|
|
res = read_config_modem(info->configuration,
|
2005-11-28 23:20:25 +00:00
|
|
|
|
&bitrate, &parity, &datasize, &stopbits);
|
2005-11-27 22:31:07 +00:00
|
|
|
|
if (res != -1)
|
|
|
|
|
{
|
2005-08-30 08:03:50 +00:00
|
|
|
|
res = OpenDriver(c2pstring(".AOut"), &out_refnum0);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
if (res != noErr) {
|
2005-08-30 08:03:50 +00:00
|
|
|
|
printf("Cannot open modem output port (%d)\n", res);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-11-27 22:31:07 +00:00
|
|
|
|
res = setserial(out_refnum0, bitrate,
|
|
|
|
|
datasize,
|
|
|
|
|
parity,
|
|
|
|
|
stopbits);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
if (res != noErr) {
|
2005-08-30 08:03:50 +00:00
|
|
|
|
printf("Cannot setup modem output port (%d)\n",
|
|
|
|
|
res);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-30 08:03:50 +00:00
|
|
|
|
#ifdef USE_CLI
|
|
|
|
|
res = OpenDriver(c2pstring(".AIn"), &in_refnum0);
|
|
|
|
|
if (res != noErr) {
|
|
|
|
|
printf("Cannot open modem input port (%d)\n", res);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-11-27 22:31:07 +00:00
|
|
|
|
res = setserial(in_refnum0, bitrate,
|
|
|
|
|
datasize,
|
|
|
|
|
parity,
|
|
|
|
|
stopbits);
|
2005-08-30 08:03:50 +00:00
|
|
|
|
if (res != noErr) {
|
|
|
|
|
printf("Cannot setup modem input port (%d)\n",
|
|
|
|
|
res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif /* USE_CLI */
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-27 22:31:07 +00:00
|
|
|
|
res = read_config_printer(info->configuration,
|
2005-11-28 23:20:25 +00:00
|
|
|
|
&bitrate, &parity, &datasize, &stopbits);
|
2005-11-27 22:31:07 +00:00
|
|
|
|
if (res != -1) {
|
2005-08-30 08:03:50 +00:00
|
|
|
|
res = OpenDriver(c2pstring(".BOut"), &out_refnum1);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
if (res != noErr) {
|
2005-08-30 08:03:50 +00:00
|
|
|
|
printf("Cannot open printer output port (%d)\n", res);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-11-27 22:31:07 +00:00
|
|
|
|
res = setserial(out_refnum1, bitrate,
|
|
|
|
|
datasize,
|
|
|
|
|
parity,
|
|
|
|
|
stopbits);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
if (res != noErr) {
|
2005-08-30 08:03:50 +00:00
|
|
|
|
printf("Cannot setup printer output port (%d)\n"
|
|
|
|
|
, res);
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-30 08:03:50 +00:00
|
|
|
|
#ifdef USE_CLI
|
|
|
|
|
res = OpenDriver(c2pstring(".BIn"), &in_refnum1);
|
|
|
|
|
if (res != noErr) {
|
|
|
|
|
printf("Cannot open printer input port (%d)\n", res);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-11-27 22:31:07 +00:00
|
|
|
|
res = setserial(in_refnum1, bitrate,
|
|
|
|
|
datasize,
|
|
|
|
|
parity,
|
|
|
|
|
stopbits);
|
2005-08-30 08:03:50 +00:00
|
|
|
|
if (res != noErr) {
|
|
|
|
|
printf("Cannot setup printer input port (%d)\n"
|
|
|
|
|
, res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif /* USE_CLI */
|
2004-06-25 11:16:51 +00:00
|
|
|
|
}
|
2004-06-22 22:05:24 +00:00
|
|
|
|
|
2004-06-22 22:59:20 +00:00
|
|
|
|
#if USE_BUFFER
|
2004-06-22 22:05:24 +00:00
|
|
|
|
buff_len = 0;
|
2004-06-22 22:59:20 +00:00
|
|
|
|
#endif
|
2004-06-22 22:05:24 +00:00
|
|
|
|
}
|
2005-08-27 15:27:03 +00:00
|
|
|
|
|
|
|
|
|
#ifdef USE_CLI
|
2005-08-30 08:03:50 +00:00
|
|
|
|
void serial_cursor_save(void)
|
2005-08-27 15:27:03 +00:00
|
|
|
|
{
|
2005-08-30 08:03:50 +00:00
|
|
|
|
serial_put('');
|
|
|
|
|
serial_put('7');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void serial_cursor_restore(void)
|
|
|
|
|
{
|
|
|
|
|
serial_put('');
|
|
|
|
|
serial_put('8');
|
2005-08-27 15:27:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int serial_getchar(void)
|
|
|
|
|
{
|
2005-08-30 08:03:50 +00:00
|
|
|
|
int count;
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
|
if (in_refnum0 != -1)
|
|
|
|
|
{
|
|
|
|
|
count = read(in_refnum0, &c, 1);
|
|
|
|
|
if (count == 1)
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_refnum1 != -1)
|
|
|
|
|
{
|
|
|
|
|
count = read(in_refnum1, &c, 1);
|
|
|
|
|
if (count == 1)
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-31 23:32:43 +00:00
|
|
|
|
return 0;
|
2005-08-27 15:27:03 +00:00
|
|
|
|
}
|
2005-08-30 08:03:50 +00:00
|
|
|
|
|
|
|
|
|
int serial_keypressed()
|
|
|
|
|
{
|
2005-08-31 23:32:43 +00:00
|
|
|
|
if (serial_getchar() != 0)
|
2005-08-30 08:03:50 +00:00
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-08-27 15:27:03 +00:00
|
|
|
|
#endif
|