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 // Must be kept in sync with BluetoothProfile.java
30 enum class ConnectionState : uint8_t {
31   DISCONNECTED = 0,
32   CONNECTING,
33   CONNECTED,
34   DISCONNECTING,
35 };
36 
37 /** Results codes for the failed preset operations */
38 enum class ErrorCode : uint8_t {
39   NO_ERROR = 0,
40   SET_NAME_NOT_ALLOWED,     // Preset cannot be written (read only preset)
41   OPERATION_NOT_SUPPORTED,  // If theres no optional characteristic,
42                             // or request opcode is invalid or not supported
43   OPERATION_NOT_POSSIBLE,   // Operation cannot be performed at this time
44   INVALID_PRESET_NAME_LENGTH,
45   INVALID_PRESET_INDEX,
46   GROUP_OPERATION_NOT_SUPPORTED,
47   PROCEDURE_ALREADY_IN_PROGRESS,
48 };
49 
50 enum class PresetInfoReason : uint8_t {
51   ALL_PRESET_INFO = 0,
52   PRESET_INFO_UPDATE,
53   PRESET_DELETED,
54   PRESET_AVAILABILITY_CHANGED,
55   PRESET_INFO_REQUEST_RESPONSE,
56 };
57 
58 struct PresetInfo {
59   uint8_t preset_index;
60 
61   bool writable;
62   bool available;
63   std::string preset_name;
64 };
65 
66 /** Service supported feature bits */
67 static constexpr uint8_t kFeatureBitHearingAidTypeBinaural = 0x00;
68 static constexpr uint8_t kFeatureBitHearingAidTypeMonaural = 0x01;
69 static constexpr uint8_t kFeatureBitHearingAidTypeBanded = 0x02;
70 static constexpr uint8_t kFeatureBitPresetSynchronizationSupported = 0x04;
71 static constexpr uint8_t kFeatureBitIndependentPresets = 0x08;
72 static constexpr uint8_t kFeatureBitDynamicPresets = 0x10;
73 static constexpr uint8_t kFeatureBitWritablePresets = 0x20;
74 
75 /** Invalid values for the group and preset identifiers */
76 static constexpr uint8_t kHasPresetIndexInvalid = 0x00;
77 static constexpr int kHasGroupIdInvalid = -1;
78 
79 class HasClientCallbacks {
80 public:
81   virtual ~HasClientCallbacks() = default;
82 
83   /** Callback for profile connection state change */
84   virtual void OnConnectionState(ConnectionState state, 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(std::variant<RawAddress, int> addr_or_group_id,
94                                       uint8_t preset_index) = 0;
95 
96   /** Callbacks for the active preset selection error */
97   virtual void OnActivePresetSelectError(std::variant<RawAddress, int> addr_or_group_id,
98                                          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, std::vector<PresetInfo> info_records) = 0;
103 
104   /** Callback for the preset details get error */
105   virtual void OnPresetInfoError(std::variant<RawAddress, int> addr_or_group_id,
106                                  uint8_t preset_index, ErrorCode error_code) = 0;
107 
108   /** Callback for the preset name set error */
109   virtual void OnSetPresetNameError(std::variant<RawAddress, int> addr_or_group_id,
110                                     uint8_t preset_index, ErrorCode error_code) = 0;
111 };
112 
113 class HasClientInterface {
114 public:
115   virtual ~HasClientInterface() = default;
116 
117   /** Register the Hearing Aid Service Client profile callbacks */
118   virtual void Init(HasClientCallbacks* callbacks) = 0;
119 
120   /** Connect to HAS service */
121   virtual void Connect(const RawAddress& addr) = 0;
122 
123   /** Disconnect from HAS service */
124   virtual void Disconnect(const RawAddress& addr) = 0;
125 
126   /** Select preset by the index as currently active */
127   virtual void SelectActivePreset(std::variant<RawAddress, int> addr_or_group_id,
128                                   uint8_t preset_index) = 0;
129 
130   /** Select next preset as currently active */
131   virtual void NextActivePreset(std::variant<RawAddress, int> addr_or_group_id) = 0;
132 
133   /** Select previous preset as currently active */
134   virtual void PreviousActivePreset(std::variant<RawAddress, int> addr_or_group_id) = 0;
135 
136   /** Get preset name by the index */
137   virtual void GetPresetInfo(const RawAddress& addr, uint8_t preset_index) = 0;
138 
139   /** Set preset name by the index */
140   virtual void SetPresetName(std::variant<RawAddress, int> addr_or_group_id, uint8_t preset_index,
141                              std::string name) = 0;
142 
143   /** Called when HAS capable device is unbonded */
144   virtual void RemoveDevice(const RawAddress& addr) = 0;
145 
146   /** Closes the interface */
147   virtual void Cleanup(void) = 0;
148 };
149 
150 }  // namespace has
151 }  // namespace bluetooth
152