Пример #1
0
    def from_c(cls, adv_report_evt):
        ad_list         = util.uint8_array_to_list(adv_report_evt.data, adv_report_evt.dlen)
        ble_adv_data    = cls()
        index           = 0
        while index < len(ad_list):
            ad_len  = ad_list[index]
            ad_type = ad_list[index + 1]
            offset  = index + 2
            try:
                key                         = BLEAdvData.Types(ad_type)
                ble_adv_data.records[key]   = ad_list[offset: offset + ad_len - 1]
            except ValueError:
                logger.error('Invalid advertising data type: 0x{:02X}'.format(ad_type))
                pass
            except IndexError:
                logger.error('Invalid advertising data: {}'.format(ad_list))
                return ble_adv_data
            index += (ad_len + 1)

        return ble_adv_data
Пример #2
0
    def from_c(cls, adv_report_evt):
        ad_list = util.uint8_array_to_list(adv_report_evt.data,
                                           adv_report_evt.dlen)
        ble_adv_data = cls()
        index = 0
        while index < len(ad_list):
            ad_len = ad_list[index]
            ad_type = ad_list[index + 1]
            offset = index + 2
            try:
                key = BLEAdvData.Types(ad_type)
                ble_adv_data.records[key] = ad_list[offset:offset + ad_len - 1]
            except ValueError:
                logger.error(
                    'Invalid advertising data type: 0x{:02X}'.format(ad_type))
                pass
            except IndexError:
                logger.error('Invalid advertising data: {}'.format(ad_list))
                return ble_adv_data
            index += (ad_len + 1)

        return ble_adv_data
