-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BLE communication refactoring #7
Open
monday8am
wants to merge
5
commits into
master
Choose a base branch
from
descriptor-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
97aa2c4
Added APDU level fragmenter.
b451031
Custom implementation of PacketFragmenter protocol.
f8163e0
Applied APDU fragmenter in received-sent data.
6208274
Refactored the read-write characteristic handlers.
8df3bf4
Updated to use android-ble-client 0.1.11
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/fidesmo/ble/client/apdu/ApduFragmenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.fidesmo.ble.client.apdu; | ||
|
||
public interface ApduFragmenter { | ||
|
||
byte[][] decode(byte[] encodedApdus); | ||
byte[] encode(byte[][] apdus); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/fidesmo/ble/client/apdu/CustomFragmentationProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.fidesmo.ble.client.apdu; | ||
|
||
import com.fidesmo.ble.client.protocol.PacketFragmenter; | ||
|
||
public interface CustomFragmentationProtocol { | ||
PacketFragmenter fragmenter(int var1, byte[] var2); | ||
|
||
CustomPacketDefragmenter deframenter(); | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/fidesmo/ble/client/apdu/CustomPacketDefragmenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.fidesmo.ble.client.apdu; | ||
|
||
|
||
public interface CustomPacketDefragmenter { | ||
void clear(); | ||
|
||
void append(byte[] buffer); | ||
|
||
void add(byte[] buffer); | ||
|
||
boolean complete(); | ||
|
||
boolean empty(); | ||
|
||
byte[] getBuffer(); | ||
} |
123 changes: 123 additions & 0 deletions
123
app/src/main/java/com/fidesmo/ble/client/apdu/CustomPacketFragmenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.fidesmo.ble.client.apdu; | ||
|
||
|
||
import android.util.Log; | ||
|
||
import com.fidesmo.ble.client.BleUtils; | ||
import com.fidesmo.ble.client.protocol.PacketFragmenter; | ||
import com.fidesmo.ble.client.protocol.SimplePacketFragmenter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class CustomPacketFragmenter implements PacketFragmenter { | ||
public static final int HEADER_SIZE = 4; | ||
private final int maxLength; | ||
private final byte[] buffer; | ||
private final int totalPackets; | ||
private int sentPackets = 0; | ||
|
||
public CustomPacketFragmenter(int maxLength, byte[] buffer) { | ||
if(maxLength <= HEADER_SIZE) { | ||
throw new IllegalArgumentException("MTU size can not be less of equal to header size"); | ||
} else { | ||
this.maxLength = maxLength; | ||
this.buffer = Arrays.copyOf(buffer, buffer.length); | ||
int packetMax = maxLength - HEADER_SIZE; | ||
int carriedLen = Math.max(buffer.length, 1); | ||
this.totalPackets = carriedLen / packetMax + (carriedLen % packetMax > 0?1:0); | ||
} | ||
} | ||
|
||
public byte[] nextFragment() { | ||
int available = this.maxLength - HEADER_SIZE; | ||
int offset = available * this.sentPackets; | ||
int len = Math.min(available, this.buffer.length - offset); | ||
byte[] chunk = new byte[len + HEADER_SIZE]; | ||
BleUtils.packInt2(this.totalPackets, chunk, 0); | ||
BleUtils.packInt2(++this.sentPackets, chunk, HEADER_SIZE/2); | ||
System.arraycopy(this.buffer, offset, chunk, HEADER_SIZE, len); | ||
return chunk; | ||
} | ||
|
||
public boolean hasMoreData() { | ||
return this.sentPackets < this.totalPackets; | ||
} | ||
|
||
public static CustomFragmentationProtocol factory() { | ||
return new CustomPacketFragmenter.Factory(); | ||
} | ||
|
||
private static class Factory implements CustomFragmentationProtocol { | ||
private Factory() { | ||
} | ||
|
||
public PacketFragmenter fragmenter(int mtu, byte[] buffer) { | ||
return new CustomPacketFragmenter(mtu, buffer); | ||
} | ||
|
||
public CustomPacketDefragmenter deframenter() { | ||
return new CustomPacketFragmenter.Builder(); | ||
} | ||
} | ||
|
||
public static class Builder implements CustomPacketDefragmenter { | ||
private int totalNo; | ||
private int packetNo; | ||
|
||
List<byte[]> byteArray; | ||
|
||
private Builder() { | ||
byteArray = new ArrayList<>(); | ||
totalNo = 0; | ||
packetNo = 0; | ||
} | ||
|
||
public void clear(){ | ||
byteArray.clear(); | ||
totalNo = 0; | ||
packetNo = 0; | ||
} | ||
|
||
public void append(byte[] array){ | ||
totalNo = BleUtils.unpackInt2(array, 0); | ||
packetNo = BleUtils.unpackInt2(array, HEADER_SIZE/2); | ||
byte[] buffer = new byte[array.length - HEADER_SIZE]; | ||
System.arraycopy(array, HEADER_SIZE, buffer, 0, buffer.length); | ||
byteArray.add(buffer); | ||
} | ||
|
||
public void add(byte[] array){ | ||
byteArray.add(array); | ||
} | ||
|
||
public byte[] getBuffer(){ | ||
byte[] retArray; | ||
int totalSize = 0; | ||
|
||
for(int i=0; i < byteArray.size();i++){ | ||
totalSize = totalSize + byteArray.get(i).length; | ||
} | ||
|
||
int copuCounter = 0; | ||
if(totalSize > 0) { | ||
retArray = new byte[totalSize]; | ||
for(int ii=0; ii < byteArray.size();ii++){ | ||
byte[] tmpArr = byteArray.get(ii); | ||
System.arraycopy(tmpArr, 0, retArray,copuCounter,tmpArr.length); | ||
copuCounter = copuCounter + tmpArr.length; | ||
} | ||
}else{ | ||
retArray = new byte[]{}; | ||
} | ||
return retArray; | ||
} | ||
|
||
public boolean complete() { | ||
return totalNo == packetNo; | ||
} | ||
|
||
public boolean empty() { return totalNo == 0 && packetNo == 0; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can think on how to merge this
CustomPacketDefragmenter
with original Packet fragmenter/defragmenter interfaces? Some of those methods can be handy there too.But I have some notes about the interface:
add
is not informative, looking at the interface you can't say what it is doing and how it is different from theappend
. Maybe we can rename both to something likeaddPacket
andaddBuffer
(or nay other variant)?getBuffer
and simplifiedcomplete
(which in turn has to becompleted
maybe?) andempty
. IHMO it would be good to choose one naming scheme or appendisCompleted
or removeget
what is better to you.clear
and simply recreate the builder, why do you want to reuse it?