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 #include "a2dp_service_state_machine.h"
17 #include "a2dp_service.h"
18 #include "a2dp_sink.h"
19 #include "a2dp_source.h"
20 #include "log.h"
21
22 namespace OHOS {
23 namespace bluetooth {
Dispatch(const utility::Message & msg)24 bool A2dpDisconnected::Dispatch(const utility::Message &msg)
25 {
26 LOG_INFO("[A2dpDisconnected] %{public}s role[%u]\n", __func__, msg.arg1_);
27
28 int ret = A2DP_SUCCESS;
29 uint8_t role = msg.arg1_;
30 A2dpProfile *pflA2dp = GetProfileInstance(role);
31 BtAddr btAddr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x00};
32 RawAddress rawAddr = *((RawAddress *)(msg.arg2_));
33 rawAddr.ConvertToUint8(btAddr.addr);
34
35 A2dpService *service = GetServiceInstance(msg.arg1_);
36 switch (msg.what_) {
37 case A2DP_MSG_CONNECT:
38 Transition(A2DP_STATE_CONNECTING);
39 ret = pflA2dp->Connect(btAddr);
40 service->ProcessConnectFrameworkCallback(static_cast<int>(BTConnectState::CONNECTING), rawAddr);
41 break;
42 case A2DP_MSG_PROFILE_CONNECTED:
43 Transition(A2DP_STATE_CONNECTED);
44 break;
45 case A2DP_MSG_PROFILE_CONNECTING:
46 Transition(A2DP_STATE_CONNECTING);
47 service->ProcessConnectFrameworkCallback(static_cast<int>(BTConnectState::CONNECTING), rawAddr);
48 break;
49 case A2DP_MSG_CONNECT_FORBIDDEN:
50 ret = pflA2dp->Disconnect(btAddr);
51 break;
52 default:
53 break;
54 }
55
56 return (ret == A2DP_SUCCESS) ? true : false;
57 }
58
Dispatch(const utility::Message & msg)59 bool A2dpDisconnecting::Dispatch(const utility::Message &msg)
60 {
61 LOG_INFO("[A2dpDisconnecting] %{public}s role[%u]\n", __func__, msg.arg1_);
62
63 switch (msg.what_) {
64 case A2DP_MSG_PROFILE_DISCONNECTED:
65 Transition(A2DP_STATE_DISCONNECTED);
66 break;
67 default:
68 break;
69 }
70 return true;
71 }
72
Dispatch(const utility::Message & msg)73 bool A2dpConnected::Dispatch(const utility::Message &msg)
74 {
75 int ret = A2DP_SUCCESS;
76 uint8_t role = msg.arg1_;
77 A2dpProfile *pflA2dp = GetProfileInstance(role);
78 BtAddr btAddr = {};
79 RawAddress rawAddr = *((RawAddress *)(msg.arg2_));
80 A2dpService *service = GetServiceInstance(role);
81 A2dpDeviceInfo *deviceInfo = nullptr;
82 rawAddr.ConvertToUint8(btAddr.addr);
83 if (pflA2dp == nullptr || service == nullptr) {
84 return false;
85 }
86 deviceInfo = service->GetDeviceFromList(rawAddr);
87 if (deviceInfo == nullptr) {
88 LOG_ERROR("[A2dpConnected] %{public}s Can't get the device by address\n", __func__);
89 return false;
90 }
91 switch (msg.what_) {
92 case A2DP_MSG_DISCONNECT:
93 Transition(A2DP_STATE_DISCONNECTING);
94 ret = pflA2dp->Disconnect(btAddr);
95 service->ProcessConnectFrameworkCallback(static_cast<int>(BTConnectState::DISCONNECTING), rawAddr);
96 break;
97 case A2DP_MSG_PROFILE_DISCONNECTED:
98 Transition(A2DP_STATE_DISCONNECTED);
99 break;
100 case A2DP_MSG_PROFILE_DISCONNECTING:
101 Transition(A2DP_STATE_DISCONNECTING);
102 service->ProcessConnectFrameworkCallback(static_cast<int>(BTConnectState::DISCONNECTING), rawAddr);
103 break;
104 case A2DP_MSG_PROFILE_AUDIO_PLAY_START:
105 UpdateDeviceInformation(rawAddr, true, role);
106 break;
107 case A2DP_MSG_PROFILE_AUDIO_PLAY_SUSPEND:
108 case A2DP_MSG_PROFILE_AUDIO_PLAY_STOP:
109 UpdateDeviceInformation(rawAddr, false, role);
110 break;
111 case A2DP_MSG_PROFILE_CONNECTING:
112 Transition(A2DP_STATE_CONNECTING);
113 service->ProcessConnectFrameworkCallback(static_cast<int>(BTConnectState::CONNECTING), rawAddr);
114 UpdateDeviceInformation(rawAddr, false, role);
115 break;
116 default:
117 break;
118 }
119 return (ret == A2DP_SUCCESS) ? true : false;
120 }
121
UpdateDeviceInformation(RawAddress rawAddr,bool value,uint8_t role)122 void A2dpConnected::UpdateDeviceInformation(RawAddress rawAddr, bool value, uint8_t role)
123 {
124 LOG_INFO("[A2dpConnected] %{public}s PlayingStatus[%{public}d]\n", __func__, value);
125
126 A2dpDeviceInfo *deviceInfo = nullptr;
127 A2dpService *service = GetServiceInstance(role);
128
129 if (service == nullptr) {
130 LOG_ERROR("[A2dpConnected] %{public}s Can't get the service of a2dp\n", __func__);
131 return;
132 }
133
134 deviceInfo = service->GetDeviceFromList(rawAddr);
135 if (deviceInfo == nullptr) {
136 LOG_ERROR("[A2dpConnected] %{public}s Can't get the device by address\n", __func__);
137 return;
138 }
139
140 if (deviceInfo->GetPlayingState()) {
141 deviceInfo->SetPlayingState(value);
142 }
143 }
144
Dispatch(const utility::Message & msg)145 bool A2dpConnecting::Dispatch(const utility::Message &msg)
146 {
147 LOG_INFO("[A2dpConnecting] %{public}s role[%u]\n", __func__, msg.arg1_);
148
149 int ret = A2DP_SUCCESS;
150 uint8_t role = msg.arg1_;
151 A2dpProfile *pflA2dp = GetProfileInstance(role);
152 BtAddr btAddr;
153 RawAddress rawAddr = *((RawAddress *)(msg.arg2_));
154 rawAddr.ConvertToUint8(btAddr.addr);
155 A2dpService *service = GetServiceInstance(role);
156
157 switch (msg.what_) {
158 case A2DP_MSG_CONNECT_TIMEOUT:
159 ret = pflA2dp->Disconnect(btAddr);
160 break;
161 case A2DP_MSG_PROFILE_CONNECTED:
162 Transition(A2DP_STATE_CONNECTED);
163 break;
164 case A2DP_MSG_PROFILE_DISCONNECTED:
165 Transition(A2DP_STATE_DISCONNECTED);
166 break;
167 case A2DP_MSG_PROFILE_DISCONNECTING:
168 Transition(A2DP_STATE_DISCONNECTING);
169 service->ProcessConnectFrameworkCallback(static_cast<int>(BTConnectState::DISCONNECTING), rawAddr);
170 break;
171 case A2DP_MSG_DISCONNECT:
172 ret = pflA2dp->Disconnect(btAddr);
173 break;
174 case A2DP_MSG_CONNECT_FORBIDDEN:
175 ret = pflA2dp->Disconnect(btAddr);
176 break;
177 default:
178 break;
179 }
180
181 return (ret == A2DP_SUCCESS) ? true : false;
182 }
183
SetRole(uint8_t role)184 void A2dpStateManager::SetRole(uint8_t role)
185 {
186 role_ = role;
187 }
188 } // namespace bluetooth
189 } // namespace OHOS