1 /*
2  * Copyright (c) 2001 Tommy Bohlin <tommy@gatespace.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /* irda.h
27  */
28 
29 #include <sys/types.h>
30 
31 #ifdef __NetBSD__
32 #include <sys/param.h>
33 #if __NetBSD_Version__ >= 105260000
34 #include <dev/ir/irdaio.h>
35 #define IRDA_KERNEL_DRIVER
36 #endif
37 #endif /*__NetBSD__*/
38 
39 typedef int bool;
40 #define FALSE                    0
41 #define TRUE                     1
42 
43 /**********************************************************************
44  * General utilities
45  **********************************************************************/
46 
47 void showBytes(const void* buf, int len);
48 void showChars(const void* buf, int len);
49 
50 void putBELong(void* buf, int val);
51 void putBEShort(void* buf, int val);
52 int  getBELong(const void* buf);
53 int  getBEShort(const void* buf);
54 int  getBEVariable(const void* buf, int len);
55 int  getLEVariable(const void* buf, int len);
56 
57 int  getLEParameter(int key, int deflt, const void* buf, int len);
58 int  getBEParameter(int key, int deflt, const void* buf, int len);
59 
60 int  highestBit(int mask);
61 int  lowestBit(int mask);
62 
63 /**********************************************************************
64  * OS dependent utilities
65  **********************************************************************/
66 
67 void showResources(void);
68 
69 bool setFileLog(const char* file);
70 void setSysLog(void);
71 void birda_log(const char* fmt, ...);
72 
73 void* allocMem(const char* id, int size);
74 void* growMem(void* ptr, int size);
75 void  freeMem(void* ptr);
76 
77 unsigned getRandom(unsigned max);
78 
79 extern int evtDebug;
80 #define EVT_DEBUG_SELECT         1
81 #define EVT_DEBUG_TIMERS         2
82 
83 void evtSetTimer(int delay, void (*func)(void*), void* handle);
84 void evtCancelTimer(void (*func)(void*), void* handle);
85 void evtLoop(void);
86 
87 void evtRemoveSource(int fd);
88 void evtAddSource(int fd, void (*func)(void* handle), void* handle);
89 
90 /**********************************************************************
91  * Serial ports
92  **********************************************************************/
93 
94 typedef struct SerialPort {
95   void (*close)(struct SerialPort* sp);
96   void (*setSpeed)(struct SerialPort* sp, int speed);
97   void (*setLine)(struct SerialPort* sp, int control);
98 #define LINE_DTR                 1
99 #define LINE_RTS                 2
100   int  (*getSpeedMask)(struct SerialPort* sp);
101   int  (*getChar)(struct SerialPort* sp);
102 #define SERIAL_INBUF_EMPTY       (-1)
103   void (*putChar)(struct SerialPort* sp, int c);
104   /*-- User fields ----------*/
105   int debug;
106 #define SP_DEBUG_INPUT           1
107   void *handle;
108   void (*status)(struct SerialPort* sp, int event);
109 #define SERIAL_INPUT_AVAILABLE   1
110 } SerialPort;
111 
112 SerialPort* createSerialPort(const char* dev, int maxspeed);
113 
114 /**********************************************************************
115  * Serial devices
116  **********************************************************************/
117 
118 typedef struct SerialDevice {
119   void (*close)(struct SerialDevice* sd);
120   int  (*setSpeed)(struct SerialDevice* sd, int speed);
121   int  (*getSpeedMask)(struct SerialDevice* sd);
122   int  (*getMinTurnaroundMask)(struct SerialDevice* sd);
123   int  (*getChar)(struct SerialDevice* sd);
124 #define SERIAL_INBUF_EMPTY       (-1)
125   void (*putChar)(struct SerialDevice* sd, int c);
126   /*-- User fields ----------*/
127   int debug;
128   void* handle;
129   void (*status)(struct SerialDevice* sd, int event);
130 #define SERIAL_INPUT_AVAILABLE   1
131 } SerialDevice;
132 
133 SerialDevice* createRawDevice(SerialPort* sp);
134 SerialDevice* createTekram210Device(SerialPort* sp);
135 SerialDevice* createJetEyeDevice(SerialPort* sp);
136 SerialDevice* createACTiSYS220Device(SerialPort* sp, bool plus);
137 SerialDevice* createLiteLinkDevice(SerialPort* sp);
138 SerialDevice* createGIrBILDevice(SerialPort* sp);
139 SerialDevice* createRedlinkDevice(SerialPort* sp);
140 
141 /**********************************************************************
142  * Frame devices
143  **********************************************************************/
144 
145 typedef struct FrameDevice {
146   void (*close)(struct FrameDevice* fd);
147   int  (*setParams)(struct FrameDevice* fd, int speed, int ebofs, int maxSize);
148   void (*resetParams)(struct FrameDevice* fd);
149 #define DEFAULT_SPEED            9600
150 #define DEFAULT_EBOFS            10
151 #define DEFAULT_SIZE             386
152   void (*sendFrame)(struct FrameDevice* fd, const void* buf, int len);
153   int  (*getSpeedMask)(struct FrameDevice* fd);
154 /* SIR speeds */
155 #define SPEED_2400               0x001
156 #define SPEED_9600               0x002
157 #define SPEED_19200              0x004
158 #define SPEED_38400              0x008
159 #define SPEED_57600              0x010
160 #define SPEED_115200             0x020
161 /* MIR speeds */
162 #define SPEED_576000             0x040
163 #define SPEED_1152000            0x080
164 /* FIR speeds */
165 #define SPEED_4000000            0x100
166 /* VFIR speeds */
167 #define SPEED_16000000           0x200
168   int  (*getMinTurnaroundMask)(struct FrameDevice* fd);
169 #define MIN_TA_10ms              0x01
170 #define MIN_TA_5ms               0x02
171 #define MIN_TA_1ms               0x04
172 #define MIN_TA_500us             0x08
173 #define MIN_TA_100us             0x10
174 #define MIN_TA_50us              0x20
175 #define MIN_TA_10us              0x40
176 #define MIN_TA_0                 0x80
177   /*-- User fields ----------*/
178   int  debug;
179 #define FRAME_DEBUG_INPUT        1
180   void* handle;
181   void (*frame)(struct FrameDevice* fd, void* buf, int len);
182 } FrameDevice;
183 
184 int createSIRFrame(int ebofs, const u_char *src, int len, u_char *dst, int maxlen);
185 
186 FrameDevice* createSIRFrameDevice(SerialDevice* dev);
187 FrameDevice* createKernelFrameDevice(char *dev, int maxspeed);
188 
189 /**********************************************************************
190  * LAP/LM
191  **********************************************************************/
192 
193 typedef struct Connection {
194   /*-- User fields ----------*/
195   int debug;
196   void* handle;
197   void (*status)(struct Connection* con, int event, void* buf, int len);
198 #define CONN_OPENED              1
199 #define CONN_CLOSED              2
200   void (*data)(struct Connection* con, void* buf, int len);
201 } Connection;
202 
203 void connClose(Connection* con);
204 int  connGetSendDataSize(Connection* con);
205 int  connGetRecvDataSize(Connection* con);
206 bool connWrite(Connection* con, const void* buf, int len);
207 bool connWrite2(Connection* con, const void* buf1, int len1, const void* buf2, int len2);
208 
209 typedef struct LSAP {
210   /*-- User fields ----------*/
211   int debug;
212   void* handle;
213   bool (*accept)(struct LSAP* lsap, Connection* con, void* buf, int len);
214 } LSAP;
215 
216 void lsapClose(LSAP* lsap);
217 u_char lsapGetSelector(LSAP* lsap);
218 
219 typedef struct LAP {
220   /*-- User fields ----------*/
221   int flags;
222 #define HINT_PNP                 0x0001
223 #define HINT_PDA                 0x0002
224 #define HINT_COMPUTER            0x0004
225 #define HINT_PRINTER             0x0008
226 #define HINT_MODEM               0x0010
227 #define HINT_FAX                 0x0020
228 #define HINT_LAN_ACCESS          0x0040
229 #define HINT_TELEPHONY           0x0100
230 #define HINT_FILE_SERVER         0x0200
231 #define HINT_IRCOMM              0x0400
232 #define HINT_MESSAGE             0x0800
233 #define HINT_HTTP                0x1000
234 #define HINT_IROBEX              0x2000
235   int debug;
236 #define LAP_DEBUG_INFO           0x01
237 #define LAP_DEBUG_FRAMES_IN      0x02
238 #define LAP_DEBUG_FRAMES_OUT     0x04
239 #define LAP_DEBUG_TIMERS         0x08
240 #define LAP_SNOOP                0x10
241   void* handle;
242   bool (*status)(struct LAP* lap, int event, int address, int hints, int charset, char* name, int len);
243 #define LAP_DISCOVERING          1
244 #define LAP_CONNECTING           2
245 #define LAP_DISCOVERED           3
246 #define LAP_DISCOVERY_END        4
247 #define LAP_CONNECTED            5
248 #define LAP_DISCONNECTED         6
249 } LAP;
250 
251 LAP* createLAP(FrameDevice* fdev, int charSet, const char* name, int length);
252 #define CHARSET_ASCII            0
253 #define CHARSET_ISO_LATIN1       1
254 #define CHARSET_UNICODE          0xff
255 
256 void lapClose(LAP* lap);
257 bool lapDiscover(LAP* lap);
258 bool lapConnect(LAP* lap, int address);
259 void lapDisconnect(LAP* lap);
260 
261 LSAP* lapNewLSAP(LAP* lap, int flags);
262 #define LM_TINY_TP               1
263 
264 Connection* lapNewConnection(LAP* lap, int rlsap, int flags, const void* buf, int len);
265 
266 /**********************************************************************
267  * IAS
268  **********************************************************************/
269 
270 typedef struct Attribute {
271   /*-- User fields ----------*/
272   int debug;
273 } Attribute;
274 
275 void iasAttrDelete(Attribute* attr);
276 
277 typedef struct Object {
278   /*-- User fields ----------*/
279   int debug;
280 } Object;
281 
282 void iasObjDelete(Object* obj);
283 Attribute* iasObjNewInteger(Object*, const char* name, int value);
284 Attribute* iasObjNewString(Object*, const char* name, int charset, const char* value, int length);
285 Attribute* iasObjNewOctets(Object*, const char* name, const void* value, int length);
286 
287 typedef struct IASServer {
288   /*-- User fields ----------*/
289   int debug;
290 #define IAS_DEBUG_INFO           1
291 } IASServer;
292 
293 IASServer* createIASServer(LAP* lap, int charset, const char* name, int len);
294 
295 void iasSrvClose(IASServer* ias);
296 Object* iasSrvNewObject(IASServer* ias, const char* class);
297 
298 typedef struct IASClient {
299   /*-- User fields ----------*/
300   int debug;
301 #define IAS_DEBUG_INFO           1
302   void* handle;
303   void (*status)(struct IASClient* ic, int event);
304 #define IAS_QUERY_COMPLETE       1
305 #define IAS_QUERY_FAILED         2
306 } IASClient;
307 
308 IASClient* createIASClient(LAP* lap);
309 
310 void iasCltClose(IASClient* ias);
311 bool iasCltGetValueByClass(IASClient* ias, const char* class, const char* name);
312 int  iasCltResType(IASClient* ias);
313 #define IAS_NONE                 0
314 #define IAS_INT                  1
315 #define IAS_OCTETS               2
316 #define IAS_STRING               3
317 int  iasCltResId(IASClient* ias);
318 int  iasCltResInt(IASClient* ias);
319 int  iasCltResLength(IASClient* ias);
320 int  iasCltResCharSet(IASClient* ias);
321 void* iasCltResPtr(IASClient* ias);
322 void iasCltResNext(IASClient* ias);
323 void iasCltShowResult(IASClient* ias);
324 
325 /**********************************************************************
326  * OBEX
327  **********************************************************************/
328 
329 typedef struct OBEXServer {
330   /*-- User fields ----------*/
331   int debug;
332 #define OBEX_DEBUG_INFO          1
333   void* handle;
334   void (*object)(struct OBEXServer* obex, void* buf, int len);
335 } OBEXServer;
336 
337 OBEXServer* createOBEXServer(LAP* lap, IASServer* ias);
338 
339 void obexSrvClose(OBEXServer* obex);
340 
341 typedef struct OBEXClient {
342   /*-- User fields ----------*/
343   int debug;
344 #define OBEX_DEBUG_INFO          1
345   void* handle;
346   void (*status)(struct OBEXClient* obex, int event);
347 #define OBEX_SUCCESS             1
348 #define OBEX_FAILURE             2
349 } OBEXClient;
350 
351 OBEXClient* createOBEXClient(LAP* lap);
352 
353 void obexCltClose(OBEXClient* obex);
354 bool obexCltPut(OBEXClient* obex, const void* buf, int len);
355 
356 void createOBEXSender(int port);
357 
358 /**********************************************************************
359  * COMM
360  **********************************************************************/
361 
362 typedef struct COMMPort {
363   /*-- User fields ----------*/
364   int debug;
365   void* handle;
366   void (*status)(struct COMMPort* comm, int event);
367 #define COMM_CONNECTED           1
368 #define COMM_DISCONNECTED        2
369   void (*data)(struct COMMPort* comm, void* buf, int len);
370 } COMMPort;
371 
372 void commPortClose(COMMPort* cp);
373 void commPortWrite(COMMPort* cp, const void* buf, int len);
374 
375 typedef struct COMMServer {
376   /*-- User fields ----------*/
377   int debug;
378 #define COMM_DEBUG_INFO          1
379 } COMMServer;
380 
381 COMMServer* createCOMMServer(LAP* lap, IASServer* ias);
382 
383 COMMPort* commSrvNewPort(COMMServer* comm);
384 void commSrvClose(COMMServer* comm);
385 
386 typedef struct COMMClient {
387   /*-- User fields ----------*/
388   int debug;
389 #define COMM_DEBUG_INFO          1
390   void* handle;
391   void (*status)(struct COMMClient* comm, int event);
392   void (*data)(struct COMMClient* comm, void* buf, int len);
393 } COMMClient;
394 
395 COMMClient* createCOMMClient(LAP* lap, int serviceMask, int portMask, bool isDCE);
396 #define COMM_ST_3WIRE_RAW        1
397 #define COMM_ST_3WIRE            2
398 #define COMM_ST_9WIRE            4
399 #define COMM_ST_CENTRONICS       8
400 #define COMM_PT_SERIAL           1
401 #define COMM_PT_PARALLEL         2
402 
403 void commCltClose(COMMClient* comm);
404 void commCltWrite(COMMClient* comm, const void* buf, int len);
405 void commCltSetParams(COMMClient* comm, int dataRate, int dataFormat);
406 #define COMM_8N1                 0x3
407 void commCltSetDCE(COMMClient* comm, int dce);
408 #define COMM_CTS                 0x10
409 #define COMM_DSR                 0x20
410 #define COMM_RI                  0x40
411 #define COMM_CD                  0x80
412 void commCltSetDTE(COMMClient* comm, int dte);
413 #define COMM_DTR                 0x04
414 #define COMM_RTS                 0x08
415 
416 /**********************************************************************
417  * Options processing utility
418  **********************************************************************/
419 
420 typedef struct Option {
421   const char type;
422 #define OPTION_BOOL              0
423 #define OPTION_INT               1
424 #define OPTION_STRING            2
425   const char opt;
426   const char* arg;
427   const char* desc;
428   union {
429     bool b;
430     int i;
431     char* s;
432   } val;
433 } Option;
434 
435 void doOptions(int argc, char* const argv[],
436 	       int optc, Option* options,
437 	       int* verbosityp, const char* desc);
438 void doConnect(void);
439 
440 LAP* optLap;
441 IASServer* optIas;
442 
443 void (*optLapConnected)(LAP* lap);
444 void (*optLapDisconnected)(LAP* lap);
445