-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handling of short discriminator in Linux BLE scan.
A few changes here: 1. Implement a SetupDiscriminator class that commons up logic like "does this discriminator, which might be short or long, match the given long discriminator?" and "extract short discriminator from long discriminator". 2. Change SetupPayload to more clearly indicate whether it's storing a short or long discriminator, instead of storing a short discriminator the same way as a long discriminator with the low bits all 0 and hoping consumers check isShortDiscriminator. 3. Update BLE scanning APIs to take SetupDiscriminator. 4. Fix the Linux and Tizen BLE code to properly handle short discriminators (which used to not match if the long discriminator happened to have the low 8 bits nonzero). 5. Fix the Darwin BLE code to properly handle long discriminators that have 0 low bits. Before this change they used to incorrectly match a long discriminator which had the same 4 high bits but different 8 low bits. Fixes #21160
- Loading branch information
1 parent
b647dfb
commit 39ebc91
Showing
36 changed files
with
301 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file defines the SetupDiscriminator type, which is used by | ||
* low-level code (e.g. BLE) in addition to setup payload code. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <lib/support/CodeUtils.h> | ||
|
||
#include <cstdint> | ||
|
||
namespace chip { | ||
|
||
class SetupDiscriminator | ||
{ | ||
public: | ||
constexpr SetupDiscriminator() : mDiscriminator(0), mIsShortDiscriminator(0) {} | ||
|
||
// See section 5.1.2. QR Code in the Matter specification | ||
static constexpr int kLongBits = 12; | ||
|
||
// See section 5.1.3. Manual Pairing Code in the Matter specification | ||
static constexpr int kShortBits = 4; | ||
|
||
void SetShortValue(uint8_t discriminator) | ||
{ | ||
VerifyOrDie(discriminator == (discriminator & kShortMask)); | ||
mDiscriminator = (discriminator & kShortMask); | ||
mIsShortDiscriminator = true; | ||
} | ||
|
||
void SetLongValue(uint16_t discriminator) | ||
{ | ||
VerifyOrDie(discriminator == (discriminator & kLongMask)); | ||
mDiscriminator = (discriminator & kLongMask); | ||
mIsShortDiscriminator = false; | ||
} | ||
|
||
bool IsShortDiscriminator() const { return mIsShortDiscriminator; } | ||
|
||
uint8_t GetShortValue() const | ||
{ | ||
if (IsShortDiscriminator()) | ||
{ | ||
return static_cast<uint8_t>(mDiscriminator); | ||
} | ||
|
||
return LongToShortValue(mDiscriminator); | ||
} | ||
|
||
uint16_t GetLongValue() const | ||
{ | ||
VerifyOrDie(!IsShortDiscriminator()); | ||
return mDiscriminator; | ||
} | ||
|
||
bool MatchesLongDiscriminator(uint16_t discriminator) const | ||
{ | ||
if (!IsShortDiscriminator()) | ||
{ | ||
return mDiscriminator == discriminator; | ||
} | ||
|
||
return mDiscriminator == LongToShortValue(discriminator); | ||
} | ||
|
||
bool operator==(const SetupDiscriminator & other) const | ||
{ | ||
return mIsShortDiscriminator == other.mIsShortDiscriminator && mDiscriminator == other.mDiscriminator; | ||
} | ||
|
||
private: | ||
static_assert(kLongBits == 12, "Unexpected field length"); | ||
|
||
static constexpr uint16_t kLongMask = (1 << kLongBits) - 1; | ||
static constexpr uint8_t kShortMask = (1 << kShortBits) - 1; | ||
|
||
static uint8_t LongToShortValue(uint16_t longValue) | ||
{ | ||
// Short value consists of the high bits of the long value. | ||
constexpr int kLongToShortShift = kLongBits - kShortBits; | ||
return static_cast<uint8_t>(longValue >> kLongToShortShift); | ||
} | ||
|
||
// If long discriminator, all 12 bits are used. If short discriminator, | ||
// only the low kManualSetupDiscriminatorFieldLengthInBits bits are used, to | ||
// store the value of the short discriminator (which contains only the high | ||
// bits of the complete 12-bit discriminator). | ||
uint16_t mDiscriminator : 12; | ||
uint16_t mIsShortDiscriminator : 1; | ||
}; | ||
|
||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.