FuJuntang
2022-07-06 e795b6226a92961ae10d5ef497d2f78fe88fb918
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "probe/probe.h"
#include "proto_comm.h"
#include "proto_dbg.h"
 
/*probe the broadcastted devices*/
int DiscoverDevice(void (*cb)(char *DeviceXAddr))
{
    int i;
    int result = 0;
    unsigned int count = 0;
    struct soap *soap = NULL;
    struct wsdd__ProbeType      req;
    struct __wsdd__ProbeMatches rep;
    struct wsdd__ProbeMatchType *probeMatch;
 
    SOAP_ASSERT(NULL != (soap = proto_soap_new(SOAP_SOCK_TIMEOUT)));
 
    /*Set the packet header descriptor*/
    proto_init_header(soap);
    
    /*Set the probe devices type*/
    proto_init_ProbeType(soap, &req);
 
    /*deliver the broadcast probe message*/
    result = soap_send___wsdd__Probe(soap, SOAP_MCAST_ADDR, NULL, &req);
    while (SOAP_OK == result)
    {
        memset(&rep, 0x00, sizeof(rep));
        result = soap_recv___wsdd__ProbeMatches(soap, &rep);
        if (SOAP_OK == result) {
            if (soap->error) {
                soap_perror(soap, "ProbeMatches");
            } else {
                /*Get the probing devices response*/
                dump__wsdd__ProbeMatches(&rep);
 
                if (NULL != rep.wsdd__ProbeMatches) {
                    count += rep.wsdd__ProbeMatches->__sizeProbeMatch;
                    for(i = 0; i < rep.wsdd__ProbeMatches->__sizeProbeMatch; i++) {
                        probeMatch = rep.wsdd__ProbeMatches->ProbeMatch + i;
                        if (NULL != cb) {
                            cb(probeMatch->XAddrs);
                        }
                    }
                }
            }
        } else if (soap->error) {
            break;
        }
    }
 
    SOAP_DBGERR("\ndetect end! It has detected %d devices!\n", count);
 
    if (NULL != soap) {
        proto_soap_delete(soap);
    }
 
    return count;
}