Utils.h
1 #pragma once 2 3 #include <cstdint> 4 #include <exception> 5 #include <string> 6 7 #include "winrt/Windows.Foundation.h" 8 #include "winrt/Windows.Storage.Streams.h" 9 #include "winrt/base.h" 10 11 using namespace winrt::Windows; 12 using namespace winrt::Windows::Storage::Streams; 13 14 constexpr uint32_t TEN_SECONDS_IN_MSECS = 10000; 15 16 namespace ble_peripheral 17 { 18 19 std::string _mac_address_to_str(uint64_t mac_address); 20 uint64_t _str_to_mac_address(std::string mac_address); 21 22 winrt::guid uuid_to_guid(const std::string &uuid); 23 std::string guid_to_uuid(const winrt::guid &guid); 24 25 std::vector<uint8_t> to_bytevc(IBuffer buffer); 26 IBuffer from_bytevc(std::vector<uint8_t> bytes); 27 std::string to_hexstring(std::vector<uint8_t> bytes); 28 29 std::string to_lower_case(std::string str); 30 31 std::string to_uuidstr(winrt::guid guid); 32 33 /// To call async functions synchronously 34 template <typename async_t> 35 static auto async_get(async_t const &async) 36 { 37 if (async.Status() == Foundation::AsyncStatus::Started) 38 { 39 wait_for_completed(async, TEN_SECONDS_IN_MSECS); 40 } 41 try 42 { 43 return async.GetResults(); 44 } 45 catch (const winrt::hresult_error &err) 46 { 47 throw FlutterError(winrt::to_string(err.message())); 48 } 49 catch (const std::exception &err) 50 { 51 throw FlutterError(err.what()); 52 } 53 catch (...) 54 { 55 throw FlutterError("Unknown error"); 56 } 57 } 58 59 } // namespace ble_peripheral