summaryrefslogtreecommitdiff
path: root/gatosocket.h
blob: 56760eebece01a2d5b8a52d81b3c58b0a8b24ae6 (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
#ifndef GATOSOCKET_H
#define GATOSOCKET_H

#include <QtCore/QObject>
#include <QtCore/QQueue>
#include <QtCore/QSocketNotifier>

#include "gatoaddress.h"

/** This class encapsulates a message-oriented bluetooth L2CAP socket. */
class GatoSocket : public QObject
{
	Q_OBJECT
	Q_ENUMS(State)

public:
	explicit GatoSocket(QObject *parent);
	~GatoSocket();

	enum State {
		StateDisconnected,
		StateConnecting,
		StateConnected
	};

	enum Error {
		TimeoutError,
		UnknownError
	};

	enum SecurityLevel {
		SecurityNone,
		SecurityLow,
		SecurityMedium,
		SecurityHigh
	};

	State state() const;

	bool connectTo(const GatoAddress &addr, unsigned short cid);
	void close();

	/** Dequeues a pending message from the rx queue.
	 *  Doesn't block: if there are no pending messages, returns null QByteArray. */
	QByteArray receive();
	/** Adds a message to the tx queue. */
	void send(const QByteArray &pkt);

	SecurityLevel securityLevel() const;
	bool setSecurityLevel(SecurityLevel level);

signals:
	void connected();
	void disconnected();
	void error(Error error);
	void readyRead();

private:
	bool transmit(const QByteArray &pkt);

private slots:
	void readNotify();
	void writeNotify();

private:
	State s;
	int fd;
	QSocketNotifier *readNotifier;
	QQueue<QByteArray> readQueue;
	QSocketNotifier *writeNotifier;
	QQueue<QByteArray> writeQueue;
};

#endif // GATOSOCKET_H