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