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