1 /*
2  * Copyright (c) 2022 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 "daudio_ipc_callback_stub.h"
17 
18 #include "daudio_errorcode.h"
19 #include "daudio_log.h"
20 
21 #undef DH_LOG_TAG
22 #define DH_LOG_TAG "DAudioIpcCallbackStub"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
DAudioIpcCallbackStub()26 DAudioIpcCallbackStub::DAudioIpcCallbackStub() : IRemoteStub(true)
27 {
28     memberFuncMap_[NOTIFY_REGRESULT] = &DAudioIpcCallbackStub::OnNotifyRegResultInner;
29     memberFuncMap_[NOTIFY_UNREGRESULT] = &DAudioIpcCallbackStub::OnNotifyUnregResultInner;
30     memberFuncMap_[NOTIFY_STATE_CHANGED] = &DAudioIpcCallbackStub::OnHardwareStateChangedInner;
31     memberFuncMap_[NOTIFY_DATASYNC_TRIGGER] = &DAudioIpcCallbackStub::OnDataSyncTriggerInner;
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t DAudioIpcCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
35     MessageOption &option)
36 {
37     DHLOGI("On remote request, code: %{public}u", code);
38     std::u16string desc = DAudioIpcCallbackStub::GetDescriptor();
39     std::u16string remoteDesc = data.ReadInterfaceToken();
40     if (desc != remoteDesc) {
41         DHLOGE("RemoteDesc is invalid.");
42         return ERR_DH_AUDIO_SA_INVALID_INTERFACE_TOKEN;
43     }
44 
45     switch (code) {
46         case NOTIFY_REGRESULT:
47             return OnNotifyRegResultInner(data, reply, option);
48         case NOTIFY_UNREGRESULT:
49             return OnNotifyUnregResultInner(data, reply, option);
50         case NOTIFY_STATE_CHANGED:
51             return OnHardwareStateChangedInner(data, reply, option);
52         case NOTIFY_DATASYNC_TRIGGER:
53             return OnDataSyncTriggerInner(data, reply, option);
54         default:
55             DHLOGE("Invalid request code.");
56             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57     }
58     return ERR_DH_AUDIO_NOT_FOUND_KEY;
59 }
60 
OnNotifyRegResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)61 int32_t DAudioIpcCallbackStub::OnNotifyRegResultInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
62 {
63     std::string networkId = data.ReadString();
64     std::string dhId = data.ReadString();
65     std::string reqId = data.ReadString();
66     int32_t status = data.ReadInt32();
67     std::string resultData = data.ReadString();
68     int32_t ret = OnNotifyRegResult(networkId, dhId, reqId, status, resultData);
69     return ret;
70 }
71 
OnNotifyUnregResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)72 int32_t DAudioIpcCallbackStub::OnNotifyUnregResultInner(MessageParcel &data, MessageParcel &reply,
73     MessageOption &option)
74 {
75     std::string networkId = data.ReadString();
76     std::string dhId = data.ReadString();
77     std::string reqId = data.ReadString();
78     int32_t status = data.ReadInt32();
79     std::string resultData = data.ReadString();
80     int32_t ret = OnNotifyUnregResult(networkId, dhId, reqId, status, resultData);
81     return ret;
82 }
83 
OnHardwareStateChangedInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)84 int32_t DAudioIpcCallbackStub::OnHardwareStateChangedInner(MessageParcel &data, MessageParcel &reply,
85     MessageOption &option)
86 {
87     std::string networkId = data.ReadString();
88     std::string dhId = data.ReadString();
89     int32_t status = data.ReadInt32();
90     int32_t ret = OnHardwareStateChanged(networkId, dhId, status);
91     return ret;
92 }
93 
OnDataSyncTriggerInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)94 int32_t DAudioIpcCallbackStub::OnDataSyncTriggerInner(MessageParcel &data, MessageParcel &reply,
95     MessageOption &option)
96 {
97     std::string networkId = data.ReadString();
98     int32_t ret = OnDataSyncTrigger(networkId);
99     return ret;
100 }
101 } // DistributedHardware
102 } // OHOS