Skip to content

Commit 2612984

Browse files
author
Kevin J Walters
committed
Switching to the new match_prefixes for Advertisement matching which replaces prefix.
Changing to matching using the library rather than the match_locally enabled code and a few fixes around that dusty code path. match_prefixes came up in the review adafruit#1185
1 parent 95acc97 commit 2612984

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

CLUE_Rock_Paper_Scissors/advanced/rps_advertisements.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,19 @@ class RpsEncDataAdvertisement(Advertisement):
6363
"""
6464
flags = None
6565

66-
_PREFIX_FMT = "<B" "BHBH"
66+
_PREFIX_FMT = "<BHBH"
6767
_DATA_FMT_ENC_DATA = "8s"
6868

69-
prefix = struct.pack(
70-
_PREFIX_FMT,
71-
struct.calcsize(_PREFIX_FMT) - 1,
72-
MANUFACTURING_DATA_ADT,
73-
ADAFRUIT_COMPANY_ID,
74-
struct.calcsize("<H" + _DATA_FMT_ENC_DATA),
75-
RPS_ENC_DATA_ID
69+
# match_prefixes tuple replaces deprecated prefix
70+
# comma for 1 element is very important!
71+
match_prefixes = (
72+
struct.pack(
73+
_PREFIX_FMT,
74+
MANUFACTURING_DATA_ADT,
75+
ADAFRUIT_COMPANY_ID,
76+
struct.calcsize("<H" + _DATA_FMT_ENC_DATA),
77+
RPS_ENC_DATA_ID
78+
),
7679
)
7780
manufacturer_data = LazyObjectField(
7881
ManufacturerData,
@@ -111,20 +114,19 @@ class RpsKeyDataAdvertisement(Advertisement):
111114
"""
112115
flags = None
113116

114-
_PREFIX_FMT = "<B" "BHBH"
117+
_PREFIX_FMT = "<BHBH"
115118
_DATA_FMT_KEY_DATA = "8s"
116119

