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 "devmgr_service_clnt.h"
10 #include "hdf_base.h"
11 #include "hdf_device.h"
12 #include "hdf_log.h"
13 #include "hdf_object_manager.h"
14
15 #define HDF_LOG_TAG devmgr_service_clnt
16
DevmgrServiceClntAttachDeviceHost(uint16_t hostId,struct IDevHostService * hostService)17 int DevmgrServiceClntAttachDeviceHost(uint16_t hostId, struct IDevHostService *hostService)
18 {
19 struct IDevmgrService *devMgrSvcIf = NULL;
20 struct DevmgrServiceClnt *inst = DevmgrServiceClntGetInstance();
21 if (inst == NULL || inst->devMgrSvcIf == NULL) {
22 HDF_LOGE("failed to attach device host, get device manager service client is null");
23 return HDF_FAILURE;
24 }
25
26 devMgrSvcIf = inst->devMgrSvcIf;
27 if (devMgrSvcIf->AttachDeviceHost == NULL) {
28 HDF_LOGE("failed to attach device host, attach device host function is null");
29 return HDF_FAILURE;
30 }
31 return devMgrSvcIf->AttachDeviceHost(devMgrSvcIf, hostId, hostService);
32 }
33
DevmgrServiceClntAttachDevice(struct IHdfDeviceToken * deviceToken)34 int DevmgrServiceClntAttachDevice(struct IHdfDeviceToken *deviceToken)
35 {
36 struct IDevmgrService *devMgrSvcIf = NULL;
37 struct DevmgrServiceClnt *inst = DevmgrServiceClntGetInstance();
38 if ((inst == NULL) || (inst->devMgrSvcIf == NULL)) {
39 HDF_LOGE("devmgr client failed to attach device, inst is null");
40 return HDF_FAILURE;
41 }
42
43 devMgrSvcIf = inst->devMgrSvcIf;
44 if (devMgrSvcIf->AttachDevice == NULL) {
45 HDF_LOGE("devmgr client failed to attach device, dmsOps->AttachDevice is nul");
46 return HDF_FAILURE;
47 }
48 return devMgrSvcIf->AttachDevice(devMgrSvcIf, deviceToken);
49 }
50
DevmgrServiceClntDetachDevice(devid_t devid)51 int DevmgrServiceClntDetachDevice(devid_t devid)
52 {
53 struct IDevmgrService *devMgrSvcIf = NULL;
54 struct DevmgrServiceClnt *inst = DevmgrServiceClntGetInstance();
55 if (inst == NULL || inst->devMgrSvcIf == NULL) {
56 HDF_LOGE("devmgr client failed to deatch device, inst is null");
57 return HDF_FAILURE;
58 }
59
60 devMgrSvcIf = inst->devMgrSvcIf;
61 if (devMgrSvcIf->DetachDevice == NULL) {
62 return HDF_FAILURE;
63 }
64 return devMgrSvcIf->DetachDevice(devMgrSvcIf, devid);
65 }
66
DevmgrServiceClntGetInstance(void)67 struct DevmgrServiceClnt *DevmgrServiceClntGetInstance(void)
68 {
69 static struct DevmgrServiceClnt instance = {0};
70 if (instance.devMgrSvcIf == NULL) {
71 instance.devMgrSvcIf = (struct IDevmgrService *)HdfObjectManagerGetObject(HDF_OBJECT_ID_DEVMGR_SERVICE);
72 }
73 return &instance;
74 }
75
DevmgrServiceClntFreeInstance(struct DevmgrServiceClnt * inst)76 void DevmgrServiceClntFreeInstance(struct DevmgrServiceClnt *inst)
77 {
78 if ((inst != NULL) && (inst->devMgrSvcIf != NULL)) {
79 HdfObjectManagerFreeObject((struct HdfObject *)inst->devMgrSvcIf);
80 inst->devMgrSvcIf = NULL;
81 }
82 }
83