• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
17 #include <sys/time.h>
18 #include <unistd.h>
19 #include <hdf_sbuf.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 
27 #define USB_HOST_PNP_TEST_SERVICE_NAME  "hdf_usb_pnp_notify_service"
28 #define USB_TEST_SAMPLE_MODULE_NAME     "usb_test_sample_driver"
29 #define USB_TEST_SAMPLE_SERVICE_NAME    "usb_test_sample_service"
30 
31 #define STR_LEN     1024
32 #ifndef INT32_MAX
33 #define INT32_MAX 0x7fffffff
34 #endif
35 #define USB_TEST_INTERFACE_NUM  2
36 
37 struct HdfSBuf *g_data;
38 struct HdfSBuf *g_reply;
39 
UsbPnpTestEventReceived(void * priv,uint32_t id,struct HdfSBuf * data)40 static int UsbPnpTestEventReceived(void *priv, uint32_t id, struct HdfSBuf *data)
41 {
42     int ret;
43 
44     switch (id) {
45         default:
46             ret = HDF_ERR_INVALID_PARAM;
47             break;
48     }
49 
50     HDF_LOGI("%s:%d ret=%d DONE", __func__, __LINE__, ret);
51 
52     return ret;
53 }
54 
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57     HDF_LOGI("%s:%d usbhost pnp test start", __func__, __LINE__);
58     int ret;
59     struct HdfIoService *testService = NULL;
60     static struct HdfDevEventlistener usbPnpTestListener = {
61         .callBack = UsbPnpTestEventReceived,
62     };
63 
64     struct HdfIoService *serv = HdfIoServiceBind(USB_HOST_PNP_TEST_SERVICE_NAME);
65     if (serv == NULL) {
66         HDF_LOGE("%s:%d fail to get service %s", \
67             __func__, __LINE__, USB_HOST_PNP_TEST_SERVICE_NAME);
68         return HDF_FAILURE;
69     }
70 
71     ret = HdfDeviceRegisterEventListener(serv, &usbPnpTestListener);
72     if (ret != HDF_SUCCESS) {
73         HdfIoServiceRecycle(serv);
74         HDF_LOGE("HdfDeviceRegisterEventListener faile ret=%d", ret);
75         return HDF_FAILURE;
76     }
77 
78     g_data = HdfSBufObtainDefaultSize();
79     g_reply = HdfSBufObtainDefaultSize();
80     if (g_data == NULL || g_reply == NULL) {
81         HdfIoServiceRecycle(serv);
82         HDF_LOGE("%s:%d GetService err", __func__, __LINE__);
83         return HDF_FAILURE;
84     }
85 
86     HdfSbufWriteString(g_data, USB_TEST_SAMPLE_MODULE_NAME);
87     HdfSbufWriteString(g_data, USB_TEST_SAMPLE_SERVICE_NAME);
88 
89     ret = serv->dispatcher->Dispatch(&serv->object, USB_PNP_DRIVER_REGISTER_DEVICE, g_data, NULL);
90     if (ret != HDF_SUCCESS) {
91         HDF_LOGE("%s:%d Dispatch USB_PNP_DRIVER_REGISTER_DEVICE err", __func__, __LINE__);
92         goto out;
93     }
94     HDF_LOGI("%s:%d", __func__, __LINE__);
95     testService = HdfIoServiceBind(USB_TEST_SAMPLE_SERVICE_NAME);
96     if (testService == NULL) {
97         HDF_LOGE("%s:%d testService USB_PNP_DRIVER_REGISTER_DEVICE err", __func__, __LINE__);
98         goto out;
99     }
100 
101     HDF_LOGI("%s:%d", __func__, __LINE__);
102     ret = serv->dispatcher->Dispatch(&serv->object, USB_PNP_DRIVER_UNREGISTER_DEVICE, g_data, NULL);
103     if (ret != HDF_SUCCESS) {
104         HdfIoServiceRecycle(testService);
105         HDF_LOGE("%s:%d Dispatch USB_PNP_DRIVER_UNREGISTER_DEVICE err", __func__, __LINE__);
106         goto out;
107     }
108 
109     HDF_LOGI("%s:%d", __func__, __LINE__);
110 
111 out:
112     HdfSBufRecycle(g_data);
113     HdfSBufRecycle(g_reply);
114 
115     HdfIoServiceRecycle(serv);
116 
117     HDF_LOGI("%s:%d usbhost pnp test end", __func__, __LINE__);
118     return HDF_SUCCESS;
119 }
120 
121