Skip to content

Commit 4eef666

Browse files
authored
Merge pull request #173 from EasyRiderr/fix_add_gatt
Fix add gatt by adding new **primary** services
2 parents 20c3ac5 + 5b66200 commit 4eef666

File tree

2 files changed

+98
-41
lines changed

2 files changed

+98
-41
lines changed

bless/backends/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ async def add_gatt(self, gatt_tree: Dict):
309309
uuids and the attributes are the properties
310310
"""
311311
for service_uuid, service_info in gatt_tree.items():
312-
await self.add_new_service(service_uuid)
312+
await self.add_new_service(service_uuid, primary=True)
313313
for char_uuid, char_info in service_info.items():
314314
await self.add_new_characteristic(
315315
service_uuid,

test/backends/test_server.py

Lines changed: 97 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,43 +66,7 @@ def hex_to_byte(self, hexstr: str) -> bytearray:
6666
def byte_to_hex(self, b: bytearray) -> str:
6767
return "".join([hex(x)[2:] for x in b]).upper()
6868

69-
@pytest.mark.asyncio
70-
async def test_server(self):
71-
# Initialize
72-
server: BlessServer = BlessServer("Test Server")
73-
74-
# setup a service
75-
service_uuid: str = str(uuid.uuid4())
76-
await server.add_new_service(service_uuid)
77-
78-
assert len(server.services) > 0
79-
# print(server.services)
80-
81-
# setup a characteristic for the service
82-
char_uuid: str = str(uuid.uuid4())
83-
char_flags: GATTCharacteristicProperties = (
84-
GATTCharacteristicProperties.read
85-
| GATTCharacteristicProperties.write
86-
| GATTCharacteristicProperties.notify
87-
)
88-
value: Optional[bytearray] = None
89-
permissions: GATTAttributePermissions = (
90-
GATTAttributePermissions.readable | GATTAttributePermissions.writable
91-
)
92-
93-
if use_encrypted:
94-
print("\nEncryption has been enabled, ensure that you are bonded")
95-
permissions = (
96-
GATTAttributePermissions.read_encryption_required
97-
| GATTAttributePermissions.write_encryption_required
98-
)
99-
100-
await server.add_new_characteristic(
101-
service_uuid, char_uuid, char_flags, value, permissions
102-
)
103-
104-
assert server.services[service_uuid].get_characteristic(char_uuid)
105-
69+
async def setup_callbacks_and_start(self, server: BlessServer) -> None:
10670
# Set up read, write, and subscribe callbacks
10771
def read(
10872
characteristic: BlessGATTCharacteristic, request: BlessGATTRequest
@@ -112,7 +76,7 @@ def read(
11276

11377
def write(
11478
characteristic: BlessGATTCharacteristic,
115-
value: bytearray,
79+
value: bytes,
11680
request: BlessGATTRequest,
11781
) -> None:
11882
print(f"Write request: {request}")
@@ -140,6 +104,12 @@ def unsubscribe(
140104

141105
assert await server.is_advertising() is True
142106

107+
async def read_write_notify_tests_on(
108+
self,
109+
server: BlessServer,
110+
svc_uuid: str,
111+
char_uuid: str
112+
) -> None:
143113
# Subscribe
144114
assert await server.is_connected() is False
145115

@@ -175,16 +145,103 @@ def unsubscribe(
175145
print("A new value will be notified on the phone")
176146
await aioconsole.ainput("Press enter to receive the new value...")
177147

178-
server.update_value(service_uuid, char_uuid)
148+
server.update_value(svc_uuid, char_uuid)
179149

180150
new_value: str = await aioconsole.ainput("Enter the new value: ")
181151
assert new_value == hex_val
182152

183153
# unsubscribe
184154
print("Unsubscribe from the characteristic")
185-
await aioconsole.ainput("Press entery when ready...")
155+
await aioconsole.ainput("Press enter when ready...")
186156
assert await server.is_connected() is False
187157

158+
@pytest.mark.asyncio
159+
async def test_server(self):
160+
# Initialize
161+
server: BlessServer = BlessServer("Test Server")
162+
163+
# setup a service
164+
service_uuid: str = str(uuid.uuid4())
165+
await server.add_new_service(service_uuid)
166+
167+
assert len(server.services) > 0
168+
# print(server.services)
169+
170+
# setup a characteristic for the service
171+
char_uuid: str = str(uuid.uuid4())
172+
char_flags: GATTCharacteristicProperties = (
173+
GATTCharacteristicProperties.read
174+
| GATTCharacteristicProperties.write
175+
| GATTCharacteristicProperties.notify
176+
)
177+
value: Optional[bytearray] = None
178+
permissions: GATTAttributePermissions = (
179+
GATTAttributePermissions.readable | GATTAttributePermissions.writable
180+
)
181+
182+
if use_encrypted:
183+
print("\nEncryption has been enabled, ensure that you are bonded")
184+
permissions = (
185+
GATTAttributePermissions.read_encryption_required
186+
| GATTAttributePermissions.write_encryption_required
187+
)
188+
189+
await server.add_new_characteristic(
190+
service_uuid, char_uuid, char_flags, value, permissions
191+
)
192+
193+
assert server.services[service_uuid].get_characteristic(char_uuid)
194+
195+
await self.setup_callbacks_and_start(server)
196+
await self.read_write_notify_tests_on(server, service_uuid, char_uuid)
197+
198+
# Stop Advertising
199+
await server.stop()
200+
await asyncio.sleep(2)
201+
assert await server.is_advertising() is False
202+
203+
@pytest.mark.asyncio
204+
async def test_server_add_gatt(self):
205+
# Initialize
206+
server: BlessServer = BlessServer("Test Server with a GATT Tree")
207+
208+
# setup a gatt tree
209+
service1_uuid: str = str(uuid.uuid4())
210+
service2_uuid: str = str(uuid.uuid4())
211+
characteristic1_uuid: str = str(uuid.uuid4())
212+
characteristic2_uuid: str = str(uuid.uuid4())
213+
gatt_tree = {
214+
service1_uuid: {
215+
characteristic1_uuid: {
216+
"Properties": GATTCharacteristicProperties.read
217+
| GATTCharacteristicProperties.write
218+
| GATTCharacteristicProperties.notify,
219+
"Value": None,
220+
"Permissions": GATTAttributePermissions.readable
221+
| GATTAttributePermissions.writable,
222+
}
223+
},
224+
service2_uuid: {
225+
characteristic2_uuid: {
226+
"Properties": GATTCharacteristicProperties.read
227+
| GATTCharacteristicProperties.write
228+
| GATTCharacteristicProperties.notify,
229+
"Value": None,
230+
"Permissions": GATTAttributePermissions.readable
231+
| GATTAttributePermissions.writable,
232+
}
233+
}
234+
}
235+
236+
await server.add_gatt(gatt_tree)
237+
assert len(server.services) == 2
238+
await self.setup_callbacks_and_start(server)
239+
await self.read_write_notify_tests_on(
240+
server,
241+
service2_uuid,
242+
characteristic2_uuid
243+
)
244+
188245
# Stop Advertising
189246
await server.stop()
190247
await asyncio.sleep(2)

0 commit comments

Comments
 (0)