Skip to content

Commit

Permalink
api v3.6.2 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
scaryghost committed Nov 28, 2018
1 parent d0b274b commit 0a8f237
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 36 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ android {
defaultConfig {
minSdkVersion 18
targetSdkVersion 28
versionCode 58
versionName "3.6.0"
versionCode 59
versionName "3.6.2"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ public Data createMessage(boolean logData, final MetaWearBoardPrivate mwPrivate,
fuser = dpModules.activeProcessors.get(fuser.editor.source.input.eventConfig[2]);
}

DataTypeBase source = fuser.editor.source.input == null ? fuser.editor.source : fuser.editor.source.input;
int offset = 0;
final Data[] unwrappedData = new Data[fuser.editor.config.length + 1];
unwrappedData[0] = fuser.editor.source.input.createMessage(logData, mwPrivate, data, timestamp, mapper);
offset+= fuser.editor.source.input.attributes.length();
unwrappedData[0] = source.createMessage(logData, mwPrivate, data, timestamp, mapper);
offset+= source.attributes.length();

for(int i = 2; i < fuser.editor.config.length; i++) {
DataProcessorImpl.Processor value = dpModules.activeProcessors.get(fuser.editor.config[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public Task<List<File>> downloadFirmwareUpdateFilesAsync(final String version) {
JSONObject builds = models.getJSONObject(persist.boardInfo.modelNumber);

ModuleInfo mInfo = persist.boardInfo.moduleInfo.get(Constant.Module.SETTINGS);
String build = mInfo != null && mInfo.extra.length >= 2 ? String.format(Locale.US, "%d", ((short) mInfo.extra[1] & 0xff)) : DEFAULT_FIRMWARE_BUILD;
String build = mInfo != null && mInfo.extra.length >= 2 && mInfo.extra[1] != 0 ? String.format(Locale.US, "%d", ((short) mInfo.extra[1] & 0xff)) : DEFAULT_FIRMWARE_BUILD;

Pair<JSONObject, Version> result = findFirmwareAttrs(builds.getJSONObject(build), version);

Expand Down Expand Up @@ -665,7 +665,7 @@ public Task<String> findLatestAvailableFirmwareAsync() {
JSONObject builds = models.getJSONObject(persist.boardInfo.modelNumber);

ModuleInfo info = persist.boardInfo.moduleInfo.get(Constant.Module.SETTINGS);
String build = info.extra.length >= 2 ? String.format(Locale.US, "%d", info.extra[1]) : DEFAULT_FIRMWARE_BUILD;
String build = info != null && info.extra.length >= 2 && info.extra[1] != 0 ? String.format(Locale.US, "%d", info.extra[1]) : DEFAULT_FIRMWARE_BUILD;

Pair<JSONObject, Version> result = findFirmwareAttrs(builds.getJSONObject(build), null);
return Task.forResult(result.second.compareTo(persist.boardInfo.firmware) > 0 ? result.second.toString() : null);
Expand Down
75 changes: 53 additions & 22 deletions library/src/main/java/com/mbientlab/metawear/impl/LoggingImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.concurrent.atomic.AtomicReference;

import bolts.Capture;
import bolts.Continuation;
import bolts.Task;
import bolts.TaskCompletionSource;

Expand Down Expand Up @@ -509,6 +510,57 @@ private Task<Collection<DataLogger>> queryActiveLoggersInnerAsync(final byte id)
final Capture<byte[]> response = new Capture<>();
final DataProcessorImpl dataprocessor = (DataProcessorImpl) mwPrivate.getModules().get(DataProcessor.class);

final Deque<Byte> fuserIds = new LinkedList<>();
final Deque<Pair<DataTypeBase, ProcessorEntry>> fuserConfigs = new LinkedList<>();
final Capture<Continuation<Deque<ProcessorEntry>, Task<DataTypeBase>>> onProcessorSynced = new Capture<>();
onProcessorSynced.set(task -> {
Deque<ProcessorEntry> result = task.getResult();
ProcessorEntry first = result.peek();
DataTypeBase type = guessLogSource(mwPrivate.getDataTypes(), new Tuple3<>(first.source[0], first.source[1], first.source[2]), first.offset, first.length);

byte revision = mwPrivate.lookupModuleInfo(DATA_PROCESSOR).revision;
while(!result.isEmpty()) {
ProcessorEntry current = result.poll();
if (current.config[0] == DataProcessorConfig.Fuser.ID) {
for(int i = 0; i < (current.config[1] & 0x1f); i++) {
fuserIds.push(current.config[i + 2]);
}
fuserConfigs.push(new Pair<>(type, current));;
} else {
DataProcessorConfig config = DataProcessorConfig.from(mwPrivate.getFirmwareVersion(), revision, current.config);
Pair<? extends DataTypeBase, ? extends DataTypeBase> next = type.dataProcessorTransform(config,
(DataProcessorImpl) mwPrivate.getModules().get(DataProcessor.class));

next.first.eventConfig[2] = current.id;
if (next.second != null) {
next.second.eventConfig[2] = current.id;
}
dataprocessor.addProcessor(current.id, next.second, type, config);
type = next.first;
}
}

if (fuserIds.size() == 0) {
while(fuserConfigs.size() != 0) {
Pair<DataTypeBase, ProcessorEntry> top = fuserConfigs.poll();
DataProcessorConfig config = DataProcessorConfig.from(mwPrivate.getFirmwareVersion(), revision, top.second.config);
Pair<? extends DataTypeBase, ? extends DataTypeBase> next = top.first.dataProcessorTransform(config,
(DataProcessorImpl) mwPrivate.getModules().get(DataProcessor.class));

next.first.eventConfig[2] = top.second.id;
if (next.second != null) {
next.second.eventConfig[2] = top.second.id;
}
dataprocessor.addProcessor(top.second.id, next.second, top.first, config);

type = next.first;
}
return Task.forResult(type);
} else {
return dataprocessor.pullChainAsync(fuserIds.poll()).onSuccessTask(onProcessorSynced.get());
}
});

return syncLoggerConfigTask.execute("Did not receive logger config for id=" + id + " within %dms", Constant.RESPONSE_TIMEOUT,
() -> mwPrivate.sendCommand(new byte[] {0x0b, Util.setRead(TRIGGER), id})
).onSuccessTask(task -> {
Expand All @@ -518,28 +570,7 @@ private Task<Collection<DataLogger>> queryActiveLoggersInnerAsync(final byte id)
byte length = (byte) (((response.get()[5] >> 5) & 0x3) + 1);

if (response.get()[2] == DATA_PROCESSOR.id && (response.get()[3] == DataProcessorImpl.NOTIFY || Util.clearRead(response.get()[3]) == DataProcessorImpl.STATE)) {
return dataprocessor.pullChainAsync(response.get()[4]).onSuccessTask(task13 -> {
Deque<ProcessorEntry> result = task13.getResult();
ProcessorEntry first = result.peek();
DataTypeBase type = guessLogSource(mwPrivate.getDataTypes(), new Tuple3<>(first.source[0], first.source[1], first.source[2]),
first.offset, first.length);

byte revision = mwPrivate.lookupModuleInfo(DATA_PROCESSOR).revision;
while(!result.isEmpty()) {
ProcessorEntry current = result.poll();
DataProcessorConfig config = DataProcessorConfig.from(mwPrivate.getFirmwareVersion(), revision, current.config);
Pair<? extends DataTypeBase, ? extends DataTypeBase> next = type.dataProcessorTransform(config,
(DataProcessorImpl) mwPrivate.getModules().get(DataProcessor.class));

next.first.eventConfig[2] = current.id;
if (next.second != null) {
next.second.eventConfig[2] = current.id;
}
dataprocessor.addProcessor(current.id, next.second, type, config);
type = next.first;
}
return Task.forResult(type);
});
return dataprocessor.pullChainAsync(response.get()[4]).onSuccessTask(onProcessorSynced.get());
} else {
return Task.forResult(guessLogSource(mwPrivate.getDataTypes(), new Tuple3<>(response.get()[2], response.get()[3], response.get()[4]), offset.get(), length));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ private static AnonymousRoute[] retrieveLoggers(MetaWearBoard mwBoard) throws Ex

static class TestBase extends UnitTestBase {
TestBase() {
this((byte) 0x8, (byte) 0x3);
}

TestBase(byte accRange, byte gyroRange) {
junitPlatform.boardInfo = new MetaWearBoardInfo(AccelerometerBmi160.class, GyroBmi160.class, MagnetometerBmm150.class, SensorFusionBosch.class);

junitPlatform.addCustomResponse(new byte[]{0x3, (byte) 0x83},
new byte[]{0x03, (byte) 0x83, 40, 8});
new byte[]{0x03, (byte) 0x83, 40, accRange});
junitPlatform.addCustomResponse(new byte[]{0x13, (byte) 0x83},
new byte[]{0x13, (byte) 0x83, 40, 3});
new byte[]{0x13, (byte) 0x83, 40, gyroRange});
junitPlatform.addCustomResponse(new byte[]{0x19, (byte) 0x82},
new byte[]{0x19, (byte) 0x82, 0x1, 0xf});
}
Expand Down Expand Up @@ -461,4 +465,86 @@ public void CheckIdentifier() throws Exception {
assertEquals("step-counter", retrieveLoggers(mwBoard)[0].identifier());
}
}
public static class TestFuser extends TestBase {
public TestFuser() {
super((byte) 0x03, (byte) 0x04);

junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x00},
new byte[]{0x0b, (byte) 0x82, 0x09, 0x03, 0x01, 0x60});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x01},
new byte[]{0x0b, (byte) 0x82, 0x09, 0x03, 0x01, 0x64});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x02},
new byte[]{0x0b, (byte) 0x82, 0x09, 0x03, 0x01, 0x68});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x03},
new byte[]{0x0b, (byte) 0x82});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x04},
new byte[]{0x0b, (byte) 0x82});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x05},
new byte[]{0x0b, (byte) 0x82});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x06},
new byte[]{0x0b, (byte) 0x82});
junitPlatform.addCustomResponse(new byte[]{0xb, (byte) 0x82, 0x07},
new byte[]{0x0b, (byte) 0x82});
junitPlatform.addCustomResponse(new byte[]{0x9, (byte) 0x82, 0x00},
new byte[]{0x09, (byte) 0x82, 0x13, 0x05, (byte) 0xff, (byte) 0xa0, 0x0f, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xe9, (byte) 0xff });
junitPlatform.addCustomResponse(new byte[]{0x9, (byte) 0x82, 0x01},
new byte[]{0x09, (byte) 0x82, 0x03, 0x04, (byte) 0xff, (byte) 0xa0, 0x1b, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xe9, (byte) 0xff });
}

