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 "hfp_ag_profile_event_sender.h"
17 
18 #include "hfp_ag_defines.h"
19 #include "hfp_ag_service.h"
20 
21 namespace OHOS {
22 namespace bluetooth {
GetInstance()23 HfpAgProfileEventSender &HfpAgProfileEventSender::GetInstance()
24 {
25     static HfpAgProfileEventSender hfpAgUplinkService;
26     return hfpAgUplinkService;
27 }
28 
SendMessageToService(const HfpAgMessage & msg)29 void HfpAgProfileEventSender::SendMessageToService(const HfpAgMessage &msg)
30 {
31     HfpAgService *service = HfpAgService::GetService();
32     if (service != nullptr) {
33         service->PostEvent(msg);
34     }
35 }
36 
AnswerCall(const std::string & device) const37 void HfpAgProfileEventSender::AnswerCall(const std::string &device) const
38 {
39     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
40     msg.dev_ = device;
41     msg.type_ = HFP_AG_MSG_TYPE_ANSWER_CALL;
42     SendMessageToService(msg);
43 }
44 
HangupCall(const std::string & device) const45 void HfpAgProfileEventSender::HangupCall(const std::string &device) const
46 {
47     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
48     msg.dev_ = device;
49     msg.type_ = HFP_AG_MSG_TYPE_HANGUP_CALL;
50     SendMessageToService(msg);
51 }
52 
HfVolumeChanged(const std::string & device,int type,int volume) const53 void HfpAgProfileEventSender::HfVolumeChanged(const std::string &device, int type, int volume) const
54 {
55     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT, type);
56     msg.arg3_ = volume;
57     msg.dev_ = device;
58     msg.type_ = HFP_AG_MSG_TYPE_VOLUME_CHANGED;
59     SendMessageToService(msg);
60 }
61 
DialOutCall(const std::string & device,const std::string & number,int dialType) const62 void HfpAgProfileEventSender::DialOutCall(const std::string &device, const std::string &number, int dialType) const
63 {
64     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
65     msg.dev_ = device;
66     msg.str_ = number;
67     msg.type_ = HFP_AG_MSG_TYPE_DIAL_CALL;
68     msg.arg1_ = dialType;
69     SendMessageToService(msg);
70 }
71 
SendDtmf(const std::string & device,int dtmf) const72 void HfpAgProfileEventSender::SendDtmf(const std::string &device, int dtmf) const
73 {
74     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT, dtmf);
75     msg.dev_ = device;
76     msg.type_ = HFP_AG_MSG_TYPE_SEND_DTMF;
77     msg.arg1_ = dtmf;
78     SendMessageToService(msg);
79 }
80 
EnableNoiseReduction(const std::string & device,bool enable) const81 void HfpAgProfileEventSender::EnableNoiseReduction(const std::string &device, bool enable) const
82 {
83     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
84     msg.dev_ = device;
85     msg.type_ = HFP_AG_MSG_TYPE_NOISE_REDUCTION;
86     msg.arg1_ = enable ? 1 : 0;
87     SendMessageToService(msg);
88 }
89 
VoiceRecognitionStateChanged(const std::string & device,int status) const90 void HfpAgProfileEventSender::VoiceRecognitionStateChanged(const std::string &device, int status) const
91 {
92     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
93     msg.dev_ = device;
94     msg.type_ = HFP_AG_MSG_TYPE_VR_CHANGED;
95     msg.arg1_ = status;
96     SendMessageToService(msg);
97 }
98 
HoldCall(const std::string & device,int chld) const99 void HfpAgProfileEventSender::HoldCall(const std::string &device, int chld) const
100 {
101     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT, chld);
102     msg.dev_ = device;
103     msg.type_ = HFP_AG_MSG_TYPE_AT_CHLD;
104     SendMessageToService(msg);
105 }
106 
GetSubscriberNumber(const std::string & device) const107 void HfpAgProfileEventSender::GetSubscriberNumber(const std::string &device) const
108 {
109     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
110     msg.dev_ = device;
111     msg.type_ = HFP_AG_MSG_TYPE_SUBSCRIBER_NUMBER_REQUEST;
112     SendMessageToService(msg);
113 }
114 
GetAgIndicator(const std::string & device) const115 void HfpAgProfileEventSender::GetAgIndicator(const std::string &device) const
116 {
117     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
118     msg.dev_ = device;
119     msg.type_ = HFP_AG_MSG_TYPE_AT_CIND;
120     SendMessageToService(msg);
121 }
122 
GetNetworkOperater(const std::string & device) const123 void HfpAgProfileEventSender::GetNetworkOperater(const std::string &device) const
124 {
125     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
126     msg.dev_ = device;
127     msg.type_ = HFP_AG_MSG_TYPE_AT_COPS;
128     SendMessageToService(msg);
129 }
130 
GetCurrentCalls(const std::string & device) const131 void HfpAgProfileEventSender::GetCurrentCalls(const std::string &device) const
132 {
133     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
134     msg.dev_ = device;
135     msg.type_ = HFP_AG_MSG_TYPE_AT_CLCC;
136     SendMessageToService(msg);
137 }
138 
ProcessATBind(const std::string & device,const std::string & atString) const139 void HfpAgProfileEventSender::ProcessATBind(const std::string &device, const std::string &atString) const
140 {
141     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
142     msg.dev_ = device;
143     msg.str_ = atString;
144     msg.type_ = HFP_AG_MSG_TYPE_AT_BIND;
145     SendMessageToService(msg);
146 }
147 
SendHfIndicator(const std::string & device,int indId,int indValue) const148 void HfpAgProfileEventSender::SendHfIndicator(const std::string &device, int indId, int indValue) const
149 {
150     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT, indId);
151     msg.arg3_ = indValue;
152     msg.dev_ = device;
153     msg.type_ = HFP_AG_MSG_TYPE_AT_BIEV;
154     SendMessageToService(msg);
155 }
156 
ProcessAtBia(const std::string & device,const HfpAgTransferData & data) const157 void HfpAgProfileEventSender::ProcessAtBia(const std::string &device, const HfpAgTransferData &data) const
158 {
159     HfpAgMessage msg(HFP_AG_CONTROL_OTHER_MODULES_EVT);
160     msg.dev_ = device;
161     msg.type_ = HFP_AG_MSG_TYPE_AT_BIA;
162     msg.data_ = data;
163     SendMessageToService(msg);
164 }
165 
ConnectRequest(const std::string & device,int handle,int what) const166 void HfpAgProfileEventSender::ConnectRequest(const std::string &device, int handle, int what) const
167 {
168     HfpAgMessage msg(what);
169     msg.dev_ = device;
170     msg.arg1_ = handle;
171     SendMessageToService(msg);
172 }
173 
UpdateConnectState(const std::string & device,int what) const174 void HfpAgProfileEventSender::UpdateConnectState(const std::string &device, int what) const
175 {
176     HfpAgMessage msg(what);
177     msg.dev_ = device;
178     SendMessageToService(msg);
179 }
180 
ScoConnectRequest(const std::string & device,int what,uint8_t linkType) const181 void HfpAgProfileEventSender::ScoConnectRequest(const std::string &device, int what, uint8_t linkType) const
182 {
183     HfpAgMessage msg(what);
184     msg.arg1_ = linkType;
185     msg.dev_ = device;
186     SendMessageToService(msg);
187 }
188 
UpdateScoConnectState(const std::string & device,int what) const189 void HfpAgProfileEventSender::UpdateScoConnectState(const std::string &device, int what) const
190 {
191     HfpAgMessage msg(what);
192     msg.dev_ = device;
193     SendMessageToService(msg);
194 }
195 
GetResponseHoldState(const std::string & device,int what) const196 void HfpAgProfileEventSender::GetResponseHoldState(const std::string &device, int what) const
197 {
198     HfpAgMessage msg(what);
199     msg.dev_ = device;
200     msg.type_ = what;
201     SendMessageToService(msg);
202 }
203 
SetResponseHoldState(const std::string & device,int what,int btrh) const204 void HfpAgProfileEventSender::SetResponseHoldState(const std::string &device, int what, int btrh) const
205 {
206     HfpAgMessage msg(what);
207     msg.dev_ = device;
208     msg.type_ = what;
209     msg.arg1_ = btrh;
210     SendMessageToService(msg);
211 }
212 
ProcessSdpDiscoveryResult(const std::string & device,int what) const213 void HfpAgProfileEventSender::ProcessSdpDiscoveryResult(const std::string &device, int what) const
214 {
215     HfpAgMessage msg(what);
216     msg.dev_ = device;
217     SendMessageToService(msg);
218 }
219 
ProcessAtBcc(const std::string & device) const220 void HfpAgProfileEventSender::ProcessAtBcc(const std::string &device) const
221 {
222     HfpAgMessage msg(HFP_AG_CONNECT_AUDIO_EVT);
223     msg.dev_ = device;
224     SendMessageToService(msg);
225 }
226 
ProcessAtBcs(const std::string & device) const227 void HfpAgProfileEventSender::ProcessAtBcs(const std::string &device) const
228 {
229     HfpAgMessage msg(HFP_AG_CODEC_NEGOTIATED_EVT);
230     msg.dev_ = device;
231     SendMessageToService(msg);
232 }
233 
RemoveStateMachine(const std::string & device) const234 void HfpAgProfileEventSender::RemoveStateMachine(const std::string &device) const
235 {
236     HfpAgMessage msg(HFP_AG_REMOVE_STATE_MACHINE_EVT);
237     msg.dev_ = device;
238     SendMessageToService(msg);
239 }
240 
SendRingAndClip(const std::string & device) const241 void HfpAgProfileEventSender::SendRingAndClip(const std::string &device) const
242 {
243     HfpAgMessage msg(HFP_AG_RING_TIMEOUT_EVT);
244     msg.dev_ = device;
245     SendMessageToService(msg);
246 }
247 
ProcessCKpdEvent(const std::string & device) const248 void HfpAgProfileEventSender::ProcessCKpdEvent(const std::string &device) const
249 {
250     HfpAgMessage msg(HFP_AG_PROCESS_CKPD_EVT);
251     msg.dev_ = device;
252     SendMessageToService(msg);
253 }
254 
GetVoiceTagNumber(const std::string & device) const255 void HfpAgProfileEventSender::GetVoiceTagNumber(const std::string &device) const
256 {
257     HfpAgMessage msg(HFP_AG_GET_VOICE_NUMBER);
258     msg.dev_ = device;
259     SendMessageToService(msg);
260 }
261 
GetDispatchter() const262 utility::Dispatcher *HfpAgProfileEventSender::GetDispatchter() const
263 {
264     HfpAgService *service = HfpAgService::GetService();
265     if (service != nullptr) {
266         return service->GetDispatcher();
267     } else {
268         LOG_ERROR("[HFP AG]%{public}s():hfpAgService_ is nullptr!", __FUNCTION__);
269         return nullptr;
270     }
271 }
272 }  // namespace bluetooth
273 }  // namespace OHOS
274