117-
# prefix appears to be used to determine whether an incoming
118-
# packet matches this class
119-
# The second struct.calcsize needs to include the _DATA_FMT for some
120-
# reason I either don't know or can't remember
121-
prefix = struct.pack(
122-
_PREFIX_FMT,
123-
struct.calcsize(_PREFIX_FMT) - 1,
124-
MANUFACTURING_DATA_ADT,
125-
ADAFRUIT_COMPANY_ID,
126-
struct.calcsize("<H" + _DATA_FMT_KEY_DATA),
127-
RPS_KEY_DATA_ID
120+
# match_prefixes tuple replaces deprecated prefix
121+
# comma for 1 element is very important!
122+
match_prefixes = (
123+
struct.pack(
124+
_PREFIX_FMT,
125+
MANUFACTURING_DATA_ADT,
126+
ADAFRUIT_COMPANY_ID,
127+
struct.calcsize("<H" + _DATA_FMT_KEY_DATA),
128+
RPS_KEY_DATA_ID
129+
),
128130
)
129131
manufacturer_data = LazyObjectField(
130132
ManufacturerData,
@@ -164,15 +166,18 @@ class RpsRoundEndAdvertisement(Advertisement):
164166
"""
165167
flags = None
166168

167-
_PREFIX_FMT = "<B" "BHBH"
168-
169-
prefix = struct.pack(
170-
_PREFIX_FMT,
171-
struct.calcsize(_PREFIX_FMT) - 1,
172-
MANUFACTURING_DATA_ADT,
173-
ADAFRUIT_COMPANY_ID,
174-
struct.calcsize("<H" + _DATA_FMT_ROUND),
175-
RPS_ROUND_ID
169+
_PREFIX_FMT = "<BHBH"
170+
171+
# match_prefixes tuple replaces deprecated prefix
172+
# comma for 1 element is very important!
173+
match_prefixes = (
174+
struct.pack(
175+
_PREFIX_FMT,
176+
MANUFACTURING_DATA_ADT,
177+
ADAFRUIT_COMPANY_ID,
178+
struct.calcsize("<H" + _DATA_FMT_ROUND),
179+
RPS_ROUND_ID
180+
),
176181
)
177182
manufacturer_data = LazyObjectField(
178183
ManufacturerData,
@@ -207,18 +212,19 @@ class JoinGameAdvertisement(Advertisement):
207212
"""
208213
flags = None
209214

210-
_PREFIX_FMT = "<B" "BHBH"
215+
_PREFIX_FMT = "<BHBH"
211216
_DATA_FMT = "8s" # this NUL pads for 8s if necessary
212217

213-
prefix = struct.pack(
214-
_PREFIX_FMT,
215-
struct.calcsize(_PREFIX_FMT) - 1,
216-
MANUFACTURING_DATA_ADT,
217-
ADAFRUIT_COMPANY_ID,
218-
##struct.calcsize("<H" + _SEQ_FMT + _DATA_FMT),
219-
struct.calcsize("<H" + _DATA_FMT),
220-
##struct.calcsize("<H"),
221-
GM_JOIN_ID
218+
# match_prefixes tuple replaces deprecated prefix
219+
# comma for 1 element is very important!
220+
match_prefixes = (
221+
struct.pack(
222+
_PREFIX_FMT,
223+
MANUFACTURING_DATA_ADT,
224+
ADAFRUIT_COMPANY_ID,
225+
struct.calcsize("<H" + _DATA_FMT),
226+
GM_JOIN_ID
227+
),
222228
)
223229
manufacturer_data = LazyObjectField(
224230
ManufacturerData,

CLUE_Rock_Paper_Scissors/advanced/rps_comms.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ def startScan(radio, send_ad, send_advertising,
133133
# If using application Advertisement type matching then
134134
# check the Advertisement's prefix and continue for loop if it
135135
# does not match
136+
adv = None
137+
d_print(5, "RXed RTA", match_locally, addr_text, repr(adv_ss))
136138
if match_locally:
137-
d_print(5, "RXed RTA", addr_text, repr(adv_ss))
138139
adv_ss_as_bytes = adafruit_ble.advertising.standard.encode_data(adv_ss.data_dict)
139-
adv = None
140140
for cls in rx_ad_classes:
141141
prefix = cls.prefix
142142
# This DOES NOT IMPLEMENT PROPER MATCHING
@@ -151,15 +151,17 @@ def startScan(radio, send_ad, send_advertising,
151151
d_print(4, "RXed mm RTA", addr_text, adv)
152152
break
153153

154-
if adv is None:
155-
if endscan_cb is not None and endscan_cb(addr_text, adv_ss.address, adv_ss):
156-
complete = True
157-
break
158-
else:
159-
continue
160154
else:
161-
adv = adv_ss
162-
d_print(4, "RXed RTA", addr_text, adv)
155+
if any(isinstance(adv_ss, cls) for cls in rx_ad_classes):
156+
adv = adv_ss
157+
158+
# Continue loop after an endscan callback if ad is not of interest
159+
if adv is None: # this means adv was not in rx_ad_classes
160+
if endscan_cb is not None and endscan_cb(addr_text, adv_ss.address, adv_ss):
161+
complete = True
162+
break
163+
else:
164+
continue
163165

164166
# Must be a match if this is reached
165167
matching_ads += 1
@@ -233,7 +235,7 @@ def broadcastAndReceive(radio,
233235
minimum_rssi=-90,
234236
receive_n=0,
235237
seq_tx=None,
236-
match_locally=True,
238+
match_locally=False,
237239
scan_response_request=False,
238240
ad_cb=None,
239241
ads_by_addr={},
@@ -274,6 +276,8 @@ def broadcastAndReceive(radio,
274276

275277
if match_locally:
276278
ss_rx_ad_classes = (Advertisement,)
279+
elif scan_response_request:
280+
ss_rx_ad_classes = rx_ad_classes + (Advertisement,)
277281
else:
278282
ss_rx_ad_classes = rx_ad_classes
279283

CLUE_Rock_Paper_Scissors/simple/clue-simple-rpsgame.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# clue-simple-rpsgame v1.1
1+
# clue-simple-rpsgame v1.2
22
# CircuitPython rock paper scissors game over Bluetooth LE
33

44
# Tested with CLUE and Circuit Playground Bluefruit Alpha with TFT Gizmo
@@ -233,16 +233,19 @@ class RpsAdvertisement(Advertisement):
233233

234234
flags = None
235235

236-
_PREFIX_FMT = "<B" "BHBH"
236+
_PREFIX_FMT = "<BHBH"
237237
_DATA_FMT = "8s" # this NUL pads if necessary
238238

239-
prefix = struct.pack(
240-
_PREFIX_FMT,
241-
struct.calcsize(_PREFIX_FMT) - 1,
242-
MANUFACTURING_DATA_ADT,
243-
ADAFRUIT_COMPANY_ID,
244-
struct.calcsize("<H" + _DATA_FMT),
245-
RPS_DATA_ID
239+
# match_prefixes tuple replaces deprecated prefix
240+
# comma for 1 element is very important!
241+
match_prefixes = (
242+
struct.pack(
243+
_PREFIX_FMT,
244+
MANUFACTURING_DATA_ADT,
245+
ADAFRUIT_COMPANY_ID,
246+
struct.calcsize("<H" + _DATA_FMT),
247+
RPS_DATA_ID
248+
),
246249
)
247250
manufacturer_data = LazyObjectField(
248251
ManufacturerData,

0 commit comments

Comments
 (0)