Tizen 2.1 base
[platform/upstream/hplip.git] / io / hpmud / hpmudi.h
1 /*****************************************************************************\
2
3   hpmudi.h - internal definitions for multi-point transport driver 
4  
5   (c) 2004-2007 Copyright Hewlett-Packard Development Company, LP
6
7   Permission is hereby granted, free of charge, to any person obtaining a copy 
8   of this software and associated documentation files (the "Software"), to deal 
9   in the Software without restriction, including without limitation the rights 
10   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
11   of the Software, and to permit persons to whom the Software is furnished to do 
12   so, subject to the following conditions:
13
14   The above copyright notice and this permission notice shall be included in all
15   copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
18   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
19   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
20   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
21   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24   Author: Naga Samrat Chowdary Narla,
25 \*****************************************************************************/
26
27 #ifndef _HPMUDI_H
28 #define _HPMUDI_H
29
30 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE
32 #endif
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
37 #include <sys/time.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <syslog.h>
43 #include <time.h>
44 #include <ctype.h>
45 #include <pthread.h>
46 #include <arpa/inet.h>
47 #include "hpmud.h"
48 #include "musb.h"
49 #include "mlc.h"
50 #include "dot4.h"
51 #include "pml.h"
52 #ifdef HAVE_LIBNETSNMP
53 #include "jd.h"
54 #endif
55 #ifdef HAVE_PPORT
56 #include "pp.h"
57 #endif
58
59 // Don DO NOT commit with HPMUD_DEBUG enabled :( 
60 //#define HPMUD_DEBUG
61
62 #define _STRINGIZE(x) #x
63 #define STRINGIZE(x) _STRINGIZE(x)
64
65 #define BUG(args...) syslog(LOG_ERR, __FILE__ " " STRINGIZE(__LINE__) ": " args)
66 //#define BUG(args...) fprintf(stderr, __FILE__ " " STRINGIZE(__LINE__) ": " args)
67
68 #ifdef HPMUD_DEBUG
69    #define DBG(args...) syslog(LOG_INFO, __FILE__ " " STRINGIZE(__LINE__) ": " args)
70 // #define DBG(args...) fprintf(stderr, __FILE__ " " STRINGIZE(__LINE__) ": " args)
71    #define DBG_DUMP(data, size) sysdump((data), (size))
72    #define DBG_SZ(args...) syslog(LOG_INFO, args)
73 #else
74    #define DBG(args...)
75    #define DBG_DUMP(data, size)
76    #define DBG_SZ(args...)
77 #endif
78
79 #define HEX2INT(x, i) if (x >= '0' && x <= '9')      i |= x - '0'; \
80                        else if (x >= 'A' && x <= 'F') i |= 0xA + x - 'A'; \
81                        else if (x >= 'a' && x <= 'f') i |= 0xA + x - 'a'
82
83 /* offset_of returns the number of bytes that the fieldname MEMBER is offset from the beginning of the structure TYPE */
84 #define offset_of(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
85
86 #define HPMUD_EXCEPTION_TIMEOUT 45000000  /* microseconds */
87 #define HPMUD_EXCEPTION_SEC_TIMEOUT 45  /* seconds */
88 #define HPMUD_MDNS_TIMEOUT 10  /* seconds */
89
90 #define NFAULT_BIT  0x08
91 #define PERROR_BIT  0x20
92
93 enum HPMUD_CHANNEL_ID
94 {
95    HPMUD_PML_CHANNEL = 1,
96    HPMUD_PRINT_CHANNEL = 2,
97    HPMUD_SCAN_CHANNEL = 4,
98    HPMUD_FAX_SEND_CHANNEL = 7,
99    HPMUD_CONFIG_UPLOAD_CHANNEL = 0xe,
100    HPMUD_CONFIG_DOWNLOAD_CHANNEL = 0xf,
101    HPMUD_MEMORY_CARD_CHANNEL = 0x11,
102    HPMUD_EWS_CHANNEL = 0x12,          /* Embeded Web Server interface ff/1/1, any unused socket id */
103    HPMUD_SOAPSCAN_CHANNEL = 0x13,          /* Soap Scan interface ff/2/1, any unused socket id */
104    HPMUD_SOAPFAX_CHANNEL = 0x14,          /* Soap Fax interface ff/3/1, any unused socket id */
105    HPMUD_MARVELL_SCAN_CHANNEL = 0x15,    /* Marvell scan interface ff/ff/ff, any unused socket id */
106    HPMUD_MARVELL_FAX_CHANNEL = 0x16,    /* Marvell fax interface ff/ff/ff, any unused socket id */
107    HPMUD_EWS_LEDM_CHANNEL = 0x17,     /* Embeded Web Server interface ff/4/1, any unused socket id */
108    HPMUD_LEDM_SCAN_CHANNEL = 0x18,  /* LEDM scan interface ff/cc/0, any unused socket id */
109    HPMUD_WIFI_CHANNEL = 0x2b,      /* WIFI config */
110    HPMUD_DEVMGMT_CHANNEL = 0x2c,      /* decimal 44 */
111    HPMUD_MAX_CHANNEL_ID
112 };
113
114 #define HPMUD_DEVICE_MAX 2      /* zero is not used */
115 #define HPMUD_CHANNEL_MAX HPMUD_MAX_CHANNEL_ID
116
117 /* MLC/1284.4 attributes. Note for MLC, attributes must remain persistant while transport is up. */
118 typedef struct
119 {
120    unsigned short h2pcredit;   /* host to peripheral credit (dot4: primary socket id credit for sending) */
121    unsigned short p2hcredit;  /* peripheral to host credit (dot4: secondary socket id credit for sending) */   
122    unsigned short h2psize;  /* host to peripheral packet size in bytes (dot4: primary max packet size for sending) */
123    unsigned short p2hsize;  /* peripheral to host packet size in bytes (dot4: secondary max packet size for sending) */
124 } transport_attributes;
125
126 typedef struct _mud_channel_vf
127 {
128    enum HPMUD_RESULT (*open)(struct _mud_channel *pc);                                        /* transport specific open */
129    enum HPMUD_RESULT (*close)(struct _mud_channel *pc);                                       /* transport specific close */
130    enum HPMUD_RESULT (*channel_write)(struct _mud_channel *pc, const void *buf, int size, int timeout, int *bytes_wrote);  /* tranport specific write */
131    enum HPMUD_RESULT (*channel_read)(struct _mud_channel *pc, void *buf, int size, int timeout, int *bytes_read);   /* transport specific read */
132 } mud_channel_vf;
133
134 typedef struct _mud_device_vf
135 {
136    int (*write)(int fd, const void *buf, int size, int usec_timeout);                     /* low level device write */
137    int (*read)(int fd, void *buf, int size, int usec_timout);           /* low level device read */
138    enum HPMUD_RESULT (*open)(struct _mud_device *pd);                                        /* device specific open */
139    enum HPMUD_RESULT (*close)(struct _mud_device *pd);                                       /* device specific close */
140    enum HPMUD_RESULT (*get_device_id)(struct _mud_device *pd, char *id, int size, int *bytes_read);                      /* IEEE 1284 device id string */
141    enum HPMUD_RESULT (*get_device_status)(struct _mud_device *pd, unsigned int *status);                     /* device 8-bit status */
142    enum HPMUD_RESULT (*channel_open)(struct _mud_device *pd, const char *channel_name, HPMUD_CHANNEL *cd);                        /* channel specific open */
143    enum HPMUD_RESULT (*channel_close)(struct _mud_device *pd, struct _mud_channel *pc);                                     /* channel specific close */
144    enum HPMUD_RESULT (*channel_write)(struct _mud_device *pd, struct _mud_channel *pc, const void *buf, int size, int sec_timeout, int *bytes_wrote);
145    enum HPMUD_RESULT (*channel_read)(struct _mud_device *pd, struct _mud_channel *pc, void *buf, int size, int sec_timeout, int *bytes_read);   
146 } mud_device_vf;
147
148 typedef struct _mud_channel
149 {
150    char sn[HPMUD_LINE_SIZE];         /* service name */
151    unsigned char sockid;       /* socket id */
152    int client_cnt;             /* number of clients using this channel */
153    int index;                  /* channel[index] of this object */
154    int fd;                     /* file descriptor for this channel */
155    pid_t pid;                  /* process owner */
156    int dindex;                 /* device[dindex] parent device */ 
157
158    /* MLC/1284.4 specific variables. */
159    transport_attributes ta;
160    unsigned char rbuf[HPMUD_BUFFER_SIZE];  /* read packet buffer */
161    int rindex;
162    int rcnt;
163
164    /* JetDirect specific data. */
165    int socket;
166
167    mud_channel_vf vf;
168 } mud_channel;
169
170 typedef struct _mud_device
171 {
172    char uri[HPMUD_LINE_SIZE];
173    char id[1024];                    /* device id */
174    int index;                        /* device[index] of this object */
175    enum HPMUD_IO_MODE io_mode;
176    mud_channel channel[HPMUD_CHANNEL_MAX];
177    int channel_cnt;                  /* number of open channels */
178    int open_fd;                      /* file descriptor used by device_open */
179
180    /* MLC/1284.4 specific variables. */
181    int mlc_up;                       /* 0=transport down, 1=transport up */
182    int mlc_fd;                       /* file descriptor used by 1284.4/MLC transport */
183
184    /* JetDirect specific data. */
185    char ip[HPMUD_LINE_SIZE];              /* internet address */
186    int port;
187
188    mud_device_vf vf;                 /* virtual function table */
189    pthread_mutex_t mutex;  
190 } mud_device;
191
192 typedef struct
193 {
194    mud_device device[HPMUD_DEVICE_MAX];
195    pthread_mutex_t mutex;  
196 } mud_session;
197
198 extern mud_session *msp __attribute__ ((visibility ("hidden")));
199
200 void __attribute__ ((visibility ("hidden"))) sysdump(const void *data, int size);
201 int __attribute__ ((visibility ("hidden"))) mm_device_lock(int fd, HPMUD_DEVICE index);
202 int __attribute__ ((visibility ("hidden"))) mm_device_unlock(int fd, HPMUD_DEVICE index);
203 int __attribute__ ((visibility ("hidden"))) mm_device_trylock(int fd, HPMUD_DEVICE index);
204 int __attribute__ ((visibility ("hidden"))) is_hp(const char *id);
205 int  __attribute__ ((visibility ("hidden"))) generalize_model(const char *sz, char *buf, int bufSize);
206 int  __attribute__ ((visibility ("hidden"))) generalize_serial(const char *sz, char *buf, int bufSize);
207 int __attribute__ ((visibility ("hidden"))) get_uri_model(const char *uri, char *buf, int bufSize);
208 int __attribute__ ((visibility ("hidden"))) get_uri_serial(const char *uri, char *buf, int bufSize);
209 enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) service_to_channel(mud_device *pd, const char *sn, HPMUD_CHANNEL *index);
210
211 #endif // _HPMUDI_H
212