~ubuntu-branches/ubuntu/intrepid/kdebluetooth/intrepid-proposed

« back to all changes in this revision

Viewing changes to kdebluetooth/libkbluetooth/adapter_old.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2008-08-07 09:49:47 UTC
  • mto: This revision was merged to the branch mainline in revision 56.
  • Revision ID: james.westby@ubuntu.com-20080807094947-pj6q3uxwuv7l844q
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//-*-c++-*-
2
 
/***************************************************************************
3
 
 *   Copyright (C) 2003 by Fred Schaettgen                                 *
4
 
 *   kdebluetooth@schaettgen.de                                            *
5
 
 *                                                                         *
6
 
 *   This program is free software; you can redistribute it and/or modify  *
7
 
 *   it under the terms of the GNU General Public License as published by  *
8
 
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 
 *   (at your option) any later version.                                   *
10
 
 ***************************************************************************/
11
 
 
12
 
#include <stdio.h>
13
 
#include <stdlib.h>
14
 
#include <unistd.h>
15
 
#include <string.h>
16
 
#include <signal.h>
17
 
#include <fcntl.h>
18
 
#include <errno.h>
19
 
#include <ctype.h>
20
 
#include <kdebug.h>
21
 
 
22
 
#include <termios.h>
23
 
#include <fcntl.h>
24
 
#include <getopt.h>
25
 
#include <sys/ioctl.h>
26
 
#include <sys/socket.h>
27
 
#include <asm/types.h>
28
 
#include <netinet/in.h>
29
 
 
30
 
#include "adapter_old.h"
31
 
#include <qstring.h>
32
 
#include <sys/socket.h>
33
 
#include <bluetooth/bluetooth.h>
34
 
#include <bluetooth/hci.h>
35
 
#include <bluetooth/hci_lib.h>
36
 
#include <hcisocket.h>
37
 
 
38
 
namespace KBluetooth
39
 
