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 <hdf_dlist.h>
17 #include <hdf_log.h>
18 #include <hdf_remote_service.h>
19 #include <hdf_service_status.h>
20 #include <osal_mem.h>
21 #define SERVSTAT_LISTENER_INTERFACE_DESCRIPTOR "HDI.IServiceStatusListener.V1_0"
22 struct ServstatListenerStub {
23 struct ServiceStatusListener listener;
24 struct HdfRemoteService *remote;
25 };
26
ServstatListenerStubRemoteDispatch(struct HdfRemoteService * remote,int cmdid,struct HdfSBuf * data,struct HdfSBuf * reply)27 int ServstatListenerStubRemoteDispatch(
28 struct HdfRemoteService *remote, int cmdid, struct HdfSBuf *data, struct HdfSBuf *reply)
29 {
30 (void)reply;
31 struct ServstatListenerStub *stub = (struct ServstatListenerStub *)remote;
32 struct ServiceStatus status;
33
34 if (cmdid != SERVIE_STATUS_LISTENER_NOTIFY) {
35 return HDF_ERR_NOT_SUPPORT;
36 }
37 if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) {
38 HDF_LOGE("failed to check interface");
39 return HDF_ERR_INVALID_PARAM;
40 }
41 if (ServiceStatusUnMarshalling(&status, data) != HDF_SUCCESS) {
42 return HDF_ERR_INVALID_PARAM;
43 }
44 HDF_LOGI("ServstatListenerStubRemoteDispatch: service name %{public}s", status.serviceName);
45
46 if (stub->listener.callback != NULL) {
47 stub->listener.callback(&stub->listener, &status);
48 return HDF_SUCCESS;
49 }
50 return HDF_ERR_INVALID_OBJECT;
51 }
52
ServiceStatusListenerMarshalling(struct ServiceStatusListener * listener,struct HdfSBuf * buf)53 int ServiceStatusListenerMarshalling(struct ServiceStatusListener *listener, struct HdfSBuf *buf)
54 {
55 if (listener == NULL || buf == NULL) {
56 return HDF_ERR_INVALID_PARAM;
57 }
58
59 struct ServstatListenerStub *listenerStub = CONTAINER_OF(listener, struct ServstatListenerStub, listener);
60 if (listenerStub->remote == NULL) {
61 return HDF_ERR_INVALID_OBJECT;
62 }
63 return HdfSbufWriteRemoteService(buf, listenerStub->remote);
64 }
65
HdiServiceStatusListenerNewInstance(void)66 struct ServiceStatusListener *HdiServiceStatusListenerNewInstance(void)
67 {
68 struct ServstatListenerStub *stub = OsalMemCalloc(sizeof(struct ServstatListenerStub));
69 if (stub == NULL) {
70 return NULL;
71 }
72 static struct HdfRemoteDispatcher remoteDispatch = {
73 .Dispatch = ServstatListenerStubRemoteDispatch,
74 };
75
76 stub->remote = HdfRemoteServiceObtain((struct HdfObject *)stub, &remoteDispatch);
77 if (stub->remote == NULL) {
78 OsalMemFree(stub);
79 return NULL;
80 }
81 if (!HdfRemoteServiceSetInterfaceDesc(stub->remote, SERVSTAT_LISTENER_INTERFACE_DESCRIPTOR)) {
82 HdfRemoteServiceRecycle(stub->remote);
83 stub->remote = NULL;
84 OsalMemFree(stub);
85 return NULL;
86 }
87 return &stub->listener;
88 }
89
HdiServiceStatusListenerFree(struct ServiceStatusListener * listener)90 void HdiServiceStatusListenerFree(struct ServiceStatusListener *listener)
91 {
92 if (listener == NULL) {
93 return;
94 }
95
96 struct ServstatListenerStub *stub = CONTAINER_OF(listener, struct ServstatListenerStub, listener);
97 HdfRemoteServiceRecycle(stub->remote);
98 OsalMemFree(stub);
99 }