summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTHCISocket.h
blob: a004989699e0c9b58183121e4f6ce90fe6332bc7 (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
//-*-c++-*-
/***************************************************************************
 *   Copyright (C) 2003 by Fred Schaettgen                                 *
 *   kdebluetooth@schaettgen.de                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#ifndef OTHCISOCKET_H
#define OTHCISOCKET_H

#include <qobject.h>
#include <qsocketnotifier.h>
#include <qsocketdevice.h>
#include <qguardedptr.h>

class QSocket;

namespace Opietooth2 {

/** Bluetooth HCI Socket class.
 * This class provides socket level access to the Bluez HCI layer.
 * It is set up to filter out all but HCI event packets, since more
 * is only allowed for privileged users.
 * @todo writeHciEvent() function
 * @todo error handling
 */

class OTDriver;

class OTHCISocket : public QObject {

    Q_OBJECT

public:

    OTHCISocket( OTDriver * ConnectTo );
    virtual ~OTHCISocket();

    /** Opens a HCI socket for the given
    @return true if sucessfully opened, false otherwise
    */
    virtual bool open();

    /** Closes the HCI socket. */
    virtual void close();

    bool sendCommand( unsigned char ogf, 
                      unsigned short ocf,
                      QByteArray buf
                    );
    bool readStatus( unsigned char ogf, 
                     unsigned short ocf, 
                     int *status, 
                     int timeout_ms = 1000);
    
    /** Reads whole HCI packet.
    @param packetType [out] The packet type. Should always be ...
    @param eventCode [out] The event code.
    @param buflen [in/out] The maximum size of the buffer / the number of
    bytes written into the buffer.
    @param paramBuf pointer to a buffer for the HCI event packet
    @return true if successful
    */
    /*bool readEvent(unsigned char &packetType,
                      unsigned char &eventCode, unsigned char &buflen,
                      char* paramBuf);*/

    enum Error { ErrSocket = 1 };

    /** Forces reading the next event packet. */
    void readEvent( void );

    /** Returns the internal socket */
    int socket( void );

    inline QSocketDevice & socketDevice() 
      { return HCISocket; }

    inline OTDriver * driver() const
      { return Driver; }

signals:

    void event( unsigned char eventCode, QByteArray buf);
    void error( QString message );
    void connectionClosed( );

private:

    void updateStatus( const QByteArray& data );

    //QSocketDevice hciSocket;
    QGuardedPtr<QSocketNotifier> HCIReadNotifier;
    QSocketDevice                HCISocket;
    OTDriver *                   Driver ;
    
    bool                         BStatusSet;
    unsigned short               LastStatusOcf;
    unsigned char                LastStatusOgf;
    int                          LastStatus;

private slots:

    void slotSocketActivated();
    void slotSocketError(int);
    void slotConnectionClosed();

};

}

#endif