From f15aa2bfe1e0f7e7663e28f79808ed4d45d3549f Mon Sep 17 00:00:00 2001 From: Paul DeMarco Date: Fri, 1 Sep 2017 04:13:23 -0400 Subject: [PATCH] Merged in GUID library, this will most likely be replaced by published Uuid library --- lib/flutter_blue.dart | 6 +-- lib/src/guid.dart | 103 ++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 3 -- test/guid_test.dart | 60 ++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 lib/src/guid.dart create mode 100644 test/guid_test.dart diff --git a/lib/flutter_blue.dart b/lib/flutter_blue.dart index f192cb66..d340e5c2 100644 --- a/lib/flutter_blue.dart +++ b/lib/flutter_blue.dart @@ -7,15 +7,15 @@ library flutter_blue; import 'dart:async'; import 'package:flutter/services.dart'; -import 'package:guid/guid.dart'; import 'gen/flutterblue.pb.dart' as protos; import 'package:meta/meta.dart'; -import 'dart:typed_data'; import 'package:collection/collection.dart'; +import 'package:convert/convert.dart'; part 'src/flutter_blue.dart'; part 'src/constants.dart'; part 'src/bluetooth_device.dart'; part 'src/bluetooth_service.dart'; part 'src/bluetooth_characteristic.dart'; -part 'src/bluetooth_descriptor.dart'; \ No newline at end of file +part 'src/bluetooth_descriptor.dart'; +part 'src/guid.dart'; \ No newline at end of file diff --git a/lib/src/guid.dart b/lib/src/guid.dart new file mode 100644 index 00000000..77d9bd79 --- /dev/null +++ b/lib/src/guid.dart @@ -0,0 +1,103 @@ +// Copyright 2017, Paul DeMarco. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +part of flutter_blue; + +class Guid { + + final List _bytes; + final int _hashCode; + + Guid._internal(List bytes) : _bytes = bytes, _hashCode = _calcHashCode(bytes); + + Guid(String input) : this._internal(_fromString(input)); + + Guid.fromMac(String input) : this._internal(_fromMacString(input)); + + Guid.empty(): this._internal(new List.filled(16, 0)); + + static List _fromMacString(input) { + var bytes = new List.filled(16, 0); + + if(input == null){ + throw new ArgumentError("Input was null"); + } + input = input.toLowerCase(); + + final RegExp regex = new RegExp('[0-9a-f]{2}'); + Iterable matches = regex.allMatches(input); + + if(matches.length != 6){ + throw new FormatException("The format is invalid: " + input); + } + + int i = 0; + for(Match match in matches) { + var hexString = input.substring(match.start,match.end); + bytes[i] = hex.decode(hexString)[0]; + i++; + } + + return bytes; + } + + static List _fromString(input) { + var bytes = new List.filled(16, 0); + if(input == null){ + throw new ArgumentError("Input was null"); + } + if(input.length < 32){ + throw new FormatException("The format is invalid"); + } + input = input.toLowerCase(); + + final RegExp regex = new RegExp('[0-9a-f]{2}'); + Iterable matches = regex.allMatches(input); + if(matches.length != 16){ + throw new FormatException("The format is invalid"); + } + int i = 0; + for(Match match in matches) { + var hexString = input.substring(match.start,match.end); + bytes[i] = hex.decode(hexString)[0]; + i++; + } + return bytes; + } + + static int _calcHashCode(List bytes) { + const equality = const ListEquality(); + return equality.hash(bytes); + } + + @override + String toString() { + String one = hex.encode(_bytes.sublist(0,4)); + String two = hex.encode(_bytes.sublist(4,6)); + String three = hex.encode(_bytes.sublist(6,8)); + String four = hex.encode(_bytes.sublist(8,10)); + String five = hex.encode(_bytes.sublist(10,16)); + return "$one-$two-$three-$four-$five"; + } + + String toMac() { + String one = hex.encode(_bytes.sublist(0,1)); + String two = hex.encode(_bytes.sublist(1,2)); + String three = hex.encode(_bytes.sublist(2,3)); + String four = hex.encode(_bytes.sublist(3,4)); + String five = hex.encode(_bytes.sublist(4,5)); + String six = hex.encode(_bytes.sublist(5,6)); + return "$one:$two:$three:$four:$five:$six".toUpperCase(); + } + + List toByteArray() { + return _bytes; + } + + operator ==(other) => + other is Guid && this.hashCode == other.hashCode; + + int get hashCode => _hashCode; + +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index d1c736d6..a1f7c575 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,9 +13,6 @@ dependencies: flutter: sdk: flutter convert: "^2.0.1" - guid: - git: - url: git://github.com/pauldemarco/guid.git protobuf: '^0.5.5' diff --git a/test/guid_test.dart b/test/guid_test.dart new file mode 100644 index 00000000..4abbdb00 --- /dev/null +++ b/test/guid_test.dart @@ -0,0 +1,60 @@ +// Copyright 2017, Paul DeMarco. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:flutter_blue/flutter_blue.dart'; +import 'package:test/test.dart'; + +main() { + group("Guid", (){ + test('equality', (){ + var guid = new Guid("{00002a43-0000-1000-8000-00805f9b34fb}"); + var guid2 = new Guid("00002a43-0000-1000-8000-00805f9b34fb"); + expect(guid, guid2); + + var mac = new Guid.fromMac("01:02:03:04:05:06"); + var mac2 = new Guid.fromMac("01:02:03:04:05:06"); + expect(mac, mac2); + }); + + test('empty()', (){ + var guid = new Guid.empty(); + expect("[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", guid.toByteArray().toString()); + }); + + test('toByteArray()', (){ + var guid = new Guid("{00002a43-0000-1000-8000-00805f9b34fb}"); + expect("[0, 0, 42, 67, 0, 0, 16, 0, 128, 0, 0, 128, 95, 155, 52, 251]", guid.toByteArray().toString()); + }); + + test('toString()', (){ + var guid = new Guid("{00002a43-0000-1000-8000-00805f9b34fb}"); + expect("00002a43-0000-1000-8000-00805f9b34fb", guid.toString()); + }); + + test('fromMac()', (){ + var guid = new Guid.fromMac("24:0A:64:50:A4:67"); + expect("[36, 10, 100, 80, 164, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", guid.toByteArray().toString()); + }); + + test('fromMac()', (){ + var guid = new Guid.fromMac("24:0A:64:50:A4:67"); + expect("24:0A:64:50:A4:67", guid.toMac()); + }); + + test('hashCode', (){ + var guid = new Guid.fromMac("24:0A:64:50:A4:67"); + var guid2 = new Guid.fromMac("24:0A:64:50:A4:67"); + expect(guid.hashCode, guid2.hashCode); + }); + + test('empty() equality', (){ + var guid = new Guid.empty(); + var guid2 = new Guid.empty(); + var guid3 = new Guid.fromMac("24:0A:64:50:A4:67"); + expect(guid == guid2, true); + expect(guid == guid3, false); + }); + }); + +} \ No newline at end of file