|
| 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 | +# pylint: disable=invalid-name |
| 34 | +def DeviceInformationService(*, model_number=None, serial_number=None, firmware_revision=None, |
| 35 | + hardware_revision='', software_revision='', manufacturer=''): |
| 36 | + """ |
| 37 | + Set up a Service with fixed Device Information Service charcteristics. |
| 38 | + All values are optional. |
| 39 | +
|
| 40 | + Note that this is a pseudo class: It returns a Service, but is not a subclass of Service, |
| 41 | + because Service is a native class and doesn't handle subclassing. |
| 42 | +
|
| 43 | + :param str model_number: Device model number. If None use `sys.platform`. |
| 44 | + :param str serial_number: Device serial number. If None use a hex representation of |
| 45 | + ``microcontroller.cpu.id``. |
| 46 | + :param str firmware_revision: Device firmware revision. If None use ``os.uname().version``. |
| 47 | + :param str hardware_revision: Device hardware revision. |
| 48 | + :param str software_revision: Device software revision. |
| 49 | + :param str manufacturer: Device manufacturer name |
| 50 | +
|
| 51 | + Example:: |
| 52 | +
|
| 53 | + dis = DeviceInformationService(software_revision="1.2.4", manufacturer="Acme Corp") |
| 54 | + """ |
| 55 | + |
| 56 | + # Avoid creating constants with names if not necessary. Just takes up space. |
| 57 | + # Device Information Service UUID = 0x180A |
| 58 | + # Module Number UUID = 0x2A24 |
| 59 | + # Serial Number UUID = 0x2A25 |
| 60 | + # Firmware Revision UUID = 0x2A26 |
| 61 | + # Hardware Revision UUID = 0x2A27 |
| 62 | + # Software Revision UUID = 0x2A28 |
| 63 | + # Manufacturer Name UUID = 0x2A29 |
| 64 | + |
| 65 | + characteristics = [] |
| 66 | + # Values must correspond to UUID numbers. |
| 67 | + |
| 68 | + if model_number is None: |
| 69 | + import sys |
| 70 | + model_number = sys.platform |
| 71 | + if serial_number is None: |
| 72 | + import microcontroller |
| 73 | + import binascii |
| 74 | + serial_number = binascii.hexlify(microcontroller.cpu.uid).decode('utf-8') # pylint: disable=no-member |
| 75 | + |
| 76 | + if firmware_revision is None: |
| 77 | + import os |
| 78 | + firmware_revision = os.uname().version |
| 79 | + |
| 80 | + for uuid_num, string in zip( |
| 81 | + range(0x2A24, 0x2A29+1), |
| 82 | + (model_number, serial_number, |
| 83 | + firmware_revision, hardware_revision, software_revision, |
| 84 | + manufacturer)): |
| 85 | + |
| 86 | + char = Characteristic(UUID(uuid_num), properties=Characteristic.READ, |
| 87 | + read_perm=Attribute.OPEN, write_perm=Attribute.NO_ACCESS, |
| 88 | + fixed_length=True, max_length=len(string)) |
| 89 | + char.value = string |
| 90 | + characteristics.append(char) |
| 91 | + |
| 92 | + return Service(UUID(0x180A), characteristics) |
0 commit comments