Пример #3
0
    def ble_evt_handler(self, adapter, ble_event):
        evt_id = None
        try:
            evt_id = BLEEvtID(ble_event.header.evt_id)
        except:
            logger.error('Invalid received BLE event id: 0x{:02X}'.format(
                ble_event.header.evt_id))
            return
        try:
            if evt_id == BLEEvtID.gap_evt_connected:
                connected_evt = ble_event.evt.gap_evt.params.connected

                for obs in self.observers:
                    obs.on_gap_evt_connected(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gap_evt.conn_handle,
                        peer_addr=BLEGapAddr.from_c(connected_evt.peer_addr),
                        own_addr=BLEGapAddr.from_c(connected_evt.own_addr),
                        role=BLEGapRoles(connected_evt.role),
                        conn_params=BLEGapConnParams.from_c(
                            connected_evt.conn_params))

            elif evt_id == BLEEvtID.gap_evt_disconnected:
                disconnected_evt = ble_event.evt.gap_evt.params.disconnected

                for obs in self.observers:
                    obs.on_gap_evt_disconnected(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gap_evt.conn_handle,
                        reason=BLEHci(disconnected_evt.reason))

            elif evt_id == BLEEvtID.gap_evt_timeout:
                timeout_evt = ble_event.evt.gap_evt.params.timeout

                for obs in self.observers:
                    obs.on_gap_evt_timeout(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gap_evt.conn_handle,
                        src=BLEGapTimeoutSrc(timeout_evt.src))

            elif evt_id == BLEEvtID.gap_evt_adv_report:
                adv_report_evt = ble_event.evt.gap_evt.params.adv_report
                adv_type = None
                if not adv_report_evt.scan_rsp:
                    adv_type = BLEGapAdvType(adv_report_evt.type)

                for obs in self.observers:
                    obs.on_gap_evt_adv_report(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gap_evt.conn_handle,
                        peer_addr=BLEGapAddr.from_c(adv_report_evt.peer_addr),
                        rssi=adv_report_evt.rssi,
                        adv_type=adv_type,
                        adv_data=BLEAdvData.from_c(adv_report_evt))

            elif evt_id == BLEEvtID.evt_tx_complete:
                tx_complete_evt = ble_event.evt.common_evt.params.tx_complete

                for obs in self.observers:
                    obs.on_evt_tx_complete(
                        ble_driver=self,
                        conn_handle=ble_event.evt.common_evt.conn_handle,
                        count=tx_complete_evt.count)

            elif evt_id == BLEEvtID.gattc_evt_write_rsp:
                write_rsp_evt = ble_event.evt.gattc_evt.params.write_rsp

                for obs in self.observers:
                    obs.on_gattc_evt_write_rsp(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gattc_evt.conn_handle,
                        status=BLEGattStatusCode(
                            ble_event.evt.gattc_evt.gatt_status),
                        error_handle=ble_event.evt.gattc_evt.error_handle,
                        attr_handle=write_rsp_evt.handle,
                        write_op=BLEGattWriteOperation(write_rsp_evt.write_op),
                        offset=write_rsp_evt.offset,
                        data=util.uint8_array_to_list(write_rsp_evt.data,
                                                      write_rsp_evt.len))

            elif evt_id == BLEEvtID.gattc_evt_hvx:
                hvx_evt = ble_event.evt.gattc_evt.params.hvx

                for obs in self.observers:
                    obs.on_gattc_evt_hvx(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gattc_evt.conn_handle,
                        status=BLEGattStatusCode(
                            ble_event.evt.gattc_evt.gatt_status),
                        error_handle=ble_event.evt.gattc_evt.error_handle,
                        attr_handle=hvx_evt.handle,
                        hvx_type=BLEGattHVXType(hvx_evt.type),
                        data=util.uint8_array_to_list(hvx_evt.data,
                                                      hvx_evt.len))

            elif evt_id == BLEEvtID.gattc_evt_prim_srvc_disc_rsp:
                prim_srvc_disc_rsp_evt = ble_event.evt.gattc_evt.params.prim_srvc_disc_rsp

                services = list()
                for s in util.service_array_to_list(
                        prim_srvc_disc_rsp_evt.services,
                        prim_srvc_disc_rsp_evt.count):
                    services.append(BLEService.from_c(s))

                for obs in self.observers:
                    obs.on_gattc_evt_prim_srvc_disc_rsp(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gattc_evt.conn_handle,
                        status=BLEGattStatusCode(
                            ble_event.evt.gattc_evt.gatt_status),
                        services=services)

            elif evt_id == BLEEvtID.gattc_evt_char_disc_rsp:
                char_disc_rsp_evt = ble_event.evt.gattc_evt.params.char_disc_rsp

                characteristics = list()
                for ch in util.ble_gattc_char_array_to_list(
                        char_disc_rsp_evt.chars, char_disc_rsp_evt.count):
                    characteristics.append(BLECharacteristic.from_c(ch))

                for obs in self.observers:
                    obs.on_gattc_evt_char_disc_rsp(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gattc_evt.conn_handle,
                        status=BLEGattStatusCode(
                            ble_event.evt.gattc_evt.gatt_status),
                        characteristics=characteristics)

            elif evt_id == BLEEvtID.gattc_evt_desc_disc_rsp:
                desc_disc_rsp_evt = ble_event.evt.gattc_evt.params.desc_disc_rsp

                descriptions = list()
                for d in util.desc_array_to_list(desc_disc_rsp_evt.descs,
                                                 desc_disc_rsp_evt.count):
                    descriptions.append(BLEDescriptor.from_c(d))

                for obs in self.observers:
                    obs.on_gattc_evt_desc_disc_rsp(
                        ble_driver=self,
                        conn_handle=ble_event.evt.gattc_evt.conn_handle,
                        status=BLEGattStatusCode(
                            ble_event.evt.gattc_evt.gatt_status),
                        descriptions=descriptions)

        except Exception as e:
            logger.error("Exception: {}".format(str(e)))
            for line in traceback.extract_tb(sys.exc_info()[2]):
                logger.error(line)
            logger.error("")
