• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <hardware/bluetooth.h>
21 
22 #include <variant>
23 #include <vector>
24 
25 namespace bluetooth {
26 namespace has {
27 
28 /** Connection State */
29 enum class ConnectionState : uint8_t {
30   DISCONNECTED = 0,
31   CONNECTING,
32   CONNECTED,
33   DISCONNECTING,
34 };
35 
36 /** Results codes for the failed preset operations */
37 enum class ErrorCode : uint8_t {
38   NO_ERROR = 0,
39   SET_NAME_NOT_ALLOWED,     // Preset cannot be written (read only preset)
40   OPERATION_NOT_SUPPORTED,  // If theres no optional characteristic,
41                             // or request opcode is invalid or not supported
42   OPERATION_NOT_POSSIBLE,   // Operation cannot be performed at this time
43   INVALID_PRESET_NAME_LENGTH,
44   INVALID_PRESET_INDEX,
45   GROUP_OPERATION_NOT_SUPPORTED,
46   PROCEDURE_ALREADY_IN_PROGRESS,
47 };
48 
49 enum class PresetInfoReason : uint8_t {
50   ALL_PRESET_INFO = 0,
51   PRESET_INFO_UPDATE,
52   PRESET_DELETED,
53   PRESET_AVAILABILITY_CHANGED,
54   PRESET_INFO_REQUEST_RESPONSE,
55 };
56 
57 struct PresetInfo {
58   uint8_t preset_index;
59 
60   bool writable;
61   bool available;
62   std::string preset_name;
63 };
64 
65 /** Service supported feature bits */
66 static constexpr uint8_t kFeatureBitHearingAidTypeBinaural = 0x00;
67 static constexpr uint8_t kFeatureBitHearingAidTypeMonaural = 0x01;
68 static constexpr uint8_t kFeatureBitHearingAidTypeBanded = 0x02;
69 static constexpr uint8_t kFeatureBitPresetSynchronizationSupported = 0x04;
70 static constexpr uint8_t kFeatureBitIndependentPresets = 0x08;
71 static constexpr uint8_t kFeatureBitDynamicPresets = 0x10;
72 static constexpr uint8_t kFeatureBitWritablePresets = 0x20;
73 
74 /** Invalid values for the group and preset identifiers */
75 static constexpr uint8_t kHasPresetIndexInvalid = 0x00;
76 static constexpr int kHasGroupIdInvalid = -1;
77 
78 class HasClientCallbacks {
79  public:
80   virtual ~HasClientCallbacks() = default;
81 
82   /** Callback for profile connection state change */
83   virtual void OnConnectionState(ConnectionState state,
84                                  const RawAddress& addr) = 0;
85 
86   /** Callback for the new available device */
87   virtual void OnDeviceAvailable(const RawAddress& addr, uint8_t features) = 0;
88 
89   /** Callback for getting device HAS flags */
90   virtual void OnFeaturesUpdate(const RawAddress& addr, uint8_t features) = 0;
91 
92   /** Callback for the currently active preset */
93   virtual void OnActivePresetSelected(
94       std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index) = 0;
95 
96   /** Callbacks for the active preset selection error */
97   virtual void OnActivePresetSelectError(
98       std::variant<RawAddress, int> addr_or_group_id, ErrorCode error_code) = 0;
99 
100   /** Callbacks for the preset details event */
101   virtual void OnPresetInfo(std::variant<RawAddress, int> addr_or_group_id,
102                             PresetInfoReason change_id,
103                             std::vector<PresetInfo> info_records) = 0;
104 
105   /** Callback for the preset details get error */
106   virtual void OnPresetInfoError(std::variant<RawAddress, int> addr_or_group_id,
107                                  uint8_t preset_index,
108                                  ErrorCode error_code) = 0;
109 
110   /** Callback for the preset name set error */
111   virtual void OnSetPresetNameError(
112       std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index,
113       ErrorCode error_code) = 0;
114 };
115 
116 class HasClientInterface {
117  public:
118   virtual ~HasClientInterface() = default;
119 
120   /** Register the Hearing Aid Service Client profile callbacks */
121   virtual void Init(HasClientCallbacks* callbacks) = 0;
122 
123   /** Connect to HAS service */
124   virtual void Connect(const RawAddress& addr) = 0;
125 
126   /** Disconnect from HAS service */
127   virtual void Disconnect(const RawAddress& addr) = 0;
128 
129   /** Select preset by the index as currently active */
130   virtual void SelectActivePreset(
131       std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index) = 0;
132 
133   /** Select next preset as currently active */
134   virtual void NextActivePreset(
135       std::variant<RawAddress, int> addr_or_group_id) = 0;
136 
137   /** Select previous preset as currently active */
138   virtual void PreviousActivePreset(
139       std::variant<RawAddress, int> addr_or_group_id) = 0;
140 
141   /** Get preset name by the index */
142   virtual void GetPresetInfo(const RawAddress& addr, uint8_t preset_index) = 0;
143 
144   /** Set preset name by the index */
145   virtual void SetPresetName(std::variant<RawAddress, int> addr_or_group_id,
146                              uint8_t preset_index, std::string name) = 0;
147 
148   /** Called when HAS capable device is unbonded */
149   virtual void RemoveDevice(const RawAddress& addr) = 0;
150 
151   /** Closes the interface */
152   virtual void Cleanup(void) = 0;
153 };
154 
155 }  // namespace has
156 }  // namespace bluetooth
157