1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "flow_control_test.h"
10 #include "flow_control.h"
11 #include "hdf_log.h"
12 #include "securec.h"
13 #include "osal_time.h"
14 
15 #define WATITE_RESULT_TIME 1000 // 1000ms
16 static struct FlowControlModule *g_flowControlInstance = NULL;
17 static bool g_result = false;
18 
19 static uint8_t g_dhcpData[] = {
20     0x42, 0x2b, 0x13, 0x41, 0xc9, 0xd7, 0x38, 0x81, 0x13, 0x1c, 0x6d, 0xad, 0x08, 0x00, 0x45, 0x00, 0x01, 0x50, 0x00,
21     0x01, 0x00, 0x00, 0xff, 0x11, 0xba, 0x9c, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x44, 0x00, 0x43,
22     0x01, 0x3c, 0xd4, 0xa8
23 };
24 
IsDeviceStaOrP2PClient(void)25 static bool IsDeviceStaOrP2PClient(void)
26 {
27     return true;
28 }
29 
SendDataPacket(NetBufQueue * q,void * fcmPrivate,int32_t fwPriorityId)30 static int32_t SendDataPacket(NetBufQueue *q, void *fcmPrivate, int32_t fwPriorityId)
31 {
32     NetBuf *buff = NULL;
33     uint32_t count = NetBufQueueSize(q);
34     if (count != 1) {
35         HDF_LOGE("%s count error", __func__);
36         return HDF_FAILURE;
37     }
38     buff = NetBufQueueDequeue(q);
39     g_result = false;
40     HDF_LOGE("%s enter", __func__);
41     if (memcmp(NetBufGetAddress(buff, E_DATA_BUF), g_dhcpData, sizeof(g_dhcpData) / sizeof(g_dhcpData[0])) != 0) {
42         HDF_LOGE("%s fail : memcmp != size", __func__);
43         return HDF_FAILURE;
44     }
45     g_result = true;
46     (void)fcmPrivate;
47     (void)fwPriorityId;
48     return HDF_SUCCESS;
49 }
50 
51 static struct FlowControlOp g_flowControlOp = {
52     .isDeviceStaOrP2PClient = IsDeviceStaOrP2PClient,
53     .txDataPacket = SendDataPacket,
54     .rxDataPacket = NULL,
55     .getTxQueueId = NULL,
56     .getRxQueueId = NULL,
57     .getTxPriorityId = NULL,
58     .getRxPriorityId = NULL,
59 };
60 
WiFiFlowControlTestEnv(void)61 static bool WiFiFlowControlTestEnv(void)
62 {
63     if (g_flowControlInstance == NULL) {
64         g_flowControlInstance = InitFlowControl(NULL);
65         if (g_flowControlInstance == NULL) {
66             HDF_LOGE("%s fail : InitFlowControl = null!", __func__);
67             return false;
68         }
69     }
70     g_flowControlInstance->interface->registerFlowControlOp(g_flowControlInstance, &g_flowControlOp);
71     return true;
72 }
73 
WiFiFlowControlTestOut(void)74 static bool WiFiFlowControlTestOut(void)
75 {
76     if (g_flowControlInstance != NULL) {
77         DeInitFlowControl(g_flowControlInstance);
78         g_flowControlInstance = NULL;
79     }
80     return true;
81 }
82 
ConstructEapolNetBuf(void)83 static NetBuf *ConstructEapolNetBuf(void)
84 {
85     NetBuf *buff = NULL;
86     int count = sizeof(g_dhcpData) / sizeof(g_dhcpData[0]);
87     buff = NetBufAlloc(count);
88     if (buff == NULL) {
89         HDF_LOGE("%s fail : NetBufAlloc = null!", __func__);
90         return buff;
91     }
92     NetBufPush(buff, E_DATA_BUF, count);
93     if (memcpy_s(NetBufGetAddress(buff, E_DATA_BUF), count, g_dhcpData, count) != EOK) {
94         NetBufFree(buff);
95         buff = NULL;
96         HDF_LOGE("%s fail : memcpy_s fail", __func__);
97         return buff;
98     }
99     return buff;
100 }
101 
WiFiFlowControlTestInit(void)102 int32_t WiFiFlowControlTestInit(void)
103 {
104     if (WiFiFlowControlTestEnv()) {
105         return HDF_SUCCESS;
106     }
107     return HDF_FAILURE;
108 }
109 
WiFiFlowControlTestDeinit(void)110 int32_t WiFiFlowControlTestDeinit(void)
111 {
112     if (WiFiFlowControlTestOut()) {
113         return HDF_SUCCESS;
114     }
115     return HDF_FAILURE;
116 }
117 
WiFiFlowControlTestGetEapolQueueId(void)118 int32_t WiFiFlowControlTestGetEapolQueueId(void)
119 {
120     NetBuf *buff = NULL;
121     FlowControlQueueID id;
122     if (!WiFiFlowControlTestEnv()) {
123         return HDF_FAILURE;
124     }
125     buff = ConstructEapolNetBuf();
126     if (buff == NULL) {
127         return HDF_FAILURE;
128     }
129     if (g_flowControlInstance->interface == NULL) {
130         HDF_LOGE("%s interface =null!", __func__);
131         return HDF_FAILURE;
132     }
133     id = g_flowControlInstance->interface->getQueueIdByEtherBuff(buff);
134     HDF_LOGE("%s get id =%d!", __func__, id);
135 
136     NetBufFree(buff);
137     DeInitFlowControl(g_flowControlInstance);
138     g_flowControlInstance = NULL;
139     if (id == VIP_QUEUE_ID) {
140         return HDF_SUCCESS;
141     }
142     return HDF_FAILURE;
143 }
144 
WiFiFlowControlTestSendData(void)145 int32_t WiFiFlowControlTestSendData(void)
146 {
147     NetBuf *buff = NULL;
148     FlowControlQueueID id;
149     if (!WiFiFlowControlTestEnv()) {
150         return false;
151     }
152     buff = ConstructEapolNetBuf();
153     if (buff == NULL) {
154         return HDF_FAILURE;
155     }
156     if (g_flowControlInstance->interface == NULL) {
157         HDF_LOGE("%s interface = null!", __func__);
158         return HDF_FAILURE;
159     }
160     id = g_flowControlInstance->interface->getQueueIdByEtherBuff(buff);
161     g_result = false;
162     if (g_flowControlInstance->interface->sendBuffToFCM(g_flowControlInstance, buff, id, FLOW_TX) != HDF_SUCCESS) {
163         HDF_LOGE("%s sendBuffToFCM fail!", __func__);
164         return HDF_FAILURE;
165     }
166     if (g_flowControlInstance->interface->schedFCM(g_flowControlInstance, FLOW_TX) != HDF_SUCCESS) {
167         HDF_LOGE("%s sendBuffToFCM fail!", __func__);
168         return HDF_FAILURE;
169     }
170     OsalMSleep(WATITE_RESULT_TIME);
171     NetBufFree(buff);
172     DeInitFlowControl(g_flowControlInstance);
173     g_flowControlInstance = NULL;
174     HDF_LOGE("%s g_result = %d!", __func__, g_result);
175     return g_result ? HDF_SUCCESS : HDF_FAILURE;
176 }