00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
#include "bluelink.h"
00014
00015
00016
#include <unistd.h>
00017
#include <sys/types.h>
00018
#include <sys/wait.h>
00019
00020
00021
00022
00023
00024
00025 BlueLink::BlueLink()
00026 {
00027
m_port = NULL;
00028
m_communicate =
false;
00029
m_connected =
false;
00030
00031 }
00032
00033
00034
00035
00036
00037 BlueLink::~BlueLink()
00038 {
00039 }
00040
00041
00042
00043
00044
00045
00046 v24_port_t*
BlueLink::getPort()
00047 {
00048
return m_port;
00049 }
00050
00051
00052
00053
00054
00055
00056 bool BlueLink::getState()
00057 {
00058
return m_connected;
00059 }
00060
00061
00062
00063
00064
00065
00066 int BlueLink::connectRfcomm(
const QString& comDevice,
const QString& btAddr)
00067 {
00068
00069
m_rfcommCommand =
"/usr/bin/rfcomm connect ";
00070
m_rfcommCommand.append(comDevice);
00071
m_rfcommCommand.append(
" ");
00072
m_rfcommCommand.append(btAddr);
00073
m_rfcommCommand.append(
" 1");
00074
00075
00076
const char *commandChar =
m_rfcommCommand.latin1();
00077
00078 pid_t child_pid;
00079
int child_status;
00080
00081 child_pid = fork();
00082
00083
if(child_pid == 0)
00084 {
00085
00086
00087 execl(
"/bin/sh",
"sh",
"-c", commandChar, 0);
00088
00089
00090 printf(
"Unknown command\n");
00091 exit(0);
00092 }
00093
else
00094 {
00095
00096
00097
return child_status;
00098 }
00099 }
00100
00101
00102
00103
00104
00105
00106 int BlueLink::disconnectRfcomm(
const QString &comDevice)
00107 {
00108
00109
m_rfcommCommand =
"/usr/bin/rfcomm release ";
00110
m_rfcommCommand.append(comDevice);
00111
00112
00113
const char *commandChar =
m_rfcommCommand.latin1();
00114
00115 pid_t child_pid;
00116
int child_status;
00117
00118 child_pid = fork();
00119
00120
if(child_pid == 0)
00121 {
00122
00123
00124 execl(
"/bin/sh",
"sh",
"-c", commandChar, 0);
00125
00126
00127 printf(
"Unknown command\n");
00128 exit(0);
00129 }
00130
else
00131 {
00132
00133
00134
return child_status;
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143
00144 void BlueLink::connecttoPhone(
const QString &comDevice)
00145 {
00146
m_communicate =
false;
00147
m_connected =
false;
00148
00149
00150
m_communicate =
true;
00151
00152
m_port = v24OpenPort(comDevice, V24_RTS_CTS);
00153
m_communicate =
false;
00154
00155
00156
if (
m_port==NULL)
00157 {
00158
m_connected =
false;
00159 }
00160
else
00161 {
00162
m_connected =
true;
00163
00164
00165
00166
00167
m_phoneAnswer =
talktoPhone(
m_port,
"at+cnmi=?\r");
00168
m_phoneMode =
m_phoneAnswer.join(
":");
00169
m_phoneMode =
m_phoneMode.section(
':',2,2);
00170
m_phoneMode =
m_phoneMode.section(
'(',1,1);
00171
m_phoneMode =
m_phoneMode.section(
')',0,0);
00172
00173
00174
if (
m_phoneMode ==
"2")
00175 {
00176
m_phoneAnswer = talktoPhone(
m_port,
"at+cnmi=2,1,0,0\r");
00177 }
00178
else if (
m_phoneMode ==
"3")
00179 {
00180
m_phoneAnswer = talktoPhone(
m_port,
"at+cnmi=3,1,0,0\r");
00181 }
00182 }
00183 }
00184
00185
00186
00187
00188
00189 void BlueLink::disconnectPhone()
00190 {
00191
00192 v24ClosePort(
m_port);
00193
m_port = NULL;
00194
m_communicate =
false;
00195
m_connected =
false;
00196
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 QStringList
BlueLink::talktoPhone(v24_port_t *m_port,
const QString &m_command)
00206 {
00207
00208
00209
00210
00211
00212 QStringList m_phoneTalk;
00213
00214 QString m_phoneTmp;
00215
00216
00217
00218
00219
int m_v24Value = 0;
00220
00221
00222
char ansChar[80];
00223
00224
00225
const char *cmdChar = m_command.latin1();
00226
00227
00228
00229
m_communicate =
true;
00230
00231
00232 m_v24Value = v24Puts(m_port, cmdChar);
00233
00234
00235
if (m_v24Value < strlen(cmdChar))
00236 {
00237
m_communicate =
false;
00238 m_phoneTalk <<
"ezv24Error";
00239
return m_phoneTalk;
00240 }
00241
else
00242 {
00243
while((strncmp(ansChar,
"OK", 2) != 0) && (strncmp(ansChar,
"ERROR", 5) != 0))
00244 {
00245
00246 m_v24Value = v24Gets(m_port, ansChar,
sizeof(ansChar)-1);
00247
00248
00249
if (m_v24Value == -1)
00250 {
00251
m_communicate =
false;
00252 m_phoneTalk <<
"ezv24Error";
00253
return m_phoneTalk;
00254 }
00255
else
00256 {
00257
00258
for (
int i = 0; i < m_v24Value; i++)
00259
if (!ansChar[i])
00260 {
00261 ansChar[i] = 0x80;
00262 }
00263
00264 m_phoneTmp.append(ansChar);
00265
00266
if (m_phoneTmp.right(1) ==
"\n")
00267 {
00268 m_phoneTalk << m_phoneTmp;
00269 m_phoneTmp =
"";
00270 }
00271 }
00272 }
00273
00274
m_communicate =
false;
00275
return m_phoneTalk;
00276 }
00277
00278 }
00279
00280