~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kinfocenter/Modules/view1394/view1394.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * view1394.cpp
 
3
 *
 
4
 *  Copyright (C) 2003 Alexander Neundorf <neundorf@kde.org>
 
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
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
#include "view1394.h"
 
22
 
 
23
#include <QLayout>
 
24
#include <QPushButton>
 
25
#include <QFile>
 
26
#include <QTextStream>
 
27
#include <QTreeWidgetItem>
 
28
#include <QTreeWidget>
 
29
#include <QVBoxLayout>
 
30
 
 
31
#include <kcomponentdata.h>
 
32
#include <kdialog.h>
 
33
#include <kglobal.h>
 
34
#include <klocale.h>
 
35
#include <kstandarddirs.h>
 
36
#include <kpluginfactory.h>
 
37
#include <kpluginloader.h>
 
38
 
 
39
#include <sys/time.h>
 
40
#include <sys/types.h>
 
41
#include <unistd.h>
 
42
#include <netinet/in.h>
 
43
#include <stdio.h>
 
44
#include <string.h>
 
45
 
 
46
#include <libraw1394/csr.h>
 
47
 
 
48
#define CONFIGROM_BASE 0x00
 
49
#define CONFIGROM_CAP  0x08
 
50
#define CONFIGROM_GUID_HI 0x0c
 
51
#define CONFIGROM_GUID_LO 0x10
 
52
#define MAX_1394_PORTS 16
 
53
 
 
54
int my_reset_handler(raw1394handle_t handle, unsigned int) {
 
55
        View1394* view1394=(View1394*)raw1394_get_userdata(handle);
 
56
        if (view1394!=0)
 
57
                view1394->rescanBus();
 
58
        return 0;
 
59
}
 
60
 
 
61
K_PLUGIN_FACTORY(View1394Factory, registerPlugin<View1394>();)
 
62
K_EXPORT_PLUGIN(View1394Factory("kcmview1394"))
 
63
 
 
64
View1394::View1394(QWidget *parent, const QVariantList &) :
 
65
        KCModule(View1394Factory::componentData(), parent), m_insideRescanBus(false) {
 
66
        setQuickHelp(i18n("<qt>Here you can see some information about "
 
67
                "your IEEE 1394 configuration. "
 
68
                "The meaning of the columns:"
 
69
                "<ul>"
 
70
                "<li><b>Name</b>: port or node name, the number can change with each bus reset</li>"
 
71
                "<li><b>GUID</b>: the 64 bit GUID of the node</li>"
 
72
                "<li><b>Local</b>: checked if the node is an IEEE 1394 port of your computer</li>"
 
73
                "<li><b>IRM</b>: checked if the node is isochronous resource manager capable</li>"
 
74
                "<li><b>CRM</b>: checked if the node is cycle master capable</li>"
 
75
                "<li><b>ISO</b>: checked if the node supports isochronous transfers</li>"
 
76
                "<li><b>BM</b>: checked if the node is bus manager capable</li>"
 
77
                "<li><b>PM</b>: checked if the node is power management capable</li>"
 
78
                "<li><b>Acc</b>: the cycle clock accuracy of the node, valid from 0 to 100</li>"
 
79
                "<li><b>Speed</b>: the speed of the node</li>"
 
80
                "<li><b>Vendor</b>: the vendor of the device</li>"
 
81
                "</ul></qt>"
 
82
                ));
 
83
 
 
84
        m_ouiDb=new OuiDb();
 
85
        QVBoxLayout *box=new QVBoxLayout(this);
 
86
        box->setSpacing(KDialog::spacingHint());
 
87
        box->setMargin(0);
 
88
        m_view=new View1394Widget(this);
 
89
        /*
 
90
        for (int i=2; i<8; i++)
 
91
                m_view->m_listview->setColumnAlignment(i, Qt::AlignHCenter);
 
92
        m_view->m_listview->setColumnAlignment(8, Qt::AlignRight);
 
93
        m_view->m_listview->setColumnAlignment(9, Qt::AlignRight);
 
94
        */
 
95
        box->addWidget(m_view);
 
96
        m_view->layout()->setMargin(0);
 
97
        connect(m_view->m_busResetPb, SIGNAL(clicked()), this, SLOT(generateBusReset()));
 
98
        connect(&m_rescanTimer, SIGNAL(timeout()), this, SLOT(rescanBus()));
 
99
        rescanBus();
 
100
}
 
101
 
 
102
View1394::~View1394() {
 
103
        foreach(QSocketNotifier* notifier, m_notifiers) {
 
104
                delete notifier;
 
105
        }
 
106
 
 
107
        m_notifiers.clear();
 
108
        delete m_ouiDb;
 
109
}
 
