1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AVRCP_TG_INTERNAL_H
17 #define AVRCP_TG_INTERNAL_H
18 
19 #include <cmath>
20 #include <cstring>
21 #include <deque>
22 #include <functional>
23 #include <list>
24 #include <map>
25 #include <memory>
26 #include <queue>
27 #include <vector>
28 
29 #include "avctp.h"
30 #include "base_def.h"
31 #include "btstack.h"
32 #include "interface_profile.h"
33 #include "log.h"
34 #include "message.h"
35 #include "packet.h"
36 #include "raw_address.h"
37 #include "timer.h"
38 
39 namespace OHOS {
40 namespace bluetooth {
41 /// The macro used to safely delete the pointer to an object created by[new].
42 template<typename T>
AvrcpTgSafeDelete(T * p)43 void AvrcpTgSafeDelete(T *p)
44 {
45     if (p != nullptr) {
46         delete p;
47         p = nullptr;
48     }
49 }
50 
51 /// The Version of AVRCP Profile is 1.6.
52 static const uint16_t AVRC_TG_PROFILE_REV_1_6 = 0x0106;
53 /// The default mtu size of the control channel.
54 /// 512 bytes is the smallest mtu.
55 static const int AVRC_TG_DEFAULT_CONTROL_MTU_SIZE = 512;
56 /// The default mtu size of the browse channel.
57 static const int AVRC_TG_DEFAULT_BROWSE_MTU_SIZE = 335;
58 /// The default Bluetooth SIG company ID.
59 static const uint32_t AVRC_TG_DEFAULT_BLUETOOTH_SIG_COMPANY_ID = 0x001958;
60 /// The default address of the bluetooth device.
61 static const std::string AVRC_TG_DEFAULT_BLUETOOTH_ADDRESS = "00:00:00:00:00:00";
62 /// The AVRCP TG shall return its response frame within 100 milliseconds counting from the receipt of the command frame.
63 static const int AVRC_TG_TIMER_T_RCP = 100;
64 /// The AVRCP TG shall return its response frame within 200 milliseconds counting from the receipt of the command frame.
65 static const int AVRC_TG_TIMER_T_MTC = 200;
66 /// The AVRCP TG shall return its response frame within 1000 milliseconds counting from the receipt of the command
67 /// frame.
68 static const int AVRC_TG_TIMER_T_MTP = 1000;
69 /// Either if the target has not received the pressed command within two seconds or the target receives the pressed
70 /// command with another operation id, then the target regards that the released command was sent but missed to receive.
71 /// @see AV/C Panel Subunit Specification 1.23 Section 9.4 PASS THROUGH control command.
72 static const int AVRC_TG_TIMER_PASS_THROUGH = 2000;
73 /// The default size of the command queue.
74 /// This means how many commands can be cached for a connection.
75 static const int AVRC_TG_DEFAULT_SIZE_OF_QUEUE = 20;
76 /// The maximum of number of device connections
77 static const int AVRC_TG_DEFAULT_MAX_OF_CONN = 6;
78     /**
79  * @brief This enumeration declares applicable to service class UUIDs that are registered into the SDP.
80  */
81 using AvrcTgServiceClassUUIDs = enum {
82     ///  A/V remote control target.
83     AVRC_TG_AV_REMOTE_CONTROL_TARGET = 0x110C,
84     /// A/V remote control.
85     /// NOTE: Used as both Service Class Identifier and Profile Identifier.
86     AVRC_TG_AV_REMOTE_CONTROL = 0x110E,
87     /// A/V remote control controller.
88     /// NOTE: The AVRCP specification v1.3 and later require that 0x110E also be included in the ServiceClassIDList
89     /// before 0x110F for backwards compatibility.
90     AVRC_TG_AV_REMOTE_CONTROL_CONTROLLER = 0x110F,
91 };
92 
93 
94 /**
95  * @brief This enumeration declares a set of values associated with the offset
96  * frame.
97  */
98 enum AvrcTgOffset : uint8_t {
99     AVRC_TG_OFFSET_ONE_BIT = 0x01,
100     AVRC_TG_OFFSET_TWO_BITS = 0x02,
101     AVRC_TG_OFFSET_THREE_BITS = 0x03,
102     AVRC_TG_OFFSET_FOUR_BITS = 0x04,
103     AVRC_TG_OFFSET_FIVE_BITS = 0x05,
104     AVRC_TG_OFFSET_SIX_BITS = 0x06,
105     AVRC_TG_OFFSET_SEVEN_BITS = 0x07,
106     AVRC_TG_OFFSET_EIGHT_BITS = 0x08,
107     AVRC_TG_OFFSET_SIXTEEN_BITS = 0x10,
108     AVRC_TG_OFFSET_SEVENTEEN_BITS = 0x11,
109     AVRC_TG_OFFSET_EIGHTEEN_BITS = 0x12,
110     AVRC_TG_OFFSET_NINETEEN_BITS = 0x13,
111     AVRC_TG_OFFSET_TWENTY_BITS = 0x14,
112     AVRC_TG_OFFSET_TWENTYONE_BITS = 0x15,
113     AVRC_TG_OFFSET_TWENTYTWO_BITS = 0x16,
114     AVRC_TG_OFFSET_TWENTYTHREE_BITS = 0x17,
115     AVRC_TG_OFFSET_TWENTYFOUR_BITS = 0x18,
116     AVRC_TG_OFFSET_TWENTYFIVE_BITS = 0x19,
117 };
118 
119 /**
120  * @brief This enumeration declares features that are supported by the AVRCP TG profile.
121  *
122  * @detail Some of the features will be registered into the SDP.
123  * @see Audio/Video Remote Control 1.6.2 Section 8 Service discovery interoperability requirements ->
124  *      Table 8.2: Service record for TG.
125  */
126 using AvrcTgFeatures = enum {
127     /// Invalid value.
128     AVRC_TG_FEATURE_INVALID_FEATURE = 0x0000,
129 
130     /// Supported category 1: player/recorder.
131     AVRC_TG_FEATURE_CATEGORY_1 = 0x0001,
132     /// Supported category 2: monitor/amplifier.
133     AVRC_TG_FEATURE_CATEGORY_2 = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_ONE_BIT),
134     /// Supported category 3: tuner.
135     AVRC_TG_FEATURE_CATEGORY_3 = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWO_BITS),
136     /// Supported category 4: menu.
137     AVRC_TG_FEATURE_CATEGORY_4 = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_THREE_BITS),
138     /// Supported player application settings.
139     AVRC_TG_FEATURE_PLAYER_APPLICATION_SETTINGS = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_FOUR_BITS),
140     /// Supported group navigation.
141     AVRC_TG_FEATURE_GROUP_NAVIGATION = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_FIVE_BITS),
142     /// Supported browsing.
143     /// This in the SDP record shall only be supported if browsing of the "Media Player Virtual File system" is
144     /// supported.
145     AVRC_TG_FEATURE_BROWSING = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_SIX_BITS),
146     /// Supported multiple media player applications.
147     AVRC_TG_FEATURE_MULTIPLE_MEDIA_PLAYER_APPLICATIONS = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_SEVEN_BITS),
148     /// Supported cover art.
149     AVRC_TG_FEATURE_COVER_ART = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_EIGHT_BITS),
150 
151     /// Supported the key operation.
152     AVRC_TG_FEATURE_KEY_OPERATION = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_SIXTEEN_BITS),
153     /// Supported the absolute volume.
154     AVRC_TG_FEATURE_ABSOLUTE_VOLUME = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_SEVENTEEN_BITS),
155     /// Supported the notification of "PLAYBACK_STATUS_CHANGED"
156     AVRC_TG_FEATURE_NOTIFY_PLAYBACK_STATUS_CHANGED = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_EIGHTEEN_BITS),
157     /// Supported the notification of "TRACK_REACHED_END"
158     AVRC_TG_FEATURE_NOTIFY_TRACK_REACHED_END = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_NINETEEN_BITS),
159     /// Supported the notification of "TRACK_REACHED_START"
160     AVRC_TG_FEATURE_NOTIFY_TRACK_REACHED_START = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWENTY_BITS),
161     /// Supported the notification of "PLAYBACK_POSITION_CHANGED"
162     AVRC_TG_FEATURE_NOTIFY_PLAYBACK_POSITION_CHANGED = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWENTYONE_BITS),
163     /// Supported the notification of "PLAYER_SETTING_CHANGED"
164     AVRC_TG_FEATURE_NOTIFY_PLAYER_SETTING_CHANGED = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWENTYTWO_BITS),
165     /// Supported the notification of "NOW_PLAYING_CONTENT_CHANGED"
166     AVRC_TG_FEATURE_NOTIFY_NOW_PLAYING_CONTENT_CHANGED =
167         (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWENTYTHREE_BITS),
168     /// Supported the notification of "UIDS_CHANGED"
169     AVRC_TG_FEATURE_NOTIFY_UIDS_CHANGED = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWENTYFOUR_BITS),
170     /// Supported the notification of "ABSOLUTE_VOLUME_CHANGED"
171     AVRC_TG_FEATURE_NOTIFY_ABSOLUTE_VOLUME_CHANGED = (AVRC_TG_FEATURE_CATEGORY_1 << AVRC_TG_OFFSET_TWENTYFIVE_BITS),
172 };
173 }  // namespace bluetooth
174 }  // namespace OHOS
175 
176 #endif  // !AVRCP_TG_INTERNAL_H
177