summaryrefslogtreecommitdiff
path: root/prnt/hpijs/systemservices.h
blob: 7712a336369b6474da3e18498f3c5b30fb84a038 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*****************************************************************************\
  systemservices.h : Interface for the SystemServices class

  Copyright (c) 1996 - 2001, Hewlett-Packard Co.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. Neither the name of Hewlett-Packard nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*****************************************************************************/


#ifndef APDK_SYSTEMSERVICES_H
#define APDK_SYSTEMSERVICES_H

APDK_BEGIN_NAMESPACE

struct IO_MODE
{
 //NO_FEEDBACK, STATUS_ONLY, BIDI, USB
    BOOL bDevID;
    BOOL bStatus;
    BOOL bUSB;
}; //IO_MODE


//forward declarations
class DeviceRegistry;
//class RasterSender;
class Scripter;

const int DevIDBuffSize = 512;       //!< size of buffer used by SetDevInfo

//SystemServices
//! Provides interface to host environment
/*! \class SystemServices systemservices.h "hpprintapi.h"
The SystemServices object is used to encapsulate memory-management, I/O,
clock access, user notification and UI, and other utilities provided by the
operating system or host system supporting the driver. It is an abstract base
class, requiring implementation of the necessary routines by the host.
Creation of SystemServices is the first step in the calling sequence for the
driver, followed by creation of the PrintContext and the Job. The derived
class constructor must include a call to the member function InitDeviceComm,
which establishes communication with the printer if possible.
The only reference to this object in API-calling code should be to its
constructor and destructor, which must be invoked prior to and after all other
driver calls.
******************************************************************************/
class SystemServices
{
friend class PrintContext;
friend class Printer;   // for saved device strings
#ifdef APDK_APOLLO2XXX
friend class Apollo2xxx;    // it needs to check the specific model string
#endif
friend class Tester;
friend class AsciiScripter;
friend class BinaryScripter;
public:
    SystemServices();
    virtual ~SystemServices();

    /*!
    Check this member variable for the validity of the constructed object.
    Calling code must check that the value of this variable is NO_ERROR before
    proceeding.
    \sa DRIVER_ERROR
    */
    DRIVER_ERROR constructor_error;

    //! Must include in derived class constructor (if using bi-di)
    DRIVER_ERROR InitDeviceComm();

    /////////////////////////////////////////////////////////////////////
    IO_MODE IOMode;

    //! Passes a command to the I/O system to send all data to the printer immediately.
    /*!
    A typical I/O system either buffer data or send data immediatly.  If the I/O
    system is buffering data then typically store all data sent in a buffer until it
    has some amount of data (say 1, 2, or 4 Kbytes) then will send that block of data
    to the printer. In some cases, it may be necessary that data that has just been
    sent to the I/O system reach the printer ASAP, at which point the printer driver
    will check the status or deviceID string of the printer and continue. Often,
    USB implementations will favor the block-send mechanism because it is more
    efficient through the system's call stack. Simple and traditional centronics
    I/O is usually not buffered, but this is still a system-by-system I/O preference.

    If the I/O system is buffering data into blocks before sending to the printer,
    this command should pass a command to the I/O system to stop waiting and send
    everything it has to the printer now. The base implimentation simply returns
    NO_ERROR and is appropriate for a system that does not buffer I/O.

    \note  The appropriate way to handle a FlushIO on a buffered system is to call
    the USB class driver's I/O Control routine with a flush command.  If the USB
    class driver has no flush command then check to see if it has timed flush
    (it auto-flushes every n [milli]seconds).  If it does then the base FlushIO
    method will work okay.

    \warning In the past some have implemented flush by sending 10K nulls to the USB
    driver.  Currently this does work and will cause data already in the buffer
    to be sent to the printer.  Current printers ignore the null, however, there
    may be printers in the future that cannot handle this behavior.
    */
    virtual DRIVER_ERROR FlushIO() { return NO_ERROR; }

    //! Tells the I/O system that the print job has ended prematurely.
    /*!
    This routine instructs the I/O system that this print job has ended
    prematurely, that no further data will be sent and that any data being held
    in a "send" buffer can be thrown away. This command is optional and depends
    on the implementation of the I/O system.  In some error conditions, the
    printer is incapable of accepting more data. The printer driver will sense
    this error and stop sending data, but if the I/O system has buffered data
    it may continue to wait for the printer to accept this remaining data
    before unloading. This mechanism will allow for a more efficient clean up
    of the aborted job.
    */
    virtual DRIVER_ERROR AbortIO() { return NO_ERROR; }

    /*!
    Uses message code to relay a message to the user. Must be implemented in
    derived class provided by developer. This routine will cause a corresponding
    text string (or graphic, or both) to be displayed to the user to inform them
    of printer status. This is how printer events such as out-of-paper will be
    communicated to the user.
    */
    virtual void DisplayPrinterStatus (DISPLAY_STATUS ePrinterStatus)=0;

    /*!
    Must be implemented in derived class. Waits for the specified number of
    milliseconds before returning to the caller. Gives host system a chance to
    process asynchronous events.  Derived implimentation of BusyWait MUST allow
    the processing of asynchronous events such as user interface changes, response
    to dialogs or pop-ups.  The BusyWait method should return NO_ERROR if we should
    continue processing.  The APDK core code calls busy wait within loops (when
    the printer is out of paper, has no pen, etc...) and will continue when the
    user has fixed the problem.  If the user wants to cancel the job then the
    implemented version of BusyWait must return JOB_CANCELED so that the APDK
    stops retrying and cancels the job.
    */
    virtual DRIVER_ERROR BusyWait(DWORD msec)=0;

