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"
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_) \
24 /* otherwise init array element by position */
25 #define INIT_ARRAY_AT(in_, out_) \
29 #define CONVERT(in_, out_) \
30 INIT_ARRAY_AT(in_, out_)
33 #if defined(MOZ_WIDGET_GONK)
34 #include <android/log.h>
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, ...) \
42 if (gBluetoothDebugFlag) { \
43 __android_log_print(ANDROID_LOG_INFO, "GeckoBluetooth", \
44 "%s: " msg, __FUNCTION__, ##__VA_ARGS__); \
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__) \
56 * Prints DEBUG build warnings, which show in DEBUG build only.
58 #define BT_WARNING(args...) \
59 NS_WARNING(nsPrintfCString(args).get()) \
62 #define BT_LOGD(msg, ...) \
64 if (gBluetoothDebugFlag) { \
65 printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__); \
69 #define BT_LOGR(msg, ...) printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__))
70 #define BT_WARNING(msg, ...) printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__))
74 * Prints 'R'ELEASE build logs for WebBluetooth API v2.
76 #define BT_API2_LOGR(msg, ...) \
77 BT_LOGR("[WEBBT-API2] " msg, ##__VA_ARGS__)
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)))
88 * Ensure success of system message broadcast with void return.
90 #define BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters) \
92 if (!BroadcastSystemMessage(type, parameters)) { \
93 BT_WARNING("Failed to broadcast [%s]", \
94 NS_ConvertUTF16toUTF8(type).get()); \
100 * Convert an enum value to string then append it to an array.
102 #define BT_APPEND_ENUM_STRING(array, enumType, enumValue) \
104 uint32_t index = uint32_t(enumValue); \
106 name.AssignASCII(enumType##Values::strings[index].value, \
107 enumType##Values::strings[index].length); \
108 array.AppendElement(name); \
112 * Resolve promise with |ret| if |x| is false.
114 #define BT_ENSURE_TRUE_RESOLVE(x, ret) \
116 if (MOZ_UNLIKELY(!(x))) { \
117 BT_API2_LOGR("BT_ENSURE_TRUE_RESOLVE(" #x ") failed"); \
118 promise->MaybeResolve(ret); \
119 return promise.forget(); \
124 * Reject promise with |ret| if |x| is false.
126 #define BT_ENSURE_TRUE_REJECT(x, ret) \
128 if (MOZ_UNLIKELY(!(x))) { \
129 BT_API2_LOGR("BT_ENSURE_TRUE_REJECT(" #x ") failed"); \
130 promise->MaybeReject(ret); \
131 return promise.forget(); \
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
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
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
{
229 enum BluetoothBondState
{
235 enum BluetoothTypeOfDevice
{
236 TYPE_OF_DEVICE_BREDR
,
241 enum BluetoothPropertyType
{
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
{
260 SCAN_MODE_CONNECTABLE
,
261 SCAN_MODE_CONNECTABLE_DISCOVERABLE
264 enum BluetoothSspVariant
{
265 SSP_VARIANT_PASSKEY_CONFIRMATION
,
266 SSP_VARIANT_PASSKEY_ENTRY
,
268 SSP_VARIANT_PASSKEY_NOTIFICATION
271 struct BluetoothUuid
{
275 struct BluetoothServiceRecord
{
281 struct BluetoothRemoteInfo
{
287 struct BluetoothProperty
{
289 BluetoothPropertyType mType
;
296 PROPERTY_REMOTE_FRIENDLY_NAME */
300 nsTArray
<BluetoothUuid
> mUuidArray
;
302 /* PROPERTY_ADAPTER_BONDED_DEVICES */
303 nsTArray
<nsString
> mStringArray
;
305 /* PROPERTY_CLASS_OF_DEVICE
306 PROPERTY_ADAPTER_DISCOVERY_TIMEOUT */
309 /* PROPERTY_RSSI_VALUE */
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
{
332 enum BluetoothHandsfreeAtResponse
{
333 HFP_AT_RESPONSE_ERROR
,
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
{
367 enum BluetoothHandsfreeCallMptyType
{
368 HFP_CALL_MPTY_TYPE_SINGLE
,
369 HFP_CALL_MPTY_TYPE_MULTI
372 enum BluetoothHandsfreeCallState
{
373 HFP_CALL_STATE_ACTIVE
,
375 HFP_CALL_STATE_DIALING
,
376 HFP_CALL_STATE_ALERTING
,
377 HFP_CALL_STATE_INCOMING
,
378 HFP_CALL_STATE_WAITING
,
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
{
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
{
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,
450 PLAYSTATUS_ERROR
= 0xFF,
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
,
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
{
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
{
508 struct BluetoothAvrcpNotificationParam
{
509 ControlPlayStatus mPlayStatus
;
514 uint8_t mValues
[256];
517 struct BluetoothAvrcpPlayerSettings
{
520 uint8_t mValues
[256];
523 struct BluetoothGattAdvData
{
524 uint8_t mAdvData
[62];
527 struct BluetoothGattId
{
532 struct BluetoothGattServiceId
{
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
;
547 struct BluetoothGattWriteParam
{
548 BluetoothGattServiceId mServiceId
;
549 BluetoothGattId mCharId
;
550 BluetoothGattId mDescriptorId
;
554 struct BluetoothGattNotifyParam
{
555 uint8_t mValue
[BLUETOOTH_GATT_MAX_ATTR_LEN
];
557 BluetoothGattServiceId mServiceId
;
558 BluetoothGattId mCharId
;
563 END_BLUETOOTH_NAMESPACE
565 #endif // mozilla_dom_bluetooth_bluetoothcommon_h__