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_sbuf.h>
17 #include <stdio.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include "hdf_base.h"
21 #include "hdf_device_desc.h"
22 #include "hdf_log.h"
23 #include "hdf_usb_pnp_manage.h"
24 
25 #define HDF_LOG_TAG                    USB_HOST_PNP_TEST
26 #define USB_HOST_PNP_TEST_SERVICE_NAME "hdf_usb_pnp_notify_service"
27 #define USB_TEST_SAMPLE_MODULE_NAME    "usb_test_sample_driver"
28 #define USB_TEST_SAMPLE_SERVICE_NAME   "usb_test_sample_service"
29 #define STR_LEN                        1024
30 #define USB_TEST_INTERFACE_NUM         2
31 
32 #ifndef INT32_MAX
33 #define INT32_MAX 0x7fffffff
34 #endif
35 
UsbPnpTestEventReceived(void * priv,uint32_t id,struct HdfSBuf * data)36 static int32_t UsbPnpTestEventReceived(void *priv, uint32_t id, struct HdfSBuf *data)
37 {
38     (void)priv;
39     (void)data;
40     int32_t ret = HDF_ERR_INVALID_PARAM;
41     HDF_LOGI("%{public}s:%{public}d id is %{public}u", __func__, __LINE__, id);
42     return ret;
43 }
44 
main(int32_t argc,char * argv[])45 int32_t main(int32_t argc, char *argv[])
46 {
47     (void)argc;
48     (void)argv;
49     struct HdfSBuf *data;
50     struct HdfSBuf *reply;
51     struct HdfIoService *testService = NULL;
52     static struct HdfDevEventlistener usbPnpTestListener = {
53         .callBack = UsbPnpTestEventReceived,
54     };
55 
56     struct HdfIoService *serv = HdfIoServiceBind(USB_HOST_PNP_TEST_SERVICE_NAME);
57     if (serv == NULL) {
58         HDF_LOGE("%{public}s: fail to get service %{public}s", __func__, USB_HOST_PNP_TEST_SERVICE_NAME);
59         return HDF_FAILURE;
60     }
61 
62     if (HdfDeviceRegisterEventListener(serv, &usbPnpTestListener) != HDF_SUCCESS) {
63         HdfIoServiceRecycle(serv);
64         HDF_LOGE("HdfDeviceRegisterEventListener failed");
65         return HDF_FAILURE;
66     }
67 
68     data = HdfSbufObtainDefaultSize();
69     reply = HdfSbufObtainDefaultSize();
70     if (data == NULL || reply == NULL) {
71         HdfIoServiceRecycle(serv);
72         HDF_LOGE("%{public}s: GetService err", __func__);
73         return HDF_FAILURE;
74     }
75 
76     HdfSbufWriteString(data, USB_TEST_SAMPLE_MODULE_NAME);
77     HdfSbufWriteString(data, USB_TEST_SAMPLE_SERVICE_NAME);
78 
79     if (serv->dispatcher->Dispatch(&serv->object, USB_PNP_DRIVER_REGISTER_DEVICE, data, NULL) != HDF_SUCCESS) {
80         HDF_LOGE("%{public}s: Dispatch pnp register device err", __func__);
81         goto OUT;
82     }
83 
84     testService = HdfIoServiceBind(USB_TEST_SAMPLE_SERVICE_NAME);
85     if (testService == NULL) {
86         HDF_LOGE("%{public}s: HdfIoServiceBind err", __func__);
87         goto OUT;
88     }
89 
90     if (serv->dispatcher->Dispatch(&serv->object, USB_PNP_DRIVER_UNREGISTER_DEVICE, data, NULL) != HDF_SUCCESS) {
91         HdfIoServiceRecycle(testService);
92         HDF_LOGE("%{public}s: Dispatch pnp unregister device err", __func__);
93         goto OUT;
94     }
95     return HDF_SUCCESS;
96 OUT:
97     HdfSbufRecycle(data);
98     HdfSbufRecycle(reply);
99     HdfIoServiceRecycle(serv);
100     return HDF_FAILURE;
101 }
102