download capi.cpp
Language: C++
Copyright: Copyright 1987-96, Bit Bucket Software Co.
LOC: 381
Project Info
BinkleyTerm XE(btxe)
Server: SourceForge
Type: cvs
...e\b\btxe\btxe\btnt\sources\
   Addr.cpp
   Addr.h
   aglcrc.cpp
   aglcrc.h
   asyn_lnx.cpp
   asyn_os2.cpp
   asyn_w32.cpp
   async.h
   asyos2v1.h
   b_banner.cpp
   b_bbs.cpp
   b_faxrcv.cpp
   b_flofil.cpp
   b_frproc.cpp
   b_help.cpp
   b_initva.cpp
   b_logs.cpp
   b_passwo.cpp
   b_proto.cpp
   b_rspfil.cpp
   b_sbinit.cpp
   b_script.cpp
   b_search.cpp
   b_sessio.cpp
   b_spawn.cpp
   b_term.cpp
   b_termov.cpp
   b_today.cpp
   b_wazoo.cpp
   b_whack.cpp
   b_wzsend.cpp
   banner.cpp
   banner.h
   BasicString.h
   bbs_ctra.cpp
   bbs_data.cpp
   bbs_incl.h
   bbs_io.cpp
   bink.h
   bink_asm.cpp
   binkdlg.cpp
   binkpipe.h
   box.h
   brec.cpp
   bsend.cpp
   bt.cpp
   btconfig.cpp
   btutil.cpp
   buffer.h
   cache.cpp
   cache.h
   callback.cpp
   callerid.cpp
   callwin.cpp
   callwin.h
   capi.cpp
   cfosline.cpp
   chat.cpp
   com.h
   com_dos.h
   com_lnx.h
   com_os2.h
   com_w32.h
   common.cpp
   cpuload.cpp
   css.cpp
   css.h
   data.cpp
   debug.cpp
   debug.h
   defines.h
   dosfuncs.cpp
   emsi.cpp
   emxmxcom.cpp
   evtparse.cpp
   evtsetup.cpp
   exceptio.cpp
   exceptio.h
   Exceptions.cpp
   Exceptions.h
   externs.h
   faxproto.h
   fcfgread.cpp
   fidomail.h
   file_all.cpp
   file_dos.cpp
   file_io.h
   file_lnx.cpp
   file_os2.cpp
   file_w32.cpp
   foffsets.h
   fossil.cpp
   freepoll.cpp
   ftsc.cpp
   gethcomm.h
   heap.cpp
   history.cpp
   history.h
   hydra.cpp
   hydra.h
   includes.cpp
   includes.h
   janus.cpp
   janus.h
   keybd.h
   keyfncs.h
   keymap.cpp
   langload.cpp
   language.h
   lngmagic.h
   m7rec.cpp
   m7send.cpp
   mailer.cpp
   mailovly.cpp
   mailroot.cpp
   maxcomm.h
   maxmcp.cpp
   maxmcp.h
   maxprm.cpp
   mdm_proc.cpp
   misc.cpp
   msgs.h
   Node.cpp
   Node.h
   NodeList.cpp
   NodeList.h
   nodeproc.cpp
   os2_pm.cpp
   outbound.cpp
   pipe.cpp
   pktmsgs.cpp
   pktmsgs.h
   protcomm.cpp
   prototyp.h
   recvbark.cpp
   recvsync.cpp
   sbuf.cpp
   sbuf.h
   sched.cpp
   sched.h
   script2.cpp
   SelfTest.cpp
   SelfTest.h
   sendbark.cpp
   sendsync.cpp
   squish.cpp
   squish.h
   srif.cpp
   statetbl.cpp
   stats.cpp
   stdafx.cpp
   stdafx.h
   timer.cpp
   timer.h
   times.cpp
   types.h
   v7debug.cpp
   version.cpp
   version7.cpp
   version7.h
   vfos_dos.cpp
   vfos_lnx.cpp
   vfos_os2.cpp
   vfos_qnx.cpp
   vfos_w32.cpp
   vfossil.h
   video.h
   vt100.cpp
   vt100.h
   xfer.h
   xmrec.cpp
   xmsend.cpp
   xstring.h
   yoohoo.cpp
   zmodem.cpp
   zmodem.h

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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*---------------------------------------------------------------------------*
 *
 *                               BinkleyTerm
 *
 *              (C) Copyright 1987-96, Bit Bucket Software Co.
 *     For license and contact information see /doc/orig_260/license.260.
 *
 *           This version was modified by the BinkleyTerm XE Team.
 *        For contact information see /doc/team.lst and /doc/join.us.
 *  For a complete list of changes see /doc/xe_user.doc and /doc/xe_hist.doc.
 *
 * Filename    : $Source: /cvsroot/btxe/btnt/sources/capi.cpp,v $
 * Revision    : $Revision: 1.1 $
 * Tagname     : $Name:  $
 * Last updated: $Date: 2005/01/01 15:18:12 $
 * State       : $State: Exp $
 * Orig. Author: C.F.S.
 *
 * Description : CAPI-CID module
 *
 *---------------------------------------------------------------------------*/

