atcommand.cpp
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 "atcommand.h"
00026
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029
00030 ATParameter::ATParameter()
00031 {
00032 mUserInput = false;
00033 }
00034
00035 ATParameter::ATParameter(const QString &value,const QString &name,
00036 bool userInput)
00037 {
00038 mName = name;
00039 mValue = value;
00040 mUserInput = userInput;
00041 }
00042
00043
00044 ATCommand::ATCommand()
00045 {
00046 mHexOutput = false;
00047
00048 construct();
00049 }
00050
00051 ATCommand::ATCommand(const QString &cmdString)
00052 {
00053 setCmdName(i18n("New Command"));
00054 setCmdString(cmdString);
00055 mHexOutput = false;
00056
00057 extractParameters();
00058
00059 construct();
00060 }
00061
00062 ATCommand::ATCommand(const QString &cmdName,const QString &cmdString,
00063 bool hexOutput)
00064 {
00065 setCmdName(cmdName);
00066 setCmdString(cmdString);
00067 mHexOutput = hexOutput;
00068
00069 construct();
00070 }
00071
00072 void ATCommand::construct()
00073 {
00074 mAutoDelete = false;
00075 mResultFieldsList.setAutoDelete(true);
00076 mParameters.setAutoDelete(true);
00077 }
00078
00079 ATCommand::~ATCommand()
00080 {
00081
00082 }
00083
00084
00085 void ATCommand::setCmdName(const QString &cmdName)
00086 {
00087 mCmdName = cmdName;
00088 }
00089
00090 QString ATCommand::cmdName()
00091 {
00092 return mCmdName;
00093 }
00094
00095
00096 void ATCommand::setCmdString(const QString &cmdString)
00097 {
00098 mCmdString = cmdString;
00099
00100 mId = cmdString;
00101 if (mId.startsWith("at")) mId = mId.mid(2);
00102 else mCmdString.prepend("at");
00103
00104
00105 }
00106
00107 QString ATCommand::cmdString()
00108 {
00109 return mCmdString;
00110 }
00111
00112 QString ATCommand::cmd()
00113 {
00114 if (mParameters.count() > 0) {
00115 QString cmd = cmdString().left(cmdString().find("=") + 1);
00116
00117 for(uint i=0;i<mParameters.count();++i) {
00118 cmd += mParameters.at(i)->value();
00119 if (i < mParameters.count() - 1) cmd += ",";
00120 }
00121
00122 return cmd;
00123 } else {
00124 return cmdString();
00125 }
00126 }
00127
00128 QString ATCommand::id()
00129 {
00130 return mId;
00131 }
00132
00133 void ATCommand::setHexOutput(bool hexOutput)
00134 {
00135 mHexOutput = hexOutput;
00136 }
00137
00138 bool ATCommand::hexOutput()
00139 {
00140 return mHexOutput;
00141 }
00142
00143 void ATCommand::setResultString(const QString &resultString)
00144 {
00145 mResultString = resultString;
00146
00147 mResultFieldsList.clear();
00148
00149 QStringList resultFields = QStringList::split("\n",mResultString);
00150
00151 for(QStringList::Iterator it = resultFields.begin();
00152 it != resultFields.end(); ++it) {
00153 setResultFields(*it);
00154 }
00155 }
00156
00157 void ATCommand::setResultFields(QString fieldsString)
00158 {
00159 QString id = mId.upper().left(mId.find('='));
00160
00161
00162
00163
00164 if (fieldsString.startsWith(id)) {
00165 fieldsString = fieldsString.mid(id.length() + 2);
00166 }
00167
00168 QStringList *fields = new QStringList;
00169
00170 *fields = QStringList::split(',',fieldsString);
00171
00172 mResultFieldsList.append(fields);
00173
00174
00175
00176
00177
00178
00179
00180
00181 }
00182
00183 QString ATCommand::resultString()
00184 {
00185 return mResultString;
00186 }
00187
00188 QString ATCommand::resultField(int index)
00189 {
00190 if (mResultFieldsList.count() == 0) return "";
00191
00192 QStringList *resultFields = mResultFieldsList.at(0);
00193
00194 QStringList::Iterator it = resultFields->at(index);
00195 if (it == resultFields->end()) {
00196 kdDebug() << "ATCommand::resultField: index " << index << " out of range."
00197 << endl;
00198 return "";
00199 }
00200
00201 return *it;
00202 }
00203
00204
00205 QPtrList<QStringList> *ATCommand::resultFields()
00206 {
00207 return &mResultFieldsList;
00208 }
00209
00210 void ATCommand::addParameter(ATParameter *p)
00211 {
00212 mParameters.append(p);
00213 }
00214
00215 void ATCommand::clearParameters()
00216 {
00217 mParameters.clear();
00218 }
00219
00220 QPtrList<ATParameter> ATCommand::parameters()
00221 {
00222 return mParameters;
00223 }
00224
00225 void ATCommand::setParameter(int index,const QString &value)
00226 {
00227 if (mParameters.count() <= (unsigned int)index) {
00228 kdDebug() << "ATCommand " << cmdName() << " has no Parameter " << index
00229 << endl;
00230 return;
00231 }
00232
00233 mParameters.at(index)->setValue(value);
00234 }
00235
00236 void ATCommand::setParameter(int index,int value)
00237 {
00238 setParameter(index,QString::number(value));
00239 }
00240
00241 QString ATCommand::processOutput(const QString &output)
00242 {
00243 if (hexOutput()) {
00244 QString hexString = output.mid(output.find('\n')+1);
00245 int i=0;
00246 QString aChar = hexString.mid(i,2);
00247 QString result;
00248 while(!aChar.isEmpty()) {
00249 int charValue = aChar.toInt(0,16);
00250 QChar charEncoded(charValue);
00251
00252 result += charEncoded;
00253 i += 2;
00254 aChar = hexString.mid(i,2);
00255 }
00256 result += "\n";
00257 return result;
00258 } else {
00259 return output;
00260 }
00261 }
00262
00263 QString ATCommand::processOutput()
00264 {
00265 return processOutput(mResultString);
00266 }
00267
00268 void ATCommand::extractParameters()
00269 {
00270
00271
00272 int pos = cmdString().find("=");
00273 if (pos < 0) return;
00274
00275 QString paraString = cmdString().mid(pos+1);
00276
00277 QStringList paraList = QStringList::split(",",paraString);
00278
00279 QStringList::ConstIterator it = paraList.begin();
00280 QStringList::ConstIterator end = paraList.end();
00281 int argNum = 1;
00282 while(it != end) {
00283 addParameter(new ATParameter(*it,i18n("Arg %1").arg(QString::number(argNum++)),
00284 false));
00285 ++it;
00286 }
00287 }
This file is part of the documentation for kandy Library Version 3.2.2.