    /*!
    Retrieves the device identifier string from the printer.

    \param strID Pointer to buffer for storing string.
    \param iSize The size of buffer.
    \return IO_ERROR
    */
    virtual DRIVER_ERROR ReadDeviceID(BYTE* strID, int iSize)=0;

    /*!
    This method will allocate a contiguous block of memory at least iMemSize bytes long.

    \param iMemSize - size of block.
    \return Pointer to the memory block. Can also return NULL, if allocation failed.
    */
    virtual BYTE* AllocMem (int iMemSize)=0;

    /*!
    Frees a given block of memory previously allocated with AllocMem.

    \param pMem - pointer to the block of memory to be freed.
    */
    virtual void FreeMem (BYTE* pMem)=0;

    virtual BOOL PrinterIsAlive();

    /*!
    This method reads status register from printer. If the parallel status byte
    is returned, the function returns TRUE when it sets this value. Otherwise
    the function returns FALSE if this functionality is not supported.

    \param bStatReg Pointer to a byte into which status register will be copied.

    \return TRUE The value is set after returning the parallel status byte,
    FALSE if the functionality is not supported.
    */
    virtual BOOL GetStatusInfo(BYTE* bStatReg)=0;

    /*!
    Sends bytes of data to the printer.
    \note This method decrements the dwCount parameter (*dwCount) by the number
    of bytes that were actually sent.

    \param pBuffer pointer to buffer full of data.
    \param dwCount (in/out) Upon entry, this contains the number of bytes of
    data requested to send. Upon return from the method, dwCount is the number
    of bytes that were not sent, i.e. the remaining bytes.
    */
    virtual DRIVER_ERROR ToDevice(const BYTE* pBuffer, DWORD* dwCount)=0;

    /*!
    Gets data from printer.

    \param pReadBuff Pointer to buffer into which data is to be copied.
    \param wReadCount Output parameter (pointer to allocated DWORD) telling
    number of bytes copied.
    */
    virtual DRIVER_ERROR FromDevice(BYTE* pReadBuff, DWORD* wReadCount)=0;

    virtual DRIVER_ERROR GetECPStatus(BYTE *pStatusString,int *pECPLength, int ECPChannel);

    /*!
    This routine will query the system for the current tick count (time).
    Since it will be used primarily for retry time-outs in the driver, it only
    needs to provide a running count of elapsed time since the device was booted,
    for example.

    \return DWORD Number of ticks for current tick count.
    */
    virtual DWORD GetSystemTickCount (void)=0;

    virtual float power(float x, float y)=0;

    /*!
    Returns the model portion of the firmware ID string from the printer.
    */
    const char* PrinterModel() { return strModel; }

	/*!
	Returns the size of the send buffer.
	*/
    unsigned int GetSendBufferSize() const { return iSendBufferSize; }

#if defined (APDK_DJ3320)
    /*!
    This method return the reads Crossbow/Spear vertical alignment value from printer. 
	If the vertical alignment value is set by the user from host, 
	the function returns TRUE. Otherwise the function returns FALSE if this functionality 
	is not supported.

    \param cVertAlignVal Pointer to a byte into which vertical alignment value will be copied.

    \return TRUE The value is set after returning the vertical alignment value,
    FALSE if the functionality is not supported.
    */
    virtual BOOL GetVerticalAlignmentValue(BYTE* cVertAlignVal) { return FALSE; }

    /*!
    This method reads Crossbow/Spear vertical alignment value from the device during
    printer instantiation. The value is saved for later calls by GetVerticalAlignmentValue. 

    \return TRUE valid value read,
    FALSE invalid value or not supported.
    */

    virtual BOOL GetVertAlignFromDevice() { return FALSE; }
#endif

// utilities ///////////////////////////////////////////////////////

    // call FreeMem after checking for null ptr
    DRIVER_ERROR FreeMemory(void *ptr);
    DRIVER_ERROR GetDeviceID(BYTE* strID, int iSize, BOOL query);

	/*!
	Return the VIP version of the firmware ID from the printer.
	*/
    int    GetVIPVersion () { return VIPVersion; }

#if defined(APDK_CAPTURE)
    Scripter *pScripter;
    /*!
    Begin recording script for debugging.
    \param FileName Name of the output file which will hold the script.
    \param ascii If TRUE use ASCII implementation. Otherwise, use binary.
    */
    DRIVER_ERROR InitScript(const char* FileName, BOOL ascii, BOOL read=FALSE);

	/*!
    Stop recording script for debugging.
	*/
    DRIVER_ERROR EndScript();

    BOOL Capturing;
    BOOL replay;
#endif

    virtual int GetPJLHeaderBuffer (char **szPJLBuffer)
    {
        return 0;
    }

protected:
    virtual void AdjustIO(IO_MODE IM, const char* model = NULL);

    BYTE strDevID[DevIDBuffSize]; // save whole DevID string

    unsigned int iSendBufferSize;


    PORTID ePortID;

    DeviceRegistry* DR;

    char strModel[DevIDBuffSize]; // to contain the MODEL (MDL) from the DevID
    char strPens[132];   // to contain the VSTATUS penID from the DevID
    int  VIPVersion;    // VIP version from the DevID

    // for internal use
    virtual BYTE* AllocMem (int iMemSize, BOOL trackmemory)
    { return AllocMem(iMemSize); }

    virtual void FreeMem (BYTE* pMem, BOOL trackmemory)
    { FreeMem(pMem); }

}; //SystemServices

APDK_END_NAMESPACE


#endif //APDK_SYSTEMSERVICES_H