#if defined(OS_2) && !defined(__EMX__)
#pragma stack16 (0)

#include "includes.h"
#include "third/capi11.h"

static HMODULE capiDLL = (HMODULE) 0;

HFILE IsdnHandle;
int ApplId;
int cancel;
static HEV EventSemaphore = 0;
int Manufacturer = 0;
bool needs_dossleep = FALSE;

void CAPIGoodBye (void);

char *MessageLost = "CAPI message lost";
char *BadID = "CAPI error: Invalid application ID";

#define  MAN_UNKNOWN 0
#define  MAN_DIEHL   1
#define  MAN_TELES   2
#define  MAN_NCP     3
#define  MAN_NCPA    4

void
ConvertToDecent (unsigned char *number)
{
    int i, j;
    int l = number[0];            /* Length in [0], "nice" Pascal */

    for (i = 1; i <= l; i++)
    {
        if (number[i] & 0x80)
            break;
    }

    j = 0;
    for (i++; i <= l; i++, j++)
        number[j] = number[i];

    number[j] = '\0';
}

void
#if defined(__IBMC__) || defined(__IBMCPP__)
ProcessCAPIMessage (void *msgrx)
#else
ProcessCAPIMessage (API_MSG _Far16 * msgrx)
#endif
{
    API_MSG msg =
        {
            {0, 0, 0, 0}
        };
    API_MSG *apimsg = msgrx;
    unsigned short cmx;
    int plci, i, j;
    unsigned char phone[40];

    msg.header.Appl_id = ApplId;
    msg.header.Number = 0;

    if (apimsg == NULL)           /* Null message */
        return;

    cmx = apimsg->header.Command;

    switch (cmx)
    {
    case CONNECT_I:
        plci = apimsg->info.connect_ind.PLCI;
        i = apimsg->info.connect_ind.Structs[0];
        for (j = 0; j <= i; j++)
            phone[j] = apimsg->info.connect_ind.Structs[j];
        phone[j] = 0;
        ConvertToDecent (phone);
        if (UpdateCID)
            strcpy (CIDFromCAPI, (char *) phone);
        msg.header.Length = 11;
        msg.header.Command = CONNECT_I | RESPONSE;
        msg.info.connect_res.PLCI = plci;
        msg.info.connect_res.Reject = 1;
        API_PUT_MESSAGE (ApplId, (API_MSG * _Seg16) & msg);
        return;

    case DISCONNECT_I:
        plci = apimsg->info.disconnect_ind.PLCI;
        msg.header.Length = 10;
        msg.header.Command = DISCONNECT_I | CONFIRM;
        msg.info.disconnect_res.PLCI = plci;
        API_PUT_MESSAGE (ApplId, (API_MSG * _Seg16) & msg);
        return;

        /* Should not happen but just in case we are ready to process them */
    case INFO_I:
        plci = apimsg->info.info_ind.PLCI;
        msg.header.Length = 10;
        msg.header.Command = INFO_I | CONFIRM;
        msg.info.info_con.PLCI = plci;
        API_PUT_MESSAGE (ApplId, (API_MSG * _Seg16) & msg);
        return;
    }

    return;
}

typedef void _Far16 _Pascal fnCapiSignal (void);

static void _Far16 _Pascal
SignalRoutine (void)
{
    DosPostEventSem (EventSemaphore);
    if (needs_dossleep)
        DosSleep (1);
    return;
}

