-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialupAlerter.cpp
285 lines (252 loc) · 8.06 KB
/
DialupAlerter.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DialupAlerter.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TDialupAlerter *)
{
new TDialupAlerter(NULL);
}
//---------------------------------------------------------------------------
__fastcall TDialupAlerter::TDialupAlerter(TComponent* Owner)
: TComponent(Owner)
{
FOnNoLineAvailable = NULL;
FOnEventNotification = NULL;
FActive = false;
FConnected = false;
FLineDeviceID = -1;
APIVersion = TAPI_CURRENT_VERSION;
LinkCount = 0;
LastErrorMsg = new AnsiString();
MyTAPIHandle = (LPHLINEAPP)malloc(sizeof(HLINEAPP));
LineMsg = (LPLINEMESSAGE)malloc(sizeof(LINEMESSAGE));
InitParms = (LPLINEINITIALIZEEXPARAMS)malloc(sizeof(LINEINITIALIZEEXPARAMS));
InitParms->dwTotalSize = sizeof(LINEINITIALIZEEXPARAMS);
InitParms->dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
CallInfo = (LPLINECALLINFO)malloc(sizeof(LINECALLINFO));
CallInfo->dwTotalSize = sizeof(LINECALLINFO);
}
//---------------------------------------------------------------------------
__fastcall TDialupAlerter::~TDialupAlerter()
{
Shutdown();
if (CallInfo != NULL)
free(CallInfo);
if (InitParms != NULL)
free(InitParms);
if (LineMsg != NULL)
free(LineMsg);
if (MyTAPIHandle != NULL)
free(MyTAPIHandle);
delete LastErrorMsg;
}
//---------------------------------------------------------------------------
namespace Dialupalerter
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TDialupAlerter)};
RegisterComponents("Celestial", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::GetErrorMessage(AnsiString Tag, int res) {
*LastErrorMsg = Tag;
if (res == 0)
*LastErrorMsg = *LastErrorMsg + ": ";
else
*LastErrorMsg = *LastErrorMsg + ": TAPI ERROR: ";
switch (res) {
case 0:
*LastErrorMsg = *LastErrorMsg + "SUCCESS";
break;
case LINEERR_INVALAPPNAME:
*LastErrorMsg = *LastErrorMsg + "Invalid AppName";
break;
case LINEERR_OPERATIONFAILED:
*LastErrorMsg = *LastErrorMsg + "Operation failed";
break;
case LINEERR_INIFILECORRUPT:
*LastErrorMsg = *LastErrorMsg + "INI file corrupt";
break;
case LINEERR_INVALPOINTER:
*LastErrorMsg = *LastErrorMsg + "Invalid pointer";
break;
case LINEERR_REINIT:
*LastErrorMsg = *LastErrorMsg + "Re-initiallized";
break;
case LINEERR_NOMEM:
*LastErrorMsg = *LastErrorMsg + "Out of memory";
break;
case LINEERR_INVALPARAM:
*LastErrorMsg = *LastErrorMsg + "Invalid parameter";
break;
case LINEERR_ALLOCATED:
*LastErrorMsg = *LastErrorMsg + "Allocated";
break;
case LINEERR_LINEMAPPERFAILED:
*LastErrorMsg = *LastErrorMsg + "LineMapper failed";
break;
case LINEERR_BADDEVICEID:
*LastErrorMsg = *LastErrorMsg + "Bad device ID";
break;
case LINEERR_NODRIVER:
*LastErrorMsg = *LastErrorMsg + "No driver";
break;
case LINEERR_INCOMPATIBLEAPIVERSION:
*LastErrorMsg = *LastErrorMsg + "Incompatible API version";
break;
case LINEERR_INCOMPATIBLEEXTVERSION:
*LastErrorMsg = *LastErrorMsg + "Incompatible extension version";
break;
case LINEERR_RESOURCEUNAVAIL:
*LastErrorMsg = *LastErrorMsg + "Resource unavailable";
break;
case LINEERR_INVALMEDIAMODE:
*LastErrorMsg = *LastErrorMsg + "Invalid media mode";
break;
case LINEERR_STRUCTURETOOSMALL:
*LastErrorMsg = *LastErrorMsg + "Structure too small";
break;
case LINEERR_UNINITIALIZED:
*LastErrorMsg = *LastErrorMsg + "Uninitialized";
break;
case LINEERR_INVALPRIVSELECT:
*LastErrorMsg = *LastErrorMsg + "Invalid privilege selection";
break;
case LINEERR_NODEVICE:
*LastErrorMsg = *LastErrorMsg + "No device";
break;
case LINEERR_OPERATIONUNAVAIL:
*LastErrorMsg = *LastErrorMsg + "Operation unavailable";
break;
default:
*LastErrorMsg = *LastErrorMsg + "Unknown error";
}
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::Startup() {
LastErrorCode = lineInitializeEx(MyTAPIHandle, NULL,
NULL, Application->Title.c_str(), &NumDevices, &APIVersion, InitParms);
GetErrorMessage("INIT", LastErrorCode);
if (LastErrorCode == 0) {
EventHandle = InitParms->Handles.hEvent;
LinkCount++;
if (FLineDeviceID != -1) {
for (unsigned long i = 0; i < NumDevices; i++) {
OpenLine(i);
if (FLineDeviceID != -1) {
SetStatusChangeRequests(LineHandle);
if (LastErrorCode != 0)
CloseLine(LineHandle);
else
break;
}
}
}
}
else
GetErrorMessage("INIT", LastErrorCode);
FActive = (FLineDeviceID != -1);
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::Shutdown() {
CloseLine(LineHandle);
do {
LastErrorCode = lineShutdown(*MyTAPIHandle);
GetErrorMessage("SHUT", LastErrorCode);
LinkCount--;
} while (LinkCount > 0);
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::OpenLine(int Dev) {
int id = -1;
HLINE linehandle;
LastErrorCode = lineOpen(*MyTAPIHandle, Dev, &linehandle,
APIVersion, 0, NULL, LINECALLPRIVILEGE_MONITOR, 0, NULL);
GetErrorMessage("OPEN" + IntToStr(Dev), LastErrorCode);
if (LastErrorCode == 0) {
id = Dev;
LineHandle = linehandle;
}
FLineDeviceID = id;
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::CloseLine(HLINE handle) {
if (FLineDeviceID != -1) {
LastErrorCode = lineClose(handle);
GetErrorMessage("CLOS", LastErrorCode);
FLineDeviceID = -1;
}
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::SetStatusChangeRequests(HLINE l) {
LastErrorCode = lineSetStatusMessages(l,
LINEDEVSTATE_CONNECTED &
LINEDEVSTATE_DISCONNECTED &
LINEDEVSTATE_INSERVICE &
LINEDEVSTATE_OUTOFSERVICE &
LINEDEVSTATE_OPEN &
LINEDEVSTATE_CLOSE,
0);
GetErrorMessage("SETS", LastErrorCode);
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::CheckMessages() {
AnsiString *tx = new AnsiString();
LastErrorCode = lineGetMessage(*MyTAPIHandle, LineMsg, 0);
if (LastErrorCode == 0) {
switch (LineMsg->dwMessageID) {
case LINEDEVSTATE_CONNECTED:
*tx = "Connected";
FConnected = true;
break;
case LINEDEVSTATE_DISCONNECTED:
*tx = "Disconnected";
FConnected = false;
break;
case LINEDEVSTATE_INSERVICE:
*tx = "In-service";
break;
case LINEDEVSTATE_OUTOFSERVICE:
*tx = "Out-of-service";
break;
case LINEDEVSTATE_OPEN:
*tx = "Line open";
break;
case LINEDEVSTATE_CLOSE:
*tx = "Line closed";
break;
default:
*tx = "Unknown";
}
if (FOnEventNotification != NULL)
FOnEventNotification(this, tx);
}
delete tx;
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::SetActive(bool act) {
if (act) {
Startup();
if (FLineDeviceID == -1)
if (FOnNoLineAvailable != NULL)
FOnNoLineAvailable(this);
}
else
Shutdown();
}
//---------------------------------------------------------------------------
void __fastcall TDialupAlerter::SetLineDeviceID(int lid) {
if (FLineDeviceID != lid) {
if (FLineDeviceID != -1)
CloseLine(LineHandle);
OpenLine(lid);
}
}
//---------------------------------------------------------------------------