Tizen 2.0 Release
[external/epson-laser-printer-escpage.git] / lib / epson-protocol.c
1 /*________________________________  epson-protocol.c   _________________________________*/\r
2 \r
3 /*       1         2         3         4         5         6         7         8        */\r
4 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/\r
5 /*******************************************|********************************************/\r
6 /*\r
7  *   Copyright (c) 2009  Seiko Epson Corporation   All rights reserved.\r
8  *\r
9  *   Copyright protection claimed includes all forms and matters of copyrightable\r
10  *   material and information now allowed by statutory or judicial law or hereinafter\r
11  *   granted, including without limitation, material generated from the software\r
12  *   programs which are displayed on the screen such as icons, screen display looks,\r
13  *   etc.\r
14  */\r
15 /*******************************************|********************************************/\r
16 /*                                                                                      */\r
17 /*                              Protocol Depend functions                               */\r
18 /*                                                                                      */\r
19 /*                                Public Function Calls                                 */\r
20 /*                              --------------------------                              */\r
21 /*              EPS_ERR_CODE prtFunctionCheck       (commMode, usbFuncPtrs, netFuncPtrs,        */\r
22 /*                                           cmnFuncPtrs                        );      */\r
23 /*      EPS_ERR_CODE prtFindPrinter         (timeout                                                    );      */\r
24 /*      void         prtCancelFindPrinter       (                                   );      */\r
25 /*      EPS_ERR_CODE prtProbePrinterByID        (probeParam                         );      */\r
26 /*      EPS_ERR_CODE prtProbePrinterByAddr      (probeParam                         );      */\r
27 /*      EPS_ERR_CODE prtRecoverPE           (                                   );      */\r
28 /*      EPS_ERR_CODE prtGetPMString         (printer, pString, bufSize          );      */\r
29 /*      EPS_ERR_CODE prtRegPrinter          (printer, bNotify                   );      */\r
30 /*      EPS_ERR_CODE prtIsRegistered        (Address, Protocol                  );      */\r
31 /*      EPS_ERR_CODE prtClearPrinterList    (printer                            );      */\r
32 /*      EPS_ERR_CODE prtClearSupportedMedia (printer                            );      */\r
33 /*      EPS_ERR_CODE prtSetIdStr            (printer, idString                  );      */\r
34 /*                                                                                      */\r
35 /*******************************************|********************************************/\r
36 \r
37 /*------------------------------------  Includes   -------------------------------------*/\r
38 /*******************************************|********************************************/\r
39 #include "epson-escpr-pvt.h"\r
40 #include "epson-escpr-err.h"\r
41 #include "epson-escpr-services.h"\r
42 #include "epson-escpr-mem.h"\r
43 #ifdef GCOMSW_CMD_ESCPAGE\r
44 #include "epson-escpage.h"\r
45 #endif\r
46 #ifdef GCOMSW_CMD_ESCPAGE_S\r
47 #include "epson-escpage-s.h"\r
48 #endif\r
49 \r
50 #include "epson-protocol.h"\r
51 \r
52 #ifdef GCOMSW_PRT_USE_USB\r
53 #include "epson-usb.h"\r
54 #endif\r
55 \r
56 #ifdef GCOMSW_PRT_USE_LPR\r
57 #include "epson-net-lpr.h"\r
58 #endif\r
59 \r
60 #ifdef GCOMSW_PRT_USE_RAW\r
61 #include "epson-net-raw.h"\r
62 #endif\r
63 \r
64 /*-----------------------------  Local Macro Definitions -------------------------------*/\r
65 /*******************************************|********************************************/\r
66 #ifdef EPS_LOG_MODULE_PRTCOL\r
67 #define EPS_LOG_MODULE  EPS_LOG_MODULE_PRTCOL\r
68 #else\r
69 #define EPS_LOG_MODULE  0\r
70 #endif\r
71 \r
72 /*------------------------------------  Definition   -----------------------------------*/\r
73 /*******************************************|********************************************/\r
74 /* Find Printer functions                                                               */\r
75 typedef EPS_ERR_CODE    (*NET_FindStart )(EPS_SOCKET*, const EPS_INT8*, EPS_BOOL        );\r
76 typedef EPS_ERR_CODE    (*NET_FindCheck )(EPS_SOCKET, EPS_PRINTER_INN**                 );\r
77 typedef EPS_ERR_CODE    (*NET_FindEnd   )(EPS_SOCKET                                    );\r
78 \r
79 typedef struct tagEPS_FIND_FUNCS {\r
80         NET_FindStart   fncStart;                       /* Start function                               */\r
81         NET_FindCheck   fncCheck;                       /* Check function                               */\r
82         NET_FindEnd             fncEnd;                         /* End function                                 */\r
83         EPS_SOCKET              sock;                           /* socekt                                       */\r
84         EPS_INT8                address[EPS_ADDR_BUFFSIZE];\r
85 }EPS_FIND_FUNCS;\r
86 \r
87 \r
88 #define EPS_FINDNOTIFY_DELAY_COUNT              (10)\r
89 \r
90 /*--------------------------  ESC/P-R Net Lib Global Variables  ------------------------*/\r
91 /*******************************************|********************************************/\r
92 \r
93         /*** Extern Function                                                                */\r
94         /*** -------------------------------------------------------------------------------*/\r
95 extern EPS_NET_FUNC    epsNetFnc;\r
96 extern EPS_CMN_FUNC    epsCmnFnc;\r
97 \r
98     /*** Print Job Structure                                                            */\r
99     /*** -------------------------------------------------------------------------------*/\r
100 extern EPS_PRINT_JOB   printJob;\r
101 \r
102         /*** Find                                                                           */\r
103         /*** -------------------------------------------------------------------------------*/\r
104 EPS_BOOL     g_FindBreak;                                                                       /* Find printer end flag    */\r
105 EPS_INT32    g_FindProtocol;                                                                    \r
106 \r
107 /*-------------------------  Module "Local Global" Variables  --------------------------*/\r
108 /*******************************************|********************************************/\r
109 \r
110     /*** internal stock                                                                 */\r
111     /*** -------------------------------------------------------------------------------*/\r
112 static EPS_PRINTER_LIST epsPrinterList;                         /* Printer List                                         */\r
113 \r
114 \r
115 \r
116 /*--------------------------------  Local Functions   ----------------------------------*/\r
117 /*******************************************|********************************************/\r
118 #ifdef GCOMSW_PRT_USE_NETWORK\r
119 static EPS_ERR_CODE FindNetPrinter      (EPS_INT32, EPS_UINT32, EPS_BOOL, const EPS_INT8*       );\r
120 #endif\r
121 static void MakePrinterStructure        (EPS_PRINTER* dst, EPS_PRINTER_INN* src                         );\r
122 static EPS_BOOL     IsValidAddress  (const EPS_INT8*                                    );\r
123 \r
124 \r
125 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
126 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
127 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/\r
128 /*--------------------              Public Functions               ---------------------*/\r
129 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/\r
130 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
131 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
132 \r
133 /*******************************************|********************************************/\r
134 /*                                                                                      */\r
135 /* Function name:     prtFunctionCheck()                                                */\r
136 /*                                                                                      */\r
137 /* Arguments                                                                            */\r
138 /* ---------                                                                            */\r
139 /* Name:        Type:               Description:                                        */\r
140 /* commMode     EPS_INT8            I: Communication Mode                               */\r
141 /*                                     (Bi-Directional or Uni-Directional)              */\r
142 /* usbFuncPtrs  EPS_USB_FUNC*       I: Data structure containing function pointers to   */\r
143 /*                                     USB I/O functions.                               */\r
144 /* netFuncPtrs  EPS_NET_FUNC*       I: Data structure containing function pointers to   */\r
145 /*                                     Socket I/O functions.                            */\r
146 /* cmnFuncPtrs  EPS_CMN_FUNC*       I: Data structure containing function pointers to   */\r
147 /*                                     external memory management functions.            */\r
148 /* Return value:                                                                        */\r
149 /*      << Normal >>                                                                    */\r
150 /*      EPS_ERR_NONE                     - Success                                      */\r
151 /*      << Error >>                                                                     */\r
152 /*      EPS_ERR_INV_ARG_COMMMODE         - Invalid argument "commMode"                  */\r
153 /*      EPS_ERR_INV_FNCP_NULL            - Invalid function pointer structure           */\r
154 /*      EPS_ERR_INV_FNCP_FINDCALLBACK    - Invalid function pointer "findCallback"      */\r
155 /*      EPS_ERR_INV_FNCP_MEMALLOC        - Invalid function pointer "memAlloc"          */\r
156 /*      EPS_ERR_INV_FNCP_MEMFREE         - Invalid function pointer "memFree"           */\r
157 /*      EPS_ERR_INV_FNCP_SLEEP           - Invalid function pointer "sleep"             */\r
158 /*      EPS_ERR_INV_FNCP_GETTIME         - Invalid function pointer "GetTime"           */\r
159 /*      EPS_ERR_INV_FNCP_OPENPORTAL      - Invalid function pointer "openPortal"        */\r
160 /*      EPS_ERR_INV_FNCP_CLOSEPORTAL     - Invalid function pointer "closePortal"       */\r
161 /*      EPS_ERR_INV_FNCP_READPORTAL      - Invalid function pointer "readPortal"        */\r
162 /*      EPS_ERR_INV_FNCP_WRITEPORTAL     - Invalid function pointer "writePortal"       */\r
163 /*      EPS_ERR_INV_FNCP_FINDFIRST       - Invalid function pointer "findFirst"         */\r
164 /*      EPS_ERR_INV_FNCP_FINDNEXT        - Invalid function pointer "findNext"          */\r
165 /*      EPS_ERR_INV_FNCP_FINDCLOSE       - Invalid function pointer "findClose"         */\r
166 /*      EPS_ERR_INV_FNCP_NETSOCKET       - Invalid function pointer "socket"            */\r
167 /*      EPS_ERR_INV_FNCP_NETCLOSE        - Invalid function pointer "close"             */\r
168 /*      EPS_ERR_INV_FNCP_NETCONNECT      - Invalid function pointer "connect"           */\r
169 /*      EPS_ERR_INV_FNCP_NETSHUTDOWN     - Invalid function pointer "shutdown"          */\r
170 /*      EPS_ERR_INV_FNCP_NETBIND         - Invalid function pointer "bind"              */\r
171 /*      EPS_ERR_INV_FNCP_NETLISTEN       - Invalid function pointer "listen"            */\r
172 /*      EPS_ERR_INV_FNCP_NETACCEPT       - Invalid function pointer "accept"            */\r
173 /*      EPS_ERR_INV_FNCP_NETSEND         - Invalid function pointer "send"              */\r
174 /*      EPS_ERR_INV_FNCP_NETSENDTO       - Invalid function pointer "sendTo"            */\r
175 /*      EPS_ERR_INV_FNCP_NETRECEIVE      - Invalid function pointer "receive"           */\r
176 /*      EPS_ERR_INV_FNCP_NETRECEIVEFROM  - Invalid function pointer "receiveFrom"       */\r
177 /*      EPS_ERR_INV_FNCP_NETGETSOCKNAME  - Invalid function pointer "getsockname"       */\r
178 /*      EPS_ERR_INV_FNCP_NETSETBROADCAST - Invalid function pointer "setBroadcast"      */\r
179 /*      EPS_ERR_MEMORY_ALLOCATION        - Failed to allocate memory                    */\r
180 /*                                                                                      */\r
181 /* Description:                                                                         */\r
182 /*      Validate external functions.                                                    */\r
183 /*                                                                                      */\r
184 /*******************************************|********************************************/\r
185 EPS_ERR_CODE    prtFunctionCheck (\r
186 \r
187         EPS_INT32               commMode, \r
188                 const EPS_USB_FUNC*         usbFuncPtrs,\r
189         const EPS_NET_FUNC*     netFuncPtrs,\r
190         const EPS_CMN_FUNC*         cmnFuncPtrs\r
191 \r
192 ){\r
193         EPS_UINT8*      testptr;                        /* Test pointer to do memory test   */\r
194     EPS_UINT32      validProtocol; \r
195 \r
196         EPS_LOG_FUNCIN\r
197 \r
198 /*** Validate communication mode                                                        */\r
199         if( (commMode & EPS_COMM_UNID) && (commMode & EPS_COMM_BID) ){\r
200                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
201         }\r
202         if( !(commMode & EPS_PROTOCOL_ALL) ){\r
203                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
204         }\r
205 \r
206         validProtocol = EPS_PROTOCOL_INVALID;\r
207 #ifdef GCOMSW_PRT_USE_USB\r
208         validProtocol |= EPS_PROTOCOL_USB;\r
209 #endif\r
210 #ifdef GCOMSW_PRT_USE_LPR\r
211         validProtocol |= EPS_PROTOCOL_LPR;\r
212 #endif\r
213 #ifdef GCOMSW_PRT_USE_RAW\r
214         validProtocol |= EPS_PROTOCOL_RAW;\r
215 #endif\r
216         if( EPS_PRT_PROTOCOL(commMode) & ~validProtocol ){\r
217                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
218         }\r
219 \r
220 /*** Validate External Functions                                                        */\r
221         if(cmnFuncPtrs == NULL){\r
222         EPS_RETURN( EPS_ERR_INV_FNCP_NULL )\r
223         }\r
224 \r
225         if (cmnFuncPtrs->memAlloc       == NULL){\r
226         EPS_RETURN( EPS_ERR_INV_FNCP_MEMALLOC )\r
227         }\r
228 \r
229         if (cmnFuncPtrs->memFree        == NULL){\r
230         EPS_RETURN( EPS_ERR_INV_FNCP_MEMFREE )\r
231         }\r
232 #ifdef GCOMSW_EPSON_SLEEP\r
233         if (cmnFuncPtrs->sleep          != NULL){\r
234 #else\r
235         if (cmnFuncPtrs->sleep          == NULL){\r
236 #endif\r
237         EPS_RETURN( EPS_ERR_INV_FNCP_SLEEP )\r
238         }\r
239 \r
240         if (cmnFuncPtrs->findCallback   == NULL){\r
241         EPS_RETURN( EPS_ERR_INV_FNCP_FINDCALLBACK )\r
242         }\r
243 \r
244         if (cmnFuncPtrs->getTime == NULL\r
245                 && ((cmnFuncPtrs->lockSync == NULL) || (cmnFuncPtrs->unlockSync == NULL)) ){\r
246         EPS_RETURN( EPS_ERR_INV_FNCP_GETTIME )\r
247         }\r
248 \r
249         /*** Check Memory Functions                                                         */\r
250     testptr = NULL;\r
251     testptr = (EPS_UINT8 *)cmnFuncPtrs->memAlloc(10);\r
252     if (testptr == NULL) {\r
253         EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION ) \r
254     }\r
255     cmnFuncPtrs->memFree(testptr);\r
256 \r
257 \r
258 /*** Validate USB Functions                                                            */\r
259 #ifdef GCOMSW_PRT_USE_USB\r
260         if (commMode & EPS_PROTOCOL_USB) {\r
261                 if (usbFuncPtrs == NULL){\r
262             EPS_RETURN( EPS_ERR_INV_FNCP_NULL )\r
263                 }\r
264 \r
265                 if (usbFuncPtrs->openPortal     == NULL){\r
266             EPS_RETURN( EPS_ERR_INV_FNCP_OPENPORTAL )\r
267                 }\r
268                 if (usbFuncPtrs->closePortal    == NULL){\r
269             EPS_RETURN( EPS_ERR_INV_FNCP_CLOSEPORTAL )\r
270                 }\r
271                 if (usbFuncPtrs->writePortal    == NULL){\r
272             EPS_RETURN( EPS_ERR_INV_FNCP_WRITEPORTAL )\r
273                 }\r
274 \r
275         if ( commMode & EPS_COMM_BID ) {\r
276                         if (usbFuncPtrs->readPortal    == NULL){\r
277                                 EPS_RETURN( EPS_ERR_INV_FNCP_READPORTAL )\r
278                         }\r
279                         if (usbFuncPtrs->findFirst     == NULL){\r
280                                 EPS_RETURN( EPS_ERR_INV_FNCP_FINDFIRST )\r
281                         }\r
282                         if (usbFuncPtrs->findNext     == NULL){\r
283                                 EPS_RETURN( EPS_ERR_INV_FNCP_FINDNEXT )\r
284                         }\r
285                         if (usbFuncPtrs->findClose     == NULL){\r
286                                 EPS_RETURN( EPS_ERR_INV_FNCP_FINDCLOSE )\r
287                         }\r
288 #if 0 /* not necessary */\r
289                         if (cmnFuncPtrs->stateCallback == NULL)\r
290                                 return (EPS_ERR_CODE)EPS_ERR_INV_FNCP_NOTIFYCALLBACK;\r
291 #endif\r
292                 }\r
293     }\r
294 #else\r
295         (void)usbFuncPtrs;\r
296 #endif\r
297 \r
298 \r
299 /*** Validate Net Functions                                                            */\r
300 #ifdef GCOMSW_PRT_USE_NETWORK\r
301         if( (commMode & EPS_PROTOCOL_NET) ){\r
302                 if (netFuncPtrs == NULL){\r
303             EPS_RETURN( EPS_ERR_INV_FNCP_NULL )\r
304                 }\r
305 \r
306                 if (netFuncPtrs->socket        == NULL){\r
307             EPS_RETURN( EPS_ERR_INV_FNCP_NETSOCKET )\r
308                 }\r
309                 if (netFuncPtrs->close         == NULL){\r
310             EPS_RETURN( EPS_ERR_INV_FNCP_NETCLOSE )\r
311                 }\r
312                 if (netFuncPtrs->connect       == NULL){\r
313             EPS_RETURN( EPS_ERR_INV_FNCP_NETCONNECT )\r
314                 }\r
315                 if (netFuncPtrs->shutdown      == NULL){\r
316             EPS_RETURN( EPS_ERR_INV_FNCP_NETSHUTDOWN )\r
317                 }\r
318                 if (netFuncPtrs->send          == NULL){\r
319             EPS_RETURN( EPS_ERR_INV_FNCP_NETSEND )\r
320                 }\r
321                 if (netFuncPtrs->sendTo        == NULL){\r
322             EPS_RETURN( EPS_ERR_INV_FNCP_NETSENDTO )\r
323                 }\r
324                 if (netFuncPtrs->receive       == NULL){\r
325             EPS_RETURN( EPS_ERR_INV_FNCP_NETRECEIVE )\r
326                 }\r
327                 if (netFuncPtrs->receiveFrom   == NULL){\r
328             EPS_RETURN( EPS_ERR_INV_FNCP_NETRECEIVEFROM )\r
329                 }\r
330 \r
331 #ifdef GCOMSW_PRT_USE_LPR\r
332                 if(commMode & EPS_PROTOCOL_LPR){\r
333                         if (netFuncPtrs->setBroadcast       == NULL){\r
334                                 EPS_RETURN( EPS_ERR_INV_FNCP_NETSETBROADCAST )\r
335                         }\r
336                 }\r
337 #endif\r
338 \r
339         }\r
340 #else\r
341         (void)netFuncPtrs;\r
342 #endif /* GCOMSW_PRT_USE_NETWORK */\r
343 \r
344 #ifdef GCOMSW_EPSON_SLEEP\r
345 /*** Initial parameters for internal (simple) timing routine                            */\r
346 /*** Used when system sleep routines are not available                                  */\r
347     if (epsCmnFnc.sleep == serSleep) {\r
348         tdx = sleepTime = 0;\r
349         ftime(&sleepS); \r
350         while (!sleepTime) {\r
351             ftime(&sleepE);\r
352             sleepTime  = 1000*(sleepE.time    - sleepS.time) +\r
353                               (sleepE.millitm - sleepS.millitm);\r
354             tdx++;\r
355         }\r
356         tdx *= 50;\r
357         for (idx = 1; idx < tdx; idx++) {ftime(&sleepE); } \r
358         sleepTime  = 1000*(sleepE.time    - sleepS.time) +\r
359                           (sleepE.millitm - sleepS.millitm);\r
360         printJob.sleepSteps = tdx/sleepTime;                /* Num steps for one  msec  */\r
361         if (!printJob.sleepSteps) \r
362             printJob.sleepSteps = -sleepTime/tdx;           /* Num  msec for each step  */\r
363     }\r
364 #endif /* GCOMSW_EPSON_SLEEP */\r
365 \r
366         EPS_RETURN( EPS_ERR_NONE )\r
367 }\r
368 \r
369 \r
370 /*******************************************|********************************************/\r
371 /*                                                                                      */\r
372 /* Function name:   prtFindPrinter()                                                    */\r
373 /*                                                                                      */\r
374 /* Arguments                                                                            */\r
375 /* ---------                                                                            */\r
376 /* Name:        Type:               Description:                                        */\r
377 /* protocol     EPS_INT32           I: traget protocol                                  */\r
378 /* timeout      EPS_UINT32          I: find timeout                                     */\r
379 /*                                                                                      */\r
380 /* Return value:                                                                        */\r
381 /*      EPS_ERR_NONE                                    - Success (Opened Communication)                */\r
382 /*      EPS_ERR_MEMORY_ALLOCATION               - Fail to memory allocation                     */\r
383 /*      EPS_ERR_PRINTER_NOT_FOUND               - Printer not found (or error occur)                */\r
384 /*      EPS_ERR_COMM_ERROR              - Communication Error                           */\r
385 /*                                                                                      */\r
386 /*      EPS_FIND_CANCELED           - user discontinued it                              */\r
387 /*                                                                                      */\r
388 /* Description:                                                                         */\r
389 /*     Find USB and network printers.                                                   */\r
390 /*                                                                                      */\r
391 /*******************************************|********************************************/\r
392 EPS_ERR_CODE prtFindPrinter (\r
393 \r
394                 EPS_INT32           protocol,\r
395         EPS_UINT32          timeout\r
396 \r
397 ){\r
398     EPS_ERR_CODE    ret = EPS_ERR_PRINTER_NOT_FOUND; /* Return status of internal calls */\r
399     EPS_INT32       initProtocol = EPS_PRT_PROTOCOL(printJob.commMode);\r
400     EPS_INT32       tgtProtocol = EPS_PRT_PROTOCOL(protocol);\r
401 \r
402         EPS_LOG_FUNCIN\r
403 \r
404         if( (tgtProtocol | initProtocol) != initProtocol){\r
405                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
406         }\r
407         g_FindProtocol = tgtProtocol;\r
408 \r
409         /* Reset break flag */\r
410         if( epsCmnFnc.lockSync && epsCmnFnc.unlockSync ){\r
411                 epsCmnFnc.lockSync();\r
412                 g_FindBreak = FALSE;\r
413                 epsCmnFnc.unlockSync();\r
414         }\r
415 \r
416 /***------------------------------------------------------------------------------------*/\r
417 /*** USB find                                                                           */\r
418 /***------------------------------------------------------------------------------------*/\r
419 #ifdef GCOMSW_PRT_USE_USB\r
420         if( protocol & EPS_PROTOCOL_USB ){\r
421 \r
422                 ret = usbFind( &timeout, printJob.commMode );\r
423 \r
424                 if( !(EPS_ERR_NONE == ret || EPS_ERR_PRINTER_NOT_FOUND == ret\r
425                         || EPS_ERR_NOT_OPEN_IO == ret) ){\r
426                         EPS_RETURN( ret )\r
427                 }\r
428         }\r
429 #endif\r
430 \r
431 /***------------------------------------------------------------------------------------*/\r
432 /*** Net find                                                                           */\r
433 /***------------------------------------------------------------------------------------*/\r
434 #ifdef GCOMSW_PRT_USE_NETWORK\r
435         if( protocol & EPS_PROTOCOL_NET ){\r
436 \r
437                 ret = FindNetPrinter( protocol, timeout, TRUE, NULL );\r
438 \r
439         }\r
440 #endif /* GCOMSW_PRT_USE_NETWORK */\r
441 \r
442         if( epsPrinterList.num <= 0 ){\r
443                 if(EPS_ERR_NONE == ret ||\r
444                    EPS_ERR_PRINTER_NOT_USEFUL== ret ){\r
445                         ret = EPS_ERR_PRINTER_NOT_FOUND;\r
446                 }\r
447         } else{\r
448                 if(EPS_ERR_PRINTER_NOT_FOUND == ret ||\r
449                    EPS_ERR_PRINTER_NOT_USEFUL== ret ){\r
450                         ret = EPS_ERR_NONE;\r
451                 }\r
452         }\r
453                 \r
454         EPS_RETURN( ret )\r
455 }\r
456 \r
457 \r
458 /*******************************************|********************************************/\r
459 /*                                                                                      */\r
460 /* Function name:   prtCancelFindPrinter()                                              */\r
461 /*                                                                                      */\r
462 /* Arguments                                                                            */\r
463 /* ---------                                                                            */\r
464 /* Name:        Type:               Description:                                        */\r
465 /* -                    void                -                                                   */\r
466 /*                                                                                      */\r
467 /* Return value:                                                                        */\r
468 /*      EPS_ERR_NONE               - Success                                            */\r
469 /*      EPS_ERR_INVALID_CALL       - invalid called                                     */\r
470 /*                                                                                      */\r
471 /* Description:                                                                         */\r
472 /*     Cancel epsFindPrinter(), epsProbePrinter().                                      */\r
473 /*                                                                                      */\r
474 /*******************************************|********************************************/\r
475 EPS_ERR_CODE prtCancelFindPrinter(\r
476 \r
477         void                                    \r
478 \r
479 ){\r
480         EPS_LOG_FUNCIN\r
481 \r
482         if( epsCmnFnc.lockSync && epsCmnFnc.unlockSync ){\r
483                 if( 0 == epsCmnFnc.lockSync() ){\r
484                         g_FindBreak = TRUE;\r
485                         epsCmnFnc.unlockSync();\r
486                 } \r
487                 EPS_RETURN( EPS_ERR_NONE )\r
488         } else{\r
489                 EPS_RETURN( EPS_ERR_INVALID_CALL )\r
490         }\r
491 }\r
492 \r
493 \r
494 /*******************************************|********************************************/\r
495 /*                                                                                      */\r
496 /* Function name:   prtProbePrinterByID()                                               */\r
497 /*                                                                                      */\r
498 /* Arguments                                                                            */\r
499 /* ---------                                                                            */\r
500 /* Name:        Type:               Description:                                        */\r
501 /* probeParam   EPS_PROBE*          Pointer to a Prober parameter structure             */\r
502 /*                                                                                      */\r
503 /* Return value:                                                                        */\r
504 /*      EPS_ERR_NONE                    - Success (Opened Communication)                */\r
505 /*      EPS_ERR_PRINTER_NOT_FOUND       - printer not found                             */\r
506 /*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */\r
507 /*      EPS_ERR_PROTOCOL_NOT_SUPPORTED  - Unsupported function Error                    */\r
508 /*                                                                                      */\r
509 /* Description:                                                                         */\r
510 /*     Probe printer by ID string.                                                      */\r
511 /*                                                                                      */\r
512 /*******************************************|********************************************/\r
513 EPS_ERR_CODE   prtProbePrinterByID (\r
514 \r
515                 const EPS_PROBE*   probeParam\r
516 \r
517 ){\r
518 /*** Declare Variable Local to Routine                                                  */\r
519     EPS_ERR_CODE                ret = EPS_ERR_NONE;          /* Return status of internal calls */\r
520     EPS_PRINTER_INN*    innerPrinter = NULL;\r
521         EPS_UINT16          protocol = EPS_PROTOCOL_INVALID;\r
522     EPS_INT8            sParse[EPS_PRNID_BUFFSIZE];\r
523     EPS_INT8*           pPos = NULL;\r
524     EPS_INT32           nSegCnt = 0;\r
525     EPS_UINT32          nTmp = 0;\r
526     EPS_INT8            defStr[EPS_PRNID_BUFFSIZE];\r
527 \r
528         EPS_LOG_FUNCIN\r
529 \r
530 /*** Initialize Local Variables                                                         */\r
531     ret = EPS_ERR_NONE;\r
532 \r
533 /*** Parse ID String                                                                    */\r
534         strcpy(sParse, probeParam->dsc.identify);\r
535 \r
536         pPos = strtok(sParse, EPS_PID_SEP);\r
537     for(nSegCnt = 0; pPos != NULL && nSegCnt < EPS_PID_SEGNUM; nSegCnt++){\r
538                 switch(nSegCnt){\r
539                 case 0:                 /* P */\r
540                         if( 0 != strcmp(pPos, EPS_PID_PRT) ){\r
541                                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ID )\r
542                         }\r
543                         break;\r
544 \r
545                 case 1:                 /* Get protocol number */\r
546                         sscanf(pPos, "%x", &nTmp);\r
547                         if(nTmp == 0){\r
548                                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ID )\r
549                         }\r
550                         protocol = (EPS_UINT16)nTmp;\r
551                         break;\r
552 \r
553                 case 2:                 /* D */\r
554                         if( 0 != strcmp(pPos, EPS_PID_DEF) ){\r
555                                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ID )\r
556                         }\r
557                         break;\r
558 \r
559                 case 3:                 /* Get description string */\r
560                         strcpy(defStr, pPos);\r
561                         break;\r
562                 }\r
563 \r
564                 pPos = strtok(NULL, EPS_PID_SEP);\r
565     }\r
566         if(nSegCnt < EPS_PID_SEGNUM){\r
567                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ID )\r
568         }\r
569 \r
570         /* Reset break flag */\r
571         if( epsCmnFnc.lockSync && epsCmnFnc.unlockSync ){\r
572                 epsCmnFnc.lockSync();\r
573                 g_FindBreak = FALSE;\r
574                 epsCmnFnc.unlockSync();\r
575         }\r
576 \r
577 /*** protocol depend probe                                                              */\r
578         switch( EPS_PRT_PROTOCOL( protocol ) ){\r
579 #ifdef GCOMSW_PRT_USE_USB\r
580         case EPS_PROTOCOL_USB:\r
581                 if( printJob.commMode & EPS_PROTOCOL_USB ){\r
582                         ret = usbProbePrinterByID(defStr, probeParam->timeout, &innerPrinter);\r
583                 } else{\r
584                         ret = EPS_ERR_PROTOCOL_NOT_SUPPORTED;\r
585                 }\r
586                 break;\r
587 #endif\r
588 \r
589 #ifdef GCOMSW_PRT_USE_LPR\r
590         case EPS_PROTOCOL_LPR:\r
591                 if( printJob.commMode & EPS_PROTOCOL_LPR ){\r
592                         ret = lprProbePrinterByID(defStr, probeParam->timeout, &innerPrinter);\r
593                 } else{\r
594                         ret = EPS_ERR_PROTOCOL_NOT_SUPPORTED;\r
595                 }\r
596                 break;\r
597 #endif\r
598 \r
599 #ifdef GCOMSW_PRT_USE_RAW\r
600         case EPS_PROTOCOL_RAW:\r
601                 if( printJob.commMode & EPS_PROTOCOL_RAW ){\r
602                         ret = rawProbePrinterByID(defStr, probeParam->timeout, &innerPrinter);\r
603                 } else{\r
604                         ret = EPS_ERR_PROTOCOL_NOT_SUPPORTED;\r
605                 }\r
606                 break;\r
607 #endif\r
608 \r
609         default:\r
610                 ret = EPS_ERR_INV_ARG_PRINTER_ID;\r
611         }\r
612 \r
613         if(EPS_ERR_NONE == ret){\r
614             innerPrinter->protocol = protocol | EPS_PRT_DIRECTION(printJob.commMode);\r
615                 ret = prtRegPrinter( innerPrinter, TRUE );\r
616                 if(EPS_FIND_CANCELED == ret){\r
617                         ret = EPS_ERR_NONE;\r
618                 }\r
619         }\r
620 \r
621         EPS_RETURN( ret )\r
622 }\r
623 \r
624 \r
625 /*******************************************|********************************************/\r
626 /*                                                                                      */\r
627 /* Function name:   prtProbePrinterByAddr()                                             */\r
628 /*                                                                                      */\r
629 /* Arguments                                                                            */\r
630 /* ---------                                                                            */\r
631 /* Name:        Type:               Description:                                        */\r
632 /* probeParam   EPS_PROBE*          Pointer to a Prober parameter structure             */\r
633 /*                                                                                      */\r
634 /* Return value:                                                                        */\r
635 /*      EPS_ERR_NONE                    - Success (Opened Communication)                */\r
636 /*      EPS_ERR_PRINTER_NOT_FOUND       - printer not found                             */\r
637 /*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */\r
638 /*      EPS_ERR_PROTOCOL_NOT_SUPPORTED  - Unsupported function Error                    */\r
639 /*      EPS_ERR_PRINTER_NOT_USEFUL      - received but not usefl                        */\r
640 /*                                                                                      */\r
641 /* Description:                                                                         */\r
642 /*     Probe network printer by IP Address.                                             */\r
643 /*                                                                                      */\r
644 /*******************************************|********************************************/\r
645 EPS_ERR_CODE   prtProbePrinterByAddr (\r
646 \r
647                 const EPS_PROBE*    probeParam\r
648 \r
649 ){\r
650 /*** Declare Variable Local to Routine                                                  */\r
651     EPS_ERR_CODE        ret = EPS_ERR_PRINTER_NOT_FOUND; /* Return status of internal calls */\r
652     EPS_INT32       initProtocol = EPS_PRT_PROTOCOL(printJob.commMode);\r
653     EPS_INT32       tgtProtocol = EPS_PROTOCOL_INVALID;\r
654 \r
655         EPS_LOG_FUNCIN\r
656 \r
657 /*** Validate input parameters                                                          */\r
658         if (NULL == probeParam) {\r
659                 EPS_RETURN( EPS_ERR_INV_ARG_PROBEINFO )\r
660         }\r
661         tgtProtocol = EPS_PRT_PROTOCOL(probeParam->dsc.addr.protocol);\r
662         if( (tgtProtocol | initProtocol) != initProtocol){\r
663                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
664         }\r
665         g_FindProtocol = tgtProtocol;\r
666 \r
667         /* Reset break flag */\r
668         if( epsCmnFnc.lockSync && epsCmnFnc.unlockSync ){\r
669                 epsCmnFnc.lockSync();\r
670                 g_FindBreak = FALSE;\r
671                 epsCmnFnc.unlockSync();\r
672         }\r
673 \r
674         if( IsValidAddress(probeParam->dsc.addr.address) ){\r
675 #if defined(GCOMSW_PRT_USE_NETWORK)\r
676                 /***--------------------------------------------------------------------------------*/\r
677                 /*** If IPAddress, Probe network printer                                            */\r
678                 /***--------------------------------------------------------------------------------*/\r
679                 if( tgtProtocol & EPS_PROTOCOL_NET ){\r
680                         ret = FindNetPrinter(tgtProtocol, probeParam->timeout, FALSE, probeParam->dsc.addr.address);\r
681                         if(EPS_FIND_CANCELED == ret){\r
682                                 ret = EPS_ERR_NONE;\r
683                         }\r
684                 } else{\r
685                         ret =  EPS_ERR_PROTOCOL_NOT_SUPPORTED;\r
686                 }\r
687 #else\r
688                 ret =  EPS_ERR_PROTOCOL_NOT_SUPPORTED;\r
689 #endif /* GCOMSW_PRT_USE_NETWORK */\r
690         } else{\r
691                 ret =  EPS_ERR_INV_ARG_PRINTER_ADDR;\r
692         }\r
693 \r
694         if( epsPrinterList.num <= 0 ){\r
695                 if(EPS_ERR_NONE == ret){\r
696                         ret = EPS_ERR_PRINTER_NOT_FOUND;\r
697                 }\r
698         } else{\r
699                 if(EPS_ERR_PRINTER_NOT_FOUND == ret ||\r
700                    EPS_ERR_PRINTER_NOT_USEFUL== ret ){\r
701                         ret = EPS_ERR_NONE;\r
702                 }\r
703         }\r
704 \r
705         EPS_RETURN( ret )\r
706 }\r
707 \r
708 \r
709 /*******************************************|********************************************/\r
710 /*                                                                                      */\r
711 /* Function name:     prtSetupJobFunctions()                                            */\r
712 /*                                                                                      */\r
713 /* Arguments                                                                            */\r
714 /* ---------                                                                            */\r
715 /* Name:            Type:                   Description:                                */\r
716 /* printer          const EPS_PRINTER*      I: Pointer to a target printer info.        */\r
717 /*                                                                                      */\r
718 /* Return value:                                                                        */\r
719 /*      << Normal >>                                                                    */\r
720 /*      EPS_ERR_NONE                    - Success                                       */\r
721 /*      EPS_ERR_OPR_FAIL                - Internal Error                                */\r
722 /*                                                                                      */\r
723 /* Description:                                                                         */\r
724 /*      construct job function table.                                                   */\r
725 /*                                                                                      */\r
726 /*******************************************|********************************************/\r
727 EPS_ERR_CODE prtSetupJobFunctions (\r
728                                                                    \r
729                 const EPS_PRINTER_INN*  printer, \r
730                 EPS_JOB_FUNCS*              pFuncs\r
731                 \r
732 ){\r
733 /*** Declare Variable Local to Routine                                                  */\r
734         EPS_ERR_CODE            ret = EPS_ERR_NONE;         /* Return status of internal calls  */\r
735 \r
736         EPS_LOG_FUNCIN\r
737 \r
738         switch( EPS_PRT_PROTOCOL( printer->protocol ) ){\r
739 #ifdef GCOMSW_PRT_USE_USB\r
740         case EPS_PROTOCOL_USB:\r
741                 pFuncs->StartJob        = &usbStartJob;\r
742                 pFuncs->EndJob          = &usbEndJob;\r
743                 pFuncs->RestartJob              = &usbRestartJob;\r
744                 pFuncs->WriteData       = &usbWritePrintData;\r
745                 pFuncs->ResetPrinter    = &usbResetPrinter;\r
746                 pFuncs->MonitorStatus   = &usbGetJobStatus;\r
747                 pFuncs->GetStatus               = &usbGetStatus;\r
748                 break;\r
749 #endif\r
750 \r
751 #ifdef GCOMSW_PRT_USE_LPR\r
752         case EPS_PROTOCOL_LPR:\r
753                 pFuncs->StartJob        = &lprStartJob;\r
754                 pFuncs->EndJob          = &lprEndJob;\r
755                 pFuncs->RestartJob              = &lprRestartJob;\r
756                 pFuncs->WriteData       = &lprWritePrintData;\r
757                 pFuncs->ResetPrinter    = &lprResetPrinter;\r
758                 pFuncs->StartPage       = &lprStartPage;\r
759                 pFuncs->EndPage         = &lprEndPage;\r
760                 pFuncs->MonitorStatus   = &lprGetJobStatus;\r
761                 pFuncs->GetStatus               = &lprGetStatus;\r
762                 lprSetupSTFunctions(printer);\r
763                 break;\r
764 #endif\r
765 \r
766 #ifdef GCOMSW_PRT_USE_RAW\r
767         case EPS_PROTOCOL_RAW:\r
768                 pFuncs->StartJob        = &rawStartJob;\r
769                 pFuncs->EndJob          = &rawEndJob;\r
770                 pFuncs->RestartJob              = &rawRestartJob;\r
771                 pFuncs->WriteData       = &rawWritePrintData;\r
772                 pFuncs->ResetPrinter    = &rawResetPrinter;\r
773                 pFuncs->StartPage       = &rawStartPage;\r
774                 pFuncs->EndPage         = &rawEndPage;\r
775                 pFuncs->MonitorStatus   = &rawGetJobStatus;\r
776                 pFuncs->GetStatus               = &rawGetStatus;\r
777                 rawSetupSTFunctions(printer);\r
778                 break;\r
779 #endif\r
780 \r
781         default:\r
782                 ret = EPS_ERR_OPR_FAIL;\r
783         }\r
784 \r
785         EPS_RETURN( ret )\r
786 }\r
787 \r
788 \r
789 /*******************************************|********************************************/\r
790 /*                                                                                      */\r
791 /* Function name:   prtGetInkStatus()                                                   */\r
792 /*                                                                                      */\r
793 /* Arguments                                                                            */\r
794 /* ---------                                                                            */\r
795 /* Name:        Type:               Description:                                        */\r
796 /* status       EPS_STATUS*         Pointer to the printer status.                      */\r
797 /*                                                                                      */\r
798 /* Return value:                                                                        */\r
799 /*      EPS_ERR_NONE                    - Success                                       */\r
800 /*      EPS_ERR_NEED_BIDIRECT           - Need Bi-Directional Communication             */\r
801 /*      EPS_ERR_COMM_ERROR              - Failed to send command                        */\r
802 /*      EPS_ERR_NOT_OPEN_IO             - Cannot Open I/O Portal                        */\r
803 /*      EPS_ERR_NOT_CLOSE_IO            - Cannot Close I/O Portal                       */\r
804 /*      EPS_ERR_PROTOCOL_NOT_SUPPORTED  - Unsupported function Error                    */\r
805 /*                                                                                      */\r
806 /* Description:                                                                         */\r
807 /*      Gets the Ink Infomation.                                                        */\r
808 /*                                                                                      */\r
809 /*******************************************|********************************************/\r
810 EPS_ERR_CODE    prtGetInkInfo (\r
811 \r
812         const EPS_PRINTER_INN*  printer,\r
813                 EPS_INK_INFO*                   info\r
814 \r
815 ){\r
816 /*** Declare Variable Local to Routine                                                  */\r
817         EPS_ERR_CODE    ret = EPS_ERR_NONE;         /* Return status of internal calls      */\r
818         EPS_STATUS_INFO lclStatus;\r
819         EPS_INT32       idx;\r
820 \r
821         EPS_LOG_FUNCIN\r
822 \r
823 /*** Validate communication mode                                                        */\r
824         if ( !EPS_IS_BI_PROTOCOL(printer->protocol) ){\r
825         EPS_RETURN( EPS_ERR_NEED_BIDIRECT )\r
826         }\r
827 \r
828         memset(&lclStatus, 0, sizeof(lclStatus));\r
829 /*** protocol GetStatus                                                                 */\r
830         switch( EPS_PRT_PROTOCOL( printer->protocol ) ){\r
831 #ifdef GCOMSW_PRT_USE_USB\r
832         case EPS_PROTOCOL_USB:\r
833                 ret = usbGetStatus(&lclStatus, NULL, NULL);\r
834                 break;\r
835 #endif\r
836 \r
837 #ifdef GCOMSW_PRT_USE_LPR\r
838         case EPS_PROTOCOL_LPR:\r
839                 ret = lprGetInkInfo(&lclStatus);\r
840                 break;\r
841 #endif\r
842 \r
843 #ifdef GCOMSW_PRT_USE_RAW\r
844         case EPS_PROTOCOL_RAW:\r
845                 ret = rawGetInkInfo(&lclStatus);\r
846                 break;\r
847 #endif\r
848 \r
849         default:\r
850                 ret = EPS_ERR_OPR_FAIL;\r
851         }\r
852 \r
853         if(EPS_ERR_NONE == ret\r
854                 && EPS_SUBPROTOCOL_PCDEV != EPS_PRT_SUBPROTOCOL(printer->protocol) ){\r
855                 info->number         = lclStatus.nInkNo;\r
856                 for(idx=0; idx < EPS_INK_NUM; idx++) {\r
857                         info->colors[idx]    = lclStatus.nColorType[idx];\r
858                         info->remaining[idx] = lclStatus.nColor[idx];\r
859                 }\r
860         }\r
861 \r
862         EPS_RETURN( ret )\r
863 }\r
864 \r
865 \r
866 /*******************************************|********************************************/\r
867 /*                                                                                      */\r
868 /* Function name:   prtGetPMString()                                                    */\r
869 /*                                                                                      */\r
870 /* Arguments                                                                            */\r
871 /* ---------                                                                            */\r
872 /* Name:            Type:                   Description:                                */\r
873 /* printer          EPS_PRINTER_INN*        I: Pointer to a PrinterInfo                 */\r
874 /* type                         EPS_INT32                               I: PM kind 1 or 2                           */\r
875 /* pString                      EPS_UINT8*                              O: Pointer to PM String                     */\r
876 /* bufSize                      EPS_INT32                               I: pString buffer size                      */\r
877 /*                                                                                      */\r
878 /* Return value:                                                                        */\r
879 /*      EPS_ERR_NONE                    - Success                                       */\r
880 /*      EPS_ERR_OPR_FAIL                - Internal Error                                */\r
881 /*      EPS_ERR_MEMORY_ALLOCATION       - Failed to allocate memory                     */\r
882 /*      EPS_ERR_COMM_ERROR              - Communication Error                           */\r
883 /*      EPS_ERR_PROTOCOL_NOT_SUPPORTED  - Unsupported function Error                    */\r
884 /*                                                                                      */\r
885 /* Description:                                                                         */\r
886 /*                                                                                      */\r
887 /*******************************************|********************************************/\r
888 EPS_ERR_CODE     prtGetPMString (\r
889 \r
890         const EPS_PRINTER_INN*  printer,\r
891                 EPS_INT32               type,\r
892         EPS_UINT8*              pString,\r
893                 EPS_INT32*              bufSize\r
894 \r
895 ){\r
896 /*** Declare Variable Local to Routine                                                  */\r
897         EPS_ERR_CODE     ret = EPS_ERR_NONE;           /* Return status of internal calls   */\r
898 \r
899         EPS_LOG_FUNCIN\r
900 \r
901         switch( EPS_PRT_PROTOCOL( printer->protocol ) ){\r
902 #ifdef GCOMSW_PRT_USE_USB\r
903         case EPS_PROTOCOL_USB:\r
904                 ret = usbGetPMString(printer, type, pString, bufSize);\r
905                 break;\r
906 #endif\r
907 \r
908 #ifdef GCOMSW_PRT_USE_LPR\r
909         case EPS_PROTOCOL_LPR:\r
910                 ret = lprGetPMString(printer, type, pString, bufSize);\r
911                 break;\r
912 #endif\r
913 \r
914 #ifdef GCOMSW_PRT_USE_RAW\r
915         case EPS_PROTOCOL_RAW:\r
916                 ret = rawGetPMString(printer, type, pString, bufSize);\r
917                 break;\r
918 #endif\r
919 \r
920         default:\r
921                 ret = EPS_ERR_OPR_FAIL;\r
922         }\r
923 \r
924         EPS_RETURN( ret )\r
925 }\r
926 \r
927 \r
928 /*******************************************|********************************************/\r
929 /*                                                                                      */\r
930 /* Function name:   prtRegPrinter()                                                     */\r
931 /*                                                                                      */\r
932 /* Arguments                                                                            */\r
933 /* ---------                                                                            */\r
934 /* Name:        Type:               Description:                                        */\r
935 /* printer      EPS_PRINTER*        I:Data structure containing printer information.    */\r
936 /* bNotify      EPS_BOOL            I:This item need notify.                            */\r
937 /*                                                                                      */\r
938 /* Return value:                                                                        */\r
939 /*      EPS_ERR_NONE                            - Printer settled into one of the requested state(s)*/\r
940 /*      EPS_ERR_OPR_FAIL                        - Internal Error                                    */\r
941 /*      EPS_ERR_MEMORY_ALLOCATION   - Failed to allocate memory                         */\r
942 /*      EPS_FIND_CANCELED           - cancel find by user                               */\r
943 /*                                                                                      */\r
944 /* Description:                                                                         */\r
945 /*      Registers a new printer with the global linked list managed by this module.             */\r
946 /*              And Notify to user.                                                             */\r
947 /*                                                                                      */\r
948 /*******************************************|********************************************/\r
949 EPS_ERR_CODE prtRegPrinter(\r
950                                                    \r
951                 EPS_PRINTER_INN* printer,\r
952                 EPS_BOOL                 bNotify\r
953 \r
954 ){\r
955 /*** Declare Variables Local to Routine */\r
956         EPS_PRINTER             usrPrinter;                                             /* for user notify                  */\r
957         EPS_PL_NODE*    current = NULL;\r
958         EPS_BOOL                registered = FALSE;\r
959 \r
960         EPS_LOG_FUNCIN\r
961 \r
962         EPS_DBGPRINT( ("%X / %s\n", printer->protocol, printer->location) )\r
963 \r
964         if (printer->protocol == EPS_PROTOCOL_INVALID){\r
965                 EPS_RETURN( EPS_ERR_OPR_FAIL )\r
966         }\r
967 \r
968         if( NULL == epsPrinterList.root){\r
969         /*** This is the first printer in the list.                                                                             */\r
970                 epsPrinterList.root = (EPS_PL_NODE*)EPS_ALLOC(sizeof(EPS_PL_NODE));\r
971                 if(epsPrinterList.root == NULL) {\r
972                         EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )\r
973                 }\r
974                 epsPrinterList.num = 1;\r
975 \r
976                 current = epsPrinterList.root;\r
977                 current->printer = printer;\r
978                 current->next = NULL;\r
979 \r
980         } else {\r
981 \r
982                 current = epsPrinterList.root;\r
983                 while( current ){\r
984                         /* If we've found a printer in the list that matches the module's printer,              */\r
985                         if( 0 == strcmp(printer->location, current->printer->location)){\r
986                                 if(0 == strcmp(printer->modelName, current->printer->modelName)){\r
987                                         registered = TRUE;\r
988                                 }\r
989                         }\r
990 \r
991                         if (registered ){\r
992                                 \r
993                                 /* And the new protocol has priority over the old one...                                        */\r
994                                 if (EPS_PRT_PROTOCOL(current->printer->protocol) > EPS_PRT_PROTOCOL(printer->protocol) ) {\r
995                                         /* Prioritize the protocols. The lower-numbered protocol always                 */\r
996                                         /* has precendence over the higher-numbered one (1 outranks 3).                 */\r
997                                         current->printer->protocol = printer->protocol;\r
998 \r
999                                         /* Replase protocol */\r
1000                                         EPS_DBGPRINT( ("Discover %d %s %s (Upgrade)\n", printer->protocol, printer->modelName, printer->printerID) )\r
1001                                         EPS_SAFE_RELEASE( current->printer->protocolInfo );\r
1002                                         EPS_SAFE_RELEASE( current->printer );\r
1003                                         current->printer = printer;\r
1004                                         break;\r
1005                                 } else{\r
1006                                         /* In this case, the current protocol already has priority. Simply return.      */\r
1007                                         EPS_SAFE_RELEASE( printer->protocolInfo );\r
1008                                         EPS_SAFE_RELEASE( printer );\r
1009                                         EPS_RETURN( EPS_ERR_NONE )\r
1010                                 }\r
1011                         }\r
1012 \r
1013                         if( NULL == current->next){\r
1014                                 break;                  \r
1015                         }\r
1016                         current = current->next;\r
1017                 }\r
1018 \r
1019                 /* If we've reached this part of the code, it means we haven't found the printer.       */\r
1020                 /* Time to add a new node to the list.                                                                                          */\r
1021                 /* The variable "current" is right now pointing to the last member of the list.         */\r
1022                 if( !registered ){\r
1023                         /* Allocate a new printer on the end of the linked list.                                                        */\r
1024                         current->next = (EPS_PL_NODE*)EPS_ALLOC(sizeof(EPS_PL_NODE));\r
1025                         if(current->next == NULL) {\r
1026                                 EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )\r
1027                         }\r
1028                         \r
1029                         /* Fill in the list node.                                                                                                       */\r
1030                         current->next->printer = printer;\r
1031                         current->next->next = NULL;\r
1032                         epsPrinterList.num++;\r
1033 \r
1034                         current = current->next;\r
1035                 }\r
1036         }\r
1037 \r
1038         EPS_DBGPRINT( ("Discover x%X (x%X) %s %s\n", printer->protocol, printer->supportFunc, printer->modelName, printer->printerID) )\r
1039 \r
1040         if( TRUE == bNotify ){\r
1041                 MakePrinterStructure(&usrPrinter, printer);\r
1042 \r
1043                 if( EPS_USER_CANCEL == epsCmnFnc.findCallback(usrPrinter) ){\r
1044                         EPS_RETURN( EPS_FIND_CANCELED )\r
1045                 }\r
1046         }\r
1047 \r
1048         EPS_RETURN( EPS_ERR_NONE )\r
1049 }\r
1050 \r
1051 \r
1052 /*******************************************|********************************************/\r
1053 /*                                                                                      */\r
1054 /* Function name:   prtIsRegistered()                                                   */\r
1055 /*                                                                                      */\r
1056 /* Arguments                                                                            */\r
1057 /* ---------                                                                            */\r
1058 /* Name:        Type:               Description:                                        */\r
1059 /* Address      EPS_INT8*           IP Address.                                                                                 */\r
1060 /* modelName    EPS_INT8*           Printer model name.                                                                 */\r
1061 /* Protocol     EPS_INT32           Protocol ID.                                                                                */\r
1062 /*                                                                                      */\r
1063 /* Return value:                                                                        */\r
1064 /* TRUE (registered), FALSE(not registered).                                                                                    */\r
1065 /*                                                                                      */\r
1066 /* Description:                                                                         */\r
1067 /*      Whether the printer has registered is confirmed.                                                                */\r
1068 /*                                                                                      */\r
1069 /*******************************************|********************************************/\r
1070 EPS_BOOL prtIsRegistered(\r
1071                                                  \r
1072                 const EPS_INT8 *Address, \r
1073                 const EPS_INT8 *modelName,\r
1074                 EPS_INT32 Protocol\r
1075                 \r
1076 ){\r
1077         EPS_PL_NODE* current = epsPrinterList.root;\r
1078         EPS_BOOL         bMatch = FALSE;\r
1079 \r
1080         EPS_LOG_FUNCIN\r
1081 \r
1082         while(NULL != current) {\r
1083                 /* If we've found a printer in the list that matches the module's printer,              */\r
1084                 bMatch = FALSE;\r
1085                 if( 0 == strcmp(current->printer->location, Address) ){\r
1086                         if(NULL != modelName){\r
1087                                 if( 0 == strcmp(current->printer->modelName, modelName) ){\r
1088                                         bMatch = TRUE;\r
1089                                 }\r
1090                         } else{\r
1091                                 bMatch = TRUE;\r
1092                         }\r
1093                 }\r
1094 \r
1095                 if(TRUE == bMatch){\r
1096                         /* And the new protocol has priority over the old one...                                        */\r
1097                         if (EPS_PRT_PROTOCOL(current->printer->protocol) <= EPS_PRT_PROTOCOL(Protocol)) {\r
1098                                 EPS_RETURN( TRUE )\r
1099                         } else{\r
1100                                 /* already registered However it's low level protocol                                   */\r
1101                                 EPS_RETURN( FALSE )\r
1102                         }\r
1103                 }\r
1104 \r
1105                 current = current->next;\r
1106         } \r
1107 \r
1108         EPS_RETURN( FALSE )\r
1109 }\r
1110 \r
1111 \r
1112 /*******************************************|********************************************/\r
1113 /*                                                                                      */\r
1114 /* Function name:   prtAddUsrPrinter()                                                  */\r
1115 /*                                                                                      */\r
1116 /* Arguments                                                                            */\r
1117 /* ---------                                                                            */\r
1118 /* Name:        Type:               Description:                                        */\r
1119 /* usrPrinter   EPS_PRINTER*        I: User specified printer.                                                  */\r
1120 /* printer      EPS_PRINTER*        O: Internal printer information.                                    */\r
1121 /*                                                                                      */\r
1122 /* Return value:                                                                        */\r
1123 /*      EPS_ERR_NONE                    - Success                                       */\r
1124 /*      EPS_ERR_INV_ARG_COMMMODE        - Invalid argument "usrPrinter.protocol"        */\r
1125 /*      EPS_ERR_INV_ARG_PRINTER_ADDR    - Invalid format " printer.location"            */\r
1126 /*      EPS_ERR_MEMORY_ALLOCATION       - Fail to memory allocation                     */\r
1127 /*      EPS_ERR_INV_PRINT_LANGUAGE      - Invalid argument "usrPrinter.language"        */\r
1128 /*                                                                                      */\r
1129 /* Description:                                                                         */\r
1130 /*      Register a user specified printer.                                                              */\r
1131 /*                                                                                      */\r
1132 /*******************************************|********************************************/\r
1133 EPS_ERR_CODE prtAddUsrPrinter(\r
1134                         \r
1135                 const EPS_PRINTER*  usrPrinter,\r
1136                 EPS_PRINTER_INN**       printer\r
1137 \r
1138 ){\r
1139     EPS_ERR_CODE                ret = EPS_ERR_NONE;\r
1140     EPS_INT32                   initProtocol = EPS_PRT_PROTOCOL(printJob.commMode);\r
1141     EPS_INT32                   tgtProtocol = EPS_PRT_PROTOCOL(usrPrinter->protocol);\r
1142         EPS_INT8                        address[EPS_ADDR_BUFFSIZE];\r
1143         EPS_INT8*                       p = NULL;\r
1144         EPS_INT32                       nPort = 0;\r
1145         EPS_PRINTER_INN*        innerPrinter = NULL;\r
1146 \r
1147         EPS_LOG_FUNCIN\r
1148 \r
1149         *printer = NULL;\r
1150 \r
1151         /*** Validate input parameters */\r
1152         if( (tgtProtocol | initProtocol) != initProtocol){      /* Not Init protocol */\r
1153                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
1154         }\r
1155         if( tgtProtocol & EPS_PROTOCOL_USB ){                           /* USB */\r
1156                 \r
1157                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
1158         }\r
1159         if( memGetBitCount(tgtProtocol) > 1 ){                          /* Multi protocol */\r
1160                 EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
1161         }\r
1162         if( '\0' == usrPrinter->location[0] ){\r
1163                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ADDR )\r
1164         }\r
1165         if( EPS_ADDR_BUFFSIZE < strlen(usrPrinter->location) ){\r
1166                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ADDR )\r
1167         }\r
1168 \r
1169         if( !(EPS_LANG_ESCPR                    == usrPrinter->language ||\r
1170 #ifdef GCOMSW_CMD_ESCPAGE_S\r
1171                   EPS_LANG_ESCPAGE_S            == usrPrinter->language ||\r
1172 #endif\r
1173                   EPS_LANG_ESCPAGE                      == usrPrinter->language ||\r
1174                   EPS_LANG_ESCPAGE_COLOR        == usrPrinter->language  ) ){\r
1175                 EPS_RETURN( EPS_ERR_INV_PRINT_LANGUAGE );\r
1176         }\r
1177 \r
1178         /*** pase location */\r
1179         strcpy(address, usrPrinter->location);\r
1180         p = strchr(address, ':');\r
1181         if( NULL != p ){\r
1182                 *p = '\0';\r
1183                 p++;                                    /* 1 = ':' */\r
1184                 sscanf(p, "%d", &nPort);\r
1185         } else{\r
1186                 /* Default Port */\r
1187                 if( tgtProtocol & EPS_PROTOCOL_LPR ){\r
1188 #ifdef GCOMSW_PRT_USE_LPR\r
1189                         nPort = lprGetDefautiPort();\r
1190 #else\r
1191                         EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
1192 #endif\r
1193                 } else if( tgtProtocol & EPS_PROTOCOL_RAW ){\r
1194 #ifdef GCOMSW_PRT_USE_RAW\r
1195                         nPort = rawGetDefautiPort();\r
1196 #else\r
1197                         EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
1198 #endif\r
1199                 } else{\r
1200                         EPS_RETURN( EPS_ERR_INV_ARG_COMMMODE )\r
1201                 }\r
1202         }\r
1203         if( FALSE == IsValidAddress( address ) ){\r
1204                 EPS_RETURN( EPS_ERR_INV_ARG_PRINTER_ADDR )\r
1205         }\r
1206 \r
1207         /*** Create new printer */\r
1208         innerPrinter = (EPS_PRINTER_INN*)EPS_ALLOC(sizeof(EPS_PRINTER_INN));\r
1209         if( NULL == innerPrinter ){\r
1210                 EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )\r
1211         }\r
1212 \r
1213         memset(innerPrinter, 0x00, sizeof(EPS_PRINTER_INN));\r
1214 \r
1215         strcpy(innerPrinter->location, address);\r
1216         innerPrinter->printPort         = (EPS_UINT16)nPort;\r
1217         innerPrinter->protocol          = usrPrinter->protocol;\r
1218         innerPrinter->supportFunc       = EPS_SPF_RGBPRINT | EPS_SPF_JPGPRINT;\r
1219     innerPrinter->JpgMax                = EPS_JPEG_SIZE_UNLIMIT;\r
1220         innerPrinter->language          = usrPrinter->language;\r
1221         strcpy(innerPrinter->modelName, usrPrinter->modelName);\r
1222         /* innerPrinter->manufacturerName */\r
1223         /* innerPrinter->friendlyName */\r
1224         /* innerPrinter->printerID; */\r
1225         /* innerPrinter->protocolInfo; */\r
1226 \r
1227         /*** Append to printer lsit */\r
1228         ret = prtRegPrinter(innerPrinter, FALSE);\r
1229         if( EPS_ERR_NONE == ret ){\r
1230                 *printer = innerPrinter;\r
1231         }\r
1232 \r
1233         EPS_RETURN( ret )\r
1234 }\r
1235 \r
1236 \r
1237 /*******************************************|********************************************/\r
1238 /*                                                                                      */\r
1239 /* Function name:     prtGetInnerPrinter()                                              */\r
1240 /*                                                                                      */\r
1241 /* Arguments                                                                            */\r
1242 /* ---------                                                                            */\r
1243 /* Name:            Type:                   Description:                                */\r
1244 /* printer          EPS_PRINTER             Pointer to public infomation structure      */\r
1245 /*                                                                                      */\r
1246 /* Return value:                                                                        */\r
1247 /* EPS_PRINTER_INN                          Pointer to private infomation structure     */\r
1248 /*                                                                                      */\r
1249 /* Description:                                                                         */\r
1250 /*      Get private infomation of printer.                                              */\r
1251 /*                                                                                      */\r
1252 /*******************************************|********************************************/\r
1253 EPS_PRINTER_INN* prtGetInnerPrinter(\r
1254                         \r
1255                 const EPS_PRINTER*  printer\r
1256 \r
1257 ){\r
1258     EPS_PL_NODE*                pCur = epsPrinterList.root;\r
1259         EPS_PRINTER_INN*        innerPrinter = NULL;\r
1260 \r
1261     /* collates it with found printer */\r
1262         while(NULL != pCur){\r
1263                 if(0 == strcmp(printer->modelName, pCur->printer->modelName)\r
1264                         &&  0 == strcmp(printer->location, pCur->printer->location) ){\r
1265                         innerPrinter = pCur->printer;\r
1266                         break;\r
1267                 }\r
1268                 pCur = pCur->next;\r
1269         } \r
1270 \r
1271         return innerPrinter;\r
1272 }\r
1273 \r
1274 \r
1275 /*******************************************|********************************************/\r
1276 /*                                                                                      */\r
1277 /* Function name:     prtClearPrinterList()                                             */\r
1278 /*                                                                                      */\r
1279 /* Arguments                                                                            */\r
1280 /* ---------                                                                            */\r
1281 /* Name:            Type:                   Description:                                */\r
1282 /* void                                                                                 */\r
1283 /*                                                                                      */\r
1284 /* Return value:                                                                        */\r
1285 /*      void                                                                            */\r
1286 /*                                                                                      */\r
1287 /* Description:                                                                         */\r
1288 /*      Crean up inside list of printer structure .                                     */\r
1289 /*                                                                                      */\r
1290 /*******************************************|********************************************/\r
1291 void prtClearPrinterList(\r
1292                                                  \r
1293                 void\r
1294                 \r
1295 ){\r
1296         EPS_PL_NODE* pCur = epsPrinterList.root;\r
1297         EPS_PL_NODE* pNext = NULL;\r
1298 \r
1299         EPS_LOG_FUNCIN\r
1300 \r
1301         /* Clear stock printer list */\r
1302         while(NULL != pCur){\r
1303                 pNext = pCur->next;\r
1304 \r
1305                 prtClearSupportedMedia(pCur->printer);\r
1306 \r
1307                 EPS_SAFE_RELEASE( pCur->printer->protocolInfo );\r
1308                 EPS_SAFE_RELEASE( pCur->printer );\r
1309                 EPS_SAFE_RELEASE( pCur );\r
1310 \r
1311                 pCur = pNext;\r
1312         } \r
1313 \r
1314         epsPrinterList.num = 0;\r
1315         epsPrinterList.root = NULL;\r
1316 \r
1317         EPS_RETURN_VOID\r
1318 }\r
1319 \r
1320 \r
1321 /*******************************************|********************************************/\r
1322 /*                                                                                      */\r
1323 /* Function name:     prtClearSupportedMedia()                                          */\r
1324 /*                                                                                      */\r
1325 /* Arguments                                                                            */\r
1326 /* ---------                                                                            */\r
1327 /* Name:            Type:                   Description:                                */\r
1328 /* printer          EPS_PRINTER_INN*            I: Pointer to a printer info structure      */\r
1329 /*                                                                                      */\r
1330 /* Return value:                                                                        */\r
1331 /*      void                                                                            */\r
1332 /*                                                                                      */\r
1333 /* Description:                                                                         */\r
1334 /*      Crean up inside list of supported media structure.                              */\r
1335 /*                                                                                      */\r
1336 /*******************************************|********************************************/\r
1337 void prtClearSupportedMedia(\r
1338                                                         \r
1339         EPS_PRINTER_INN*  printer\r
1340 \r
1341 ){\r
1342     EPS_INT32       idx;\r
1343 \r
1344         EPS_LOG_FUNCIN\r
1345 \r
1346     /* Clear "supportedMedia"                                              */\r
1347         if( NULL == printer ){\r
1348                 return;\r
1349         }\r
1350 \r
1351 #ifdef GCOMSW_CMD_ESCPAGE\r
1352         if(EPS_LANG_ESCPR == printer->language ){\r
1353 #endif\r
1354                 /*** ESC/P-R ***/\r
1355                 if( NULL != printer->supportedMedia.sizeList ){\r
1356                         for(idx = 0; idx < printer->supportedMedia.numSizes; idx++) {\r
1357                                 EPS_SAFE_RELEASE(printer->supportedMedia.sizeList[idx].typeList);\r
1358                         }\r
1359                         EPS_SAFE_RELEASE(printer->supportedMedia.sizeList);\r
1360                 }\r
1361 #ifdef GCOMSW_CMD_ESCPAGE\r
1362         } else{\r
1363                 /*** ESC/Page ***/\r
1364                 pageClearSupportedMedia(printer);\r
1365         }\r
1366 #endif\r
1367 \r
1368         printer->supportedMedia.numSizes = 0;\r
1369         printer->supportedMedia.JpegSizeLimit = EPS_JPEG_SIZE_MAX;  /* lowest guarantee */\r
1370         printer->supportedMedia.resolution = EPS_IR_360X360;\r
1371 \r
1372         EPS_RETURN_VOID\r
1373 }\r
1374 \r
1375 \r
1376 /*******************************************|********************************************/\r
1377 /*                                                                                      */\r
1378 /* Function name:     prtSetIdStr()                                                     */\r
1379 /*                                                                                      */\r
1380 /* Arguments                                                                            */\r
1381 /* ---------                                                                            */\r
1382 /* Name:            Type:                   Description:                                */\r
1383 /* printer          EPS_PRINTER_INN*            I/O: Pointer to a printer info structure    */\r
1384 /* idString         EPS_INT8*                           I: protocol depended unique string          */\r
1385 /*                                                                                      */\r
1386 /* Return value:                                                                        */\r
1387 /*      void                                                                            */\r
1388 /*                                                                                      */\r
1389 /* Description:                                                                         */\r
1390 /*      Set the Printer ID.                                                             */\r
1391 /*                                                                                      */\r
1392 /*******************************************|********************************************/\r
1393 void prtSetIdStr(\r
1394                                  \r
1395                         EPS_PRINTER_INN* printer, \r
1396                         const EPS_INT8* idString\r
1397                         \r
1398 ){\r
1399         sprintf(printer->printerID, EPS_PRINTER_ID_STR, \r
1400         EPS_PRT_PROTOCOL_EX(printer->protocol), idString);\r
1401 \r
1402 }\r
1403 \r
1404 \r
1405 /*******************************************|********************************************/\r
1406 /*                                                                                      */\r
1407 /* Function name:   prtRecoverPE()                                                      */\r
1408 /*                                                                                      */\r
1409 /* Arguments                                                                            */\r
1410 /* ---------                                                                            */\r
1411 /* Name:        Type:               Description:                                        */\r
1412 /* void         -                   -                                                   */\r
1413 /*                                                                                      */\r
1414 /* Return value:                                                                        */\r
1415 /*      EPS_ERR_NONE                    - Recovered successfully                        */\r
1416 /*      EPS_ERR_COMM_ERROR              - Recovery failed                               */\r
1417 /*                                                                                      */\r
1418 /* Description:                                                                         */\r
1419 /*      Tries to recover from the paper end error                                       */\r
1420 /*                                                                                      */\r
1421 /*******************************************|********************************************/\r
1422 EPS_ERR_CODE    prtRecoverPE (\r
1423 \r
1424         void\r
1425 \r
1426 ){\r
1427     EPS_ERR_CODE ret;\r
1428     \r
1429         EPS_LOG_FUNCIN\r
1430 \r
1431         switch( EPS_PRT_PROTOCOL( printJob.printer->protocol ) ){\r
1432 #ifdef GCOMSW_PRT_USE_USB\r
1433         case EPS_PROTOCOL_USB:\r
1434                 ret = usbMechCommand(EPS_CBTCOM_PE);\r
1435                 break;\r
1436 #endif\r
1437 \r
1438 #ifdef GCOMSW_PRT_USE_LPR\r
1439         case EPS_PROTOCOL_LPR:\r
1440                 ret = lprMechCommand(printJob.printer, EPS_CBTCOM_PE);\r
1441                 break;\r
1442 #endif\r
1443 \r
1444 #ifdef GCOMSW_PRT_USE_RAW\r
1445         case EPS_PROTOCOL_RAW:\r
1446                 ret = rawMechCommand(printJob.printer, EPS_CBTCOM_PE);\r
1447                 break;\r
1448 #endif\r
1449         default:\r
1450                 EPS_RETURN( EPS_ERR_NONE )       /* Other protocol Not Support recover command */\r
1451         }\r
1452 \r
1453     EPS_RETURN( ret )\r
1454 }\r
1455 \r
1456 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
1457 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
1458 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/\r
1459 /*--------------------               Local Functions               ---------------------*/\r
1460 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/\r
1461 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
1462 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
1463 #ifdef GCOMSW_PRT_USE_NETWORK\r
1464 /*******************************************|********************************************/\r
1465 /*                                                                                      */\r
1466 /* Function name:   FindNetPrinter()                                                    */\r
1467 /*                                                                                      */\r
1468 /* Arguments                                                                            */\r
1469 /* ---------                                                                            */\r
1470 /* Name:        Type:               Description:                                        */\r
1471 /* protocol     EPS_INT32           I: traget protocol                                  */\r
1472 /* timeout      EPS_UINT32          I: find timeout                                     */\r
1473 /* plural       EPS_BOOL            I: TRUE= find plural printer / FALSE= one printer   */\r
1474 /* address      EPS_INT8*           I: send discover message address.                   */\r
1475 /*                                     (If plural=TRUE, this is NULL)                   */\r
1476 /*                                                                                      */\r
1477 /* Return value:                                                                        */\r
1478 /*      EPS_ERR_NONE                - Success (Opened Communication)                    */\r
1479 /*      EPS_ERR_MEMORY_ALLOCATION   - Fail to memory allocation                         */\r
1480 /*      EPS_ERR_PRINTER_NOT_FOUND       - Printer not found (or error occur)                    */\r
1481 /*      EPS_ERR_COMM_ERROR          - socket error                                      */\r
1482 /*                                                                                      */\r
1483 /*      EPS_FIND_CANCELED           - user discontinued it                              */\r
1484 /*                                                                                      */\r
1485 /* Description:                                                                         */\r
1486 /*     Find network printers.                                                           */\r
1487 /*                                                                                      */\r
1488 /*******************************************|********************************************/\r
1489 static EPS_ERR_CODE FindNetPrinter (\r
1490 \r
1491                 EPS_INT32           protocol, \r
1492         EPS_UINT32          timeout,\r
1493         EPS_BOOL            plural,\r
1494                 const EPS_INT8*         address\r
1495 \r
1496 ){\r
1497     EPS_ERR_CODE    ret = EPS_ERR_PRINTER_NOT_FOUND; /* Return status of internal calls */\r
1498         EPS_PRINTER_INN*        innerPrinter = NULL;\r
1499 \r
1500         EPS_FIND_FUNCS* pProtocolFncs = NULL;\r
1501         EPS_FIND_FUNCS* pFncs = NULL;\r
1502         EPS_UINT32              tmStart, tmNow, tmSpan, tmReq;\r
1503         EPS_INT32               nProtocolCnt = 0;\r
1504         EPS_INT32               nCnt = 0;\r
1505         EPS_BOOL                bBreak = FALSE;\r
1506 \r
1507         EPS_LOG_FUNCIN\r
1508 \r
1509         if(epsCmnFnc.getTime){\r
1510                 tmStart = epsCmnFnc.getTime();\r
1511                 tmNow = tmSpan = tmReq = 0;\r
1512         } else{\r
1513                 timeout = tmStart = tmNow = tmSpan = tmReq = 0;\r
1514         }\r
1515 \r
1516 #ifdef GCOMSW_PRT_USE_LPR\r
1517         if(protocol & EPS_PROTOCOL_LPR) nProtocolCnt++;\r
1518 #endif\r
1519 #ifdef GCOMSW_PRT_USE_RAW\r
1520         if( (protocol & EPS_PROTOCOL_RAW) && !(protocol & EPS_PROTOCOL_LPR) ) nProtocolCnt++;\r
1521 #endif\r
1522         if(0 == nProtocolCnt){\r
1523                 EPS_RETURN( EPS_ERR_PRINTER_NOT_FOUND )\r
1524         }\r
1525 \r
1526         /* Setup net Find functions */\r
1527         pProtocolFncs = (EPS_FIND_FUNCS*)EPS_ALLOC(sizeof(EPS_FIND_FUNCS)*nProtocolCnt);\r
1528     if(NULL == pProtocolFncs){\r
1529         EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION )\r
1530     }\r
1531 \r
1532         pFncs = pProtocolFncs;\r
1533 \r
1534 #ifdef GCOMSW_PRT_USE_LPR\r
1535         if(protocol & EPS_PROTOCOL_LPR){\r
1536                 pFncs->fncStart = &lprFindStart;\r
1537                 pFncs->fncCheck = &lprFind;\r
1538                 pFncs->fncEnd = &lprFindEnd;\r
1539                 pFncs->sock = EPS_INVALID_SOCKET;\r
1540                 if(plural){\r
1541                         strcpy(pFncs->address, EPSNET_UDP_BROADCAST_ADDR);\r
1542                 } else{\r
1543                         strcpy(pFncs->address, address);\r
1544                 }\r
1545                 pFncs++;\r
1546         }\r
1547 #endif\r
1548 #ifdef GCOMSW_PRT_USE_RAW\r
1549         if( (protocol & EPS_PROTOCOL_RAW) && !(protocol & EPS_PROTOCOL_LPR) ){\r
1550                 pFncs->fncStart = &rawFindStart;\r
1551                 pFncs->fncCheck = &rawFind;\r
1552                 pFncs->fncEnd = &rawFindEnd;\r
1553                 pFncs->sock = EPS_INVALID_SOCKET;\r
1554                 if(plural){\r
1555                         strcpy(pFncs->address, EPSNET_UDP_BROADCAST_ADDR);\r
1556                 } else{\r
1557                         strcpy(pFncs->address, address);\r
1558                 }\r
1559         }\r
1560 #endif\r
1561 \r
1562         /* Start (send discover message) */\r
1563         pFncs = pProtocolFncs;\r
1564         ret = EPS_ERR_NONE;\r
1565         for(nCnt = 0; (nCnt < nProtocolCnt) && (ret == EPS_ERR_NONE); nCnt++, pFncs++){\r
1566                 ret = pFncs->fncStart( &(pFncs->sock), pFncs->address, plural );\r
1567         }\r
1568 \r
1569         /* Check response */\r
1570         if(     EPS_ERR_NONE == ret){\r
1571                 ret = EPS_ERR_PRINTER_NOT_FOUND;\r
1572         }\r
1573         while( (EPS_ERR_PRINTER_NOT_FOUND == ret || EPS_ERR_PRINTER_NOT_USEFUL == ret) && !bBreak ){\r
1574                 pFncs = pProtocolFncs;\r
1575                 for(nCnt = 0; nCnt < nProtocolCnt; ){\r
1576                         innerPrinter = NULL;\r
1577                         ret = pFncs->fncCheck(pFncs->sock, &innerPrinter);\r
1578                         \r
1579                         if( EPS_COM_NOT_RECEIVE == ret ){\r
1580                                 ret = EPS_ERR_PRINTER_NOT_FOUND;\r
1581                         } else{\r
1582                                 tmReq = 0;\r
1583                         }\r
1584 \r
1585                         if( EPS_ERR_NONE == ret ){\r
1586                             innerPrinter->protocol |= EPS_PRT_DIRECTION(printJob.commMode);\r
1587                                 \r
1588                                 ret = prtRegPrinter( innerPrinter, TRUE );\r
1589 \r
1590                                 if(EPS_FIND_CANCELED == ret){\r
1591                                         break;\r
1592                                 } else if(FALSE == plural){\r
1593                                         /* find one printer */\r
1594                                         bBreak = TRUE;\r
1595                                         break;\r
1596                                 }\r
1597 \r
1598                         } else if(EPS_ERR_PRINTER_NOT_FOUND == ret ){\r
1599                                 /* next protocol */\r
1600                                 nCnt++;\r
1601                                 pFncs++;\r
1602                         } else if(EPS_ERR_PRINTER_NOT_USEFUL == ret ){\r
1603                                 if(FALSE == plural){\r
1604                                         /* find one printer */\r
1605                                         bBreak = TRUE;\r
1606                                         break;\r
1607                                 }\r
1608                                 /* next protocol */\r
1609                                 nCnt++;\r
1610                                 pFncs++;\r
1611                         } else{\r
1612                                 bBreak = TRUE;\r
1613                                 break;\r
1614                         }\r
1615                 }\r
1616                 if(bBreak)break;\r
1617 \r
1618                 /* epsCancelFindPriter() */\r
1619                 if( epsCmnFnc.lockSync && epsCmnFnc.unlockSync ){\r
1620                         if( 0 == epsCmnFnc.lockSync() ){\r
1621                                 if( g_FindBreak ){\r
1622                                         epsCmnFnc.unlockSync();\r
1623                                         bBreak = TRUE;\r
1624                                         break;\r
1625                                 }\r
1626                                 epsCmnFnc.unlockSync();\r
1627                         }\r
1628                 }\r
1629 \r
1630                 /* Timeout */\r
1631                 if(timeout > 0){\r
1632                         tmNow = epsCmnFnc.getTime();\r
1633                         tmSpan = (EPS_UINT32)(tmNow - tmStart);\r
1634                         /*EPS_DBGPRINT( ("TM %u - %u <> %u\n", tmNow, tmStart, tmSpan) )*/\r
1635                         if( tmSpan >= timeout ){\r
1636                                 bBreak = TRUE;\r
1637                                 break;\r
1638                         }\r
1639                 }\r
1640 \r
1641                 /* re isuue request */\r
1642                 /*EPS_DBGPRINT( ("TM %u - %u <> %u\n", tmNow, tmStart, tmSpan) )*/\r
1643                 if( (EPS_ERR_PRINTER_NOT_FOUND == ret ||\r
1644                          EPS_ERR_PRINTER_NOT_USEFUL == ret) && epsCmnFnc.getTime )\r
1645                 {\r
1646                         tmNow = epsCmnFnc.getTime();\r
1647                         if( 0 == tmReq ){\r
1648                                 tmReq = tmNow;\r
1649                         } else{\r
1650                                 /* beef up */\r
1651                                 if( EPSNET_FIND_REREQUEST_TIME <= (EPS_UINT32)(tmNow - tmReq) ){\r
1652                                         EPS_DBGPRINT( ("beef up TM %u - %u <> %u\n", tmNow, tmReq, (EPS_UINT32)(tmNow - tmReq)) )\r
1653                                         pFncs = pProtocolFncs;\r
1654                                         ret = EPS_ERR_NONE;\r
1655                                         for(nCnt = 0; (nCnt < nProtocolCnt) && (ret == EPS_ERR_NONE); nCnt++, pFncs++){\r
1656                                                 ret = pFncs->fncStart( &(pFncs->sock), pFncs->address, plural );\r
1657                                         }\r
1658                                         tmReq = 0;\r
1659                                         if(     EPS_ERR_NONE == ret){\r
1660                                                 ret = EPS_ERR_PRINTER_NOT_FOUND;\r
1661                                         }\r
1662                                 }\r
1663 \r
1664                         }\r
1665                 }\r
1666         }\r
1667 \r
1668         /* End */\r
1669         pFncs = pProtocolFncs;\r
1670         for(nCnt = 0; (nCnt < nProtocolCnt); nCnt++, pFncs++){\r
1671                 pFncs->fncEnd( pFncs->sock );\r
1672         }\r
1673 \r
1674         EPS_SAFE_RELEASE(pProtocolFncs);\r
1675 \r
1676         EPS_RETURN( ret )\r
1677 }\r
1678 \r
1679 #endif  /* GCOMSW_PRT_USE_NETWORK */\r
1680 \r
1681 \r
1682 /*******************************************|********************************************/\r
1683 /*                                                                                      */\r
1684 /* Function name:     MakePrinterStructure()                                            */\r
1685 /*                                                                                      */\r
1686 /* Arguments                                                                            */\r
1687 /* ---------                                                                            */\r
1688 /* Name:        Type:               Description:                                        */\r
1689 /* dst          EPS_PRINTER*        O: Printer information                              */\r
1690 /* src          EPS_PRINTER_INN*    I: Printer information internal                     */\r
1691 /*                                                                                      */\r
1692 /* Return value:                                                                        */\r
1693 /*      void                                                                            */\r
1694 /*                                                                                      */\r
1695 /* Description:                                                                         */\r
1696 /*      Copy printer information.                                                       */\r
1697 /*                                                                                      */\r
1698 /*******************************************|********************************************/\r
1699 static void MakePrinterStructure(\r
1700                 \r
1701                 EPS_PRINTER* dst,\r
1702                 EPS_PRINTER_INN* src\r
1703 \r
1704 ){\r
1705         EPS_DBGPRINT(("%s : %d\n", src->modelName, src->protocol))\r
1706         dst->protocol                                   = EPS_PRT_PROTOCOL(src->protocol);\r
1707         dst->supportFunc                                = src->supportFunc;\r
1708         dst->language                                   = src->language;\r
1709         strcpy(dst->location,                   src->location);\r
1710         strcpy(dst->macAddress,                 src->macAddress);\r
1711         strcpy(dst->manufacturerName,   src->manufacturerName);\r
1712         strcpy(dst->modelName,                  src->modelName);\r
1713         strcpy(dst->friendlyName,               src->friendlyName);\r
1714         strcpy(dst->printerID,                  src->printerID);\r
1715 }\r
1716 \r
1717 \r
1718 /*******************************************|********************************************/\r
1719 /*                                                                                      */\r
1720 /* Function name:     IsValidAddress()                                                      */\r
1721 /*                                                                                      */\r
1722 /* Arguments                                                                            */\r
1723 /* ---------                                                                            */\r
1724 /* Name:        Type:               Description:                                        */\r
1725 /* addr         EPS_INT8*           I: IP Address                                       */\r
1726 /*                                                                                      */\r
1727 /* Return value:                                                                        */\r
1728 /*      TRUE                        - valid                                             */\r
1729 /*      FALSE                       - invalid                                                   */\r
1730 /*                                                                                      */\r
1731 /* Description:                                                                         */\r
1732 /*      Validation of IP(v4) Address.                                                   */\r
1733 /*                                                                                      */\r
1734 /*******************************************|********************************************/\r
1735 static EPS_BOOL IsValidAddress(\r
1736                                            \r
1737                 const EPS_INT8* addr\r
1738                 \r
1739 ){\r
1740 #define EPS_IPSEGNUM   (4)\r
1741 \r
1742         EPS_UINT32 nSeg[EPS_IPSEGNUM];\r
1743         EPS_INT16  nCnt;\r
1744 \r
1745         memset(nSeg, 0xFF, sizeof(EPS_UINT32)*EPS_IPSEGNUM);\r
1746 \r
1747         sscanf(addr, "%d.%d.%d.%d", &nSeg[0], &nSeg[1], &nSeg[2], &nSeg[3]);\r
1748 \r
1749         for(nCnt = 0; nCnt < EPS_IPSEGNUM; nCnt++){\r
1750                 if(nSeg[nCnt] > 255){\r
1751                         return FALSE;\r
1752                 }\r
1753         }\r
1754 \r
1755         return TRUE;\r
1756 }\r
1757 \r
1758 /*________________________________  epson-protocol.c   _________________________________*/\r
1759 \r
1760 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/\r
1761 /*       1         2         3         4         5         6         7         8        */\r
1762 /*******************************************|********************************************/\r
1763 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
1764 /***** End of File *** End of File *** End of File *** End of File *** End of File ******/\r
1765 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
1766 \r