1# Copyright 2021-2022 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# ----------------------------------------------------------------------------- 16# Imports 17# ----------------------------------------------------------------------------- 18import logging 19import struct 20 21from .gatt import ( 22 Service, 23 Characteristic, 24 GATT_GENERIC_ACCESS_SERVICE, 25 GATT_DEVICE_NAME_CHARACTERISTIC, 26 GATT_APPEARANCE_CHARACTERISTIC, 27) 28 29# ----------------------------------------------------------------------------- 30# Logging 31# ----------------------------------------------------------------------------- 32logger = logging.getLogger(__name__) 33 34 35# ----------------------------------------------------------------------------- 36# Classes 37# ----------------------------------------------------------------------------- 38 39# ----------------------------------------------------------------------------- 40class GenericAccessService(Service): 41 def __init__(self, device_name, appearance=(0, 0)): 42 device_name_characteristic = Characteristic( 43 GATT_DEVICE_NAME_CHARACTERISTIC, 44 Characteristic.READ, 45 Characteristic.READABLE, 46 device_name.encode('utf-8')[:248], 47 ) 48 49 appearance_characteristic = Characteristic( 50 GATT_APPEARANCE_CHARACTERISTIC, 51 Characteristic.READ, 52 Characteristic.READABLE, 53 struct.pack('<H', (appearance[0] << 6) | appearance[1]), 54 ) 55 56 super().__init__( 57 GATT_GENERIC_ACCESS_SERVICE, 58 [device_name_characteristic, appearance_characteristic], 59 ) 60