{
40
 
 
41
 
DeviceAddress RAdapter::getAddress() const
42
 
{
43
 
    return address;
44
 
}
45
 
 
46
 
QString RAdapter::getName() const
47
 
{
48
 
    return nameStr;
49
 
}
50
 
 
51
 
int RAdapter::getIndex() const
52
 
{
53
 
    return index;
54
 
}
55
 
 
56
 
RAdapter::RAdapter(int index, const DeviceAddress& address, QString name)
57
 
{
58
 
    this->index = index;
59
 
    this->address = address;
60
 
    this->nameStr = name;
61
 
}
62
 
 
63
 
 
64
 
RAdapter::ConnectionInfoVector RAdapter::getAclConnections() const
65
 
{
66
 
    ConnectionInfoVector ret;
67
 
 
68
 
    hci_conn_list_req *cl;
69
 
    hci_conn_info *ci;
70
 
    char buf[10*sizeof(*ci) + sizeof(*cl)];
71
 
    cl = (hci_conn_list_req*)buf;
72
 
    cl->dev_id = getIndex();
73
 
    cl->conn_num = 10;
74
 
    ci = cl->conn_info;
75
 
 
76
 
    HciSocket s(NULL, "", getIndex());
77
 
    if (!s.open()) {
78
 
        return ret;
79
 
    }
80
 
 
81
 
    if (ioctl(s.socket(), HCIGETCONNLIST, (void*)cl)) {
82
 
        kdWarning() << "Can't get connection list: "
83
 
            << QString::fromLocal8Bit(strerror(errno)) <<  endl;
84
 
        return ret;
85
 
    }
86
 
 
87
 
    for (int i=0; i < cl->conn_num; i++, ci++) {
88
 
        if (ci->type == ACL_LINK) {
89
 
            ConnectionInfo i;
90
 
            i.address = DeviceAddress(ci->bdaddr, false);
91
 
            i.handle = ci->handle;
92
 
            i.out = (ci->out != 0);
93
 
            i.type = ConnectionType(ci->type);
94
 
            switch (ci->state) {
95
 
            case 0: i.state = NOT_CONNECTED; break;
96
 
            case 1: i.state = CONNECTED; break;
97
 
            case 5: i.state = CONNECTING; break;
98
 
            default: i.state = UNKNOWN_STATE;
99
 
            }
100
 
            i.link_mode = ci->link_mode;
101
 
            ret.push_back(i);
102
 
        }
103
 
    }
104
 
 
105
 
    return ret;
106
 
}
107
 
 
108
 
RAdapter::ConnectionState RAdapter::getAclConnectionState(const DeviceAddress& addr) const
109
 
{
110
 
    ConnectionState ret = UNKNOWN_STATE;
111
 
 
112
 
    hci_conn_list_req *cl;
113
 
    hci_conn_info *ci;
114
 
    char buf[10*sizeof(*ci) + sizeof(*cl)];
115
 
    cl = (hci_conn_list_req*)buf;
116
 
    cl->dev_id = getIndex();
117
 
    cl->conn_num = 10;
118
 
    ci = cl->conn_info;
119
 
 
120
 
    HciSocket s(NULL, "", getIndex());
121
 
    if (!s.open()) {
122
 
        return ret;
123
 
    }
124
 
 
125
 
    if (ioctl(s.socket(), HCIGETCONNLIST, (void*)cl)) {
126
 
        kdWarning() << "Can't get connection list"
127
 
            << QString::fromLocal8Bit(strerror(errno)) <<  endl;
128
 
        return ret;
129
 
    }
130
 
 
131
 
    int state = 0;
132
 
    for (int i=0; i < cl->conn_num; i++, ci++) {
133
 
        if ((ci->type == ACL_LINK) &&
134
 
            (DeviceAddress(ci->bdaddr,false) == addr)) {
135
 
            state = ci->state;
136
 
        }
137
 
    }
138
 
 
139
 
    switch (state) {
140
 
    case 0: ret = NOT_CONNECTED; break;
141
 
    case 1: ret = CONNECTED; break;
142
 
    case 5: ret = CONNECTING; break;
143
 
    default: ret = UNKNOWN_STATE;
144
 
    }
145
 
 
146
 
    return ret;
147
 
}
148
 
 
149
 
 
150
 
int RAdapter::getClassOfDevice() const
151
 
{
152
 
    HciSocket s(NULL, "", getIndex());
153
 
    if (!s.open()) {
154
 
        return -1;
155
 
    } 
156
 
    uint8_t cls[3];
157
 
    if (hci_read_class_of_dev(s.socket(), cls, 1000) < 0) {
158
 
        s.close();
159
 
        return -1;
160
 
    }
161
 
    s.close();
162
 
    return cls[2]*256*256+cls[1]*256+cls[0];
163
 
}
164
 
 
165
 
Adapters::Adapters()
166
 
{
167
 
    update();
168
 
}
169
 
 
170
 
int Adapters::count()
171
 
{
172
 
    return adapters.size();
173
 
}
174
 
 
175
 
const RAdapter& Adapters::operator[](int n) const
176
 
{
177
 
    return adapters[n];
178
 
}
179
 
 
180
 
void Adapters::update()
181
 
{
182
 
    adapters.clear();
183
 
    hci_for_each_dev(HCI_UP, dev_info, (long int)this);
184
 
}
185
 
 
186
 
int Adapters::dev_info(int s, int dev_id, long arg)
187
 
{
188
 
    reinterpret_cast<Adapters*>(arg)->dev_info(s, dev_id);
189
 
    return 0;
190
 
}
191
 
 
192
 
void Adapters::dev_info(int s, int dev_id)
193
 
{
194
 
    struct hci_dev_info di;
195
 
    di.dev_id = dev_id;
196
 
    if (ioctl(s, HCIGETDEVINFO, (void*) &di))
197
 
        return;
198
 
 
199
 
    RAdapter a = RAdapter(dev_id, DeviceAddress(di.bdaddr), QString(di.name));
200
 
    adapters.push_back(a);
201
 
}
202
 
}