• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "devsvc_manager_clnt.h"
10 #include "hdf_log.h"
11 #include "hdf_object_manager.h"
12 
13 #define HDF_LOG_TAG devsvc_manager_clnt
14 
DevSvcManagerClntAddService(struct HdfDeviceObject * service,const struct HdfServiceInfo * servinfo)15 int DevSvcManagerClntAddService(struct HdfDeviceObject *service, const struct HdfServiceInfo *servinfo)
16 {
17     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
18     struct IDevSvcManager *serviceManager = NULL;
19     if (devSvcMgrClnt == NULL) {
20         HDF_LOGE("failed to add service, client is null");
21         return HDF_FAILURE;
22     }
23     if (servinfo->devClass >= DEVICE_CLASS_MAX) {
24         HDF_LOGE("failed to add service, invalid class");
25         return HDF_FAILURE;
26     }
27 
28     serviceManager = devSvcMgrClnt->devSvcMgrIf;
29     if (serviceManager == NULL || serviceManager->AddService == NULL) {
30         HDF_LOGE("serviceManager AddService function is null");
31         return HDF_FAILURE;
32     }
33     return serviceManager->AddService(serviceManager, service, servinfo);
34 }
35 
DevSvcManagerClntUpdateService(struct HdfDeviceObject * service,const struct HdfServiceInfo * servinfo)36 int DevSvcManagerClntUpdateService(struct HdfDeviceObject *service, const struct HdfServiceInfo *servinfo)
37 {
38     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
39     struct IDevSvcManager *serviceManager = NULL;
40     if (devSvcMgrClnt == NULL) {
41         HDF_LOGE("failed to update service, client is null");
42         return HDF_FAILURE;
43     }
44     if (servinfo->devClass >= DEVICE_CLASS_MAX) {
45         HDF_LOGE("failed to update service, invalid class");
46         return HDF_FAILURE;
47     }
48 
49     serviceManager = devSvcMgrClnt->devSvcMgrIf;
50     if (serviceManager == NULL || serviceManager->UpdateService == NULL) {
51         HDF_LOGE("serviceManager UpdateService function is null");
52         return HDF_FAILURE;
53     }
54     return serviceManager->UpdateService(serviceManager, service, servinfo);
55 }
56 
DevSvcManagerClntGetService(const char * svcName)57 const struct HdfObject *DevSvcManagerClntGetService(const char *svcName)
58 {
59     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
60     struct IDevSvcManager *serviceManager = NULL;
61     if (devSvcMgrClnt == NULL) {
62         HDF_LOGE("failed to get service, client is null");
63         return NULL;
64     }
65 
66     serviceManager = devSvcMgrClnt->devSvcMgrIf;
67     if (serviceManager == NULL || serviceManager->GetService == NULL) {
68         HDF_LOGE("serviceManager GetService function is null");
69         return NULL;
70     }
71     return serviceManager->GetService(serviceManager, svcName);
72 }
73 
DevSvcManagerClntGetDeviceObject(const char * svcName)74 struct HdfDeviceObject *DevSvcManagerClntGetDeviceObject(const char *svcName)
75 {
76     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
77     struct IDevSvcManager *serviceManager = NULL;
78     if (devSvcMgrClnt == NULL) {
79         HDF_LOGE("failed to get device object, client is null");
80         return NULL;
81     }
82 
83     serviceManager = devSvcMgrClnt->devSvcMgrIf;
84     if (serviceManager == NULL || serviceManager->GetObject == NULL) {
85         HDF_LOGE("failed to get device object, method not implement");
86         return NULL;
87     }
88     return serviceManager->GetObject(serviceManager, svcName);
89 }
90 
DevSvcManagerClntSubscribeService(const char * svcName,struct SubscriberCallback callback)91 int DevSvcManagerClntSubscribeService(const char *svcName, struct SubscriberCallback callback)
92 {
93     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
94     struct IDevSvcManager *serviceManager = NULL;
95     if (devSvcMgrClnt == NULL || svcName == NULL) {
96         HDF_LOGE("failed to subscribe service, client or svcName is null");
97         return HDF_FAILURE;
98     }
99 
100     serviceManager = devSvcMgrClnt->devSvcMgrIf;
101     if (serviceManager == NULL || serviceManager->SubscribeService == NULL) {
102         HDF_LOGE("failed to subscribe service, method not implement");
103         return HDF_FAILURE;
104     }
105     return serviceManager->SubscribeService(serviceManager, svcName, callback);
106 }
107 
DevSvcManagerClntUnsubscribeService(const char * svcName)108 int DevSvcManagerClntUnsubscribeService(const char *svcName)
109 {
110     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
111     struct IDevSvcManager *serviceManager = NULL;
112     if (devSvcMgrClnt == NULL || svcName == NULL) {
113         HDF_LOGE("failed to unsubscribe service, client or svcName is null");
114         return HDF_FAILURE;
115     }
116 
117     serviceManager = devSvcMgrClnt->devSvcMgrIf;
118     if (serviceManager == NULL || serviceManager->UnsubscribeService == NULL) {
119         HDF_LOGE("failed to unsubscribe service, method not implement");
120         return HDF_FAILURE;
121     }
122     return serviceManager->UnsubscribeService(serviceManager, svcName);
123 }
124 
DevSvcManagerClntRemoveService(const char * svcName)125 void DevSvcManagerClntRemoveService(const char *svcName)
126 {
127     struct DevSvcManagerClnt *devSvcMgrClnt = DevSvcManagerClntGetInstance();
128     struct IDevSvcManager *serviceManager = NULL;
129     if (devSvcMgrClnt == NULL) {
130         HDF_LOGE("failed to remove service, devSvcMgrClnt is null");
131         return;
132     }
133 
134     serviceManager = devSvcMgrClnt->devSvcMgrIf;
135     if (serviceManager == NULL || serviceManager->RemoveService == NULL) {
136         return;
137     }
138     serviceManager->RemoveService(serviceManager, svcName, NULL);
139 }
140 
DevSvcManagerClntConstruct(struct DevSvcManagerClnt * inst)141 static void DevSvcManagerClntConstruct(struct DevSvcManagerClnt *inst)
142 {
143     inst->devSvcMgrIf = (struct IDevSvcManager *)HdfObjectManagerGetObject(HDF_OBJECT_ID_DEVSVC_MANAGER);
144 }
145 
DevSvcManagerClntGetInstance(void)146 struct DevSvcManagerClnt *DevSvcManagerClntGetInstance(void)
147 {
148     static struct DevSvcManagerClnt *instance = NULL;
149     if (instance == NULL) {
150         static struct DevSvcManagerClnt singletonInstance;
151         DevSvcManagerClntConstruct(&singletonInstance);
152         instance = &singletonInstance;
153     }
154     return instance;
155 }
156 
DevSvcManagerClntFreeInstance(struct DevSvcManagerClnt * instance)157 void DevSvcManagerClntFreeInstance(struct DevSvcManagerClnt *instance)
158 {
159     if (instance != NULL) {
160         HdfObjectManagerFreeObject((struct HdfObject *)instance->devSvcMgrIf);
161     }
162 }
163 
164