summaryrefslogtreecommitdiff
path: root/noncore/unsupported/gsmtool/gsmtool.cpp
blob: 827346106032f592cb0715780cce3141a0f7d68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "gsmtool.h"
#include <qpushbutton.h>
#include <qcombobox.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qtabwidget.h>
#include <qlistview.h>
#include <qtextbrowser.h>
#include <qmultilineedit.h>

#include <termios.h>

#include <gsmlib/gsm_me_ta.h>
#include <gsmlib/gsm_unix_serial.h>
#include <gsmlib/gsm_sms.h>
#include <gsmlib/gsm_sorted_sms_store.h>

using namespace gsmlib;


/* 
 *  Constructs a GSMTool which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 */
GSMTool::GSMTool( QWidget* parent,  const char* name, WFlags fl )
    : GSMToolBase( parent, name, fl )
{
	devicelocked = 0;
	me = NULL;
	sms_store = NULL;
	setConnected(FALSE);
	/* FIXME: Persistent settings for device/baudrate */
	connect(ConnectButton, SIGNAL(clicked()), this, SLOT(doConnectButton()));
	connect(ScanButton, SIGNAL(clicked()), this, SLOT(doScanButton()));
	connect(TabWidget2, SIGNAL(currentChanged(QWidget *)), this, SLOT(doTabChanged()));
	connect(SMSStoreList, SIGNAL(activated(int)), this, SLOT(doSMSStoreChanged()));
	connect(SMSViewType, SIGNAL(activated(int)), this, SLOT(doSMSTypeChanged()));
	connect(SMSList, SIGNAL(selectionChanged(QListViewItem *)), this, SLOT(doSelectedSMSChanged(QListViewItem *)));
	timerid = -1; // Is this not possible normally? 
}

/*  
 *  Destroys the object and frees any allocated resources
 */
GSMTool::~GSMTool()
{
    // no need to delete child widgets, Qt does it all for us
	if (devicelocked)
		unlockDevice();
}
const speed_t GSMTool::baudrates[12] = {
	B300, B600, B1200, B2400, B4800, B9600, B19200,
	B38400, B57600, B115200, B230400, B460800
};

int GSMTool::lockDevice( )
{
	devicelocked = 1;
	/* FIXME */
	return 0;
}

void GSMTool::unlockDevice( )
{
	devicelocked = 0;
}

void GSMTool::setConnected( bool conn )
{
	TabWidget2->setTabEnabled(tab_2, conn);
	TabWidget2->setTabEnabled(tab_3, conn);
	MfrLabel->setEnabled(conn);
	MfrText->setEnabled(conn);
	ModelLabel->setEnabled(conn);
	ModelText->setEnabled(conn);
	RevisionLabel->setEnabled(conn);
	RevisionText->setEnabled(conn);
	SerialLabel->setEnabled(conn);
	SerialText->setEnabled(conn);

}
void GSMTool::doTabChanged()
{
	int index = TabWidget2->currentPageIndex();
	qDebug("tab changed to %d", index);
	
	if (index == 1) {
		timerid = startTimer(5000);
		timerEvent(NULL);
	} else if (timerid != -1) {
		killTimer(timerid);
		timerid = -1;
	}
}

void GSMTool::timerEvent( QTimerEvent * )
{
	OPInfo opi;

	opi = me->getCurrentOPInfo();

	if (opi._numericName == NOT_SET) {
		NetStatText->setText("No network");
		NetworkLabel->setEnabled(FALSE);
		NetworkText->setEnabled(FALSE);
		NetworkText->setText("");
		SigStrText->setEnabled(FALSE);
		SigStrText->setText("");
		dB->setEnabled(FALSE);
		SigStrLabel->setEnabled(FALSE);
	} else {
		// FIXME: Add 'roaming' info from AT+CFUN
		qDebug("network");
		NetStatText->setText("Registered");
		NetworkLabel->setEnabled(TRUE);
		NetworkText->setEnabled(TRUE);
		NetworkText->setText(opi._longName.c_str());
		SigStrText->setEnabled(TRUE);

		qDebug("get sig str");
		int csq = me->getSignalStrength();
		if (csq == 0) {
			SigStrText->setText("<= -113");
			dB->setEnabled(TRUE);
			SigStrLabel->setEnabled(TRUE);
		} else if (csq == 99) {
			SigStrText->setText("Unknown");
			dB->setEnabled(FALSE);
			SigStrLabel->setEnabled(FALSE);
		} else {
			char buf[6];
			sprintf(buf, "%d", -113 + (2*csq));
			SigStrText->setText(buf);
			dB->setEnabled(TRUE);
			SigStrLabel->setEnabled(TRUE);
		}
	}	
}

void GSMTool::doSMSStoreChanged()
{
	const char *storename = SMSStoreList->currentText().ascii();
	qDebug("Store Changed to '%s'", storename);
	if (!strcmp(storename, "None")) {
		sms_store = NULL;
	} else try {
		sms_store = me->getSMSStore(storename);

		qDebug("got store of size %d", sms_store->size());
	} catch (GsmException) {
		sms_store = NULL;
		qDebug("get store failed");
	}
	
	SMSList->setEnabled(!(sms_store == NULL));
	doSMSTypeChanged();
}

