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 "devhost_service_proxy.h"
17 #include "dev_attribute_serialize.h"
18 #include "devmgr_service_full.h"
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21 #include "hdf_sbuf.h"
22 #include "osal_mem.h"
23 #include "osal_message.h"
24
25 #define HDF_LOG_TAG devhost_service_proxy
26
DevHostServiceProxyOpsDevice(struct IDevHostService * inst,const struct HdfDeviceInfo * attribute,int opsCode)27 static int32_t DevHostServiceProxyOpsDevice(
28 struct IDevHostService *inst, const struct HdfDeviceInfo *attribute, int opsCode)
29 {
30 int status = HDF_FAILURE;
31 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
32 struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst;
33 if (hostClnt->remote == NULL || data == NULL) {
34 HDF_LOGE("Adding device failed, hostClnt->remote or data or reply is null");
35 goto FINISHED;
36 }
37
38 DeviceAttributeSerialize(attribute, data);
39 status = hostClnt->remote->dispatcher->Dispatch(hostClnt->remote, opsCode, data, NULL);
40 FINISHED:
41 HdfSbufRecycle(data);
42 return status;
43 }
44
DevHostServiceProxyAddDevice(struct IDevHostService * inst,const struct HdfDeviceInfo * attribute)45 static int32_t DevHostServiceProxyAddDevice(
46 struct IDevHostService *inst, const struct HdfDeviceInfo *attribute)
47 {
48 return DevHostServiceProxyOpsDevice(inst, attribute, DEVHOST_SERVICE_ADD_DEVICE);
49 }
50
DevHostServiceProxyDelDevice(struct IDevHostService * inst,devid_t devid)51 static int32_t DevHostServiceProxyDelDevice(
52 struct IDevHostService *inst, devid_t devid)
53 {
54 int status = HDF_FAILURE;
55 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
56 struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst;
57 if (hostClnt->remote == NULL || data == NULL) {
58 HDF_LOGE("Del device failed, hostClnt->remote or data is null");
59 goto FINISHED;
60 }
61
62 HdfSbufWriteUint32(data, devid);
63 status = hostClnt->remote->dispatcher->Dispatch(hostClnt->remote, DEVHOST_SERVICE_DEL_DEVICE, data, NULL);
64
65 FINISHED:
66 HdfSbufRecycle(data);
67 return status;
68 }
69
DevHostServiceProxyOnRemoteDied(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)70 void DevHostServiceProxyOnRemoteDied(struct HdfDeathRecipient *recipient, struct HdfRemoteService *service)
71 {
72 if (recipient == NULL || service == NULL) {
73 return;
74 }
75
76 struct HdfMessageTask *task = DevmgrServiceFullGetMessageTask();
77 if (task == NULL) {
78 return;
79 }
80 struct HdfMessage *message = HdfMessageObtain(sizeof(void *));
81 if (message == NULL) {
82 return;
83 }
84 struct DevHostServiceProxy *proxy = HDF_SLIST_CONTAINER_OF(
85 struct HdfDeathRecipient, recipient, struct DevHostServiceProxy, recipient);
86 message->messageId = DEVMGR_MESSAGE_DEVHOST_DIED;
87 message->data[0] = (void *)(uintptr_t)proxy->hostId;
88 message->data[1] = (void *)service;
89 HDF_LOGD("%{public}s: host %{public}u dead, respawn it", __FUNCTION__, proxy->hostId);
90 task->SendMessage(task, message, false);
91 }
92
DevHostServicProxyDump(struct IDevHostService * inst,struct HdfSBuf * data,struct HdfSBuf * reply)93 static int32_t DevHostServicProxyDump(struct IDevHostService *inst, struct HdfSBuf *data, struct HdfSBuf *reply)
94 {
95 struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst;
96 if (hostClnt->remote == NULL || data == NULL || reply == NULL) {
97 HDF_LOGE("dump host failed, hostClnt->remote or data or reply is null");
98 return HDF_FAILURE;
99 }
100
101 int status = hostClnt->remote->dispatcher->Dispatch(hostClnt->remote, DEVHOST_SERVICE_DUMP, data, reply);
102 return status;
103 }
104
DevHostServiceProxyConstruct(struct DevHostServiceProxy * inst,struct HdfRemoteService * remote)105 void DevHostServiceProxyConstruct(
106 struct DevHostServiceProxy *inst, struct HdfRemoteService *remote)
107 {
108 inst->remote = remote;
109 inst->super.AddDevice = DevHostServiceProxyAddDevice;
110 inst->super.DelDevice = DevHostServiceProxyDelDevice;
111 inst->super.Dump = DevHostServicProxyDump;
112 inst->recipient.OnRemoteDied = DevHostServiceProxyOnRemoteDied;
113 if (remote != NULL) {
114 remote->target = (struct HdfObject *)inst;
115 }
116
117 HdfRemoteServiceAddDeathRecipient(remote, &inst->recipient);
118 }
119
DevHostServiceProxyObtain(uint32_t hostId,struct HdfRemoteService * remote)120 struct IDevHostService *DevHostServiceProxyObtain(uint32_t hostId, struct HdfRemoteService *remote)
121 {
122 struct DevHostServiceProxy *instance =
123 (struct DevHostServiceProxy *)OsalMemCalloc(sizeof(struct DevHostServiceProxy));
124 if (instance != NULL) {
125 instance->hostId = hostId;
126 DevHostServiceProxyConstruct(instance, remote);
127 }
128 return (struct IDevHostService *)instance;
129 }
130
DevHostServiceProxyRecycle(struct DevHostServiceProxy * inst)131 void DevHostServiceProxyRecycle(struct DevHostServiceProxy *inst)
132 {
133 if (inst != NULL) {
134 if (inst->remote != NULL) {
135 HdfRemoteServiceRecycle(inst->remote);
136 inst->remote = NULL;
137 }
138 OsalMemFree(inst);
139 }
140 }
141
142