1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "device_token_clnt.h"
10 #include "devhost_service_clnt.h"
11 #include "devmgr_service_start.h"
12 #include "hdf_base.h"
13 #include "hdf_driver_installer.h"
14 #include "hdf_log.h"
15 #include "osal_mem.h"
16
17 #define HDF_LOG_TAG devhost_service_clnt
18
DevHostServiceClntInstallDriver(struct DevHostServiceClnt * hostClnt)19 int DevHostServiceClntInstallDriver(struct DevHostServiceClnt *hostClnt)
20 {
21 int ret;
22 struct HdfSListIterator it;
23 struct HdfDeviceInfo *deviceInfo = NULL;
24 struct IDevHostService *devHostSvcIf = NULL;
25 if (hostClnt == NULL) {
26 HDF_LOGE("failed to install driver, hostClnt is null");
27 return HDF_FAILURE;
28 }
29
30 devHostSvcIf = (struct IDevHostService *)hostClnt->hostService;
31 if (devHostSvcIf == NULL || devHostSvcIf->AddDevice == NULL) {
32 HDF_LOGE("devHostSvcIf or devHostSvcIf->AddDevice is null");
33 return HDF_FAILURE;
34 }
35 HdfSListIteratorInit(&it, hostClnt->deviceInfos);
36 while (HdfSListIteratorHasNext(&it)) {
37 deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&it);
38 if ((deviceInfo == NULL) || (deviceInfo->preload == DEVICE_PRELOAD_DISABLE)) {
39 continue;
40 }
41 if ((DeviceManagerIsQuickLoad() == DEV_MGR_QUICK_LOAD) &&
42 (deviceInfo->preload == DEVICE_PRELOAD_ENABLE_STEP2)) {
43 continue;
44 }
45 ret = devHostSvcIf->AddDevice(devHostSvcIf, deviceInfo);
46 if (ret != HDF_SUCCESS) {
47 HDF_LOGE("failed to install driver %s, ret = %d", deviceInfo->svcName, ret);
48 }
49 }
50 return HDF_SUCCESS;
51 }
52
DevHostServiceClntConstruct(struct DevHostServiceClnt * hostClnt)53 static void DevHostServiceClntConstruct(struct DevHostServiceClnt *hostClnt)
54 {
55 HdfSListInit(&hostClnt->devices);
56 hostClnt->deviceHashMap = (Map *)OsalMemCalloc(sizeof(Map));
57 if (hostClnt->deviceHashMap == NULL) {
58 HDF_LOGE("%s:failed to malloc deviceHashMap", __func__);
59 return;
60 }
61 MapInit(hostClnt->deviceHashMap);
62 }
63
DevHostServiceClntNewInstance(uint16_t hostId,const char * hostName)64 struct DevHostServiceClnt *DevHostServiceClntNewInstance(uint16_t hostId, const char *hostName)
65 {
66 struct DevHostServiceClnt *hostClnt =
67 (struct DevHostServiceClnt *)OsalMemCalloc(sizeof(struct DevHostServiceClnt));
68 if (hostClnt != NULL) {
69 hostClnt->hostId = hostId;
70 hostClnt->hostName = hostName;
71 hostClnt->devCount = 0;
72 DevHostServiceClntConstruct(hostClnt);
73 }
74 return hostClnt;
75 }
76
DevHostServiceClntFreeInstance(struct DevHostServiceClnt * hostClnt)77 void DevHostServiceClntFreeInstance(struct DevHostServiceClnt *hostClnt)
78 {
79 if (hostClnt != NULL) {
80 HdfSListFlush(&hostClnt->devices, DeviceTokenClntDelete);
81 HdfSListFlush(hostClnt->deviceInfos, HdfDeviceInfoDelete);
82 OsalMemFree(hostClnt->deviceHashMap);
83 OsalMemFree(hostClnt);
84 }
85 }
86
DevHostServiceClntDelete(struct DevHostServiceClnt * hostClnt)87 void DevHostServiceClntDelete(struct DevHostServiceClnt *hostClnt)
88 {
89 if (hostClnt != NULL) {
90 DevHostServiceClntFreeInstance(hostClnt);
91 }
92 }
93
94