1 /*___________________________________  epson-cbt.c   ___________________________________*/
2 
3 /*       1         2         3         4         5         6         7         8        */
4 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
5 /*******************************************|********************************************/
6 /*
7  *   Copyright (c) 2009  Seiko Epson Corporation   All rights reserved.
8  *
9  *   Copyright protection claimed includes all forms and matters of copyrightable
10  *   material and information now allowed by statutory or judicial law or hereinafter
11  *   granted, including without limitation, material generated from the software
12  *   programs which are displayed on the screen such as icons, screen display looks,
13  *   etc.
14  */
15 /*******************************************|********************************************/
16 /*                                                                                      */
17 /*                                   Epson CBT Module                                   */
18 /*                                                                                      */
19 /*                                Public Function Calls                                 */
20 /*                              --------------------------                              */
21 /*              EPS_ERR_CODE cbtCommOpen         (void                          );      */
22 /*              EPS_INT32    cbtCommClose        (void                          );      */
23 /*              EPS_ERR_CODE cbtCommChannelOpen  (Channel, bRetry               );      */
24 /*              EPS_INT32    cbtCommChannelClose (Channel                       );      */
25 /*              EPS_INT32    cbtCommReadData     (Channel, Buffer, BuffLen, Size);      */
26 /*              EPS_INT32    cbtCommWriteData    (Channel, Buffer, BuffLen, Size);      */
27 /*                                                                                      */
28 /*******************************************|********************************************/
29 
30 /*------------------------------------  Includes   -------------------------------------*/
31 /*******************************************|********************************************/
32 #include "epson-escpr-pvt.h"
33 #include "epson-escpr-err.h"
34 #include "epson-escpr-mem.h"
35 #include "epson-escpr-services.h"
36 #include "epson-cbt.h"
37 
38 /*------------------------------------  Definition   -----------------------------------*/
39 /*******************************************|********************************************/
40 #define CBT_OPENCH_WAIT				(50)
41 #define CBT_OPENCH_RETRY            (50)
42 
43 /*------------------------------- Local Compiler Switch --------------------------------*/
44 /*******************************************|********************************************/
45 #define LCOMSW_CBT_SOCKETFUNC_ON       1    /* the socket ID mode (0:OFF  1:ON(Default))*/
46 
47 #define LCOMSW_CBT_ALLOC_MEM           1    /* Use static memory                        */
48                                             /*  0 -> Use static memory                  */
49                                             /*  1 -> Use epsFnc.memAlloc() (Default)    */
50 
51 #ifdef EPS_LOG_MODULE_CBT
52 #define EPS_LOG_MODULE	EPS_LOG_MODULE_CBT
53 #else
54 #define EPS_LOG_MODULE	0
55 #endif
56 
57 /*----------------------------  ESC/P-R Lib Global Variables  --------------------------*/
58 /*******************************************|********************************************/
59 
60     /*** Print Job/Page Struct                                                          */
61     /*** -------------------------------------------------------------------------------*/
62 extern EPS_PRINT_JOB    printJob;
63 
64     /*** System Dependent Function (USB Communication)                                  */
65     /*** -------------------------------------------------------------------------------*/
66 extern EPS_USB_FUNC  epsUsbFnc;
67 extern EPS_CMN_FUNC  epsCmnFnc;
68 
69     /*** I/O Channel                                                                    */
70     /*** -------------------------------------------------------------------------------*/
71 EPS_BOOL    ioOpenState     = EPS_IO_NOT_OPEN; /* Open state of I/O port (Bi-Directional)  */
72 EPS_BOOL    ioDataChState	= EPS_IO_NOT_OPEN; /* Open state of Data Channel               */
73 EPS_BOOL    ioControlChState= EPS_IO_NOT_OPEN; /* Open state of Control Channel            */
74 EPS_BOOL    ioOpenUniDirect = EPS_IO_NOT_OPEN; /* Open state of I/O port (Uni-Directional) */
75 
76 /*---------------------------  Data Structure Declarations   ---------------------------*/
77 /*******************************************|********************************************/
78 
79 /*------------------------  Epson CBT Module Global Variables  -------------------------*/
80 /*******************************************|********************************************/
81 static CBTS_PRNINFO *gpCbtPrnInfo = NULL;       /* Printer Information                  */
82 
83 /*--------------------------------  Local Definition   ---------------------------------*/
84 /*******************************************|********************************************/
85 #define CBT_DUMYREAD_MAX	(128)				/* dummy read try max                   */
86 
87 /*---------------------  Memory allocation for Epson CBT Module ------------------------*/
88 /*******************************************|********************************************/
89 #if    LCOMSW_CBT_ALLOC_MEM == 0
90 static CBTS_PRNINFO sgcbtprninfo;
91 static EPS_UINT8    sgreadbuff[CBT_MAX_CH][CBT_MAX_RTMP];
92 static EPS_UINT8    sgrtmpbuff[CBT_MAX_CH][CBT_MAX_RTMP];
93 static EPS_UINT8    sgwritebuff[CBT_TXPSIZE];
94 static EPS_UINT8    sgcbtreadrtnbuff[CBT_MAX_RTMP];
95 static EPS_UINT8    sgcbtdatareadbuff[CBT_MAX_RTMP];
96 #endif    /* LCOMSW_CBT_ALLOC_MEM */
97 
98 /*------------------------------------  Debug Dump   -----------------------------------*/
99 /*******************************************|********************************************/
100     /*** ALL Debug Dump Switch for <epson-cbt.>                                         */
101     /*** -------------------------------------------------------------------------------*/
102 #define _COMM_DEBUG         0           /* 0: OFF    1: ON                              */
103 #define _CBTDEBUG_          0           /* 0: OFF    1: ON                              */
104 #define CBT_DBG_ERROR       0           /* 0: OFF    1: ON                              */
105 
106     /*** _COMM_DEBUG --- Communication Module Debug                                     */
107     /***--------------------------------------------------------------------------------*/
108 #if _COMM_DEBUG
109 #define COMM_DBG_ERROR
110 #define CBTDebugPrintf(a)  printf a
111 #else   /* def _COMM_DEBUG */
112 #define CBTDebugPrintf(a)
113 #endif  /* def _COMM_DEBUG */
114 
115     /*** _CBTDEBUG_ --- CBT Debug                                                       */
116     /***--------------------------------------------------------------------------------*/
117 #if _CBTDEBUG_
118 #define CBT_DBG_IO
119 
120 static EPS_INT8 debMsgWork[512];
DebMsgOut(EPS_INT8 * buf)121 static void     DebMsgOut(EPS_INT8 *buf)
122 {
123 #ifdef _WINCE32_TEST_
124     HANDLE fh_Alloc;
125     DWORD  wb;
126 
127     fh_Alloc = CreateFile(L"\\CBT_LOG.TXT",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,
128                           OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
129     SetFilePointer(fh_Alloc,0,NULL,FILE_END);
130     WriteFile(fh_Alloc, buf, strlen(buf), &wb, NULL);
131     CloseHandle(fh_Alloc);
132 #else  /* def _WINCE32_TEST_ */
133     fprintf(stderr,buf);
134 #endif /* def _WINCE32_TEST_ */
135 }
136 #endif  /* def _CBTDEBUG_ */
137 
138 /*--------------------------------  Local Functions   ----------------------------------*/
139 /*******************************************|********************************************/
140     /*** Communication Module                                                           */
141     /*** -------------------------------------------------------------------------------*/
142 static EPS_INT32    commOpen                (const EPS_USB_DEVICE*, EPS_FILEDSC*        );
143 static EPS_INT32    commClose               (EPS_FILEDSC                                );
144 static EPS_INT32    commOpenChannel         (EPS_FILEDSC, EPS_INT32, EPS_BOOL           );
145 static EPS_INT32    commCloseChannel        (EPS_FILEDSC, EPS_INT32                     );
146 static EPS_INT32    commChChange            (EPS_INT32, EPS_UINT8*                      );
147 
148     /*** Mini CBT Engine                                                                */
149     /*** -------------------------------------------------------------------------------*/
150 static EPS_INT16    EPCBT_Open              (EPS_FILEDSC, void*, EPS_WritePortal, EPS_ReadPortal);
151 static EPS_INT16    EPCBT_Close             (EPS_FILEDSC                                );
152 static EPS_INT16    EPCBT_OpenChannel       (EPS_FILEDSC, EPS_UINT8, EPS_BOOL           );
153 static EPS_INT16    EPCBT_CloseChannel      (EPS_FILEDSC, EPS_UINT8                     );
154 static EPS_INT16    EPCBT_Write             (EPS_FILEDSC, EPS_UINT8, const EPS_UINT8*, EPS_INT32*);
155 static EPS_INT16    EPCBT_Read              (EPS_FILEDSC, EPS_UINT8, EPS_UINT8*, EPS_INT32*          );
156 #if LCOMSW_CBT_SOCKETFUNC_ON
157 static EPS_INT16    EPCBT_GetSocketID       (EPS_FILEDSC, EPS_UINT8*, EPS_UINT8*, EPS_INT32          );
158 #endif
159 static EPS_INT16    CbtWriteRtn             (EPS_FILEDSC, CBTS_PRNINFO*, const EPS_UINT8*, EPS_INT32, EPS_INT32*);
160 static EPS_INT16    CbtReadRtn              (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8, EPS_UINT8*, EPS_INT32, EPS_INT32*, EPS_UINT8);
161 static void         CbtDummyRead            (EPS_FILEDSC, CBTS_PRNINFO*                 );
162 static void         CbtChDummyRead          (EPS_FILEDSC, CBTS_PRNINFO*, CBTS_CHINFO*   );
163 static EPS_INT16    CbtReplyCheck           (EPS_UINT8*, EPS_INT32, EPS_UINT8           );
164 static EPS_INT16    CbtEpsonPacking         (EPS_FILEDSC, CBTS_PRNINFO*                 );
165 static EPS_INT16    CbtInitCommand          (EPS_FILEDSC, CBTS_PRNINFO*                              );
166 static EPS_INT32    CbtOpenChannelCommand   (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8, EPS_UINT32*, EPS_UINT32*, EPS_UINT32, EPS_UINT32);
167 static EPS_INT16    CbtCloseChannelCommand  (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8                   );
168 static EPS_INT32    CbtCreditReqCommand     (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8, EPS_UINT32, EPS_UINT32);
169 static EPS_INT16    CbtCreditCommand        (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8, EPS_UINT32       );
170 #if LCOMSW_CBT_SOCKETFUNC_ON
171 static EPS_INT16    CbtGetSocketIDCommand   (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8*, EPS_INT32, EPS_UINT8*);
172 #endif
173 static EPS_INT32    CbtDataWrite            (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8, const EPS_UINT8*, EPS_INT32);
174 static EPS_INT16    CbtDataRead             (EPS_FILEDSC, CBTS_PRNINFO*, EPS_UINT8, EPS_UINT8*, EPS_INT32*);
175 static EPS_INT16    CbtMemAlloc             (void                                       );
176 static void         CbtMemFree              (void                                       );
177 static void         CbtPutBigEndianByte2    (EPS_UINT32, EPS_UINT8*                     );
178 
179 
180 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
181 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
182 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
183 /*--------------------              Public Functions               ---------------------*/
184 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
185 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
186 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
187 
188 /*******************************************|********************************************/
189 /*                                                                                      */
190 /* Function name:   cbtCommOpen()                                                       */
191 /*                                                                                      */
192 /* Arguments                                                                            */
193 /* ---------                                                                            */
194 /* Name:        Type:               Description:                                        */
195 /* devinfo      EPS_USB_DEVICE      I: Pointer to a usb device infomation               */
196 /* fd           EPS_FILEDSC*        O: file discripter                                  */
197 /*                                                                                      */
198 /* Return value:                                                                        */
199 /*      EPS_ERR_NONE                    - Success (Opened Communication)                */
200 /*      EPSCBT_ERR_2NDOPEN              - CBT channels are already opened               */
201 /*      EPS_ERR_NOT_OPEN_IO             - Cannot Open I/O Portal                        */
202 /*      EPCBT_ERR_CBT2NDOPEN            - CBT channels are already opened               */
203 /*      EPCBT_ERR_MEMORY                - Fail to memory allocation                     */
204 /*      EPCBT_ERR_CBTDISABLE            - Fail to CBT communication                     */
205 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
206 /*      EPCBT_ERR_INITDENIED            - Printer cannot start CBT mode                 */
207 /*      EPCBT_ERR_VERSION               - Incompatible version                          */
208 /*      EPCBT_ERR_INITFAILED            - Not used by EPSON                             */
209 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
210 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
211 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
212 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
213 /*      EPCBT_ERR_NOSERVICE             - The channel does not support the requested    */
214 /*                                        service                                       */
215 /*                                                                                      */
216 /* Description:                                                                         */
217 /*     Opens the communication on the specified.                                        */
218 /*                                                                                      */
219 /*******************************************|********************************************/
cbtCommOpen(const EPS_USB_DEVICE * devinfo,EPS_FILEDSC * fd)220 EPS_INT32   cbtCommOpen (
221 
222         const EPS_USB_DEVICE*	devinfo,
223 		EPS_FILEDSC*			fd
224 ){
225 
226     EPS_INT32 Ret;                              /* Return status of internal calls      */
227 
228 	EPS_LOG_FUNCIN;
229 
230 /*** Open Communication                                                                 */
231     if ((Ret = commOpen(devinfo, fd)) != EPCBT_ERR_NONE){
232         CBTDebugPrintf(("EPS CBT: Open Failed [%d]\r\n",Ret));
233 		EPS_RETURN( Ret );
234     }
235 
236     EPS_RETURN( EPS_ERR_NONE );
237 }
238 
239 /*******************************************|********************************************/
240 /*                                                                                      */
241 /* Function name:   cbtCommClose()                                                      */
242 /*                                                                                      */
243 /* Arguments                                                                            */
244 /* ---------                                                                            */
245 /* Name:        Type:               Description:                                        */
246 /* fd           EPS_FILEDSC         I: file discripter                                  */
247 /*                                                                                      */
248 /* Return value:                                                                        */
249 /*      EPS_ERR_NONE                    - Closed Communication                          */
250 /*      EPSCBT_ERR_NOTOPEN              - I/O port is not opened                        */
251 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
252 /*                                                                                      */
253 /* Description:                                                                         */
254 /*      Closes the communication on the active printer.                                 */
255 /*                                                                                      */
256 /*******************************************|********************************************/
cbtCommClose(EPS_FILEDSC fd)257 EPS_INT32   cbtCommClose (
258 
259         EPS_FILEDSC		fd
260 
261 ){
262     EPS_INT32 Ret;
263 
264 	EPS_LOG_FUNCIN;
265 
266 	CBTDebugPrintf(("EPS CBT:CLOSE --> Closing Communication on the device\r\n"));
267 
268     Ret = commClose(fd);
269 
270     if (Ret != EPCBT_ERR_NONE){
271         CBTDebugPrintf(("EPS CBT: Close failed\r\n"));
272         EPS_RETURN( Ret );
273     }
274     EPS_RETURN( EPS_ERR_NONE );
275 }
276 
277 /*******************************************|********************************************/
278 /*                                                                                      */
279 /* Function name:   cbtCommChannelOpen()                                                */
280 /*                                                                                      */
281 /* Arguments                                                                            */
282 /* ---------                                                                            */
283 /* Name:        Type:               Description:                                        */
284 /* fd           EPS_FILEDSC         I: file discripter                                  */
285 /* Channel      EPS_INT32           I: Communication Channel                            */
286 /* bRetry       EPS_BOOL            I: If TRUE, retry open                              */
287 /*                                                                                      */
288 /* Return value:                                                                        */
289 /*      EPS_ERR_NONE                    - Success (Opened Communication)                */
290 /*      EPSCBT_ERR_NOTOPEN              - CBT channels are not opened                   */
291 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
292 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
293 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
294 /*      EPCBT_ERR_CH2NDOPEN             - Channel is already open                       */
295 /*      EPCBT_ERR_RPLYPSIZE             - Size of packet from OpenChannel is invalid    */
296 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
297 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
298 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
299 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
300 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
301 /*      EPCBT_ERR_RESOURCE              - Insufficient printer resources for            */
302 /*                                        OpenChannel                                   */
303 /*      EPCBT_ERR_OPENCHANNEL           - Not used by EPSON                             */
304 /*      EPCBT_ERR_CHNOTSUPPORT          - The channel is not supported                  */
305 /*      EPCBT_ERR_PACKETSIZE            - Invalid packet size.                          */
306 /*                                        (0x0001 ? 0x0005 bytes)                       */
307 /*      EPCBT_ERR_NULLPACKETSZ          - Packet size is 0x0000 in both directions.     */
308 /*                                        No data can be transferred. Channel was       */
309 /*                                        not opened                                    */
310 /*                                                                                      */
311 /* Description:                                                                         */
312 /*      Open the communication channel.                                                 */
313 /*                                                                                      */
314 /*******************************************|********************************************/
cbtCommChannelOpen(EPS_FILEDSC fd,EPS_INT32 Channel,EPS_BOOL bRetry)315 EPS_INT32   cbtCommChannelOpen (
316 
317 		EPS_FILEDSC	fd,
318         EPS_INT32   Channel,
319 		EPS_BOOL	bRetry
320 
321 ){
322     EPS_INT32 Ret;
323 
324 	EPS_LOG_FUNCIN;
325 
326     if ((Ret = commOpenChannel(fd, Channel, bRetry)) != EPCBT_ERR_NONE){
327 
328         if (Channel == EPS_CBTCHANNEL_CTRL) {
329             CBTDebugPrintf(("EPS CBT: Control Channel Open Failed\r\n"));
330             EPS_RETURN( Ret );
331         } else if (Channel == EPS_CBTCHANNEL_DATA) {
332             CBTDebugPrintf(("EPS CBT: Data Channel Open Failed\r\n"));
333             EPS_RETURN( Ret );
334         }
335 
336     }
337 
338     EPS_RETURN( EPS_ERR_NONE );
339 }
340 
341 /*******************************************|********************************************/
342 /*                                                                                      */
343 /* Function name:   cbtCommChannelClose()                                               */
344 /*                                                                                      */
345 /* Arguments                                                                            */
346 /* ---------                                                                            */
347 /* Name:        Type:               Description:                                        */
348 /* fd           EPS_FILEDSC         I: file discripter                                  */
349 /* Channel      EPS_INT32           I: Communication Channel                            */
350 /*                                                                                      */
351 /* Return value:                                                                        */
352 /*      EPS_ERR_NONE                    - Success (Closed Communication)                */
353 /*      EPSCBT_ERR_NOTOPEN              - CBT channels are not opened                   */
354 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
355 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
356 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
357 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
358 /*      EPCBT_ERR_CLOSEDENIED           - Close Channel is denied                       */
359 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
360 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
361 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
362 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
363 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
364 /*                                                                                      */
365 /* Description:                                                                         */
366 /*      Close the communication channel.                                                */
367 /*                                                                                      */
368 /*******************************************|********************************************/
cbtCommChannelClose(EPS_FILEDSC fd,EPS_INT32 Channel)369 EPS_INT32   cbtCommChannelClose (
370 
371 		EPS_FILEDSC	fd,
372         EPS_INT32   Channel
373 
374 ){
375     EPS_INT32 Ret;
376 
377 	EPS_LOG_FUNCIN;
378 
379 	if ((Ret = commCloseChannel(fd, Channel)) != EPCBT_ERR_NONE){
380         CBTDebugPrintf(("EPS CBT: Channel Close Failed\r\n"));
381         EPS_RETURN( Ret );
382     }
383 
384     EPS_RETURN( EPS_ERR_NONE );
385 }
386 
387 /*******************************************|********************************************/
388 /*                                                                                      */
389 /* Function name:   cbtCommReadData()                                                   */
390 /*                                                                                      */
391 /* Arguments                                                                            */
392 /* ---------                                                                            */
393 /* Name:        Type:               Description:                                        */
394 /* fd           EPS_FILEDSC         I: file discripter                                  */
395 /* Channel      EPS_INT32           I: Channel-Select                                   */
396 /*                                      00: Data Channel                                */
397 /*                                      01: Control Channel                             */
398 /* Buffer       EPS_UINT8*          I: Buffer Pointer for Read Data                     */
399 /* BuffLen      EPS_INT32           I: Read Data Buffer Length (bytes)                  */
400 /* Size         EPS_INT32*          O: Actuall Read Size                                */
401 /*                                                                                      */
402 /* Return value:                                                                        */
403 /*      EPCBT_ERR_NONE                  - Success                                       */
404 /*      EPSCBT_ERR_NOTOPEN              - I/O port is not opened                        */
405 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
406 /*      EPCBT_ERR_CBTNOTOPEN            - CBT is not opened                             */
407 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
408 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
409 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
410 /*      EPCBT_ERR_CREDITOVF             - Credit over flow                              */
411 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
412 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
413 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
414 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
415 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
416 /*      EPCBT_ERR_READERROR             - Data read error                               */
417 /*                                                                                      */
418 /* Description:                                                                         */
419 /*      Read the data from printer.                                                     */
420 /*                                                                                      */
421 /*******************************************|********************************************/
cbtCommReadData(EPS_FILEDSC fd,EPS_INT32 Channel,EPS_UINT8 * Buffer,EPS_INT32 BuffLen,EPS_INT32 * Size)422 EPS_INT32   cbtCommReadData (
423 
424 		EPS_FILEDSC	fd,
425         EPS_INT32   Channel,
426         EPS_UINT8*  Buffer,
427         EPS_INT32   BuffLen,
428         EPS_INT32*  Size
429 
430 ){
431     EPS_INT32   Ret   = EPCBT_ERR_NONE;
432     EPS_INT32   InLen = 0;
433     EPS_UINT8   CBTCh = 0;
434 
435 	EPS_LOG_FUNCIN;
436 
437 	if ( ioOpenState == EPS_IO_NOT_OPEN )          /* Open check */
438         Ret = EPSCBT_ERR_NOTOPEN;
439     else
440     {
441         Ret = commChChange( Channel, &CBTCh );              /* channel change */
442         if( Ret == EPCBT_ERR_NONE )
443         {
444             *Size = 0;                                      /* size clear */
445             Ret = EPCBT_Read( fd, CBTCh, NULL, &InLen );        /* Size Get */
446             if ( Ret == EPCBT_ERR_NONE )
447             {
448                 if ( InLen > BuffLen )                      /* small buffer size ? */
449                     InLen = BuffLen;
450                 Ret = EPCBT_Read( fd, CBTCh, Buffer, &InLen );   /* Data Get */
451                 *Size = InLen;
452             }
453         }
454     }
455 
456 #ifdef COMM_DBG_ERROR
457     if ( Ret != EPCBT_ERR_NONE )
458         CBTDebugPrintf(("<<  ReadData Ret[%d] Size[%d]\r\n", Ret, *Size));
459 #endif
460 
461     EPS_RETURN( Ret );
462 }
463 
464 /*******************************************|********************************************/
465 /*                                                                                      */
466 /* Function name:   cbtCommWriteData()                                                  */
467 /*                                                                                      */
468 /* Arguments                                                                            */
469 /* ---------                                                                            */
470 /* Name:        Type:               Description:                                        */
471 /* fd           EPS_FILEDSC         I: file discripter                                  */
472 /* Channel      EPS_INT32           I: Channel-Select                                   */
473 /*                                      00: Data Channel                                */
474 /*                                      01: Control Channel                             */
475 /* Buffer       const EPS_UINT8*    I: Buffer Pointer for Write Data                    */
476 /* BuffLen      EPS_INT32           I: Write Data Buffer Length (bytes)                 */
477 /* Size         EPS_INT32*          O: Actuall Write Size                               */
478 /*                                                                                      */
479 /* Return value:                                                                        */
480 /*      EPCBT_ERR_NONE                  - Success                                       */
481 /*      EPSCBT_ERR_NOTOPEN              - I/O port is not opened                        */
482 /*      EPCBT_ERR_CBTNOTOPEN            - CBT is not opened                             */
483 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
484 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
485 /*      EPCBT_ERR_FNCDISABLE            - Function is disable to be completely finished */
486 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
487 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
488 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
489 /*      EPCBT_ERR_WRITEERROR            - Failed to write                               */
490 /*                                                                                      */
491 /* Description:                                                                         */
492 /*      Write the data to printer.                                                      */
493 /*                                                                                      */
494 /*******************************************|********************************************/
cbtCommWriteData(EPS_FILEDSC fd,EPS_INT32 Channel,const EPS_UINT8 * Buffer,EPS_INT32 BuffLen,EPS_INT32 * Size)495 EPS_INT32   cbtCommWriteData (
496 
497 		EPS_FILEDSC			fd,
498         EPS_INT32           Channel,
499         const EPS_UINT8*    Buffer,
500         EPS_INT32           BuffLen,
501         EPS_INT32*          Size
502 
503 ){
504     EPS_INT32   Ret = EPCBT_ERR_NONE;
505     EPS_INT32   Written;
506     EPS_INT32   RestSize;
507     EPS_UINT8   CBTCh = 0;
508 
509 	EPS_LOG_FUNCIN;
510 
511 	CBTDebugPrintf(("EPS CBT : cbtCommWriteData -> ioOpenState == %s\r\n", ioOpenState == EPS_IO_OPEN ? "ON" : "OFF"));
512 
513     if ( ioOpenState == EPS_IO_NOT_OPEN ) {                    /* Open check */
514         Ret = EPSCBT_ERR_NOTOPEN;
515     }
516     else
517     {
518         /* Send Data */
519         Ret = commChChange( Channel, &CBTCh );                          /* channel change */
520         if( Ret == EPCBT_ERR_NONE )
521         {
522             *Size    = 0;                                               /* size clear */
523             RestSize = BuffLen;
524 
525             while ( RestSize > 0 )
526             {
527                 Written = RestSize;
528                 Ret = EPCBT_Write(fd, CBTCh, &Buffer[*Size], &Written );    /* Write */
529                 *Size    += Written;                                    /* Set Write Length */
530                 RestSize -= Written;
531 
532                 if( Ret != EPCBT_ERR_NONE )
533                     break;
534             }
535         }
536     }
537 
538 #ifdef COMM_DBG_ERROR
539     if ( Ret != EPCBT_ERR_NONE )
540         CBTDebugPrintf(("<<  WriteData Ret[%d] Size[%d]\r\n", Ret, *Size));
541 #endif
542 
543     EPS_RETURN( Ret );
544 }
545 
546 
547 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
548 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
549 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
550 /*--------------------               Local Functions               ---------------------*/
551 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/
552 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
553 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
554 
555 /*******************************************|********************************************/
556 /*                                                                                      */
557 /* Function name:   commOpen()                                                          */
558 /*                                                                                      */
559 /* Arguments                                                                            */
560 /* ---------                                                                            */
561 /* Name:        Type:               Description:                                        */
562 /* devinfo      EPS_USB_DEVICE      I: Pointer to a usb device infomation               */
563 /* fd           EPS_FILEDSC*        O: file discripter                                  */
564 /*                                                                                      */
565 /* Return value:                                                                        */
566 /*      EPCBT_ERR_NONE                  - Success                                       */
567 /*      EPSCBT_ERR_2NDOPEN              - CBT channels are already opened               */
568 /*      EPS_ERR_NOT_OPEN_IO             - Cannot Open I/O Portal                        */
569 /*      EPCBT_ERR_CBT2NDOPEN            - CBT channels are already opened               */
570 /*      EPCBT_ERR_MEMORY                - Fail to memory allocation                     */
571 /*      EPCBT_ERR_CBTDISABLE            - Fail to CBT communication                     */
572 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
573 /*      EPCBT_ERR_INITDENIED            - Printer cannot start CBT mode                 */
574 /*      EPCBT_ERR_VERSION               - Incompatible version                          */
575 /*      EPCBT_ERR_INITFAILED            - Not used by EPSON                             */
576 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
577 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
578 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
579 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
580 /*      EPCBT_ERR_NOSERVICE             - The channel does not support the requested    */
581 /*                                        service                                       */
582 /*                                                                                      */
583 /* Description:                                                                         */
584 /*      Opens the communication.                                                        */
585 /*                                                                                      */
586 /*******************************************|********************************************/
commOpen(const EPS_USB_DEVICE * devinfo,EPS_FILEDSC * fd)587 static EPS_INT32    commOpen (
588 
589         const EPS_USB_DEVICE*	devinfo,
590 		EPS_FILEDSC*			fd
591 
592 ){
593     EPS_INT32   Ret = EPCBT_ERR_NONE;
594     EPS_UINT8   Sid = 0;
595 
596 	EPS_LOG_FUNCIN;
597 
598     if ( ioOpenState == EPS_IO_OPEN ) {        /* Open check */
599         Ret = EPSCBT_ERR_2NDOPEN;
600     } else {
601         if ( (*fd = epsUsbFnc.openPortal(devinfo)) == EPS_INVALID_FILEDSC)
602             EPS_RETURN( EPS_ERR_NOT_OPEN_IO );
603 
604         /* Open process */
605         Ret = EPCBT_Open(*fd, NULL, epsUsbFnc.writePortal, epsUsbFnc.readPortal);        /* Open */
606 
607         if ( Ret == EPCBT_ERR_NONE ) {
608             /* channel confirm */
609             Ret = EPCBT_GetSocketID( *fd, &Sid, (EPS_UINT8*)"EPSON-DATA", 10 );
610             if ( Ret == EPCBT_ERR_NONE )
611                 Ret = EPCBT_GetSocketID( *fd, &Sid, (EPS_UINT8*)"EPSON-CTRL", 10 );
612 
613 			if(EPCBT_ERR_NONE != Ret ){
614 				EPCBT_Close(*fd);
615 			}
616 		} else if(EPS_INVALID_FILEDSC != *fd){
617 			epsUsbFnc.closePortal(*fd);
618 			*fd = EPS_INVALID_FILEDSC;
619 		}
620     }
621 
622     if ( Ret == EPCBT_ERR_NONE )
623         ioOpenState = EPS_IO_OPEN;        /* Open flag On */
624 
625     CBTDebugPrintf(("EPS CBT : commOpen -> ioOpenState == %s\r\n", ioOpenState == EPS_IO_OPEN ? "ON" : "OFF"));
626 
627     EPS_RETURN( Ret );
628 }
629 
630 /*******************************************|********************************************/
631 /*                                                                                      */
632 /* Function name:   commClose()                                                         */
633 /*                                                                                      */
634 /* Arguments                                                                            */
635 /* ---------                                                                            */
636 /* Name:        Type:               Description:                                        */
637 /* fd           EPS_FILEDSC         I: file discripter                                  */
638 /*                                                                                      */
639 /* Return value:                                                                        */
640 /*      EPCBT_ERR_NONE                  - Success                                       */
641 /*      EPSCBT_ERR_NOTOPEN              - I/O port is not opened                        */
642 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
643 /*                                                                                      */
644 /* Description:                                                                         */
645 /*      Close the communication.                                                        */
646 /*                                                                                      */
647 /*******************************************|********************************************/
commClose(EPS_FILEDSC fd)648 static EPS_INT32    commClose (
649 
650         EPS_FILEDSC		fd
651 
652 ){
653     EPS_INT32 Ret           = EPCBT_ERR_NONE;
654     EPS_INT32 closePtlState = 0;
655 
656 	EPS_LOG_FUNCIN;
657 
658 	if ( ioOpenState == EPS_IO_NOT_OPEN )        /* Open check */
659         Ret = EPSCBT_ERR_NOTOPEN;
660     else
661     {
662         Ret = EPCBT_Close(fd);    /* Channel close & cbt exit */
663 
664         closePtlState = epsUsbFnc.closePortal(fd);
665 
666         ioOpenState = EPS_IO_NOT_OPEN;        /* Open flag Off */
667     }
668 
669 #ifdef COMM_DBG_ERROR
670     if ( Ret != EPCBT_ERR_NONE )
671         CBTDebugPrintf(("<<  Close Ret[%d]  Close Portal[%d]\r\n", Ret, closePtlState));
672 #endif
673 
674     EPS_RETURN( Ret );
675 }
676 
677 /*******************************************|********************************************/
678 /*                                                                                      */
679 /* Function name:   commOpenChannel()                                                   */
680 /*                                                                                      */
681 /* Arguments                                                                            */
682 /* ---------                                                                            */
683 /* Name:        Type:               Description:                                        */
684 /* fd           EPS_FILEDSC         I: file discripter                                  */
685 /* Channel      EPS_INT32           I: channel number                                   */
686 /* bRetry       EPS_BOOL            I: If TRUE, retry open                              */
687 /*                                                                                      */
688 /* Return value:                                                                        */
689 /*      EPCBT_ERR_NONE                  - Success                                       */
690 /*      EPSCBT_ERR_NOTOPEN              - CBT channels are not opened                   */
691 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
692 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
693 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
694 /*      EPCBT_ERR_CH2NDOPEN             - Channel is already open                       */
695 /*      EPCBT_ERR_RPLYPSIZE             - Size of packet from OpenChannel is invalid    */
696 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
697 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
698 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
699 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
700 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
701 /*      EPCBT_ERR_RESOURCE              - Insufficient printer resources for            */
702 /*                                        OpenChannel                                   */
703 /*      EPCBT_ERR_OPENCHANNEL           - Not used by EPSON                             */
704 /*      EPCBT_ERR_CHNOTSUPPORT          - The channel is not supported                  */
705 /*      EPCBT_ERR_PACKETSIZE            - Invalid packet size.                          */
706 /*                                        (0x0001 ? 0x0005 bytes)                       */
707 /*      EPCBT_ERR_NULLPACKETSZ          - Packet size is 0x0000 in both directions.     */
708 /*                                        No data can be transferred. Channel was       */
709 /*                                        not opened                                    */
710 /*                                                                                      */
711 /* Description:                                                                         */
712 /*      Opens the communication channel.                                                */
713 /*                                                                                      */
714 /*******************************************|********************************************/
commOpenChannel(EPS_FILEDSC fd,EPS_INT32 Channel,EPS_BOOL bRetry)715 static EPS_INT32    commOpenChannel (
716 
717 			EPS_FILEDSC	fd,
718 			EPS_INT32   Channel,
719 			EPS_BOOL	bRetry
720 
721 ){
722     EPS_INT32   Ret   = EPCBT_ERR_NONE;
723     EPS_UINT8   CBTCh = 0;
724 
725 	EPS_LOG_FUNCIN;
726 
727     if ( ioOpenState == EPS_IO_NOT_OPEN ) {        /* Open check */
728         Ret = EPSCBT_ERR_NOTOPEN;
729     } else{
730         Ret = commChChange( Channel, &CBTCh );        /* Channel Change */
731 
732         if ( Ret == EPCBT_ERR_NONE ) {
733             if ((CBTCh == CBT_CBTCH_DATA) && (ioDataChState == EPS_IO_NOT_OPEN)) {
734                 Ret = (EPS_INT16)EPCBT_OpenChannel(fd, CBTCh, bRetry);    /* Open Data Channel */
735                 if (Ret == EPCBT_ERR_NONE)
736                     ioDataChState = EPS_IO_OPEN;
737             }
738 
739             if ((CBTCh == CBT_CBTCH_CTRL) && (ioControlChState == EPS_IO_NOT_OPEN)) {
740                 Ret = (EPS_INT16)EPCBT_OpenChannel(fd, CBTCh, bRetry);    /* Open Control Channel */
741                 if (Ret == EPCBT_ERR_NONE)
742                     ioControlChState = EPS_IO_OPEN;
743             }
744         }
745     }
746 
747 #ifdef COMM_DBG_ERROR
748     CBTDebugPrintf(("EPS CBT : commOpenChannel(%x)->%d\r\n", CBTCh, Ret));
749     CBTDebugPrintf(("EPS CBT : commOpenChannel-> ioOpenState == %s\r\n", ioOpenState == EPS_IO_OPEN ? "ON" : "OFF"));
750     if ( Ret != EPCBT_ERR_NONE )
751         CBTDebugPrintf(("<<  Close Channel Ret[%d]\r\n", Ret));
752 #endif
753 
754     EPS_RETURN( Ret );
755 }
756 
757 /*******************************************|********************************************/
758 /*                                                                                      */
759 /* Function name:   commCloseChannel()                                                  */
760 /*                                                                                      */
761 /* Arguments                                                                            */
762 /* ---------                                                                            */
763 /* Name:        Type:               Description:                                        */
764 /* fd           EPS_FILEDSC         I: file discripter                                  */
765 /* Channel      EPS_INT32           I: channel number                                   */
766 /*                                                                                      */
767 /* Return value:                                                                        */
768 /*      EPCBT_ERR_NONE                  - Success                                       */
769 /*      EPSCBT_ERR_NOTOPEN              - CBT channels are not opened                   */
770 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
771 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
772 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
773 /*      EPCBT_ERR_NONE                  - Success                                       */
774 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
775 /*      EPCBT_ERR_CLOSEDENIED           - Close Channel is denied                       */
776 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
777 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
778 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
779 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
780 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
781 /*                                                                                      */
782 /* Description:                                                                         */
783 /*      Close the communication channel.                                                */
784 /*                                                                                      */
785 /*******************************************|********************************************/
commCloseChannel(EPS_FILEDSC fd,EPS_INT32 Channel)786 static EPS_INT32    commCloseChannel (
787 
788 		EPS_FILEDSC	fd,
789         EPS_INT32   Channel
790 
791 ){
792     EPS_INT32   Ret   = EPCBT_ERR_NONE;
793     EPS_UINT8   CBTCh = 0;
794 
795 	EPS_LOG_FUNCIN;
796 
797 	if ( ioOpenState == EPS_IO_NOT_OPEN )        /* Open check */
798         Ret = EPSCBT_ERR_NOTOPEN;
799     else
800     {
801         /* Channel close */
802         Ret = commChChange( Channel, &CBTCh );        /* Channel Change */
803 
804         if ( Ret == EPCBT_ERR_NONE ) {
805             if ((CBTCh == CBT_CBTCH_DATA) && (ioDataChState == EPS_IO_OPEN)) {
806                 Ret = (EPS_INT16)EPCBT_CloseChannel(fd, CBTCh);    /* Close Data Channel */
807                 /*if (Ret == EPCBT_ERR_NONE)		Force reset */
808                     ioDataChState = EPS_IO_NOT_OPEN;
809             }
810 
811             if ((CBTCh == CBT_CBTCH_CTRL) && (ioControlChState == EPS_IO_OPEN)) {
812                 Ret = (EPS_INT16)EPCBT_CloseChannel(fd, CBTCh);    /* Close Control Channel */
813                 /*if (Ret == EPCBT_ERR_NONE)		Force reset */
814                     ioControlChState = EPS_IO_NOT_OPEN;
815             }
816         }
817 	}
818 
819 #ifdef COMM_DBG_ERROR
820     if ( Ret != EPCBT_ERR_NONE )
821         CBTDebugPrintf(("<<  Close Channel Ret[%d]\r\n", Ret));
822 #endif
823 
824     EPS_RETURN( Ret );
825 }
826 
827 /*******************************************|********************************************/
828 /*                                                                                      */
829 /* Function name:   commChChange()                                                      */
830 /*                                                                                      */
831 /* Arguments                                                                            */
832 /* ---------                                                                            */
833 /* Name:        Type:               Description:                                        */
834 /* Channel      EPS_INT32           I: Channel number definition                        */
835 /*                                      00: Data Channel                                */
836 /*                                      01: Control Channel                             */
837 /* CBTCh        EPS_UINT8*          O: Pointer to the channel number                    */
838 /*                                      40: Data Channel                                */
839 /*                                      02: Control Channel                             */
840 /*                                                                                      */
841 /* Return value:                                                                        */
842 /*      EPCBT_ERR_NONE                  - Success                                       */
843 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
844 /*                                                                                      */
845 /* Description:                                                                         */
846 /*      Set the channel number.                                                         */
847 /*                                                                                      */
848 /*******************************************|********************************************/
commChChange(EPS_INT32 Channel,EPS_UINT8 * CBTCh)849 static EPS_INT32    commChChange (
850 
851         EPS_INT32   Channel,
852         EPS_UINT8   *CBTCh
853 
854 ){
855     EPS_INT32   Ret = EPCBT_ERR_NONE;
856 
857 	EPS_LOG_FUNCIN;
858 
859 	switch (Channel)
860     {
861         case EPS_CBTCHANNEL_DATA:    /* Data Channel */
862             *CBTCh = CBT_CBTCH_DATA;
863             break;
864         case EPS_CBTCHANNEL_CTRL:    /* Control Channel */
865             *CBTCh = CBT_CBTCH_CTRL;
866             break;
867         default:                    /* Error */
868             Ret = EPSCBT_ERR_PARAM;
869             break;
870     }
871 
872     EPS_RETURN( Ret );
873 }
874 
875 
876 /*******************************************|********************************************/
877 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
878 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv                       vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
879 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv    Mini CBT Engine    vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
880 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv                       vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
881 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
882 /*******************************************|********************************************/
883 
884 /*******************************************|********************************************/
885 /*                                                                                      */
886 /* Function name:   EPCBT_Open                                                          */
887 /*                                                                                      */
888 /* Arguments                                                                            */
889 /* ---------                                                                            */
890 /* Name:        Type:               Description:                                        */
891 /* fd           EPS_FILEDSC         I: file discripter                                  */
892 /* pvPortInfo   void *              I: Port information                                 */
893 /* lpWriteFunc  EPS_WritePortal     I: Pointer to write function                        */
894 /* lpReadFunc   EPS_ReadPortal      I: Pointer to read functiont information            */
895 /*                                                                                      */
896 /* Return value:                                                                        */
897 /*      EPCBT_ERR_NONE                  - Success                                       */
898 /*      EPCBT_ERR_CBT2NDOPEN            - CBT channels are already opened               */
899 /*      EPCBT_ERR_MEMORY                - Fail to memory allocation                     */
900 /*      EPCBT_ERR_CBTDISABLE            - Fail to CBT communication                     */
901 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
902 /*      EPCBT_ERR_INITDENIED            - Printer cannot start CBT mode                 */
903 /*      EPCBT_ERR_VERSION               - Incompatible version                          */
904 /*      EPCBT_ERR_INITFAILED            - Not used by EPSON                             */
905 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
906 /*                                                                                      */
907 /* Description:                                                                         */
908 /*      Opens the CBT communication                                                     */
909 /*                                                                                      */
910 /*******************************************|********************************************/
EPCBT_Open(EPS_FILEDSC fd,void * pvPortInfo,EPS_WritePortal lpWriteFunc,EPS_ReadPortal lpReadFunc)911 static EPS_INT16    EPCBT_Open (
912 
913 		EPS_FILEDSC			fd,
914         void*               pvPortInfo,
915         EPS_WritePortal     lpWriteFunc,
916         EPS_ReadPortal      lpReadFunc
917 
918 ){
919     EPS_INT16 Ret = EPCBT_ERR_NONE;
920 
921 	EPS_LOG_FUNCIN;
922 
923 #ifdef    CBT_DBG_IO
924     sprintf(debMsgWork,"EPCBT_Open (S)\r\n");
925     DebMsgOut(debMsgWork);
926 #endif
927 
928     if( gpCbtPrnInfo != NULL) {
929         Ret = EPCBT_ERR_CBT2NDOPEN;
930     } else {
931         Ret = CbtMemAlloc( );
932     }
933 
934     if ( Ret == EPCBT_ERR_NONE ) {
935         gpCbtPrnInfo->pPortInfo   = pvPortInfo;
936         gpCbtPrnInfo->pCbtWRtnFnc = lpWriteFunc;
937         gpCbtPrnInfo->pCbtRRtnFnc = lpReadFunc;
938         gpCbtPrnInfo->Mode        = CBT_MODE_CBTOFF;
939 
940         CbtDummyRead( fd, gpCbtPrnInfo );               /* Dummy read                       */
941         Ret = CbtEpsonPacking( fd, gpCbtPrnInfo );      /* send EpsonPackingCommand         */
942         if ( Ret == EPCBT_ERR_NONE )
943             Ret = CbtInitCommand( fd, gpCbtPrnInfo );   /* send InitCommand                 */
944     }
945 
946     if ( Ret == EPCBT_ERR_NONE ) {                  /* OK ?                             */
947         gpCbtPrnInfo->Mode = CBT_MODE_CBTON;
948     } else {
949         if(Ret != EPCBT_ERR_CBT2NDOPEN)
950             CbtMemFree( );
951     }
952 
953 #if    CBT_DBG_ERROR
954     if( Ret != EPCBT_ERR_NONE ) {
955         sprintf(debMsgWork," <- CBT_Open Ret[%d]\r\n",Ret);
956         DebMsgOut(debMsgWork);
957     }
958 #endif
959 
960 #ifdef    CBT_DBG_IO
961     sprintf(debMsgWork,"EPCBT_Open (E)\r\n");
962     DebMsgOut(debMsgWork);
963 #endif
964 
965     EPS_RETURN( Ret );
966 }
967 
968 /*******************************************|********************************************/
969 /*                                                                                      */
970 /* Function name:   EPCBT_Close()                                                       */
971 /*                                                                                      */
972 /* Arguments                                                                            */
973 /* ---------                                                                            */
974 /* Name:        Type:               Description:                                        */
975 /* fd           EPS_FILEDSC         I: file discripter                                  */
976 /*                                                                                      */
977 /* Return value:                                                                        */
978 /*      EPCBT_ERR_NONE                  - Success                                       */
979 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
980 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
981 /*      EPCBT_ERR_CLOSEDENIED           - Close Channel is denied                       */
982 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
983 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
984 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
985 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
986 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
987 /*                                                                                      */
988 /* Description:                                                                         */
989 /*      End CBT mode.                                                                   */
990 /*                                                                                      */
991 /*******************************************|********************************************/
EPCBT_Close(EPS_FILEDSC fd)992 static EPS_INT16    EPCBT_Close (
993 
994         EPS_FILEDSC	fd
995 
996 ){
997     EPS_INT16       Ret = EPCBT_ERR_NONE;
998     EPS_INT32       lp1;
999     CBTS_CHINFO*    ChPtr;
1000 
1001 	EPS_LOG_FUNCIN;
1002 
1003 #ifdef    CBT_DBG_IO
1004     sprintf(debMsgWork,"EPCBT_Close (S)\r\n");
1005     DebMsgOut(debMsgWork);
1006 #endif
1007 
1008     if( gpCbtPrnInfo == NULL) {
1009         Ret = EPCBT_ERR_CBTNOTOPEN;
1010     } else {
1011         gpCbtPrnInfo->Mode = CBT_MODE_REQOFF;
1012 
1013         for ( lp1 = 1; lp1 < CBT_MAX_CH ; lp1++ ) {
1014             ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
1015 
1016             if ( ChPtr->No != 0 ) {
1017                 Ret = CbtCloseChannelCommand( fd, gpCbtPrnInfo, ChPtr->No );    /* Channel Close */
1018             }
1019         }
1020 
1021         for ( lp1 = 0; lp1 < CBT_MAX_CH ; lp1++ ) {
1022             ChPtr           = &gpCbtPrnInfo->Chinfo[lp1];
1023             ChPtr->No       = 0;
1024             ChPtr->PtSsize  = 0;
1025             ChPtr->StPsize  = 0;
1026             ChPtr->CreditP  = 0;
1027             ChPtr->CreditH  = 0;
1028             ChPtr->ReadSize = 0;
1029             ChPtr->RTmpTop  = 0;
1030             ChPtr->RTmpEnd  = 0;
1031             ChPtr->RTmpCnt  = 0;
1032         }
1033 
1034         gpCbtPrnInfo->Mode = CBT_MODE_CBTOFF;
1035     }
1036     CbtMemFree( );
1037 
1038 #if    CBT_DBG_ERROR
1039     if( Ret != EPCBT_ERR_NONE ) {
1040         sprintf(debMsgWork, " <- CBT_Close Error! Ret[%d]\r\n",Ret);
1041         DebMsgOut(debMsgWork);
1042     }
1043 #endif
1044 
1045 #ifdef    CBT_DBG_IO
1046     sprintf(debMsgWork,"EPCBT_Close (E)\r\n");
1047     DebMsgOut(debMsgWork);
1048 #endif
1049 
1050     EPS_RETURN( Ret );
1051 }
1052 
1053 /*******************************************|********************************************/
1054 /*                                                                                      */
1055 /* Function name:   EPCBT_OpenChannel                                                   */
1056 /*                                                                                      */
1057 /* Arguments                                                                            */
1058 /* ---------                                                                            */
1059 /* Name:        Type:               Description:                                        */
1060 /* fd           EPS_FILEDSC         I: file discripter                                  */
1061 /* SID          EPS_UINT8           I: socket id                                        */
1062 /* bRetry       EPS_BOOL            I: If TRUE, retry open                              */
1063 /*                                                                                      */
1064 /* Return value:                                                                        */
1065 /*      EPCBT_ERR_NONE                  - Port is not open                              */
1066 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
1067 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
1068 /*      EPCBT_ERR_CH2NDOPEN             - Channel is already open                       */
1069 /*      EPCBT_ERR_RPLYPSIZE             - Size of packet from OpenChannel is invalid    */
1070 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
1071 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
1072 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
1073 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
1074 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
1075 /*      EPCBT_ERR_RESOURCE              - Insufficient printer resources for            */
1076 /*                                        OpenChannel                                   */
1077 /*      EPCBT_ERR_OPENCHANNEL           - Not used by EPSON                             */
1078 /*      EPCBT_ERR_CHNOTSUPPORT          - The channel is not supported                  */
1079 /*      EPCBT_ERR_PACKETSIZE            - Invalid packet size.                          */
1080 /*                                        (0x0001 ? 0x0005 bytes)                       */
1081 /*      EPCBT_ERR_NULLPACKETSZ          - Packet size is 0x0000 in both directions.     */
1082 /*                                        No data can be transferred. Channel was       */
1083 /*                                        not opened                                    */
1084 /*                                                                                      */
1085 /* Description:                                                                         */
1086 /*      Opens the CBT communication.                                                    */
1087 /*                                                                                      */
1088 /*******************************************|********************************************/
EPCBT_OpenChannel(EPS_FILEDSC fd,EPS_UINT8 SID,EPS_BOOL bRetry)1089 static EPS_INT16    EPCBT_OpenChannel (
1090 
1091 			EPS_FILEDSC		fd,
1092 			EPS_UINT8		SID,
1093 			EPS_BOOL		bRetry
1094 
1095 ){
1096     EPS_INT16   Ret     = EPCBT_ERR_NONE;
1097     EPS_UINT32  PtSsize = 0;
1098     EPS_UINT32  StPsize = 0;
1099     EPS_INT32   iCredit = 0;
1100     EPS_INT32   nOpenRetry = 0;
1101     EPS_INT32   lp1;
1102     CBTS_CHINFO *ChPtr;
1103 
1104 	EPS_LOG_FUNCIN;
1105 
1106 #ifdef    CBT_DBG_IO
1107     sprintf(debMsgWork,"EPCBT_OpenChannel (S)    0x%02x\r\n", SID);
1108     DebMsgOut(debMsgWork);
1109 #endif
1110 
1111     if( gpCbtPrnInfo == NULL) {
1112         Ret = EPCBT_ERR_CBTNOTOPEN;
1113     } else {
1114         if ( gpCbtPrnInfo->Mode == CBT_MODE_CBTON ) {
1115             if ( (SID == 0x00) || (SID == 0xff) )
1116                 Ret = EPCBT_ERR_PARAM;
1117             else {
1118                 for ( lp1 = 1; lp1 < CBT_MAX_CH ; lp1++ ) {
1119                     ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
1120                     if ( ChPtr->No == SID ) {                /* channel open ? */
1121                         Ret = EPCBT_ERR_CH2NDOPEN;
1122                         break;
1123                     }
1124                 }
1125                 if ( Ret == EPCBT_ERR_NONE ) {
1126                     if ( gpCbtPrnInfo->Chinfo[lp1-1].No != 0 )     /* Over flow */
1127                         Ret = EPCBT_ERR_PARAM;
1128                 }
1129             }
1130         } else {
1131             Ret = EPCBT_ERR_CBTNOTOPEN;
1132         }
1133     }
1134     if ( Ret == EPCBT_ERR_NONE ) {
1135         if ( SID == CBT_SID_DATA ) {
1136             PtSsize = CBT_TXPSIZE;
1137             StPsize = CBT_RXPSIZE;
1138         } else {
1139             PtSsize = CBT_RXPSIZE;
1140             StPsize = CBT_RXPSIZE;
1141         }
1142 
1143 		if(bRetry){
1144 			for( ; EPCBT_ERR_RESOURCE == (iCredit = CbtOpenChannelCommand(fd, gpCbtPrnInfo, SID, &PtSsize, &StPsize, 0, 0) )
1145 				&& nOpenRetry < CBT_OPENCH_RETRY; nOpenRetry++){
1146 
1147 				serDelayThread(CBT_OPENCH_WAIT, &epsCmnFnc);
1148 			}
1149 		} else{
1150 			iCredit = CbtOpenChannelCommand(fd, gpCbtPrnInfo, SID, &PtSsize, &StPsize, 0, 0);
1151 		}
1152 
1153 		if ( iCredit < 0 )
1154             Ret = (EPS_INT16)iCredit;
1155     }
1156     if ( Ret == EPCBT_ERR_NONE ) {
1157 
1158         if ( ( PtSsize == 0 ) && ( StPsize == 0 ) )
1159             Ret = EPCBT_ERR_RPLYPSIZE;
1160         else {
1161             if ( (0 < PtSsize) && (PtSsize < 6) )
1162                 Ret = EPCBT_ERR_RPLYPSIZE;
1163             else {
1164                 if ( (0 < StPsize) && (StPsize < 6) )
1165                     Ret = EPCBT_ERR_RPLYPSIZE;
1166             }
1167         }
1168     }
1169 
1170     if ( Ret == EPCBT_ERR_NONE ) {
1171         for ( lp1 = 1; ; lp1++ ) {
1172             if ( lp1 >= CBT_MAX_CH ) {
1173                 Ret = EPCBT_ERR_PARAM;
1174                 break;
1175             }
1176 
1177             ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
1178 
1179             if ( ChPtr->No == 0 ) {
1180                 ChPtr->No       = SID;
1181                 ChPtr->PtSsize  = (EPS_INT32)PtSsize;
1182                 ChPtr->StPsize  = (EPS_INT32)StPsize;
1183                 ChPtr->CreditP  = iCredit;
1184                 ChPtr->CreditH  = 0;
1185                 ChPtr->ReadSize = 0;
1186                 ChPtr->RTmpTop  = 0;
1187                 ChPtr->RTmpEnd  = 0;
1188                 ChPtr->RTmpCnt  = 0;
1189                 if ( SID == CBT_SID_CTRL ) {
1190                     CbtChDummyRead( fd, gpCbtPrnInfo, ChPtr );    /* Dummy Read */
1191                 }
1192                 break;
1193             }
1194         }
1195     }
1196 
1197 #if    CBT_DBG_ERROR
1198     if( Ret != EPCBT_ERR_NONE ) {
1199         sprintf(debMsgWork, " <- EPCBT_OpenChannel Ret[%d] CH[%x]\r\n", Ret,SID);
1200         DebMsgOut(debMsgWork);
1201     }
1202 #endif
1203 
1204 #ifdef    CBT_DBG_IO
1205     sprintf(debMsgWork,"EPCBT_OpenChannel (E)    0x%02x\r\n", SID);
1206     DebMsgOut(debMsgWork);
1207 #endif
1208 
1209     EPS_RETURN( Ret );
1210 }
1211 
1212 /*******************************************|********************************************/
1213 /*                                                                                      */
1214 /* Function name:   EPCBT_CloseChannel()                                                */
1215 /*                                                                                      */
1216 /* Arguments                                                                            */
1217 /* ---------                                                                            */
1218 /* Name:        Type:               Description:                                        */
1219 /* fd           EPS_FILEDSC         I: file discripter                                  */
1220 /* SID          EPS_UINT8           I: Socket ID                                        */
1221 /*                                                                                      */
1222 /* Return value:                                                                        */
1223 /*      EPCBT_ERR_NONE                  - Port is not open                              */
1224 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
1225 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
1226 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
1227 /*      EPCBT_ERR_NONE                  - Success                                       */
1228 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
1229 /*      EPCBT_ERR_CLOSEDENIED           - Close Channel is denied                       */
1230 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
1231 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
1232 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
1233 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
1234 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
1235 /*                                                                                      */
1236 /* Description:                                                                         */
1237 /*      Close the communication channel.                                                */
1238 /*                                                                                      */
1239 /*******************************************|********************************************/
EPCBT_CloseChannel(EPS_FILEDSC fd,EPS_UINT8 SID)1240 static EPS_INT16    EPCBT_CloseChannel (
1241 
1242         EPS_FILEDSC	fd,
1243 		EPS_UINT8	SID
1244 
1245 ){
1246     EPS_INT16       Ret   = EPCBT_ERR_NONE;
1247     EPS_INT32       lp1;
1248     CBTS_CHINFO*    ChPtr = NULL;
1249 
1250 	EPS_LOG_FUNCIN;
1251 
1252 #ifdef    CBT_DBG_IO
1253     sprintf(debMsgWork,"EPCBT_CloseChannel (S)    0x%02x\r\n", SID);
1254     DebMsgOut(debMsgWork);
1255 #endif
1256 
1257     if( gpCbtPrnInfo == NULL) {
1258         Ret = EPCBT_ERR_CBTNOTOPEN;
1259     } else {
1260         if ( gpCbtPrnInfo->Mode == CBT_MODE_CBTON ) {
1261             if ( (SID == 0x00) || (SID == 0xff) )
1262                 Ret = EPCBT_ERR_PARAM;
1263             else {
1264                 for ( lp1 = 1; ; lp1++ ) {
1265                     if ( lp1 >= CBT_MAX_CH ) {
1266                         Ret = EPCBT_ERR_CHNOTOPEN;
1267                         break;
1268                     }
1269                     ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
1270                     if ( ChPtr->No == SID )
1271                         break;
1272                 }
1273             }
1274         } else {
1275             EPS_RETURN( Ret );                    /* return normal end */
1276         }
1277     }
1278     if ( Ret == EPCBT_ERR_NONE ) {
1279         Ret = CbtCloseChannelCommand( fd, gpCbtPrnInfo, SID );
1280         /*if ( Ret == EPCBT_ERR_NONE ) {	Force reset */
1281             ChPtr->No       = 0;
1282             ChPtr->PtSsize  = 0;
1283             ChPtr->StPsize  = 0;
1284             ChPtr->CreditP  = 0;
1285             ChPtr->CreditH  = 0;
1286             ChPtr->ReadSize = 0;
1287             ChPtr->RTmpTop  = 0;
1288             ChPtr->RTmpEnd  = 0;
1289             ChPtr->RTmpCnt  = 0;
1290         /*}									Force reset */
1291     }
1292 
1293 #if    CBT_DBG_ERROR
1294     if( Ret != EPCBT_ERR_NONE ) {
1295         sprintf(debMsgWork, " <- CBT_CloseChannel Ret[%d] CH[%x]\r\n",Ret,SID);
1296         DebMsgOut(debMsgWork);
1297     }
1298 #endif
1299 
1300 #ifdef    CBT_DBG_IO
1301     sprintf(debMsgWork,"EPCBT_CloseChannel (E)    0x%02x\r\n", SID);
1302     DebMsgOut(debMsgWork);
1303 #endif
1304 
1305     EPS_RETURN( Ret );
1306 }
1307 
1308 /*******************************************|********************************************/
1309 /*                                                                                      */
1310 /* Function name:   EPCBT_Write()                                                       */
1311 /*                                                                                      */
1312 /* Arguments                                                                            */
1313 /* ---------                                                                            */
1314 /* Name:        Type:               Description:                                        */
1315 /* fd           EPS_FILEDSC         I: file discripter                                  */
1316 /* SID          EPS_UINT8           I: Channel-Select                                   */
1317 /* lpBuffer     const EPS_UINT8*    I: Buffer Pointer for Write Data                    */
1318 /* lpSize       EPS_INT32*          O: Write Data Buffer Length (bytes)                 */
1319 /*                                                                                      */
1320 /* Return value:                                                                        */
1321 /*      EPCBT_ERR_NONE                  - Success                                       */
1322 /*      EPCBT_ERR_CBTNOTOPEN            - CBT is not opened                             */
1323 /*      EPSCBT_ERR_PARAM                - Parameter error                               */
1324 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
1325 /*      EPCBT_ERR_FNCDISABLE            - Function is disable to be completely finished */
1326 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
1327 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
1328 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
1329 /*      EPCBT_ERR_WRITEERROR            - Failed to write                               */
1330 /*                                                                                      */
1331 /* Description:                                                                         */
1332 /*      Write the data to printer.                                                      */
1333 /*                                                                                      */
1334 /*******************************************|********************************************/
EPCBT_Write(EPS_FILEDSC fd,EPS_UINT8 SID,const EPS_UINT8 * lpBuffer,EPS_INT32 * lpSize)1335 static EPS_INT16    EPCBT_Write (
1336 
1337         EPS_FILEDSC			fd,
1338 		EPS_UINT8           SID,
1339         const EPS_UINT8*    lpBuffer,
1340         EPS_INT32*          lpSize
1341 
1342 ){
1343     EPS_INT16       Ret = EPCBT_ERR_NONE;
1344     EPS_INT32       Credit;
1345     EPS_INT32       Size;
1346     EPS_INT32       Count;
1347     EPS_INT32       lp1;
1348     EPS_INT32       loopSize;
1349     EPS_INT32       SendMaxSize;
1350     CBTS_CHINFO*    ChPtr = NULL;
1351 
1352 	EPS_LOG_FUNCIN;
1353 
1354 #ifdef    CBT_DBG_IO
1355     sprintf(debMsgWork,"EPCBT_Write (S)    0x%02x(%04d)\r\n", SID, *lpSize);
1356     DebMsgOut(debMsgWork);
1357 #endif
1358 
1359     if( gpCbtPrnInfo == NULL) {
1360         Ret = EPCBT_ERR_CBTNOTOPEN;
1361     } else {
1362         if ( gpCbtPrnInfo->Mode == CBT_MODE_CBTON ) {
1363             if ( (SID == 0x00) || (SID == 0xff) ) {
1364                 Ret = EPCBT_ERR_PARAM;
1365             } else {
1366                 for ( lp1 = 1; ; lp1++ ) {
1367                     if ( lp1 >= CBT_MAX_CH ) {
1368                         Ret = EPCBT_ERR_CHNOTOPEN;
1369                         break;
1370                     }
1371                     ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
1372                     if ( ChPtr->No == SID )
1373                         break;
1374                 }
1375             }
1376         } else {
1377             Ret = EPCBT_ERR_CBTNOTOPEN;
1378         }
1379     }
1380     if ( Ret == EPCBT_ERR_NONE ) {
1381         if( (ChPtr->PtSsize - 6) < *lpSize)
1382             SendMaxSize = ChPtr->PtSsize - 6;
1383         else
1384             SendMaxSize = *lpSize;
1385         loopSize = *lpSize;
1386         *lpSize  = 0;
1387         while(loopSize) {
1388 
1389             if ( ChPtr->CreditP == 0 ) {
1390                 Credit = CbtCreditReqCommand(fd, gpCbtPrnInfo, SID, CBT_CREDIT_LPT, 0xffff);
1391                 if ( Credit < 0 )
1392                     Ret = (EPS_INT16)Credit;
1393                 else if ( Credit == 0 )
1394                     Ret = EPCBT_ERR_FNCDISABLE;
1395                 else
1396                     ChPtr->CreditP = Credit;
1397             }
1398 
1399             if ( Ret != EPCBT_ERR_NONE )
1400                 break;
1401 
1402             if(SendMaxSize < loopSize)
1403                 Size = SendMaxSize;
1404             else
1405                 Size = loopSize;
1406             Count = CbtDataWrite( fd, gpCbtPrnInfo, SID, &lpBuffer[*lpSize], Size );
1407             ChPtr->CreditP -= 1;
1408             if ( Count >= 0 ) {
1409                 *lpSize  += Count;
1410                 loopSize -= Count;
1411             } else {
1412                 Ret = (EPS_INT16)Count;
1413                 break;
1414             }
1415         }
1416     }
1417 
1418 #if    CBT_DBG_ERROR
1419     if( Ret != EPCBT_ERR_NONE ) {
1420         sprintf(debMsgWork, " <- CBT_Write Ret[%d] CH[%x] Size[%d]\r\n",Ret,SID,*lpSize);
1421         DebMsgOut(debMsgWork);
1422     }
1423 #endif
1424 
1425 #ifdef    CBT_DBG_IO
1426     sprintf(debMsgWork,"EPCBT_Write (E)    0x%02x(%04d)\r\n", SID, *lpSize);
1427     DebMsgOut(debMsgWork);
1428 #endif
1429 
1430     EPS_RETURN( Ret );
1431 }
1432 
1433 /*******************************************|********************************************/
1434 /*                                                                                      */
1435 /* Function name:   EPCBT_Read                                                          */
1436 /*                                                                                      */
1437 /* Arguments                                                                            */
1438 /* ---------                                                                            */
1439 /* Name:        Type:               Description:                                        */
1440 /* fd           EPS_FILEDSC         I: file discripter                                  */
1441 /* SID          EPS_UINT8           I: Socket ID                                        */
1442 /* lpBuffer     EPS_UINT8*          O: Pointer to buffer for read data                  */
1443 /* lpSize       EPS_INT32*          O: Pointer to variable for read data size           */
1444 /*                                                                                      */
1445 /* Return value:                                                                        */
1446 /*      EPCBT_ERR_NONE                  - Success                                       */
1447 /*      EPCBT_ERR_CBTNOTOPEN            - CBT is not opened                             */
1448 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
1449 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
1450 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
1451 /*      EPCBT_ERR_CREDITOVF             - Credit over flow                              */
1452 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
1453 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
1454 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
1455 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
1456 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
1457 /*      EPCBT_ERR_READERROR             - Data read error                               */
1458 /*                                                                                      */
1459 /* Description:                                                                         */
1460 /*      Receive the data from printer.                                                  */
1461 /*                                                                                      */
1462 /*******************************************|********************************************/
EPCBT_Read(EPS_FILEDSC fd,EPS_UINT8 SID,EPS_UINT8 * lpBuffer,EPS_INT32 * lpSize)1463 static EPS_INT16    EPCBT_Read (
1464 
1465 		EPS_FILEDSC	fd,
1466         EPS_UINT8   SID,
1467         EPS_UINT8*  lpBuffer,
1468         EPS_INT32*  lpSize
1469 
1470 ){
1471     EPS_INT16       Ret   = EPCBT_ERR_NONE;
1472     EPS_INT32       Size  = 0;
1473     EPS_INT32       lp1;
1474     CBTS_CHINFO*    ChPtr = NULL;
1475 
1476 	EPS_LOG_FUNCIN;
1477 
1478 #ifdef    CBT_DBG_IO
1479     sprintf(debMsgWork,"EPCBT_Read (S)    0x%02x(%04d)\r\n", SID, *lpSize);
1480     DebMsgOut(debMsgWork);
1481 #endif
1482 
1483     if( gpCbtPrnInfo == NULL) {
1484         Ret = EPCBT_ERR_CBTNOTOPEN;
1485     } else {
1486         if ( gpCbtPrnInfo->Mode == CBT_MODE_CBTON ) {
1487             if ( (SID == 0x00) || (SID == 0xff) ) {
1488                 Ret = EPCBT_ERR_PARAM;
1489             } else {
1490                 for ( lp1 = 1; ; lp1++ ) {
1491                     if ( lp1 >= CBT_MAX_CH ) {
1492                         Ret = EPCBT_ERR_CHNOTOPEN;
1493                         break;
1494                     }
1495                     ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
1496                     if ( ChPtr->No == SID )
1497                         break;
1498                 }
1499             }
1500         } else {
1501             Ret = EPCBT_ERR_CBTNOTOPEN;
1502         }
1503     }
1504     if ( Ret == EPCBT_ERR_NONE ) {
1505         if ( lpBuffer == NULL ) {
1506             if ( ChPtr->CreditH == 0 ) {
1507                 Ret = CbtCreditCommand( fd, gpCbtPrnInfo, SID, 1 );
1508                 if ( Ret == EPCBT_ERR_NONE )
1509                     ChPtr->CreditH += 1;
1510             }
1511             if ( Ret == EPCBT_ERR_NONE ) {
1512                 Ret     = CbtDataRead( fd, gpCbtPrnInfo, SID, NULL, &Size );
1513                 *lpSize = Size;
1514             }
1515         } else {
1516             Size    = *lpSize;
1517             Ret     = CbtDataRead( fd, gpCbtPrnInfo, SID, lpBuffer, &Size );
1518             *lpSize = Size;
1519         }
1520     }
1521 
1522 #if    CBT_DBG_ERROR
1523     if( Ret != EPCBT_ERR_NONE ) {
1524         sprintf(debMsgWork, " <- CBT_Read Ret[%d] CH[%x] Size[%d]\r\n",Ret,SID,*lpSize);
1525         DebMsgOut(debMsgWork);
1526     }
1527 #endif
1528 
1529 #ifdef    CBT_DBG_IO
1530     sprintf(debMsgWork,"EPCBT_Read (E)    0x%02x(%04d)\r\n", SID, *lpSize);
1531     DebMsgOut(debMsgWork);
1532 #endif
1533 
1534     EPS_RETURN( Ret );
1535 }
1536 
1537 /*******************************************|********************************************/
1538 /*                                                                                      */
1539 /* Function name:   EPCBT_GetSocketID                                                   */
1540 /*                                                                                      */
1541 /* Arguments                                                                            */
1542 /* ---------                                                                            */
1543 /* Name:        Type:               Description:                                        */
1544 /* fd           EPS_FILEDSC         I: file discripter                                  */
1545 /* pSID         EPS_UINT8 *         I: Socket ID                                        */
1546 /* pName        EPS_UINT8 *         I: Parameter Name                                   */
1547 /* Size         EPS_INT32           I: Size of socket ID                                */
1548 /*                                                                                      */
1549 /* Return value:                                                                        */
1550 /*      EPCBT_ERR_NONE                  - Success                                       */
1551 /*      EPCBT_ERR_CBTNOTOPEN            - Port is not open                              */
1552 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
1553 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
1554 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
1555 /*      EPCBT_ERR_NOSERVICE             - The channel does not support the requested    */
1556 /*                                        service                                       */
1557 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
1558 /*                                                                                      */
1559 /* Description:                                                                         */
1560 /*      Gets the socket ID.                                                             */
1561 /*                                                                                      */
1562 /*******************************************|********************************************/
1563 #if LCOMSW_CBT_SOCKETFUNC_ON
EPCBT_GetSocketID(EPS_FILEDSC fd,EPS_UINT8 * pSID,EPS_UINT8 * pName,EPS_INT32 Size)1564 static EPS_INT16    EPCBT_GetSocketID (
1565 
1566 		EPS_FILEDSC	fd,
1567         EPS_UINT8*  pSID,
1568         EPS_UINT8*  pName,
1569         EPS_INT32   Size
1570 
1571 ){
1572     EPS_INT16   Ret      = EPCBT_ERR_NONE;
1573     EPS_UINT8   SocketID = 0;
1574 
1575 	EPS_LOG_FUNCIN;
1576 
1577 #ifdef    CBT_DBG_IO
1578     sprintf(debMsgWork,"EPCBT_GetSocketID (S)\r\n");
1579     DebMsgOut(debMsgWork);
1580 #endif
1581 
1582     if( gpCbtPrnInfo == NULL) {
1583         Ret = EPCBT_ERR_CBTNOTOPEN;
1584     } else {
1585         if ( gpCbtPrnInfo->Mode == CBT_MODE_CBTON ) {
1586             if ( (pSID == NULL) || (pName == NULL) ) {
1587                 Ret = EPCBT_ERR_PARAM;
1588             }
1589         } else {
1590             Ret = EPCBT_ERR_CBTNOTOPEN;
1591         }
1592     }
1593     if ( Ret == EPCBT_ERR_NONE ) {
1594         Ret = CbtGetSocketIDCommand( fd, gpCbtPrnInfo, pName, Size, &SocketID );
1595         if ( Ret == EPCBT_ERR_NONE )
1596             *pSID = SocketID;
1597     }
1598 
1599 #if    CBT_DBG_ERROR
1600     if( Ret != EPCBT_ERR_NONE ) {
1601         sprintf(debMsgWork, " <- CBT_GetSocketID Ret[%d] SID[%02x]\r\n", Ret,*pSID);
1602         DebMsgOut(debMsgWork);
1603     }
1604 #endif
1605 
1606 #ifdef    CBT_DBG_IO
1607     sprintf(debMsgWork,"EPCBT_GetSocketID (E)\r\n");
1608     DebMsgOut(debMsgWork);
1609 #endif
1610 
1611     EPS_RETURN( Ret );
1612 }
1613 #endif  /* def LCOMSW_CBT_SOCKETFUNC_ON */
1614 
1615 /*******************************************|********************************************/
1616 /*                                                                                      */
1617 /* Function name:   CbtWriteRtn                                                         */
1618 /*                                                                                      */
1619 /* Arguments                                                                            */
1620 /* ---------                                                                            */
1621 /* Name:        Type:               Description:                                        */
1622 /* fd           EPS_FILEDSC         I: file discripter                                  */
1623 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
1624 /* Buff         const EPS_UINT8*    I: Pointer to the write data buffer                 */
1625 /* BuffLen      EPS_INT32           I: Size of the write data buffer                    */
1626 /* Cnt          EPS_INT32*          O: Write data size                                  */
1627 /*                                                                                      */
1628 /* Return value:                                                                        */
1629 /*      EPCBT_ERR_NONE                  - Success                                       */
1630 /*      EPCBT_ERR_WRITEERROR            - Write error                                   */
1631 /*                                                                                      */
1632 /* Description:                                                                         */
1633 /*      Sends the data to the printer.                                                  */
1634 /*                                                                                      */
1635 /*******************************************|********************************************/
CbtWriteRtn(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,const EPS_UINT8 * Buff,EPS_INT32 BuffLen,EPS_INT32 * Cnt)1636 static EPS_INT16    CbtWriteRtn (
1637 
1638 		EPS_FILEDSC			fd,
1639         CBTS_PRNINFO*       pCbtPrnInfo,
1640         const EPS_UINT8*    Buff,
1641         EPS_INT32           BuffLen,
1642         EPS_INT32*          Cnt
1643 
1644 ){
1645     EPS_INT32   Ans;
1646 #ifdef    CBT_DBG_IO
1647     EPS_INT32   lp1;
1648 #endif
1649 
1650 	EPS_LOG_FUNCIN;
1651 
1652     Ans = ( pCbtPrnInfo->pCbtWRtnFnc )( fd, (EPS_UINT8 *)Buff, BuffLen, Cnt );
1653 
1654 #ifdef    CBT_DBG_IO
1655     sprintf(debMsgWork,"  * Write * Size=%4d(%4d) Data= ",BuffLen, *Cnt);
1656     DebMsgOut(debMsgWork);
1657     for (lp1 = 0; (lp1 < *Cnt); lp1++) {
1658         sprintf(debMsgWork,"%02X ",Buff[lp1]);
1659         DebMsgOut(debMsgWork);
1660     }
1661     DebMsgOut("\r\n");
1662 #endif
1663 
1664 	if ( Ans == 0 ){
1665         EPS_RETURN( EPCBT_ERR_NONE );
1666 	}else{
1667         EPS_RETURN( EPCBT_ERR_WRITEERROR );
1668 	}
1669 }
1670 
1671 /*******************************************|********************************************/
1672 /*                                                                                      */
1673 /* Function name:   CbtReadRtn                                                          */
1674 /*                                                                                      */
1675 /* Arguments                                                                            */
1676 /* ---------                                                                            */
1677 /* Name:        Type:               Description:                                        */
1678 /* fd           EPS_FILEDSC         I: file discripter                                  */
1679 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
1680 /* SID          EPS_UINT8           I: Socket ID                                        */
1681 /* lpBuffer     EPS_UINT8*          I: Pointer to the receive data buffer               */
1682 /* BuffSize     EPS_INT32           I: Receive data buffer size                         */
1683 /* Cnt          EPS_INT32 *         O: Receive data size                                */
1684 /* DummyRead    EPS_UINT8           I: Flag for the dummy read                          */
1685 /*                                                                                      */
1686 /* Return value:                                                                        */
1687 /*      EPCBT_ERR_NONE                  - Success                                       */
1688 /*      EPCBT_ERR_PARAM                 - Parameter error                               */
1689 /*      EPCBT_ERR_READERROR             - Read data error                               */
1690 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
1691 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
1692 /*                                                                                      */
1693 /* Description:                                                                         */
1694 /*      Recives the data from printer.                                                  */
1695 /*                                                                                      */
1696 /*******************************************|********************************************/
CbtReadRtn(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID,EPS_UINT8 * lpBuffer,EPS_INT32 BuffSize,EPS_INT32 * Cnt,EPS_UINT8 DummyRead)1697 static EPS_INT16    CbtReadRtn (
1698 
1699 		EPS_FILEDSC		fd,
1700         CBTS_PRNINFO*   pCbtPrnInfo,
1701         EPS_UINT8       SID,
1702         EPS_UINT8*      lpBuffer,
1703         EPS_INT32       BuffSize,
1704         EPS_INT32*      Cnt,
1705         EPS_UINT8       DummyRead
1706 
1707 ){
1708     EPS_INT16   Ret    = EPCBT_ERR_NONE;
1709     EPS_INT32   Ans;
1710     EPS_INT32   Count  = 0;
1711     EPS_INT32   lp1;
1712     EPS_INT32   PSize  = 0;
1713     EPS_INT32   Top;
1714     EPS_UINT8   RxSID;
1715     CBTS_CHINFO *ChSer = NULL;
1716     CBTS_CHINFO *ChPtr = NULL;
1717     EPS_UINT8   *wRBuff;
1718     EPS_INT32   nDlySpan = 10;		/* first 10 ms */
1719     EPS_INT32   nDlyTotal = 2000;	/* total 2000 ms */
1720 
1721 	EPS_LOG_FUNCIN;
1722 
1723     wRBuff = pCbtPrnInfo->pCbtReadRtnBuff;
1724     if ( DummyRead == CBT_READ_DMY ) {
1725         Ans = ( pCbtPrnInfo->pCbtRRtnFnc )( fd, wRBuff, CBT_MAX_RTMP, &Count );
1726 
1727         *Cnt = Count;
1728         if ( Ans != 0 ) {
1729             Ret = EPCBT_ERR_READERROR;
1730         }
1731 #ifdef    CBT_DBG_IO
1732         sprintf(debMsgWork,"  * Read  * Size=%4d(%4d) Data= ",CBT_MAX_RTMP, Count);
1733         DebMsgOut(debMsgWork);
1734         for (lp1 = 0; (lp1 < Count); lp1++) {
1735             sprintf(debMsgWork,"%02X ",wRBuff[lp1]);
1736             DebMsgOut(debMsgWork);
1737         }
1738         DebMsgOut("\r\n");
1739 #endif
1740 
1741 #ifdef    CBT_DBG_READ
1742     sprintf(debMsgWork,"<CBT Dummy CbtReadRtn> Req-SID[%02x] Size[%d]\r\n",SID, *Cnt);
1743     DebMsgOut(debMsgWork);
1744     for (lp1 = 0; lp1 < *Cnt; lp1++) {
1745         sprintf(debMsgWork,"%02X ",wRBuff[lp1]);
1746         DebMsgOut(debMsgWork);
1747     }
1748     DebMsgOut("\r\n");
1749 #endif
1750         EPS_RETURN( Ret );
1751     }
1752 
1753 
1754     for ( lp1 = 0; ; lp1++ ) {
1755 		if ( lp1 >= CBT_MAX_CH ){
1756             EPS_RETURN( EPCBT_ERR_PARAM );
1757 		}
1758 
1759         ChSer = &pCbtPrnInfo->Chinfo[lp1];
1760         if ( ChSer->No == SID )
1761             break;
1762     }
1763 
1764 	while(nDlyTotal > 0){
1765         if ( ChSer->RTmpCnt > 0 ) {
1766             PSize = ChSer->RTmpBuff[(ChSer->RTmpTop+3) % CBT_MAX_RTMP]
1767                         + (ChSer->RTmpBuff[(ChSer->RTmpTop+2) % CBT_MAX_RTMP] << 8);
1768 
1769             if ( BuffSize < PSize ) {
1770                 Ret = EPCBT_ERR_PARAM;
1771                 break;
1772             }
1773             for ( lp1 = 0; lp1 < PSize; lp1++ ) {
1774                 lpBuffer[lp1]  = ChSer->RTmpBuff[ChSer->RTmpTop];
1775                 ChSer->RTmpTop = (ChSer->RTmpTop+1) % CBT_MAX_RTMP;
1776                 ChSer->RTmpCnt = (ChSer->RTmpCnt-1);
1777             }
1778             *Cnt = PSize;
1779             break;
1780         }
1781 
1782 
1783 		Ans = ( pCbtPrnInfo->pCbtRRtnFnc )( fd, wRBuff, CBT_MAX_RTMP, &Count );
1784 
1785 #ifdef    CBT_DBG_IO
1786         sprintf(debMsgWork,"  * Read  * Size=%4d(%4d) Data= ",CBT_MAX_RTMP, Count);
1787         DebMsgOut(debMsgWork);
1788         for (lp1 = 0; (lp1 < Count); lp1++) {
1789             sprintf(debMsgWork,"%02X ",wRBuff[lp1]);
1790             DebMsgOut(debMsgWork);
1791         }
1792         DebMsgOut("\r\n");
1793 #endif
1794 
1795         if ( Count != 0 ) {
1796             Top = 0;
1797             for ( ;; ) {
1798                 PSize = wRBuff[Top+3] + (wRBuff[Top+2] << 8);
1799                 if ( ( Count - Top < 4 ) || ( PSize == 0 ) || ( PSize > Count - Top ) ) {
1800                     Ret = EPCBT_ERR_MULFORMEDPACKET;
1801                     break;
1802                 }
1803                 RxSID = wRBuff[Top];
1804 
1805                 for ( lp1 = 0; ; lp1++ ) {
1806                     if ( lp1 >= CBT_MAX_CH ) {
1807                         Ret = EPCBT_ERR_UEREPLY;
1808                         break;
1809                     }
1810                     ChPtr = &pCbtPrnInfo->Chinfo[lp1];
1811 
1812                     /* Match channel ? */
1813                     if ( ChPtr->No == RxSID )
1814                         break;
1815                 }
1816 
1817                 if ( Ret == EPCBT_ERR_NONE ) {
1818 
1819                     ChPtr->CreditH -= 1;
1820 
1821                     for ( lp1 = 0; lp1 < PSize; lp1++ ) {
1822                         ChPtr->RTmpBuff[ChPtr->RTmpEnd] = wRBuff[Top+lp1];
1823                         ChPtr->RTmpEnd = (ChPtr->RTmpEnd+1) % CBT_MAX_RTMP;
1824                         ChPtr->RTmpCnt = (ChPtr->RTmpCnt+1);
1825                     }
1826 
1827                     Top += PSize;
1828                     if ( Top >= Count )
1829                         break;
1830                 } else {
1831                     break;
1832                 }
1833             }
1834             continue;
1835         } else {
1836             if ( Ans != 0 ) {
1837                 Ret = EPCBT_ERR_READERROR;
1838                 break;
1839             }
1840         }
1841 
1842         /* Wait */
1843         serDelayThread(nDlySpan, &epsCmnFnc);
1844 
1845 		nDlyTotal -= nDlySpan;
1846 		if(nDlySpan < 200){
1847 			nDlySpan += nDlySpan/2;
1848 			if(nDlySpan > 200){
1849 				nDlySpan = 200;		/* max 200ms */
1850 			}
1851 		}
1852 	}
1853 
1854     if ( nDlyTotal < 0 )
1855         Ret = EPCBT_ERR_READERROR;
1856 
1857 #ifdef    CBT_DBG_READ
1858     sprintf(debMsgWork,"<CBT CbtReadRtn> Req-SID[%02x] Size[%d]\r\n",SID, *Cnt);
1859     DebMsgOut(debMsgWork);
1860     for (lp1 = 0; lp1 < *Cnt; lp1++) {
1861         sprintf(debMsgWork,"%02X ",lpBuffer[lp1]);
1862         DebMsgOut(debMsgWork);
1863     }
1864     DebMsgOut("\r\n");
1865 #endif
1866 
1867     EPS_RETURN( Ret );
1868 }
1869 
1870 /*******************************************|********************************************/
1871 /*                                                                                      */
1872 /* Function name:   CbtDummyRead                                                        */
1873 /*                                                                                      */
1874 /* Arguments                                                                            */
1875 /* ---------                                                                            */
1876 /* Name:        Type:               Description:                                        */
1877 /* fd           EPS_FILEDSC         I: file discripter                                  */
1878 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
1879 /*                                                                                      */
1880 /* Return value:                                                                        */
1881 /*      None                                                                            */
1882 /*                                                                                      */
1883 /* Description:                                                                         */
1884 /*      Try to read data from printer.                                                  */
1885 /*                                                                                      */
1886 /*******************************************|********************************************/
CbtDummyRead(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo)1887 static void     CbtDummyRead (
1888 
1889 		EPS_FILEDSC		fd,
1890         CBTS_PRNINFO*	pCbtPrnInfo
1891 
1892 ){
1893     EPS_INT32 i = 0;
1894     EPS_INT32 Cnt = 0;
1895 
1896 	EPS_LOG_FUNCIN;
1897 
1898     for ( i = 0; i < CBT_DUMYREAD_MAX ;i++ ) {
1899         if ( CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, NULL, 0, &Cnt, CBT_READ_DMY ) == EPCBT_ERR_NONE ) {
1900             if ( Cnt == 0 )
1901                 break;
1902         } else {
1903             break;
1904         }
1905     }
1906 
1907     EPS_RETURN_VOID;
1908 }
1909 
1910 /*******************************************|********************************************/
1911 /*                                                                                      */
1912 /* Function name:   CbtChDummyRead                                                      */
1913 /*                                                                                      */
1914 /* Arguments                                                                            */
1915 /* ---------                                                                            */
1916 /* Name:        Type:               Description:                                        */
1917 /* fd           EPS_FILEDSC         I: file discripter                                  */
1918 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
1919 /* ChPtr        CBTS_CHINFO*        I: Channel                                          */
1920 /*                                                                                      */
1921 /* Return value:                                                                        */
1922 /*      None                                                                            */
1923 /*                                                                                      */
1924 /* Description:                                                                         */
1925 /*      Try to read by using <ChPtr>.                                                   */
1926 /*                                                                                      */
1927 /*******************************************|********************************************/
CbtChDummyRead(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,CBTS_CHINFO * ChPtr)1928 static void     CbtChDummyRead (
1929 
1930 		EPS_FILEDSC		fd,
1931         CBTS_PRNINFO*   pCbtPrnInfo,
1932         CBTS_CHINFO*    ChPtr
1933 
1934 ){
1935     EPS_INT16   Ret;
1936     EPS_INT32   Size;
1937 
1938 	EPS_LOG_FUNCIN;
1939 
1940     do {
1941         Size = 0;
1942         if ( ChPtr->CreditH == 0 ) {
1943             Ret = CbtCreditCommand( fd, pCbtPrnInfo, ChPtr->No, 1 );
1944             if ( Ret == EPCBT_ERR_NONE )
1945                 ChPtr->CreditH += 1;
1946             else
1947                 break;
1948         }
1949         Ret = CbtReadRtn( fd, pCbtPrnInfo, ChPtr->No, NULL, 0, &Size, CBT_READ_DMY );
1950         if(Size)
1951             ChPtr->CreditH -= 1;
1952         Size -= 6;
1953     } while(Size > 0);
1954 
1955     EPS_RETURN_VOID;
1956 }
1957 
1958 /*******************************************|********************************************/
1959 /*                                                                                      */
1960 /* Function name:   CbtReplyCheck                                                       */
1961 /*                                                                                      */
1962 /* Arguments                                                                            */
1963 /* ---------                                                                            */
1964 /* Name:        Type:               Description:                                        */
1965 /* Reply        EPS_UINT8*          I: Reply data                                       */
1966 /* ReplySz      EPS_INT32           I: Reply data size                                  */
1967 /* RpyType      EPS_UINT8           I: Reply data type                                  */
1968 /*                                                                                      */
1969 /* Return value:                                                                        */
1970 /*      EPCBT_ERR_NONE                  - Success                                       */
1971 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
1972 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
1973 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
1974 /*                                                                                      */
1975 /* Description:                                                                         */
1976 /*     Checks the CBT command replys.                                                   */
1977 /*                                                                                      */
1978 /*******************************************|********************************************/
CbtReplyCheck(EPS_UINT8 * Reply,EPS_INT32 ReplySz,EPS_UINT8 RpyType)1979 static EPS_INT16    CbtReplyCheck (
1980 
1981         EPS_UINT8*  Reply,
1982         EPS_INT32   ReplySz,
1983         EPS_UINT8   RpyType
1984 
1985 ){
1986     EPS_INT16    Ret = EPCBT_ERR_NONE;
1987 
1988 	EPS_LOG_FUNCIN;
1989 
1990 	if ( ReplySz == 0 ){
1991         EPS_RETURN( EPCBT_ERR_NOREPLY );
1992 	}
1993 
1994 	if( ( Reply[0] != 0x00 ) || ( Reply[1] != 0x00 ) || ( Reply[2] != 0x00 ) ){
1995         EPS_RETURN( EPCBT_ERR_MULFORMEDPACKET );
1996 	}
1997 
1998     switch ( Reply[6] ){
1999         case    CBT_RPY_INIT:
2000             if (Reply[3] != CBT_RPY_SIZE_INIT)
2001                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2002             else if ( RpyType != CBT_CMD_INIT )
2003                 Ret = EPCBT_ERR_UEREPLY;
2004             break;
2005 
2006         case    CBT_RPY_OPENCHANNEL:
2007             if (Reply[3] != CBT_RPY_SIZE_OPENCHANNEL)
2008                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2009             else if ( RpyType != CBT_CMD_OPENCHANNEL )
2010                 Ret = EPCBT_ERR_UEREPLY;
2011             break;
2012 
2013         case    CBT_RPY_CLOSECHANNEL:
2014             if (Reply[3] != CBT_RPY_SIZE_CLOSECHANNEL)
2015                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2016             else if ( RpyType != CBT_CMD_CLOSECHANNEL )
2017                 Ret = EPCBT_ERR_UEREPLY;
2018             break;
2019 
2020         case    CBT_RPY_CREDIT:
2021             if (Reply[3] != CBT_RPY_SIZE_CREDIT)
2022                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2023             else if ( RpyType != CBT_CMD_CREDIT )
2024                 Ret = EPCBT_ERR_UEREPLY;
2025             break;
2026 
2027         case    CBT_RPY_CREDITREQUEST:
2028             if (Reply[3] != CBT_RPY_SIZE_CREDITREQUEST)
2029                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2030             else if ( RpyType != CBT_CMD_CREDITREQUEST )
2031                 Ret = EPCBT_ERR_UEREPLY;
2032             break;
2033 
2034         case    CBT_RPY_DEBIT:
2035         case    CBT_RPY_DEBITREQUEST:
2036         case    CBT_RPY_CONFIGSOCKET:
2037             break;
2038 
2039         case    CBT_RPY_EXIT:
2040             if (Reply[3] != CBT_RPY_SIZE_EXIT)
2041                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2042             else if ( RpyType != CBT_CMD_EXIT )
2043                 Ret = EPCBT_ERR_UEREPLY;
2044             break;
2045 
2046         case    CBT_RPY_GETSOCKETID:
2047             if ( RpyType != CBT_CMD_GETSOCKETID )
2048                 Ret = EPCBT_ERR_UEREPLY;
2049             break;
2050 
2051         case    CBT_RPY_GETSERVICENAME:
2052             break;
2053 
2054         case    CBT_RPY_EPSONPACKING:
2055             if (Reply[3] != CBT_RPY_SIZE_EPSONPACKING)
2056                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2057             else if ( RpyType != CBT_CMD_EPSONPACKING )
2058                 Ret = EPCBT_ERR_UEREPLY;
2059             break;
2060 
2061         default:
2062             Ret = EPCBT_ERR_MULFORMEDPACKET;
2063     }
2064 
2065     EPS_RETURN( Ret );
2066 }
2067 
2068 /*******************************************|********************************************/
2069 /*                                                                                      */
2070 /* Function name:   CbtEpsonPacking                                                     */
2071 /*                                                                                      */
2072 /* Arguments                                                                            */
2073 /* ---------                                                                            */
2074 /* Name:        Type:               Description:                                        */
2075 /* fd           EPS_FILEDSC         I: file discripter                                  */
2076 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2077 /*                                                                                      */
2078 /* Return value:                                                                        */
2079 /*      EPCBT_ERR_NONE                  - Success                                       */
2080 /*      EPCBT_ERR_CBTDISABLE            - Fail to CBT communication                     */
2081 /*                                                                                      */
2082 /* Description:                                                                         */
2083 /*      Sends EpsonPackingCommand and receives reply.                                   */
2084 /*                                                                                      */
2085 /*******************************************|********************************************/
CbtEpsonPacking(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo)2086 static EPS_INT16    CbtEpsonPacking (
2087 
2088 		EPS_FILEDSC		fd,
2089         CBTS_PRNINFO*	pCbtPrnInfo
2090 
2091 ){
2092     EPS_INT16   Ret     = EPCBT_ERR_NONE;
2093     EPS_INT16   Ans;
2094     EPS_INT32   Cnt     = 0;
2095     EPS_INT32   BuffLen = CBT_CMD_SIZE_EPSONPACKING;
2096     EPS_INT32   RSize   = 0;
2097     static const EPS_UINT8 Buff[] = {
2098                                      0x00,0x00,0x00,0x1b,0x01,0x40,0x45,0x4a,0x4c,0x20,
2099                                      0x31,0x32,0x38,0x34,0x2e,0x34,0x0a,0x40,0x45,0x4a,
2100                                      0x4c,0x0a,0x40,0x45,0x4a,0x4c,0x0a
2101                                     };
2102     EPS_UINT8   RBuff[CBT_CMDREPLY_BUFFSZ];
2103 
2104 	EPS_LOG_FUNCIN;
2105 
2106     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2107 
2108     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2109     if ( Ans != EPCBT_ERR_NONE )
2110         Ret = EPCBT_ERR_CBTDISABLE;
2111     else {
2112         if ( Cnt != BuffLen )
2113             Ret = EPCBT_ERR_CBTDISABLE;
2114     }
2115 
2116 	if ( Ret != EPCBT_ERR_NONE ){
2117         EPS_RETURN( Ret );    /* return error end */
2118 	}
2119 
2120     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2121     if ( Ans != EPCBT_ERR_NONE )
2122         Ret = EPCBT_ERR_CBTDISABLE;    /* no receive */
2123     else {
2124         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_EPSONPACKING );
2125         if ( Ret == EPCBT_ERR_NONE ) {
2126             if( ( RBuff[7] != 0x00 ) && ( RBuff[7] != 0x02 ) )
2127                 Ret = EPCBT_ERR_CBTDISABLE;
2128         }
2129     }
2130 
2131     EPS_RETURN( Ret );
2132 }
2133 
2134 /*******************************************|********************************************/
2135 /*                                                                                      */
2136 /* Function name:   CbtInitCommand                                                      */
2137 /*                                                                                      */
2138 /* Arguments                                                                            */
2139 /* ---------                                                                            */
2140 /* Name:        Type:               Description:                                        */
2141 /* fd           EPS_FILEDSC         I: file discripter                                  */
2142 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2143 /*                                                                                      */
2144 /* Return value:                                                                        */
2145 /*      EPCBT_ERR_NONE                  - Success                                       */
2146 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
2147 /*      EPCBT_ERR_INITDENIED            - Printer cannot start CBT mode                 */
2148 /*      EPCBT_ERR_VERSION               - Incompatible version                          */
2149 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
2150 /*                                                                                      */
2151 /* Description:                                                                         */
2152 /*      Sends init command and receives reply.                                          */
2153 /*                                                                                      */
2154 /*******************************************|********************************************/
CbtInitCommand(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo)2155 static EPS_INT16    CbtInitCommand (
2156 
2157 		EPS_FILEDSC		fd,
2158         CBTS_PRNINFO*   pCbtPrnInfo
2159 
2160 ){
2161     EPS_INT16   Ret     = EPCBT_ERR_NONE;
2162     EPS_INT16   Ans;
2163     EPS_INT32   Cnt     = 0;
2164     EPS_INT32   BuffLen = CBT_CMD_SIZE_INIT;
2165     EPS_INT32   RSize   = 0;
2166     static const EPS_UINT8 Buff[] = {0x00,0x00,0x00,CBT_CMD_SIZE_INIT,0x01,0x00,0x00,0x10};
2167     EPS_UINT8   RBuff[CBT_CMDREPLY_BUFFSZ];
2168 
2169 	EPS_LOG_FUNCIN;
2170 
2171     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2172 
2173     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2174     if ( Ans != EPCBT_ERR_NONE )
2175         Ret = EPCBT_ERR_FATAL;
2176     else {
2177         if ( Cnt != BuffLen )
2178             Ret = EPCBT_ERR_FATAL;
2179     }
2180 
2181     if ( Ret != EPCBT_ERR_NONE )
2182         EPS_RETURN( Ret );    /* return error end */
2183 
2184     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2185     if ( Ans != EPCBT_ERR_NONE )
2186         Ret = EPCBT_ERR_FATAL;        /* No receive */
2187     else {
2188         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_INIT );
2189         if ( Ret == EPCBT_ERR_NONE ) {
2190             if ( RBuff[7] != 0x00 ) {
2191                 if ( RBuff[7] == 0x01 )
2192                     Ret = EPCBT_ERR_INITDENIED;
2193                 else if ( RBuff[7] == 0x02 )
2194                     Ret = EPCBT_ERR_VERSION;
2195                 else if ( RBuff[7] == 0x0b )
2196                     Ret = EPCBT_ERR_INITFAILED;
2197                 else
2198                     Ret = EPCBT_ERR_UERESULT;
2199             }
2200         }
2201     }
2202 
2203     EPS_RETURN( Ret );
2204 }
2205 
2206 
2207 /*******************************************|********************************************/
2208 /*                                                                                      */
2209 /* Function name:   CbtOpenChannelCommand()                                             */
2210 /*                                                                                      */
2211 /* Arguments                                                                            */
2212 /* ---------                                                                            */
2213 /* Name:        Type:               Description:                                        */
2214 /* fd           EPS_FILEDSC         I: file discripter                                  */
2215 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Port information                                 */
2216 /* SID          EPS_UINT8           I: Socket ID                                        */
2217 /* pPtSsz       EPS_INT32*          O: Primary to secondary packet size                 */
2218 /* pStPsz       EPS_INT32*          O: Secondary to primary packet size                 */
2219 /* CreditReq    EPS_INT32           I: Credit Requested                                 */
2220 /* CreditMax    EPS_INT32           I: Maximum Outstanding Credit                       */
2221 /*                                                                                      */
2222 /* Return value:                                                                        */
2223 /*      Credit number                   - Data packet number that is able to recieve    */
2224 /*                                        by throughing <SID>.                          */
2225 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
2226 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
2227 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
2228 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
2229 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
2230 /*      EPCBT_ERR_RESOURCE              - Insufficient printer resources for OpenChannel*/
2231 /*      EPCBT_ERR_OPENCHANNEL           - Not used by EPSON                             */
2232 /*      EPCBT_ERR_CHNOTSUPPORT          - The channel is not supported                  */
2233 /*      EPCBT_ERR_PACKETSIZE            - Invalid packet size.                          */
2234 /*                                        (0x0001 ? 0x0005 bytes)                       */
2235 /*      EPCBT_ERR_NULLPACKETSZ          - Packet size is 0x0000 in both directions.     */
2236 /*                                        No data can be transferred. Channel was       */
2237 /*                                        not opened                                    */
2238 /*                                                                                      */
2239 /* Description:                                                                         */
2240 /*      Sends the open channel command and receives the reply.                          */
2241 /*                                                                                      */
2242 /*******************************************|********************************************/
CbtOpenChannelCommand(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID,EPS_UINT32 * pPtSsz,EPS_UINT32 * pStPsz,EPS_UINT32 CreditReq,EPS_UINT32 CreditMax)2243 static EPS_INT32    CbtOpenChannelCommand (
2244 
2245 		EPS_FILEDSC		fd,
2246         CBTS_PRNINFO*   pCbtPrnInfo,
2247         EPS_UINT8       SID,
2248         EPS_UINT32*     pPtSsz,
2249         EPS_UINT32*     pStPsz,
2250         EPS_UINT32      CreditReq,
2251         EPS_UINT32      CreditMax
2252 
2253 ){
2254     EPS_INT32   Ret     = EPCBT_ERR_NONE;
2255     EPS_INT16   Ans;
2256     EPS_INT32   Cnt     = 0;
2257     EPS_INT32   BuffLen = CBT_CMD_SIZE_OPENCHANNEL;
2258     EPS_INT32   RSize   = 0;
2259     static EPS_UINT8    Buff[] = {0x00,0x00,0x00,CBT_CMD_SIZE_OPENCHANNEL,0x01,0x00,0x01,0x00,0x00,0,0,0,0,0,0,0,0};
2260     EPS_UINT8   RBuff[CBT_CMDREPLY_BUFFSZ];
2261 
2262 	EPS_LOG_FUNCIN;
2263 
2264     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2265 
2266     Buff[7] = SID;
2267     Buff[8] = SID;
2268 
2269     CbtPutBigEndianByte2(*pPtSsz,&Buff[9]);
2270     CbtPutBigEndianByte2(*pStPsz,&Buff[11]);
2271     CbtPutBigEndianByte2(CreditReq,&Buff[13]);
2272     CbtPutBigEndianByte2(CreditMax,&Buff[15]);
2273 
2274     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2275     if ( Ans != EPCBT_ERR_NONE )
2276         Ret = EPCBT_ERR_FATAL;
2277     else {
2278         if ( Cnt != BuffLen )
2279             Ret = EPCBT_ERR_FATAL;
2280     }
2281 
2282 	if ( Ret != EPCBT_ERR_NONE ){
2283         EPS_RETURN( Ret );    /* return error end */
2284 	}
2285 
2286     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2287     if ( Ans != EPCBT_ERR_NONE )
2288         Ret = EPCBT_ERR_FATAL;        /* No receive */
2289     else {
2290         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_OPENCHANNEL );
2291         if ( Ret == EPCBT_ERR_NONE ) {
2292             if ( (RBuff[7] != 0x00) && (RBuff[7] != 0x06) ) {
2293                 if ( RBuff[7] == 0x04 )
2294                     Ret = EPCBT_ERR_RESOURCE;
2295                 else if ( RBuff[7] == 0x05 )
2296                     Ret = EPCBT_ERR_OPENCHANNEL;
2297                 else if ( RBuff[7] == 0x09 )
2298                     Ret = EPCBT_ERR_CHNOTSUPPORT;
2299                 else if ( RBuff[7] == 0x0C )
2300                     Ret = EPCBT_ERR_PACKETSIZE;
2301                 else if ( RBuff[7] == 0x0D )
2302                     Ret = EPCBT_ERR_NULLPACKETSZ;
2303                 else
2304                     Ret = EPCBT_ERR_UERESULT;
2305             }
2306         }
2307     }
2308 
2309 	if ( Ret != EPCBT_ERR_NONE ){
2310         EPS_RETURN( Ret );    /* error end */
2311 	}
2312 
2313     *pPtSsz = (EPS_UINT32)((RBuff[10] << 8) + RBuff[11]);
2314     *pStPsz = (EPS_UINT32)((RBuff[12] << 8) + RBuff[13]);
2315     Ret     = (RBuff[14] << 8) + RBuff[15];
2316 
2317     EPS_RETURN( Ret );
2318 }
2319 
2320 /*******************************************|********************************************/
2321 /*                                                                                      */
2322 /* Function name:   CbtCloseChannelCommand()                                            */
2323 /*                                                                                      */
2324 /* Arguments                                                                            */
2325 /* ---------                                                                            */
2326 /* Name:        Type:               Description:                                        */
2327 /* fd           EPS_FILEDSC         I: file discripter                                  */
2328 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Port information                                 */
2329 /* SID          EPS_UINT8           I: Socket ID                                        */
2330 /*                                                                                      */
2331 /* Return value:                                                                        */
2332 /*      EPCBT_ERR_NONE                  - Success                                       */
2333 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
2334 /*      EPCBT_ERR_CLOSEDENIED           - Close Channel is denied                       */
2335 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
2336 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
2337 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
2338 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
2339 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
2340 /*                                                                                      */
2341 /* Description:                                                                         */
2342 /*      Send CloseChannel and receive reply.                                            */
2343 /*                                                                                      */
2344 /*******************************************|********************************************/
CbtCloseChannelCommand(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID)2345 static EPS_INT16    CbtCloseChannelCommand (
2346 
2347  		EPS_FILEDSC		fd,
2348 		CBTS_PRNINFO*   pCbtPrnInfo,
2349         EPS_UINT8       SID
2350 
2351 ){
2352     EPS_INT16   Ret     = EPCBT_ERR_NONE;
2353     EPS_INT16   Ans;
2354     EPS_INT32   Cnt     = 0;
2355     EPS_INT32   BuffLen = CBT_CMD_SIZE_CLOSECHANNEL;
2356     EPS_INT32   RSize   = 0;
2357     static EPS_UINT8    Buff[] = {0x00,0x00,0x00,CBT_CMD_SIZE_CLOSECHANNEL,0x01,0x00,0x02,0x00,0x00,0x00};
2358     EPS_UINT8   RBuff[CBT_CMDREPLY_BUFFSZ];
2359 
2360 	EPS_LOG_FUNCIN;
2361 
2362     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2363 
2364     Buff[7] = SID;
2365     Buff[8] = SID;
2366     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2367     if ( Ans != EPCBT_ERR_NONE )
2368         Ret = EPCBT_ERR_FATAL;
2369     else {
2370         if ( Cnt != BuffLen )
2371             Ret = EPCBT_ERR_FATAL;
2372     }
2373 
2374 	if ( Ret != EPCBT_ERR_NONE ){
2375         EPS_RETURN( Ret );    /* return error end */
2376 	}
2377 
2378     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2379     if ( Ans != EPCBT_ERR_NONE )
2380         Ret = EPCBT_ERR_FATAL;        /* No receive */
2381     else{
2382         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_CLOSECHANNEL );
2383         if ( Ret == EPCBT_ERR_NONE ) {
2384             if ( RBuff[7] != 0x00 ) {
2385                 if ( RBuff[7] == 0x03 )
2386                     Ret = EPCBT_ERR_CLOSEDENIED;
2387                 else if ( RBuff[7] == 0x08 )
2388                     Ret = EPCBT_ERR_CMDDENIED;
2389                 else
2390                     Ret = EPCBT_ERR_UERESULT;
2391             }
2392         }
2393     }
2394 
2395     EPS_RETURN( Ret );
2396 }
2397 
2398 /*******************************************|********************************************/
2399 /*                                                                                      */
2400 /* Function name:   CbtCreditReqCommand()                                               */
2401 /*                                                                                      */
2402 /* Arguments                                                                            */
2403 /* ---------                                                                            */
2404 /* Name:        Type:               Description:                                        */
2405 /* fd           EPS_FILEDSC         I: file discripter                                  */
2406 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2407 /* SID          EPS_UINT8           I: SocketID                                         */
2408 /* CreditReq    EPS_INT32           I: Credit Requested                                 */
2409 /* MaxCredit    EPS_INT32           I: Maximum OutStanding Credit                       */
2410 /*                                                                                      */
2411 /* Return value:                                                                        */
2412 /*      Credit                          - Success                                       */
2413 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
2414 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
2415 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
2416 /*                                                                                      */
2417 /* Description:                                                                         */
2418 /*      Request credits and return credit number.                                       */
2419 /*                                                                                      */
2420 /*******************************************|********************************************/
CbtCreditReqCommand(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID,EPS_UINT32 CreditReq,EPS_UINT32 MaxCredit)2421 static EPS_INT32    CbtCreditReqCommand (
2422 
2423         EPS_FILEDSC		fd,
2424 		CBTS_PRNINFO*   pCbtPrnInfo,
2425         EPS_UINT8       SID,
2426         EPS_UINT32      CreditReq,
2427         EPS_UINT32      MaxCredit
2428 
2429 ){
2430     EPS_INT32       Ret     = EPCBT_ERR_NONE;
2431     EPS_INT16       Ans;
2432     EPS_INT32       Cnt     = 0;
2433     EPS_INT32       BuffLen = CBT_CMD_SIZE_CREDITREQUEST;
2434     EPS_INT32       RSize   = 0;
2435     static EPS_UINT8 Buff[] = {0x00,0x00,0x00,CBT_CMD_SIZE_CREDITREQUEST,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00};
2436     EPS_UINT8       RBuff[CBT_CMDREPLY_BUFFSZ];
2437 
2438 	EPS_LOG_FUNCIN;
2439 
2440     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2441 
2442     Buff[7] = SID;
2443     Buff[8] = SID;
2444 
2445     CbtPutBigEndianByte2(CreditReq,&Buff[9]);
2446     CbtPutBigEndianByte2(MaxCredit,&Buff[11]);
2447 
2448     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2449     if ( Ans != EPCBT_ERR_NONE )
2450         Ret = EPCBT_ERR_FATAL;
2451     else {
2452         if ( Cnt != BuffLen )
2453             Ret = EPCBT_ERR_FATAL;
2454     }
2455 
2456 	if ( Ret != EPCBT_ERR_NONE ){
2457         EPS_RETURN( Ret );    /* return error end */
2458 	}
2459 
2460     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2461     if ( Ans != EPCBT_ERR_NONE )
2462         Ret = EPCBT_ERR_FATAL;        /* No receive */
2463     else {
2464         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_CREDITREQUEST );
2465         if ( Ret == EPCBT_ERR_NONE ) {
2466             if ( RBuff[7] != 0x00 ) {
2467                 if ( RBuff[7] == 0x08 )
2468                     Ret = EPCBT_ERR_CMDDENIED;
2469                 else
2470                     Ret = EPCBT_ERR_UERESULT;
2471             }
2472         }
2473     }
2474 
2475 	if ( Ret != EPCBT_ERR_NONE ){
2476         EPS_RETURN( Ret );    /* return error end */
2477 	}
2478 
2479     Ret = ( RBuff[11] + (RBuff[10] << 8) );
2480 
2481     EPS_RETURN( Ret );
2482 }
2483 
2484 /*******************************************|********************************************/
2485 /*                                                                                      */
2486 /* Function name:   CbtCreditCommand()                                                  */
2487 /*                                                                                      */
2488 /* Arguments                                                                            */
2489 /* ---------                                                                            */
2490 /* Name:        Type:               Description:                                        */
2491 /* fd           EPS_FILEDSC         I: file discripter                                  */
2492 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2493 /* SID          EPS_UINT8           I: SocketID                                         */
2494 /* Credit       EPS_INT32           I: Credit                                           */
2495 /*                                                                                      */
2496 /* Return value:                                                                        */
2497 /*      EPCBT_ERR_NONE                  - Success                                       */
2498 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
2499 /*      EPCBT_ERR_CREDITOVF             - Credit over flow                              */
2500 /*      EPCBT_ERR_CMDDENIED             - Command is denied (Channel is not opened)     */
2501 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
2502 /*      EPCBT_ERR_NOREPLY               - No reply from printer                         */
2503 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
2504 /*      EPCBT_ERR_UEREPLY               - Received reply for a command not issued       */
2505 /*                                                                                      */
2506 /* Description:                                                                         */
2507 /*      Open the communication channel.                                                 */
2508 /*                                                                                      */
2509 /*******************************************|********************************************/
CbtCreditCommand(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID,EPS_UINT32 Credit)2510 static EPS_INT16    CbtCreditCommand (
2511 
2512 		EPS_FILEDSC		fd,
2513         CBTS_PRNINFO*   pCbtPrnInfo,
2514         EPS_UINT8       SID,
2515         EPS_UINT32      Credit
2516 
2517 ){
2518     EPS_INT16       Ret     = EPCBT_ERR_NONE;
2519     EPS_INT16       Ans;
2520     EPS_INT32       Cnt     = 0;
2521     EPS_INT32       BuffLen = CBT_CMD_SIZE_CREDIT;
2522     EPS_INT32       RSize   = 0;
2523     static EPS_UINT8 Buff[] = {0x00,0x00,0x00,CBT_CMD_SIZE_CREDIT,0x01,0x00,0x03,0x00,0x00,0x00,0x00};
2524     EPS_UINT8       RBuff[CBT_CMDREPLY_BUFFSZ];
2525 
2526 	EPS_LOG_FUNCIN;
2527 
2528     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2529 
2530     Buff[7] = SID;
2531     Buff[8] = SID;
2532 
2533     CbtPutBigEndianByte2(Credit,&Buff[9]);
2534 
2535     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2536     if ( Ans != EPCBT_ERR_NONE )
2537         Ret = EPCBT_ERR_FATAL;
2538     else {
2539         if ( Cnt != BuffLen )
2540             Ret = EPCBT_ERR_FATAL;
2541     }
2542 
2543 	if ( Ret != EPCBT_ERR_NONE ){
2544         EPS_RETURN( Ret );    /* return error end */
2545 	}
2546 
2547     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2548 
2549     if ( Ans != EPCBT_ERR_NONE )
2550         Ret = EPCBT_ERR_FATAL;        /* No receive */
2551     else {
2552         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_CREDIT );
2553         if ( Ret == EPCBT_ERR_NONE ) {
2554             if ( RBuff[7] != 0x00 ) {
2555                 if ( RBuff[7] == 0x07 )
2556                     Ret = EPCBT_ERR_CREDITOVF;
2557                 else if ( RBuff[7] == 0x08 )
2558                     Ret = EPCBT_ERR_CMDDENIED;
2559                 else
2560                     Ret = EPCBT_ERR_UERESULT;
2561             }
2562         }
2563     }
2564 
2565     EPS_RETURN( Ret );
2566 }
2567 
2568 /*******************************************|********************************************/
2569 /*                                                                                      */
2570 /* Function name:   CbtGetSocketIDCommand                                               */
2571 /*                                                                                      */
2572 /* Arguments                                                                            */
2573 /* ---------                                                                            */
2574 /* Name:        Type:               Description:                                        */
2575 /* fd           EPS_FILEDSC         I: file discripter                                  */
2576 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2577 /* lpNamepName  EPS_UINT8 *         I: Parameter Name                                   */
2578 /* Size         EPS_INT32           I: Size of socket ID                                */
2579 /* SocketID     EPS_UINT8 *         O: SocketID                                         */
2580 /*                                                                                      */
2581 /* Return value:                                                                        */
2582 /*      EPCBT_ERR_NONE                  - Success                                       */
2583 /*      EPCBT_ERR_FATAL                 - Fatal communication error                     */
2584 /*      EPCBT_ERR_MULFORMEDPACKET       - Received invalid packet                       */
2585 /*      EPCBT_ERR_NOSERVICE             - The channel does not support the requested    */
2586 /*                                        service                                       */
2587 /*      EPCBT_ERR_UERESULT              - Undefined result value                        */
2588 /*                                                                                      */
2589 /* Description:                                                                         */
2590 /*      Sends GetSocketID and receives reply.                                           */
2591 /*                                                                                      */
2592 /*******************************************|********************************************/
2593 #if LCOMSW_CBT_SOCKETFUNC_ON
CbtGetSocketIDCommand(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 * lpName,EPS_INT32 Size,EPS_UINT8 * SocketID)2594 static EPS_INT16    CbtGetSocketIDCommand (
2595 
2596 		EPS_FILEDSC		fd,
2597         CBTS_PRNINFO*   pCbtPrnInfo,
2598         EPS_UINT8*      lpName,
2599         EPS_INT32       Size,
2600         EPS_UINT8*      SocketID
2601 
2602 ){
2603 /*** Declare Variable Local to Routine                                                  */
2604     EPS_INT16   Ret   = EPCBT_ERR_NONE;
2605     EPS_INT16   Ans;
2606     EPS_INT32   Cnt   = 0;
2607     EPS_INT32   BuffLen;
2608     EPS_INT32   lp1;
2609     EPS_INT32   RSize = 0;
2610     EPS_UINT8   Buff[CBT_CMD_SIZE_GETSOCKETID + 40];
2611     EPS_UINT8   RBuff[CBT_CMDREPLY_BUFFSZ];
2612 
2613 	EPS_LOG_FUNCIN;
2614 
2615 /*** Initialize Local Variables                                                         */
2616     memset(Buff,  0, CBT_CMD_SIZE_GETSOCKETID + 40);
2617     memset(RBuff, 0, CBT_CMDREPLY_BUFFSZ);
2618 
2619     BuffLen = CBT_CMD_SIZE_GETSOCKETID + Size;
2620 
2621     Buff[0] = 0x00;
2622     Buff[1] = 0x00;
2623     Buff[2] = 0x00;
2624     Buff[3] = (EPS_UINT8)BuffLen;
2625     Buff[4] = 0x01;
2626     Buff[5] = 0x00;
2627     Buff[6] = 0x09;
2628 
2629 /*** Sends GetSocketID and receives reply                                               */
2630     for ( lp1 = 0; lp1 < Size; lp1++ )
2631         Buff[7+lp1] = lpName[lp1];
2632 
2633     Ans = CbtWriteRtn( fd, pCbtPrnInfo, Buff, BuffLen, &Cnt );
2634 
2635     if ( Ans != EPCBT_ERR_NONE )
2636         Ret = EPCBT_ERR_FATAL;
2637     else {
2638         if (Cnt != BuffLen)
2639             Ret = EPCBT_ERR_FATAL;
2640     }
2641 
2642 	if ( Ret != EPCBT_ERR_NONE ){
2643         EPS_RETURN( Ret );    /* return error end */
2644 	}
2645 
2646     Ans = CbtReadRtn( fd, pCbtPrnInfo, CBT_SID_MAIN, RBuff, CBT_CMDREPLY_BUFFSZ, &RSize, CBT_READ_NORMAL );
2647     if ( Ans != EPCBT_ERR_NONE )
2648         Ret = EPCBT_ERR_FATAL;        /* No receive */
2649     else {
2650         Ret = CbtReplyCheck( RBuff, RSize, CBT_CMD_GETSOCKETID );
2651         if ( Ret == EPCBT_ERR_NONE ) {
2652             if ( RBuff[3] != (Size + CBT_RPY_SIZE_GETSOCKETID) )
2653                 Ret = EPCBT_ERR_MULFORMEDPACKET;
2654             else if ( RBuff[7] != 0x00 ) {
2655                 if ( RBuff[7] == 0x0A )
2656                     Ret = EPCBT_ERR_NOSERVICE;
2657                 else
2658                     Ret = EPCBT_ERR_UERESULT;
2659             } else {
2660                 *SocketID = RBuff[8];
2661             }
2662         }
2663     }
2664 
2665     EPS_RETURN( Ret );
2666 }
2667 #endif    /* def LCOMSW_CBT_SOCKETFUNC_ON */
2668 
2669 /*******************************************|********************************************/
2670 /*                                                                                      */
2671 /* Function name:   CbtDataWrite()                                                      */
2672 /*                                                                                      */
2673 /* Arguments                                                                            */
2674 /* ---------                                                                            */
2675 /* Name:        Type:               Description:                                        */
2676 /* fd           EPS_FILEDSC         I: file discripter                                  */
2677 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2678 /* SID          EPS_UINT8           I: SocketID                                         */
2679 /* lpBuffer     const EPS_UINT8*    I: Pointer to the write data buffer                 */
2680 /* Size         EPS_INT32           I: Size of the write data buffer                    */
2681 /*                                                                                      */
2682 /* Return value:                                                                        */
2683 /*      Actually Write Size             - Success                                       */
2684 /*      EPCBT_ERR_WRITEERROR            - Failed to write                               */
2685 /*                                                                                      */
2686 /* Description:                                                                         */
2687 /*      Send data to printer.                                                           */
2688 /*                                                                                      */
2689 /*******************************************|********************************************/
CbtDataWrite(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID,const EPS_UINT8 * lpBuffer,EPS_INT32 Size)2690 static EPS_INT32    CbtDataWrite (
2691 
2692         EPS_FILEDSC			fd,
2693 		CBTS_PRNINFO*       pCbtPrnInfo,
2694         EPS_UINT8           SID,
2695         const EPS_UINT8*    lpBuffer,
2696         EPS_INT32           Size
2697 
2698 ){
2699     EPS_INT32   Ret = EPCBT_ERR_NONE;
2700     EPS_INT16   Ans;
2701     EPS_INT32   Cnt = 0;
2702     EPS_UINT32  BuffLen;
2703     EPS_UINT32  lp1;
2704     EPS_UINT8*  Buff;
2705 
2706 	EPS_LOG_FUNCIN;
2707 
2708     Buff = pCbtPrnInfo->pWriteBuff;
2709     *(Buff + 0) = SID;
2710     *(Buff + 1) = SID;
2711 
2712     /* check packet size and Control */
2713     if( (Size+6) > CBT_TXPSIZE)
2714         BuffLen = CBT_TXPSIZE;
2715     else
2716         BuffLen = (EPS_UINT32)(Size + 6);
2717 
2718     CbtPutBigEndianByte2(BuffLen, (Buff+2));
2719     *(Buff + 4) = 0;
2720     *(Buff + 5) = 0x01;            /* end data */
2721 
2722     Buff += 6;
2723     for ( lp1=6; lp1<BuffLen; lp1++ )
2724         *Buff++ = *lpBuffer++;
2725 
2726     Ans = CbtWriteRtn( fd, pCbtPrnInfo, pCbtPrnInfo->pWriteBuff, (EPS_INT32)BuffLen, &Cnt );
2727 
2728     Ret = Cnt - 6;
2729     if (Ret < 0)
2730         Ret = EPCBT_ERR_WRITEERROR;
2731     else
2732         if ( Ans != EPCBT_ERR_NONE )
2733             Ret = EPCBT_ERR_WRITEERROR;
2734 
2735     EPS_RETURN( Ret );
2736 }
2737 
2738 /*******************************************|********************************************/
2739 /*                                                                                      */
2740 /* Function name:   CbtDataRead                                                         */
2741 /*                                                                                      */
2742 /* Arguments                                                                            */
2743 /* ---------                                                                            */
2744 /* Name:        Type:               Description:                                        */
2745 /* fd           EPS_FILEDSC         I: file discripter                                  */
2746 /* pCbtPrnInfo  CBTS_PRNINFO*       I: Printer information                              */
2747 /* SID          EPS_UINT8           I: Socket ID                                        */
2748 /* lpBuffer     EPS_UINT8*          O: Pointer to buffer for read data                  */
2749 /* Size         EPS_INT32*          O: Pointer to variable for read data size           */
2750 /*                                                                                      */
2751 /* Return value:                                                                        */
2752 /*      EPCBT_ERR_NONE                  - Success                                       */
2753 /*      EPCBT_ERR_CHNOTOPEN             - Channel is not opened                         */
2754 /*      EPCBT_ERR_READERROR             - Data read error                               */
2755 /*                                                                                      */
2756 /* Description:                                                                         */
2757 /*      Read data from printer.                                                         */
2758 /*                                                                                      */
2759 /*******************************************|********************************************/
CbtDataRead(EPS_FILEDSC fd,CBTS_PRNINFO * pCbtPrnInfo,EPS_UINT8 SID,EPS_UINT8 * lpBuffer,EPS_INT32 * Size)2760 static EPS_INT16 CbtDataRead (
2761 
2762 		EPS_FILEDSC		fd,
2763         CBTS_PRNINFO*   pCbtPrnInfo,
2764         EPS_UINT8       SID,
2765         EPS_UINT8*      lpBuffer,
2766         EPS_INT32*      Size
2767 
2768 ){
2769     EPS_INT16       Ret   = EPCBT_ERR_NONE;
2770     EPS_INT16       Ans;
2771     EPS_INT32       Cnt   = 0;
2772     EPS_INT32       lp1;
2773     CBTS_CHINFO*    ChPtr = NULL;
2774     EPS_UINT8*      RBuff;
2775     EPS_UINT8*      SBuff;
2776 
2777  	EPS_LOG_FUNCIN;
2778 
2779    for ( lp1 = 0; ; lp1++ ) {
2780 	   if ( lp1 >= CBT_MAX_CH ){
2781             EPS_RETURN( EPCBT_ERR_CHNOTOPEN );
2782 	   }
2783 
2784         ChPtr = &pCbtPrnInfo->Chinfo[lp1];
2785         if ( ChPtr->No == SID )            /* channel open ? */
2786             break;
2787     }
2788 
2789     SBuff = ChPtr->ReadBuff;
2790     if ( lpBuffer == NULL ) {
2791         RBuff = pCbtPrnInfo->pCbtDataReadBuff;
2792         Ans = CbtReadRtn( fd, pCbtPrnInfo, SID, RBuff, CBT_MAX_RTMP, &Cnt, CBT_READ_NORMAL );
2793         if ( Cnt > 6 ) {
2794             Cnt -= 6;
2795             RBuff += 6;
2796             SBuff += ChPtr->ReadSize;
2797             for ( lp1 = 0; lp1 < Cnt; lp1++ )
2798                 *SBuff++ = *RBuff++;
2799 
2800             ChPtr->ReadSize += Cnt;
2801         } else {
2802             if ( Ans != EPCBT_ERR_NONE )
2803                 Ret = EPCBT_ERR_READERROR;
2804         }
2805         *Size = ChPtr->ReadSize;
2806     } else {
2807         RBuff = lpBuffer;
2808         /* Buffer Size Check */
2809         if ( *Size > ChPtr->ReadSize )
2810             *Size = ChPtr->ReadSize;
2811         for ( lp1 = 0; lp1 < *Size; lp1++ )
2812             *RBuff++ = *SBuff++;
2813         ChPtr->ReadSize = 0;
2814     }
2815 
2816     EPS_RETURN( Ret );
2817 }
2818 
2819 /*******************************************|********************************************/
2820 /*                                                                                      */
2821 /* Function name:   CbtMemAlloc                                                         */
2822 /*                                                                                      */
2823 /* Arguments                                                                            */
2824 /* ---------                                                                            */
2825 /* Name:        Type:               Description:                                        */
2826 /* -            void                -                                                   */
2827 /*                                                                                      */
2828 /* Return value:                                                                        */
2829 /*      EPCBT_ERR_NONE                  - Success                                       */
2830 /*      EPCBT_ERR_MEMORY                - Fail to memory allocation                     */
2831 /*                                                                                      */
2832 /* Description:                                                                         */
2833 /*      Allocates the memory for CBT modules.                                           */
2834 /*                                                                                      */
2835 /*******************************************|********************************************/
CbtMemAlloc(void)2836 static EPS_INT16    CbtMemAlloc (
2837 
2838         void
2839 
2840 ){
2841     EPS_INT32       lp1;
2842     CBTS_CHINFO     *ChPtr;
2843 
2844 #if    LCOMSW_CBT_ALLOC_MEM
2845     gpCbtPrnInfo = (CBTS_PRNINFO*)EPS_ALLOC(sizeof(CBTS_PRNINFO));
2846     if(!gpCbtPrnInfo)
2847         return    EPCBT_ERR_MEMORY;
2848     memset(gpCbtPrnInfo, 0, sizeof(CBTS_PRNINFO));
2849 #else    /* LCOMSW_CBT_ALLOC_MEM */
2850     gpCbtPrnInfo = &sgcbtprninfo;
2851 #endif    /* LCOMSW_CBT_ALLOC_MEM */
2852 
2853     /* clear */
2854     gpCbtPrnInfo->pPortInfo        = NULL;
2855     gpCbtPrnInfo->pWriteBuff       = NULL;
2856     gpCbtPrnInfo->pCbtReadRtnBuff  = NULL;
2857     gpCbtPrnInfo->pCbtDataReadBuff = NULL;
2858     for ( lp1 = 0; lp1 < CBT_MAX_CH; lp1++ ) {
2859         ChPtr           = &gpCbtPrnInfo->Chinfo[lp1];
2860         ChPtr->No       = (EPS_UINT8)(lp1 == 0 ? CBT_SID_MAIN : 0);
2861         ChPtr->PtSsize  = 0;
2862         ChPtr->StPsize  = 0;
2863         ChPtr->CreditP  = 0;
2864         ChPtr->CreditH  = 0;
2865         ChPtr->ReadSize = 0;
2866         ChPtr->ReadBuff = NULL;
2867         ChPtr->RTmpBuff = NULL;
2868         ChPtr->RTmpTop  = 0;
2869         ChPtr->RTmpEnd  = 0;
2870         ChPtr->RTmpCnt  = 0;
2871     }
2872 
2873 #if    LCOMSW_CBT_ALLOC_MEM
2874     gpCbtPrnInfo->pWriteBuff       = (EPS_UINT8*)EPS_ALLOC(CBT_TXPSIZE);
2875     if(!gpCbtPrnInfo->pWriteBuff)
2876         return    EPCBT_ERR_MEMORY;
2877     gpCbtPrnInfo->pCbtReadRtnBuff  = (EPS_UINT8*)EPS_ALLOC(CBT_MAX_RTMP);
2878     if(!gpCbtPrnInfo->pCbtReadRtnBuff)
2879         return    EPCBT_ERR_MEMORY;
2880     gpCbtPrnInfo->pCbtDataReadBuff = (EPS_UINT8*)EPS_ALLOC(CBT_MAX_RTMP);
2881     if(!gpCbtPrnInfo->pCbtDataReadBuff)
2882         return    EPCBT_ERR_MEMORY;
2883 #else    /* LCOMSW_CBT_ALLOC_MEM */
2884     gpCbtPrnInfo->pWriteBuff       = (EPS_UINT8*)&sgwritebuff[0];
2885     gpCbtPrnInfo->pCbtReadRtnBuff  = (EPS_UINT8*)&sgcbtreadrtnbuff[0];
2886     gpCbtPrnInfo->pCbtDataReadBuff = (EPS_UINT8*)&sgcbtdatareadbuff[0];
2887 #endif    /* LCOMSW_CBT_ALLOC_MEM */
2888 
2889     for ( lp1 = 0; lp1 < CBT_MAX_CH; lp1++ ) {
2890         ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
2891 
2892 #if    LCOMSW_CBT_ALLOC_MEM
2893         ChPtr->ReadBuff = (EPS_UINT8*)EPS_ALLOC(CBT_MAX_RTMP);
2894         if(!ChPtr->ReadBuff)
2895             return    EPCBT_ERR_MEMORY;
2896         ChPtr->RTmpBuff = (EPS_UINT8*)EPS_ALLOC(CBT_MAX_RTMP);
2897         if(!ChPtr->RTmpBuff)
2898             return    EPCBT_ERR_MEMORY;
2899 #else    /* LCOMSW_CBT_ALLOC_MEM */
2900         ChPtr->ReadBuff = (EPS_UINT8*)&sgreadbuff[lp1][0];
2901         ChPtr->RTmpBuff = (EPS_UINT8*)&sgrtmpbuff[lp1][0];
2902 #endif    /* LCOMSW_CBT_ALLOC_MEM */
2903 
2904     }
2905 
2906     return    EPCBT_ERR_NONE;
2907 }
2908 
2909 /*******************************************|********************************************/
2910 /*                                                                                      */
2911 /* Function name:   CbtMemFree                                                          */
2912 /*                                                                                      */
2913 /* Arguments                                                                            */
2914 /* ---------                                                                            */
2915 /* Name:        Type:               Description:                                        */
2916 /* -            void                -                                                   */
2917 /*                                                                                      */
2918 /* Return value:                                                                        */
2919 /*      None                                                                            */
2920 /*                                                                                      */
2921 /* Description:                                                                         */
2922 /*      Frees the memory for CBT modules.                                               */
2923 /*                                                                                      */
2924 /*******************************************|********************************************/
CbtMemFree(void)2925 static void     CbtMemFree (
2926 
2927         void
2928 
2929 ){
2930     EPS_INT32       lp1;
2931     CBTS_CHINFO     *ChPtr;
2932 
2933     if(!gpCbtPrnInfo) {
2934         return;
2935     }
2936 
2937     for ( lp1 = 0; lp1 < CBT_MAX_CH; lp1++ ) {
2938         ChPtr = &gpCbtPrnInfo->Chinfo[lp1];
2939 #if    LCOMSW_CBT_ALLOC_MEM
2940         EPS_SAFE_RELEASE(ChPtr->ReadBuff);
2941         EPS_SAFE_RELEASE(ChPtr->RTmpBuff);
2942 #else    /* LCOMSW_CBT_ALLOC_MEM */
2943         ChPtr->ReadBuff = NULL;
2944         ChPtr->RTmpBuff = NULL;
2945 #endif    /* LCOMSW_CBT_ALLOC_MEM */
2946 
2947     }
2948 
2949 #if    LCOMSW_CBT_ALLOC_MEM
2950     EPS_SAFE_RELEASE(gpCbtPrnInfo->pWriteBuff);
2951     EPS_SAFE_RELEASE(gpCbtPrnInfo->pCbtReadRtnBuff);
2952     EPS_SAFE_RELEASE(gpCbtPrnInfo->pCbtDataReadBuff);
2953     EPS_SAFE_RELEASE(gpCbtPrnInfo);
2954 #else    /* LCOMSW_CBT_ALLOC_MEM */
2955     gpCbtPrnInfo->pWriteBuff       = NULL;
2956     gpCbtPrnInfo->pCbtReadRtnBuff  = NULL;
2957     gpCbtPrnInfo->pCbtDataReadBuff = NULL;
2958     gpCbtPrnInfo = NULL;
2959 #endif    /* LCOMSW_CBT_ALLOC_MEM */
2960 
2961     return;
2962 }
2963 
2964 /*******************************************|********************************************/
2965 /*                                                                                      */
2966 /* Function name:   CbtPutBigEndianByte2()                                              */
2967 /*                                                                                      */
2968 /* Arguments                                                                            */
2969 /* ---------                                                                            */
2970 /* Name:        Type:               Description:                                        */
2971 /* value        EPS_INT32           I: same value                                       */
2972 /* buf[2]       EPS_UINT8           I/O: Big endian value                               */
2973 /*                                                                                      */
2974 /* Return value:                                                                        */
2975 /*      None                                                                            */
2976 /*                                                                                      */
2977 /* Description:                                                                         */
2978 /*      Change input value to the big endian value.                                     */
2979 /*                                                                                      */
2980 /*******************************************|********************************************/
CbtPutBigEndianByte2(EPS_UINT32 value,EPS_UINT8 * buf)2981 static void     CbtPutBigEndianByte2 (
2982 
2983         EPS_UINT32  value,
2984         EPS_UINT8   *buf
2985 
2986 ){
2987 
2988 /*** Change 2 bytes value to the big endianness                                 */
2989     EPS_UINT16 value2byte = (EPS_UINT16)value;
2990 
2991     buf[0] = (EPS_UINT8)((value2byte >> 8) & 0x00ff);
2992     buf[1] = (EPS_UINT8)((value2byte     ) & 0x00ff);
2993 
2994 }
2995 
2996 /*___________________________________  epson-cbt.c  ____________________________________*/
2997 
2998 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
2999 /*       1         2         3         4         5         6         7         8        */
3000 /*******************************************|********************************************/
3001 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
3002 /***** End of File *** End of File *** End of File *** End of File *** End of File ******/
3003 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
3004 
3005