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
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
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
48 #pragma implementation "modem.h"
52 #include <ptclib/modem.h>
57 ///////////////////////////////////////////////////////////////////////////////
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
)
85 PString
PModem::GetInitString() const
90 void PModem::SetDeinitString(const PString
& str
)
95 PString
PModem::GetDeinitString() const
100 void PModem::SetPreDialString(const PString
& str
)
105 PString
PModem::GetPreDialString() const
110 void PModem::SetPostDialString(const PString
& str
)
115 PString
PModem::GetPostDialString() const
120 void PModem::SetBusyString(const PString
& str
)
125 PString
PModem::GetBusyString() const
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
)
145 PString
PModem::GetConnectString() const
150 void PModem::SetHangUpString(const PString
& str
)
155 PString
PModem::GetHangUpString() const
160 PModem::Status
PModem::GetStatus() const
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
))
180 status
= Uninitialised
;
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
))
208 status
= Uninitialised
;
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
233 case AwaitingResponse
:
235 case Deinitialising
:
236 case SendingUserCommand
:
245 BOOL
PModem::Initialise()
247 if (CanInitialise()) {
248 status
= Initialising
;
249 if (SendCommandString(initCmd
)) {
250 status
= Initialised
;
253 status
= InitialiseFailed
;
259 BOOL
PModem::CanDeinitialise() const
265 case AwaitingResponse
:
268 case Deinitialising
:
269 case SendingUserCommand
:
278 BOOL
PModem::Deinitialise()
280 if (CanDeinitialise()) {
281 status
= Deinitialising
;
282 if (SendCommandString(deinitCmd
)) {
283 status
= Uninitialised
;
286 status
= DeinitialiseFailed
;
292 BOOL
PModem::CanDial() const
298 case InitialiseFailed
:
300 case AwaitingResponse
:
303 case Deinitialising
:
304 case DeinitialiseFailed
:
305 case SendingUserCommand
:
314 BOOL
PModem::Dial(const PString
& number
)
320 if (!SendCommandString(preDialCmd
+ "\\s" + number
+ postDialCmd
)) {
325 status
= AwaitingResponse
;
327 PTimer timeout
= 120000;
328 PINDEX connectPosition
= 0;
329 PINDEX busyPosition
= 0;
330 PINDEX noCarrierPosition
= 0;
334 if ((nextChar
= ReadCharWithTimeout(timeout
)) < 0)
337 if (ReceiveCommandString(nextChar
, connectReply
, connectPosition
, 0))
340 if (ReceiveCommandString(nextChar
, busyReply
, busyPosition
, 0)) {
345 if (ReceiveCommandString(nextChar
, noCarrierReply
, noCarrierPosition
, 0)) {
356 BOOL
PModem::CanHangUp() const
362 case InitialiseFailed
:
364 case AwaitingResponse
:
366 case Deinitialising
:
367 case SendingUserCommand
:
376 BOOL
PModem::HangUp()
380 if (SendCommandString(hangUpCmd
)) {
381 status
= Initialised
;
384 status
= HangUpFailed
;
390 BOOL
PModem::CanSendUser() const
396 case InitialiseFailed
:
398 case AwaitingResponse
:
400 case Deinitialising
:
401 case SendingUserCommand
:
410 BOOL
PModem::SendUser(const PString
& str
)
413 Status oldStatus
= status
;
414 status
= SendingUserCommand
;
415 if (SendCommandString(str
)) {
429 status
= InitialiseFailed
;
432 case AwaitingResponse
:
436 status
= HangUpFailed
;
438 case Deinitialising
:
439 status
= DeinitialiseFailed
;
447 BOOL
PModem::CanRead() const
453 case AwaitingResponse
:
455 case Deinitialising
:
456 case SendingUserCommand
:
465 // End Of File ///////////////////////////////////////////////////////////////