@Test
public void syncLoggers() throws Exception {
connectToBoard();
assertEquals(1, retrieveLoggers(mwBoard).length);
}

@Test
public void checkScheme() throws Exception {
connectToBoard();

AnonymousRoute[] result = retrieveLoggers(mwBoard);

assertEquals("acceleration:fuser?id=1", result[0].identifier());
}

@Test
public void handleDownload() throws Exception {
connectToBoard();

Task<AnonymousRoute[]> task = mwBoard.createAnonymousRoutesAsync();
task.waitForCompletion();

if (task.isFaulted()) {
throw task.getError();
}

AngularVelocity[] expectedGyro= new AngularVelocity[] {
new AngularVelocity(Float.intBitsToFloat(0x3E84AED4), Float.intBitsToFloat(0x3EEC18FA), Float.intBitsToFloat(0x3D3B512C)),
new AngularVelocity(Float.intBitsToFloat(0x3E9C18FA), Float.intBitsToFloat(0x3EFDA896), Float.intBitsToFloat(0x3DE2576A))
};
Acceleration[] expectedAcc= new Acceleration[] {
new Acceleration(Float.intBitsToFloat(0xBCBD0000), Float.intBitsToFloat(0x3CA48000), Float.intBitsToFloat(0x3F82DA00)),
new Acceleration(Float.intBitsToFloat(0xBC4A0000), Float.intBitsToFloat(0x3C9A8000), Float.intBitsToFloat(0x3F840400))
};

final AngularVelocity[] actualGyro = new AngularVelocity[2];
final Acceleration[] actualAcc = new Acceleration[2];
task.getResult()[0].subscribe(new Subscriber() {
int i = 0;
@Override
public void apply(Data data, Object... env) {
Data[] values = data.value(Data[].class);
actualAcc[i] = values[0].value(Acceleration.class);
actualGyro[i] = values[1].value(AngularVelocity.class);
i++;
}
});

sendMockResponse(new byte[] {0x0b, 0x07, (byte) 0xc0, (byte) 0xac, 0x1b, 0x00, 0x00, (byte) 0x86, (byte) 0xfe, 0x49, 0x01, (byte) 0xc1, (byte) 0xac, 0x1b, 0x00, 0x00, 0x6d, 0x41, 0x44, 0x00});
sendMockResponse(new byte[] {0x0b, 0x07, (byte) 0xc2, (byte) 0xac, 0x1b, 0x00, 0x00, 0x79, 0x00, 0x0c, 0x00, (byte) 0xc0, (byte) 0xc8, 0x1b, 0x00, 0x00, 0x36, (byte) 0xff, 0x35, 0x01});
sendMockResponse(new byte[] {0x0b, 0x07, (byte) 0xc1, (byte) 0xc8, 0x1b, 0x00, 0x00, 0x02, 0x42, 0x50, 0x00, (byte) 0xc2, (byte) 0xc8, 0x1b, 0x00, 0x00, (byte) 0x82, 0x00, 0x1d, 0x00});

assertArrayEquals(expectedAcc, actualAcc);
assertArrayEquals(expectedGyro, actualGyro);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeoutException;

import bolts.AggregateException;
Expand Down Expand Up @@ -526,24 +527,39 @@ public void updateToLatest() throws Exception {
}
}