110
 
 
111
bool View1394::readConfigRom(raw1394handle_t handle, nodeid_t nodeid, quadlet_t& firstQuad, quadlet_t& cap, octlet_t& guid) {
 
112
        quadlet_t q=0;
 
113
        firstQuad=0;
 
114
        cap=0;
 
115
        guid=0;
 
116
        nodeaddr_t addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_BASE;
 
117
        for (int count=0; count<5; count++) {
 
118
                struct timeval tv;
 
119
                q=0;
 
120
                int res=raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q);
 
121
                if (res==0) {
 
122
                        firstQuad=ntohl(q);
 
123
                        break;
 
124
                }
 
125
                tv.tv_sec=0;
 
126
                tv.tv_usec=10*1000;
 
127
                select(0, 0, 0, 0, &tv);
 
128
        }
 
129
        if (firstQuad==0)
 
130
                return false;
 
131
 
 
132
        addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_CAP;
 
133
        if (raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q)!=0)
 
134
                return false;
 
135
 
 
136
        cap=ntohl(q);
 
137
 
 
138
        addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_GUID_HI;
 
139
        if (raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q)!=0)
 
140
                return false;
 
141
 
 
142
        guid=octlet_t(ntohl(q))<<32;
 
143
 
 
144
        addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_GUID_LO;
 
145
        if (raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q)!=0)
 
146
                return false;
 
147
 
 
148
        guid=guid|ntohl(q);
 
149
 
 
150
        return true;
 
151
}
 
152
 
 
153
void View1394::callRaw1394EventLoop(int fd) {
 
154
        for (QList<raw1394handle_t>::iterator it= m_handles.begin(); it!=m_handles.end(); ++it)
 
155
                if (raw1394_get_fd(*it)==fd) {
 
156
                        raw1394_loop_iterate(*it);
 
157
                        break;
 
158
                }
 
159
}
 
