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()
27 {
28 memberFuncMap_[NOTIFY_REG_RESULT] = &DCameraSourceCallbackStub::NotifyRegResultInner;
29 memberFuncMap_[NOTIFY_UNREG_RESULT] = &DCameraSourceCallbackStub::NotifyUnregResultInner;
30 }
31
~DCameraSourceCallbackStub()32 DCameraSourceCallbackStub::~DCameraSourceCallbackStub()
33 {}
34
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t DCameraSourceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
36 MessageOption &option)
37 {
38 DHLOGI("DCameraSourceCallbackStub OnRemoteRequest code: %d", code);
39 std::u16string desc = DCameraSourceCallbackStub::GetDescriptor();
40 std::u16string remoteDesc = data.ReadInterfaceToken();
41 if (desc != remoteDesc) {
42 DHLOGE("DCameraSourceCallbackStub::OnRemoteRequest remoteDesc is invalid!");
43 return ERR_INVALID_DATA;
44 }
45 auto itFunc = memberFuncMap_.find(code);
46 if (itFunc == memberFuncMap_.end()) {
47 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48 }
49
50 auto memberFunc = itFunc->second;
51 return (this->*memberFunc)(data, reply);
52 }
53
NotifyRegResultInner(MessageParcel & data,MessageParcel & reply)54 int32_t DCameraSourceCallbackStub::NotifyRegResultInner(MessageParcel &data, MessageParcel &reply)
55 {
56 DHLOGI("DCameraSourceCallbackStub NotifyRegResultInner");
57 int32_t ret = DCAMERA_OK;
58 do {
59 std::string devId = data.ReadString();
60 std::string dhId = data.ReadString();
61 std::string reqId = data.ReadString();
62 int32_t status = data.ReadInt32();
63 std::string result = data.ReadString();
64 if (!CheckParams(devId, dhId, reqId, result)) {
65 DHLOGE("DCameraSourceCallbackStub NotifyRegResultInner input is invalid");
66 ret = DCAMERA_BAD_VALUE;
67 break;
68 }
69 ret = OnNotifyRegResult(devId, dhId, reqId, status, result);
70 } while (0);
71 reply.WriteInt32(ret);
72 return DCAMERA_OK;
73 }
74
NotifyUnregResultInner(MessageParcel & data,MessageParcel & reply)75 int32_t DCameraSourceCallbackStub::NotifyUnregResultInner(MessageParcel &data, MessageParcel &reply)
76 {
77 DHLOGI("DCameraSourceCallbackStub NotifyUnregResultInner");
78 int32_t ret = DCAMERA_OK;
79 do {
80 std::string devId = data.ReadString();
81 std::string dhId = data.ReadString();
82 std::string reqId = data.ReadString();
83 int32_t status = data.ReadInt32();
84 std::string result = data.ReadString();
85 if (!CheckParams(devId, dhId, reqId, result)) {
86 DHLOGE("DCameraSourceCallbackStub NotifyUnregResultInner input is invalid");
87 ret = DCAMERA_BAD_VALUE;
88 break;
89 }
90 ret = OnNotifyUnregResult(devId, dhId, reqId, status, result);
91 } while (0);
92 reply.WriteInt32(ret);
93 return DCAMERA_OK;
94 }
95
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const std::string & result)96 bool DCameraSourceCallbackStub::CheckParams(const std::string& devId, const std::string& dhId, const std::string& reqId,
97 const std::string& result)
98 {
99 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
100 DHLOGE("DCameraSourceCallbackStub CheckParams devId or dhId is invalid");
101 return false;
102 }
103
104 if (reqId.empty() || reqId.size() > DID_MAX_SIZE || result.size() > PARAM_MAX_SIZE) {
105 DHLOGE("DCameraSourceCallbackStub CheckParams reqId or result is invalid");
106 return false;
107 }
108 return true;
109 }
110 } // namespace DistributedHardware
111 } // namespace OHOS
112