Skip to content

Commit 33d75f4

Browse files
authored
Merge pull request #18 from dhalbert/bleio-api-revamp
Bleio attribute api revamp
2 parents eb5ae68 + 67b9341 commit 33d75f4

14 files changed

+318
-103
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Introduction
1313
:target: https://travis-ci.com/adafruit/Adafruit_CircuitPython_ble
1414
:alt: Build Status
1515

16-
This module provides higher-level BLE (Bluetooth Low Energy) functionality, building on the native `bleio` module.
16+
This module provides higher-level BLE (Bluetooth Low Energy) functionality,
17+
building on the native `_bleio` module.
1718

1819
Dependencies
1920
=============

adafruit_ble/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
====================================================
2525
2626
This module provides higher-level BLE (Bluetooth Low Energy) functionality,
27-
building on the native `bleio` module.
27+
building on the native `_bleio` module.
2828
2929
* Author(s): Dan Halbert for Adafruit Industries
3030

adafruit_ble/address.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_ble.address`
24+
====================================================
25+
26+
BLE Address
27+
28+
* Author(s): Dan Halbert for Adafruit Industries
29+
30+
"""
31+
32+
from _bleio import UUID as _bleio_Address
33+
34+
UUID = _bleio_Address
35+
"""`adafruit_ble.Address` is the same as `_bleio.Address`"""

adafruit_ble/advertising.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@ class Advertisement:
179179
consisting of an advertising data packet and an optional scan response packet.
180180
181181
:param int flags: advertising flags. Default is general discovery, and BLE only (not classic)
182+
:param int appearance: If not None, add BLE Appearance value to advertisement.
183+
An Appearance describes what kind of device is advertising (keyboard, clock,
184+
glucose meter, etc.)
182185
"""
183-
def __init__(self, flags=None, tx_power=None):
186+
def __init__(self, flags=None, tx_power=None, appearance=None):
184187
self._packet = AdvertisingPacket()
185188
self._scan_response_packet = None
186189
if flags:
@@ -190,6 +193,8 @@ def __init__(self, flags=None, tx_power=None):
190193

191194
if tx_power is not None:
192195
self._packet.add_tx_power(tx_power)
196+
if appearance is not None:
197+
self._packet.add_appearance(appearance)
193198

194199
def add_name(self, name):
195200
"""Add name to advertisement. If it doesn't fit, add truncated name to packet,
@@ -246,10 +251,11 @@ class ServerAdvertisement(Advertisement):
246251
247252
:param Peripheral peripheral: the Peripheral to advertise. Use its services and name.
248253
:param int tx_power: transmit power in dBm at 0 meters (8 bit signed value). Default 0 dBm
254+
:param int appearance: If not None, add appearance value to advertisement.
249255
"""
250256

251-
def __init__(self, peripheral, *, tx_power=0):
252-
super().__init__()
257+
def __init__(self, peripheral, *, tx_power=0, appearance=None):
258+
super().__init__(tx_power=tx_power, appearance=appearance)
253259
uuids = [service.uuid for service in peripheral.services if not service.secondary]
254260
self.add_uuids(uuids,
255261
AdvertisingPacket.ALL_16_BIT_SERVICE_UUIDS,

adafruit_ble/beacon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"""
3131

3232
import struct
33-
import bleio
33+
from _bleio import Peripheral
3434

3535
from .advertising import AdvertisingPacket
3636

@@ -41,7 +41,7 @@ def __init__(self, advertising_packet):
4141
4242
:param AdvertisingPacket advertising_packet
4343
"""
44-
self._broadcaster = bleio.Peripheral(name=None)
44+
self._broadcaster = Peripheral()
4545
self._advertising_packet = advertising_packet
4646

4747
def start(self, interval=1.0):
@@ -76,7 +76,7 @@ def __init__(self, company_id, uuid, major, minor, rssi):
7676
Example::
7777
7878
from adafruit_ble.beacon import LocationBeacon
79-
from bleio import UUID
79+
from adafruit_ble.uuid import UUID
8080
test_uuid = UUID('12345678-1234-1234-1234-123456789abc')
8181
test_company = 0xFFFF
8282
b = LocationBeacon(test_company, test_uuid, 123, 234, -54)

adafruit_ble/current_time_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import struct
3232
import time
3333

34-
from bleio import Peripheral, UUID
34+
from _bleio import Peripheral, UUID
3535
from .advertising import SolicitationAdvertisement
3636

3737
class CurrentTimeClient:
@@ -66,7 +66,7 @@ class CurrentTimeClient:
6666
LOCAL_TIME_INFORMATION_UUID = UUID(0x2A0F)
6767

6868
def __init__(self, name=None, tx_power=0):
69-
self._periph = Peripheral(name=name)
69+
self._periph = Peripheral(name)
7070
self._advertisement = SolicitationAdvertisement(self._periph.name,
7171
(self.CTS_UUID,), tx_power=tx_power)
7272
self._current_time_char = self._local_time_char = None
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_ble.device_information`
24+
====================================================
25+
26+
Device Information Service (DIS)
27+
28+
* Author(s): Dan Halbert for Adafruit Industries
29+
30+
"""
31+
from _bleio import Attribute, Characteristic, Service, UUID
32+
33+
class DeviceInformationService:
34+
"""This is a factory class only, and has no instances."""
35+
36+
@staticmethod
37+
def add_to_peripheral(peripheral, *, model_number=None, serial_number=None,
38+
firmware_revision=None, hardware_revision='',
39+
software_revision='', manufacturer=''):
40+
"""
41+
Add a Service with fixed Device Information Service characteristics to the given Peripheral.
42+
All values are optional.
43+
44+
:param str model_number: Device model number. If None use `sys.platform`.
45+
:param str serial_number: Device serial number. If None use a hex representation of
46+
``microcontroller.cpu.id``.
47+
:param str firmware_revision: Device firmware revision.
48+
If None use ``os.uname().version``.
49+
:param str hardware_revision: Device hardware revision.
50+
:param str software_revision: Device software revision.
51+
:param str manufacturer: Device manufacturer name
52+
:return: the created Service
53+
54+
Example::
55+
56+
peripheral = Peripheral()
57+
dis = DeviceInformationService.add_to_peripheral(
58+
peripheral, software_revision="1.2.4", manufacturer="Acme Corp")
59+
"""
60+
61+
# Avoid creating constants with names if not necessary. Just takes up space.
62+
# Device Information Service UUID = 0x180A
63+
# Module Number UUID = 0x2A24
64+
# Serial Number UUID = 0x2A25
65+
# Firmware Revision UUID = 0x2A26
66+
# Hardware Revision UUID = 0x2A27
67+
# Software Revision UUID = 0x2A28
68+
# Manufacturer Name UUID = 0x2A29
69+
70+
service = Service.add_to_peripheral(peripheral, UUID(0x180A))
71+
72+
if model_number is None:
73+
import sys
74+
model_number = sys.platform
75+
if serial_number is None:
76+
import microcontroller
77+
import binascii
78+
serial_number = binascii.hexlify(microcontroller.cpu.uid).decode('utf-8') # pylint: disable=no-member
79+
80+
if firmware_revision is None:
81+
import os
82+
firmware_revision = os.uname().version
83+
84+
# Values must correspond to UUID numbers.
85+
for uuid_num, value in zip(
86+
range(0x2A24, 0x2A29+1),
87+
(model_number, serial_number,
88+
firmware_revision, hardware_revision, software_revision,
89+
manufacturer)):
90+
91+
Characteristic.add_to_service(
92+
service, UUID(uuid_num), properties=Characteristic.READ,
93+
read_perm=Attribute.OPEN, write_perm=Attribute.NO_ACCESS,
94+
fixed_length=True, max_length=len(value),
95+
initial_value=value)
96+
97+
return service

0 commit comments

Comments
 (0)