• 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 <unistd.h>
18 #include <sys/time.h>
19 #include <hdf_sbuf.h>
20 #include "securec.h"
21 #include "osal_atomic.h"
22 #include "hdf_base.h"
23 #include "hdf_log.h"
24 #include "hdf_device_desc.h"
25 #include "hdf_usb_pnp_manage.h"
26 
27 #define HDF_LOG_TAG   USB_HOST_PNP_TEST
28 
29 #if USB_PNP_NOTIFY_TEST_MODE == true
30 #define USB_HOST_PNP_TEST_SERVICE_NAME "hdf_usb_pnp_notify_service"
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 
TestPnpWriteLog(char * string)40 static void TestPnpWriteLog(char *string)
41 {
42     char str[STR_LEN] = {0};
43     FILE *fp = NULL;
44     struct timeval time;
45 
46     gettimeofday(&time, NULL);
47 
48     fp = fopen("/data/usbhost_pnp_xts", "a+");
49 
50     int32_t ret = snprintf_s(str, STR_LEN, STR_LEN - 1, "[XTSCHECK] %d.%06d, %s\n",
51         time.tv_sec, time.tv_usec, string);
52     if (ret < 0) {
53         HDF_LOGE("%s: sbuf write failed", __func__);
54         (void)fclose(fp);
55         return;
56     }
57 
58     (void)fwrite(str, strlen(str), 1, fp);
59     (void)fclose(fp);
60 }
61 
TestInitPnpInfo(enum UsbPnpNotifyServiceCmd cmdType)62 static void TestInitPnpInfo(enum UsbPnpNotifyServiceCmd cmdType)
63 {
64     struct UsbPnpNotifyMatchInfoTable infoTable;
65     uint8_t i;
66 
67     infoTable.usbDevAddr = 0;
68     infoTable.devNum = 0;
69     if (cmdType == USB_PNP_NOTIFY_REMOVE_TEST) {
70         infoTable.busNum = -1;
71     } else {
72         infoTable.busNum = 0;
73     }
74 
75     infoTable.deviceInfo.vendorId = 0xFFF0;
76     infoTable.deviceInfo.productId = 0xFFF0;
77     infoTable.deviceInfo.bcdDeviceLow = 0x0000;
78     infoTable.deviceInfo.bcdDeviceHigh = 0x0000;
79     infoTable.deviceInfo.deviceClass = 0;
80     infoTable.deviceInfo.deviceSubClass = 0;
81     infoTable.deviceInfo.deviceProtocol = 0;
82 
83     infoTable.removeType = USB_PNP_NOTIFY_REMOVE_BUS_DEV_NUM;
84 
85     if (cmdType != USB_PNP_NOTIFY_REMOVE_TEST) {
86         infoTable.numInfos = USB_TEST_INTERFACE_NUM;
87         for (i = 0; i < infoTable.numInfos; i++) {
88             infoTable.interfaceInfo[i].interfaceClass = 0;
89             infoTable.interfaceInfo[i].interfaceSubClass = 0;
90             infoTable.interfaceInfo[i].interfaceProtocol = 0;
91             infoTable.interfaceInfo[i].interfaceNumber = i;
92         }
93     }
94 
95     if (!HdfSbufWriteBuffer(g_data, (const void *)(&infoTable), sizeof(struct UsbPnpNotifyMatchInfoTable))) {
96         HDF_LOGE("%s: sbuf write infoTable failed", __func__);
97     }
98 }
99 
TestPnpAdd(struct HdfIoService * serv)100 static void TestPnpAdd(struct HdfIoService *serv)
101 {
102     int32_t replyData = 0;
103     bool flag = false;
104 
105     TestPnpWriteLog("usb pnp sample device driver test add start");
106 
107     TestInitPnpInfo(USB_PNP_NOTIFY_ADD_TEST);
108 
109     int32_t status = serv->dispatcher->Dispatch(&serv->object, USB_PNP_NOTIFY_ADD_TEST, g_data, g_reply);
110     if (status) {
111         HDF_LOGE("%s: Dispatch USB_PNP_NOTIFY_ADD_TEST failed status = %d", __func__, status);
112         return;
113     }
114 
115     flag = HdfSbufReadInt32(g_reply, &replyData);
116     if ((flag == false) || (replyData != INT32_MAX)) {
117         TestPnpWriteLog("usb pnp sample device driver test add reply faile.");
118     } else if ((flag == true) && (replyData == INT32_MAX)) {
119         TestPnpWriteLog("usb pnp sample device driver test add reply ok.");
120     }
121 }
122 
TestPnpRemove(struct HdfIoService * serv)123 static void TestPnpRemove(struct HdfIoService *serv)
124 {
125     int32_t replyData = 0;
126     bool flag = false;
127 
128     TestPnpWriteLog("usb pnp sample device driver test remove start");
129 
130     TestInitPnpInfo(USB_PNP_NOTIFY_REMOVE_TEST);
131 
132     int32_t status = serv->dispatcher->Dispatch(&serv->object, USB_PNP_NOTIFY_REMOVE_TEST, g_data, g_reply);
133     if (status) {
134         HDF_LOGE("%s: Dispatch USB_PNP_NOTIFY_REMOVE_TEST failed status = %d", __func__, status);
135         return;
136     }
137 
138     flag = HdfSbufReadInt32(g_reply, &replyData);
139     if ((flag == false) || (replyData != INT32_MAX)) {
140         TestPnpWriteLog("usb pnp sample device driver test remove reply faile.");
141     } else if ((flag == true) && (replyData == INT32_MAX)) {
142         TestPnpWriteLog("usb pnp sample device driver test remove reply ok.");
143     }
144 }
145 #endif
main(int32_t argc,char * argv[])146 int32_t main(int32_t argc, char *argv[])
147 {
148 #if USB_PNP_NOTIFY_TEST_MODE == true
149     HDF_LOGI("%s:%d usbhost pnp test start", __func__, __LINE__);
150     char cmd;
151     int32_t argNum = 2;
152     if (argc != argNum) {
153         HDF_LOGE("%s:%d invalid parma", __func__, __LINE__);
154         return HDF_FAILURE;
155     }
156     if (!strcmp(argv[1], "-add")) {
157         cmd = 'a';
158     } else if (!strcmp(argv[1], "-remove")) {
159         cmd = 'r';
160     } else {
161         HDF_LOGE("%s:%d invalid paramter", __func__, __LINE__);
162         return HDF_FAILURE;
163     }
164 
165     struct HdfIoService *serv = HdfIoServiceBind(USB_HOST_PNP_TEST_SERVICE_NAME);
166     if (serv == NULL) {
167         HDF_LOGE("%s:%d fail to get service %s", \
168             __func__, __LINE__, USB_HOST_PNP_TEST_SERVICE_NAME);
169         return HDF_FAILURE;
170     }
171 
172     g_data = HdfSbufObtainDefaultSize();
173     g_reply = HdfSbufObtainDefaultSize();
174     if (g_data == NULL || g_reply == NULL) {
175         HDF_LOGE("%s:%d GetService err", __func__, __LINE__);
176         return HDF_FAILURE;
177     }
178 
179     switch (cmd) {
180         case 'a':
181             TestPnpAdd(serv);
182             break;
183         case 'r':
184             TestPnpRemove(serv);
185             break;
186         default:
187             break;
188     }
189 
190     HdfSbufRecycle(g_data);
191     HdfSbufRecycle(g_reply);
192 
193     HdfIoServiceRecycle(serv);
194 
195     HDF_LOGI("%s:%d usbhost pnp test end", __func__, __LINE__);
196 #else
197     HDF_LOGE("%s:%d USB_PNP_NOTIFY_TEST_MODE is not support!!!", __func__, __LINE__);
198 #endif
199     return HDF_SUCCESS;
200 }
201 
202