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 "devmgr_service_start.h"
10 #include "devhost_service_clnt.h"
11 #include "devmgr_service.h"
12 #include "devsvc_manager_clnt.h"
13 #include "hdf_base.h"
14 #include "hdf_device_node.h"
15 #include "hdf_io_service.h"
16 #include "hdf_log.h"
17 #include "hdf_sbuf.h"
18
19 #define DEV_MGR_NODE_PERM 0660
20
21 static int g_isQuickLoad = DEV_MGR_SLOW_LOAD;
22
GetDeviceServiceNameByClass(DeviceClass deviceClass,struct HdfSBuf * reply)23 static void GetDeviceServiceNameByClass(DeviceClass deviceClass, struct HdfSBuf *reply)
24 {
25 struct HdfSListIterator itDeviceInfo;
26 struct HdfDeviceInfo *deviceInfo = NULL;
27 struct DevHostServiceClnt *hostClnt = NULL;
28 struct DevmgrService *devMgrSvc = (struct DevmgrService *)DevmgrServiceGetInstance();
29 if (devMgrSvc == NULL || reply == NULL) {
30 return;
31 }
32
33 HdfSbufFlush(reply);
34 DLIST_FOR_EACH_ENTRY(hostClnt, &devMgrSvc->hosts, struct DevHostServiceClnt, node) {
35 HdfSListIteratorInit(&itDeviceInfo, hostClnt->deviceInfos);
36 while (HdfSListIteratorHasNext(&itDeviceInfo)) {
37 deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&itDeviceInfo);
38 if (deviceInfo->policy == SERVICE_POLICY_CAPACITY) {
39 struct HdfDeviceObject *deviceObject = DevSvcManagerClntGetDeviceObject(deviceInfo->svcName);
40 if (deviceObject == NULL || (deviceObject->deviceClass != deviceClass)) {
41 continue;
42 }
43 HdfSbufWriteString(reply, deviceInfo->svcName);
44 }
45 }
46 }
47 HdfSbufWriteString(reply, NULL);
48 }
49
DeviceManagerDispatch(struct HdfObject * stub,int code,struct HdfSBuf * data,struct HdfSBuf * reply)50 int DeviceManagerDispatch(struct HdfObject *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply)
51 {
52 int ret = HDF_FAILURE;
53 int32_t deviceClass = 0;
54 const char *svcName = NULL;
55 struct DevmgrService *devMgrSvc = (struct DevmgrService *)stub;
56 if (data == NULL || devMgrSvc == NULL) {
57 HDF_LOGE("%s: input param is invalid", __func__);
58 return ret;
59 }
60 OsalMutexLock(&devMgrSvc->devMgrMutex);
61 switch (code) {
62 case DEVMGR_LOAD_SERVICE:
63 svcName = HdfSbufReadString(data);
64 if (svcName == NULL) {
65 HDF_LOGE("%s: get svc name is null", __func__);
66 break;
67 }
68 static struct SubscriberCallback callback = {
69 .deviceObject = NULL,
70 .OnServiceConnected = NULL,
71 };
72 ret = DevSvcManagerClntSubscribeService(svcName, callback);
73 break;
74 case DEVMGR_UNLOAD_SERVICE:
75 svcName = HdfSbufReadString(data);
76 if (svcName == NULL) {
77 HDF_LOGE("%s: svc name is null", __func__);
78 break;
79 }
80 ret = DevSvcManagerClntUnsubscribeService(svcName);
81 break;
82 case DEVMGR_GET_SERVICE:
83 if (!HdfSbufReadInt32(data, &deviceClass)) {
84 HDF_LOGE("%s: failed to get deviceClass", __func__);
85 break;
86 }
87 GetDeviceServiceNameByClass(deviceClass, reply);
88 ret = HDF_SUCCESS;
89 break;
90 default:
91 HDF_LOGE("%s: unsupported configuration type: %d", __func__, code);
92 break;
93 }
94 OsalMutexUnlock(&devMgrSvc->devMgrMutex);
95 return ret;
96 }
97
DeviceManagerSetQuickLoad(int loadFlag)98 void DeviceManagerSetQuickLoad(int loadFlag)
99 {
100 g_isQuickLoad = loadFlag;
101 }
102
DeviceManagerIsQuickLoad(void)103 int DeviceManagerIsQuickLoad(void)
104 {
105 return g_isQuickLoad;
106 }
107
DeviceManagerStart(void)108 int DeviceManagerStart(void)
109 {
110 struct IDevmgrService *instance = DevmgrServiceGetInstance();
111
112 if (instance == NULL || instance->StartService == NULL) {
113 HDF_LOGE("device manager start failed, service instance is null");
114 return HDF_FAILURE;
115 }
116 struct HdfIoService *ioService = HdfIoServicePublish(DEV_MGR_NODE, DEV_MGR_NODE_PERM);
117 if (ioService != NULL) {
118 static struct HdfIoDispatcher dispatcher = {
119 .Dispatch = DeviceManagerDispatch,
120 };
121 ioService->dispatcher = &dispatcher;
122 ioService->target = &instance->base;
123 }
124 return instance->StartService(instance);
125 }
126
DeviceManagerStartStep2()127 int DeviceManagerStartStep2()
128 {
129 if (DeviceManagerIsQuickLoad() == DEV_MGR_SLOW_LOAD) {
130 HDF_LOGW("%s: device manager is not set to QuickLoad mode", __func__);
131 return HDF_SUCCESS;
132 }
133 struct DevmgrService *devMgrSvc = (struct DevmgrService *)DevmgrServiceGetInstance();
134 return DevmgrServiceLoadLeftDriver(devMgrSvc);
135 }
136
137