Bumping gaia.json for 4 gaia revision(s) a=gaia-bump
[gecko.git] / dom / bluetooth2 / BluetoothCommon.h
blobe7ddb7c28e4e43ce67ef2f8f55a560bb342afbd2
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_bluetooth_bluetoothcommon_h__
8 #define mozilla_dom_bluetooth_bluetoothcommon_h__
10 #include "mozilla/Observer.h"
11 #include "nsPrintfCString.h"
12 #include "nsString.h"
13 #include "nsTArray.h"
15 extern bool gBluetoothDebugFlag;
17 #define SWITCH_BT_DEBUG(V) (gBluetoothDebugFlag = V)
19 #if MOZ_IS_GCC && MOZ_GCC_VERSION_AT_LEAST(4, 7, 0)
20 /* use designated array initializers if supported */
21 #define INIT_ARRAY_AT(in_, out_) \
22 [in_] = out_
23 #else
24 /* otherwise init array element by position */
25 #define INIT_ARRAY_AT(in_, out_) \
26 out_
27 #endif
29 #define CONVERT(in_, out_) \
30 INIT_ARRAY_AT(in_, out_)
32 #undef BT_LOG
33 #if defined(MOZ_WIDGET_GONK)
34 #include <android/log.h>
36 /**
37 * Prints 'D'EBUG build logs, which show in DEBUG build only when
38 * developer setting 'Bluetooth output in adb' is enabled.
40 #define BT_LOGD(msg, ...) \
41 do { \
42 if (gBluetoothDebugFlag) { \
43 __android_log_print(ANDROID_LOG_INFO, "GeckoBluetooth", \
44 "%s: " msg, __FUNCTION__, ##__VA_ARGS__); \
45 } \
46 } while(0)
48 /**
49 * Prints 'R'ELEASE build logs, which show in both RELEASE and DEBUG builds.
51 #define BT_LOGR(msg, ...) \
52 __android_log_print(ANDROID_LOG_INFO, "GeckoBluetooth", \
53 "%s: " msg, __FUNCTION__, ##__VA_ARGS__) \
55 /**
56 * Prints DEBUG build warnings, which show in DEBUG build only.
58 #define BT_WARNING(args...) \
59 NS_WARNING(nsPrintfCString(args).get()) \
61 #else
62 #define BT_LOGD(msg, ...) \
63 do { \
64 if (gBluetoothDebugFlag) { \
65 printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__); \
66 } \
67 } while(0)
69 #define BT_LOGR(msg, ...) printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__))
70 #define BT_WARNING(msg, ...) printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__))
71 #endif
73 /**
74 * Prints 'R'ELEASE build logs for WebBluetooth API v2.
76 #define BT_API2_LOGR(msg, ...) \
77 BT_LOGR("[WEBBT-API2] " msg, ##__VA_ARGS__)
79 /**
80 * Wrap literal name and value into a BluetoothNamedValue
81 * and append it to the array.
83 #define BT_APPEND_NAMED_VALUE(array, name, value) \
84 array.AppendElement(BluetoothNamedValue(NS_LITERAL_STRING(name), \
85 BluetoothValue(value)))
87 /**
88 * Ensure success of system message broadcast with void return.
90 #define BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters) \
91 do { \
92 if (!BroadcastSystemMessage(type, parameters)) { \
93 BT_WARNING("Failed to broadcast [%s]", \
94 NS_ConvertUTF16toUTF8(type).get()); \
95 return; \
96 } \
97 } while(0)
99 /**
100 * Convert an enum value to string then append it to an array.
102 #define BT_APPEND_ENUM_STRING(array, enumType, enumValue) \
103 do { \
104 uint32_t index = uint32_t(enumValue); \
105 nsAutoString name; \
106 name.AssignASCII(enumType##Values::strings[index].value, \
107 enumType##Values::strings[index].length); \
108 array.AppendElement(name); \
109 } while(0) \
112 * Resolve promise with |ret| if |x| is false.
114 #define BT_ENSURE_TRUE_RESOLVE(x, ret) \
115 do { \
116 if (MOZ_UNLIKELY(!(x))) { \
117 BT_API2_LOGR("BT_ENSURE_TRUE_RESOLVE(" #x ") failed"); \
118 promise->MaybeResolve(ret); \
119 return promise.forget(); \
121 } while(0)
124 * Reject promise with |ret| if |x| is false.
126 #define BT_ENSURE_TRUE_REJECT(x, ret) \
127 do { \
128 if (MOZ_UNLIKELY(!(x))) { \
129 BT_API2_LOGR("BT_ENSURE_TRUE_REJECT(" #x ") failed"); \
130 promise->MaybeReject(ret); \
131 return promise.forget(); \
133 } while(0)
135 #define BEGIN_BLUETOOTH_NAMESPACE \
136 namespace mozilla { namespace dom { namespace bluetooth {
137 #define END_BLUETOOTH_NAMESPACE \
138 } /* namespace bluetooth */ } /* namespace dom */ } /* namespace mozilla */
139 #define USING_BLUETOOTH_NAMESPACE \
140 using namespace mozilla::dom::bluetooth;
142 #define KEY_LOCAL_AGENT "/B2G/bluetooth/agent"
143 #define KEY_REMOTE_AGENT "/B2G/bluetooth/remote_device_agent"
144 #define KEY_MANAGER "/B2G/bluetooth/manager"
145 #define KEY_ADAPTER "/B2G/bluetooth/adapter"
146 #define KEY_PAIRING_LISTENER "/B2G/bluetooth/pairing_listener"
149 * When the connection status of a Bluetooth profile is changed, we'll notify
150 * observers which register the following topics.
152 #define BLUETOOTH_A2DP_STATUS_CHANGED_ID "bluetooth-a2dp-status-changed"
153 #define BLUETOOTH_HFP_STATUS_CHANGED_ID "bluetooth-hfp-status-changed"
154 #define BLUETOOTH_HID_STATUS_CHANGED_ID "bluetooth-hid-status-changed"
155 #define BLUETOOTH_SCO_STATUS_CHANGED_ID "bluetooth-sco-status-changed"
158 * When the connection status of a Bluetooth profile is changed, we'll
159 * dispatch one of the following events.
161 #define A2DP_STATUS_CHANGED_ID "a2dpstatuschanged"
162 #define HFP_STATUS_CHANGED_ID "hfpstatuschanged"
163 #define SCO_STATUS_CHANGED_ID "scostatuschanged"
166 * Types of pairing requests for constructing BluetoothPairingEvent and
167 * BluetoothPairingHandle.
169 #define PAIRING_REQ_TYPE_DISPLAYPASSKEY "displaypasskeyreq"
170 #define PAIRING_REQ_TYPE_ENTERPINCODE "enterpincodereq"
171 #define PAIRING_REQ_TYPE_CONFIRMATION "pairingconfirmationreq"
172 #define PAIRING_REQ_TYPE_CONSENT "pairingconsentreq"
175 * System message to launch bluetooth app if no pairing listener is ready to
176 * receive pairing requests.
178 #define SYS_MSG_BT_PAIRING_REQ "bluetooth-pairing-request"
181 * The app origin of bluetooth app, which is responsible for listening pairing
182 * requests.
184 #define BLUETOOTH_APP_ORIGIN "app://bluetooth.gaiamobile.org"
187 * When a remote device gets paired / unpaired with local bluetooth adapter,
188 * we'll dispatch an event.
190 #define DEVICE_PAIRED_ID "devicepaired"
191 #define DEVICE_UNPAIRED_ID "deviceunpaired"
194 * When receiving a query about current play status from remote device, we'll
195 * dispatch an event.
197 #define REQUEST_MEDIA_PLAYSTATUS_ID "requestmediaplaystatus"
199 // Bluetooth address format: xx:xx:xx:xx:xx:xx (or xx_xx_xx_xx_xx_xx)
200 #define BLUETOOTH_ADDRESS_LENGTH 17
201 #define BLUETOOTH_ADDRESS_NONE "00:00:00:00:00:00"
202 #define BLUETOOTH_ADDRESS_BYTES 6
204 // Bluetooth stack internal error, such as I/O error
205 #define ERR_INTERNAL_ERROR "InternalError"
208 * BT specification v4.1 defines the maximum attribute length as 512 octets.
209 * Currently use 600 here to conform to bluedroid's BTGATT_MAX_ATTR_LEN.
211 #define BLUETOOTH_GATT_MAX_ATTR_LEN 600
213 BEGIN_BLUETOOTH_NAMESPACE
215 enum BluetoothStatus {
216 STATUS_SUCCESS,
217 STATUS_FAIL,
218 STATUS_NOT_READY,
219 STATUS_NOMEM,
220 STATUS_BUSY,
221 STATUS_DONE,
222 STATUS_UNSUPPORTED,
223 STATUS_PARM_INVALID,
224 STATUS_UNHANDLED,
225 STATUS_AUTH_FAILURE,
226 STATUS_RMT_DEV_DOWN
229 enum BluetoothBondState {
230 BOND_STATE_NONE,
231 BOND_STATE_BONDING,
232 BOND_STATE_BONDED
235 enum BluetoothTypeOfDevice {
236 TYPE_OF_DEVICE_BREDR,
237 TYPE_OF_DEVICE_BLE,
238 TYPE_OF_DEVICE_DUAL
241 enum BluetoothPropertyType {
242 PROPERTY_UNKNOWN,
243 PROPERTY_BDNAME,
244 PROPERTY_BDADDR,
245 PROPERTY_UUIDS,
246 PROPERTY_CLASS_OF_DEVICE,
247 PROPERTY_TYPE_OF_DEVICE,
248 PROPERTY_SERVICE_RECORD,
249 PROPERTY_ADAPTER_SCAN_MODE,
250 PROPERTY_ADAPTER_BONDED_DEVICES,
251 PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
252 PROPERTY_REMOTE_FRIENDLY_NAME,
253 PROPERTY_REMOTE_RSSI,
254 PROPERTY_REMOTE_VERSION_INFO,
255 PROPERTY_REMOTE_DEVICE_TIMESTAMP
258 enum BluetoothScanMode {
259 SCAN_MODE_NONE,
260 SCAN_MODE_CONNECTABLE,
261 SCAN_MODE_CONNECTABLE_DISCOVERABLE
264 enum BluetoothSspVariant {
265 SSP_VARIANT_PASSKEY_CONFIRMATION,
266 SSP_VARIANT_PASSKEY_ENTRY,
267 SSP_VARIANT_CONSENT,
268 SSP_VARIANT_PASSKEY_NOTIFICATION
271 struct BluetoothUuid {
272 uint8_t mUuid[16];
275 struct BluetoothServiceRecord {
276 BluetoothUuid mUuid;
277 uint16_t mChannel;
278 char mName[256];
281 struct BluetoothRemoteInfo {
282 int mVerMajor;
283 int mVerMinor;
284 int mManufacturer;
287 struct BluetoothProperty {
288 /* Type */
289 BluetoothPropertyType mType;
291 /* Value
294 /* PROPERTY_BDNAME
295 PROPERTY_BDADDR
296 PROPERTY_REMOTE_FRIENDLY_NAME */
297 nsString mString;
299 /* PROPERTY_UUIDS */
300 nsTArray<BluetoothUuid> mUuidArray;
302 /* PROPERTY_ADAPTER_BONDED_DEVICES */
303 nsTArray<nsString> mStringArray;
305 /* PROPERTY_CLASS_OF_DEVICE
306 PROPERTY_ADAPTER_DISCOVERY_TIMEOUT */
307 uint32_t mUint32;
309 /* PROPERTY_RSSI_VALUE */
310 int32_t mInt32;
312 /* PROPERTY_TYPE_OF_DEVICE */
313 BluetoothTypeOfDevice mTypeOfDevice;
315 /* PROPERTY_SERVICE_RECORD */
316 BluetoothServiceRecord mServiceRecord;
318 /* PROPERTY_SCAN_MODE */
319 BluetoothScanMode mScanMode;
321 /* PROPERTY_REMOTE_VERSION_INFO */
322 BluetoothRemoteInfo mRemoteInfo;
325 enum BluetoothSocketType {
326 RFCOMM = 1,
327 SCO = 2,
328 L2CAP = 3,
329 EL2CAP = 4
332 enum BluetoothHandsfreeAtResponse {
333 HFP_AT_RESPONSE_ERROR,
334 HFP_AT_RESPONSE_OK
337 enum BluetoothHandsfreeAudioState {
338 HFP_AUDIO_STATE_DISCONNECTED,
339 HFP_AUDIO_STATE_CONNECTING,
340 HFP_AUDIO_STATE_CONNECTED,
341 HFP_AUDIO_STATE_DISCONNECTING,
344 enum BluetoothHandsfreeCallAddressType {
345 HFP_CALL_ADDRESS_TYPE_UNKNOWN,
346 HFP_CALL_ADDRESS_TYPE_INTERNATIONAL
349 enum BluetoothHandsfreeCallDirection {
350 HFP_CALL_DIRECTION_OUTGOING,
351 HFP_CALL_DIRECTION_INCOMING
354 enum BluetoothHandsfreeCallHoldType {
355 HFP_CALL_HOLD_RELEASEHELD,
356 HFP_CALL_HOLD_RELEASEACTIVE_ACCEPTHELD,
357 HFP_CALL_HOLD_HOLDACTIVE_ACCEPTHELD,
358 HFP_CALL_HOLD_ADDHELDTOCONF
361 enum BluetoothHandsfreeCallMode {
362 HFP_CALL_MODE_VOICE,
363 HFP_CALL_MODE_DATA,
364 HFP_CALL_MODE_FAX
367 enum BluetoothHandsfreeCallMptyType {
368 HFP_CALL_MPTY_TYPE_SINGLE,
369 HFP_CALL_MPTY_TYPE_MULTI
372 enum BluetoothHandsfreeCallState {
373 HFP_CALL_STATE_ACTIVE,
374 HFP_CALL_STATE_HELD,
375 HFP_CALL_STATE_DIALING,
376 HFP_CALL_STATE_ALERTING,
377 HFP_CALL_STATE_INCOMING,
378 HFP_CALL_STATE_WAITING,
379 HFP_CALL_STATE_IDLE
382 enum BluetoothHandsfreeConnectionState
384 HFP_CONNECTION_STATE_DISCONNECTED,
385 HFP_CONNECTION_STATE_CONNECTING,
386 HFP_CONNECTION_STATE_CONNECTED,
387 HFP_CONNECTION_STATE_SLC_CONNECTED,
388 HFP_CONNECTION_STATE_DISCONNECTING
391 enum BluetoothHandsfreeNetworkState {
392 HFP_NETWORK_STATE_NOT_AVAILABLE,
393 HFP_NETWORK_STATE_AVAILABLE
396 enum BluetoothHandsfreeNRECState {
397 HFP_NREC_STOPPED,
398 HFP_NREC_STARTED
401 enum BluetoothHandsfreeServiceType {
402 HFP_SERVICE_TYPE_HOME,
403 HFP_SERVICE_TYPE_ROAMING
406 enum BluetoothHandsfreeVoiceRecognitionState {
407 HFP_VOICE_RECOGNITION_STOPPED,
408 HFP_VOICE_RECOGNITION_STARTED
411 enum BluetoothHandsfreeVolumeType {
412 HFP_VOLUME_TYPE_SPEAKER,
413 HFP_VOLUME_TYPE_MICROPHONE
416 class BluetoothSignal;
417 typedef mozilla::Observer<BluetoothSignal> BluetoothSignalObserver;
419 // Enums for object types, currently used for shared function lookups
420 // (get/setproperty, etc...). Possibly discernable via dbus paths, but this
421 // method is future-proofed for platform independence.
422 enum BluetoothObjectType {
423 TYPE_MANAGER = 0,
424 TYPE_ADAPTER = 1,
425 TYPE_DEVICE = 2,
427 TYPE_INVALID
430 enum BluetoothA2dpAudioState {
431 A2DP_AUDIO_STATE_REMOTE_SUSPEND,
432 A2DP_AUDIO_STATE_STOPPED,
433 A2DP_AUDIO_STATE_STARTED,
436 enum BluetoothA2dpConnectionState {
437 A2DP_CONNECTION_STATE_DISCONNECTED,
438 A2DP_CONNECTION_STATE_CONNECTING,
439 A2DP_CONNECTION_STATE_CONNECTED,
440 A2DP_CONNECTION_STATE_DISCONNECTING
443 enum ControlPlayStatus {
444 PLAYSTATUS_STOPPED = 0x00,
445 PLAYSTATUS_PLAYING = 0x01,
446 PLAYSTATUS_PAUSED = 0x02,
447 PLAYSTATUS_FWD_SEEK = 0x03,
448 PLAYSTATUS_REV_SEEK = 0x04,
449 PLAYSTATUS_UNKNOWN,
450 PLAYSTATUS_ERROR = 0xFF,
453 enum {
454 AVRCP_UID_SIZE = 8
457 enum BluetoothAvrcpMediaAttribute {
458 AVRCP_MEDIA_ATTRIBUTE_TITLE,
459 AVRCP_MEDIA_ATTRIBUTE_ARTIST,
460 AVRCP_MEDIA_ATTRIBUTE_ALBUM,
461 AVRCP_MEDIA_ATTRIBUTE_TRACK_NUM,
462 AVRCP_MEDIA_ATTRIBUTE_NUM_TRACKS,
463 AVRCP_MEDIA_ATTRIBUTE_GENRE,
464 AVRCP_MEDIA_ATTRIBUTE_PLAYING_TIME
467 enum BluetoothAvrcpPlayerAttribute {
468 AVRCP_PLAYER_ATTRIBUTE_EQUALIZER,
469 AVRCP_PLAYER_ATTRIBUTE_REPEAT,
470 AVRCP_PLAYER_ATTRIBUTE_SHUFFLE,
471 AVRCP_PLAYER_ATTRIBUTE_SCAN
474 enum BluetoothAvrcpStatus {
475 AVRCP_STATUS_BAD_COMMAND,
476 AVRCP_STATUS_BAD_PARAMETER,
477 AVRCP_STATUS_NOT_FOUND,
478 AVRCP_STATUS_INTERNAL_ERROR,
479 AVRCP_STATUS_SUCCESS
482 enum BluetoothAvrcpEvent {
483 AVRCP_EVENT_PLAY_STATUS_CHANGED,
484 AVRCP_EVENT_TRACK_CHANGE,
485 AVRCP_EVENT_TRACK_REACHED_END,
486 AVRCP_EVENT_TRACK_REACHED_START,
487 AVRCP_EVENT_PLAY_POS_CHANGED,
488 AVRCP_EVENT_APP_SETTINGS_CHANGED
491 enum BluetoothAvrcpNotification {
492 AVRCP_NTF_INTERIM,
493 AVRCP_NTF_CHANGED
496 enum BluetoothAvrcpRemoteFeature {
497 AVRCP_REMOTE_FEATURE_NONE,
498 AVRCP_REMOTE_FEATURE_METADATA,
499 AVRCP_REMOTE_FEATURE_ABSOLUTE_VOLUME,
500 AVRCP_REMOTE_FEATURE_BROWSE
503 struct BluetoothAvrcpElementAttribute {
504 uint32_t mId;
505 nsString mValue;
508 struct BluetoothAvrcpNotificationParam {
509 ControlPlayStatus mPlayStatus;
510 uint8_t mTrack[8];
511 uint32_t mSongPos;
512 uint8_t mNumAttr;
513 uint8_t mIds[256];
514 uint8_t mValues[256];
517 struct BluetoothAvrcpPlayerSettings {
518 uint8_t mNumAttr;
519 uint8_t mIds[256];
520 uint8_t mValues[256];
523 struct BluetoothGattAdvData {
524 uint8_t mAdvData[62];
527 struct BluetoothGattId {
528 BluetoothUuid mUuid;
529 uint8_t mInstanceId;
532 struct BluetoothGattServiceId {
533 BluetoothGattId mId;
534 uint8_t mIsPrimary;
537 struct BluetoothGattReadParam {
538 BluetoothGattServiceId mServiceId;
539 BluetoothGattId mCharId;
540 BluetoothGattId mDescriptorId;
541 uint8_t mValue[BLUETOOTH_GATT_MAX_ATTR_LEN];
542 uint16_t mValueLength;
543 uint16_t mValueType;
544 uint8_t mStatus;
547 struct BluetoothGattWriteParam {
548 BluetoothGattServiceId mServiceId;
549 BluetoothGattId mCharId;
550 BluetoothGattId mDescriptorId;
551 uint8_t mStatus;
554 struct BluetoothGattNotifyParam {
555 uint8_t mValue[BLUETOOTH_GATT_MAX_ATTR_LEN];
556 nsString mBdAddr;
557 BluetoothGattServiceId mServiceId;
558 BluetoothGattId mCharId;
559 uint16_t mLength;
560 uint8_t mIsNotify;
563 END_BLUETOOTH_NAMESPACE
565 #endif // mozilla_dom_bluetooth_bluetoothcommon_h__