1 /*
2  * Copyright (c) 2021-2022 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_stub.h"
17 #include "dev_attribute_serialize.h"
18 #include "devhost_dump.h"
19 #include "devhost_service_proxy.h"
20 #include "hdf_base.h"
21 #include "hdf_log.h"
22 #include "hdf_sbuf.h"
23 #include "osal_mem.h"
24 
25 #define HDF_LOG_TAG devhost_service_stub
26 
DevHostSetCurrentSecurec(const char * hostName)27 static void DevHostSetCurrentSecurec(const char *hostName)
28 {
29     (void)hostName;
30 }
31 
DispatchAddDevice(struct IDevHostService * serviceIf,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int DispatchAddDevice(struct IDevHostService *serviceIf, struct HdfSBuf *data, struct HdfSBuf *reply)
33 {
34     (void)reply;
35     if ((serviceIf == NULL) || (serviceIf->AddDevice == NULL)) {
36         HDF_LOGE("serviceIf or serviceIf->AddDevice is NULL");
37         return HDF_FAILURE;
38     }
39     struct HdfDeviceInfo *attribute = DeviceAttributeDeserialize(data);
40     if (attribute == NULL) {
41         HDF_LOGE("Dispatch failed, attribute is null");
42         return HDF_FAILURE;
43     }
44     int ret = serviceIf->AddDevice(serviceIf, attribute);
45     if (ret != HDF_SUCCESS) {
46         HDF_LOGE("Dispatch failed, add service failed and ret is %{public}d", ret);
47     }
48     DeviceSerializedAttributeRelease(attribute);
49     return ret;
50 }
51 
DispatchDelDevice(struct IDevHostService * serviceIf,struct HdfSBuf * data,struct HdfSBuf * reply)52 static int DispatchDelDevice(struct IDevHostService *serviceIf, struct HdfSBuf *data, struct HdfSBuf *reply)
53 {
54     uint32_t deviceId = 0;
55     (void)reply;
56     if ((serviceIf == NULL) || (serviceIf->DelDevice == NULL)) {
57         HDF_LOGE("serviceIf or serviceIf->DelDevice is NULL");
58         return HDF_FAILURE;
59     }
60     if (!HdfSbufReadUint32(data, &deviceId)) {
61         HDF_LOGE("failed to del device, invalid device id");
62         return HDF_FAILURE;
63     }
64 
65     int ret = serviceIf->DelDevice(serviceIf, deviceId);
66     if (ret != HDF_SUCCESS) {
67         HDF_LOGE("del service failed, ret is %{public}d", ret);
68     }
69     return ret;
70 }
71 
DispatchDump(struct IDevHostService * serviceIf,struct HdfSBuf * data,struct HdfSBuf * reply)72 int32_t DispatchDump(struct IDevHostService *serviceIf, struct HdfSBuf *data, struct HdfSBuf *reply)
73 {
74     (void)serviceIf;
75 
76     DevHostDump(data, reply);
77     return HDF_SUCCESS;
78 }
79 
DevHostServiceStubDispatch(struct HdfRemoteService * stub,int code,struct HdfSBuf * data,struct HdfSBuf * reply)80 static int DevHostServiceStubDispatch(
81     struct HdfRemoteService *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply)
82 {
83     int ret = HDF_FAILURE;
84     if (stub == NULL || data == NULL) {
85         return ret;
86     }
87     struct DevHostServiceStub *serviceStub = (struct DevHostServiceStub *)stub;
88     struct IDevHostService *serviceIf = (struct IDevHostService *)&serviceStub->super;
89     DevHostSetCurrentSecurec(serviceStub->super.super.hostName);
90     OsalMutexLock(&serviceStub->hostSvcMutex);
91     switch (code) {
92         case DEVHOST_SERVICE_ADD_DEVICE: {
93             ret = DispatchAddDevice(serviceIf, data, reply);
94             break;
95         }
96         case DEVHOST_SERVICE_DEL_DEVICE: {
97             ret = DispatchDelDevice(serviceIf, data, reply);
98             break;
99         }
100         case DEVHOST_SERVICE_DUMP: {
101             ret = DispatchDump(serviceIf, data, reply);
102             break;
103         }
104         default: {
105             HDF_LOGE("DevHostServiceStubDispatch unknown code:%{public}d", code);
106             break;
107         }
108     }
109     OsalMutexUnlock(&serviceStub->hostSvcMutex);
110     return ret;
111 }
112 
DevHostServiceStubConstruct(struct DevHostServiceStub * inst)113 static void DevHostServiceStubConstruct(struct DevHostServiceStub *inst)
114 {
115     static struct HdfRemoteDispatcher dispatcher = {.Dispatch = DevHostServiceStubDispatch};
116 
117     DevHostServiceFullConstruct(&inst->super);
118     inst->remote = HdfRemoteServiceObtain((struct HdfObject *)inst, &dispatcher);
119 
120     OsalMutexInit(&inst->hostSvcMutex);
121 }
122 
DevHostServiceStubCreate(void)123 struct HdfObject *DevHostServiceStubCreate(void)
124 {
125     static struct DevHostServiceStub *instance = NULL;
126     if (instance != NULL) {
127         return (struct HdfObject *)&instance->super;
128     }
129     instance = (struct DevHostServiceStub *)OsalMemCalloc(sizeof(struct DevHostServiceStub));
130     if (instance != NULL) {
131         DevHostServiceStubConstruct(instance);
132         return (struct HdfObject *)&instance->super;
133     }
134     return NULL;
135 }
136 
DevHostServiceStubRelease(struct HdfObject * object)137 void DevHostServiceStubRelease(struct HdfObject *object)
138 {
139     struct DevHostServiceStub *instance = (struct DevHostServiceStub *)object;
140     if (instance != NULL) {
141         DevHostServiceFullDestruct(&instance->super);
142         if (instance->remote != NULL) {
143             HdfRemoteServiceRecycle(instance->remote);
144             instance->remote = NULL;
145         }
146         OsalMutexDestroy(&instance->hostSvcMutex);
147         OsalMemFree(instance);
148     }
149 }
150