@RunWith(Parameterized.class)
public static class TestFirmwareBuildCheck extends UnitTestBase {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{ (byte) 0x80, "128" },
{ (byte) 0x00, "vanilla" }
});
}

@Parameter
public byte revision;

@Parameter(value = 1)
public String buildName;

@Before
public void setup() throws Exception {
public void setup() {
junitPlatform.boardInfo = MetaWearBoardInfo.MOTION_R;
junitPlatform.addCustomModuleInfo(new byte[] {0x11, (byte) 0x80, 0x00, 0x08, 0x03, (byte) 0x80});
junitPlatform.firmware = "1.4.2";
connectToBoard();
}


@Test
public void checkFilename() throws Exception {
junitPlatform.addCustomModuleInfo(new byte[] {0x11, (byte) 0x80, 0x00, 0x08, 0x03, revision});
connectToBoard();

Task<List<File>> filesTask = mwBoard.downloadFirmwareUpdateFilesAsync();
filesTask.waitForCompletion();

// Firmware v1.4.0+ can just upload the next firmware image
final String[] expected = new String[] {
"0.1_5_128_1.4.4_firmware.zip",
String.format(Locale.US, "0.1_5_%s_1.5.0_firmware.zip", buildName)
};
assertArrayEquals(expected, fileToNames(filesTask.getResult()));
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/test/res/info2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"0.1" : {
"5" : {
"128" : {
"1.4.4" : {
"1.5.0" : {
"filename" : "firmware.zip",
"min-android-version" : "3.5.0",
"min-ios-version" : "3.1.3",
Expand Down

0 comments on commit 0a8f237

Please sign in to comment.