Skip to content

Commit

Permalink
Merged in GUID library, this will most likely be replaced by publishe…
Browse files Browse the repository at this point in the history
…d Uuid library
  • Loading branch information
pauldemarco committed Sep 1, 2017
1 parent 2859b78 commit f15aa2b
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/flutter_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
part 'src/bluetooth_descriptor.dart';
part 'src/guid.dart';
103 changes: 103 additions & 0 deletions lib/src/guid.dart
Original file line number Diff line number Diff line change
@@ -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<int> _bytes;
final int _hashCode;

Guid._internal(List<int> 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<int> _fromMacString(input) {
var bytes = new List<int>.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<Match> 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<int> _fromString(input) {
var bytes = new List<int>.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<Match> 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<int> bytes) {
const equality = const ListEquality<int>();
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<int> toByteArray() {
return _bytes;
}

operator ==(other) =>
other is Guid && this.hashCode == other.hashCode;

int get hashCode => _hashCode;

}
3 changes: 0 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down
60 changes: 60 additions & 0 deletions test/guid_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});

}

0 comments on commit f15aa2b

Please sign in to comment.