~ubuntu-branches/ubuntu/trusty/powertop-1.13/trusty

« back to all changes in this revision

Viewing changes to bluetooth.c

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2011-11-11 13:36:57 UTC
  • Revision ID: package-import@ubuntu.com-20111111133657-dumpedggvnmcjb2e
Tags: upstream-1.13
ImportĀ upstreamĀ versionĀ 1.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2007, Intel Corporation
 
3
 *
 
4
 * This file is part of PowerTOP
 
5
 *
 
6
 * This program file is free software; you can redistribute it and/or modify it
 
7
 * under the terms of the GNU General Public License as published by the
 
8
 * Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
13
 * for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program in a file named COPYING; if not, write to the
 
17
 * Free Software Foundation, Inc.,
 
18
 * 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301 USA
 
20
 *
 
21
 * Authors:
 
22
 *      Arjan van de Ven <arjan@linux.intel.com>
 
23
 */
 
24
 
 
25
#include <unistd.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
#include <stdint.h>
 
30
#include <sys/types.h>
 
31
#include <dirent.h>
 
32
#include <sys/types.h>
 
33
#include <sys/socket.h>
 
34
#include <sys/ioctl.h>
 
35
#include <errno.h>
 
36
 
 
37
#include "powertop.h"
 
38
 
 
39
 
 
40
/* structure definitions copied from include/net/bluetooth/hci.h from the 2.6.20 kernel */
 
41
#define HCIGETDEVINFO   _IOR('H', 211, int)
 
42
#define BTPROTO_HCI     1
 
43
 
 
44
#define __u16 uint16_t
 
45
#define __u8 uint8_t
 
46
#define __u32 uint32_t
 
47
 
 
48
static int suggested = 0;
 
49
 
 
50
typedef struct {
 
51
        __u8 b[6];
 
52
} __attribute__((packed)) bdaddr_t;
 
53
 
 
54
struct hci_dev_stats {
 
55
        __u32 err_rx;
 
56
        __u32 err_tx;
 
57
        __u32 cmd_tx;
 
58
        __u32 evt_rx;
 
59
        __u32 acl_tx;
 
60
        __u32 acl_rx;
 
61
        __u32 sco_tx;
 
62
        __u32 sco_rx;
 
63
        __u32 byte_rx;
 
64
        __u32 byte_tx;
 
65
};
 
66
 
 
67
 
 
68
struct hci_dev_info {
 
69
        __u16 dev_id;
 
70
        char  name[8];
 
71
 
 
72
        bdaddr_t bdaddr;
 
73
 
 
74
        __u32 flags;
 
75
        __u8  type;
 
76
 
 
77
        __u8  features[8];
 
78
 
 
79
        __u32 pkt_type;
 
80
        __u32 link_policy;
 
81
        __u32 link_mode;
 
82
 
 
83
        __u16 acl_mtu;
 
84
        __u16 acl_pkts;
 
85
        __u16 sco_mtu;
 
86
        __u16 sco_pkts;
 
87
 
 
88
        struct hci_dev_stats stat;
 
89
};
 
90
 
 
91
static int previous_bytes = -1;
 
92
 
 
93
void turn_bluetooth_off(void)
 
94
{
 
95
        suggested = 1;
 
96
        system("/usr/sbin/hciconfig hci0 down &> /dev/null");
 
97
        system("/sbin/rmmod hci_usb &> /dev/null");
 
98
}
 
99
 
 
100
void suggest_bluetooth_off(void)
 
101
{
 
102
        struct hci_dev_info devinfo;
 
103
        FILE *file;
 
104
        int fd;
 
105
        int ret;
 
106
        int thisbytes = 0;
 
107
 
 
108
        if (suggested)
 
109
                return;
 
110
 
 
111
        /* first check if /sys/modules/bluetooth exists, if not, don't probe bluetooth because
 
112
           it would trigger an autoload */
 
113
 
 
114
        if (access("/sys/module/bluetooth",F_OK))
 
115
                return;
 
116
 
 
117
        fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
 
118
        if (fd < 0)
 
119
                return;
 
120
 
 
121
        memset(&devinfo, 0, sizeof(devinfo));
 
122
        strcpy(devinfo.name, "hci0");
 
123
        ret = ioctl(fd, HCIGETDEVINFO, (void *) &devinfo);
 
124
        if (ret < 0)
 
125
                goto out;
 
126
 
 
127
        if ( (devinfo.flags & 1) == 0 &&
 
128
                access("/sys/module/hci_usb",F_OK)) /* interface down already */
 
129
                goto out;
 
130
 
 
131
        thisbytes += devinfo.stat.byte_rx;
 
132
        thisbytes += devinfo.stat.byte_tx;
 
133
 
 
134
        if (thisbytes != previous_bytes)
 
135
                goto out;
 
136
 
 
137
        /* now, also check for active connections */
 
138
        file = popen("/usr/bin/hcitool con 2> /dev/null", "r");
 
139
        if (file) {
 
140
                char line[2048];
 
141
                /* first line is standard header */
 
142
                fgets(line,2048,file);
 
143
                memset(line, 0, 2048);
 
144
                fgets(line, 2047, file);
 
145
                pclose(file);
 
146
                if (strlen(line)>0)
 
147
                        goto out;
 
148
        }
 
149
 
 
150
        add_suggestion( _("Suggestion: Disable the unused bluetooth interface with the following command:\n"
 
151
                        "  hciconfig hci0 down ; rmmod hci_usb\n"
 
152
                        "Bluetooth is a radio and consumes quite some power, and keeps USB busy as well.\n"), 40, 'B' , _(" B - Turn Bluetooth off "), turn_bluetooth_off);
 
153
out:
 
154
        previous_bytes = thisbytes;
 
155
        close(fd);
 
156
        return;
 
157
}