• 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 "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     if (data != NULL) {
42         HdfSbufRecycle(data);
43     }
44     return status;
45 }
46 
DevHostServiceProxyAddDevice(struct IDevHostService * inst,const struct HdfDeviceInfo * attribute)47 static int32_t DevHostServiceProxyAddDevice(
48     struct IDevHostService *inst, const struct HdfDeviceInfo *attribute)
49 {
50     return DevHostServiceProxyOpsDevice(inst, attribute, DEVHOST_SERVICE_ADD_DEVICE);
51 }
52 
DevHostServiceProxyDelDevice(struct IDevHostService * inst,devid_t devid)53 static int32_t DevHostServiceProxyDelDevice(
54     struct IDevHostService *inst, devid_t devid)
55 {
56     int status = HDF_FAILURE;
57     struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
58     struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst;
59     if (hostClnt->remote == NULL || data == NULL) {
60         HDF_LOGE("Del device failed, hostClnt->remote or data is null");
61         goto FINISHED;
62     }
63 
64     HdfSbufWriteUint32(data, devid);
65     status = hostClnt->remote->dispatcher->Dispatch(hostClnt->remote, DEVHOST_SERVICE_DEL_DEVICE, data, NULL);
66 
67 FINISHED:
68     if (data != NULL) {
69         HdfSbufRecycle(data);
70     }
71     return status;
72 }
73 
DevHostServiceProxyOnRemoteDied(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)74 void DevHostServiceProxyOnRemoteDied(struct HdfDeathRecipient *recipient, struct HdfRemoteService *service)
75 {
76     if (recipient == NULL || service == NULL) {
77         return;
78     }
79 
80     struct HdfMessageTask *task = DevmgrServiceFullGetMessageTask();
81     if (task == NULL) {
82         return;
83     }
84     struct HdfMessage *message = HdfMessageObtain(sizeof(void *));
85     if (message == NULL) {
86         return;
87     }
88     struct DevHostServiceProxy *proxy = HDF_SLIST_CONTAINER_OF(
89         struct HdfDeathRecipient, recipient, struct DevHostServiceProxy, recipient);
90     message->messageId = DEVMGR_MESSAGE_DEVHOST_DIED;
91     message->data[0] = (void *)(uintptr_t)proxy->hostId;
92     message->data[1] = (void *)service;
93     HDF_LOGD("%{public}s: host %{public}u dead, respawn it", __FUNCTION__, proxy->hostId);
94     task->SendMessage(task, message, false);
95 }
96 
DevHostServiceProxyConstruct(struct DevHostServiceProxy * inst,struct HdfRemoteService * remote)97 void DevHostServiceProxyConstruct(
98     struct DevHostServiceProxy *inst, struct HdfRemoteService *remote)
99 {
100     inst->remote = remote;
101     inst->super.AddDevice = DevHostServiceProxyAddDevice;
102     inst->super.DelDevice = DevHostServiceProxyDelDevice;
103     inst->recipient.OnRemoteDied = DevHostServiceProxyOnRemoteDied;
104     if (remote != NULL) {
105         remote->target = (struct HdfObject *)inst;
106     }
107 
108     HdfRemoteServiceAddDeathRecipient(remote, &inst->recipient);
109 }
110 
DevHostServiceProxyObtain(uint32_t hostId,struct HdfRemoteService * remote)111 struct IDevHostService *DevHostServiceProxyObtain(uint32_t hostId, struct HdfRemoteService *remote)
112 {
113     struct DevHostServiceProxy *instance =
114         (struct DevHostServiceProxy *)OsalMemCalloc(sizeof(struct DevHostServiceProxy));
115     if (instance != NULL) {
116         instance->hostId = hostId;
117         DevHostServiceProxyConstruct(instance, remote);
118     }
119     return (struct IDevHostService *)instance;
120 }
121 
DevHostServiceProxyRecycle(struct DevHostServiceProxy * inst)122 void DevHostServiceProxyRecycle(struct DevHostServiceProxy *inst)
123 {
124     if (inst != NULL) {
125         if (inst->remote != NULL) {
126             HdfRemoteServiceRecycle(inst->remote);
127             inst->remote = NULL;
128         }
129         OsalMemFree(inst);
130     }
131 }
132 
133