1 /* SANE - Scanner Access Now Easy.
2 
3    Copyright (C) 2011-2020 Rolf Bensch <rolf at bensch hyphen online dot de>
4    Copyright (C) 2006-2007 Wittawat Yamwong <wittawat@web.de>
5 
6    This file is part of the SANE package.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 
21    As a special exception, the authors of SANE give permission for
22    additional uses of the libraries contained in this release of SANE.
23 
24    The exception is that, if you link a SANE library with other files
25    to produce an executable, this does not by itself cause the
26    resulting executable to be covered by the GNU General Public
27    License.  Your use of that executable is in no way restricted on
28    account of linking the SANE library code into it.
29 
30    This exception does not, however, invalidate any other reasons why
31    the executable file might be covered by the GNU General Public
32    License.
33 
34    If you submit changes to SANE to the maintainers to be included in
35    a subsequent release, you agree by submitting the changes that
36    those changes may be distributed with this exception intact.
37 
38    If you write modifications of your own for SANE, it is your choice
39    whether to permit this exception to apply to your modifications.
40    If you do not wish that, delete this exception notice.
41  */
42 #ifndef PIXMA_COMMON_H
43 #define PIXMA_COMMON_H
44 
45 
46 #include <time.h>		/* time_t */
47 #include "pixma.h"
48 
49 
50 /*! \defgroup subdriver Subdriver Interface
51  *  \brief Subdriver interface. */
52 
53 /*! \defgroup debug Debug utilities
54  *  \brief Debug utilities. */
55 
56 #ifdef NDEBUG
57 # define PDBG(x)     do {} while(0)
58 # define PASSERT(x)  do {} while(0)
59 #else
60 # define PDBG(x) x
61 # define PASSERT(x) do {				\
62     if (!(x))						\
63 	pixma_dbg(1, "ASSERT failed:%s:%d: "	\
64 		     #x "\n", __FILE__, __LINE__);	\
65   } while(0)
66 #endif
67 
68 
69 #define PIXMA_STATUS_OK       0x0606
70 #define PIXMA_STATUS_FAILED   0x1515
71 #define PIXMA_STATUS_BUSY     0x1414
72 
73 #define PIXMA_MAX_ID_LEN 30
74 
75 /* These may have been defined elsewhere */
76 #ifndef MIN
77 #define MIN(x,y) (((x) < (y)) ? (x):(y))
78 #endif
79 #ifndef MAX
80 #define MAX(x,y) (((x) < (y)) ? (y):(x))
81 #endif
82 #define ALIGN_SUP(x,n) (((x) + (n) - 1) / (n) * (n))
83 #define ALIGN_INF(x,n) (((x) / (n)) * (n))
84 
85 struct pixma_io_t;
86 
87 struct pixma_limits_t
88 {
89   unsigned xdpi, ydpi;
90   unsigned width, height;
91 };
92 
93 struct pixma_cmdbuf_t
94 {
95   unsigned cmd_header_len, res_header_len, cmd_len_field_ofs;
96   unsigned expected_reslen, cmdlen;
97   int reslen;
98   unsigned size;
99   uint8_t *buf;
100 };
101 
102 struct pixma_imagebuf_t
103 {
104   uint8_t *wptr, *wend;
105   const uint8_t *rptr, *rend;
106 };
107 
108 struct pixma_t
109 {
110   pixma_t *next;
111   struct pixma_io_t *io;
112   const pixma_scan_ops_t *ops;
113   pixma_scan_param_t *param;
114   const pixma_config_t *cfg;
115   char id[PIXMA_MAX_ID_LEN + 1];
116   int cancel;			/* NOTE: It can be set in a signal handler. */
117   uint32_t events;
118   void *subdriver;		/* can be used by model driver. */
119   int rec_tmo;                  /* receive timeout [s] */
120   pixma_paper_source_t last_source; /* used for calibrate=once */
121 
122   /* private */
123   uint64_t cur_image_size;
124   pixma_imagebuf_t imagebuf;
125   unsigned scanning:1;
126   unsigned underrun:1;
127 };
128 
129 /** \addtogroup subdriver
130  * @{ */
131 /** Scan operations for subdriver. */
132 struct pixma_scan_ops_t
133 {
134     /** Allocate a data structure for the subdriver. It is called after the
135      *  core driver connected to the scanner. The subdriver should reset the
136      *  scanner to a known state in this function. */
137   int (*open) (pixma_t *);
138 
139     /** Free resources allocated by the subdriver. Don't forget to send abort
140      *  command to the scanner if it is scanning. */
141   void (*close) (pixma_t *);
142 
143     /** Setup the scanner for scan parameters defined in \a s->param. */
144   int (*scan) (pixma_t * s);
145 
146     /** Fill a buffer with image data. The subdriver has two choices:
147      * -# Fill the buffer pointed by ib->wptr directly and leave
148      *    ib->rptr and ib->rend untouched. The length of the buffer is
149      *    ib->wend - ib->wptr. It must update ib->wptr accordingly.
150      * -# Update ib->rptr and ib->rend to point to the beginning and
151      *    the end of the internal buffer resp. The length of the buffer
152      *    is ib->rend - ib->rptr. This function is called again if
153      *    and only if pixma_read_image() has copied the whole buffer.
154      *
155      * The subdriver must wait until there is at least one byte to read or
156      * return 0 for the end of image. */
157   int (*fill_buffer) (pixma_t *, pixma_imagebuf_t * ib);
158 
159     /** Cancel the scan operation if necessary and free resources allocated in
160      *  scan(). */
161   void (*finish_scan) (pixma_t *);
162 
163     /** [Optional] Wait for a user's event, e.g. button event. \a timeout is
164      *  in milliseconds. If an event occurred before it's timed out, flags in
165      *  \a s->events should be set accordingly.
166      *  \see PIXMA_EV_* */
167   void (*wait_event) (pixma_t * s, int timeout);
168 
169     /** Check the scan parameters. The parameters can be adjusted if they are
170      *  out of range, e.g. width > max_width. */
171   int (*check_param) (pixma_t *, pixma_scan_param_t *);
172 
173     /** Read the device status. \see pixma_get_device_status() */
174   int (*get_status) (pixma_t *, pixma_device_status_t *);
175 };
176 
177 
178 /** \name Functions for read and write big-endian integer values */
179 /**@{*/
180 void pixma_set_be16 (uint16_t x, uint8_t * buf);
181 void pixma_set_be32 (uint32_t x, uint8_t * buf);
182 uint16_t pixma_get_be16 (const uint8_t * buf);
183 uint32_t pixma_get_be32 (const uint8_t * buf);
184 /**@}*/
185 
186 /** \name Utility functions */
187 /**@{*/
188 uint8_t pixma_sum_bytes (const void *data, unsigned len);
189 int pixma_check_dpi (unsigned dpi, unsigned max);
190 void pixma_sleep (unsigned long usec);
191 void pixma_get_time (time_t * sec, uint32_t * usec);
192 uint8_t * pixma_r_to_ir (uint8_t * gptr, uint8_t * sptr, unsigned w, unsigned c);
193 uint8_t * pixma_rgb_to_gray (uint8_t * gptr, uint8_t * sptr, unsigned w, unsigned c);
194 uint8_t * pixma_binarize_line(pixma_scan_param_t *, uint8_t * dst, uint8_t * src, unsigned width, unsigned c);
195 /**@}*/
196 
197 /** \name Command related functions */
198 /**@{*/
199 int pixma_cmd_transaction (pixma_t *, const void *cmd, unsigned cmdlen,
200 			   void *data, unsigned expected_len);
201 int pixma_check_result (pixma_cmdbuf_t *);
202 uint8_t *pixma_newcmd (pixma_cmdbuf_t *, unsigned cmd,
203 		       unsigned dataout, unsigned datain);
204 int pixma_exec (pixma_t *, pixma_cmdbuf_t *);
205 int pixma_exec_short_cmd (pixma_t *, pixma_cmdbuf_t *, unsigned cmd);
206 int pixma_map_status_errno (unsigned status);
207 #if defined(HAVE_LIBXML2)
208 int pixma_parse_xml_response(const char *xml_message);
209 #endif
210 /**@}*/
211 
212 #define pixma_fill_checksum(start, end) do {		\
213     *(end) = -pixma_sum_bytes(start, (end)-(start));	\
214 } while(0)
215 
216 /** @} end of group subdriver */
217 
218 /** \addtogroup debug
219  *  @{ */
220 void pixma_set_debug_level (int level);
221 #ifndef NDEBUG
222 void pixma_hexdump (int level, const void *d_, unsigned len);
223 
224 /* len:   length of data or error code.
225    size:  if >= 0, force to print 'size' bytes.
226    max:   maximum number of bytes to print(-1 means no limit). */
227 void pixma_dump (int level, const char *type, const void *data, int len,
228 		 int size, int max);
229 #  define DEBUG_DECLARE_ONLY
230 #  include "../include/sane/sanei_debug.h"
231 #endif /* NDEBUG */
232 /** @} end of group debug */
233 
234 #endif
235