• 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 "hdf_device_object.h"
10 #include "devhost_service.h"
11 #include "devsvc_manager_clnt.h"
12 #include "hdf_base.h"
13 #include "hdf_cstring.h"
14 #include "hdf_device_node.h"
15 #include "hdf_driver_loader.h"
16 #include "hdf_log.h"
17 #include "hdf_object_manager.h"
18 #include "hdf_power_manager.h"
19 #include "hdf_service_observer.h"
20 #include "osal_mem.h"
21 #include "power_state_token.h"
22 
23 #define HDF_LOG_TAG device_object
24 
25 #define SERVICE_INFO_LEN_MAX 128
26 
HdfDeviceSubscribeService(struct HdfDeviceObject * deviceObject,const char * serviceName,struct SubscriberCallback callback)27 int32_t HdfDeviceSubscribeService(
28     struct HdfDeviceObject *deviceObject, const char *serviceName, struct SubscriberCallback callback)
29 {
30     struct DevHostService *hostService = NULL;
31     struct HdfDeviceNode *devNode = NULL;
32     if (deviceObject == NULL || serviceName == NULL) {
33         HDF_LOGE("failed to subscribe service, serviceName is null");
34         return HDF_FAILURE;
35     }
36     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
37         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
38     hostService = devNode->hostService;
39     if (hostService == NULL) {
40         HDF_LOGE("failed to subscribe service, hostService is null");
41         return HDF_FAILURE;
42     }
43 
44     return HdfServiceObserverSubscribeService(&hostService->observer, serviceName, devNode->devId, callback);
45 }
46 
HdfDeviceGetServiceName(const struct HdfDeviceObject * deviceObject)47 const char *HdfDeviceGetServiceName(const struct HdfDeviceObject *deviceObject)
48 {
49     struct HdfDeviceNode *devNode = NULL;
50     if (deviceObject == NULL) {
51         HDF_LOGE("failed to get service name, deviceObject is invalid");
52         return NULL;
53     }
54     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
55         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
56     return devNode->servName;
57 }
58 
HdfPmRegisterPowerListener(struct HdfDeviceObject * deviceObject,const struct IPowerEventListener * listener)59 int HdfPmRegisterPowerListener(struct HdfDeviceObject *deviceObject, const struct IPowerEventListener *listener)
60 {
61     struct HdfDeviceNode *devNode = NULL;
62     if (deviceObject == NULL) {
63         return HDF_ERR_INVALID_PARAM;
64     }
65     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
66         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
67     return HdfDeviceNodeAddPowerStateListener(devNode, listener);
68 }
69 
HdfPmUnregisterPowerListener(struct HdfDeviceObject * deviceObject,const struct IPowerEventListener * listener)70 void HdfPmUnregisterPowerListener(struct HdfDeviceObject *deviceObject, const struct IPowerEventListener *listener)
71 {
72     struct HdfDeviceNode *devNode = NULL;
73     if (deviceObject == NULL) {
74         return;
75     }
76     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
77         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
78     HdfDeviceNodeRemovePowerStateListener(devNode, listener);
79 }
80 
HdfPmAcquireDevice(struct HdfDeviceObject * deviceObject)81 void HdfPmAcquireDevice(struct HdfDeviceObject *deviceObject)
82 {
83     struct HdfDeviceNode *devNode = NULL;
84     struct IPowerStateToken *tokenIf = NULL;
85     if (deviceObject == NULL) {
86         HDF_LOGE("HdfPmAcquireDevice input param is invalid");
87         return;
88     }
89     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
90         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
91     tokenIf = (struct IPowerStateToken *)devNode->powerToken;
92     if ((tokenIf != NULL) && (tokenIf->AcquireWakeLock != NULL)) {
93         tokenIf->AcquireWakeLock(tokenIf);
94     }
95 }
96 
HdfPmReleaseDevice(struct HdfDeviceObject * deviceObject)97 void HdfPmReleaseDevice(struct HdfDeviceObject *deviceObject)
98 {
99     struct HdfDeviceNode *devNode = NULL;
100     struct IPowerStateToken *tokenIf = NULL;
101     if (deviceObject == NULL) {
102         HDF_LOGE("HdfPmReleaseDevice input param is invalid");
103         return;
104     }
105     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
106         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
107     tokenIf = (struct IPowerStateToken *)devNode->powerToken;
108     if ((tokenIf != NULL) && (tokenIf->ReleaseWakeLock != NULL)) {
109         tokenIf->ReleaseWakeLock(tokenIf);
110     }
111 }
112 
113 #ifndef __LITEOS_M__
HdfPmAcquireDeviceAsync(struct HdfDeviceObject * deviceObject)114 void HdfPmAcquireDeviceAsync(struct HdfDeviceObject *deviceObject)
115 {
116     struct HdfDeviceNode *devNode = NULL;
117 
118     if (deviceObject == NULL) {
119         HDF_LOGE("%s: input param is invalid", __func__);
120         return;
121     }
122 
123     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
124         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
125     HdfPmTaskPut(devNode->powerToken, HDF_PM_REQUEST_ACQUIRE);
126 }
127 
HdfPmReleaseDeviceAsync(struct HdfDeviceObject * deviceObject)128 void HdfPmReleaseDeviceAsync(struct HdfDeviceObject *deviceObject)
129 {
130     struct HdfDeviceNode *devNode = NULL;
131 
132     if (deviceObject == NULL) {
133         HDF_LOGE("%s: input param is invalid", __func__);
134         return;
135     }
136 
137     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
138         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
139     HdfPmTaskPut(devNode->powerToken, HDF_PM_REQUEST_RELEASE);
140 }
141 #endif
142 
HdfPmSetMode(struct HdfDeviceObject * deviceObject,uint32_t mode)143 void HdfPmSetMode(struct HdfDeviceObject *deviceObject, uint32_t mode)
144 {
145     struct HdfDeviceNode *devNode = NULL;
146     struct PowerStateToken *token = NULL;
147     if (deviceObject == NULL || mode > HDF_POWER_MODE_MAX) {
148         HDF_LOGE("%s: input param is invalid", __func__);
149         return;
150     }
151     devNode = (struct HdfDeviceNode *)HDF_SLIST_CONTAINER_OF(
152         struct HdfDeviceObject, deviceObject, struct HdfDeviceNode, deviceObject);
153     token = devNode->powerToken;
154     if (token != NULL) {
155         token->mode = mode;
156     }
157 }
158 
HdfDeviceSetClass(struct HdfDeviceObject * deviceObject,DeviceClass deviceClass)159 bool HdfDeviceSetClass(struct HdfDeviceObject *deviceObject, DeviceClass deviceClass)
160 {
161     if ((deviceObject == NULL) || (deviceClass >= DEVICE_CLASS_MAX)) {
162         return false;
163     }
164     deviceObject->deviceClass = deviceClass;
165     return true;
166 }
167 
HdfDeviceObjectConstruct(struct HdfDeviceObject * deviceObject)168 void HdfDeviceObjectConstruct(struct HdfDeviceObject *deviceObject)
169 {
170     if (deviceObject != NULL) {
171 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
172         deviceObject->deviceMatchAttr = NULL;
173 #else
174         deviceObject->property = NULL;
175 #endif
176         deviceObject->service = NULL;
177         deviceObject->deviceClass = DEVICE_CLASS_DEFAULT;
178     }
179 }
180 
HdfDeviceObjectAlloc(struct HdfDeviceObject * parent,const char * driverName)181 struct HdfDeviceObject *HdfDeviceObjectAlloc(struct HdfDeviceObject *parent, const char *driverName)
182 {
183     struct HdfDeviceNode *newNode = NULL;
184     struct HdfDeviceNode *parentDevNode = CONTAINER_OF(parent, struct HdfDeviceNode, deviceObject);
185 
186     if (parent == NULL) {
187         HDF_LOGE("failed to alloc device, parent invalid");
188         return NULL;
189     }
190 
191     if (parentDevNode->devStatus != DEVNODE_LAUNCHED) {
192         HDF_LOGE("failed to alloc device, parent status invalid %{public}u", parentDevNode->devStatus);
193         return NULL;
194     }
195 
196     newNode = (struct HdfDeviceNode *)HdfObjectManagerGetObject(HDF_OBJECT_ID_DEVICE_SERVICE);
197     if (newNode == NULL) {
198         return NULL;
199     }
200     newNode->driverName = HdfStringCopy(driverName);
201     if (newNode->driverName == NULL) {
202         HdfDeviceNodeFreeInstance(newNode);
203         return NULL;
204     }
205 
206     newNode->hostService = parentDevNode->hostService;
207     newNode->device = parentDevNode->device;
208 
209     return &newNode->deviceObject;
210 }
211 
HdfDeviceObjectRelease(struct HdfDeviceObject * dev)212 void HdfDeviceObjectRelease(struct HdfDeviceObject *dev)
213 {
214     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
215     if (dev == NULL) {
216         return;
217     }
218 
219     if (devNode->device != NULL && devNode->device->super.Detach != NULL) {
220         devNode->device->super.Detach(&devNode->device->super, devNode);
221     }
222     HdfDeviceNodeFreeInstance(devNode);
223 }
224 
HdfDeviceObjectRegister(struct HdfDeviceObject * dev)225 int HdfDeviceObjectRegister(struct HdfDeviceObject *dev)
226 {
227     int ret = HDF_FAILURE;
228     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
229     struct IDriverLoader *driverLoader = HdfDriverLoaderGetInstance();
230 
231     if (dev == NULL || devNode->driverName == NULL || devNode->device == NULL || driverLoader == NULL ||
232         driverLoader->GetDriver == NULL) {
233         HDF_LOGE("failed to add device, param invalid");
234         return HDF_ERR_INVALID_PARAM;
235     }
236 
237     devNode->driver = driverLoader->GetDriver(devNode->driverName);
238     if (devNode->driver == NULL) {
239         HDF_LOGE("can not found driver %s", devNode->driverName);
240         return HDF_DEV_ERR_NO_DEVICE;
241     }
242 
243     ret = devNode->device->super.Attach(&devNode->device->super, devNode);
244     if (ret != HDF_SUCCESS) {
245         HDF_LOGE("failed to attach device %s", devNode->driverName);
246         return HDF_DEV_ERR_ATTACHDEV_FAIL;
247     }
248 
249     return ret;
250 }
251 
HdfDeviceObjectUnRegister(struct HdfDeviceObject * dev)252 int HdfDeviceObjectUnRegister(struct HdfDeviceObject *dev)
253 {
254     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
255     if (devNode == NULL || devNode->device == NULL) {
256         return HDF_ERR_INVALID_OBJECT;
257     }
258 
259     return devNode->device->super.Detach(&devNode->device->super, devNode);
260 }
261 
HdfDeviceObjectPublishService(struct HdfDeviceObject * dev,const char * servName,uint8_t policy,uint32_t perm)262 int HdfDeviceObjectPublishService(struct HdfDeviceObject *dev, const char *servName, uint8_t policy, uint32_t perm)
263 {
264     int ret;
265     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
266     if (dev == NULL || servName == NULL) {
267         return HDF_ERR_INVALID_PARAM;
268     }
269 
270     if (policy <= SERVICE_POLICY_NONE || policy >= SERVICE_POLICY_INVALID) {
271         return HDF_DEV_ERR_NO_DEVICE_SERVICE;
272     }
273 
274     if (devNode->servStatus) {
275         HDF_LOGE("failed to publish public service, repeat publish");
276         return HDF_FAILURE;
277     }
278 
279     devNode->servName = HdfStringCopy(servName);
280     if (devNode->servName == NULL) {
281         return HDF_DEV_ERR_NO_MEMORY;
282     }
283 
284     devNode->policy = policy;
285     devNode->permission = perm;
286 
287     ret = DeviceDriverBind(devNode);
288     if (ret != HDF_SUCCESS) {
289         return ret;
290     }
291 
292     return devNode->super.PublishService(devNode);
293 }
294 
HdfDeviceObjectRemoveService(struct HdfDeviceObject * dev)295 int HdfDeviceObjectRemoveService(struct HdfDeviceObject *dev)
296 {
297     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
298     if (dev == NULL) {
299         return HDF_ERR_INVALID_PARAM;
300     }
301 
302     return devNode->super.RemoveService(devNode);
303 }
HdfDeviceObjectSetServInfo(struct HdfDeviceObject * dev,const char * info)304 int HdfDeviceObjectSetServInfo(struct HdfDeviceObject *dev, const char *info)
305 {
306     if (dev == NULL || info == NULL || strlen(info) > SERVICE_INFO_LEN_MAX) {
307         return HDF_ERR_INVALID_PARAM;
308     }
309 
310     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
311     if (devNode->servInfo != NULL) {
312         OsalMemFree((char *)devNode->servInfo);
313     }
314     devNode->servInfo = HdfStringCopy(info);
315     if (devNode->servInfo == NULL) {
316         return HDF_ERR_MALLOC_FAIL;
317     }
318     return HDF_SUCCESS;
319 }
320 
HdfDeviceObjectUpdate(struct HdfDeviceObject * dev)321 int HdfDeviceObjectUpdate(struct HdfDeviceObject *dev)
322 {
323     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
324     if (dev == NULL) {
325         return HDF_ERR_INVALID_PARAM;
326     }
327     struct HdfServiceInfo servInfo;
328     HdfServiceInfoInit(&servInfo, devNode);
329     return DevSvcManagerClntUpdateService(&devNode->deviceObject, &servInfo);
330 }
331 
HdfDeviceObjectSetInterfaceDesc(struct HdfDeviceObject * dev,const char * interfaceDesc)332 int HdfDeviceObjectSetInterfaceDesc(struct HdfDeviceObject *dev, const char *interfaceDesc)
333 {
334     struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject);
335     if (dev == NULL || interfaceDesc == NULL) {
336         return HDF_ERR_INVALID_PARAM;
337     }
338     devNode->interfaceDesc = HdfStringCopy(interfaceDesc);
339     return devNode->interfaceDesc != NULL ? HDF_SUCCESS : HDF_ERR_MALLOC_FAIL;
340 }
341 
HdfDeviceObjectCheckInterfaceDesc(struct HdfDeviceObject * dev,struct HdfSBuf * data)342 bool __attribute__((weak)) HdfDeviceObjectCheckInterfaceDesc(struct HdfDeviceObject *dev, struct HdfSBuf *data)
343 {
344     (void)dev;
345     (void)data;
346     return true;
347 }