diff --git a/appveyor.yml b/appveyor.yml index de58a594b..dc71ef679 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,6 +14,7 @@ install: - ps: npm -v - ps: node-gyp -v - cmd: yarn install --frozen-lockfile + - cmd: yarn build:source - cmd: yarn ci - ps: $publish_binary = 0 - ps: if ($env:appveyor_repo_tag -match "true") { $publish_binary=1; } @@ -28,4 +29,5 @@ build_script: prebuild -t 6.14.4 -t 8.12.0 -r node --upload $env:prebuild_upload 2>&1 | write-host prebuild -t 2.0.0 -r electron --upload $env:prebuild_upload 2>&1 | write-host } - echo "done." + echo "done." + diff --git a/lib/mac/src/ble_manager.mm b/lib/mac/src/ble_manager.mm index d921841e8..94c982087 100644 --- a/lib/mac/src/ble_manager.mm +++ b/lib/mac/src/ble_manager.mm @@ -9,6 +9,27 @@ #import #include "objc_cpp.h" +#include + +#define LOGE(message, ...) \ +{\ + char buff[255];\ + snprintf(buff, sizeof(buff), ": " message, __VA_ARGS__);\ + std::string buffAsStdStr = __FUNCTION__;\ + buffAsStdStr += buff;\ + emit.Log(buffAsStdStr);\ +} + +const char* description(NSError *error) { + auto description = [error description]; + if(description != nil) { + auto str = [description UTF8String]; + if(str != NULL) { + return str; + } + } + return ""; +} @implementation BLEManager - (instancetype)init { @@ -80,7 +101,7 @@ - (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPer int rssi = [RSSI intValue]; emit.Scan(uuid, rssi, p); } -#include + - (BOOL)connect:(NSString*) uuid { CBPeripheral *peripheral = [self.peripherals objectForKey:uuid]; if(!peripheral) { @@ -91,8 +112,9 @@ - (BOOL)connect:(NSString*) uuid { [self.peripherals setObject:peripheral forKey:uuid]; } else { std::stringstream str; - str << "Peripheral not found! counts: "; - str << [peripherals count]; + str << "Peripheral with uuid "; + str << [uuid UTF8String]; + str << " not found!"; emit.Connected([uuid UTF8String], str.str().c_str()); return NO; } @@ -111,7 +133,8 @@ - (void) centralManager:(CBCentralManager *)central didFailToConnectPeripheral:( std::string uuid = getUuid(peripheral); std::string message = "Connection failed"; if(error) { - message += ": " + std::string([[error debugDescription] UTF8String]); + message += ": "; + message += description(error); } emit.Connected(uuid, message); } @@ -121,6 +144,7 @@ - (BOOL)disconnect:(NSString*) uuid { [self.centralManager cancelPeripheralConnection:peripheral]; return YES; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } @@ -134,6 +158,7 @@ - (BOOL)updateRSSI:(NSString*) uuid { [peripheral readRSSI]; return YES; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } @@ -159,6 +184,7 @@ -(BOOL) discoverServices:(NSString*) uuid serviceUuids:(NSArray*) ser [peripheral discoverServices:servicesUuid]; return YES; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } @@ -181,13 +207,19 @@ - (BOOL)discoverIncludedServices:(NSString*) uuid forService:(NSString*) service [peripheral discoverIncludedServices:includedServices forService:service]; return YES; } + LOGE("service not found %s %s", [uuid UTF8String], [serviceUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error { std::string uuid = getUuid(peripheral); auto serviceUuid = [[service.UUID UUIDString] UTF8String]; + if(error) { + LOGE("error %s %s %s", uuid.c_str(), serviceUuid, description(error)); + } std::vector services = getServices(service.includedServices); emit.IncludedServicesDiscovered(uuid, serviceUuid, services); } @@ -207,13 +239,19 @@ - (BOOL)discoverCharacteristics:(NSString*) uuid forService:(NSString*) serviceU [peripheral discoverCharacteristics:characteristicsUuid forService:service]; return YES; } + LOGE("service not found %s %s", [uuid UTF8String], [serviceUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } -(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = std::string([service.UUID.UUIDString UTF8String]); + auto serviceUuid = [service.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s", uuid.c_str(), serviceUuid, description(error)); + } auto characteristics = getCharacteristics(service.characteristics); emit.CharacteristicsDiscovered(uuid, serviceUuid, characteristics); } @@ -225,14 +263,20 @@ - (BOOL)read:(NSString*) uuid service:(NSString*) serviceUuid characteristic:(NS [peripheral readValueForCharacteristic:characteristic]; return YES; } + LOGE("characteristic not found %s %s %s", [uuid UTF8String], [serviceUuid UTF8String], [characteristicUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } - (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; - std::string characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + auto serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; + auto characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s %s", uuid.c_str(), serviceUuid, characteristicUuid, description(error)); + } const UInt8* bytes = (UInt8 *)[characteristic.value bytes]; Data data; data.assign(bytes, bytes+[characteristic.value length]); @@ -251,14 +295,20 @@ - (BOOL)write:(NSString*) uuid service:(NSString*) serviceUuid characteristic:(N } return YES; } + LOGE("characteristic not found %s %s %s", [uuid UTF8String], [serviceUuid UTF8String], [characteristicUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } -(void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; - std::string characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + auto serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; + auto characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s %s", uuid.c_str(), serviceUuid, characteristicUuid, description(error)); + } emit.Write(uuid, serviceUuid, characteristicUuid); } @@ -268,14 +318,20 @@ - (BOOL)notify:(NSString*) uuid service:(NSString*) serviceUuid characteristic:( [peripheral setNotifyValue:on forCharacteristic:characteristic]; return YES; } + LOGE("characteristic not found %s %s %s", [uuid UTF8String], [serviceUuid UTF8String], [characteristicUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; - std::string characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + auto serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; + auto characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s %s", uuid.c_str(), serviceUuid, characteristicUuid, description(error)); + } emit.Notify(uuid, serviceUuid, characteristicUuid, characteristic.isNotifying); } @@ -287,14 +343,20 @@ - (BOOL)discoverDescriptors:(NSString*) uuid service:(NSString*) serviceUuid cha [peripheral discoverDescriptorsForCharacteristic:characteristic]; return YES; } + LOGE("characteristic not found %s %s %s", [uuid UTF8String], [serviceUuid UTF8String], [characteristicUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; - std::string characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + auto serviceUuid = [characteristic.service.UUID.UUIDString UTF8String]; + auto characteristicUuid = [characteristic.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s %s", uuid.c_str(), serviceUuid, characteristicUuid, description(error)); + } std::vector descriptors = getDescriptors(characteristic.descriptors); emit.DescriptorsDiscovered(uuid, serviceUuid, characteristicUuid, descriptors); } @@ -305,15 +367,21 @@ - (BOOL)readValue:(NSString*) uuid service:(NSString*) serviceUuid characteristi [peripheral readValueForDescriptor:descriptor]; return YES; } + LOGE("descriptor not found %s %s %s %s", [uuid UTF8String], [serviceUuid UTF8String], [characteristicUuid UTF8String], [descriptorUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = [descriptor.characteristic.service.UUID.UUIDString UTF8String]; - std::string characteristicUuid = [descriptor.characteristic.UUID.UUIDString UTF8String]; - std::string descriptorUuid = [descriptor.UUID.UUIDString UTF8String]; + auto serviceUuid = [descriptor.characteristic.service.UUID.UUIDString UTF8String]; + auto characteristicUuid = [descriptor.characteristic.UUID.UUIDString UTF8String]; + auto descriptorUuid = [descriptor.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s %s %s", uuid.c_str(), serviceUuid, characteristicUuid, descriptorUuid, description(error)); + } const UInt8* bytes = (UInt8 *)[descriptor.value bytes]; Data data; data.assign(bytes, bytes+[descriptor.value length]); @@ -329,15 +397,21 @@ - (BOOL)writeValue:(NSString*) uuid service:(NSString*) serviceUuid characterist [peripheral writeValue:data forDescriptor:descriptor]; return YES; } + LOGE("descriptor not found %s %s %s %s", [uuid UTF8String], [serviceUuid UTF8String], [characteristicUuid UTF8String], [descriptorUuid UTF8String]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error { std::string uuid = getUuid(peripheral); - std::string serviceUuid = [descriptor.characteristic.service.UUID.UUIDString UTF8String]; - std::string characteristicUuid = [descriptor.characteristic.UUID.UUIDString UTF8String]; - std::string descriptorUuid = [descriptor.UUID.UUIDString UTF8String]; + auto serviceUuid = [descriptor.characteristic.service.UUID.UUIDString UTF8String]; + auto characteristicUuid = [descriptor.characteristic.UUID.UUIDString UTF8String]; + auto descriptorUuid = [descriptor.UUID.UUIDString UTF8String]; + if(error) { + LOGE("error %s %s %s %s %s", uuid.c_str(), serviceUuid, characteristicUuid, descriptorUuid, description(error)); + } IF(NSNumber*, handle, [self getDescriptorHandle:descriptor]) { emit.WriteHandle(uuid, [handle intValue]); } @@ -350,7 +424,10 @@ - (BOOL)readHandle:(NSString*) uuid handle:(NSNumber*) handle { [peripheral readValueForDescriptor:descriptor]; return YES; } + LOGE("descriptor not found %s handle %d", [uuid UTF8String], [handle intValue]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } @@ -360,7 +437,10 @@ - (BOOL)writeHandle:(NSString*) uuid handle:(NSNumber*) handle data:(NSData*) da [peripheral writeValue:data forDescriptor:descriptor]; return YES; } + LOGE("descriptor not found %s handle %d", [uuid UTF8String], [handle intValue]); + return NO; } + LOGE("peripheral not found %s", [uuid UTF8String]); return NO; } diff --git a/lib/mac/src/callbacks.cc b/lib/mac/src/callbacks.cc index be3c2f510..5b11fdfae 100644 --- a/lib/mac/src/callbacks.cc +++ b/lib/mac/src/callbacks.cc @@ -208,3 +208,10 @@ void Emit::WriteHandle(const std::string & uuid, int descriptorHandle) { args = { _s("handleWrite"), _u(uuid), _n(descriptorHandle) }; }); } + +void Emit::Log(const std::string& log) { + mCallback->call([log](Napi::Env env, std::vector& args) { + // emit('log', log); + args = { _s("log"), _s(log) }; + }); +} diff --git a/lib/mac/src/callbacks.h b/lib/mac/src/callbacks.h index ccf270ab9..49a5d9299 100644 --- a/lib/mac/src/callbacks.h +++ b/lib/mac/src/callbacks.h @@ -25,6 +25,7 @@ class Emit { void WriteValue(const std::string& uuid, const std::string& serviceUuid, const std::string& characteristicUuid, const std::string& descriptorUuid); void ReadHandle(const std::string& uuid, int descriptorHandle, const std::vector& data); void WriteHandle(const std::string& uuid, int descriptorHandle); + void Log(const std::string& log); protected: std::shared_ptr mCallback; };