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 "devhost_service_clnt.h"
17 #include "devmgr_service.h"
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20 #include "hdf_sbuf.h"
21
22 #define HDF_LOG_TAG devmgr_query_device
23
DevFillQueryDeviceInfo(struct IDevmgrService * inst,struct HdfSBuf * data,struct HdfSBuf * reply)24 int DevFillQueryDeviceInfo(struct IDevmgrService *inst, struct HdfSBuf *data, struct HdfSBuf *reply)
25 {
26 struct DevmgrService *devMgrSvc = (struct DevmgrService *)inst;
27 struct HdfSListIterator itDeviceInfo;
28 struct HdfDeviceInfo *deviceInfo = NULL;
29 struct DevHostServiceClnt *hostClnt = NULL;
30 bool flag = false;
31 uint16_t status = 0;
32
33 if (devMgrSvc == NULL || data == NULL || reply == NULL) {
34 HDF_LOGE("%{public}s failed, parameter is null", __func__);
35 return HDF_FAILURE;
36 }
37
38 HdfSbufReadUint16(data, &status);
39 if (status != HDF_SERVICE_UNUSABLE && status != HDF_SERVICE_USABLE) {
40 HDF_LOGE("%{public}s failed, status is invalid %{public}d", __func__, status);
41 return HDF_FAILURE;
42 }
43
44 DLIST_FOR_EACH_ENTRY(hostClnt, &devMgrSvc->hosts, struct DevHostServiceClnt, node) {
45 HdfSListIteratorInit(&itDeviceInfo, &hostClnt->unloadDevInfos);
46 HDF_LOGD("%{public}s, host:%{public}s %{public}d", __func__, hostClnt->hostName, hostClnt->hostId);
47 while (HdfSListIteratorHasNext(&itDeviceInfo)) {
48 deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&itDeviceInfo);
49 HDF_LOGD("%{public}s %{public}s policy:%{public}d status:%{public}d type:%{public}d", __func__,
50 deviceInfo->svcName, deviceInfo->policy, deviceInfo->status, deviceInfo->deviceType);
51 if ((deviceInfo->policy == SERVICE_POLICY_CAPACITY || deviceInfo->policy == SERVICE_POLICY_PUBLIC) &&
52 deviceInfo->status == status) {
53 flag = HdfSbufWriteString(reply, deviceInfo->svcName);
54 if (!flag) {
55 HDF_LOGE("%{public}s writing string failed %{public}s", __func__, deviceInfo->svcName);
56 return HDF_FAILURE;
57 }
58 flag = HdfSbufWriteInt32(reply, deviceInfo->deviceType);
59 if (!flag) {
60 HDF_LOGE("%{public}s writing int32 failed %{public}d", __func__, deviceInfo->deviceType);
61 return HDF_FAILURE;
62 }
63 }
64 }
65 }
66 return HDF_SUCCESS;
67 }
68