1 /*
2  * Copyright (c) 2020-2022 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 "devhost_service_clnt.h"
10 #include "device_token_clnt.h"
11 #include "devmgr_service_start.h"
12 #include "hdf_base.h"
13 #include "hdf_device_desc.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     OsalMutexLock(&hostClnt->hostLock);
30     devHostSvcIf = (struct IDevHostService *)hostClnt->hostService;
31     if (devHostSvcIf == NULL || devHostSvcIf->AddDevice == NULL) {
32         OsalMutexUnlock(&hostClnt->hostLock);
33         HDF_LOGE("devHostSvcIf or devHostSvcIf->AddDevice is null");
34         return HDF_FAILURE;
35     }
36     HdfSListIteratorInit(&it, &hostClnt->unloadDevInfos);
37     while (HdfSListIteratorHasNext(&it)) {
38         deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&it);
39         if ((deviceInfo == NULL) || (deviceInfo->preload == DEVICE_PRELOAD_DISABLE)) {
40             continue;
41         }
42         /*
43          * If quick start feature enable, the device which 'preload' attribute set as
44          * DEVICE_PRELOAD_ENABLE_STEP2 will be loaded later
45          */
46         if (DeviceManagerIsQuickLoad() == DEV_MGR_QUICK_LOAD &&
47             deviceInfo->preload == DEVICE_PRELOAD_ENABLE_STEP2) {
48             continue;
49         }
50         ret = devHostSvcIf->AddDevice(devHostSvcIf, deviceInfo);
51         if (ret != HDF_SUCCESS) {
52             HDF_LOGE("failed to AddDevice %{public}s, ret = %{public}d", deviceInfo->svcName, ret);
53             continue;
54         }
55         deviceInfo->status = HDF_SERVICE_USABLE;
56 #ifndef __USER__
57         HdfSListIteratorRemove(&it);
58         HdfDeviceInfoFreeInstance(deviceInfo);
59 #endif
60     }
61     OsalMutexUnlock(&hostClnt->hostLock);
62     return HDF_SUCCESS;
63 }
64 
DevHostServiceClntConstruct(struct DevHostServiceClnt * hostClnt)65 static int32_t DevHostServiceClntConstruct(struct DevHostServiceClnt *hostClnt)
66 {
67     HdfSListInit(&hostClnt->devices);
68     HdfSListInit(&hostClnt->unloadDevInfos);
69     HdfSListInit(&hostClnt->dynamicDevInfos);
70     hostClnt->deviceHashMap = (Map *)OsalMemCalloc(sizeof(Map));
71     if (hostClnt->deviceHashMap == NULL) {
72         HDF_LOGE("%{public}s:failed to malloc deviceHashMap", __func__);
73         return HDF_ERR_MALLOC_FAIL;
74     }
75     if (OsalMutexInit(&hostClnt->hostLock) != HDF_SUCCESS) {
76         OsalMemFree(hostClnt->deviceHashMap);
77         return HDF_FAILURE;
78     }
79     MapInit(hostClnt->deviceHashMap);
80     return HDF_SUCCESS;
81 }
82 
DevHostServiceClntNewInstance(uint16_t hostId,const char * hostName)83 struct DevHostServiceClnt *DevHostServiceClntNewInstance(uint16_t hostId, const char *hostName)
84 {
85     struct DevHostServiceClnt *hostClnt = (struct DevHostServiceClnt *)OsalMemCalloc(sizeof(struct DevHostServiceClnt));
86     if (hostClnt == NULL) {
87         return NULL;
88     }
89     hostClnt->hostId = hostId;
90     hostClnt->hostName = hostName;
91     hostClnt->devCount = 0;
92     hostClnt->hostPid = -1;
93     hostClnt->stopFlag = false;
94     if (DevHostServiceClntConstruct(hostClnt) != HDF_SUCCESS) {
95         OsalMemFree(hostClnt);
96         hostClnt = NULL;
97     }
98     return hostClnt;
99 }
100 
DevHostServiceClntFreeInstance(struct DevHostServiceClnt * hostClnt)101 void DevHostServiceClntFreeInstance(struct DevHostServiceClnt *hostClnt)
102 {
103     if (hostClnt != NULL) {
104         HdfSListFlush(&hostClnt->devices, DeviceTokenClntDelete);
105         HdfSListFlush(&hostClnt->unloadDevInfos, HdfDeviceInfoDelete);
106         HdfSListFlush(&hostClnt->dynamicDevInfos, HdfDeviceInfoDelete);
107         OsalMemFree(hostClnt->deviceHashMap);
108         OsalMutexDestroy(&hostClnt->hostLock);
109         OsalMemFree(hostClnt);
110     }
111 }
112 
DevHostServiceClntDelete(struct DevHostServiceClnt * hostClnt)113 void DevHostServiceClntDelete(struct DevHostServiceClnt *hostClnt)
114 {
115     if (hostClnt != NULL) {
116         DevHostServiceClntFreeInstance(hostClnt);
117     }
118 }
119 
120