Beispiel #1
0
 def _setup_database(cls):
     w_props = GattsCharacteristicProperties(read=False,
                                             write=True,
                                             max_length=cls.write_size,
                                             variable_length=True)
     w_no_resp_props = GattsCharacteristicProperties(
         read=False,
         write_no_response=True,
         max_length=cls.write_size,
         variable_length=True)
     svc = cls.periph.database.add_service(cls.service_uuid)
     cls.periph_conn.write_char = svc.add_characteristic(
         cls.write_char_uuid, w_props)
     cls.periph_conn.write_no_resp_char = svc.add_characteristic(
         cls.write_no_resp_char_uuid, w_no_resp_props)
Beispiel #2
0
    def __init__(self,
                 service,
                 glucose_database,
                 security_level=SecurityLevel.OPEN,
                 include_context_characteristic=True):
        """
        :type service: GattsService
        :type glucose_database: IGlucoseDatabase
        :param security_level:
        """
        self.service = service
        self.database = glucose_database

        measurement_char_props = GattsCharacteristicProperties(read=False,
                                                               notify=True,
                                                               max_length=20)
        feature_props = GattsCharacteristicProperties(
            read=True,
            max_length=GlucoseFeatures.encoded_size(),
            variable_length=False)
        racp_props = GattsCharacteristicProperties(
            read=False,
            write=True,
            indicate=True,
            max_length=20,
            variable_length=True,
            security_level=security_level)
        self.measurement_characteristic = service.add_characteristic(
            MEASUREMENT_CHARACTERISTIC_UUID, measurement_char_props)
        if include_context_characteristic:
            self.context_characteristic = service.add_characteristic(
                MEASUREMENT_CONTEXT_CHARACTERISTIC_UUID,
                measurement_char_props)
            self.context_characteristic.on_notify_complete.register(
                self._on_notify_complete)
        else:
            self.context_characteristic = None
        self.feature_characteristic = service.add_characteristic(
            FEATURE_CHARACTERISTIC_UUID, feature_props)
        self.racp_characteristic = service.add_characteristic(
            RACP_CHARACTERISTIC_UUID, racp_props)
        self.racp_characteristic.on_write.register(self._on_racp_write)
        self._current_command = None
        self._records_to_report = []  # type: list[GlucoseMeasurement]
        self._active_notifications = []
        self.service.peer.on_disconnect.register(self._on_disconnect)
        self.measurement_characteristic.on_notify_complete.register(
            self._on_notify_complete)
Beispiel #3
0
 def set_value(self, value, max_len=None):
     if not self._char:
         props = GattsCharacteristicProperties(read=True,
                                               max_length=max_len
                                               or len(value))
         self._char = self.service.add_characteristic(
             self.uuid, props, value)
     else:
         self._char.set_value(value)
Beispiel #4
0
    def __init__(self, service, is_writable=False,
                 enable_local_time_info_char=False, enable_ref_time_info_char=False):
        """
        :type service: GattsService
        :param is_writable:
        :param enable_local_time_info_char:
        :param enable_ref_time_info_char:
        """
        self._service = service
        self._is_writable = is_writable
        self._has_local_time_info = enable_local_time_info_char
        self._has_ref_time_info = enable_ref_time_info_char
        self._current_time_read_callback = self._on_characteristic_read_auto
        self._time_delta = datetime.timedelta()

        self._on_current_time_write_event = EventSource("Current Time Write Event")
        self._on_local_time_info_write_event = EventSource("Local Time Info Write Event")

        self._current_time_dispatcher = DecodedReadWriteEventDispatcher(self, CurrentTime,
                                                                        self._on_current_time_write_event, logger)
        self._local_time_dispatcher = DecodedReadWriteEventDispatcher(self, LocalTimeInfo,
                                                                      self._on_local_time_info_write_event, logger)

        cur_time_char_props = GattsCharacteristicProperties(read=True, notify=True, write=is_writable,
                                                            variable_length=False, max_length=CurrentTime.encoded_size())
        self._cur_time_char = service.add_characteristic(CURRENT_TIME_CHARACTERISTIC_UUID, cur_time_char_props)
        self._cur_time_char.on_read.register(self._on_current_time_read)
        self._cur_time_char.on_write.register(self._current_time_dispatcher)

        if enable_local_time_info_char:
            local_time_props = GattsCharacteristicProperties(read=True, notify=True, write=is_writable,
                                                             variable_length=False, max_length=LocalTimeInfo.encoded_size())
            self._local_time_char = service.add_characteristic(LOCAL_TIME_INFO_CHARACTERISTIC_UUID, local_time_props)
            self.set_local_time_info()
            self._local_time_char.on_write.register(self._local_time_dispatcher)

        if enable_ref_time_info_char:
            ref_time_props = GattsCharacteristicProperties(read=True, notify=False, write=False,
                                                           variable_length=False, max_length=ReferenceTimeInfo.encoded_size())
            self._ref_time_char = service.add_characteristic(REFERENCE_INFO_CHARACTERISTIC_UUID, ref_time_props)
            self.set_reference_info()

        self.set_time(datetime.datetime.utcfromtimestamp(0))
Beispiel #5
0
def _generate_char(svc, uuid, *prop_names, init_value=b"", max_length=20):
    prop_args = dict(read=False,
                     write=False,
                     notify=False,
                     indicate=False,
                     broadcast=False,
                     write_no_response=False,
                     max_length=max_length)
    prop_args.update({p: True for p in prop_names})
    properties = GattsCharacteristicProperties(**prop_args)
    return svc.add_characteristic(uuid, properties, init_value)
Beispiel #6
0
    def __init__(self, service, enable_notifications=False, security_level=SecurityLevel.OPEN):
        """
        :type service: GattsService
        :param enable_notifications:
        :param security_level:
        """
        self._service = service

        battery_level_char_props = GattsCharacteristicProperties(read=True, notify=enable_notifications,
                                                                 variable_length=False,
                                                                 max_length=BatteryLevel.encoded_size(),
                                                                 security_level=security_level)
        self._batt_characteristic = service.add_characteristic(BATTERY_LEVEL_CHARACTERISTIC_UUID,
                                                               battery_level_char_props)
Beispiel #7
0
    def _setup_database(cls):
        svc = cls.periph.database.add_service(cls.service_1_uuid)

        all_props = GattsCharacteristicProperties(
            read=True,
            write=True,
            notify=True,
            indicate=True,
            write_no_response=True,
            broadcast=True,
            max_length=20,
            variable_length=True,
            sccd=True,
            user_description=GattsUserDescriptionProperties("Everything"),
            presentation_format=PresentationFormat(8, 0, 0x2700))
        cls.periph_conn.all_char = svc.add_characteristic(
            cls.all_char_uuid, all_props, b"0" * 20)

        svc = cls.periph.database.add_service(cls.service_2_uuid)
        cls.periph_conn.write_char = _generate_char(svc, cls.write_char_uuid,
                                                    "write")
        cls.periph_conn.read_char = _generate_char(svc,
                                                   cls.read_char_uuid,
                                                   "read",
                                                   init_value=b"1234" * 5)
        cls.periph_conn.notify_char = _generate_char(svc, cls.notify_char_uuid,
                                                     "notify")
        cls.periph_conn.indicate_char = _generate_char(svc,
                                                       cls.indicate_char_uuid,
                                                       "indicate")
        cls.periph_conn.write_no_resp_char = _generate_char(
            svc, cls.write_no_resp_char_uuid, "write_no_response")
        cls.periph_conn.large_char = _generate_char(
            svc,
            cls.large_char_uuid,
            "read",
            "write",
            max_length=cls.large_char_size)