kandy

commandscheduler.cpp
1/*
2 This file is part of Kandy.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include <kdebug.h>
26#include <tdelocale.h>
27
28#include "modem.h"
29
30#include "commandscheduler.h"
31#include "commandscheduler.moc"
32
33CommandScheduler::CommandScheduler(Modem *modem,TQObject *parent,
34 const char *name) :
35 TQObject(parent,name),
36 mModem(modem)
37{
38 connect(mModem,TQ_SIGNAL(gotLine(const char *)),
39 TQ_SLOT(processOutput(const char *)));
40}
41
42void CommandScheduler::execute(ATCommand *command)
43{
44 if (!mModem->isOpen()) {
45 kdDebug(5960) << "Warning! Modem not open." << endl;
46 return;
47 }
48
49 mCommandQueue.append(command);
50
51// if (mCommandQueue.count() == 1) sendCommand(command->cmdString());
52 if (mCommandQueue.count() == 1) sendCommand(command->cmd());
53}
54
55void CommandScheduler::execute(const TQString &command)
56{
57 ATCommand *cmd = new ATCommand("",command);
58 cmd->setAutoDelete(true);
59
60 execute(cmd);
61}
62
63void CommandScheduler::executeId(const TQString &id)
64{
65 TQPtrList<ATCommand> *cmds = mCommandSet.commandList();
66
67 for(uint i=0;i<cmds->count();++i) {
68 if (cmds->at(i)->id() == id) {
69 execute(cmds->at(i));
70 return;
71 }
72 }
73 kdDebug(5960) << "CommandScheduler::executeId(): Id '" << id << "' not found" << endl;
74}
75
76void CommandScheduler::sendCommand(const TQString &command)
77{
78 if (command.isEmpty()) {
79 kdDebug(5960) << "CommandScheduler::sendCommand(): Warning! Empty command."
80 << endl;
81 return;
82 }
83
84 kdDebug(5960) << "CommandScheduler:sendCommand(): " << command << endl;
85
86 mModem->writeLine(command.latin1());
87}
88
89
90void CommandScheduler::processOutput(const char *line)
91{
92 TQString l = line;
93 ATCommand *cmd = mCommandQueue.first();
94 if (l == "OK") {
95 mState = WAITING;
96 emit result(mResult);
97 cmd->setResultString(mResult);
98 emit commandProcessed(cmd);
99 nextCommand();
100 } else if (l == "ERROR") {
101 mState = WAITING;
102 emit result(i18n("Error"));
103 nextCommand();
104 } else {
105 if (mState == WAITING) {
106 mState = PROCESSING;
107 mResult = "";
108 } else if (mState == PROCESSING) {
109 if (!l.isEmpty()) {
110 mResult += l;
111 mResult += "\n";
112 }
113 }
114 }
115}
116
117void CommandScheduler::nextCommand()
118{
119 if (mCommandQueue.first()->autoDelete()) delete mCommandQueue.first();
120 mCommandQueue.removeFirst();
121 if (mCommandQueue.count() > 0) {
122 sendCommand(mCommandQueue.first()->cmd());
123 }
124}
125
126bool CommandScheduler::loadProfile(const TQString& filename)
127{
128 mCommandSet.clear();
129
130 if (!mCommandSet.loadFile(filename)) return false;
131
132 return true;
133}
134
135bool CommandScheduler::saveProfile(const TQString& filename)
136{
137 if (!mCommandSet.saveFile(filename)) return false;
138
139 return true;
140}
This class provides an abstraction of an AT command.
Definition: atcommand.h:54