forked from leggewie-DM/libcapi20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapidyn.c
414 lines (354 loc) · 10 KB
/
capidyn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* $Id: capidyn.c,v 1.1 2009/05/08 21:02:02 buzz Exp $
*
* $Log: capidyn.c,v $
* Revision 1.1 2009/05/08 21:02:02 buzz
* Add new libcapi20
*
* Revision 1.4 2004/01/16 15:27:11 calle
* remove several warnings.
*
* Revision 1.3 2004/01/16 14:57:22 calle
* gcc warning removed.
*
* Revision 1.2 2001/03/01 14:59:11 paul
* Various patches to fix errors when using the newest glibc,
* replaced use of insecure tempnam() function
* and to remove warnings etc.
*
* Revision 1.1 2000/05/18 15:21:37 calle
* static CAPI2.0 library that loads libcapi20.so dynamicly.
*
*/
#include <sys/types.h>
#define _LINUX_LIST_H
#ifndef WIN32
#include "capi_defs.h"
#endif
#include <string.h>
#include <unistd.h>
#include "capi20.h"
#include <dlfcn.h>
#define LIBCAPI "libcapi20.so.3"
static int loadlib(void);
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi20_isinstalled)(void);
unsigned capi20_isinstalled (void)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_isinstalled();
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi20_register)(unsigned, unsigned, unsigned, unsigned *);
unsigned capi20_register (unsigned MaxB3Connection,
unsigned MaxB3Blks,
unsigned MaxSizeB3,
unsigned *ApplID)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_register(MaxB3Connection,
MaxB3Blks, MaxSizeB3, ApplID);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi20_release)(unsigned);
unsigned
capi20_release (unsigned ApplID)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_release(ApplID);
}
/* ---------------------------------------------------------------------- */
static unsigned (*fptr_capi20_put_message)(unsigned, unsigned char *);
unsigned
capi20_put_message (unsigned ApplID, unsigned char *Msg)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_put_message(ApplID, Msg);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi20_get_message)(unsigned, unsigned char **);
unsigned
capi20_get_message (unsigned ApplID, unsigned char **Buf)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_get_message(ApplID, Buf);
}
/* ---------------------------------------------------------------------- */
static unsigned char *
(*fptr_capi20_get_manufacturer)(unsigned, unsigned char *);
unsigned char *
capi20_get_manufacturer(unsigned Ctrl, unsigned char *Buf)
{
if (loadlib() < 0)
return (unsigned char *)"";
return fptr_capi20_get_manufacturer(Ctrl, Buf);
}
/* ---------------------------------------------------------------------- */
static unsigned char *
(*fptr_capi20_get_version)(unsigned, unsigned char *);
unsigned char *
capi20_get_version(unsigned Ctrl, unsigned char *Buf)
{
if (loadlib() < 0)
return (unsigned char *)"";
return fptr_capi20_get_version(Ctrl, Buf);
}
/* ---------------------------------------------------------------------- */
static unsigned char *
(*fptr_capi20_get_serial_number)(unsigned, unsigned char *);
unsigned char *
capi20_get_serial_number(unsigned Ctrl, unsigned char *Buf)
{
if (loadlib() < 0)
return (unsigned char *)"";
return fptr_capi20_get_serial_number(Ctrl, Buf);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi20_get_profile)(unsigned, unsigned char *);
unsigned
capi20_get_profile(unsigned Ctrl, unsigned char *Buf)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_get_profile(Ctrl, Buf);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi20_waitformessage)(unsigned, struct timeval *);
unsigned
capi20_waitformessage(unsigned ApplID, struct timeval *TimeOut)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi20_waitformessage(ApplID, TimeOut);
}
/* ---------------------------------------------------------------------- */
static int
(*fptr_capi20_fileno)(unsigned);
int
capi20_fileno(unsigned ApplID)
{
if (loadlib() < 0)
return -1;
return fptr_capi20_fileno(ApplID);
}
/* ---------------------------------------------------------------------- */
static int
(*fptr_capi20ext_get_flags)(unsigned, unsigned *);
int
capi20ext_get_flags(unsigned ApplID, unsigned *flagsptr)
{
if (loadlib() < 0)
return -1;
return fptr_capi20ext_get_flags(ApplID, flagsptr);
}
/* ---------------------------------------------------------------------- */
static int
(*fptr_capi20ext_set_flags)(unsigned, unsigned);
int
capi20ext_set_flags(unsigned ApplID, unsigned flags)
{
if (loadlib() < 0)
return -1;
return fptr_capi20ext_set_flags(ApplID, flags);
}
/* ---------------------------------------------------------------------- */
static int
(*fptr_capi20ext_clr_flags)(unsigned, unsigned);
int
capi20ext_clr_flags(unsigned ApplID, unsigned flags)
{
if (loadlib() < 0)
return -1;
return fptr_capi20ext_clr_flags(ApplID, flags);
}
/* ---------------------------------------------------------------------- */
static char *
(*fptr_capi20ext_get_tty_devname)(unsigned, unsigned, char *, size_t);
char *
capi20ext_get_tty_devname(unsigned applid, unsigned ncci, char *buf, size_t size)
{
if (loadlib() < 0)
return 0;
return fptr_capi20ext_get_tty_devname(applid, ncci, buf, size);
}
/* ---------------------------------------------------------------------- */
static char *
(*fptr_capi20ext_get_raw_devname)(unsigned, unsigned, char *, size_t);
char *
capi20ext_get_raw_devname(unsigned applid, unsigned ncci, char *buf, size_t size)
{
if (loadlib() < 0)
return 0;
return fptr_capi20ext_get_raw_devname(applid, ncci, buf, size);
}
/* ---------------------------------------------------------------------- */
static int
(*fptr_capi20ext_ncci_opencount)(unsigned, unsigned);
int
capi20ext_ncci_opencount(unsigned applid, unsigned ncci)
{
if (loadlib() < 0)
return 0;
return fptr_capi20ext_ncci_opencount(applid, ncci);
}
/* ---------------------------------------------------------------------- */
static char *
(*fptr_capi_info2str)(_cword);
char *
capi_info2str(_cword reason)
{
if (loadlib() < 0)
return 0;
return fptr_capi_info2str(reason);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi_cmsg2message)(_cmsg *, _cbyte *);
unsigned
capi_cmsg2message(_cmsg * cmsg, _cbyte * msg)
{
if (loadlib() < 0)
return 0;
return fptr_capi_cmsg2message(cmsg, msg);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi_message2cmsg)(_cmsg *, _cbyte *);
unsigned
capi_message2cmsg(_cmsg * cmsg, _cbyte * msg)
{
if (loadlib() < 0)
return 0;
return fptr_capi_message2cmsg(cmsg, msg);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi_cmsg_header)(_cmsg *, unsigned, _cbyte, _cbyte, _cword, _cdword);
unsigned
capi_cmsg_header (_cmsg *cmsg, unsigned _ApplId,
_cbyte _Command, _cbyte _Subcommand,
_cword _Messagenumber, _cdword _Controller)
{
if (loadlib() < 0)
return 0;
return fptr_capi_cmsg_header(cmsg, _ApplId,
_Command, _Subcommand,
_Messagenumber, _Controller);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi_put_cmsg)(_cmsg *);
unsigned
capi_put_cmsg (_cmsg *cmsg)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi_put_cmsg(cmsg);
}
/* ---------------------------------------------------------------------- */
static unsigned
(*fptr_capi_get_cmsg)(_cmsg *, unsigned);
unsigned
capi_get_cmsg (_cmsg *cmsg, unsigned applid)
{
if (loadlib() < 0)
return CapiRegNotInstalled;
return fptr_capi_get_cmsg(cmsg, applid);
}
/* ---------------------------------------------------------------------- */
static char *
(*fptr_capi_cmd2str)(_cbyte, _cbyte);
char *
capi_cmd2str(_cbyte cmd, _cbyte subcmd)
{
if (loadlib() < 0)
return "";
return fptr_capi_cmd2str(cmd, subcmd);
}
/* ---------------------------------------------------------------------- */
static char *
(*fptr_capi_message2str)(_cbyte *);
char *
capi_message2str(_cbyte * msg)
{
if (loadlib() < 0)
return "";
return fptr_capi_message2str(msg);
}
/* ---------------------------------------------------------------------- */
static char *
(*fptr_capi_cmsg2str)(_cmsg *);
char *
capi_cmsg2str(_cmsg * cmsg)
{
if (loadlib() < 0)
return "";
return fptr_capi_cmsg2str(cmsg);
}
/* ---------------------------------------------------------------------- */
static void *handle;
#define resolv_sym(x) \
if ((fptr_##x = dlsym(handle, #x)) == 0) { \
(void)write(2, "Can't resolv " #x, sizeof("Can't resolv " #x)-1); \
dlclose(handle); \
handle = 0; \
return -1; \
} else
static char emsg[] = "Couldn't load shared library ";
static int loadlib(void)
{
const char *err;
if (handle)
return 0;
handle = dlopen(LIBCAPI, RTLD_GLOBAL | RTLD_NOW);
if (handle == 0) {
err = dlerror();
(void)write(2, emsg, sizeof(emsg)-1);
(void)write(2, LIBCAPI, sizeof(LIBCAPI)-1);
(void)write(2, "\n", 1);
if (err) {
(void)write(2, err, strlen(err));
(void)write(2, "\n", 1);
}
return -1;
}
resolv_sym(capi20_isinstalled);
resolv_sym(capi20_register);
resolv_sym(capi20_release);
resolv_sym(capi20_put_message);
resolv_sym(capi20_get_message);
resolv_sym(capi20_get_manufacturer);
resolv_sym(capi20_get_version);
resolv_sym(capi20_get_serial_number);
resolv_sym(capi20_get_profile);
resolv_sym(capi20_waitformessage);
resolv_sym(capi20_fileno);
resolv_sym(capi20ext_get_flags);
resolv_sym(capi20ext_set_flags);
resolv_sym(capi20ext_clr_flags);
resolv_sym(capi20ext_get_tty_devname);
resolv_sym(capi20ext_get_raw_devname);
resolv_sym(capi20ext_ncci_opencount);
resolv_sym(capi_info2str);
resolv_sym(capi_cmsg2message);
resolv_sym(capi_message2cmsg);
resolv_sym(capi_cmsg_header);
resolv_sym(capi_put_cmsg);
resolv_sym(capi_get_cmsg);
resolv_sym(capi_cmd2str);
resolv_sym(capi_message2str);
resolv_sym(capi_cmsg2str);
return 0;
}
#undef resolv_sym