• 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 <securec.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 
21 #include "discovery_service.h"
22 #include "lwip/netif.h"
23 #include "lwip/netifapi.h"
24 #include "ohos_init.h"
25 #include "softbus_common.h"
26 #include "softbus_errcode.h"
27 #include "softbus_server_frame.h"
28 
29 #define NET_WORK_NAME "wlan0"
30 #define TEST_CASE_NUM 10
31 #define MAXT_WAIT_COUNT 6
32 #define WIFI_CONFIG_INTERVAL 10
33 #define TEST_COUNT_INTREVAL 5
34 #define WAIT_SERVER_READY 5
35 #define MAX_TEST_COUNT 20
36 #define NSTACKX_MAX_IP_STRING_LEN 20
37 #define DISC_TEST_PKG_NAME "DISC_TEST"
38 
39 static int32_t g_testSuccessCnt = 0;
40 
GetSubscribeId(void)41 static int GetSubscribeId(void)
42 {
43     static int32_t subscribeId = 0;
44     subscribeId++;
45     return subscribeId;
46 }
47 
GetPublishId(void)48 static int GetPublishId(void)
49 {
50     static int32_t publishId = 0;
51     publishId++;
52     return publishId;
53 }
54 
55 static SubscribeInfo g_sInfo1 = {
56     .subscribeId = 1,
57     .medium = COAP,
58     .mode = DISCOVER_MODE_PASSIVE,
59     .freq = MID,
60     .capability = "ddmpCapability",
61     .capabilityData = (unsigned char *)"capdata3",
62     .dataLen = sizeof("capdata3"),
63     .isSameAccount = true,
64     .isWakeRemote = false
65 };
66 
67 static SubscribeInfo g_sInfo2 = {
68     .subscribeId = 1,
69     .mode = DISCOVER_MODE_PASSIVE,
70     .medium = COAP,
71     .freq = MID,
72     .capability = "hicall",
73     .capabilityData = NULL,
74     .dataLen = 0,
75     .isSameAccount = true,
76     .isWakeRemote = false
77 };
78 
79 static SubscribeInfo g_sInfo3 = {
80     .subscribeId = 1,
81     .mode = DISCOVER_MODE_ACTIVE,
82     .medium = COAP,
83     .freq = MID,
84     .capability = "ddmpCapability",
85     .capabilityData = NULL,
86     .dataLen = 0,
87     .isSameAccount = true,
88     .isWakeRemote = false
89 };
90 
91 static PublishInfo g_pInfo1 = {
92     .publishId = 1,
93     .medium = COAP,
94     .mode = DISCOVER_MODE_PASSIVE,
95     .freq = MID,
96     .capability = "dvKit",
97     .capabilityData = (unsigned char *)"capdata4",
98     .dataLen = sizeof("capdata4")
99 };
100 
101 static PublishInfo g_pInfo2 = {
102     .publishId = 1,
103     .mode = DISCOVER_MODE_ACTIVE,
104     .medium = COAP,
105     .freq = MID,
106     .capability = "dvKit",
107     .capabilityData = NULL,
108     .dataLen = 0
109 };
110 
OnDeviceFound(const DeviceInfo * device)111 static void OnDeviceFound(const DeviceInfo *device)
112 {
113     if (device == NULL) {
114         printf("ondevice found device is null\n");
115         return;
116     }
117     printf("***********OnDeviceFound!!!!!******************************************\n");
118     printf("id : %s.\n", device->devId);
119     printf("name : %s.\n", device->devName);
120     printf("device type : %u.\n", device->devType);
121     printf("capNum : %u.\n", device->capabilityBitmapNum);
122     for (uint32_t i = 0; i < device->capabilityBitmapNum; i++) {
123         printf("capBitmap[%u] : %u.\n", i, device->capabilityBitmap[i]);
124     }
125     printf("addr num : %u.\n", device->addrNum);
126     printf("ip : %s.\n", device->addr[0].info.ip.ip);
127     printf("port : %d.\n", device->addr[0].info.ip.port);
128     printf("connect type : %d.\n", device->addr[0].type);
129     printf("peerUid : %s.\n", device->addr[0].peerUid);
130     printf("hw account hash : %s.\n", device->hwAccountHash);
131     printf("**********************************************************************\n");
132     return;
133 }
134 
TestDeviceFound(const DeviceInfo * device)135 static void TestDeviceFound(const DeviceInfo *device)
136 {
137     printf("[client]TestDeviceFound\n");
138     OnDeviceFound(device);
139 }
140 
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)141 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
142 {
143     printf("[client]TestDiscoverFailed, subscribeId = %d, failReason = %d\n", subscribeId, failReason);
144 }
145 
TestDiscoverySuccess(int subscribeId)146 static void TestDiscoverySuccess(int subscribeId)
147 {
148     printf("[client]TestDiscoverySuccess, subscribeId = %d\n", subscribeId);
149 }
150 
TestPublishSuccess(int publishId)151 static void TestPublishSuccess(int publishId)
152 {
153     printf("[client]TestPublishSuccess, publishId = %d\n", publishId);
154 }
155 
TestPublishFail(int publishId,PublishFailReason reason)156 static void TestPublishFail(int publishId, PublishFailReason reason)
157 {
158     printf("[client]TestPublishFail, publishId = %d, PublishFailReason = %d\n", publishId, reason);
159 }
160 
161 static IDiscoveryCallback g_subscribeCb = {
162     .OnDeviceFound = TestDeviceFound,
163     .OnDiscoverFailed = TestDiscoverFailed,
164     .OnDiscoverySuccess = TestDiscoverySuccess
165 };
166 
167 static IPublishCallback g_publishCb = {
168     .OnPublishSuccess = TestPublishSuccess,
169     .OnPublishFail = TestPublishFail
170 };
171 
GetLocalWifiIp(char * ip,int32_t len)172 static void GetLocalWifiIp(char *ip, int32_t len)
173 {
174     struct netif *wifiNetIf = NULL;
175     ip4_addr_t ipaddr;
176     int32_t ret;
177     int32_t cnt = 0;
178     while (cnt < MAXT_WAIT_COUNT) {
179         cnt++;
180         sleep(WIFI_CONFIG_INTERVAL);
181         wifiNetIf = netifapi_netif_find(NET_WORK_NAME);
182         if (wifiNetIf == NULL) {
183             printf("netif find device failed.\n");
184             continue;
185         }
186         ret = netifapi_netif_get_addr(wifiNetIf, &ipaddr, NULL, NULL);
187         if (ret != 0) {
188             printf("netif get ip addr failed, ret = %d.\n", ret);
189             continue;
190         }
191         if (ipaddr.addr == 0) {
192             printf("wifi is not connected.\n");
193             continue;
194         }
195         break;
196     }
197     inet_ntop(AF_INET, &ipaddr, ip, len);
198     printf("wifi connected, device ip = %s.\n", ip);
199 }
200 
TestPassivePubSrv1(void)201 static void TestPassivePubSrv1(void)
202 {
203     g_pInfo1.publishId = GetPublishId();
204     if (PublishService(DISC_TEST_PKG_NAME, &g_pInfo1, &g_publishCb) != SOFTBUS_OK) {
205         printf("test failed, [%s].\n", __FUNCTION__);
206         return;
207     }
208     g_testSuccessCnt++;
209 }
210 
TestActivePubSrv2(void)211 static void TestActivePubSrv2(void)
212 {
213     g_pInfo2.publishId = GetPublishId();
214     if (PublishService(DISC_TEST_PKG_NAME, &g_pInfo2, &g_publishCb) != SOFTBUS_OK) {
215         printf("test failed, [%s].\n", __FUNCTION__);
216         return;
217     }
218     g_testSuccessCnt++;
219 }
220 
TestUnPubSrv1(void)221 static void TestUnPubSrv1(void)
222 {
223     if (UnPublishService(DISC_TEST_PKG_NAME, g_pInfo1.publishId) != SOFTBUS_OK) {
224         printf("test failed, [%s].\n", __FUNCTION__);
225         return;
226     }
227     g_testSuccessCnt++;
228 }
229 
TestUnPubSrv2(void)230 static void TestUnPubSrv2(void)
231 {
232     if (UnPublishService(DISC_TEST_PKG_NAME, g_pInfo2.publishId) != SOFTBUS_OK) {
233         printf("test failed, [%s].\n", __FUNCTION__);
234         return;
235     }
236     g_testSuccessCnt++;
237 }
238 
TestPassiveStartDisc1(void)239 static void TestPassiveStartDisc1(void)
240 {
241     g_sInfo1.subscribeId = GetSubscribeId();
242     if (StartDiscovery(DISC_TEST_PKG_NAME, &g_sInfo1, &g_subscribeCb) != SOFTBUS_OK) {
243         printf("test failed, [%s].\n", __FUNCTION__);
244         return;
245     }
246     g_testSuccessCnt++;
247 }
248 
TestPassiveStartDisc2(void)249 static void TestPassiveStartDisc2(void)
250 {
251     g_sInfo2.subscribeId = GetSubscribeId();
252     if (StartDiscovery(DISC_TEST_PKG_NAME, &g_sInfo2, &g_subscribeCb) != SOFTBUS_OK) {
253         printf("test failed, [%s].\n", __FUNCTION__);
254         return;
255     }
256     g_testSuccessCnt++;
257 }
258 
TestActiveStartDisc3(void)259 static void TestActiveStartDisc3(void)
260 {
261     g_sInfo3.subscribeId = GetSubscribeId();
262     if (StartDiscovery(DISC_TEST_PKG_NAME, &g_sInfo3, &g_subscribeCb) != SOFTBUS_OK) {
263         printf("test failed, [%s].\n", __FUNCTION__);
264         return;
265     }
266     g_testSuccessCnt++;
267 }
268 
TestStopDisc1(void)269 static void TestStopDisc1(void)
270 {
271     if (StopDiscovery(DISC_TEST_PKG_NAME, g_sInfo1.subscribeId) != SOFTBUS_OK) {
272         printf("test failed, [%s].\n", __FUNCTION__);
273         return;
274     }
275     g_testSuccessCnt++;
276 }
277 
TestStopDisc2(void)278 static void TestStopDisc2(void)
279 {
280     if (StopDiscovery(DISC_TEST_PKG_NAME, g_sInfo2.subscribeId) != SOFTBUS_OK) {
281         printf("test failed, [%s].\n", __FUNCTION__);
282         return;
283     }
284     g_testSuccessCnt++;
285 }
286 
TestStopDisc3(void)287 static void TestStopDisc3(void)
288 {
289     if (StopDiscovery(DISC_TEST_PKG_NAME, g_sInfo3.subscribeId) != SOFTBUS_OK) {
290         printf("test failed, [%s].\n", __FUNCTION__);
291         return;
292     }
293     g_testSuccessCnt++;
294 }
295 
DiscoveryTestEntry(void)296 static void DiscoveryTestEntry(void)
297 {
298     InitSoftBusServer();
299     sleep(WAIT_SERVER_READY);
300 
301     char ip[NSTACKX_MAX_IP_STRING_LEN] = {0};
302     int32_t testCnt = 0;
303     GetLocalWifiIp(ip, NSTACKX_MAX_IP_STRING_LEN);
304     while (testCnt < MAX_TEST_COUNT) {
305         TestPassivePubSrv1();
306         TestActivePubSrv2();
307         TestPassiveStartDisc1();
308         TestPassiveStartDisc2();
309         TestActiveStartDisc3();
310         sleep(TEST_COUNT_INTREVAL);
311         TestUnPubSrv1();
312         TestUnPubSrv2();
313         TestStopDisc1();
314         TestStopDisc2();
315         TestStopDisc3();
316         testCnt++;
317         printf("*****test index: %d / %d *******\n", testCnt, MAX_TEST_COUNT);
318         printf("*****success: %d / %d *****\n", g_testSuccessCnt, TEST_CASE_NUM * MAX_TEST_COUNT);
319     }
320 }
321 
322 SYS_SERVICE_INIT_PRI(DiscoveryTestEntry, 4); // 4 : priority
323