1 /*****************************************************************************\
2 
3   hpmudi.h - internal definitions for multi-point transport driver
4 
5   (c) 2004-2007 Copyright HP 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 <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mman.h>
38 #include <sys/time.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <syslog.h>
44 #include <time.h>
45 #include <ctype.h>
46 #include <pthread.h>
47 #include <arpa/inet.h>
48 #include "hpmud.h"
49 #include "musb.h"
50 #include "mlc.h"
51 #include "dot4.h"
52 #include "pml.h"
53 #ifdef HAVE_LIBNETSNMP
54 #include "jd.h"
55 #include "mdns.h"
56 #endif
57 #ifdef HAVE_PPORT
58 #include "pp.h"
59 #endif
60 
61 // DO NOT commit with HPMUD_DEBUG enabled :(
62 //#define HPMUD_DEBUG
63 
64 #define _STRINGIZE(x) #x
65 #define STRINGIZE(x) _STRINGIZE(x)
66 
67 #define BUG(args...) syslog(LOG_ERR, __FILE__ " " STRINGIZE(__LINE__) ": " args)
68 //#define BUG(args...) fprintf(stderr, __FILE__ " " STRINGIZE(__LINE__) ": " args)
69 
70 #ifdef HPMUD_DEBUG
71    #define DBG(args...) syslog(LOG_INFO, __FILE__ " " STRINGIZE(__LINE__) ": " args)
72 // #define DBG(args...) fprintf(stderr, __FILE__ " " STRINGIZE(__LINE__) ": " args)
73    #define DBG_DUMP(data, size) sysdump((data), (size))
74    #define DBG_SZ(args...) syslog(LOG_INFO, args)
75 #else
76    #define DBG(args...)
77    #define DBG_DUMP(data, size)
78    #define DBG_SZ(args...)
79 #endif
80 
81 #define HEX2INT(x, i) if (x >= '0' && x <= '9')      i |= x - '0'; \
82                        else if (x >= 'A' && x <= 'F') i |= 0xA + x - 'A'; \
83                        else if (x >= 'a' && x <= 'f') i |= 0xA + x - 'a'
84 
85 /* offset_of returns the number of bytes that the fieldname MEMBER is offset from the beginning of the structure TYPE */
86 #define offset_of(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
87 
88 #define HPMUD_EXCEPTION_TIMEOUT 45000000  /* microseconds */
89 #define HPMUD_EXCEPTION_SEC_TIMEOUT 45  /* seconds */
90 #define HPMUD_MDNS_TIMEOUT 10  /* seconds */
91 
92 #define NFAULT_BIT  0x08
93 #define PERROR_BIT  0x20
94 
95 enum HPMUD_CHANNEL_ID
96 {
97    HPMUD_PML_CHANNEL = 1,
98    HPMUD_PRINT_CHANNEL = 2,
99    HPMUD_SCAN_CHANNEL = 4,
100    HPMUD_FAX_SEND_CHANNEL = 7,
101    HPMUD_CONFIG_UPLOAD_CHANNEL = 0xe,
102    HPMUD_CONFIG_DOWNLOAD_CHANNEL = 0xf,
103    HPMUD_MEMORY_CARD_CHANNEL = 0x11,
104    HPMUD_EWS_CHANNEL = 0x12,          /* Embeded Web Server interface ff/1/1, any unused socket id */
105    HPMUD_SOAPSCAN_CHANNEL = 0x13,          /* Soap Scan interface ff/2/1, any unused socket id */
106    HPMUD_SOAPFAX_CHANNEL = 0x14,          /* Soap Fax interface ff/3/1, any unused socket id */
107    HPMUD_MARVELL_SCAN_CHANNEL = 0x15,    /* Marvell scan interface ff/ff/ff, any unused socket id */
108    HPMUD_MARVELL_FAX_CHANNEL = 0x16,    /* Marvell fax interface ff/ff/ff, any unused socket id */
109    HPMUD_EWS_LEDM_CHANNEL = 0x17,     /* Embeded Web Server interface ff/4/1, any unused socket id */
110    HPMUD_LEDM_SCAN_CHANNEL = 0x18,  /* LEDM scan interface ff/cc/0, any unused socket id */
111    HPMUD_MARVELL_EWS_CHANNEL = 0x19, /*MARVELL EWS interface found in Cicad Series*/
112    HPMUD_ESCL_SCAN_CHANNEL = 0x1a,  /* ESCL scan interface ff/cc/0, any unused socket id */
113    HPMUD_WIFI_CHANNEL = 0x2b,      /* WIFI config */
114    HPMUD_DEVMGMT_CHANNEL = 0x2c,      /* decimal 44 */
115    HPMUD_IPP_CHANNEL = 0x2d,
116    HPMUD_IPP_CHANNEL2 = 0x2e,
117    HPMUD_MAX_CHANNEL_ID
118 };
119 
120 #define HPMUD_DEVICE_MAX 2      /* zero is not used */
121 #define HPMUD_CHANNEL_MAX HPMUD_MAX_CHANNEL_ID
122 
123 /* MLC/1284.4 attributes. Note for MLC, attributes must remain persistant while transport is up. */
124 typedef struct
125 {
126    unsigned short h2pcredit;   /* host to peripheral credit (dot4: primary socket id credit for sending) */
127    unsigned short p2hcredit;  /* peripheral to host credit (dot4: secondary socket id credit for sending) */
128    unsigned short h2psize;  /* host to peripheral packet size in bytes (dot4: primary max packet size for sending) */
129    unsigned short p2hsize;  /* peripheral to host packet size in bytes (dot4: secondary max packet size for sending) */
130 } transport_attributes;
131 
132 typedef struct _mud_channel_vf
133 {
134    enum HPMUD_RESULT (*open)(struct _mud_channel *pc);                                        /* transport specific open */
135    enum HPMUD_RESULT (*close)(struct _mud_channel *pc);                                       /* transport specific close */
136    enum HPMUD_RESULT (*channel_write)(struct _mud_channel *pc, const void *buf, int size, int timeout, int *bytes_wrote);  /* tranport specific write */
137    enum HPMUD_RESULT (*channel_read)(struct _mud_channel *pc, void *buf, int size, int timeout, int *bytes_read);   /* transport specific read */
138 } mud_channel_vf;
139 
140 typedef struct _mud_device_vf
141 {
142    int (*write)(int fd, const void *buf, int size, int usec_timeout);                     /* low level device write */
143    int (*read)(int fd, void *buf, int size, int usec_timout);           /* low level device read */
144    enum HPMUD_RESULT (*open)(struct _mud_device *pd);                                        /* device specific open */
145    enum HPMUD_RESULT (*close)(struct _mud_device *pd);                                       /* device specific close */
146    enum HPMUD_RESULT (*get_device_id)(struct _mud_device *pd, char *id, int size, int *bytes_read);                      /* IEEE 1284 device id string */
147    enum HPMUD_RESULT (*get_device_status)(struct _mud_device *pd, unsigned int *status);                     /* device 8-bit status */
148    enum HPMUD_RESULT (*channel_open)(struct _mud_device *pd, const char *channel_name, HPMUD_CHANNEL *cd);                        /* channel specific open */
149    enum HPMUD_RESULT (*channel_close)(struct _mud_device *pd, struct _mud_channel *pc);                                     /* channel specific close */
150    enum HPMUD_RESULT (*channel_write)(struct _mud_device *pd, struct _mud_channel *pc, const void *buf, int size, int sec_timeout, int *bytes_wrote);
151    enum HPMUD_RESULT (*channel_read)(struct _mud_device *pd, struct _mud_channel *pc, void *buf, int size, int sec_timeout, int *bytes_read);
152 } mud_device_vf;
153 
154 typedef struct _mud_channel
155 {
156    char sn[HPMUD_LINE_SIZE];         /* service name */
157    unsigned char sockid;       /* socket id */
158    int client_cnt;             /* number of clients using this channel */
159    int index;                  /* channel[index] of this object */
160    int fd;                     /* file descriptor for this channel */
161    pid_t pid;                  /* process owner */
162    int dindex;                 /* device[dindex] parent device */
163 
164    /* MLC/1284.4 specific variables. */
165    transport_attributes ta;
166    unsigned char rbuf[HPMUD_BUFFER_SIZE];  /* read packet buffer */
167    int rindex;
168    int rcnt;
169 
170    /* JetDirect specific data. */
171    int socket;
172 
173    mud_channel_vf vf;
174 } mud_channel;
175 
176 typedef struct _mud_device
177 {
178    char uri[HPMUD_LINE_SIZE];
179    char id[1024];                    /* device id */
180    int index;                        /* device[index] of this object */
181    enum HPMUD_IO_MODE io_mode;
182    mud_channel channel[HPMUD_CHANNEL_MAX];
183    int channel_cnt;                  /* number of open channels */
184    int open_fd;                      /* file descriptor used by device_open */
185 
186    /* MLC/1284.4 specific variables. */
187    int mlc_up;                       /* 0=transport down, 1=transport up */
188    int mlc_fd;                       /* file descriptor used by 1284.4/MLC transport */
189 
190    /* JetDirect specific data. */
191    char ip[HPMUD_LINE_SIZE];              /* internet address */
192    int port;
193 
194    mud_device_vf vf;                 /* virtual function table */
195    pthread_mutex_t mutex;
196 } mud_device;
197 
198 typedef struct
199 {
200    mud_device device[HPMUD_DEVICE_MAX];
201    pthread_mutex_t mutex;
202 } mud_session;
203 
204 extern mud_session *msp __attribute__ ((visibility ("hidden")));
205 
206 void __attribute__ ((visibility ("hidden"))) sysdump(const void *data, int size);
207 int __attribute__ ((visibility ("hidden"))) mm_device_lock(int fd, HPMUD_DEVICE index);
208 int __attribute__ ((visibility ("hidden"))) mm_device_unlock(int fd, HPMUD_DEVICE index);
209 int __attribute__ ((visibility ("hidden"))) mm_device_trylock(int fd, HPMUD_DEVICE index);
210 int __attribute__ ((visibility ("hidden"))) is_hp(const char *id);
211 int  __attribute__ ((visibility ("hidden"))) generalize_model(const char *sz, char *buf, int bufSize);
212 int  __attribute__ ((visibility ("hidden"))) generalize_serial(const char *sz, char *buf, int bufSize);
213 int __attribute__ ((visibility ("hidden"))) get_uri_model(const char *uri, char *buf, int bufSize);
214 int __attribute__ ((visibility ("hidden"))) get_uri_serial(const char *uri, char *buf, int bufSize);
215 enum HPMUD_RESULT __attribute__ ((visibility ("hidden"))) service_to_channel(mud_device *pd, const char *sn, HPMUD_CHANNEL *index);
216 extern int (*getSIData)(char **pData , int *pDataLen, char **pModeSwitch, int *pModeSwitchLen);
217 extern void (*freeSIData)(char *pData, char *pModeSwitch);
218 
219 static const char *SnmpPort[] = { "","public","public.1","public.2","public.3"};
220 
221 #define PORT_PUBLIC  1
222 #define PORT_PUBLIC_1  2
223 #define PORT_PUBLIC_2  3
224 #define PORT_PUBLIC_3  4
225 
226 #endif // _HPMUDI_H
227 
228