void
Listen (PVOID x)
{
#ifdef __BORLANDC__
    /* TE 140498: This probably is not correct, but it does not compile with
     * Borland the other way round. You'd rather not use the CAPI
     * feature with a Borland compiled BT ... */
    API_MSG _Seg16 *msg;

#else
#if defined(__IBMC__) || defined(__IBMCPP__)

    API_MSG *_Seg16 msg;

#else

API_MSG _Far16 *msg;

#endif
#endif

#if !defined(__IBMC__) && !defined(__IBMCPP__)

    void _Far16 _Pascal (*pfnCapiSignal) (void);

#endif

    int rcs, rca, i;
    unsigned long scount = 0;

    x = x;

#if !defined(__IBMC__) && !defined(__IBMCPP__)
    pfnCapiSignal = &SignalRoutine;
#endif

    printf ("CAPI message process thread successfully started.\n");
    if (Manufacturer == MAN_NCPA)
    {
        do
        {
            DosSleep (5);
            rca = API_GET_MESSAGE (ApplId, (API_MSG * _Seg16 * _Seg16) & msg);
            if (rca == 0)
                ProcessCAPIMessage (msg);
            if (rca == 0x1002)
                status_line (":%s (%d)", BadID, ApplId);
            if (rca == 0x1007)
                status_line (":%s", MessageLost);
        }
        while (!cancel);

        _endthread ();
    }

    DosResetEventSem (EventSemaphore, &scount);

#if defined(__IBMC__) || defined(__IBMCPP__)
    API_SET_SIGNAL (ApplId, SignalRoutine);
#else
API_SET_SIGNAL (ApplId, pfnCapiSignal);
#endif

    if (Manufacturer == MAN_DIEHL)
    {
        do
        {
            rcs = DosWaitEventSem (EventSemaphore, 10000);
            if (rcs == 0)
                DosResetEventSem (EventSemaphore, &scount);

            for (i = 0; i < scount; i++)
            {
                rca = API_GET_MESSAGE (ApplId, (API_MSG * _Seg16 * _Seg16) & msg);
                if (rca == 0)
                    ProcessCAPIMessage (msg);
                if (rca == 0x1002)
                    status_line (":%s (%d)", BadID, ApplId);
                if (rca == 0x1007)
                    status_line (":%s", MessageLost);
            };

        }
        while (!cancel);

        _endthread ();
    }

    do
    {
        rcs = DosWaitEventSem (EventSemaphore, 10000);
        if (rcs == 0)
            DosResetEventSem (EventSemaphore, &scount);

        do
        {
            rca = API_GET_MESSAGE (ApplId, (API_MSG * _Seg16 * _Seg16) & msg);
            if (rca == 0)
                ProcessCAPIMessage (msg);
            if (rca == 0x1002)
                status_line (":%s (%d)", BadID, ApplId);
            if (rca == 0x1007)
                status_line (":%s", MessageLost);
        }
        while (rca == 0);
    }
    while (!cancel);

    _endthread ();
}