160
 
 
161
void View1394::rescanBus() {
 
162
        if (m_insideRescanBus) {
 
163
                m_rescanTimer.setSingleShot(true);
 
164
                m_rescanTimer.start(100);
 
165
                return;
 
166
        }
 
167
        m_insideRescanBus=true;
 
168
        //   static int depth=0;
 
169
        //   depth++;
 
170
        m_notifiers.clear();
 
171
        for (QList<raw1394handle_t>::iterator it=m_handles.begin(); it!=m_handles.end(); ++it)
 
172
                raw1394_destroy_handle(*it);
 
173
        m_handles.clear();
 
174
        m_view->m_listview->clear();
 
175
 
 
176
        raw1394handle_t handle=raw1394_new_handle();
 
177
        if (handle==NULL) {
 
178
                m_insideRescanBus=false;
 
179
                return;
 
180
        }
 
181
        //now search for new stuff
 
182
        struct raw1394_portinfo p_info[MAX_1394_PORTS];
 
183
        int num_of_cards=raw1394_get_port_info(handle, p_info, MAX_1394_PORTS);
 
184
        raw1394_destroy_handle(handle);
 
185
        //iterate over all cards
 
186
        for (int i=0; i<num_of_cards; i++) {
 
187
                handle=raw1394_new_handle();
 
188
                if (raw1394_set_port(handle, i)!=0) {
 
189
                        raw1394_destroy_handle(handle);
 
190
                        continue;
 
191
                }
 
192
                raw1394_set_userdata(handle, this);
 
193
                raw1394_set_bus_reset_handler(handle, my_reset_handler);
 
194
                QSocketNotifier *notif=new QSocketNotifier(raw1394_get_fd(handle),QSocketNotifier::Read);
 
195
                connect(notif, SIGNAL(activated(int)), this, SLOT(callRaw1394EventLoop(int)));
 
196
                m_notifiers.append(notif);
 
197
                m_handles.append(handle);
 
198
 
 
199
                QStringList cardContents;
 
200
                cardContents << i18n("Port %1:\"%2\"", i, p_info[i].name);
 
201
                QTreeWidgetItem* card = new QTreeWidgetItem(m_view->m_listview, cardContents);
 
202
 
 
203
                int num_of_nodes=raw1394_get_nodecount(handle);
 
204
 
 
205
                int localNodeId=raw1394_get_local_id(handle);
 
206
                //iterate over all nodes connected to this card
 
207
                for (int j=0; j<num_of_nodes; j++) {
 
208
                        //get the guid of the node
 
209
                        octlet_t guid=0;
 
210
                        quadlet_t firstQuad=0;
 
211
                        quadlet_t cap=0;
 
212
                        bool success=readConfigRom(handle, j, firstQuad, cap, guid);
 
213
 
 
214
                        QString nodeStr=i18n("Node %1", j);
 
215
                        if (!success) {
 
216
                                QStringList notReadyList;
 
217
                                notReadyList << nodeStr << i18n("Not ready");
 
218
                                new QTreeWidgetItem(card, notReadyList);
 
219
                                continue;
 
220
                        }
 
221
                        //minimal config rom
 
222
                        if (((firstQuad>>24) & 0xff)==1) {
 
223
                                QString guidStr=QString::number(firstQuad, 16);
 
224
                                guidStr="0x"+guidStr.rightJustified(6, '0');
 
225
                                
 
226
                                QStringList romList;
 
227
                                romList << nodeStr << guidStr;
 
228
                                new QTreeWidgetItem(card, romList);
 
229
                        }
 
230
                        //general config rom
 
231
                        else {
 
232
                                QString guidStr;
 
233
                                char buf[32];
 
234
                                snprintf(buf, 32, "%llX", guid);
 
235
                                guidStr=buf;
 
236
                                guidStr="0x"+guidStr.rightJustified(16, '0');
 
237
                                QString local=((j | 0xffc0) == localNodeId) ? "X" : "";
 
238
                                QString irmStr=(cap & 0x80000000) ? "X" : "";
 
239
                                QString cmStr=(cap & 0x40000000) ? "X" : "";
 
240
                                QString isStr=(cap & 0x20000000) ? "X" : "";
 
241
                                QString bmStr=(cap & 0x10000000) ? "X" : "";
 
242
                                QString pmStr=(cap & 0x08000000) ? "X" : "";
 
243
                                QString accStr=QString::number((cap &0x00ff0000)>>16);
 
244
                                int speed=(cap & 0x00000007);
 
245
                                QString speedStr;
 
246
                                switch (speed) {
 
247
                                case (3):
 
248
                                        speedStr="800";
 
249
                                        break;
 
250
                                case (2):
 
251
                                        speedStr="400";
 
252
                                        break;
 
253
                                case (1):
 
254
                                        speedStr="200";
 
255
                                        break;
 
256
                                case (0):
 
257
                                default:
 
258
                                        speedStr="100";
 
259
                                        break;
 
260
                                }
 
261
                                
 
262
                                QStringList nodeContents;
 
263
                                nodeContents << nodeStr << guidStr << local << irmStr << cmStr << isStr << bmStr << pmStr << accStr << speedStr << m_ouiDb->vendor(guid);
 
264
                                
 
265
                                new QTreeWidgetItem(card, nodeContents);
 
266
                        }
 
267
                }
 
268
                card->setExpanded(true);
 
269
        }
 
270
        //   depth--;
 
271
        m_insideRescanBus=false;
 
272
}
 
273
 
 
274
void View1394::generateBusReset() {
 
275
        for (QList<raw1394handle_t>::iterator it=m_handles.begin(); it!=m_handles.end(); ++it)
 
276
                raw1394_reset_bus(*it);
 
277
}
 
278
 
 
279
OuiDb::OuiDb() {
 
280
        QString filename=KStandardDirs::locate("data", "kcmview1394/oui.db");
 
281
        if (filename.isEmpty())
 
282
                return;
 
283
        QFile f(filename);
 
284
        if (!f.open(QIODevice::ReadOnly))
 
285
                return;
 
286
 
 
287
        QByteArray ba=f.readAll();
 
288
        int bytesLeft=ba.size();
 
289
        char* data=ba.data();
 
290
        while (bytesLeft>8) {
 
291
                char *eol=(char*)memchr((const void*)data, '\n', bytesLeft);
 
292
                if (eol==0)
 
293
                        break;
 
294
                if ((eol-data)<8)
 
295
                        break;
 
296
                data[6]='\0';
 
297
                *eol='\0';
 
298
                m_vendorIds.insert(QLatin1String(data), QString::fromUtf8(data+7));
 
299
                bytesLeft-=(eol+1-data);
 
300
                data=eol+1;
 
301
        }
 
302
 
 
303
        f.close();
 
304
}
 
305
 
 
306
QString OuiDb::vendor(octlet_t guid) {
 
307
        guid=(guid & 0xffffff0000000000LL)>>40;
 
308
        QString key=QString::number((unsigned int)(guid), 16);
 
309
        key=key.rightJustified(6, '0').toUpper();
 
310
        QString v=m_vendorIds[key];
 
311
        if (v.isEmpty())
 
312
                v=i18n("Unknown");
 
313
        return v;
 
314
}
 
315
 
 
316
// ------------------------------------------------------------------------
 
317
 
 
318
#include "view1394.moc"