Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Thai Dinh Le authored and Thai Dinh Le committed May 23, 2021
1 parent 68017d4 commit 8b7d5c0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public boolean writeToCharacteristic(String message, String mac) throws IOExcept
}

do {
bytesBuffer.reset();
bytesBuffer.write(flag);
bytesBuffer.write(bytesMsg, i, end);

Expand All @@ -167,12 +168,12 @@ public boolean writeToCharacteristic(String message, String mac) throws IOExcept
notification =
gattServer.notifyCharacteristicChanged(device, characteristic, false);

flag++;
flag = BleUtils.MESSAGE_FRAG;
i += mtu;

if (i + mtu >= bytesMsg.length) {
flag = BleUtils.MESSAGE_END;
end = bytesMsg.length - 1 - i;
end = (bytesMsg.length - 1) - i;
} else {
end = i + mtu;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/src/datalink/ble/ble_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ class BleClient extends ServiceClient {
// Reconstruct the message
String strMessage = Utf8Decoder().convert(Uint8List.fromList(buffer));
MessageAdHoc msg = MessageAdHoc.fromJson(json.decode(strMessage + '}'));
// Reset buffer
buffer.clear();

if (verbose)
log(ServiceClient.TAG, 'Client: message received from ${_device.mac}');

// Notify upper layer of a message received
controller.add(AdHocEvent(MESSAGE_RECEIVED, msg));

// Reset buffer
buffer.clear();
}
});

Expand Down Expand Up @@ -164,7 +163,7 @@ class BleClient extends ServiceClient {

Future.delayed(Duration(microseconds: UINT8_SIZE));

flag++;
flag = MESSAGE_FRAG;
i += mtu;

if (i + mtu >= msg.length) {
Expand Down
2 changes: 0 additions & 2 deletions lib/src/datalink/ble/ble_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ class BleServer extends ServiceServer {
MessageAdHoc message =
MessageAdHoc.fromJson(json.decode(Utf8Decoder().convert(bytes) + '}'));

print(message);

// Update the header of the message
if (message.header.mac == null || message.header.mac!.compareTo('') == 0) {
String uuid =
Expand Down
12 changes: 8 additions & 4 deletions lib/src/network/datalinkmanager/wrapper_ble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,18 @@ class WrapperBle extends WrapperNetwork {
List<dynamic> data = event.payload as List<dynamic>;
String mac = data[0] as String;
String uuid = data[1] as String;
int type = data[2] as int;
if (type == SERVER)
int serviceType = data[2] as int;
if (serviceType == SERVER)
break;

// Store remote node's NetworkManager
mapAddrNetwork.putIfAbsent(
uuid, () => NetworkManager(
(MessageAdHoc? msg) async => (service as ServiceClient).send(msg!),
(MessageAdHoc msg) async {
msg.header.address = _ownBleUUID;
msg.header.deviceType = BLE;
(service as ServiceClient).send(msg);
},
() => (service as ServiceClient).disconnect()
)
);
Expand Down Expand Up @@ -429,7 +433,7 @@ class WrapperBle extends WrapperNetwork {
label: header.label,
name: header.name,
mac: header.mac,
type: type
type: header.deviceType!
);

// Notify upper layer of a remote connection closed
Expand Down
15 changes: 14 additions & 1 deletion lib/src/network/datalinkmanager/wrapper_network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ abstract class WrapperNetwork {
late Set<String?> setFloodEvents;
late HashSet<AdHocDevice?> setRemoteDevices;

/// Creates a [WrapperNetwork] object.
///
/// The debug/verbose mode is set if [verbose] is true.
///
/// This object is configured according to [config], which contains specific
/// configurations.
///
/// This object maps a MAC address entry ([String]) to an [AdHocDevice] object
/// into [mapMacDevices].
WrapperNetwork(
this.verbose, Config config, HashMap<String?, AdHocDevice?> mapMacDevices,
) {
Expand All @@ -65,6 +74,9 @@ abstract class WrapperNetwork {

/*------------------------------Getters & Setters-----------------------------*/

/// Gets the direct neighbours of this node.
///
/// Returns a list of [AdHocDevice], which are direct neighbors of this node.
List<AdHocDevice> get directNeighbors {
List<AdHocDevice> devices = List.empty(growable: true);
for (String? macAddress in neighbors.labelMac.values)
Expand All @@ -73,6 +85,7 @@ abstract class WrapperNetwork {
return devices;
}

/// Returns a [Stream] of [AdHocEvent] events of lower layers.
Stream<AdHocEvent> get eventStream => controller.stream;

/*------------------------------Abstract methods------------------------------*/
Expand Down Expand Up @@ -155,7 +168,7 @@ abstract class WrapperNetwork {
address: header.address,
name: header.name,
mac: header.mac,
type: type
type: header.deviceType!
);

mapMacDevices.putIfAbsent(header.mac!, () => device);
Expand Down

0 comments on commit 8b7d5c0

Please sign in to comment.