File: printer.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (136 lines) | stat: -rw-r--r-- 3,551 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * $Id: printer.c,v 1.10 2009-10-15 11:47:49 potyra Exp $ 
 *
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "printer.h"
#include "glue-io.h"

struct cpssp {
	/* Config */

	/* Ports */
	struct sig_parallel *port_par;

	/* Signals */

	/* State */

	/* Processes */
};

#define PARPORT_CONTROL_STROBE    0x1
#define PARPORT_CONTROL_AUTOFD    0x2
#define PARPORT_CONTROL_INIT      0x4
#define PARPORT_CONTROL_SELECT    0x8

#define PARPORT_STATUS_ERROR      0x8
#define PARPORT_STATUS_SELECT     0x10
#define PARPORT_STATUS_PAPEROUT   0x20
#define PARPORT_STATUS_ACK        0x40
#define PARPORT_STATUS_BUSY       0x80


static void
printer_recv(void *_cpssp, struct sig_parallel_msg *sc)
{
        struct cpssp *cpssp = (struct cpssp *) _cpssp;
        unsigned char helper;
        struct sig_parallel_msg msg;

        /* This acts like an SPP line printer */

        /* Unless the printer is out of order, there's this signal active -
         * and this printer never is out of order! Dooh! */

        helper = PARPORT_STATUS_ERROR;

        /* Don't do anything to the data/control signals, as this isn't
         * a backchannel-enabled device.
         * Send back data/control signals unchanged to match reality */
        msg.data = sc->data;
        msg.control = sc->control;

        /* SPP protocol requirement: Only show being selected
         * if being selected! That's just logical, isn't it? */
        if (sc->control & PARPORT_CONTROL_SELECT) {
                helper |= PARPORT_STATUS_SELECT;
        }

        /* SPP protocol: Recognize INIT */
        if (!(sc->control & PARPORT_CONTROL_INIT)) {
                fprintf(stderr,"\n\nPRINTER RESET\n\n");
        }

        /* SPP protocol: Recognize STROBE */
        if (sc->control & PARPORT_CONTROL_STROBE) {
                /* On STROBE, "latch" the data lines' signals and print */
		io_write(2, &sc->data, 1);

                /*
		 * Historic printers could automatically generate a line feed
                 * after each carriage return (ask IBM why)
		 */
                if (sc->control & PARPORT_CONTROL_AUTOFD
                && sc->data == 0x0d) {
			io_write(2, "\n", 1);
                }
                msg.status = helper;

                /* Acknowledge the STROBE, wait 1ns */

                /* send_packet(&msg); */
                sig_parallel_send(cpssp->port_par, cpssp, &msg);

                /* Wait a nanosecond...
                 * though it's senseless, as we have network lag
                 * magnitudes longer than nanoseconds */
        }

        /* Tell the controller we're ready again */

        helper |= PARPORT_STATUS_ACK;
        helper |= PARPORT_STATUS_BUSY;

        msg.status = helper;
        sig_parallel_send(cpssp->port_par, cpssp, &msg);
}

void *
printer_create(
	const char *name,
	struct sig_manage *port_manage,
	struct sig_parallel *port_par
)
{
	static const struct sig_parallel_funcs funcs = {
		.recv = printer_recv,
	};
	struct cpssp *cpssp;

	cpssp = malloc(sizeof(*cpssp));
	assert(cpssp);

	cpssp->port_par = port_par;

	sig_parallel_connect(port_par, cpssp, &funcs);

	return cpssp;
}

void
printer_destroy(void *_cpssp)
{
	struct cpssp *cpssp = _cpssp;

	free(cpssp);
}