Skip to content

Commit

Permalink
mac log errors, build from source on ci
Browse files Browse the repository at this point in the history
  • Loading branch information
geovie committed Nov 26, 2018
1 parent d70e13e commit 0e6bfb1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 20 deletions.
4 changes: 3 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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."
118 changes: 99 additions & 19 deletions lib/mac/src/ble_manager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@
#import <Foundation/Foundation.h>

#include "objc_cpp.h"
#include <sstream>

#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 "<no description>";
}

@implementation BLEManager
- (instancetype)init {
Expand Down Expand Up @@ -80,7 +101,7 @@ - (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPer
int rssi = [RSSI intValue];
emit.Scan(uuid, rssi, p);
}
#include <sstream>

- (BOOL)connect:(NSString*) uuid {
CBPeripheral *peripheral = [self.peripherals objectForKey:uuid];
if(!peripheral) {
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -121,6 +144,7 @@ - (BOOL)disconnect:(NSString*) uuid {
[self.centralManager cancelPeripheralConnection:peripheral];
return YES;
}
LOGE("peripheral not found %s", [uuid UTF8String]);
return NO;
}

Expand All @@ -134,6 +158,7 @@ - (BOOL)updateRSSI:(NSString*) uuid {
[peripheral readRSSI];
return YES;
}
LOGE("peripheral not found %s", [uuid UTF8String]);
return NO;
}

Expand All @@ -159,6 +184,7 @@ -(BOOL) discoverServices:(NSString*) uuid serviceUuids:(NSArray<NSString*>*) ser
[peripheral discoverServices:servicesUuid];
return YES;
}
LOGE("peripheral not found %s", [uuid UTF8String]);
return NO;
}

Expand All @@ -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<std::string> services = getServices(service.includedServices);
emit.IncludedServicesDiscovered(uuid, serviceUuid, services);
}
Expand All @@ -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);
}
Expand All @@ -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]);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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<std::string> descriptors = getDescriptors(characteristic.descriptors);
emit.DescriptorsDiscovered(uuid, serviceUuid, characteristicUuid, descriptors);
}
Expand All @@ -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]);
Expand All @@ -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]);
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions lib/mac/src/callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<napi_value>& args) {
// emit('log', log);
args = { _s("log"), _s(log) };
});
}
1 change: 1 addition & 0 deletions lib/mac/src/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& data);
void WriteHandle(const std::string& uuid, int descriptorHandle);
void Log(const std::string& log);
protected:
std::shared_ptr<ThreadSafeCallback> mCallback;
};

0 comments on commit 0e6bfb1

Please sign in to comment.