Main Page | Directories | File List | Globals | Examples

list_devices.c

This is an example of how to query the BlueSense network for connected devices. Tries to detect a certain number of devices
Parameters:
masterInstancePtr pointer to a master instance
findGoal the number of new devices to detect
timeoutUs detection process takes up to timeoutUs microseconds
Returns:
the total number of NEW devices detected
#include <bluesense.h>

#include <stdio.h>

int main(void) {
        // allocate memory for an interface table 
        bsDeviceInterface_registeredDeviceInterfaces_t registered = bsDeviceInterface_allocInterfaceTable();
        
        // allocate memory for a device table 
        bsDeviceTable_table_t devices = bsDeviceTable_allocDeviceTable();
        
        // open a master device
        bsMaster_device_t master = bsMaster_allocMaster(registered,devices,0);
        //bsMaster_addIPAddress(master, "1558", "admin", "admin");

        // detect devices until no more new devices are found
        // the detectDevices function tries to find 10 devices in a period of 5 seconds
        // it might take 10 seconds because it will get called at least twice
         
#if 1
        // connect to the ethernet router
        bsMaster_addIPAddress(master, "1623", "admin", "admin");
#endif

        printf("Detecting devices for the next 2+ seconds...\n"); 
        fflush(stdout);

        uint16_t newFound = 1;
        while(newFound) {
                // try to detect ten devices in 2 seconds
                newFound = bsMaster_detectDevices(master,10,2*1000*1000);
        }

        // check if there are connection problems
        uint8_t msgCount = bsMaster_getMessageCount();
        if (msgCount!=0) {
                // print error messages
                printf("Connection messages: \n");
                uint8_t index;
                for (index=0; index<msgCount; index++) {
                        printf("%s \n",bsMaster_getMessage(index));
                }
                bsMaster_clearMessages();
        }
        
        printf("Found %d device(s) \n",bsDeviceTable_getListedDeviceCount(devices));

        // iterate over the found devices and show their identifier and type
        bsDeviceTable_deviceInfo_t dev = bsDeviceTable_getFirstDevice(devices);
        for(; dev; dev = bsDeviceTable_getNextDevice(dev)) {
                dex_devtype_t tp = bsDeviceTable_getDeviceType(dev);
                identifier_t id = bsDeviceTable_getDeviceId(dev);

                printf("Device id = %d, type = %d, typename = %s firmwareVersion = %d\n", 
                        id, 
                        tp, 
                        bsDeviceInterface_getDeviceTypeName(registered,tp),
                        bsDeviceTable_getDeviceFirmwareVersion(dev));
        }

        // clean up previously allocated memory
        bsMaster_freeMaster(master);
        bsDeviceTable_freeDeviceTable(devices);
        bsDeviceInterface_freeInterfaceTable(registered);

        
        printf("Press Enter to finish\n"); 
        fflush(stdout);
        getchar();
        return(0);
}       


Generated on Mon Jan 21 17:51:55 2008 for BlueSense by  doxygen 1.4.4