Exemplo n.º 1
0
    def connect(self, user_defined_features=None):
        """Open a connection to the node.

        Args:
            user_defined_features (dict, optional): User-defined feature to be
                added.
        """
        self._update_node_status(NodeStatus.CONNECTING)
        self.add_external_features(user_defined_features)
        super(Node, self).connect(self.get_tag(), self._device.addrType)

        # Getting services.
        services = self.getServices()
        if not services:
            self._update_node_status(NodeStatus.DEAD)
            return

        # Handling Debug, Config, and Feature characteristics.
        for service in services:
            if Debug.is_debug_service(str(service.uuid)):
                # Handling Debug.
                self._debug_console = self._build_debug_console(service)
            #elif Config.is_config_service(str(service.uuid)):
            # Handling Config.
            #    pass
            #else:
            # Getting characteristics.
            characteristics = service.getCharacteristics()
            for characteristic in characteristics:

                # Storing characteristics' handle to characteristic mapping.
                self._char_handle_to_characteristic_dict[
                    characteristic.getHandle()] = characteristic

                # Building characteristics' features.
                if FeatureCharacteristic.is_base_feature_characteristic(
                        str(characteristic.uuid)):
                    self._build_features(characteristic)
                elif FeatureCharacteristic.is_extended_feature_characteristic(
                        str(characteristic.uuid)):
                    self._build_features_known_uuid(characteristic, [
                        FeatureCharacteristic.get_extended_feature_class(
                            characteristic.uuid)
                    ])
                elif bool(self._external_uuid_to_features_dic) \
                    and characteristic.uuid in self._external_uuid_to_features_dic:
                    self._build_features_known_uuid(characteristic, [
                        self._external_uuid_to_features_dic[
                            characteristic.uuid]
                    ])

        # For each feature store a reference to the characteristic offering the
        # feature, useful to enable/disable notifications on the characteristic
        # itself.
        self._set_features_characteristics()

        # Change node's status.
        self._update_node_status(NodeStatus.CONNECTED)
Exemplo n.º 2
0
    def _build_features(self, characteristic):
        """Build the exported features of a BLE characteristic.

        After building the features, add them to the dictionary of the features
        to be updated.

        Args:
            characteristic (Characteristic): The BLE characteristic. Refer to
                `Characteristic <https://ianharvey.github.io/bluepy-doc/characteristic.html>`_
                for more information.
        """
        # Extracting the feature mask from the characteristic's UUID.
        feature_mask = FeatureCharacteristic.extract_feature_mask(
            characteristic.uuid)
        # Looking for the exported features in reverse order to get them in the
        # correct order in case of characteristic that exports multiple
        # features.
        features = []
        mask = 1 << 31
        for i in range(0, 32):
            if (feature_mask & mask) != 0:
                if mask in self._mask_to_feature_dic:
                    feature = self._mask_to_feature_dic[mask]
                    if feature is not None:
                        feature.set_enable(True)
                        features.append(feature)
            mask = mask >> 1

        # If the features are valid, add an entry for the corresponding
        # characteristic.
        if features:
            self._update_char_handle_to_features_dict[
                characteristic.getHandle()] = features
Exemplo n.º 3
0
    def connect(self, user_defined_features=None):
        """Open a connection to the node.

        Please note that there is no supervision timeout API within the SDK,
        hence it is not possible to detect immediately an unexpected
        disconnection; it is detected and notified via listeners as soon as a
        read/write/notify operation is executed on the device (limitation
        intrinsic to the bluepy library).

        Args:
            user_defined_features (dict, optional): User-defined feature to be
            added.

        Returns:
            bool: True if the connection to the node has been successful, False
            otherwise.
        """
        try:
            if not self._status == NodeStatus.IDLE:
                return False

            # Creating a delegate object, which is called when asynchronous
            # events such as Bluetooth notifications occur.
            self.withDelegate(NodeDelegate(self))

            # Connecting.
            self._update_node_status(NodeStatus.CONNECTING)
            self.add_external_features(user_defined_features)
            with lock(self):
                super(Node, self).connect(self.get_tag(),
                                          self._device.addrType)

            # Getting services.
            with lock(self):
                services = self.getServices()
            if not services:
                self.disconnect()
                return False

            # Handling Debug, Config, and Feature characteristics.
            for service in services:
                if Debug.is_debug_service(str(service.uuid)):
                    # Handling Debug.
                    self._debug_console = self._build_debug_console(service)
                #elif Config.is_config_service(str(service.uuid)):
                # Handling Config.
                #    pass
                #else:
                # Getting characteristics.
                with lock(self):
                    characteristics = service.getCharacteristics()
                for characteristic in characteristics:

                    # Storing characteristics' handle to characteristic mapping.
                    with lock(self):
                        self._char_handle_to_characteristic_dict[
                            characteristic.getHandle()] = characteristic

                    # Building characteristics' features.
                    if FeatureCharacteristic.is_base_feature_characteristic(
                            str(characteristic.uuid)):
                        self._build_features(characteristic)
                    elif FeatureCharacteristic.is_extended_feature_characteristic(
                            str(characteristic.uuid)):
                        self._build_features_known_uuid(
                            characteristic, [
                                FeatureCharacteristic.
                                get_extended_feature_class(characteristic.uuid)
                            ])
                    elif bool(self._external_uuid_to_features_dic) \
                        and characteristic.uuid in self._external_uuid_to_features_dic:
                        self._build_features_known_uuid(
                            characteristic, [
                                self._external_uuid_to_features_dic[
                                    characteristic.uuid]
                            ])

            # For each feature store a reference to the characteristic offering the
            # feature, useful to enable/disable notifications on the characteristic
            # itself.
            self._set_features_characteristics()

            # Change node's status.
            self._update_node_status(NodeStatus.CONNECTED)

            return self._status == NodeStatus.CONNECTED
        except BTLEException as e:
            self._unexpected_disconnect()