Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
- switch to winrt::guid
- fix advertisement
- radio watcher fixes
- add more prebuilds
  • Loading branch information
geovie committed Aug 19, 2019
1 parent 8dd8d2b commit 55786fd
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 124 deletions.
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# vs 2017 image because Windows SDK 10.0.15063.0 is not available on vs 2015 image
image: Previous Visual Studio 2017
image: Visual Studio 2017

platform:
- x86
Expand All @@ -26,7 +26,7 @@ build_script:
- ps: |
if ($publish_binary -Eq "1") {
yarn global add prebuild 2>&1 | write-host
prebuild -t 6.14.4 -t 8.12.0 -r node --upload $env:prebuild_upload 2>&1 | write-host
prebuild -t 2.0.0 -t 3.0.0 -r electron --upload $env:prebuild_upload 2>&1 | write-host
prebuild -t 6.14.4 -t 8.12.0 -t 9.11.2 -t 10.16.1 -t 11.15.0 -t 12.7.0 -r node --upload $env:prebuild_upload 2>&1 | write-host
prebuild -t 2.0.0 -t 3.0.0 -t 4.0.4 -t 5.0.0 -t 6.0.0 -r electron --upload $env:prebuild_upload 2>&1 | write-host
}
echo "done."
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'targets': [
{
'target_name': 'noble_winrt',
'sources': [ 'src/noble_winrt.cc', 'src/napi_winrt.cc', 'src/peripheral_winrt.cc', 'src/radio_watcher.cc', 'src/notify_map.cc', 'src/ble_manager.cc', 'src/winrt_cpp.cc', 'src/callbacks.cc' ],
'sources': [ 'src/noble_winrt.cc', 'src/napi_winrt.cc', 'src/peripheral_winrt.cc', 'src/radio_watcher.cc', 'src/notify_map.cc', 'src/ble_manager.cc', 'src/winrt_cpp.cc', 'src/winrt_guid.cc', 'src/callbacks.cc' ],
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")", "<!@(node -p \"require('napi-thread-safe-callback').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'cflags!': [ '-fno-exceptions' ],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "noble-winrt",
"description": "Noble (Node.js Bluetooth LE) bindings for Windows 10",
"version": "0.0.3",
"version": "0.0.4",
"author": "[email protected]",
"license": "MIT",
"repository": {
Expand Down
62 changes: 37 additions & 25 deletions src/ble_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ void BLEManager::OnRadio(Radio& radio)
}
}

void BLEManager::Scan(const std::vector<UUID>& serviceUUIDs, bool allowDuplicates)
void BLEManager::Scan(const std::vector<winrt::guid>& serviceUUIDs, bool allowDuplicates)
{
mAdvertismentMap.clear();
mAllowDuplicates = allowDuplicates;
BluetoothLEAdvertisementFilter filter = BluetoothLEAdvertisementFilter();
BluetoothLEAdvertisement advertisment = BluetoothLEAdvertisement();
Expand All @@ -138,6 +139,7 @@ void BLEManager::OnScanResult(BluetoothLEAdvertisementWatcher watcher,

if (mDeviceMap.find(uuid) == mDeviceMap.end())
{
mAdvertismentMap.insert(uuid);
auto peripheral =
PeripheralWinrt(bluetoothAddress, advertismentType, rssi, args.Advertisement());
mEmit.Scan(uuid, rssi, peripheral);
Expand All @@ -147,8 +149,9 @@ void BLEManager::OnScanResult(BluetoothLEAdvertisementWatcher watcher,
{
PeripheralWinrt& peripheral = mDeviceMap[uuid];
peripheral.Update(rssi, args.Advertisement());
if (mAllowDuplicates)
if (mAllowDuplicates || mAdvertismentMap.find(uuid) == mAdvertismentMap.end())
{
mAdvertismentMap.insert(uuid);
mEmit.Scan(uuid, rssi, peripheral);
}
}
Expand All @@ -173,8 +176,15 @@ bool BLEManager::Connect(const std::string& uuid)
return false;
}
PeripheralWinrt& peripheral = mDeviceMap[uuid];
auto completed = bind2(this, &BLEManager::OnConnected, uuid);
BluetoothLEDevice::FromBluetoothAddressAsync(peripheral.bluetoothAddress).Completed(completed);
if (!peripheral.device.has_value())
{
auto completed = bind2(this, &BLEManager::OnConnected, uuid);
BluetoothLEDevice::FromBluetoothAddressAsync(peripheral.bluetoothAddress).Completed(completed);
}
else
{
mEmit.Connected(uuid);
}
return true;
}

Expand Down Expand Up @@ -244,7 +254,8 @@ bool BLEManager::UpdateRSSI(const std::string& uuid)
return true;
}

bool BLEManager::DiscoverServices(const std::string& uuid, const std::vector<UUID>& serviceUUIDs)
bool BLEManager::DiscoverServices(const std::string& uuid,
const std::vector<winrt::guid>& serviceUUIDs)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand All @@ -257,7 +268,7 @@ bool BLEManager::DiscoverServices(const std::string& uuid, const std::vector<UUI

void BLEManager::OnServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp,
AsyncStatus status, const std::string uuid,
const std::vector<UUID> serviceUUIDs)
const std::vector<winrt::guid> serviceUUIDs)
{
if (status == AsyncStatus::Completed)
{
Expand All @@ -280,8 +291,8 @@ void BLEManager::OnServicesDiscovered(IAsyncOperation<GattDeviceServicesResult>
}
}

bool BLEManager::DiscoverIncludedServices(const std::string& uuid, const UUID& serviceUuid,
const std::vector<UUID>& serviceUUIDs)
bool BLEManager::DiscoverIncludedServices(const std::string& uuid, const winrt::guid& serviceUuid,
const std::vector<winrt::guid>& serviceUUIDs)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand All @@ -306,7 +317,7 @@ bool BLEManager::DiscoverIncludedServices(const std::string& uuid, const UUID& s
void BLEManager::OnIncludedServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp,
AsyncStatus status, const std::string uuid,
const std::string serviceId,
const std::vector<UUID> serviceUUIDs)
const std::vector<winrt::guid> serviceUUIDs)
{
if (status == AsyncStatus::Completed)
{
Expand All @@ -329,8 +340,8 @@ void BLEManager::OnIncludedServicesDiscovered(IAsyncOperation<GattDeviceServices
}
}

bool BLEManager::DiscoverCharacteristics(const std::string& uuid, const UUID& serviceUuid,
const std::vector<UUID>& characteristicUUIDs)
bool BLEManager::DiscoverCharacteristics(const std::string& uuid, const winrt::guid& serviceUuid,
const std::vector<winrt::guid>& characteristicUUIDs)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand All @@ -355,7 +366,7 @@ bool BLEManager::DiscoverCharacteristics(const std::string& uuid, const UUID& se
void BLEManager::OnCharacteristicsDiscovered(IAsyncOperation<GattCharacteristicsResult> asyncOp,
AsyncStatus status, const std::string uuid,
const std::string serviceId,
const std::vector<UUID> characteristicUUIDs)
const std::vector<winrt::guid> characteristicUUIDs)
{
if (status == AsyncStatus::Completed)
{
Expand All @@ -379,8 +390,8 @@ void BLEManager::OnCharacteristicsDiscovered(IAsyncOperation<GattCharacteristics
}
}

bool BLEManager::Read(const std::string& uuid, const UUID& serviceUuid,
const UUID& characteristicUuid)
bool BLEManager::Read(const std::string& uuid, const winrt::guid& serviceUuid,
const winrt::guid& characteristicUuid)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand Down Expand Up @@ -431,8 +442,9 @@ void BLEManager::OnRead(IAsyncOperation<GattReadResult> asyncOp, AsyncStatus sta
}
}

bool BLEManager::Write(const std::string& uuid, const UUID& serviceUuid,
const UUID& characteristicUuid, const Data& data, bool withoutResponse)
bool BLEManager::Write(const std::string& uuid, const winrt::guid& serviceUuid,
const winrt::guid& characteristicUuid, const Data& data,
bool withoutResponse)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand Down Expand Up @@ -489,8 +501,8 @@ GetDescriptorValue(GattCharacteristicProperties properties)
}
}

