1/*
2 * Copyright (c) 2022-2023 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 "input_check_param.h"
17
18#include "constants_dinput.h"
19#include "dinput_log.h"
20
21namespace OHOS {
22namespace DistributedHardware {
23namespace DistributedInput {
24DInputCheckParam &DInputCheckParam::GetInstance(void)
25{
26    static DInputCheckParam instance;
27    return instance;
28}
29bool DInputCheckParam::CheckParam(const std::string &deviceId, sptr<IRemoteBroker> callback)
30{
31    if (deviceId.empty() || deviceId.size() > DEV_ID_LENGTH_MAX) {
32        DHLOGE("CheckParam deviceId is empty or deviceId size too long.");
33        return false;
34    }
35    if (callback == nullptr) {
36        DHLOGE("CheckParam callback is null.");
37        return false;
38    }
39    return true;
40}
41
42bool DInputCheckParam::CheckParam(const std::string &deviceId, const uint32_t &inputTypes,
43    sptr<IRemoteBroker> callback)
44{
45    if (deviceId.empty() || deviceId.size() > DEV_ID_LENGTH_MAX) {
46        DHLOGE("CheckParam deviceId is empty or deviceId size too long.");
47        return false;
48    }
49    if (inputTypes > static_cast<uint32_t>(DInputDeviceType::ALL) ||
50        inputTypes == static_cast<uint32_t>(DInputDeviceType::NONE) ||
51        !(inputTypes & static_cast<uint32_t>(DInputDeviceType::ALL))) {
52        DHLOGE("CheckParam, inputTypes is invalids.");
53        return false;
54    }
55    if (callback == nullptr) {
56        DHLOGE("CheckParam callback is null.");
57        return false;
58    }
59    return true;
60}
61
62bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes,
63    sptr<IRemoteBroker> callback)
64{
65    if (srcId.empty() || srcId.size() > DEV_ID_LENGTH_MAX) {
66        DHLOGE("CheckParam srcId is empty or srcId size too long.");
67        return false;
68    }
69    if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) {
70        DHLOGE("CheckParam sinkId is empty or sinkId size too long.");
71        return false;
72    }
73    if (inputTypes > static_cast<uint32_t>(DInputDeviceType::ALL) ||
74        inputTypes == static_cast<uint32_t>(DInputDeviceType::NONE) ||
75        !(inputTypes & static_cast<uint32_t>(DInputDeviceType::ALL))) {
76        DHLOGE("CheckParam, inputTypes is invalids.");
77        return false;
78    }
79    if (callback == nullptr) {
80        DHLOGE("CheckParam callback is null.");
81        return false;
82    }
83    return true;
84}
85
86bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId,
87    sptr<IRemoteBroker> callback)
88{
89    if (srcId.empty() || srcId.size() > DEV_ID_LENGTH_MAX) {
90        DHLOGE("CheckParam srcId is empty or srcId size too long.");
91        return false;
92    }
93    if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) {
94        DHLOGE("CheckParam sinkId is empty or sinkId size too long.");
95        return false;
96    }
97    if (callback == nullptr) {
98        DHLOGE("CheckParam callback is null.");
99        return false;
100    }
101    return true;
102}
103
104bool DInputCheckParam::CheckParam(const std::string &sinkId, const std::vector<std::string> &dhIds,
105    sptr<IRemoteBroker> callback)
106{
107    if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) {
108        DHLOGE("CheckParam sinkId is empty or sinkId size too long.");
109        return false;
110    }
111    if (dhIds.empty()) {
112        DHLOGE("CheckParam dhIds is empty.");
113        return false;
114    }
115    for (auto iter : dhIds) {
116        if (iter.size() > DH_ID_LENGTH_MAX) {
117            DHLOGE("CheckParam dhId size is too long.");
118            return false;
119        }
120    }
121    if (callback == nullptr) {
122        DHLOGE("CheckParam callback is null.");
123        return false;
124    }
125    return true;
126}
127
128bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId,
129    const std::vector<std::string> &dhIds, sptr<IRemoteBroker> callback)
130{
131    if (srcId.empty() || srcId.size() > DEV_ID_LENGTH_MAX) {
132        DHLOGE("CheckParam srcId is empty or srcId size too long.");
133        return false;
134    }
135    if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) {
136        DHLOGE("CheckParam sinkId is empty or sinkId size too long.");
137        return false;
138    }
139    if (dhIds.empty()) {
140        DHLOGE("CheckParam dhIds is empty.");
141        return false;
142    }
143    for (auto iter : dhIds) {
144        if (iter.size() > DH_ID_LENGTH_MAX) {
145            DHLOGE("CheckParam dhId size is too long.");
146            return false;
147        }
148    }
149    if (callback == nullptr) {
150        DHLOGE("CheckParam callback is null.");
151        return false;
152    }
153    return true;
154}
155
156bool DInputCheckParam::CheckRegisterParam(const std::string &devId, const std::string &dhId,
157    const std::string &parameters, const std::shared_ptr<RegisterCallback> &callback)
158{
159    if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) {
160        DHLOGE("CheckParam devId is empty or devId size too long.");
161        return false;
162    }
163    if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) {
164        DHLOGE("CheckParam dhId is empty or dhId size too long.");
165        return false;
166    }
167    if (parameters.empty()) {
168        DHLOGE("CheckParam parameters is empty.");
169        return false;
170    }
171    if (callback == nullptr) {
172        DHLOGE("CheckParam callback is null.");
173        return false;
174    }
175    return true;
176}
177
178bool DInputCheckParam::CheckUnregisterParam(const std::string &devId, const std::string &dhId,
179    const std::shared_ptr<UnregisterCallback> &callback)
180{
181    if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) {
182        DHLOGE("CheckParam devId is empty or devId size too long.");
183        return false;
184    }
185    if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) {
186        DHLOGE("CheckParam dhId is empty or dhId size too long.");
187        return false;
188    }
189    if (callback == nullptr) {
190        DHLOGE("CheckParam callback is null.");
191        return false;
192    }
193    return true;
194}
195} // namespace DistributedInput
196} // namespace DistributedHardware
197} // namespace OHOS