File: faxrecv.cpp

package info (click to toggle)
capi4hylafax 1%3A01.02.03-10sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,856 kB
  • ctags: 2,966
  • sloc: cpp: 20,126; sh: 7,703; makefile: 215; perl: 36
file content (543 lines) | stat: -rw-r--r-- 20,303 bytes parent folder | download
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*---------------------------------------------------------------------------*\

    Copyright (C) 2000 AVM GmbH. All rights reserved.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY, without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, and WITHOUT
    ANY LIABILITY FOR ANY DAMAGES arising out of or in connection
    with the use or performance of this software. See the
    GNU General Public License for further details.

\*---------------------------------------------------------------------------*/

#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <syslog.h>
#include <stdarg.h>
#include <errno.h>
#include <sysexits.h>
#include <unistd.h>
#include "faxrecv.h"
#include "ConfPars.h"
#include "aString.h"
#include "osImport.h"
#include "misc.h"

/*---------------------------------------------------------------------------*\
    from capiutils.h:
\*---------------------------------------------------------------------------*/

extern "C" char *capi_info2str(c_word reason);

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

#define FAX_FILTER_BUFFER_SIZE      1000
#define CAPI_BUFFER_SIZE            CapiMaxDataSize

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

//#define GENERATE_DEBUGSFFDATAFILE

#ifdef GENERATE_DEBUGSFFDATAFILE
static FILE *DebugSffDataFile = 0;
#endif

/*===========================================================================*\
\*===========================================================================*/

CFaxReceive::CFaxReceive (CFaxReceiveDevice *FaxRecvDevice, FaxFormatType faxtype)
  : jobNr (0),
    quitRecv (0),
    hJobFile (0) {

    dhead ("CFaxReceive - Constructor", DCON_CFaxReceive);
    FaxDevice = FaxRecvDevice;
    SetFormat (faxtype);
    SetReceiveID (&ONumber);
}

/*===========================================================================*\
\*===========================================================================*/

CFaxReceive::~CFaxReceive (void) {
    dhead ("CFaxReceive - Destructor", DCON_CFaxReceive);
    CloseRecv();
    if (hJobFile > 0) {
        fclose (hJobFile);
        hJobFile = 0;
    }
}


/*===========================================================================*\
\*===========================================================================*/