bool BLEManager::Notify(const std::string& uuid, const UUID& serviceUuid,
const UUID& characteristicUuid, bool on)
bool BLEManager::Notify(const std::string& uuid, const winrt::guid& serviceUuid,
const winrt::guid& characteristicUuid, bool on)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand Down Expand Up @@ -582,8 +594,8 @@ void BLEManager::OnValueChanged(GattCharacteristic characteristic,
mEmit.Read(deviceUuid, serviceUuid, characteristicUuid, data, true);
}

bool BLEManager::DiscoverDescriptors(const std::string& uuid, const UUID& serviceUuid,
const UUID& characteristicUuid)
bool BLEManager::DiscoverDescriptors(const std::string& uuid, const winrt::guid& serviceUuid,
const winrt::guid& characteristicUuid)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand Down Expand Up @@ -630,8 +642,8 @@ void BLEManager::OnDescriptorsDiscovered(IAsyncOperation<GattDescriptorsResult>
}
}

bool BLEManager::ReadValue(const std::string& uuid, const UUID& serviceUuid,
const UUID& characteristicUuid, const UUID& descriptorUuid)
bool BLEManager::ReadValue(const std::string& uuid, const winrt::guid& serviceUuid,
const winrt::guid& characteristicUuid, const winrt::guid& descriptorUuid)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand Down Expand Up @@ -684,9 +696,9 @@ void BLEManager::OnReadValue(IAsyncOperation<GattReadResult> asyncOp, AsyncStatu
}
}

