Skip to content

Commit

Permalink
Attempted fix of state management during errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Schiffli committed Jan 19, 2018
1 parent 1254b54 commit a99765c
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 38 deletions.
64 changes: 39 additions & 25 deletions MetaWear/Classes/Core/MBLEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,24 @@ - (nonnull BFTask *)programCommandsToRunOnEventAsync:(nonnull MBLVoidHandler)blo
}

[device incrementCount];
return [[[[[BFTask taskFromMetaWearWithBlock:^id{
return [[BFTask taskFromMetaWearWithBlock:^id{
if (self.hasCommandsImpl) {
NSError *error = [NSError errorWithDomain:kMBLErrorDomain
code:kMBLErrorOperationInvalid
userInfo:@{NSLocalizedDescriptionKey : @"Cannot call programCommandsToRunOnEventAsync: if it's already programmed. Please call eraseCommandsToRunOnEventAsync first."}];
return [BFTask taskWithError:error];
}
self.hasCommandsImpl = YES;
return nil;
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self initializeAsync];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [device.command programCommandsToRunOnEventAsync:self commands:block];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self activateAsync];
return [[[[self initializeAsync] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [device.command programCommandsToRunOnEventAsync:self commands:block];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self activateAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.hasCommandsImpl = NO;
}
return task;
}];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
[device decrementCount];
return task;
Expand All @@ -139,21 +142,24 @@ - (nonnull BFTask *)eraseCommandsToRunOnEventAsync
}

[device incrementCount];
return [[[[[BFTask taskFromMetaWearWithBlock:^id{
return [[BFTask taskFromMetaWearWithBlock:^id{
if (!self.hasCommandsImpl) {
NSError *error = [NSError errorWithDomain:kMBLErrorDomain
code:kMBLErrorOperationInvalid
userInfo:@{NSLocalizedDescriptionKey : @"Cannot call eraseCommandsToRunOnEventAsync since it's not already programmed. Please call programCommandsToRunOnEventAsync: first."}];
return [BFTask taskWithError:error];
}
self.hasCommandsImpl = NO;
return nil;
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self deactivateAsync];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [device.command eraseCommandsToRunOnEventAsync:self];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self deinitializeAsync];
return [[[[self deactivateAsync] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [device.command eraseCommandsToRunOnEventAsync:self];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self deinitializeAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.hasCommandsImpl = YES;
}
return task;
}];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
[device decrementCount];
return task;
Expand Down Expand Up @@ -181,7 +187,7 @@ - (nonnull BFTask *)startLoggingAsync
}

[device incrementCount];
return [[[[[BFTask taskFromMetaWearWithBlock:^id{
return [[BFTask taskFromMetaWearWithBlock:^id{
if (self.isLoggingImpl) {
NSError *error = [NSError errorWithDomain:kMBLErrorDomain
code:kMBLErrorOperationInvalid
Expand All @@ -191,13 +197,16 @@ - (nonnull BFTask *)startLoggingAsync
self.isLoggingImpl = YES;
// Clear out the logging id's
[self.loggingIds removeAllObjects];
return nil;
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self initializeAsync];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self.module.device.logging startLoggingAsyncEvent:self];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self activateAsync];
return [[[[self initializeAsync] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self.module.device.logging startLoggingAsyncEvent:self];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self activateAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.isLoggingImpl = NO;
}
return task;
}];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
[device decrementCount];
return task;
Expand Down Expand Up @@ -264,13 +273,18 @@ - (BFTask *)downloadLogAndStopLoggingAsync:(BOOL)stopLogging
if (self.isLoggingImpl) {
self.isLoggingImpl = NO;

return [[[[self deactivateAsync] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [[[[[self deactivateAsync] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [device.logging stopLoggingEvent:self];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self deinitializeAsync];
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
// Since log downloads take a while, let's save state here
return [device synchronizeAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.isLoggingImpl = YES;
}
return task;
}];
}
return nil;
Expand Down
2 changes: 1 addition & 1 deletion MetaWear/Classes/Core/MBLMetaWear.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ @interface MBLMetaWear ()

@property (nonatomic, nullable) id<MBLRestorable> configuration;

@property (nonatomic) MBLConnectionState state;
@property (nonatomic) BOOL programedByOtherApp;
@property (nonatomic) MBLMovingAverage *rssiAverager;
//@property (nonatomic, nonnull) NSString *name;
Expand All @@ -120,6 +119,7 @@ @interface MBLMetaWear ()


// Properties from MBLMetaWear+Private.h
@property (nonatomic) MBLConnectionState state;
@property (nonatomic, nonnull) NSUUID *identifier;
@property (nonatomic) NSDictionary *advertisementData;
@property (nonatomic, nullable) NSString *mac;
Expand Down
10 changes: 4 additions & 6 deletions MetaWear/Classes/Core/MBLModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ - (BFTask *)initializeAsync
if (self.initializeCount == 1) {
initializeTask = [[self performAsyncInitialization] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.initializeCount--;
self.initializeCount = 0;
}
return task;
}];
Expand All @@ -200,12 +200,11 @@ - (BFTask *)deinitializeAsync
if (self.initializeCount == 0) {
deinitializeTask = [[self performAsyncDeinitialization] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.initializeCount++;
self.initializeCount = 1;
}
return task;
}];
}
NSAssert(self.initializeCount >= 0, @"init/deinit calls unbalanced.");
self.initializeCount = MAX(self.initializeCount, 0);
return deinitializeTask;
}];
Expand All @@ -218,7 +217,7 @@ - (BFTask *)activateAsync
if (self.activateCount == 1) {
activateTask = [[self performAsyncActivation] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.activateCount--;
self.activateCount = 0;
}
return task;
}];
Expand All @@ -234,12 +233,11 @@ - (BFTask *)deactivateAsync
if (self.activateCount == 0) {
activateTask = [[self performAsyncDeactivation] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.activateCount++;
self.activateCount = 1;
}
return task;
}];
}
NSAssert(self.activateCount >= 0, @"activate/deactivate calls unbalanced.");
self.activateCount = MAX(self.activateCount, 0);
return activateTask;
}];
Expand Down
17 changes: 11 additions & 6 deletions MetaWear/Classes/Core/MBLRegister.m
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ - (BFTask *)startNotificationsWithExecutorAsync:(BFExecutor *)executor withHandl
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self activateAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.isNotifyingImpl = NO;
[self removeNotificationHandlers];
}
[device decrementCount];
return task;
}];
Expand Down Expand Up @@ -378,6 +382,9 @@ - (BFTask *)stopNotificationsAsync
}] continueOnMetaWearWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
return [self deinitializeAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.isNotifyingImpl = YES;
}
[device decrementCount];
return task;
}];
Expand Down Expand Up @@ -501,7 +508,7 @@ - (BFTask *)initializeAsync
return [self performAsyncInitialization];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.initializeCount--;
self.initializeCount = 0;
}
return task;
}];
Expand All @@ -520,12 +527,11 @@ - (BFTask *)deinitializeAsync
return [self.module deinitializeAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.initializeCount++;
self.initializeCount = 1;
}
return task;
}];
}
NSAssert(self.initializeCount >= 0, @"init/deinit calls unbalanced.");
self.initializeCount = MAX(self.initializeCount, 0);
return deinitializeTask;
}];
Expand All @@ -540,7 +546,7 @@ - (BFTask *)activateAsync
return [self.module activateAsync];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.activateCount--;
self.activateCount = 0;
}
return task;
}];
Expand All @@ -558,12 +564,11 @@ - (BFTask *)deactivateAsync
return [self performAsyncDeactivation];
}] continueOnMetaWearWithBlock:^id _Nullable(BFTask * _Nonnull task) {
if (task.faulted) {
self.activateCount++;
self.activateCount = 1;
}
return task;
}];
}
NSAssert(self.activateCount >= 0, @"activate/deactivate calls unbalanced.");
self.activateCount = MAX(self.activateCount, 0);
return activateTask;
}];
Expand Down
1 change: 1 addition & 0 deletions MetaWear/Internal/Core/MBLMetaWear+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef void (^MBLSimulationHandler)(uint8_t module, uint8_t opcode, NSData *par

// Properties that certain privileged people need access too
@interface MBLMetaWear (Private) <MBLBluetoothPeripheralDelegate>
@property (nonatomic) MBLConnectionState state;
@property (nonatomic, nonnull) NSUUID *identifier;
@property (nonatomic) NSDictionary *advertisementData;
@property (nonatomic, nullable) NSString *mac;
Expand Down
8 changes: 8 additions & 0 deletions MetaWear/MetaWear.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
400655CE1D9A479A0066AB54 /* MBLConnectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 400655CC1D9A46FF0066AB54 /* MBLConnectionTests.m */; };
400655D11D9A67FD0066AB54 /* MBLMetaWearTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 400655D01D9A67B60066AB54 /* MBLMetaWearTests.m */; };
400655D21D9A67FD0066AB54 /* MBLMetaWearTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 400655D01D9A67B60066AB54 /* MBLMetaWearTests.m */; };
4009E36C1FE1B8C7009509CB /* MBLRegisterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4009E36B1FE1B8C7009509CB /* MBLRegisterTests.m */; };
4009E36D1FE1B8C7009509CB /* MBLRegisterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4009E36B1FE1B8C7009509CB /* MBLRegisterTests.m */; };
4009E36E1FE1B8C7009509CB /* MBLRegisterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4009E36B1FE1B8C7009509CB /* MBLRegisterTests.m */; };
403BF7731D9E348000B22B82 /* DFUTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 403BF7721D9E342800B22B82 /* DFUTests.m */; };
40703E281D94B09600F38FF0 /* MBLConverstionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 40703E101D94AE5000F38FF0 /* MBLConverstionTests.m */; };
40703E3A1D94B8CA00F38FF0 /* MBLConverstionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 40703E101D94AE5000F38FF0 /* MBLConverstionTests.m */; };
Expand Down Expand Up @@ -135,6 +138,7 @@
400655C51D9A1DF30066AB54 /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text; name = .travis.yml; path = ../.travis.yml; sourceTree = "<group>"; };
400655CC1D9A46FF0066AB54 /* MBLConnectionTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBLConnectionTests.m; sourceTree = "<group>"; };
400655D01D9A67B60066AB54 /* MBLMetaWearTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBLMetaWearTests.m; sourceTree = "<group>"; };
4009E36B1FE1B8C7009509CB /* MBLRegisterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBLRegisterTests.m; sourceTree = "<group>"; };
400C47D91E8F1CEF00F72568 /* gen_api_reference.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = gen_api_reference.sh; sourceTree = "<group>"; };
403BF7721D9E342800B22B82 /* DFUTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DFUTests.m; sourceTree = "<group>"; };
40703E061D94AE3500F38FF0 /* MetaWearUnitTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "MetaWearUnitTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -410,6 +414,7 @@
40703E141D94AE5000F38FF0 /* MBLMockMetaWearUnitTest.m */,
400655CC1D9A46FF0066AB54 /* MBLConnectionTests.m */,
400655D01D9A67B60066AB54 /* MBLMetaWearTests.m */,
4009E36B1FE1B8C7009509CB /* MBLRegisterTests.m */,
40CBC8951D9B37040078573C /* MBLMetaBootTests.m */,
40703E0F1D94AE5000F38FF0 /* MBLAutomaticTests.m */,
40703E101D94AE5000F38FF0 /* MBLConverstionTests.m */,
Expand Down Expand Up @@ -1500,6 +1505,7 @@
buildActionMask = 2147483647;
files = (
400655C31D9A19720066AB54 /* MBLAutomaticTests.m in Sources */,
4009E36C1FE1B8C7009509CB /* MBLRegisterTests.m in Sources */,
400655CE1D9A479A0066AB54 /* MBLConnectionTests.m in Sources */,
40703E281D94B09600F38FF0 /* MBLConverstionTests.m in Sources */,
400655D11D9A67FD0066AB54 /* MBLMetaWearTests.m in Sources */,
Expand All @@ -1513,6 +1519,7 @@
buildActionMask = 2147483647;
files = (
400655C41D9A19720066AB54 /* MBLAutomaticTests.m in Sources */,
4009E36D1FE1B8C7009509CB /* MBLRegisterTests.m in Sources */,
400655CD1D9A47990066AB54 /* MBLConnectionTests.m in Sources */,
40703E3A1D94B8CA00F38FF0 /* MBLConverstionTests.m in Sources */,
400655D21D9A67FD0066AB54 /* MBLMetaWearTests.m in Sources */,
Expand Down Expand Up @@ -1558,6 +1565,7 @@
buildActionMask = 2147483647;
files = (
40F4C1B81DAF3EBC00DAD8F6 /* MBLConverstionTests.m in Sources */,
4009E36E1FE1B8C7009509CB /* MBLRegisterTests.m in Sources */,
407820131D9DD5EB00FF5134 /* MBLMockMetaWearUnitTest.m in Sources */,
40F4C1B51DAF3EB100DAD8F6 /* MBLMetaWearTests.m in Sources */,
40F4C1B61DAF3EB600DAD8F6 /* MBLMetaBootTests.m in Sources */,
Expand Down
Loading

0 comments on commit a99765c

Please sign in to comment.