Skip to content

Commit

Permalink
simpleble_winrt_2 (#590)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>

Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
  • Loading branch information
Andrey1994 committed Jan 6, 2023
1 parent b3cd2be commit 3084b61
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 121 deletions.
21 changes: 9 additions & 12 deletions third_party/SimpleBLE/simpleble/include/simpleble/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ namespace SimpleBLE {
namespace Logging {

enum Level : int {
NONE = 0,
FATAL,
ERROR,
WARN,
INFO,
#pragma push_macro("DEBUG")
#undef DEBUG
DEBUG,
#pragma pop_macro("DEBUG")
VERBOSE,
None = 0,
Fatal,
Error,
Warn,
Info,
Debug,
Verbose,
};

// clang-format off
Expand Down Expand Up @@ -54,11 +51,11 @@ class SIMPLEBLE_EXPORT Logger {
Logger(Logger& other) = delete; // Remove copy constructor
void operator=(const Logger&) = delete; // Remove copy assignment

Level level_{Level::INFO};
Level level_{Level::Info};
Callback callback_{nullptr};
std::recursive_mutex mutex_;
};

} // namespace Logging

} // namespace SimpleBLE
} // namespace SimpleBLE
16 changes: 8 additions & 8 deletions third_party/SimpleBLE/simpleble/src/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ Logger::Logger() {
const std::string& function, const std::string& message) {
std::string level_string;
switch (level) {
case Level::VERBOSE:
case Level::Verbose:
level_string = "VERBOSE";
break;
case Level::DEBUG:
case Level::Debug:
level_string = "DEBUG";
break;
case Level::INFO:
case Level::Info:
level_string = "INFO";
break;
case Level::WARN:
case Level::Warn:
level_string = "WARNING";
break;
case Level::ERROR:
case Level::Error:
level_string = "ERROR";
break;
case Level::FATAL:
case Level::Fatal:
level_string = "FATAL";
break;
case Level::NONE:
case Level::None:
return;
}

Expand Down Expand Up @@ -102,4 +102,4 @@ void logfwd::receive(
}
// clang-format on

#endif
#endif
42 changes: 21 additions & 21 deletions third_party/SimpleBLE/simpleble/src/LoggingInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,52 @@

#if SIMPLEBLE_LOG_LEVEL == SIMPLEDBUS_LOG_LEVEL_VERBOSE

#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::FATAL, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::ERROR, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::WARN, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::INFO, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_DEBUG(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::DEBUG, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Fatal, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Error, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Warn, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Info, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_DEBUG(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Debug, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_VERBOSE(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::VERBOSE, "SimpleBLE", __FILE__, __LINE__, __func__, msg)

#elif SIMPLEBLE_LOG_LEVEL == SIMPLEDBUS_LOG_LEVEL_DEBUG

#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::FATAL, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::ERROR, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::WARN, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::INFO, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_DEBUG(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::DEBUG, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Fatal, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Error, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Warn, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Info, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_DEBUG(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Debug, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_VERBOSE(msg)

#elif SIMPLEBLE_LOG_LEVEL == SIMPLEDBUS_LOG_LEVEL_INFO

#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::FATAL, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::ERROR, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::WARN, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::INFO, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Fatal, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Error, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Warn, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Info, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_DEBUG(msg)
#define SIMPLEBLE_LOG_VERBOSE(msg)

#elif SIMPLEBLE_LOG_LEVEL == SIMPLEDBUS_LOG_LEVEL_WARN

#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::FATAL, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::ERROR, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::WARN, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Fatal, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Error, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Warn, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_INFO(msg)
#define SIMPLEBLE_LOG_DEBUG(msg)
#define SIMPLEBLE_LOG_VERBOSE(msg)

#elif SIMPLEBLE_LOG_LEVEL == SIMPLEDBUS_LOG_LEVEL_ERROR

#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::FATAL, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::ERROR, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Fatal, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Error, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_WARN(msg)
#define SIMPLEBLE_LOG_INFO(msg)
#define SIMPLEBLE_LOG_DEBUG(msg)
#define SIMPLEBLE_LOG_VERBOSE(msg)

#elif SIMPLEBLE_LOG_LEVEL == SIMPLEDBUS_LOG_LEVEL_FATAL

#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::FATAL, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_FATAL(msg) SimpleBLE::Logging::Logger::get()->log(SimpleBLE::Logging::Level::Fatal, "SimpleBLE", __FILE__, __LINE__, __func__, msg)
#define SIMPLEBLE_LOG_ERROR(msg)
#define SIMPLEBLE_LOG_WARN(msg)
#define SIMPLEBLE_LOG_INFO(msg)
Expand All @@ -82,4 +82,4 @@

#endif

// clang-format on
// clang-format on
86 changes: 6 additions & 80 deletions third_party/SimpleBLE/simpleble/src/backends/windows/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,16 @@
#include <sstream>

#include <sdkddkver.h>
#include "../winrt/roapi.h"

#include "LoggingInternal.h"

#if WDK_NTDDI_VERSION <= NTDDI_WIN10_VB
// For Windows SDK version before 10.0.19041.0, remap functions to post-10.0.19041.0 versions
#define WINRT_IMPL_CoGetApartmentType WINRT_CoGetApartmentType
#define WINRT_IMPL_CoInitializeEx(ptr, type) WINRT_RoInitialize(type)
#if WDK_NTDDI_VERSION < NTDDI_WIN10_VB
#error "Windows SDK version before 10.0.19041.0 is not supported"
#endif

#define MAC_ADDRESS_STR_LENGTH (size_t)17 // Two chars per byte, 5 chars for colon

// NOTE: These constants are defined in ObjIdl.h
// More info: https://learn.microsoft.com/en-us/windows/win32/api/objidlbase/ne-objidlbase-apttype
constexpr int32_t APTTYPE_CURRENT = -1;
constexpr int32_t APTTYPE_STA = 0;
constexpr int32_t APTTYPE_MTA = 1;
constexpr int32_t APTTYPE_NA = 2;
constexpr int32_t APTTYPE_MAINSTA = 3;

// https://learn.microsoft.com/en-us/windows/win32/api/objidl/ne-objidl-apttypequalifier
constexpr int32_t APTTYPEQUALIFIER_NONE = 0;
constexpr int32_t APTTYPEQUALIFIER_IMPLICIT_MTA = 1;
constexpr int32_t APTTYPEQUALIFIER_NA_ON_MTA = 2;
constexpr int32_t APTTYPEQUALIFIER_NA_ON_STA = 3;
constexpr int32_t APTTYPEQUALIFIER_NA_ON_IMPLICIT_MTA = 4;
constexpr int32_t APTTYPEQUALIFIER_NA_ON_MAINSTA = 5;
constexpr int32_t APTTYPEQUALIFIER_APPLICATION_STA = 6;
constexpr int32_t APTTYPEQUALIFIER_RESERVED_1 = 7;

constexpr int32_t COINIT_APARTMENTTHREADED = 0x2;
constexpr int32_t COINIT_MULTITHREADED = 0;
constexpr int32_t COINIT_DISABLE_OLE1DDE = 0x4;
constexpr int32_t COINIT_SPEED_OVER_MEMORY = 0x8;

// More info on COM appartments: https://learn.microsoft.com/en-us/windows/win32/com/processes--threads--and-apartments

namespace SimpleBLE {

void initialize_winrt() {
Expand All @@ -49,56 +23,8 @@ void initialize_winrt() {
if (initialized) return;
initialized = true;

int32_t cotype, qualifier;
int32_t get_apartment_result = WINRT_IMPL_CoGetApartmentType(&cotype, &qualifier);

SIMPLEBLE_LOG_INFO(
fmt::format("CoGetApartmentType: cotype={}, qualifier={}, result={}", cotype, qualifier, get_apartment_result));

winrt::hresult result;

if (cotype == APTTYPE_STA) {
// Current thread is already associated with an STA.
// No need to initialize the apartment.
return;
} else if (cotype == APTTYPE_MTA) {
// Current thread is already associated with an MTA.
// No need to initialize the apartment.
return;
} else if (cotype == APTTYPE_CURRENT) {
// Current thread is not associated with an apartment,
// or the apartment type is determined by the current threading model.
// Initialize the apartment based on the threading model.
if (qualifier == APTTYPEQUALIFIER_IMPLICIT_MTA) {
// Initialize the apartment as an MTA
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_MULTITHREADED);
} else if (qualifier == APTTYPEQUALIFIER_NA_ON_MTA) {
// Initialize the apartment as an MTA
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_MULTITHREADED);
} else if (qualifier == APTTYPEQUALIFIER_NA_ON_IMPLICIT_MTA) {
// Initialize the apartment as an MTA
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_MULTITHREADED);
} else if (qualifier == APTTYPEQUALIFIER_NA_ON_STA) {
// Initialize the apartment as an STA
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
} else if (qualifier == APTTYPEQUALIFIER_NA_ON_MAINSTA) {
// Initialize the apartment as an STA
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
} else if (qualifier == APTTYPEQUALIFIER_APPLICATION_STA) {
// Initialize the apartment as an STA
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
} else {
// qualifier is an unknown value.
// Initialize the apartment with the default concurrency model.
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
}
} else {
// cotype is an unknown value.
// Initialize the apartment with the default concurrency model.
result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
}

SIMPLEBLE_LOG_INFO(fmt::format("CoInitializeEx: result={}", result));
winrt::hresult result = RoInitialize(RO_INIT_MULTITHREADED);
SIMPLEBLE_LOG_INFO(fmt::format("RoInitialize: result={}", result));
}

std::string _mac_address_to_str(uint64_t mac_address) {
Expand Down Expand Up @@ -178,4 +104,4 @@ IBuffer bytearray_to_ibuffer(const ByteArray& array) {
return writer.DetachBuffer();
}

} // namespace SimpleBLE
} // namespace SimpleBLE

0 comments on commit 3084b61

Please sign in to comment.