bool BLEManager::WriteValue(const std::string& uuid, const UUID& serviceUuid,
const UUID& characteristicUuid, const UUID& descriptorUuid,
const Data& data)
bool BLEManager::WriteValue(const std::string& uuid, const winrt::guid& serviceUuid,
const winrt::guid& characteristicUuid,
const winrt::guid& descriptorUuid, const Data& data)
{
CHECK_DEVICE();
IFDEVICE(device, uuid)
Expand Down
27 changes: 14 additions & 13 deletions src/ble_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ class BLEManager
public:
// clang-format off
BLEManager(const Napi::Value& receiver, const Napi::Function& callback);
void Scan(const std::vector<UUID>& serviceUUIDs, bool allowDuplicates);
void Scan(const std::vector<winrt::guid>& serviceUUIDs, bool allowDuplicates);
void StopScan();
bool Connect(const std::string& uuid);
bool Disconnect(const std::string& uuid);
bool UpdateRSSI(const std::string& uuid);
bool DiscoverServices(const std::string& uuid, const std::vector<UUID>& serviceUUIDs);
bool DiscoverIncludedServices(const std::string& uuid, const UUID& serviceUuid, const std::vector<UUID>& serviceUUIDs);
bool DiscoverCharacteristics(const std::string& uuid, const UUID& service, const std::vector<UUID>& characteristicUUIDs);
bool Read(const std::string& uuid, const UUID& serviceUuid, const UUID& characteristicUuid);
bool Write(const std::string& uuid, const UUID& serviceUuid, const UUID& characteristicUuid, const Data& data, bool withoutResponse);
bool Notify(const std::string& uuid, const UUID& serviceUuid, const UUID& characteristicUuid, bool on);
bool DiscoverDescriptors(const std::string& uuid, const UUID& serviceUuid, const UUID& characteristicUuid);
bool ReadValue(const std::string& uuid, const UUID& serviceUuid, const UUID& characteristicUuid, const UUID& descriptorUuid);
bool WriteValue(const std::string& uuid, const UUID& serviceUuid, const UUID& characteristicUuid, const UUID& descriptorUuid, const Data& data);
bool DiscoverServices(const std::string& uuid, const std::vector<winrt::guid>& serviceUUIDs);
bool DiscoverIncludedServices(const std::string& uuid, const winrt::guid& serviceUuid, const std::vector<winrt::guid>& serviceUUIDs);
bool DiscoverCharacteristics(const std::string& uuid, const winrt::guid& service, const std::vector<winrt::guid>& characteristicUUIDs);
bool Read(const std::string& uuid, const winrt::guid& serviceUuid, const winrt::guid& characteristicUuid);
bool Write(const std::string& uuid, const winrt::guid& serviceUuid, const winrt::guid& characteristicUuid, const Data& data, bool withoutResponse);
bool Notify(const std::string& uuid, const winrt::guid& serviceUuid, const winrt::guid& characteristicUuid, bool on);
bool DiscoverDescriptors(const std::string& uuid, const winrt::guid& serviceUuid, const winrt::guid& characteristicUuid);
bool ReadValue(const std::string& uuid, const winrt::guid& serviceUuid, const winrt::guid& characteristicUuid, const winrt::guid& descriptorUuid);
bool WriteValue(const std::string& uuid, const winrt::guid& serviceUuid, const winrt::guid& characteristicUuid, const winrt::guid& descriptorUuid, const Data& data);
bool ReadHandle(const std::string& uuid, int handle);
bool WriteHandle(const std::string& uuid, int handle, Data data);
// clang-format on
Expand All @@ -49,9 +49,9 @@ class BLEManager
void OnScanStopped(BluetoothLEAdvertisementWatcher watcher, const BluetoothLEAdvertisementWatcherStoppedEventArgs& args);
void OnConnected(IAsyncOperation<BluetoothLEDevice> asyncOp, AsyncStatus& status, std::string uuid);
void OnConnectionStatusChanged(BluetoothLEDevice device, winrt::Windows::Foundation::IInspectable inspectable);
void OnServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp, AsyncStatus status, std::string uuid, std::vector<UUID> serviceUUIDs);
void OnIncludedServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::vector<UUID> serviceUUIDs);
void OnCharacteristicsDiscovered(IAsyncOperation<GattCharacteristicsResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::vector<UUID> characteristicUUIDs);
void OnServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp, AsyncStatus status, std::string uuid, std::vector<winrt::guid> serviceUUIDs);
void OnIncludedServicesDiscovered(IAsyncOperation<GattDeviceServicesResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::vector<winrt::guid> serviceUUIDs);
void OnCharacteristicsDiscovered(IAsyncOperation<GattCharacteristicsResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::vector<winrt::guid> characteristicUUIDs);
void OnRead(IAsyncOperation<GattReadResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::string characteristicId);
void OnWrite(IAsyncOperation<GattWriteResult> asyncOp, AsyncStatus status, std::string uuid, std::string serviceId, std::string characteristicId);
void OnNotify(IAsyncOperation<GattWriteResult> asyncOp, AsyncStatus status, GattCharacteristic characteristic, std::string uuid, std::string serviceId, std::string characteristicId, bool state);
Expand All @@ -72,5 +72,6 @@ class BLEManager
bool mAllowDuplicates;