Пример #4
0
 def from_c(cls, gattc_write_params):
     return cls(write_op=BLEGattWriteOperation(gattc_write_params.write_op),
                flags=gattc_write_params.flags,
                handle=gattc_write_params.handle,
                data=util.uint8_array_to_list(gattc_write_params.p_value,
                                              gattc_write_params.len))
Пример #5
0
 def from_c(cls, addr):
     addr_list = util.uint8_array_to_list(addr.addr,
                                          driver.BLE_GAP_ADDR_LEN)
     addr_list.reverse()
     return cls(addr_type=BLEGapAddr.Types(addr.addr_type), addr=addr_list)
Пример #6
0
 def from_c(cls, gattc_write_params):
     return cls(write_op = BLEGattWriteOperation(gattc_write_params.write_op),
                flags    = gattc_write_params.flags,
                handle   = gattc_write_params.handle,
                data     = util.uint8_array_to_list(gattc_write_params.p_value,
                                                    gattc_write_params.len))
Пример #7
0
 def from_c(cls, addr):
     addr_list = util.uint8_array_to_list(addr.addr, driver.BLE_GAP_ADDR_LEN)
     addr_list.reverse()
     return cls(addr_type    = BLEGapAddr.Types(addr.addr_type),
                addr         = addr_list)
