Fixed DevStudio 2003 build with memory check code.
[pwlib.git] / src / ptclib / modem.cxx
blob257b04f05ff099e403658e56f7311e71592084e5
1 /*
2 * modem.cxx
4 * Asynchronous Serial I/O channel class.
6 * Portable Windows Library
8 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * $Log$
27 * Revision 1.6 2002/11/06 22:47:25 robertj
28 * Fixed header comment (copyright etc)
30 * Revision 1.5 2000/01/21 17:42:53 craigs
31 * Added pragma to avoid problems when linking as Linux shared library
33 * Revision 1.4 1998/11/30 12:33:06 robertj
34 * Split serial channel and modem, modem to components library.
36 * Revision 1.3 1998/09/23 06:22:38 robertj
37 * Added open source copyright license.
39 * Revision 1.2 1996/04/15 10:57:59 robertj
40 * Moved some functions from INL to serial.cxx so unix linker can make smaller executables.
42 * Revision 1.1 1996/04/14 02:54:14 robertj
43 * Initial revision
47 #ifdef __GNUC__
48 #pragma implementation "modem.h"
49 #endif
51 #include <ptlib.h>
52 #include <ptclib/modem.h>
54 #include <ctype.h>
57 ///////////////////////////////////////////////////////////////////////////////
58 // PModem
60 PModem::PModem()
62 status = Unopened;
66 PModem::PModem(const PString & port, DWORD speed, BYTE data,
67 Parity parity, BYTE stop, FlowControl inputFlow, FlowControl outputFlow)
68 : PSerialChannel(port, speed, data, parity, stop, inputFlow, outputFlow)
70 status = IsOpen() ? Uninitialised : Unopened;
74 PModem::PModem(PConfig & cfg)
76 status = Open(cfg) ? Uninitialised : Unopened;
80 void PModem::SetInitString(const PString & str)
82 initCmd = str;
85 PString PModem::GetInitString() const
87 return initCmd;
90 void PModem::SetDeinitString(const PString & str)
92 deinitCmd = str;
95 PString PModem::GetDeinitString() const
97 return deinitCmd;
100 void PModem::SetPreDialString(const PString & str)
102 preDialCmd = str;
105 PString PModem::GetPreDialString() const
107 return preDialCmd;
110 void PModem::SetPostDialString(const PString & str)
112 postDialCmd = str;
115 PString PModem::GetPostDialString() const
117 return postDialCmd;
120 void PModem::SetBusyString(const PString & str)
122 busyReply = str;
125 PString PModem::GetBusyString() const
127 return busyReply;
130 void PModem::SetNoCarrierString(const PString & str)
132 noCarrierReply = str;
135 PString PModem::GetNoCarrierString() const
137 return noCarrierReply;
140 void PModem::SetConnectString(const PString & str)
142 connectReply = str;
145 PString PModem::GetConnectString() const
147 return connectReply;
150 void PModem::SetHangUpString(const PString & str)
152 hangUpCmd = str;
155 PString PModem::GetHangUpString() const
157 return hangUpCmd;
160 PModem::Status PModem::GetStatus() const
162 return status;
166 BOOL PModem::Close()
168 status = Unopened;
169 return PSerialChannel::Close();
173 BOOL PModem::Open(const PString & port, DWORD speed, BYTE data,
174 Parity parity, BYTE stop, FlowControl inputFlow, FlowControl outputFlow)
176 if (!PSerialChannel::Open(port,
177 speed, data, parity, stop, inputFlow, outputFlow))
178 return FALSE;
180 status = Uninitialised;
181 return TRUE;
185 static const char ModemInit[] = "ModemInit";
186 static const char ModemDeinit[] = "ModemDeinit";
187 static const char ModemPreDial[] = "ModemPreDial";
188 static const char ModemPostDial[] = "ModemPostDial";
189 static const char ModemBusy[] = "ModemBusy";
190 static const char ModemNoCarrier[] = "ModemNoCarrier";
191 static const char ModemConnect[] = "ModemConnect";
192 static const char ModemHangUp[] = "ModemHangUp";
194 BOOL PModem::Open(PConfig & cfg)
196 initCmd = cfg.GetString(ModemInit, "ATZ\\r\\w2sOK\\w100m");
197 deinitCmd = cfg.GetString(ModemDeinit, "\\d2s+++\\d2sATH0\\r");
198 preDialCmd = cfg.GetString(ModemPreDial, "ATDT");
199 postDialCmd = cfg.GetString(ModemPostDial, "\\r");
200 busyReply = cfg.GetString(ModemBusy, "BUSY");
201 noCarrierReply = cfg.GetString(ModemNoCarrier, "NO CARRIER");
202 connectReply = cfg.GetString(ModemConnect, "CONNECT");
203 hangUpCmd = cfg.GetString(ModemHangUp, "\\d2s+++\\d2sATH0\\r");
205 if (!PSerialChannel::Open(cfg))
206 return FALSE;
208 status = Uninitialised;
209 return TRUE;
213 void PModem::SaveSettings(PConfig & cfg)
215 PSerialChannel::SaveSettings(cfg);
216 cfg.SetString(ModemInit, initCmd);
217 cfg.SetString(ModemDeinit, deinitCmd);
218 cfg.SetString(ModemPreDial, preDialCmd);
219 cfg.SetString(ModemPostDial, postDialCmd);
220 cfg.SetString(ModemBusy, busyReply);
221 cfg.SetString(ModemNoCarrier, noCarrierReply);
222 cfg.SetString(ModemConnect, connectReply);
223 cfg.SetString(ModemHangUp, hangUpCmd);
227 BOOL PModem::CanInitialise() const
229 switch (status) {
230 case Unopened :
231 case Initialising :
232 case Dialling :
233 case AwaitingResponse :
234 case HangingUp :
235 case Deinitialising :
236 case SendingUserCommand :
237 return FALSE;
239 default :
240 return TRUE;
245 BOOL PModem::Initialise()
247 if (CanInitialise()) {
248 status = Initialising;
249 if (SendCommandString(initCmd)) {
250 status = Initialised;
251 return TRUE;
253 status = InitialiseFailed;
255 return FALSE;
259 BOOL PModem::CanDeinitialise() const
261 switch (status) {
262 case Unopened :
263 case Initialising :
264 case Dialling :
265 case AwaitingResponse :
266 case Connected :
267 case HangingUp :
268 case Deinitialising :
269 case SendingUserCommand :
270 return FALSE;
272 default :
273 return TRUE;
278 BOOL PModem::Deinitialise()
280 if (CanDeinitialise()) {
281 status = Deinitialising;
282 if (SendCommandString(deinitCmd)) {
283 status = Uninitialised;
284 return TRUE;
286 status = DeinitialiseFailed;
288 return FALSE;
292 BOOL PModem::CanDial() const
294 switch (status) {
295 case Unopened :
296 case Uninitialised :
297 case Initialising :
298 case InitialiseFailed :
299 case Dialling :
300 case AwaitingResponse :
301 case Connected :
302 case HangingUp :
303 case Deinitialising :
304 case DeinitialiseFailed :
305 case SendingUserCommand :
306 return FALSE;
308 default :
309 return TRUE;
314 BOOL PModem::Dial(const PString & number)
316 if (!CanDial())
317 return FALSE;
319 status = Dialling;
320 if (!SendCommandString(preDialCmd + "\\s" + number + postDialCmd)) {
321 status = DialFailed;
322 return FALSE;
325 status = AwaitingResponse;
327 PTimer timeout = 120000;
328 PINDEX connectPosition = 0;
329 PINDEX busyPosition = 0;
330 PINDEX noCarrierPosition = 0;
332 for (;;) {
333 int nextChar;
334 if ((nextChar = ReadCharWithTimeout(timeout)) < 0)
335 return FALSE;
337 if (ReceiveCommandString(nextChar, connectReply, connectPosition, 0))
338 break;
340 if (ReceiveCommandString(nextChar, busyReply, busyPosition, 0)) {
341 status = LineBusy;
342 return FALSE;
345 if (ReceiveCommandString(nextChar, noCarrierReply, noCarrierPosition, 0)) {
346 status = NoCarrier;
347 return FALSE;
351 status = Connected;
352 return TRUE;
356 BOOL PModem::CanHangUp() const
358 switch (status) {
359 case Unopened :
360 case Uninitialised :
361 case Initialising :
362 case InitialiseFailed :
363 case Dialling :
364 case AwaitingResponse :
365 case HangingUp :
366 case Deinitialising :
367 case SendingUserCommand :
368 return FALSE;
370 default :
371 return TRUE;
376 BOOL PModem::HangUp()
378 if (CanHangUp()) {
379 status = HangingUp;
380 if (SendCommandString(hangUpCmd)) {
381 status = Initialised;
382 return TRUE;
384 status = HangUpFailed;
386 return FALSE;
390 BOOL PModem::CanSendUser() const
392 switch (status) {
393 case Unopened :
394 case Uninitialised :
395 case Initialising :
396 case InitialiseFailed :
397 case Dialling :
398 case AwaitingResponse :
399 case HangingUp :
400 case Deinitialising :
401 case SendingUserCommand :
402 return FALSE;
404 default :
405 return TRUE;
410 BOOL PModem::SendUser(const PString & str)
412 if (CanSendUser()) {
413 Status oldStatus = status;
414 status = SendingUserCommand;
415 if (SendCommandString(str)) {
416 status = oldStatus;
417 return TRUE;
419 status = oldStatus;
421 return FALSE;
425 void PModem::Abort()
427 switch (status) {
428 case Initialising :
429 status = InitialiseFailed;
430 break;
431 case Dialling :
432 case AwaitingResponse :
433 status = DialFailed;
434 break;
435 case HangingUp :
436 status = HangUpFailed;
437 break;
438 case Deinitialising :
439 status = DeinitialiseFailed;
440 break;
441 default :
442 break;
447 BOOL PModem::CanRead() const
449 switch (status) {
450 case Unopened :
451 case Initialising :
452 case Dialling :
453 case AwaitingResponse :
454 case HangingUp :
455 case Deinitialising :
456 case SendingUserCommand :
457 return FALSE;
459 default :
460 return TRUE;
465 // End Of File ///////////////////////////////////////////////////////////////