std::unordered_map<std::string, PeripheralWinrt> mDeviceMap;
std::set<std::string> mAdvertismentMap;
NotifyMap mNotifyMap;
};
15 changes: 9 additions & 6 deletions src/napi_winrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include "napi_winrt.h"

#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <rpc.h>

using namespace winrt::Windows::Devices::Bluetooth;

UUID napiToUuid(Napi::String string)
winrt::guid napiToUuid(Napi::String string)
{
std::string str = string.Utf8Value();
if (str.size() == 32)
Expand All @@ -27,12 +28,14 @@ UUID napiToUuid(Napi::String string)
}
UUID uuid;
UuidFromString((RPC_CSTR)str.c_str(), &uuid);
return uuid;
std::array<uint8_t, 8> data4;
std::copy_n(uuid.Data4, data4.size(), data4.begin());
return winrt::guid(uuid.Data1, uuid.Data2, uuid.Data3, data4);
}

std::vector<UUID> napiToUuidArray(Napi::Array array)
std::vector<winrt::guid> napiToUuidArray(Napi::Array array)
{
std::vector<UUID> uuids;
std::vector<winrt::guid> uuids;
for (size_t i = 0; i < array.Length(); i++)
{
Napi::Value val = array[i];
Expand All @@ -54,13 +57,13 @@ int napiToNumber(Napi::Number number)
return number.Int32Value();
}

std::vector<UUID> getUuidArray(const Napi::Value& value)
std::vector<winrt::guid> getUuidArray(const Napi::Value& value)
{
if (value.IsArray())
{
return napiToUuidArray(value.As<Napi::Array>());
}
return std::vector<UUID>();
return std::vector<winrt::guid>();
}

bool getBool(const Napi::Value& value, bool def)
Expand Down
6 changes: 3 additions & 3 deletions src/napi_winrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include "winrt/base.h"
#include "peripheral.h"

std::vector<UUID> getUuidArray(const Napi::Value& value);
std::vector<winrt::guid> getUuidArray(const Napi::Value& value);
bool getBool(const Napi::Value& value, bool def);

UUID napiToUuid(Napi::String string);
Data napiToData(Napi::Buffer<byte> buffer);
winrt::guid napiToUuid(Napi::String string);
Data napiToData(Napi::Buffer<unsigned char> buffer);
int napiToNumber(Napi::Number number);
Loading

0 comments on commit 55786fd

Please sign in to comment.