1 /*
2  * Copyright (c) 2021-2023 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 /**
17  * @addtogroup DriverHdi
18  * @{
19  *
20  * @brief Provides APIs for a system ability to obtain hardware device interface (HDI) services,
21  * load or unload a device, and listen for service status, and capabilities for the hdi-gen tool to
22  * automatically generate code in the interface description language (IDL).
23  *
24  * The HDF and the IDL code generated allow system abilities to access the HDI driver service.
25  *
26  * @since 1.0
27  */
28 
29 /**
30  * @file iservstat_listener_hdi.h
31  *
32  * @brief Defines the data structs and interface types related to service status listening based on C++.
33  *
34  * @since 1.0
35  */
36 #ifndef HDI_SERVICE_STATUS_LISTENER_INF_H
37 #define HDI_SERVICE_STATUS_LISTENER_INF_H
38 
39 #include <refbase.h>
40 #include <iremote_broker.h>
41 #include <iremote_stub.h>
42 
43 #include "hdf_device_class.h"
44 
45 namespace OHOS {
46 namespace HDI {
47 namespace ServiceManager {
48 namespace V1_0 {
49 /**
50  * @brief Enumerates the service statuses.
51  */
52 enum ServiceStatusType {
53     /** The service is started. */
54     SERVIE_STATUS_START,
55     /** The service status changes. */
56     SERVIE_STATUS_CHANGE,
57     /** The service is stopped. */
58     SERVIE_STATUS_STOP,
59     /** register service listener status. */
60     SERVIE_STATUS_REGISTER,
61     /** Maximum value of the service status. */
62     SERVIE_STATUS_MAX,
63 };
64 
65 /**
66  * @brief Defines the service status struct.
67  * The HDF uses this struct to notify the service module of the service status.
68  */
69 struct ServiceStatus {
70     /** Service name */
71     std::string serviceName;
72     /** Device type */
73     uint16_t deviceClass;
74     /** Service status */
75     uint16_t status;
76     /** Service information */
77     std::string info;
78 };
79 
80 /**
81  * @brief Defines the <b>ServStatListener</b> class.
82  */
83 class IServStatListener : public ::OHOS::IRemoteBroker {
84 public:
85     /** HDI interface descriptor, which is used to verify the permission for accessing the HDI interface. */
86     DECLARE_INTERFACE_DESCRIPTOR(u"HDI.IServiceStatusListener.V1_0");
87 
88     /**
89      * @brief Callback of the listener. You need to implement this callback for the service module.
90      *
91      * @param status Indicates the service status obtained.
92      */
93     virtual void OnReceive(const ServiceStatus &status) = 0;
94 };
95 
96 /**
97  * @brief Defines the listener stub class to implement serialization and deserialization of interface parameters.
98  * The listener implementation class must inherit this class and implement <b>OnReceive()</b>.
99  */
100 class ServStatListenerStub : public ::OHOS::IRemoteStub<IServStatListener> {
101 public:
102     ServStatListenerStub() = default;
~ServStatListenerStub()103     virtual ~ServStatListenerStub() {}
104 
105     /**
106      * @brief Distributes messages based on the invoking ID.
107      * This API calls the private function <b>ServStatListenerStubOnReceive()</b>.
108      *
109      * @param code Indicates the distribution command word called by the IPC.
110      * @param data Indicates the input parameter, through which the HDF returns
111      * the service status information to the service module.
112      * @param reply Indicates the data returned by the service module to the HDF.
113      * @param option Indicates the call type.
114      * @return Returns <b>HDF_SUCCESS</b> if the operation is successful. Otherwise, the operation fails.
115      */
116     int OnRemoteRequest(uint32_t code,
117         ::OHOS::MessageParcel &data, ::OHOS::MessageParcel &reply, ::OHOS::MessageOption &option) override;
118 private:
119     /**
120      * @brief Marshals or unmarshals <b>OnReceive()</b> parameters.
121      *
122      * @param code Indicates the distribution command word called by the IPC.
123      * @param data Indicates the input parameter, through which the HDF returns
124      * the service status information to the service module.
125      * @param reply Indicates the data returned by the service module to the HDF.
126      * @param option Indicates the call type.
127      * @return Returns <b>HDF_SUCCESS</b> if the operation is successful. Otherwise, the operation fails.
128      */
129     int32_t ServStatListenerStubOnReceive(::OHOS::MessageParcel& data,
130         ::OHOS::MessageParcel& reply, ::OHOS::MessageOption& option);
131 };
132 } // namespace V1_0
133 } // namespace ServiceManager
134 } // namespace HDI
135 } // namespace OHOS
136 
137 #endif /* HDI_SERVICE_STATUS_LISTENER_INF_H */
138