int
CAPISetup (char *eaz, char *services)
{
    int rc;
    char *MessageBuffer;
    int NumberMessages, Layer3, L3Size, L3Packets;
    char hb[100];
    unsigned short EazMask = 0, ServiceMask = 0;
    API_MSG msg =
        {
            {0, 0, 0, 0}
        };

    printf ("CAPI: ");
    if (!API_INSTALLED ())
    {
        printf ("Not found. Cannot continue\n");
        return -1;
    }

    printf ("is installed.\n");
    printf ("Manufacturer: ");
    API_GET_MANUFACTURER (hb);
    printf ("%s\n", hb);
    Manufacturer = MAN_UNKNOWN;

    if (strstrci (hb, "Diehl") == hb)
        Manufacturer = MAN_DIEHL;
    if (strstrci (hb, "TELES") == hb)
        Manufacturer = MAN_TELES;
    if (strstrci (hb, "High So") == hb)
        Manufacturer = MAN_NCP;
    if (strstrci (hb, "NCP") == hb)
        Manufacturer = MAN_NCPA;

    API_GET_VERSION (hb);
    printf ("Version: %s\n", hb);
    if (strstr (hb, "Release 2.9") != NULL && Manufacturer == MAN_TELES)
        needs_dossleep = TRUE;
    API_GET_SERIAL_NUMBER (hb);
    printf ("Serial #: %s\n", hb);

    /*  ------------------------- Variable setup ---------------------------- */
    MessageBuffer = (char *) malloc (65000L);
    if (MessageBuffer == NULL)
    {
        printf ("Not enough memory for message buffer.\n");
        return -1;
    }

    NumberMessages = 40;
    Layer3 = 2;
    L3Size = 2048;
    if (Manufacturer == MAN_DIEHL)
        L3Packets = (63000 - (180 * NumberMessages)) / L3Size;
    else
        L3Packets = (32000 - (180 * NumberMessages)) / L3Size;

    /*  ---------------------- Registration with CAPI ---------------------- */
    ApplId = 0;
    ApplId = API_REGISTER (MessageBuffer, NumberMessages, Layer3, L3Packets, L3Size);
    if (!ApplId)
    {
        printf ("CAPI registration failed.\n");
        return -1;
    }

    printf ("Registration with CAPI successful (ID: %d)\n", ApplId);

    rc = DosCreateEventSem ((PSZ) NULL, &EventSemaphore, 0, FALSE);
    if (rc)
    {
        printf ("\nError calling DosCreateEventSem: %d\n", rc);
        CAPIGoodBye ();
        return -1;
    }

    cancel = 0;
    _beginthread (Listen, NULL, 32768, NULL);

    DosSleep (1000);              /* Sleep a bit until CAPI is ready */

    msg.header.Length = 17;
    msg.header.Command = LISTEN_R;
    msg.header.Appl_id = ApplId;

    msg.info.listen_req.Controller = 0;
    msg.info.listen_req.Info_Mask = 0x0000;  /* Don't disturb with infos */
    msg.info.listen_req.Si_Mask = 0x000F;  /* Mask */

    while (isdigit (*eaz))
        EazMask |= (0x01 << (*eaz++ & 0x0F));

    while (*services)
    {
        switch (toupper (*services++))
        {
        case 'V':
            ServiceMask |= 1;
            break;
        case 'T':
            ServiceMask |= 2;
            break;
        case 'A':
            ServiceMask |= 4;
            break;
        case '2':
            ServiceMask |= 8;
            break;
        case '4':
            ServiceMask |= 16;
            break;
        case 'I':
            ServiceMask |= 32;
            break;
        case 'D':
            ServiceMask |= 128;
            break;
        case '5':
            ServiceMask |= 256;
            break;
        case 'L':
            ServiceMask |= 512;
            break;
        case 'M':
            ServiceMask |= 1024;
            break;
        case 'R':
            ServiceMask |= 8192;
            break;
        case 'G':
            ServiceMask |= 16384;
            break;
        case 'X':
            ServiceMask |= 32768;
            break;
        }
    }

    printf ("EAZ Mask: %hu, Service Mask %hu\n", EazMask, ServiceMask);
    msg.info.listen_req.Eaz_Mask = EazMask;
    msg.info.listen_req.Si_Mask = ServiceMask;
    rc = API_PUT_MESSAGE (ApplId, (API_MSG *) & msg);
    return 0;
}

void
CAPIGoodBye (void)
{
    int rc = API_RELEASE (ApplId);

    cancel = 1;
    if (!rc)
        printf ("Capi released successfully.\n");
    else
        printf ("Could not release CAPI (error: %d)!\n", rc);

    DosSleep (100);
}


USHORT
CapiLoadDLL (void)
{
    APIRET rc = 0;

    rc = DosLoadModule (NULL, 0l, (PCSZ) "CAPI", &capiDLL);
    if (rc == 0)
    {
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_REGISTER",
                               (PFN *) & API_REGISTER);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_RELEASE",
                               (PFN *) & API_RELEASE);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_GET_MESSAGE",
                               (PFN *) & API_GET_MESSAGE);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_PUT_MESSAGE",
                               (PFN *) & API_PUT_MESSAGE);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_SET_SIGNAL",
                               (PFN *) & API_SET_SIGNAL);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_GET_MANUFACTURER",
                               (PFN *) & API_GET_MANUFACTURER);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_GET_VERSION",
                               (PFN *) & API_GET_VERSION);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_INSTALLED",
                               (PFN *) & API_INSTALLED);
        rc += BtQueryProcAddr (capiDLL, 0L, (PCSZ) "API_GET_SERIAL_NUMBER",
                               (PFN *) & API_GET_SERIAL_NUMBER);
    }

    return (USHORT) rc;
}

void
CapiUnLoadDLL (void)
{
    if (capiDLL)
        DosFreeModule (capiDLL);
}

USHORT
CapiInstalled (void)
{
    return (API_INSTALLED ());
}

int
CAPIInit (char *EAZs, char *Services)
{
    int x;

    printf ("----------------------------------------------\n");

    if (CapiLoadDLL ())
    {
        printf ("Link with CAPI failed, CAPI.DLL not found?\n");
        return -1;
    }

    x = CAPISetup (EAZs, Services);
    printf ("----------------------------------------------\n");
    return x;
}

void
CAPIStop (void)
{
    UseCAPICID = 0;
    CAPIGoodBye ();
    CapiUnLoadDLL ();
}

#endif /* if defined(OS_2) && !defined(__EMX__) */

/* $Id: capi.cpp,v 1.1 2005/01/01 15:18:12 vildanov Exp $ */

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us