aboutsummaryrefslogtreecommitdiff
path: root/src/bluetooth.vala
blob: 1952d1cb1dab3e2567ef40df35627e688210b233 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright 2013 Canonical Ltd.
 *
 * 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; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

/**
 * Base class for the bluetooth backend.
 */
public class Bluetooth: Object
{
  /* whether or not our system can be seen by other bluetooth devices */
  public bool discoverable { get; protected set; default = false; }
  public virtual void try_set_discoverable (bool b) {}

  /* whether or not there are any bluetooth adapters powered up on the system */
  public bool powered { get; protected set; default = false; }

  /* whether or not bluetooth's been disabled,
     either by a software setting or physical hardware switch */
  public bool blocked { get; protected set; default = true; }
  public virtual void try_set_blocked (bool b) {
    killswitch.try_set_blocked (b);
  }

  public class Device: Object {
    public string name { get; construct; }
    public bool supports_browsing { get; construct; }
    public bool supports_file_transfer { get; construct; }
    public Device (string name,
                   bool supports_browsing,
                   bool supports_file_transfer) {
      Object (name: name,
              supports_browsing: supports_browsing,
              supports_file_transfer: supports_file_transfer);
    }
  }

  private static uint16 get_uuid16_from_uuid (string uuid)
  {
    uint16 uuid16;

    string[] tokens = uuid.split ("-", 1);
    if (tokens.length > 0)
      uuid16 = (uint16) uint64.parse ("0x"+tokens[0]);
    else
      uuid16 = 0;

    return uuid16;
  }
        
  protected static bool uuid_supports_file_transfer (string uuid)
  {
    return get_uuid16_from_uuid (uuid) == 0x1105; // OBEXObjectPush
  }

  protected static bool uuid_supports_browsing (string uuid)
  {
    return get_uuid16_from_uuid (uuid) == 0x1106; // OBEXFileTransfer
  }

  public enum DeviceType
  {
    COMPUTER,
    PHONE,
    MODEM,
    NETWORK,
    HEADSET,
    HEADPHONES,
    VIDEO,
    OTHER_AUDIO,
    JOYPAD,
    KEYPAD,
    KEYBOARD,
    TABLET,
    MOUSE,
    PRINTER,
    CAMERA
  }

  protected static DeviceType class_to_device_type (uint32 c)
  {
    switch ((c & 0x1f00) >> 8)
      {
        case 0x01:
          return DeviceType.COMPUTER;

        case 0x02:
          switch ((c & 0xfc) >> 2)
            {
              case 0x01:
              case 0x02:
              case 0x03:
              case 0x05:
                return DeviceType.PHONE;

              case 0x04:
                return DeviceType.MODEM;
            }
          break;

        case 0x03:
          return DeviceType.NETWORK;

        case 0x04:
          switch ((c & 0xfc) >> 2)
            {
              case 0x01:
              case 0x02:
                return DeviceType.HEADSET;

              case 0x06:
                return DeviceType.HEADPHONES;

              case 0x0b: // vcr
              case 0x0c: // video camera
              case 0x0d: // camcorder
                return DeviceType.VIDEO;

              default:
                return DeviceType.OTHER_AUDIO;
            }
          //break;

        case 0x05:
          switch ((c & 0xc0) >> 6)
            {
              case 0x00:
                switch ((c & 0x1e) >> 2)
                  {
                    case 0x01:
                    case 0x02:
                      return DeviceType.JOYPAD;
                  }
                break;

              case 0x01:
                return DeviceType.KEYBOARD;

              case 0x02:
                switch ((c & 0x1e) >> 2)
                  {
                    case 0x05:
                      return DeviceType.TABLET;

                    default:
                      return DeviceType.MOUSE;
                  }
            }
          break;

        case 0x06:
          if ((c & 0x80) != 0)
            return DeviceType.PRINTER;
          if ((c & 0x20) != 0)
            return DeviceType.CAMERA;
          break;
      }

    return 0;
  }


  /***
  ****  Killswitch Implementation 
  ***/

  private KillSwitch killswitch;

  public Bluetooth (KillSwitch killswitch)
  {
    this.killswitch = killswitch;
    blocked = killswitch.blocked;
    killswitch.notify["blocked"].connect (() => blocked = killswitch.blocked );
  }
}