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