Tizen 2.1 base
[platform/upstream/hplip.git] / scan / sane / mfpdtf.h
1 /************************************************************************************\
2
3   mfpdtf.h - HP Multi-Function Peripheral Data Transfer Format filter.
4
5   (c) 2001-2005 Copyright Hewlett-Packard Development Company, LP
6
7   Permission is hereby granted, free of charge, to any person obtaining a copy 
8   of this software and associated documentation files (the "Software"), to deal 
9   in the Software without restriction, including without limitation the rights 
10   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
11   of the Software, and to permit persons to whom the Software is furnished to do 
12   so, subject to the following conditions:
13
14   The above copyright notice and this permission notice shall be included in all
15   copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
18   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
19   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
20   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
21   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24   Contributing Author(s): David Paschal, Don Welch, David Suffield
25
26 \************************************************************************************/
27
28 #if !defined(_MFPDTF_H )
29 #define _MFPDTF_H
30
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdint.h>
38
39
40 enum MfpdtfImageRecordID_e { MFPDTF_ID_START_PAGE = 0,
41                              MFPDTF_ID_RASTER_DATA = 1,
42                              MFPDTF_ID_END_PAGE = 2 };
43
44 struct MfpdtfFixedHeader_s
45 {
46         unsigned char   blockLength[4]; /* Includes header(s). */
47         unsigned char   headerLength[2];    /* Just header(s). */
48         unsigned char   dataType;
49         unsigned char   pageFlags;
50 }               __attribute__(( packed) );
51
52 struct MfpdtfImageRasterDataHeader_s
53 {
54         unsigned char   traits;         /* Unused. */
55         unsigned char   byteCount[2];
56 }               __attribute__(( packed) );
57
58 struct MfpdtfImageStartPageRecord_s
59 {
60         unsigned char   encoding;
61         unsigned char   pageNumber[2];
62         struct
63         {
64                 unsigned char   pixelsPerRow[2];
65                 unsigned char   bitsPerPixel[2];
66                 unsigned char   rowsThisPage[4];
67                 unsigned char   xres[4];
68                 unsigned char   yres[4];
69         } black, color;
70 }               __attribute__(( packed) );
71
72 struct MfpdtfImageEndPageRecord_s
73 {
74         unsigned char   unused[3];
75         struct
76         {
77                 unsigned char   numberOfRows[4];
78         } black, color;
79 }               __attribute__(( packed) );
80
81 struct Mfpdtf_s
82 {
83         //ptalChannel_t   chan;
84         int deviceid;
85         int channelid;
86         int fdLog;              /* <0 means not (yet) open. */
87         int logOffset;
88
89         struct
90         {
91                 struct timeval                          timeout;
92                 int                                     simulateImageHeaders;
93
94                 int                                     lastServiceResult;
95                 int                                     dataType;           /* <0 means not (yet) valid. */
96                 int                                     arrayRecordCount;
97                 int                                     arrayRecordSize;
98                 int                                     fixedBlockBytesRemaining;   /* Also generic data. */
99                 int                                     innerBlockBytesRemaining;   /* Image or array data. */
100                 int                                     dontDecrementInnerBlock;
101
102                 struct MfpdtfFixedHeader_s              fixedHeader;
103                 int                                     lenVariantHeader;
104                 union  MfpdtfVariantHeader_u *          pVariantHeader;
105                 struct MfpdtfImageStartPageRecord_s     imageStartPageRecord;
106                 struct MfpdtfImageRasterDataHeader_s    imageRasterDataHeader;
107                 struct MfpdtfImageEndPageRecord_s       imageEndPageRecord;
108         } read;
109 };
110
111 typedef struct Mfpdtf_s * Mfpdtf_t;
112
113 #define MFPDTF_RESULT_NEW_PAGE                 0x00000001
114 #define MFPDTF_RESULT_END_PAGE                 0x00000002
115 #define MFPDTF_RESULT_NEW_DOCUMENT             0x00000004
116 #define MFPDTF_RESULT_END_DOCUMENT             0x00000008
117
118 #define MFPDTF_RESULT_END_STREAM               0x00000010
119 #define MFPDTF_RESULT_RESERVED_20              0x00000020
120 #define MFPDTF_RESULT_RESERVED_40              0x00000040
121 #define MFPDTF_RESULT_RESERVED_80              0x00000080
122
123 #define MFPDTF_RESULT_00000100                 0x00000100
124 #define MFPDTF_RESULT_READ_TIMEOUT             0x00000200
125 #define MFPDTF_RESULT_READ_ERROR               0x00000400
126 #define MFPDTF_RESULT_OTHER_ERROR              0x00000800
127
128 #define MFPDTF_RESULT_ERROR_MASK               0x00000E00
129
130 #define MFPDTF_RESULT_NEW_DATA_TYPE            0x00001000
131 #define MFPDTF_RESULT_NEW_VARIANT_HEADER       0x00002000
132 #define MFPDTF_RESULT_GENERIC_DATA_PENDING     0x00004000
133 #define MFPDTF_RESULT_ARRAY_DATA_PENDING       0x00008000
134
135 #define MFPDTF_RESULT_NEW_START_OF_PAGE_RECORD 0x00010000
136 #define MFPDTF_RESULT_IMAGE_DATA_PENDING       0x00020000
137 #define MFPDTF_RESULT_NEW_END_OF_PAGE_RECORD   0x00040000
138 #define MFPDTF_RESULT_00080000                 0x00080000
139
140 #define MFPDTF_RESULT_INNER_DATA_PENDING       0x00028000
141
142 enum MfpdtfDataType_e { MFPDTF_DT_UNKNOWN = 0,
143                         MFPDTF_DT_FAX_IMAGES = 1,
144                         MFPDTF_DT_SCANNED_IMAGES = 2,
145                         MFPDTF_DT_DIAL_STRINGS = 3,
146                         MFPDTF_DT_DEMO_PAGES = 4,
147                         MFPDTF_DT_SPEED_DIALS = 5,
148                         MFPDTF_DT_FAX_LOGS = 6,
149                         MFPDTF_DT_CFG_PARMS = 7,
150                         MFPDTF_DT_LANG_STRS = 8,
151                         MFPDTF_DT_JUNK_FAX_CSIDS = 9, /* MFPDTF_DT_DIAL_STRINGS */
152                         MFPDTF_DT_REPORT_STRS = 10, /* MFPDTF_DT_LANG_STRS    */
153                         MFPDTF_DT_FONTS = 11,
154                         MFPDTF_DT_TTI_BITMAP = 12,
155                         MFPDTF_DT_COUNTERS = 13,
156                         MFPDTF_DT_DEF_PARMS = 14, /* MFPDTF_DT_CFG_PARMS    */
157                         MFPDTF_DT_SCAN_OPTIONS = 15,
158                         MFPDTF_DT_FW_JOB_TABLE = 17 };
159 #define MFPDTF_DT_MASK_IMAGE \
160     ((1<<MFPDTF_DT_FAX_IMAGES) | \
161      (1<<MFPDTF_DT_SCANNED_IMAGES) | \
162      (1<<MFPDTF_DT_DEMO_PAGES))
163
164 enum MfpdtfImageEncoding_e { MFPDTF_RASTER_BITMAP                                  = 0,
165                              MFPDTF_RASTER_GRAYMAP = 1,
166                              MFPDTF_RASTER_MH = 2,
167                              MFPDTF_RASTER_MR = 3,
168                              MFPDTF_RASTER_MMR = 4,
169                              MFPDTF_RASTER_RGB = 5,
170                              MFPDTF_RASTER_YCC411 = 6,
171                              MFPDTF_RASTER_JPEG = 7,
172                              MFPDTF_RASTER_PCL = 8,
173                              MFPDTF_RASTER_NOT = 9 };
174
175
176 union MfpdtfVariantHeader_u
177 {
178         struct MfpdtfVariantHeaderFaxDataArtoo_s
179         {
180                 unsigned char   majorVersion;   /* 1 */
181                 unsigned char   minorVersion;   /* 0 */
182                 unsigned char   dataSource; /* unknown=0,prev,host,fax,pollfax,scanner */
183                 unsigned char   dataFormat; /* unknown=0,ASCII=3,CCITT_G3=10 */
184                 unsigned char   dataCompression;    /* native=1,MH,MR,MMR */
185                 unsigned char   pageResolution; /* fine=0,std,CCITT300,CCITT400 */
186                 unsigned char   pageSize;       /* unknown=0 */
187                 unsigned char   pixelsPerRow[2];
188                 unsigned char   year;
189                 unsigned char   month;
190                 unsigned char   day;
191                 unsigned char   hour;
192                 unsigned char   minute;
193                 unsigned char   second;
194                 unsigned char   T30_CSI[20];
195                 unsigned char   T30_SUB[20];
196         }               __attribute__(( packed) )                            faxArtoo;
197
198         struct MfpdtfVariantHeaderFaxDataSolo_s
199         {
200                 unsigned char   majorVersion;   /* 1 */
201                 unsigned char   minorVersion;   /* 1 */
202                 unsigned char   dataSource;
203                 unsigned char   dataFormat;
204                 unsigned char   dataCompression;
205                 unsigned char   pageResolution;
206                 unsigned char   pageSize;
207                 unsigned char   pixelsPerRow[2];
208                 unsigned char   year;
209                 unsigned char   month;
210                 unsigned char   day;
211                 unsigned char   hour;
212                 unsigned char   minute;
213                 unsigned char   second;
214                 unsigned char   suppressTTI;
215                 unsigned char   T30_CSI[20];
216                 unsigned char   T30_SUB[20];
217                 unsigned char   T30_PWD[20];
218         }               __attribute__(( packed) )                            faxSolo;
219
220         struct MfpdtfVariantHeaderFaxData_s
221         {
222                 unsigned char   majorVersion;
223                 unsigned char   minorVersion;
224                 /* TODO: Finish. */
225         }               __attribute__(( packed) )                            fax;
226
227         struct MfpdtfVariantHeaderImageData_s
228         {
229                 unsigned char   majorVersion;
230                 unsigned char   minorVersion;
231                 unsigned char   sourcePages[2];
232                 unsigned char   copiesPerPage[2];
233                 unsigned char   zoomFactor[2];
234                 unsigned char   jpegQualityFactor[2];
235         }               __attribute__(( packed) ) image;
236
237         struct MfpdtfVariantHeaderArrayData_s
238         {
239                 unsigned char   majorVersion;
240                 unsigned char   minorVersion;
241                 unsigned char   recordCount[2];
242                 unsigned char   recordSize[2];
243         } __attribute__(( packed) ) array;
244 } __attribute__(( packed) );
245
246 Mfpdtf_t __attribute__ ((visibility ("hidden"))) MfpdtfAllocate(int deviceid, int channelid);
247 int __attribute__ ((visibility ("hidden"))) MfpdtfDeallocate(Mfpdtf_t mfpdtf);
248 int __attribute__ ((visibility ("hidden"))) MfpdtfSetChannel(Mfpdtf_t mfpdtf, int channelid);
249 int __attribute__ ((visibility ("hidden"))) MfpdtfLogToFile(Mfpdtf_t mfpdtf, char * filename);
250 int __attribute__ ((visibility ("hidden"))) MfpdtfReadGetSimulateImageHeaders(Mfpdtf_t mfpdtf);
251 int __attribute__ ((visibility ("hidden"))) MfpdtfReadSetSimulateImageHeaders(Mfpdtf_t mfpdtf, int simulateImageHeaders);
252 int __attribute__ ((visibility ("hidden"))) MfpdtfReadStart(Mfpdtf_t mfpdtf);
253 int __attribute__ ((visibility ("hidden"))) MfpdtfReadService(Mfpdtf_t mfpdtf);
254 int __attribute__ ((visibility ("hidden"))) MfpdtfReadGetLastServiceResult(Mfpdtf_t mfpdtf);
255 int __attribute__ ((visibility ("hidden"))) MfpdtfReadGetVariantHeader(Mfpdtf_t mfpdtf, union MfpdtfVariantHeader_u * buffer, int maxlen);
256 int __attribute__ ((visibility ("hidden"))) MfpdtfReadGetStartPageRecord(Mfpdtf_t mfpdtf, struct MfpdtfImageStartPageRecord_s * buffer, int maxlen);
257 int __attribute__ ((visibility ("hidden"))) MfpdtfReadInnerBlock(Mfpdtf_t mfpdtf, unsigned char * buffer, int countdown);
258
259 /* 
260  * Phase 2 rewrite. des
261  */
262
263 enum MFPDTF_DATA_TYPE
264 {
265    DT_FAX = 1,
266    DT_SCAN = 2
267 };
268
269 /* MFPDTF page flag bit fields. */
270 #define PF_NEW_PAGE 1
271 #define PF_END_PAGE 2
272 #define PF_NEW_DOC 4
273 #define PF_END_DOC 8
274
275 enum MFPDTF_RECORD_ID
276 {
277    ID_START_PAGE = 0,
278    ID_RASTER_DATA = 1,
279    ID_END_PAGE = 2
280 };
281
282 /* All words are stored little endian. */
283
284 typedef struct
285 {
286    uint32_t BlockLength;   /* includes header in bytes */
287    uint16_t HeaderLength;  /* in bytes */
288    uint8_t DataType;
289    uint8_t PageFlag;      
290 } __attribute__((packed)) MFPDTF_FIXED_HEADER;
291
292 typedef struct
293 {
294    uint8_t ID;
295    uint8_t Code;
296    uint16_t PageNumber;
297    uint16_t BlackPixelsPerRow;
298    uint16_t BlackBitsPerPixel;
299    uint32_t BlackRows;
300    uint32_t BlackHorzDPI;
301    uint32_t BlackVertDPI;
302    uint16_t CMYPixelsPerRow;
303    uint16_t CMYBitsPerPixel;
304    uint32_t CMYRows;
305    uint32_t CMYHorzDPI;
306    uint32_t CMYVertDPI;
307 } __attribute__((packed)) MFPDTF_START_PAGE;
308
309 typedef struct
310 {
311    uint8_t ID;
312    uint8_t dummy;
313    uint16_t Size;    /* in bytes */
314 } __attribute__((packed)) MFPDTF_RASTER;
315
316 typedef struct
317 {
318    uint8_t ID;
319    char dummy[3];
320    uint32_t BlackRows;
321    uint32_t CMYRows;
322 } __attribute__((packed)) MFPDTF_END_PAGE;
323
324 /* Folloing macros are now in gcc 4.3.2 endian.h */
325 #if !defined(htole16) 
326 #if defined(WORDS_BIGENDIAN)
327 #define htole16(A) ((((uint16_t)(A) & 0xff00) >> 8) | (((uint16_t)(A) & 0x00ff) << 8))    /* host to little-endian 16-bit value */
328 #define le16toh htole16                         /* little-endian to host 16-bit value */
329 #define htole32(A) ((((uint32_t)(A) & (uint32_t)0x000000ff) << 24) | (((uint32_t)(A) & (uint32_t)0x0000ff00) << 8) | \
330                   (((uint32_t)(A) & (uint32_t)0x00ff0000) >> 8) | (((uint32_t)(A) & (uint32_t)0xff000000) >> 24))
331 #define le32toh htole32
332 #else
333 #define htole16(A) (A)
334 #define le16toh(A) (A)
335 #define le32toh(A) (A)
336 #define htole32(A) (A)
337 #endif   // #if defined(WORDS_BIGENDIAN)
338 #endif   // #if !defined(htole16)
339
340 int __attribute__ ((visibility ("hidden"))) read_mfpdtf_block(int device, int channel, char *buf, int bufSize, int timeout);
341
342 #endif  // _MFPDTF_H