• 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 
20 #include "disc_coap.h"
21 #include "securec.h"
22 
23 #define DISC_TEST_ERR (-1)
24 #define DISC_TEST_OK 0
25 #define DISC_COAP_CAP_2 2
26 #define DISC_COAP_CAP_4 4
27 #define DISC_COAP_CAP_15 15
28 #define DISC_MAX_MS_NUM 5000
29 #define US_PER_MS 1000
30 #define US_PER_SECOND 1000000
31 
32 static DiscoveryFuncInterface *g_coapDiscFunc = NULL;
33 static struct timeval g_startTime;
34 static struct timeval g_endTime;
35 static int32_t g_timeDelayFlag = 0;
36 
OnDeviceFound(const DeviceInfo * device)37 static void OnDeviceFound(const DeviceInfo *device)
38 {
39     gettimeofday(&g_endTime, NULL);
40     g_timeDelayFlag = 1;
41     printf("***********OnDeviceFound!!!!!******************************************\n");
42     printf("id : %s.\n", device->devId);
43     printf("name : %s.\n", device->devName);
44     printf("device type : %u.\n", device->devType);
45     printf("capNum : %u.\n", device->capabilityBitmapNum);
46     for (uint32_t i = 0; i < device->capabilityBitmapNum; i++) {
47         printf("capBitmap[%u] : %u.\n", i, device->capabilityBitmap[i]);
48     }
49     printf("addr num : %u.\n", device->addrNum);
50     printf("ip : %s.\n", device->addr[0].addr);
51     printf("port : %d.\n", device->addr[0].port);
52     printf("connect type : %d.\n", device->addr[0].type);
53     printf("hw account hash : %s.\n", device->hwAccountHash);
54     printf("**********************************************************************\n");
55     return;
56 }
57 
58 static DiscInnerCallback g_discInnerCb = {
59     .OnDeviceFound = OnDeviceFound
60 };
61 
62 static PublishOption g_publishOption = {
63     .freq = 0,
64     .capabilityBitmap = {1},
65     .capabilityData = NULL,
66     .dataLen = 0
67 };
68 
69 static SubscribeOption g_subscribeOption = {
70     .freq = 1,
71     .isSameAccount = true,
72     .isWakeRemote = false,
73     .capabilityBitmap = {2},
74     .capabilityData = NULL,
75     .dataLen = 0
76 };
77 
SampleHelp()78 static void SampleHelp()
79 {
80     printf("select the behavior of the test sample.\n");
81     printf("1: init coap discovery.\n");
82     printf("2: publish service(customable).\n");
83     printf("3: unpublish service(customable).\n");
84     printf("4: start discovery(customable).\n");
85     printf("5: stop discovery(customable).\n");
86     printf("6: publish service.\n");
87     printf("7: unpublish service.\n");
88     printf("8: start discovery.\n");
89     printf("9: stop discovery.\n");
90     printf("t: test discovery time delay.\n");
91     printf("q: quit the test sample.\n");
92     return;
93 }
94 
DiscCoapTestInit(void)95 static int32_t DiscCoapTestInit(void)
96 {
97     g_coapDiscFunc = DiscCoapInit(&g_discInnerCb);
98     if (g_coapDiscFunc == NULL) {
99         printf("init coap discovery failed.\n");
100         return DISC_TEST_ERR;
101     }
102     return DISC_TEST_OK;
103 }
104 
DiscCoapTestDeinit(void)105 static void DiscCoapTestDeinit(void)
106 {
107     if (g_coapDiscFunc == NULL) {
108         return;
109     }
110 
111     DiscCoapDeinit();
112     g_coapDiscFunc = NULL;
113     return;
114 }
115 
DiscCoapPulbishService(uint32_t pubCapBitmap,uint32_t publishMode)116 static int32_t DiscCoapPulbishService(uint32_t pubCapBitmap, uint32_t publishMode)
117 {
118     if (g_coapDiscFunc == NULL) {
119         printf("g_coapDiscFunc is NULL.\n");
120         return DISC_TEST_ERR;
121     }
122 
123     g_publishOption.capabilityBitmap[0] = pubCapBitmap;
124     switch (publishMode) {
125         case 0:
126             if (g_coapDiscFunc->StartScan(&g_publishOption) != 0) {
127                 printf("passive publish failed.\n");
128                 return DISC_TEST_ERR;
129             }
130             break;
131         case 1:
132             if (g_coapDiscFunc->Publish(&g_publishOption) != 0) {
133                 printf("active publish failed.\n");
134                 return DISC_TEST_ERR;
135             }
136             break;
137         default:
138             printf("unsupport mode.\n");
139             return DISC_TEST_ERR;
140     }
141     return DISC_TEST_OK;
142 }
143 
DiscCoapUnpulbishService(uint32_t pubCapBitmap,uint32_t publishMode)144 static int32_t DiscCoapUnpulbishService(uint32_t pubCapBitmap, uint32_t publishMode)
145 {
146     if (g_coapDiscFunc == NULL) {
147         printf("g_coapDiscFunc is NULL.\n");
148         return DISC_TEST_ERR;
149     }
150 
151     g_publishOption.capabilityBitmap[0] = pubCapBitmap;
152     switch (publishMode) {
153         case 0:
154             if (g_coapDiscFunc->StopScan(&g_publishOption) != 0) {
155                 printf("passive unpublish failed.\n");
156                 return DISC_TEST_ERR;
157             }
158             break;
159         case 1:
160             if (g_coapDiscFunc->Unpublish(&g_publishOption) != 0) {
161                 printf("active unpublish failed.\n");
162                 return DISC_TEST_ERR;
163             }
164             break;
165         default:
166             printf("unsupport mode.\n");
167             return DISC_TEST_ERR;
168     }
169     return DISC_TEST_OK;
170 }
171 
DiscCoapStartDiscovery(uint32_t filterCapBitmap,uint32_t discMode)172 static int32_t DiscCoapStartDiscovery(uint32_t filterCapBitmap, uint32_t discMode)
173 {
174     if (g_coapDiscFunc == NULL) {
175         printf("g_coapDiscFunc is NULL.\n");
176         return DISC_TEST_ERR;
177     }
178 
179     g_subscribeOption.capabilityBitmap[0] = filterCapBitmap;
180     switch (discMode) {
181         case 0:
182             if (g_coapDiscFunc->Subscribe(&g_subscribeOption) != 0) {
183                 printf("passivce start discvoery failed.\n");
184                 return DISC_TEST_ERR;
185             }
186             break;
187         case 1:
188             if (g_coapDiscFunc->StartAdvertise(&g_subscribeOption) != 0) {
189                 printf("active start discvoery failed.\n");
190                 return DISC_TEST_ERR;
191             }
192             break;
193         default:
194             printf("unsupport mode.\n");
195             return DISC_TEST_ERR;
196     }
197     return DISC_TEST_OK;
198 }
199 
DiscCoapStopDiscovery(uint32_t filterCapBitmap,uint32_t discMode)200 static int32_t DiscCoapStopDiscovery(uint32_t filterCapBitmap, uint32_t discMode)
201 {
202     if (g_coapDiscFunc == NULL) {
203         printf("g_coapDiscFunc is NULL.\n");
204         return DISC_TEST_ERR;
205     }
206 
207     g_subscribeOption.capabilityBitmap[0] = filterCapBitmap;
208     switch (discMode) {
209         case 0:
210             if (g_coapDiscFunc->Unsubscribe(&g_subscribeOption) != 0) {
211                 printf("passivce stop discvoery failed.\n");
212                 return DISC_TEST_ERR;
213             }
214             break;
215         case 1:
216             if (g_coapDiscFunc->StopAdvertise(&g_subscribeOption) != 0) {
217                 printf("active stop discvoery failed.\n");
218                 return DISC_TEST_ERR;
219             }
220             break;
221         default:
222             printf("unsupport mode.\n");
223             return DISC_TEST_ERR;
224     }
225     return DISC_TEST_OK;
226 }
227 
DiscCoapTestPulbishService()228 static int32_t DiscCoapTestPulbishService()
229 {
230     if (g_coapDiscFunc == NULL) {
231         printf("g_coapDiscFunc is NULL.\n");
232         return DISC_TEST_ERR;
233     }
234 
235     printf("input the register capbility and publish mode(0-passive, 1-active).format[capability mode]: ");
236     uint32_t pubCapBitmap;
237     uint32_t publishMode;
238     int32_t ret = scanf_s("%u %u", &pubCapBitmap, &publishMode);
239     /* 2: read num */
240     if (ret != 2) {
241         printf("scanf_s failed.");
242         return DISC_TEST_ERR;
243     }
244     getchar();
245     if (DiscCoapPulbishService(pubCapBitmap, publishMode) != DISC_TEST_OK) {
246         return DISC_TEST_ERR;
247     }
248     return DISC_TEST_OK;
249 }
250 
DiscCoapTestUnpulbishService()251 static int32_t DiscCoapTestUnpulbishService()
252 {
253     if (g_coapDiscFunc == NULL) {
254         printf("g_coapDiscFunc is NULL.\n");
255         return DISC_TEST_ERR;
256     }
257 
258     printf("input the unregister capbility and publish mode(0-passive, 1-active).format[capability mode]: ");
259     uint32_t pubCapBitmap;
260     uint32_t publishMode;
261     int32_t ret = scanf_s("%u %u", &pubCapBitmap, &publishMode);
262     /* 2: read num */
263     if (ret != 2) {
264         printf("scanf_s failed.");
265         return DISC_TEST_ERR;
266     }
267     getchar();
268     if (DiscCoapUnpulbishService(pubCapBitmap, publishMode) != DISC_TEST_OK) {
269         return DISC_TEST_ERR;
270     }
271     return DISC_TEST_OK;
272 }
273 
DiscCoapTestStartDiscovery()274 static int32_t DiscCoapTestStartDiscovery()
275 {
276     if (g_coapDiscFunc == NULL) {
277         printf("g_coapDiscFunc is NULL.\n");
278         return DISC_TEST_ERR;
279     }
280 
281     printf("input the register filter capbility and discovery mode(0-passive, 1-active).format[capability mode]: ");
282     uint32_t filterCapBitmap;
283     uint32_t discMode;
284     int32_t ret = scanf_s("%u %u", &filterCapBitmap, &discMode);
285     /* 2: read num */
286     if (ret != 2) {
287         printf("scanf_s failed.");
288         return DISC_TEST_ERR;
289     }
290     getchar();
291     if (DiscCoapStartDiscovery(filterCapBitmap, discMode) != DISC_TEST_OK) {
292         return DISC_TEST_ERR;
293     }
294     return DISC_TEST_OK;
295 }
296 
DiscCoapTestStopDiscovery()297 static int32_t DiscCoapTestStopDiscovery()
298 {
299     if (g_coapDiscFunc == NULL) {
300         printf("g_coapDiscFunc is NULL.\n");
301         return DISC_TEST_ERR;
302     }
303 
304     printf("input the unregister filter capbility and discovery mode(0-passive, 1-active).format[capability mode]: ");
305     uint32_t filterCapBitmap;
306     uint32_t discMode;
307     int32_t ret = scanf_s("%u %u", &filterCapBitmap, &discMode);
308     /* 2: read num */
309     if (ret != 2) {
310         printf("scanf_s failed.");
311         return DISC_TEST_ERR;
312     }
313     getchar();
314     if (DiscCoapStopDiscovery(filterCapBitmap, discMode) != DISC_TEST_OK) {
315         return DISC_TEST_ERR;
316     }
317     return DISC_TEST_OK;
318 }
319 
CoapPulblish001(void)320 static int32_t CoapPulblish001(void)
321 {
322     DiscCoapTestDeinit();
323     if (DiscCoapTestInit() != DISC_TEST_OK) {
324         return DISC_TEST_ERR;
325     }
326 
327     if (DiscCoapPulbishService(1, 1) != DISC_TEST_OK) {
328         return DISC_TEST_ERR;
329     }
330     return DISC_TEST_OK;
331 }
332 
CoapPulblish002(void)333 static int32_t CoapPulblish002(void)
334 {
335     DiscCoapTestDeinit();
336     if (DiscCoapTestInit() != DISC_TEST_OK) {
337         return DISC_TEST_ERR;
338     }
339     if (DiscCoapPulbishService(1, 1) != DISC_TEST_OK) {
340         return DISC_TEST_ERR;
341     }
342     if (DiscCoapPulbishService(DISC_COAP_CAP_2, 1) != DISC_TEST_OK) {
343         return DISC_TEST_ERR;
344     }
345     return DISC_TEST_OK;
346 }
347 
CoapStartScan001(void)348 static int32_t CoapStartScan001(void)
349 {
350     DiscCoapTestDeinit();
351     if (DiscCoapTestInit() != DISC_TEST_OK) {
352         return DISC_TEST_ERR;
353     }
354 
355     if (DiscCoapPulbishService(1, 0) != DISC_TEST_OK) {
356         return DISC_TEST_ERR;
357     }
358     if (DiscCoapStartDiscovery(DISC_COAP_CAP_15, 1) != DISC_TEST_OK) {
359         return DISC_TEST_ERR;
360     }
361     return DISC_TEST_OK;
362 }
363 
CoapStartScan002(void)364 static int32_t CoapStartScan002(void)
365 {
366     DiscCoapTestDeinit();
367     if (DiscCoapTestInit() != DISC_TEST_OK) {
368         return DISC_TEST_ERR;
369     }
370 
371     if (DiscCoapPulbishService(1, 0) != DISC_TEST_OK) {
372         return DISC_TEST_ERR;
373     }
374     if (DiscCoapPulbishService(DISC_COAP_CAP_4, 0) != DISC_TEST_OK) {
375         return DISC_TEST_ERR;
376     }
377     if (DiscCoapStartDiscovery(DISC_COAP_CAP_15, 1) != DISC_TEST_OK) {
378         return DISC_TEST_ERR;
379     }
380     return DISC_TEST_OK;
381 }
382 
CoapUnpulblish001(void)383 static int32_t CoapUnpulblish001(void)
384 {
385     DiscCoapTestDeinit();
386     if (DiscCoapTestInit() != DISC_TEST_OK) {
387         return DISC_TEST_ERR;
388     }
389 
390     if (DiscCoapPulbishService(1, 1) != DISC_TEST_OK) {
391         return DISC_TEST_ERR;
392     }
393     if (DiscCoapUnpulbishService(1, 1) != DISC_TEST_OK) {
394         return DISC_TEST_ERR;
395     }
396     if (DiscCoapStartDiscovery(DISC_COAP_CAP_15, 1) != DISC_TEST_OK) {
397         return DISC_TEST_ERR;
398     }
399     return DISC_TEST_OK;
400 }
401 
CoapUnpulblish002(void)402 static int32_t CoapUnpulblish002(void)
403 {
404     DiscCoapTestDeinit();
405     if (DiscCoapTestInit() != DISC_TEST_OK) {
406         return DISC_TEST_ERR;
407     }
408 
409     if (DiscCoapPulbishService(1, 1) != DISC_TEST_OK) {
410         return DISC_TEST_ERR;
411     }
412     if (DiscCoapPulbishService(1, 1) != DISC_TEST_OK) {
413         return DISC_TEST_ERR;
414     }
415     if (DiscCoapUnpulbishService(1, 1) != DISC_TEST_OK) {
416         return DISC_TEST_ERR;
417     }
418     if (DiscCoapStartDiscovery(DISC_COAP_CAP_15, 1) != DISC_TEST_OK) {
419         return DISC_TEST_ERR;
420     }
421     return DISC_TEST_OK;
422 }
423 
CoapStopScan001(void)424 static int32_t CoapStopScan001(void)
425 {
426     DiscCoapTestDeinit();
427     if (DiscCoapTestInit() != DISC_TEST_OK) {
428         return DISC_TEST_ERR;
429     }
430 
431     if (DiscCoapPulbishService(1, 0) != DISC_TEST_OK) {
432         return DISC_TEST_ERR;
433     }
434     if (DiscCoapUnpulbishService(1, 0) != DISC_TEST_OK) {
435         return DISC_TEST_ERR;
436     }
437     if (DiscCoapStartDiscovery(DISC_COAP_CAP_15, 1) != DISC_TEST_OK) {
438         return DISC_TEST_ERR;
439     }
440     return DISC_TEST_OK;
441 }
442 
CoapStopScan002(void)443 static int32_t CoapStopScan002(void)
444 {
445     DiscCoapTestDeinit();
446     if (DiscCoapTestInit() != DISC_TEST_OK) {
447         return DISC_TEST_ERR;
448     }
449 
450     if (DiscCoapPulbishService(1, 0) != DISC_TEST_OK) {
451         return DISC_TEST_ERR;
452     }
453     if (DiscCoapPulbishService(1, 0) != DISC_TEST_OK) {
454         return DISC_TEST_ERR;
455     }
456     if (DiscCoapUnpulbishService(1, 0) != DISC_TEST_OK) {
457         return DISC_TEST_ERR;
458     }
459     if (DiscCoapStartDiscovery(DISC_COAP_CAP_15, 1) != DISC_TEST_OK) {
460         return DISC_TEST_ERR;
461     }
462     return DISC_TEST_OK;
463 }
464 
CoapStartAdvertise001(void)465 static int32_t CoapStartAdvertise001(void)
466 {
467     DiscCoapTestDeinit();
468     if (DiscCoapTestInit() != DISC_TEST_OK) {
469         return DISC_TEST_ERR;
470     }
471 
472     if (DiscCoapStartDiscovery(1, 1) != DISC_TEST_OK) {
473         return DISC_TEST_ERR;
474     }
475     return DISC_TEST_OK;
476 }
477 
CoapStartAdvertise002(void)478 static int32_t CoapStartAdvertise002(void)
479 {
480     DiscCoapTestDeinit();
481     if (DiscCoapTestInit() != DISC_TEST_OK) {
482         return DISC_TEST_ERR;
483     }
484 
485     if (DiscCoapStartDiscovery(DISC_COAP_CAP_4, 1) != DISC_TEST_OK) {
486         return DISC_TEST_ERR;
487     }
488     return DISC_TEST_OK;
489 }
490 
CoapStopAdvertise001(void)491 static int32_t CoapStopAdvertise001(void)
492 {
493     DiscCoapTestDeinit();
494     if (DiscCoapTestInit() != DISC_TEST_OK) {
495         return DISC_TEST_ERR;
496     }
497 
498     if (DiscCoapStartDiscovery(1, 1) != DISC_TEST_OK) {
499         return DISC_TEST_ERR;
500     }
501     if (DiscCoapStopDiscovery(1, 1) != DISC_TEST_OK) {
502         return DISC_TEST_ERR;
503     }
504     return DISC_TEST_OK;
505 }
506 
CoapStopAdvertise002(void)507 static int32_t CoapStopAdvertise002(void)
508 {
509     DiscCoapTestDeinit();
510     if (DiscCoapTestInit() != DISC_TEST_OK) {
511         return DISC_TEST_ERR;
512     }
513 
514     if (DiscCoapStartDiscovery(1, 1) != DISC_TEST_OK) {
515         return DISC_TEST_ERR;
516     }
517     if (DiscCoapStartDiscovery(1, 1) != DISC_TEST_OK) {
518         return DISC_TEST_ERR;
519     }
520     if (DiscCoapStopDiscovery(1, 1) != DISC_TEST_OK) {
521         return DISC_TEST_ERR;
522     }
523     return DISC_TEST_OK;
524 }
525 
CoapSubscribe001(void)526 static int32_t CoapSubscribe001(void)
527 {
528     DiscCoapTestDeinit();
529     if (DiscCoapTestInit() != DISC_TEST_OK) {
530         return DISC_TEST_ERR;
531     }
532 
533     if (DiscCoapStartDiscovery(1, 0) != DISC_TEST_OK) {
534         return DISC_TEST_ERR;
535     }
536     return DISC_TEST_OK;
537 }
538 
CoapSubscribe002(void)539 static int32_t CoapSubscribe002(void)
540 {
541     DiscCoapTestDeinit();
542     if (DiscCoapTestInit() != DISC_TEST_OK) {
543         return DISC_TEST_ERR;
544     }
545 
546     if (DiscCoapStartDiscovery(DISC_COAP_CAP_4, 0) != DISC_TEST_OK) {
547         return DISC_TEST_ERR;
548     }
549     return DISC_TEST_OK;
550 }
551 
CoapUnsubscribe001(void)552 static int32_t CoapUnsubscribe001(void)
553 {
554     DiscCoapTestDeinit();
555     if (DiscCoapTestInit() != DISC_TEST_OK) {
556         return DISC_TEST_ERR;
557     }
558 
559     if (DiscCoapStartDiscovery(1, 0) != DISC_TEST_OK) {
560         return DISC_TEST_ERR;
561     }
562     if (DiscCoapStopDiscovery(1, 0) != DISC_TEST_OK) {
563         return DISC_TEST_ERR;
564     }
565     return DISC_TEST_OK;
566 }
567 
CoapUnsubscribe002(void)568 static int32_t CoapUnsubscribe002(void)
569 {
570     DiscCoapTestDeinit();
571     if (DiscCoapTestInit() != DISC_TEST_OK) {
572         return DISC_TEST_ERR;
573     }
574 
575     if (DiscCoapStartDiscovery(1, 0) != DISC_TEST_OK) {
576         return DISC_TEST_ERR;
577     }
578     if (DiscCoapStartDiscovery(1, 0) != DISC_TEST_OK) {
579         return DISC_TEST_ERR;
580     }
581     if (DiscCoapStopDiscovery(1, 0) != DISC_TEST_OK) {
582         return DISC_TEST_ERR;
583     }
584     return DISC_TEST_OK;
585 }
586 
DiscCoapTestPulbishServiceCase()587 static int32_t DiscCoapTestPulbishServiceCase()
588 {
589     printf("input the testcase num=");
590     char input = getchar();
591     getchar();
592     switch (input) {
593         case '1':
594             if (CoapPulblish001() != DISC_TEST_OK) {
595                 return DISC_TEST_ERR;
596             }
597             break;
598         case '2':
599             if (CoapPulblish002() != DISC_TEST_OK) {
600                 return DISC_TEST_ERR;
601             }
602             break;
603         case '3':
604             if (CoapStartScan001() != DISC_TEST_OK) {
605                 return DISC_TEST_ERR;
606             }
607             break;
608         case '4':
609             if (CoapStartScan002() != DISC_TEST_OK) {
610                 return DISC_TEST_ERR;
611             }
612             break;
613         default:
614             printf("invalid test case num.");
615             break;
616     }
617     return DISC_TEST_OK;
618 }
619 
DiscCoapTestUnpulbishServiceCase()620 static int32_t DiscCoapTestUnpulbishServiceCase()
621 {
622     printf("input the testcase num=");
623     char input = getchar();
624     getchar();
625     switch (input) {
626         case '1':
627             if (CoapUnpulblish001() != DISC_TEST_OK) {
628                 return DISC_TEST_ERR;
629             }
630             break;
631         case '2':
632             if (CoapUnpulblish002() != DISC_TEST_OK) {
633                 return DISC_TEST_ERR;
634             }
635             break;
636         case '3':
637             if (CoapStopScan001() != DISC_TEST_OK) {
638                 return DISC_TEST_ERR;
639             }
640             break;
641         case '4':
642             if (CoapStopScan002() != DISC_TEST_OK) {
643                 return DISC_TEST_ERR;
644             }
645             break;
646         default:
647             printf("invalid test case num.");
648             break;
649     }
650     return DISC_TEST_OK;
651 }
652 
DiscCoapTestStartDiscoveryCase()653 static int32_t DiscCoapTestStartDiscoveryCase()
654 {
655     printf("input the testcase num=");
656     char input = getchar();
657     getchar();
658     switch (input) {
659         case '1':
660             if (CoapStartAdvertise001() != DISC_TEST_OK) {
661                 return DISC_TEST_ERR;
662             }
663             break;
664         case '2':
665             if (CoapStartAdvertise002() != DISC_TEST_OK) {
666                 return DISC_TEST_ERR;
667             }
668             break;
669         case '3':
670             if (CoapSubscribe001() != DISC_TEST_OK) {
671                 return DISC_TEST_ERR;
672             }
673             break;
674         case '4':
675             if (CoapSubscribe002() != DISC_TEST_OK) {
676                 return DISC_TEST_ERR;
677             }
678             break;
679         default:
680             printf("invalid test case num.");
681             break;
682     }
683     return DISC_TEST_OK;
684 }
685 
DiscCoapTestStopDiscoveryCase()686 static int32_t DiscCoapTestStopDiscoveryCase()
687 {
688     printf("input the testcase num=");
689     char input = getchar();
690     getchar();
691     switch (input) {
692         case '1':
693             if (CoapStopAdvertise001() != DISC_TEST_OK) {
694                 return DISC_TEST_ERR;
695             }
696             break;
697         case '2':
698             if (CoapStopAdvertise002() != DISC_TEST_OK) {
699                 return DISC_TEST_ERR;
700             }
701             break;
702         case '3':
703             if (CoapUnsubscribe001() != DISC_TEST_OK) {
704                 return DISC_TEST_ERR;
705             }
706             break;
707         case '4':
708             if (CoapUnsubscribe002() != DISC_TEST_OK) {
709                 return DISC_TEST_ERR;
710             }
711             break;
712         default:
713             printf("invalid test case num.");
714             break;
715     }
716     return DISC_TEST_OK;
717 }
718 
DiscCoapTestTimeDelay(void)719 static int32_t DiscCoapTestTimeDelay(void)
720 {
721     DiscCoapTestDeinit();
722     if (DiscCoapTestInit() != DISC_TEST_OK) {
723         return DISC_TEST_ERR;
724     }
725 
726     gettimeofday(&g_startTime, NULL);
727     if (DiscCoapStartDiscovery(1, 1) != DISC_TEST_OK) {
728         return DISC_TEST_ERR;
729     }
730     uint32_t i = 0;
731     g_timeDelayFlag = 0;
732     while ((i < DISC_MAX_MS_NUM) && (g_timeDelayFlag == 0)) {
733         i++;
734         usleep(US_PER_MS);
735     }
736     if (g_timeDelayFlag == 0) {
737         printf("test discovery time delay failed(reach max wait time 5s).\n");
738         return DISC_TEST_ERR;
739     }
740     uint64_t delayTime = US_PER_SECOND * (g_endTime.tv_sec - g_startTime.tv_sec) +
741         (g_endTime.tv_usec - g_startTime.tv_usec);
742     printf("discovery delay time(ms) = %" PRIu64 ".\n", delayTime / US_PER_MS);
743     return DISC_TEST_OK;
744 }
745 
main()746 int main()
747 {
748     printf("**************start coap discovery test sample!!!!!!***************\n");
749     SampleHelp();
750     while (1) {
751         sleep(1);
752         printf("input=");
753         char input = getchar();
754         getchar();
755         switch (input) {
756             case '1': {
757                 if (DiscCoapTestInit() != DISC_TEST_OK) {
758                     goto EXIT;
759                 }
760                 break;
761             }
762             case '2': {
763                 if (DiscCoapTestPulbishService() != DISC_TEST_OK) {
764                     goto EXIT;
765                 }
766                 break;
767             }
768             case '3': {
769                 if (DiscCoapTestUnpulbishService() != DISC_TEST_OK) {
770                     goto EXIT;
771                 }
772                 break;
773             }
774             case '4': {
775                 if (DiscCoapTestStartDiscovery() != DISC_TEST_OK) {
776                     goto EXIT;
777                 }
778                 break;
779             }
780             case '5': {
781                 if (DiscCoapTestStopDiscovery() != DISC_TEST_OK) {
782                     goto EXIT;
783                 }
784                 break;
785             }
786             case '6': {
787                 if (DiscCoapTestPulbishServiceCase() != DISC_TEST_OK) {
788                     goto EXIT;
789                 }
790                 break;
791             }
792             case '7': {
793                 if (DiscCoapTestUnpulbishServiceCase() != DISC_TEST_OK) {
794                     goto EXIT;
795                 }
796                 break;
797             }
798             case '8': {
799                 if (DiscCoapTestStartDiscoveryCase() != DISC_TEST_OK) {
800                     goto EXIT;
801                 }
802                 break;
803             }
804             case '9': {
805                 if (DiscCoapTestStopDiscoveryCase() != DISC_TEST_OK) {
806                     goto EXIT;
807                 }
808                 break;
809             }
810             case 't': {
811                 if (DiscCoapTestTimeDelay() != DISC_TEST_OK) {
812                     goto EXIT;
813                 }
814                 break;
815             }
816             case 'q':
817                 goto EXIT;
818             default:
819                 SampleHelp();
820                 break;
821         }
822     }
823 
824 EXIT:
825     printf("*************quit the coap discovery test sample!!!!!************\n");
826     DiscCoapTestDeinit();
827     return 0;
828 }
829