00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
#include <kdebug.h>
00026
#include <klocale.h>
00027
00028
#include "modem.h"
00029
00030
#include "commandscheduler.h"
00031
#include "commandscheduler.moc"
00032
00033 CommandScheduler::CommandScheduler(Modem *modem,QObject *parent,
00034
const char *name) :
00035 QObject(parent,name),
00036 mModem(modem)
00037 {
00038 connect(mModem,SIGNAL(gotLine(
const char *)),
00039 SLOT(processOutput(
const char *)));
00040 }
00041
00042
void CommandScheduler::execute(
ATCommand *command)
00043 {
00044
if (!mModem->isOpen()) {
00045 kdDebug() <<
"Warning! Modem not open." << endl;
00046
return;
00047 }
00048
00049 mCommandQueue.append(command);
00050
00051
00052
if (mCommandQueue.count() == 1) sendCommand(command->
cmd());
00053 }
00054
00055
void CommandScheduler::execute(
const QString &command)
00056 {
00057
ATCommand *cmd =
new ATCommand(
"",command);
00058 cmd->
setAutoDelete(
true);
00059
00060 execute(cmd);
00061 }
00062
00063
void CommandScheduler::executeId(
const QString &
id)
00064 {
00065 QPtrList<ATCommand> *cmds = mCommandSet.commandList();
00066
00067
for(uint i=0;i<cmds->count();++i) {
00068
if (cmds->at(i)->id() ==
id) {
00069 execute(cmds->at(i));
00070
return;
00071 }
00072 }
00073 kdDebug() <<
"CommandScheduler::executeId(): Id '" <<
id <<
"' not found" << endl;
00074 }
00075
00076
void CommandScheduler::sendCommand(
const QString &command)
00077 {
00078
if (command.isEmpty()) {
00079 kdDebug() <<
"CommandScheduler::sendCommand(): Warning! Empty command."
00080 << endl;
00081
return;
00082 }
00083
00084 kdDebug() <<
"CommandScheduler:sendCommand(): " << command << endl;
00085
00086 mModem->writeLine(command.latin1());
00087 }
00088
00089
00090
void CommandScheduler::processOutput(
const char *line)
00091 {
00092 QString l = line;
00093
ATCommand *cmd = mCommandQueue.first();
00094
if (l ==
"OK") {
00095 mState = WAITING;
00096 emit result(mResult);
00097 cmd->
setResultString(mResult);
00098 emit commandProcessed(cmd);
00099 nextCommand();
00100 }
else if (l ==
"ERROR") {
00101 mState = WAITING;
00102 emit result(i18n(
"Error"));
00103 nextCommand();
00104 }
else {
00105
if (mState == WAITING) {
00106 mState = PROCESSING;
00107 mResult =
"";
00108 }
else if (mState == PROCESSING) {
00109
if (!l.isEmpty()) {
00110 mResult += l;
00111 mResult +=
"\n";
00112 }
00113 }
00114 }
00115 }
00116
00117
void CommandScheduler::nextCommand()
00118 {
00119
if (mCommandQueue.first()->autoDelete())
delete mCommandQueue.first();
00120 mCommandQueue.removeFirst();
00121
if (mCommandQueue.count() > 0) {
00122 sendCommand(mCommandQueue.first()->cmd());
00123 }
00124 }
00125
00126
bool CommandScheduler::loadProfile(
const QString& filename)
00127 {
00128 mCommandSet.clear();
00129
00130
if (!mCommandSet.loadFile(filename))
return false;
00131
00132
return true;
00133 }
00134
00135
bool CommandScheduler::saveProfile(
const QString& filename)
00136 {
00137
if (!mCommandSet.saveFile(filename))
return false;
00138
00139
return true;
00140 }