Пример #8
0
    def ble_evt_handler(self, adapter, ble_event):
        evt_id = None
        try:
            evt_id = BLEEvtID(ble_event.header.evt_id)
        except:
            logger.error('Invalid received BLE event id: 0x{:02X}'.format(ble_event.header.evt_id))
            return
        try:
            if evt_id == BLEEvtID.gap_evt_connected:
                connected_evt = ble_event.evt.gap_evt.params.connected

                for obs in self.observers:
                    obs.on_gap_evt_connected(ble_driver     = self,
                                             conn_handle    = ble_event.evt.gap_evt.conn_handle,
                                             peer_addr      = BLEGapAddr.from_c(connected_evt.peer_addr),
                                             own_addr       = BLEGapAddr.from_c(connected_evt.own_addr),
                                             role           = BLEGapRoles(connected_evt.role),
                                             conn_params    = BLEGapConnParams.from_c(connected_evt.conn_params))

            elif evt_id == BLEEvtID.gap_evt_disconnected:
                disconnected_evt = ble_event.evt.gap_evt.params.disconnected

                for obs in self.observers:
                    obs.on_gap_evt_disconnected(ble_driver  = self,
                                                conn_handle = ble_event.evt.gap_evt.conn_handle,
                                                reason      = BLEHci(disconnected_evt.reason))

            elif evt_id == BLEEvtID.gap_evt_timeout:
                timeout_evt = ble_event.evt.gap_evt.params.timeout

                for obs in self.observers:
                    obs.on_gap_evt_timeout(ble_driver   = self,
                                           conn_handle  = ble_event.evt.gap_evt.conn_handle,
                                           src          = BLEGapTimeoutSrc(timeout_evt.src))

            elif evt_id == BLEEvtID.gap_evt_adv_report:
                adv_report_evt  = ble_event.evt.gap_evt.params.adv_report
                adv_type        = None
                if not adv_report_evt.scan_rsp:
                    adv_type = BLEGapAdvType(adv_report_evt.type)

                for obs in self.observers:
                    obs.on_gap_evt_adv_report(ble_driver    = self,
                                              conn_handle   = ble_event.evt.gap_evt.conn_handle,
                                              peer_addr     = BLEGapAddr.from_c(adv_report_evt.peer_addr),
                                              rssi          = adv_report_evt.rssi,
                                              adv_type      = adv_type,
                                              adv_data      = BLEAdvData.from_c(adv_report_evt))

            elif evt_id == BLEEvtID.evt_tx_complete:
                tx_complete_evt = ble_event.evt.common_evt.params.tx_complete

                for obs in self.observers:
                    obs.on_evt_tx_complete(ble_driver   = self,
                                           conn_handle  = ble_event.evt.common_evt.conn_handle,
                                           count        = tx_complete_evt.count)

            elif evt_id == BLEEvtID.gattc_evt_write_rsp:
                write_rsp_evt   = ble_event.evt.gattc_evt.params.write_rsp

                for obs in self.observers:
                    obs.on_gattc_evt_write_rsp(ble_driver   = self,
                                               conn_handle  = ble_event.evt.gattc_evt.conn_handle,
                                               status       = BLEGattStatusCode(ble_event.evt.gattc_evt.gatt_status),
                                               error_handle = ble_event.evt.gattc_evt.error_handle,
                                               attr_handle  = write_rsp_evt.handle,
                                               write_op     = BLEGattWriteOperation(write_rsp_evt.write_op),
                                               offset       = write_rsp_evt.offset,
                                               data         = util.uint8_array_to_list(write_rsp_evt.data,
                                                                                       write_rsp_evt.len))

            elif evt_id == BLEEvtID.gattc_evt_hvx:
                hvx_evt = ble_event.evt.gattc_evt.params.hvx

                for obs in self.observers:
                    obs.on_gattc_evt_hvx(ble_driver     = self,
                                         conn_handle    = ble_event.evt.gattc_evt.conn_handle,
                                         status         = BLEGattStatusCode(ble_event.evt.gattc_evt.gatt_status),
                                         error_handle   = ble_event.evt.gattc_evt.error_handle,
                                         attr_handle    = hvx_evt.handle,
                                         hvx_type       = BLEGattHVXType(hvx_evt.type),
                                         data           = util.uint8_array_to_list(hvx_evt.data, hvx_evt.len))

            elif evt_id == BLEEvtID.gattc_evt_prim_srvc_disc_rsp:
                prim_srvc_disc_rsp_evt = ble_event.evt.gattc_evt.params.prim_srvc_disc_rsp

                services = list()
                for s in util.service_array_to_list(prim_srvc_disc_rsp_evt.services, prim_srvc_disc_rsp_evt.count):
                    services.append(BLEService.from_c(s))

                for obs in self.observers:
                    obs.on_gattc_evt_prim_srvc_disc_rsp(ble_driver  = self,
                                                        conn_handle = ble_event.evt.gattc_evt.conn_handle,
                                                        status      = BLEGattStatusCode(ble_event.evt.gattc_evt.gatt_status),
                                                        services    = services)

            elif evt_id == BLEEvtID.gattc_evt_char_disc_rsp:
                char_disc_rsp_evt = ble_event.evt.gattc_evt.params.char_disc_rsp

                characteristics = list()
                for ch in util.ble_gattc_char_array_to_list(char_disc_rsp_evt.chars, char_disc_rsp_evt.count):
                    characteristics.append(BLECharacteristic.from_c(ch))

                for obs in self.observers:
                    obs.on_gattc_evt_char_disc_rsp(ble_driver       = self,
                                                   conn_handle      = ble_event.evt.gattc_evt.conn_handle,
                                                   status           = BLEGattStatusCode(ble_event.evt.gattc_evt.gatt_status),
                                                   characteristics  = characteristics)

            elif evt_id == BLEEvtID.gattc_evt_desc_disc_rsp:
                desc_disc_rsp_evt = ble_event.evt.gattc_evt.params.desc_disc_rsp

                descriptions = list()
                for d in util.desc_array_to_list(desc_disc_rsp_evt.descs, desc_disc_rsp_evt.count):
                    descriptions.append(BLEDescriptor.from_c(d))

                for obs in self.observers:
                    obs.on_gattc_evt_desc_disc_rsp(ble_driver   = self,
                                                   conn_handle  = ble_event.evt.gattc_evt.conn_handle,
                                                   status       = BLEGattStatusCode(ble_event.evt.gattc_evt.gatt_status),
                                                   descriptions = descriptions)

        except Exception as e:
            logger.error("Exception: {}".format(str(e)))
            for line in traceback.extract_tb(sys.exc_info()[2]):
                logger.error(line) 
            logger.error("")