void GSMTool::doSMSTypeChanged()
{
	int direction = SMSViewType->currentItem();
	qDebug("direction %s\n", direction?"outgoing":"incoming");

	SMSList->clear();
	doSelectedSMSChanged(NULL);

	if (sms_store == NULL)
		return;
	for (int i = 0; i < sms_store->size(); i++) {
		qDebug("Message %d", i);
		qDebug("Is%sempty", sms_store()[i].empty()?" ":" not ");
		if (sms_store()[i].empty())
			continue;

		qDebug("Status %d", sms_store()[i].status());
		SMSMessageRef message = sms_store()[i].message();
		qDebug("Got message.");
		
		//		qDebug(message->toString().c_str());
		if (direction == message->messageType()) {
			qDebug("yes\n");
			char buf[3];
			snprintf(buf, 3,  "%d", i);
			new QListViewItem(SMSList, message->address()._number.c_str(), message->serviceCentreTimestamp().toString().c_str(), buf);
		} 
	}		
}

void GSMTool::doSelectedSMSChanged(QListViewItem *item)
{
	qDebug("message changed\n");

	if (!item || sms_store == NULL) {
		SMSText->setText("");
		SMSText->setEnabled(FALSE);
		SMSDeleteButton->setEnabled(FALSE);
		SMSSendButton->setEnabled(FALSE);
		return;
	}
	/* ARGH. This sucks. Surely there's an app-private pointer in the
	   QListViewItem that I've failed to notice? 

	SMSMessageRef message = *(SMSMessageRef*)item->private;   
	*/
	qDebug("item %p\n", item);

	qDebug("text(2) is %s\n", item->text(2).ascii());
	int index = atoi(item->text(2).ascii());
	qDebug("index %d\n", index);
	SMSMessageRef message = sms_store()[index].message();

	SMSText->setText(message->userData().c_str());
	SMSText->setEnabled(TRUE);
	SMSDeleteButton->setEnabled(TRUE);
	SMSSendButton->setEnabled(TRUE);

}
void GSMTool::doScanButton()
{
	qDebug("ScanButton");

	NetworkList->setEnabled(FALSE);
	AvailNetsLabel->setEnabled(FALSE);
	NetworkList->clear();
	new QListViewItem(NetworkList, "Scanning...");

	vector<OPInfo> opis;

	try {
		opis = me->getAvailableOPInfo();
	} catch (GsmException) {
		NetworkList->clear();
		new QListViewItem(NetworkList, "Scan failed...");
		return;
	}
	
	NetworkList->clear();
	for (vector<OPInfo>::iterator i = opis.begin(); i != opis.end(); ++i) {
		char *statustext;
		switch (i->_status) {

		case UnknownOPStatus:
			statustext = "unknown";
			break;
			
		case CurrentOPStatus: 
			statustext = "current";
			break;

		case AvailableOPStatus:
			statustext = "available";
			break;

		case ForbiddenOPStatus:
			statustext = "forbidden";
			break;
		       
		default:
			statustext = "(ERROR)";
		}
		char num[7];
		snprintf(num, 6, "%d", i->_numericName);
		new QListViewItem(NetworkList, i->_longName.c_str(), statustext, num, i->_shortName.c_str());
      }
	NetworkList->setEnabled(TRUE);
	AvailNetsLabel->setEnabled(TRUE);
}
/*
 *  A simple slot... not very interesting.
 */
void GSMTool::doConnectButton()
{
	gsmlib::Port *port;

	speed_t rate;
	devicename = strdup(DeviceName->currentText().local8Bit().data());
	rate = baudrates[BaudRate->currentItem()];

	qDebug("Connect Button Pressed");
	MfrText->setText("Opening...");
	ModelText->setText("");
	RevisionText->setText("");
	SerialText->setText("");

	setConnected(FALSE);
	if (me) {
		me = NULL;
	}

	if (lockDevice()) {
		qDebug("lockDevice() failed\n");
		MfrText->setText("Lock port failed");
	};
	
	qDebug("Device name is %s\n", devicename);

	try {
		port = new UnixSerialPort(devicename, rate, DEFAULT_INIT_STRING, 0);
	} catch (GsmException) {
		qDebug("port failed");
		MfrText->setText("Open port failed");
		return;
	}
	MfrText->setText("Initialising...");
	qDebug("got port");
	try {
		me = new MeTa(port);
	} catch (GsmException) {
		qDebug("meta failed");
		MfrText->setText("Initialise GSM unit failed");
		me = NULL;
		unlockDevice();
		return;
	}

	qDebug("Opened");

	MEInfo ifo;

	MfrText->setText("Querying...");

	try {
		ifo = me->getMEInfo();
	} catch (GsmException) {
		qDebug("getMEInfo failed");
		MfrText->setText("Query GSM unit failed");
		me = NULL;
		unlockDevice();
		return;
	}

	MfrText->setText(ifo._manufacturer.c_str());
	ModelText->setText(ifo._model.c_str());
	RevisionText->setText(ifo._revision.c_str());
	SerialText->setText(ifo._serialNumber.c_str());
	setConnected(TRUE);

	SMSStoreList->clear();
	SMSStoreList->insertItem("None");

	vector<string> storenames = me->getSMSStoreNames();

	for (vector<string>::iterator i = storenames.begin(); i != storenames.end(); ++i) {
		SMSStoreList->insertItem(i->c_str());
	}
	SMSList->clear();
	SMSText->setText("");
	sms_store = NULL;
}