tBool CFaxReceive::StartReceive (void) {
    dhead ("CFaxReceive::StartReceive", DCON_CFaxReceive);
    cp_b3configuration_faxext B3Config (1, &FaxDevice->FaxNumber); //Identifier.GetPointer();
    SetB1Parameters (b1prot_G3Fax);
    SetB2Parameters (b2prot_G3Fax);
    SetB3Parameters (b3prot_G3Fax_extended, &B3Config);
    SetNCPI (0);
    return vTrue;
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::StopReceive (void) {
    dhead ("CFaxReceive::StopReceive", DCON_CFaxReceive);
    quitRecv = 1;
    StopRecv();
    ResetChannel();
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::ListenError (tUInt Controller, c_info Info) {
    dhead ("CFaxReceive::ListenError", DCON_CFaxReceive);
    if (Controller) {
        WriteLog (LOG_ERR, "Try to listen for call on Controller %d, but got error code 0x%X.\n", Controller, Info);
    } else if (CapiInfoIsRegError (Info)) {
        // Get a Register-Errror as Reason => CAPI not installed, terminate Programm
        dprint ("\nCAPI not installed! ");
        WriteLog (LOG_ERR, "CAPI not installed, started or have no access rights on it!\n");
        FaxDevice->WriteStatus ("CAPI not installed, started or have no access rights on it!!");
    } else {
        WriteLog (LOG_ERR, "Try to listen for calls but got error code 0x%X.\n", Info);
    }
    NotifyFIFO ('W');
    StopReceive();
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::IncomingCall (c_cipvalue CIPValue, CConstString *pOppositeNumber, CConstString *pMyCalledNumber) {
    dhead ("CFaxReceive::IncomingCall", DCON_CFaxReceive);
    dassert (pOppositeNumber != 0);
    dassert (pMyCalledNumber != 0);
    dparams ("%x,%s,%s", CIPValue, pOppositeNumber->GetPointer(), pMyCalledNumber->GetPointer());
    CDynamicString InfoText ("Incoming ");
    ConnectionInd();
    jobNr = 0;
    switch (CIPValue) {
    case cipvalue_Speech:
        InfoText.Append ("speech");
        break;
    case cipvalue_3_1kHzAudio:
        InfoText.Append ("analog");
        break;
    default:
        InfoText.Append ("fax");
        break;
    }
    InfoText.PrintAppend (" call on controller %d", GetActiveController());
    if (!pOppositeNumber->IsEmpty()) {
        ONumber.Set (pOppositeNumber);
        InfoText.PrintAppend (" from %S", pOppositeNumber);
    } else {
        ONumber.RemoveAll();
    }
    if (!pMyCalledNumber->IsEmpty()) {
        MyNumber.Set (pMyCalledNumber);
        InfoText.PrintAppend (" to %S", pMyCalledNumber);
    } else {
        MyNumber.RemoveAll();
    }
    if (GetFormat() == FaxFormat_Hylafax) {
        if (GetJobFile() == vFalse) {
            WriteLog (LOG_ERR, "Can't create log file for incomming call!\n");
            // TODO: better disconnect the call?
        }
        WriteLog (LOG_INFO, "SESSION BEGIN %08u %s\n", jobNr, FaxDevice->FaxNumber.GetPointer());
    }
    if (InfoText.ErrorDetected()) {
        WriteLog (LOG_ERR, "Memory Full during generation of Log output!\n");
    } else {
        dassert (InfoText.IsEmpty() == vFalse);
        dassert (InfoText.GetPointer() != 0);
        WriteLog (LOG_INFO, "%s.\n", InfoText.GetPointer());
    }
    FaxDevice->WriteStatus ("Answering the phone");
    AnswerCall (creject_Accept);
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::DoConnecting (void) {
    dhead ("CFaxReceive::DoConnecting", DCON_CFaxReceive);
    // Nothing to do!
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::ConnectProceeding (CConstString *) {
    dhead ("CFaxReceive::ConnectProceeding", DCON_CFaxReceive);
    ConnectionStarted();
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::IsConnected (cp_ncpi_all *pNCPI) {
    dhead ("CFaxReceive::IsConnected", DCON_CFaxReceive);
    ConnectLog (pNCPI);
    if (  (pNCPI)
       && (  (pNCPI->Format != 0)          // no SFF format
          || (pNCPI->Options & 0x2))) {     // fax polling not implemented yet
        dwarning (0);
        WriteLog (LOG_INFO, "Could not handle this kind of fax! Disconnect now.\n");
        Disconnect (DisconnectKind_Fast);
        return;
    }
    if (infoError (OpenRecv())) {
        WriteLog (LOG_ERR, "Can't create file for the fax. Disconnecting now!\n");
        Disconnect (DisconnectKind_Fast);
    } else {
        #ifdef GENERATE_DEBUGSFFDATAFILE
            dwarning (DebugSffDataFile == 0);
            if (!DebugSffDataFile) {
                DebugSffDataFile = fopen ("/tmp/c2faxrecv_dbgdatafile.sff", "w");
            }
        #endif
        NotifyFIFO ('I');  // Comm id
        NotifyFIFO ('B');  // Busy + inbound call
        FaxDevice->WriteStatus ("Receiving facsimile");
        NotifyFIFO ('S');
    }
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::PutDataComplete (void *, c_info) {
    dhead ("CFaxReceive::PutDataComplete", DCON_CFaxReceive);
    dwarning (0); // Should never been called!
    // Nothing to do!
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::GetData (tUByte *Data, tUShort DataLength, tUShort DataHandle) {
    dhead ("CFaxReceive::GetData", DCON_CFaxReceive);
    dassert (Data != 0);
    dwarning (DataLength > 0);
    #ifdef GENERATE_DEBUGSFFDATAFILE
        if (DebugSffDataFile) {
            fwrite (Data, DataLength, 1, DebugSffDataFile);
        }
    #endif
    if (infoError (RecvSFF (Data, DataLength))) {
        Disconnect (DisconnectKind_Fast);
    }
    GetDataComplete (DataHandle);
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::DoDisconnecting (c_info, cp_ncpi_all *pNCPI) {
    dhead ("CFaxReceive::DoDisconnecting", DCON_CFaxReceive);
    StopRecv();
    if (pNCPI) {
        if (GetPageCount() != pNCPI->Pages) {
            WriteLog (LOG_ERR, "Discrepance between CAPI's (%d) and internal (%d) number of pages!\n",
                      pNCPI->Pages, GetPageCount());
            //SetPageCount (pNCPI->Pages);
        }
    }
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::IsDisconnected (c_info Reason) {
    dhead ("CFaxReceive::IsDisconnected", DCON_CFaxReceive);
    dparams ("%x", Reason);
    CloseRecv();
    #ifdef GENERATE_DEBUGSFFDATAFILE
        if (DebugSffDataFile) {
            fclose (DebugSffDataFile);
            DebugSffDataFile = 0;
        }
    #endif

    if (GetRecvFiles() && (GetRecvFiles()->IsEmpty() == vFalse)) {
        if (FaxDevice->MainClass->FaxRcvdCmd.IsEmpty() == vFalse) {
            CDynamicString executeCommand;
            executeCommand.Set (&FaxDevice->MainClass->FaxRcvdCmd);
            char *StateText = "";

            if (GetFormat() == FaxFormat_Hylafax) {
                switch (GetRecvState()) {
                default:
                case 0:
                    StateText = "Can't build up connection for incoming call.";
                    break;
                case 1:
                    StateText = "Connection was established but no fax data was received.";
                    break;
                case 2:
                    StateText = "Connection droped unexpected during fax receiving.";
                    break;
                case 3:
                    break;
                }

                // Hylafax: <qfile> <ModemDeviceID> <CommID> <Reason> <CIDNumber> <CIDName> <destination>
                executeCommand.PrintAppend (" \"%eS\" \"%eS\" \"%08u\" \"%es\" \"%eS\" \"\" \"%eS\"",
                                            GetRecvFiles()->GetFirst(), &FaxDevice->DeviceName, jobNr,
                                            StateText, GetReceiveID(), &MyNumber);
                WriteXferLog ("RECV", jobNr, 0, 0, (char *)FaxDevice->DeviceName.GetPointer(), 0,
                              &MyNumber, StateText);
            } else  {
                // mgetty:  <RecvStatus> <Hangup Code> "<sender id>" "<receiver id>" <nr of pages> <file(s)>
                tUInt recvStatus = 0;
                switch (GetRecvState()) {
                default:
                case 0:
                    recvStatus = 6;
                    break;
                case 1:
                    recvStatus = 7;
                    break;
                case 2:
                    recvStatus = 12;
                    break;
                case 3:
                    break;
                }

                executeCommand.PrintAppend (" %u 0x%X \"", recvStatus, Reason);
                executeCommand.PrintAppend ("%eS", GetReceiveID());
                executeCommand.Append ("\" \"");
                if (!MyNumber.IsEmpty()) {
                    executeCommand.PrintAppend ("%eS", &MyNumber);
                }
                executeCommand.PrintAppend ("\" %u", GetPageCount());
                for (COneMultiString *pLauf = GetRecvFiles()->GetFirst(); (pLauf != 0); pLauf = pLauf->GetNext()) {
                    executeCommand.PrintAppend (" \"%eS\"", pLauf);
                }
            }

            if (executeCommand.ErrorDetected() == vTrue) {
                WriteLog (LOG_INFO, "Fax received and stored under %s.\n",
                        GetRecvFiles()->GetFirst()->GetPointer());
                WriteLog (LOG_ERR, "Allocating memory error! Can't notify new received fax!\n");
                FaxDevice->WriteStatus ("Allocating memory error! Can't notify new received fax!");
                NotifyFIFO ('W');
                quitRecv = 1;
            } else {
                WriteLog (LOG_INFO, "Fax received and calling '%s'.\n", executeCommand.GetPointer());
                fflush (0);
                runCmd ((char *)executeCommand.GetPointer(), vTrue);
            }
        } else {
            WriteLog (LOG_INFO, "Fax received and stored under %s.\n", GetRecvFiles()->GetFirst()->GetPointer());
        }
        GetRecvFiles()->RemoveAndDeleteAll();
    }
    WriteLog (LOG_INFO, "Connection is droped with reason 0x%X (%s).\n", Reason, capi_info2str (Reason));
    if (hJobFile > 0) {
        WriteLog (LOG_INFO, "SESSION END\n");
        fclose (hJobFile);
        hJobFile = 0;
    }
    jobNr       = 0;
    ONumber.RemoveAll();
    MyNumber.RemoveAll();
    FaxDevice->WriteStatus ("Running and idle");
    NotifyFIFO ('I');
    NotifyFIFO ('R');
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::IsReseted (void) {
    dhead ("CFaxReceive::IsReseted", DCON_CFaxReceive);
    //dassert (CapiInfoIsSuccess (Info));
    if (quitRecv == 0) {
        WriteLog (LOG_ERR, "Can't register at CAPI! Is CAPI loaded and accessible from this program?\n");
    } else {
        quitRecv = 0;
    }
    CFaxReceiveDevice *fd = FaxDevice;
    delete this;
    fd->ReceiveStoped();
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::PageEndReached (tBool /*IsLastPage*/) {
    dhead ("CFaxReceive::PageEndReached", DCON_CFaxReceive);
    NotifyFIFO ('P');
}

/*===========================================================================*\
\*===========================================================================*/

void CFaxReceive::WriteLog (tSInt priority, char *text, ...) {
    dhead ("CFaxReceive::WriteLog", DCON_CFaxReceive);
    char    timebuf[MAX_STRING_SIZE];
    timeval tv;
    va_list params;
    va_start (params, text);

    gettimeofday (&tv, 0);
    strftime (timebuf, MAX_STRING_SIZE, "%h %d %T", localtime((time_t*) &tv.tv_sec));
    sprintf (timebuf + strlen(timebuf), ".%02lu: [%5d]: ", tv.tv_usec / 10000, getpid());

    if (hJobFile) {
        fprintf (hJobFile, timebuf);
        vfprintf (hJobFile, text, params);
        va_end (params);
        va_start (params, text);
    }
    FaxDevice->vWriteLog (priority , text, params);
    va_end (params);
}

/*===========================================================================*\
\*===========================================================================*/

tUInt CFaxReceive::GetRecvFileMode (void) {
    dhead ("CFaxReceive::GetRecvFileMode", DCON_CFaxReceive);
    return FaxDevice->RecvFileMode;
}

/*===========================================================================*\
\*===========================================================================*/

CDynamicString *CFaxReceive::GetSpoolDir (void) {
    dhead ("CFaxReceive::GetSpoolDir", DCON_CFaxReceive);
    return &FaxDevice->MainClass->SpoolDir;
}

/*===========================================================================*\
\*===========================================================================*/

tBool CFaxReceive::NotifyFIFO (char Type) {
    dhead ("CFaxReceive::NotifyFIFO", DCON_CFaxReceive);
    dparams ("%c", Type);
    if (GetFormat() != FaxFormat_Hylafax) {
        return vTrue;
    }
    CDynamicString FaxInfo;
    tBool  recv = vFalse;
    char  *text = 0;
    switch (Type) {
    // Modem part
    case 'I':
        if (jobNr > 0) {
            FaxInfo.Print ("%08u", jobNr);
            text = (char *)FaxInfo.GetPointer();
        }
        break;
    case 'C':
        // TODO? MSN eintragen?
        text = (char *)ONumber.GetPointer();
        break;

    // Receive part
    case 'S':
    case 'P':
    case 'D':
        if (GetRecvFiles() && (GetRecvFiles()->IsEmpty() == vFalse)) {
            dassert (GetRecvFiles()->GetFirst()->IsEmpty() == vFalse);
            FaxInfo. Print ("%lx,%x,%x,%S,%08u,\"%S\",\"%S\",\"%s\"", (tUInt64)(time (0) - GetStartTime()),
                            GetPageCount(), FaxPropEncode (GetResolution(), GetBaudRate()),
                            GetRecvFiles()->GetFirst(), jobNr, GetReceiveID(), &ONumber, "");
            text = (char *)FaxInfo.GetPointer();
            recv = vTrue;
        } else {
            dassert (0);
            return vFalse;
        }
        break;
    default:
        return FaxDevice->NotifyFIFO (Type);
    }
    return FaxDevice->NotifyFIFO (recv, Type, text);
}

/*===========================================================================*\
\*===========================================================================*/

tBool CFaxReceive::GetJobFile (void) {
    dhead ("CFaxReceive::GetJobFile", DCON_CFaxReceive);
    dassert (GetFormat() == FaxFormat_Hylafax);
    jobNr = 0;
    if (hJobFile != 0) {
        fclose (hJobFile);
        hJobFile = 0;
    }
    char  astring[MAX_STRING_SIZE];
    tSInt len;
    tUInt tmpJobFileNr = 1;
    tSInt tmpFileD     = -1;
    tSInt SeqFile      = open (HYLAFAX_LOG_DIR HYLAFAX_SEQF_FILE, O_CREAT | O_RDWR, 0644);
    if (SeqFile >= 0) {
        flock (SeqFile, LOCK_EX);
        len = read (SeqFile, astring, MAX_STRING_SIZE);
        astring[(len <= 0) ? 0 : len] = '\0';
        tmpJobFileNr = atoi (astring) + 1;
    }
    for (tUInt trys = MAX_TRY_COUNT; trys > 0; trys--, tmpJobFileNr++) {
        if (tmpJobFileNr < 1 || tmpJobFileNr > MAX_SEQ_NUMBER) {
            tmpJobFileNr = 1;
        }
        sprintf (astring, HYLAFAX_LOG_DIR "c%08u", tmpJobFileNr);
        tmpFileD = open (astring, O_RDWR | O_CREAT | O_EXCL, FaxDevice->MainClass->LogFileMode);
        dprint ("try(%s,%u,%d) ", astring, strlen (astring), tmpFileD);
        if (tmpFileD >= 0) {
            flock (tmpFileD, LOCK_EX | LOCK_NB);
            hJobFile = fdopen (tmpFileD, "w");
            dprint ("fdopen(%x,%d) ", hJobFile, tmpFileD);
            if (hJobFile != 0) {
                jobNr = tmpJobFileNr;
                if (SeqFile >= 0) {
                    sprintf (astring , "%u", tmpJobFileNr);
                    len = strlen (astring);
                    lseek (SeqFile, 0, SEEK_SET);
                    write (SeqFile, astring, len);
                    ftruncate (SeqFile, len);
                }
                break;
            }
            close (tmpFileD);
            tmpFileD = -1;
            continue;
        }
        if (errno != EEXIST) {
            break;
        }
    }
    if (SeqFile >= 0) {
        close (SeqFile);
    }
    return (hJobFile != 0);
}

/*===========================================================================*\
\*===========================================================================*/