summaryrefslogtreecommitdiff
path: root/system/include/hardware/bt_has.h
blob: 739edc041fe959f0379996ad0da19c4461b31b03 (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
/*
 * Copyright 2021 HIMSA II K/S - www.himsa.com.
 * Represented by EHIMA - www.ehima.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <hardware/bluetooth.h>

#include <variant>
#include <vector>

namespace bluetooth {
namespace has {

/** Connection State */
enum class ConnectionState : uint8_t {
  DISCONNECTED = 0,
  CONNECTING,
  CONNECTED,
  DISCONNECTING,
};

/** Results codes for the failed preset operations */
enum class ErrorCode : uint8_t {
  NO_ERROR = 0,
  SET_NAME_NOT_ALLOWED,     // Preset cannot be written (read only preset)
  OPERATION_NOT_SUPPORTED,  // If theres no optional characteristic,
                            // or request opcode is invalid or not supported
  OPERATION_NOT_POSSIBLE,   // Operation cannot be performed at this time
  INVALID_PRESET_NAME_LENGTH,
  INVALID_PRESET_INDEX,
  GROUP_OPERATION_NOT_SUPPORTED,
  PROCEDURE_ALREADY_IN_PROGRESS,
};

enum class PresetInfoReason : uint8_t {
  ALL_PRESET_INFO = 0,
  PRESET_INFO_UPDATE,
  PRESET_DELETED,
  PRESET_AVAILABILITY_CHANGED,
  PRESET_INFO_REQUEST_RESPONSE,
};

struct PresetInfo {
  uint8_t preset_index;

  bool writable;
  bool available;
  std::string preset_name;
};

/** Service supported feature bits */
static constexpr uint8_t kFeatureBitHearingAidTypeBinaural = 0x00;
static constexpr uint8_t kFeatureBitHearingAidTypeMonaural = 0x01;
static constexpr uint8_t kFeatureBitHearingAidTypeBanded = 0x02;
static constexpr uint8_t kFeatureBitPresetSynchronizationSupported = 0x04;
static constexpr uint8_t kFeatureBitIndependentPresets = 0x08;
static constexpr uint8_t kFeatureBitDynamicPresets = 0x10;
static constexpr uint8_t kFeatureBitWritablePresets = 0x20;

/** Invalid values for the group and preset identifiers */
static constexpr uint8_t kHasPresetIndexInvalid = 0x00;
static constexpr int kHasGroupIdInvalid = -1;

class HasClientCallbacks {
 public:
  virtual ~HasClientCallbacks() = default;

  /** Callback for profile connection state change */
  virtual void OnConnectionState(ConnectionState state,
                                 const RawAddress& addr) = 0;

  /** Callback for the new available device */
  virtual void OnDeviceAvailable(const RawAddress& addr, uint8_t features) = 0;

  /** Callback for getting device HAS flags */
  virtual void OnFeaturesUpdate(const RawAddress& addr, uint8_t features) = 0;

  /** Callback for the currently active preset */
  virtual void OnActivePresetSelected(
      std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index) = 0;

  /** Callbacks for the active preset selection error */
  virtual void OnActivePresetSelectError(
      std::variant<RawAddress, int> addr_or_group_id, ErrorCode error_code) = 0;

  /** Callbacks for the preset details event */
  virtual void OnPresetInfo(std::variant<RawAddress, int> addr_or_group_id,
                            PresetInfoReason change_id,
                            std::vector<PresetInfo> info_records) = 0;

  /** Callback for the preset details get error */
  virtual void OnPresetInfoError(std::variant<RawAddress, int> addr_or_group_id,
                                 uint8_t preset_index,
                                 ErrorCode error_code) = 0;

  /** Callback for the preset name set error */
  virtual void OnSetPresetNameError(
      std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index,
      ErrorCode error_code) = 0;
};

class HasClientInterface {
 public:
  virtual ~HasClientInterface() = default;

  /** Register the Hearing Aid Service Client profile callbacks */
  virtual void Init(HasClientCallbacks* callbacks) = 0;

  /** Connect to HAS service */
  virtual void Connect(const RawAddress& addr) = 0;

  /** Disconnect from HAS service */
  virtual void Disconnect(const RawAddress& addr) = 0;

  /** Select preset by the index as currently active */
  virtual void SelectActivePreset(
      std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index) = 0;

  /** Select next preset as currently active */
  virtual void NextActivePreset(
      std::variant<RawAddress, int> addr_or_group_id) = 0;

  /** Select previous preset as currently active */
  virtual void PreviousActivePreset(
      std::variant<RawAddress, int> addr_or_group_id) = 0;

  /** Get preset name by the index */
  virtual void GetPresetInfo(const RawAddress& addr, uint8_t preset_index) = 0;

  /** Set preset name by the index */
  virtual void SetPresetName(std::variant<RawAddress, int> addr_or_group_id,
                             uint8_t preset_index, std::string name) = 0;

  /** Called when HAS capable device is unbonded */
  virtual void RemoveDevice(const RawAddress& addr) = 0;

  /** Closes the interface */
  virtual void Cleanup(void) = 0;
};

}  // namespace has
}  // namespace bluetooth