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 "dcamera_source_callback_stub.h"
17 #include "distributed_camera_errno.h"
18 #include "distributed_hardware_log.h"
19 #include "ipc_object_stub.h"
20 #include "ipc_types.h"
21 #include "message_parcel.h"
22 namespace OHOS { class MessageOption; }
23 
24 namespace OHOS {
25 namespace DistributedHardware {
DCameraSourceCallbackStub()26 DCameraSourceCallbackStub::DCameraSourceCallbackStub() : IRemoteStub(true)
27 {
28     memberFuncMap_[NOTIFY_REG_RESULT] = &DCameraSourceCallbackStub::NotifyRegResultInner;
29     memberFuncMap_[NOTIFY_UNREG_RESULT] = &DCameraSourceCallbackStub::NotifyUnregResultInner;
30     memberFuncMap_[NOTIFY_STATE_CHANGED] = &DCameraSourceCallbackStub::OnHardwareStateChangedInner;
31     memberFuncMap_[NOTIFY_DATASYNC_TRIGGER] = &DCameraSourceCallbackStub::OnDataSyncTriggerInner;
32 }
33 
~DCameraSourceCallbackStub()34 DCameraSourceCallbackStub::~DCameraSourceCallbackStub()
35 {}
36 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t DCameraSourceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
38     MessageOption &option)
39 {
40     DHLOGI("DCameraSourceCallbackStub OnRemoteRequest code: %{public}d", code);
41     std::u16string desc = DCameraSourceCallbackStub::GetDescriptor();
42     std::u16string remoteDesc = data.ReadInterfaceToken();
43     if (desc != remoteDesc) {
44         DHLOGE("remoteDesc is invalid!");
45         return ERR_INVALID_DATA;
46     }
47 
48     switch (code) {
49         case NOTIFY_REG_RESULT:
50             return NotifyRegResultInner(data, reply);
51         case NOTIFY_UNREG_RESULT:
52             return NotifyUnregResultInner(data, reply);
53         case NOTIFY_STATE_CHANGED:
54             return OnHardwareStateChangedInner(data, reply);
55         case NOTIFY_DATASYNC_TRIGGER:
56             return OnDataSyncTriggerInner(data, reply);
57         default:
58             DHLOGE("Invalid OnRemoteRequest code=%{public}d", code);
59             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60     }
61     return DCAMERA_NOT_FOUND;
62 }
63 
NotifyRegResultInner(MessageParcel & data,MessageParcel & reply)64 int32_t DCameraSourceCallbackStub::NotifyRegResultInner(MessageParcel &data, MessageParcel &reply)
65 {
66     DHLOGI("DCameraSourceCallbackStub NotifyRegResultInner");
67     int32_t ret = DCAMERA_OK;
68     do {
69         std::string devId = data.ReadString();
70         std::string dhId = data.ReadString();
71         std::string reqId = data.ReadString();
72         int32_t status = data.ReadInt32();
73         std::string result = data.ReadString();
74         if (!CheckParams(devId, dhId, reqId, result)) {
75             DHLOGE("input is invalid");
76             ret = DCAMERA_BAD_VALUE;
77             break;
78         }
79         ret = OnNotifyRegResult(devId, dhId, reqId, status, result);
80     } while (0);
81     reply.WriteInt32(ret);
82     return DCAMERA_OK;
83 }
84 
NotifyUnregResultInner(MessageParcel & data,MessageParcel & reply)85 int32_t DCameraSourceCallbackStub::NotifyUnregResultInner(MessageParcel &data, MessageParcel &reply)
86 {
87     DHLOGI("DCameraSourceCallbackStub NotifyUnregResultInner");
88     int32_t ret = DCAMERA_OK;
89     do {
90         std::string devId = data.ReadString();
91         std::string dhId = data.ReadString();
92         std::string reqId = data.ReadString();
93         int32_t status = data.ReadInt32();
94         std::string result = data.ReadString();
95         if (!CheckParams(devId, dhId, reqId, result)) {
96             DHLOGE("input is invalid");
97             ret = DCAMERA_BAD_VALUE;
98             break;
99         }
100         ret = OnNotifyUnregResult(devId, dhId, reqId, status, result);
101     } while (0);
102     reply.WriteInt32(ret);
103     return DCAMERA_OK;
104 }
105 
OnHardwareStateChangedInner(MessageParcel & data,MessageParcel & reply)106 int32_t DCameraSourceCallbackStub::OnHardwareStateChangedInner(MessageParcel &data, MessageParcel &reply)
107 {
108     DHLOGI("DCameraSourceCallbackStub OnHardwareStateChangedInner");
109     std::string devId = data.ReadString();
110     std::string dhId = data.ReadString();
111     int32_t status = data.ReadInt32();
112     int32_t ret = OnHardwareStateChanged(devId, dhId, status);
113     reply.WriteInt32(ret);
114     return DCAMERA_OK;
115 }
116 
OnDataSyncTriggerInner(MessageParcel & data,MessageParcel & reply)117 int32_t DCameraSourceCallbackStub::OnDataSyncTriggerInner(MessageParcel &data, MessageParcel &reply)
118 {
119     DHLOGI("DCameraSourceCallbackStub OnDataSyncTriggerInner");
120     std::string devId = data.ReadString();
121     int32_t ret = OnDataSyncTrigger(devId);
122     reply.WriteInt32(ret);
123     return DCAMERA_OK;
124 }
125 
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const std::string & result)126 bool DCameraSourceCallbackStub::CheckParams(const std::string& devId, const std::string& dhId, const std::string& reqId,
127     const std::string& result)
128 {
129     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
130         DHLOGE("devId or dhId is invalid");
131         return false;
132     }
133 
134     if (reqId.empty() || reqId.size() > DID_MAX_SIZE || result.size() > PARAM_MAX_SIZE) {
135         DHLOGE("reqId or result is invalid");
136         return false;
137     }
138     return true;
139 }
140 } // namespace DistributedHardware
141 } // namespace OHOS
142