• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "devmgr_service_full.h"
17 #include "devhost_service.h"
18 #include "devhost_service_clnt.h"
19 #include "devhost_service_proxy.h"
20 #include "device_token_clnt.h"
21 #include "hdf_device_token.h"
22 #include "hdf_driver_installer.h"
23 #include "hdf_log.h"
24 #include "hdf_map.h"
25 #include "hdf_message_looper.h"
26 #include "osal_message.h"
27 
28 #define HDF_LOG_TAG devmgr_service_full
29 #define INVALID_PID (-1)
30 
31 static Map g_hostMap = {0};
32 #define HOST_INIT_DIE_NUM 1
33 #define HOST_MAX_DIE_NUM 3
34 
CleanupDiedHostResources(struct DevHostServiceClnt * hostClnt)35 static void CleanupDiedHostResources(struct DevHostServiceClnt *hostClnt)
36 {
37     hostClnt->hostPid = INVALID_PID;
38     if (hostClnt->hostService != NULL) {
39         struct DevHostServiceProxy *hostProxy = (struct DevHostServiceProxy *)hostClnt->hostService;
40         DevHostServiceProxyRecycle(hostProxy);
41         hostClnt->hostService = NULL;
42     }
43 
44     HdfSListFlush(&hostClnt->devices, DeviceTokenClntDelete);
45 }
46 
DevmgrServiceFullHandleDeviceHostDied(struct DevHostServiceClnt * hostClnt)47 static int32_t DevmgrServiceFullHandleDeviceHostDied(struct DevHostServiceClnt *hostClnt)
48 {
49     bool isHostEmpty = HdfSListIsEmpty(&hostClnt->devices);
50 
51     CleanupDiedHostResources(hostClnt);
52     if (isHostEmpty || hostClnt->stopFlag) {
53         return 0;
54     }
55 
56     if (g_hostMap.nodeSize == 0) {
57         MapInit(&g_hostMap);
58     }
59     int *hostDieValue = (int *)MapGet(&g_hostMap, hostClnt->hostName);
60     if (hostDieValue == NULL) {
61         int hostDieNum = HOST_INIT_DIE_NUM;
62         MapSet(&g_hostMap, hostClnt->hostName, &hostDieNum, sizeof(int));
63     } else {
64         if (*hostDieValue > HOST_MAX_DIE_NUM) {
65             HDF_LOGE("host %{public}s die 4 times, remove it", hostClnt->hostName);
66             *hostDieValue = 0;
67             hostClnt->stopFlag = false;
68             return INVALID_PID;
69         }
70         (*hostDieValue)++;
71     }
72     HDF_LOGI("%{public}s:%{public}d", __func__, __LINE__);
73     struct IDriverInstaller *installer = DriverInstallerGetInstance();
74     if (installer != NULL && installer->StartDeviceHost != NULL) {
75         HDF_LOGI("%{public}s:%{public}d", __func__, __LINE__);
76         hostClnt->hostPid = installer->StartDeviceHost(hostClnt->hostId, hostClnt->hostName);
77         return hostClnt->hostPid;
78     }
79     return INVALID_PID;
80 }
81 
DevmgrServiceFullOnDeviceHostDied(struct DevmgrServiceFull * inst,uint32_t hostId)82 void DevmgrServiceFullOnDeviceHostDied(struct DevmgrServiceFull *inst, uint32_t hostId)
83 {
84     (void)hostId;
85     struct DevHostServiceClnt *hostClnt = NULL;
86     struct DevHostServiceClnt *hostClntTmp = NULL;
87     if (inst == NULL) {
88         return;
89     }
90     OsalMutexLock(&inst->super.devMgrMutex);
91     DLIST_FOR_EACH_ENTRY_SAFE(hostClnt, hostClntTmp,  &inst->super.hosts, struct DevHostServiceClnt, node) {
92         if (hostClnt->hostId == hostId) {
93             int32_t ret = DevmgrServiceFullHandleDeviceHostDied(hostClnt);
94             if (ret == INVALID_PID) {
95                 HDF_LOGE("%{public}s: failed to respawn host %{public}s", __func__, hostClnt->hostName);
96             }
97             break;
98         }
99     }
100     OsalMutexUnlock(&inst->super.devMgrMutex);
101 }
102 
DevmgrServiceFullDispatchMessage(struct HdfMessageTask * task,struct HdfMessage * msg)103 int32_t DevmgrServiceFullDispatchMessage(struct HdfMessageTask *task, struct HdfMessage *msg)
104 {
105     (void)task;
106     struct DevmgrServiceFull *fullService =
107         (struct DevmgrServiceFull *)DevmgrServiceGetInstance();
108     if (msg == NULL) {
109         HDF_LOGE("Input msg is null");
110         return HDF_ERR_INVALID_PARAM;
111     }
112     switch (msg->messageId) {
113         case DEVMGR_MESSAGE_DEVHOST_DIED: {
114             int hostId = (int)(uintptr_t)msg->data[0];
115             DevmgrServiceFullOnDeviceHostDied(fullService, hostId);
116             break;
117         }
118         default: {
119             HDF_LOGE("wrong message(%{public}u)", msg->messageId);
120         }
121     }
122 
123     return HDF_SUCCESS;
124 }
125 
DevmgrServiceFullGetMessageTask(void)126 struct HdfMessageTask *DevmgrServiceFullGetMessageTask(void)
127 {
128     struct DevmgrServiceFull *fullService =
129         (struct DevmgrServiceFull *)DevmgrServiceGetInstance();
130     if (fullService != NULL) {
131         return &fullService->task;
132     }
133     HDF_LOGE("Get message task failed, fullService is null");
134     return NULL;
135 }
136 
DevmgrServiceFullConstruct(struct DevmgrServiceFull * inst)137 void DevmgrServiceFullConstruct(struct DevmgrServiceFull *inst)
138 {
139     static struct IHdfMessageHandler handler = {
140         .Dispatch = DevmgrServiceFullDispatchMessage
141     };
142     if (inst != NULL) {
143         HdfMessageLooperConstruct(&inst->looper);
144         DevmgrServiceConstruct(&inst->super);
145         HdfMessageTaskConstruct(&inst->task, &inst->looper, &handler);
146     }
147 }
148 
DevmgrServiceFullCreate(void)149 struct HdfObject *DevmgrServiceFullCreate(void)
150 {
151     static struct DevmgrServiceFull *instance = NULL;
152     if (instance == NULL) {
153         static struct DevmgrServiceFull fullInstance;
154         DevmgrServiceFullConstruct(&fullInstance);
155         instance = &fullInstance;
156     }
157     return (struct HdfObject *)instance;
158 }
159 
DeviceManagerIsQuickLoad(void)160 int DeviceManagerIsQuickLoad(void)
161 {
162     return false;
163 }
164