Exemplo n.º 1
0
    def __init__(self, device=None, datasource=None, gps=None):
        '''
        Instantiates the KillerBee class.

        @type device:   String
        @param device:  Device identifier, either USB vendor:product, serial device node, or IP address
        @type datasource: String
        @param datasource: A known datasource type that is used
        by dblog to record how the data was captured.
        @type gps: String
        @param gps: Optional serial device identifier for an attached GPS
            unit. If provided, or if global variable has previously been set, 
            KillerBee skips that device in initalization process.
        @return: None
        @rtype: None
        '''

        global gps_devstring
        if gps_devstring is None and gps is not None:
            gps_devstring = gps

        self.dev = None
        self.__bus = None
        self.driver = None

        # IP devices may be the most straightforward, and we aren't doing
        # discovery, just connecting to defined addresses, so we'll check
        # first to see if we have an IP address given as our device parameter.
        if (device is not None) and kbutils.isIpAddr(device):
            from dev_sewio import isSewio
            if isSewio(device):
                from dev_sewio import SEWIO
                self.driver = SEWIO(dev=device)  #give it the ip address
            else:
                del isSewio

        # Figure out a device is one is not set, trying USB devices next
        if self.driver is None:
            if device is None:
                result = kbutils.search_usb(None)
                if result != None:
                    if USBVER == 0:
                        (self.__bus, self.dev) = result
                    elif USBVER == 1:
                        #TODO remove self.__bus attribute, not needed in 1.x as all info in self.dev
                        self.dev = result
            # Recognize if device is provided in the USB format (like a 012:456 string):
            elif ":" in device:
                result = kbutils.search_usb(device)
                if result == None:
                    raise KBInterfaceError(
                        "Did not find a USB device matching %s." % device)
                else:
                    if USBVER == 0:
                        (self.__bus, self.dev) = result
                    elif USBVER == 1:
                        #TODO remove self.__bus attribute, not needed in 1.x as all info in self.dev
                        self.dev = result

            if self.dev is not None:
                if self.__device_is(RZ_USB_VEND_ID, RZ_USB_PROD_ID):
                    from dev_rzusbstick import RZUSBSTICK
                    self.driver = RZUSBSTICK(self.dev, self.__bus)
                elif self.__device_is(ZN_USB_VEND_ID, ZN_USB_PROD_ID):
                    raise KBInterfaceError(
                        "Zena firmware not yet implemented.")
                else:
                    raise KBInterfaceError(
                        "KillerBee doesn't know how to interact with USB device vendor=%04x, product=%04x."
                        .format(self.dev.idVendor, self.dev.idProduct))

        # Figure out a device from serial if one is not set
        #TODO be able to try more than one serial device here (merge with devlist code somehow)
#        if device == None:
#            seriallist = get_serial_ports()
#            if len(seriallist) > 0:
#                device = seriallist[0]

# If a USB device driver was not loaded, now we try serial devices
        if self.driver is None:
            # If no device was specified
            if device is None:
                glob_list = get_serial_ports()
                if len(glob_list) > 0:
                    #TODO be able to check other devices if this one is not correct
                    device = glob_list[0]
            # Recognize if device specified by serial string:
            if (device is not None) and kbutils.isSerialDeviceString(device):
                self.dev = device
                if (self.dev == gps_devstring):
                    pass
                elif (DEV_ENABLE_ZIGDUINO and kbutils.iszigduino(self.dev)):
                    from dev_zigduino import ZIGDUINO
                    self.driver = ZIGDUINO(self.dev)
                elif (DEV_ENABLE_FREAKDUINO
                      and kbutils.isfreakduino(self.dev)):
                    from dev_freakduino import FREAKDUINO
                    self.driver = FREAKDUINO(self.dev)
                else:
                    gfccspi, subtype = isgoodfetccspi(self.dev)
                    if gfccspi and subtype == 0:
                        from dev_telosb import TELOSB
                        self.driver = TELOSB(self.dev)
                    elif gfccspi and subtype == 1:
                        from dev_apimote import APIMOTE
                        self.driver = APIMOTE(self.dev, revision=1)
                    elif gfccspi and subtype == 2:
                        from dev_apimote import APIMOTE
                        self.driver = APIMOTE(self.dev, revision=2)
                    else:
                        raise KBInterfaceError(
                            "KillerBee doesn't know how to interact with serial device at '%s'."
                            % self.dev)
            # Otherwise unrecognized device string type was provided:
            else:
                raise KBInterfaceError(
                    "KillerBee doesn't understand device given by '%s'." %
                    device)

        # Start a connection to the remote packet logging server, if able:
        if datasource is not None:
            try:
                import dblog
                self.dblog = dblog.DBLogger(datasource)
            except Exception as e:
                warn("Error initializing DBLogger (%s)." % e)
                datasource = None  #give up nicely if error connecting, etc.