Skip to content

Commit

Permalink
clean mbb *a bit*
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastGimbus committed Aug 10, 2024
1 parent 973ae1e commit 96ddda4
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions lib/headphones/huawei/mbb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ class MbbUtils {
}

/// Will return exception if anything wrong. Otherwise does nothing.
static Exception? verifyIntegrity(Uint8List payload) {
static void verifyIntegrity(Uint8List payload) {
// 3 magic bytes, 1 length, 1 service, 1 command, 2 checksum
if (payload.length < 3 + 1 + 1 + 1 + 2) {
return Exception("Payload $payload is too short");
throw Exception("Payload $payload is too short");
}
if (!payload.sublist(0, 2).elementsEqual([90, 0]) || payload[3] != 0) {
return Exception("Payload $payload has invalid magic bytes");
final magicBytes = (payload[0], payload[1], payload[3]);
if (magicBytes != (90, 0, 0)) {
throw Exception("Payload $payload has invalid magic bytes");
}
if (payload.length - 6 + 1 != payload[2]) {
return Exception("Length data from $payload doesn't match length byte");
throw Exception("Length data from $payload doesn't match length byte");
}
if (!verifyChecksum(payload)) {
return Exception("Checksum from $payload doesn't match");
throw Exception("Checksum from $payload doesn't match");
}
return null;
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ class MbbCommand {
runtimeType == other.runtimeType &&
serviceId == other.serviceId &&
commandId == other.commandId &&
args.elementsEqual(other.args);
const MapEquality().equals(args, other.args);

@override
int get hashCode => serviceId.hashCode ^ commandId.hashCode ^ args.hashCode;
Expand Down Expand Up @@ -135,10 +135,7 @@ class MbbCommand {
}
final cmds = <MbbCommand>[];
for (final divPay in divided) {
if (verify) {
final e = MbbUtils.verifyIntegrity(divPay);
if (e != null) throw e;
}
if (verify) MbbUtils.verifyIntegrity(divPay);
final serviceId = divPay[4];
final commandId = divPay[5];
final dataBytes = divPay.sublist(6, divPay.length - 2);
Expand Down Expand Up @@ -466,15 +463,3 @@ class Freebuds4iCommands extends GenericHeadphoneCommands {
}
}
}

extension _ListUtils on List {
bool elementsEqual(List other) {
return const ListEquality().equals(this, other);
}
}

extension _MapUtils on Map {
bool elementsEqual(Map other) {
return const MapEquality().equals(this, other);
}
}

0 comments on commit 96ddda4

Please sign in to comment.