Skip to content

Commit

Permalink
Update SetupPayload to include if Discriminator is short (#23304)
Browse files Browse the repository at this point in the history
* Update SetupPayload to include if Discriminator is short

The device Discriminator is a 12-bit unsigned integer. For
machine-readable formats, the full 12-bit Discriminator is used. For the
Manual Pairing Code, only the upper 4 bits out of the 12-bit
Discriminator are used.

Also adding @deprecated tags to the pairDevice APIs in
MTRDeviceController

* Restyled by google-java-format

* Adding flags to ignore deprecated warnings around pairDevice
implementations

* Restyled by whitespace

* Restyled by clang-format

* Removing deprecation messages in MTR code

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Sep 12, 2023
1 parent 1f775ac commit 1732884
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/setup_payload/java/SetupPayloadParser-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,17 @@ jobject TransformSetupPayload(JNIEnv * env, SetupPayload & payload)
jfieldID productId = env->GetFieldID(setupPayloadClass, "productId", "I");
jfieldID commissioningFlow = env->GetFieldID(setupPayloadClass, "commissioningFlow", "I");
jfieldID discriminator = env->GetFieldID(setupPayloadClass, "discriminator", "I");
jfieldID hasShortDiscriminator = env->GetFieldID(setupPayloadClass, "hasShortDiscriminator", "Z");
jfieldID setUpPinCode = env->GetFieldID(setupPayloadClass, "setupPinCode", "J");
jfieldID discoveryCapabilities = env->GetFieldID(setupPayloadClass, "discoveryCapabilities", "Ljava/util/Set;");

env->SetIntField(setupPayload, version, payload.version);
env->SetIntField(setupPayload, vendorId, payload.vendorID);
env->SetIntField(setupPayload, productId, payload.productID);
env->SetIntField(setupPayload, commissioningFlow, static_cast<int>(payload.commissioningFlow));
// TODO: The API we have here does not handle short discriminators in any
// sane way. Just do what we used to do, which is pretend that a short
// discriminator is actually a long discriminator with the low bits all 0.
uint16_t discriminatorValue;
if (payload.discriminator.IsShortDiscriminator())
bool isShortDiscriminator = payload.discriminator.IsShortDiscriminator();
if (isShortDiscriminator)
{
discriminatorValue = static_cast<uint16_t>(payload.discriminator.GetShortValue())
<< (SetupDiscriminator::kLongBits - SetupDiscriminator::kShortBits);
Expand All @@ -134,6 +133,7 @@ jobject TransformSetupPayload(JNIEnv * env, SetupPayload & payload)
discriminatorValue = payload.discriminator.GetLongValue();
}
env->SetIntField(setupPayload, discriminator, discriminatorValue);
env->SetBooleanField(setupPayload, hasShortDiscriminator, isShortDiscriminator);
env->SetLongField(setupPayload, setUpPinCode, payload.setUpPINCode);

env->SetObjectField(setupPayload, discoveryCapabilities,
Expand Down Expand Up @@ -275,19 +275,28 @@ void TransformSetupPayloadFromJobject(JNIEnv * env, jobject jPayload, SetupPaylo
{
jclass setupPayloadClass = env->FindClass("chip/setuppayload/SetupPayload");

jfieldID version = env->GetFieldID(setupPayloadClass, "version", "I");
jfieldID vendorId = env->GetFieldID(setupPayloadClass, "vendorId", "I");
jfieldID productId = env->GetFieldID(setupPayloadClass, "productId", "I");
jfieldID commissioningFlow = env->GetFieldID(setupPayloadClass, "commissioningFlow", "I");
jfieldID discriminator = env->GetFieldID(setupPayloadClass, "discriminator", "I");
jfieldID setUpPinCode = env->GetFieldID(setupPayloadClass, "setupPinCode", "J");
jfieldID discoveryCapabilities = env->GetFieldID(setupPayloadClass, "discoveryCapabilities", "Ljava/util/Set;");

payload.version = env->GetIntField(jPayload, version);
payload.vendorID = env->GetIntField(jPayload, vendorId);
payload.productID = env->GetIntField(jPayload, productId);
payload.commissioningFlow = static_cast<CommissioningFlow>(env->GetIntField(jPayload, commissioningFlow));
payload.discriminator.SetLongValue(env->GetIntField(jPayload, discriminator));
jfieldID version = env->GetFieldID(setupPayloadClass, "version", "I");
jfieldID vendorId = env->GetFieldID(setupPayloadClass, "vendorId", "I");
jfieldID productId = env->GetFieldID(setupPayloadClass, "productId", "I");
jfieldID commissioningFlow = env->GetFieldID(setupPayloadClass, "commissioningFlow", "I");
jfieldID discriminator = env->GetFieldID(setupPayloadClass, "discriminator", "I");
jfieldID hasShortDiscriminatorFieldId = env->GetFieldID(setupPayloadClass, "hasShortDiscriminator", "Z");
jfieldID setUpPinCode = env->GetFieldID(setupPayloadClass, "setupPinCode", "J");
jfieldID discoveryCapabilities = env->GetFieldID(setupPayloadClass, "discoveryCapabilities", "Ljava/util/Set;");

payload.version = env->GetIntField(jPayload, version);
payload.vendorID = env->GetIntField(jPayload, vendorId);
payload.productID = env->GetIntField(jPayload, productId);
payload.commissioningFlow = static_cast<CommissioningFlow>(env->GetIntField(jPayload, commissioningFlow));
jboolean hasShortDiscriminator = env->GetBooleanField(jPayload, hasShortDiscriminatorFieldId);
if (hasShortDiscriminator)
{
payload.discriminator.SetShortValue(env->GetShortField(jPayload, discriminator));
}
else
{
payload.discriminator.SetLongValue(env->GetIntField(jPayload, discriminator));
}
payload.setUpPINCode = static_cast<uint32_t>(env->GetLongField(jPayload, setUpPinCode));

jobject discoveryCapabilitiesObj = env->GetObjectField(jPayload, discoveryCapabilities);
Expand Down
27 changes: 27 additions & 0 deletions src/setup_payload/java/src/chip/setuppayload/SetupPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class SetupPayload {
public Set<DiscoveryCapability> discoveryCapabilities;
/** The CHIP device discriminator: */
public int discriminator;
/**
* If hasShortDiscriminator is true, the discriminator value contains just the high 4 bits of the
* full discriminator. For example, if hasShortDiscriminator is true and discriminator is 0xA,
* then the full discriminator can be anything in the range 0xA00 t0 0xAFF.
*/
public boolean hasShortDiscriminator;
/**
* The CHIP device setup PIN code: setupPINCode SHALL be greater than 0. Also invalid setupPINCode
* is {000000000, 11111111, 22222222, 33333333, 44444444, 55555555, 66666666, 77777777, 88888888,
Expand All @@ -41,12 +47,33 @@ public SetupPayload(
Set<DiscoveryCapability> discoveryCapabilities,
int discriminator,
long setupPinCode) {
this(
version,
vendorId,
productId,
commissioningFlow,
discoveryCapabilities,
discriminator,
false,
setupPinCode);
}

public SetupPayload(
int version,
int vendorId,
int productId,
int commissioningFlow,
Set<DiscoveryCapability> discoveryCapabilities,
int discriminator,
boolean hasShortDiscriminator,
long setupPinCode) {
this.version = version;
this.vendorId = vendorId;
this.productId = productId;
this.commissioningFlow = commissioningFlow;
this.discoveryCapabilities = discoveryCapabilities;
this.discriminator = discriminator;
this.hasShortDiscriminator = hasShortDiscriminator;
this.setupPinCode = setupPinCode;
this.optionalQRCodeInfo = new HashMap<Integer, OptionalQRCodeInfo>();
}
Expand Down

0 comments on commit 1732884

Please sign in to comment.