diff --git a/languages/cpp/src/shared/src/Async/Async.cpp b/languages/cpp/src/shared/src/Async/Async.cpp index f9994d7d..67299d68 100644 --- a/languages/cpp/src/shared/src/Async/Async.cpp +++ b/languages/cpp/src/shared/src/Async/Async.cpp @@ -60,7 +60,7 @@ namespace FireboltSDK { void Async::Clear() { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.begin(); while (index != _methodMap.end()) { CallbackMap::iterator callbackIndex = index->second.begin(); @@ -72,7 +72,6 @@ namespace FireboltSDK { } index = _methodMap.erase(index); } - _adminLock.Unlock(); } } diff --git a/languages/cpp/src/shared/src/Async/Async.h b/languages/cpp/src/shared/src/Async/Async.h index a141b9e5..18b6be95 100644 --- a/languages/cpp/src/shared/src/Async/Async.h +++ b/languages/cpp/src/shared/src/Async/Async.h @@ -18,6 +18,7 @@ #pragma once +#include #include "Module.h" namespace FireboltSDK { @@ -86,47 +87,53 @@ namespace FireboltSDK { public: template - Firebolt::Error Invoke(const string& method, const PARAMETERS& parameters, const CALLBACK& callback, void* usercb, uint32_t waitTime = DefaultWaitTime) + Firebolt::Error Invoke(const string &method, const PARAMETERS ¶meters, const CALLBACK &callback, void *usercb, uint32_t waitTime = DefaultWaitTime) { Firebolt::Error status = Firebolt::Error::None; - if (_transport != nullptr) { - Transport* transport = _transport; - std::function actualCallback = callback; - DispatchFunction lambda = [actualCallback, transport, method, parameters, waitTime](Async& parent, void* usercb) -> Firebolt::Error { + if (_transport != nullptr) + { + Transport *transport = _transport; + std::function actualCallback = callback; + DispatchFunction lambda = [actualCallback, transport, method, parameters, waitTime](Async &parent, void *usercb) -> Firebolt::Error + { RESPONSE response; uint32_t id = DefaultId; Firebolt::Error status = transport->InvokeAsync(method, parameters, id); - if (status == Firebolt::Error::None && parent.IsActive(method, usercb) == true) { + if (status == Firebolt::Error::None && parent.IsActive(method, usercb) == true) + { parent.UpdateEntry(method, usercb, id); status = transport->WaitForResponse(id, response, waitTime); - if (status == Firebolt::Error::None && parent.IsActive(method, usercb) == true) { - WPEFramework::Core::ProxyType* jsonResponse = new WPEFramework::Core::ProxyType(); + if (status == Firebolt::Error::None && parent.IsActive(method, usercb) == true) + { + WPEFramework::Core::ProxyType *jsonResponse = new WPEFramework::Core::ProxyType(); *jsonResponse = WPEFramework::Core::ProxyType::Create(); (*jsonResponse)->FromString(response); actualCallback(usercb, jsonResponse, status); parent.RemoveEntry(method, usercb); } - } return (status); }; - _adminLock.Lock(); + std::lock_guard guard(_adminLock); WPEFramework::Core::ProxyType job = WPEFramework::Core::ProxyType(WPEFramework::Core::ProxyType::Create(*this, method, lambda, usercb)); CallbackData callbackData = {lambda, job, DefaultId}; MethodMap::iterator index = _methodMap.find(method); - if (index != _methodMap.end()) { + if (index != _methodMap.end()) + { CallbackMap::iterator callbackIndex = index->second.find(usercb); - if (callbackIndex == index->second.end()) { + if (callbackIndex == index->second.end()) + { index->second.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); } - } else { + } + else + { CallbackMap callbackMap; callbackMap.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); _methodMap.emplace(std::piecewise_construct, std::forward_as_tuple(method), std::forward_as_tuple(callbackMap)); } - _adminLock.Unlock(); WPEFramework::Core::IWorkerPool::Instance().Submit(job); } @@ -140,50 +147,57 @@ namespace FireboltSDK { return (Firebolt::Error::None); } - void UpdateEntry(const string& method, void* usercb, uint32_t id) + void UpdateEntry(const string &method, void *usercb, uint32_t id) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.find(method); - if (index != _methodMap.end()) { + if (index != _methodMap.end()) + { CallbackMap::iterator callbackIndex = index->second.find(usercb); - if (callbackIndex != index->second.end()) { + if (callbackIndex != index->second.end()) + { callbackIndex->second.id = id; } } - _adminLock.Unlock(); } - void RemoveEntry(const string& method, void* usercb) + void RemoveEntry(const string &method, void *usercb) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.find(method); - if (index != _methodMap.end()) { + if (index != _methodMap.end()) + { CallbackMap::iterator callbackIndex = index->second.find(usercb); - if (callbackIndex != index->second.end()) { - if (IsValidJob(callbackIndex->second)) { + if (callbackIndex != index->second.end()) + { + if (IsValidJob(callbackIndex->second)) + { WPEFramework::Core::IWorkerPool::Instance().Revoke(callbackIndex->second.job); } index->second.erase(callbackIndex); - if (index->second.size() == 0) { + if (index->second.size() == 0) + { _methodMap.erase(index); } } } - _adminLock.Unlock(); } - bool IsActive(const string& method, void* usercb) + bool IsActive(const string &method, void *usercb) { bool valid = false; - _adminLock.Lock(); - MethodMap::iterator index = _methodMap.find(method); - if (index != _methodMap.end()) { - CallbackMap::iterator callbackIndex = index->second.find(usercb); - if (callbackIndex != index->second.end()) { - valid = true; + { + std::lock_guard guard(_adminLock); + MethodMap::iterator index = _methodMap.find(method); + if (index != _methodMap.end()) + { + CallbackMap::iterator callbackIndex = index->second.find(usercb); + if (callbackIndex != index->second.end()) + { + valid = true; + } } } - _adminLock.Unlock(); return valid; } @@ -195,9 +209,9 @@ namespace FireboltSDK { private: MethodMap _methodMap; - WPEFramework::Core::CriticalSection _adminLock; - Transport* _transport; + std::mutex _adminLock; + Transport *_transport; - static Async* _singleton; + static Async *_singleton; }; } diff --git a/languages/cpp/src/shared/src/Event/Event.cpp b/languages/cpp/src/shared/src/Event/Event.cpp index 6f771462..041c0098 100644 --- a/languages/cpp/src/shared/src/Event/Event.cpp +++ b/languages/cpp/src/shared/src/Event/Event.cpp @@ -16,6 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include "Transport/Transport.h" #include "Event.h" @@ -100,35 +101,43 @@ namespace FireboltSDK { std::vector eventMaps = {&_internalEventMap, &_externalEventMap}; // Combine both _internalEventMap and _externalEventMap into a single loop - for (auto eventMap : eventMaps) { - _adminLock.Lock(); + for (auto eventMap : eventMaps) + { + std::lock_guard guard(_adminLock); EventMap::iterator eventIndex = eventMap->find(eventName); - if (eventIndex != eventMap->end()) { - CallbackMap& callbacks = eventIndex->second; - for (CallbackMap::iterator callbackIndex = callbacks.begin(); callbackIndex != callbacks.end();) { + if (eventIndex != eventMap->end()) + { + CallbackMap &callbacks = eventIndex->second; + for (CallbackMap::iterator callbackIndex = callbacks.begin(); callbackIndex != callbacks.end();) + { State state; - if (callbackIndex->second.state != State::REVOKED) { + if (callbackIndex->second.state != State::REVOKED) + { callbackIndex->second.state = State::EXECUTING; } state = callbackIndex->second.state; - _adminLock.Unlock(); - if (state == State::EXECUTING) { + _adminLock.unlock(); + if (state == State::EXECUTING) + { callbackIndex->second.lambda(callbackIndex->first, callbackIndex->second.userdata, response); } - _adminLock.Lock(); - if (callbackIndex->second.state == State::REVOKED) { + _adminLock.lock(); + if (callbackIndex->second.state == State::REVOKED) + { callbackIndex = callbacks.erase(callbackIndex); - if (callbacks.empty()) { + if (callbacks.empty()) + { eventMap->erase(eventIndex); // Erase from the correct eventMap - break; // No need to continue iterating if map is empty + break; // No need to continue iterating if map is empty } - } else { + } + else + { callbackIndex->second.state = State::IDLE; ++callbackIndex; } } } - _adminLock.Unlock(); } return Firebolt::Error::None; } @@ -141,34 +150,41 @@ namespace FireboltSDK { // Combine both _internalEventMap and _externalEventMap into a single loop std::vector eventMaps = {&_internalEventMap, &_externalEventMap}; - for (auto eventMap : eventMaps) { - _adminLock.Lock(); // Lock inside the loop + for (auto eventMap : eventMaps) + { + std::lock_guard guard(_adminLock); // Find the eventIndex for eventName in the current eventMap EventMap::iterator eventIndex = eventMap->find(eventName); - if (eventIndex != eventMap->end()) { + if (eventIndex != eventMap->end()) + { // Find the callbackIndex for usercb in the current CallbackMap CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb); - if (callbackIndex != eventIndex->second.end()) { + if (callbackIndex != eventIndex->second.end()) + { // Check if callback is not executing, then erase it - if (callbackIndex->second.state != State::EXECUTING) { + if (callbackIndex->second.state != State::EXECUTING) + { eventIndex->second.erase(callbackIndex); - } else { + } + else + { // Mark the callback as revoked callbackIndex->second.state = State::REVOKED; } // Check if the CallbackMap is empty after potential erasure - if (eventIndex->second.empty()) { + if (eventIndex->second.empty()) + { eventMap->erase(eventIndex); - } else { + } + else + { // Set status to General error if CallbackMap is not empty status = Firebolt::Error::General; } } } - - _adminLock.Unlock(); // Unlock after processing each eventMap } return status; @@ -179,20 +195,21 @@ namespace FireboltSDK { // Clear both _internalEventMap and _externalEventMap std::vector eventMaps = {&_internalEventMap, &_externalEventMap}; - for (auto eventMap : eventMaps) { - _adminLock.Lock(); // Lock before clearing + for (auto eventMap : eventMaps) + { + std::lock_guard guard(_adminLock); EventMap::iterator eventIndex = eventMap->begin(); - while (eventIndex != eventMap->end()) { + while (eventIndex != eventMap->end()) + { CallbackMap::iterator callbackIndex = eventIndex->second.begin(); - while (callbackIndex != eventIndex->second.end()) { + while (callbackIndex != eventIndex->second.end()) + { callbackIndex = eventIndex->second.erase(callbackIndex); } eventIndex = eventMap->erase(eventIndex); } - - _adminLock.Unlock(); // Unlock after clearing } } -} \ No newline at end of file +} diff --git a/languages/cpp/src/shared/src/Event/Event.h b/languages/cpp/src/shared/src/Event/Event.h index 6e599716..9bb970b2 100644 --- a/languages/cpp/src/shared/src/Event/Event.h +++ b/languages/cpp/src/shared/src/Event/Event.h @@ -18,6 +18,7 @@ #pragma once +#include #include "Module.h" namespace FireboltSDK { @@ -136,26 +137,30 @@ namespace FireboltSDK { return (Firebolt::Error::None); }; CallbackData callbackData = {implementation, userdata, State::IDLE}; - _adminLock.Lock(); - EventMap::iterator eventIndex = eventMap.find(eventName); - if (eventIndex != eventMap.end()) { - CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb); - - if (callbackIndex == eventIndex->second.end()) { - std::cout << "Registering new callback for event: " << eventName << std::endl; - eventIndex->second.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); - status = Firebolt::Error::None; - } - } else { - CallbackMap callbackMap; - callbackMap.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); - eventMap.emplace(std::piecewise_construct, std::forward_as_tuple(eventName), std::forward_as_tuple(callbackMap)); - status = Firebolt::Error::None; + { + std::lock_guard guard(_adminLock); + EventMap::iterator eventIndex = eventMap.find(eventName); + if (eventIndex != eventMap.end()) + { + CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb); + + if (callbackIndex == eventIndex->second.end()) + { + std::cout << "Registering new callback for event: " << eventName << std::endl; + eventIndex->second.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); + status = Firebolt::Error::None; + } + } + else + { + CallbackMap callbackMap; + callbackMap.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); + eventMap.emplace(std::piecewise_construct, std::forward_as_tuple(eventName), std::forward_as_tuple(callbackMap)); + status = Firebolt::Error::None; + } } - - _adminLock.Unlock(); return status; } Firebolt::Error Revoke(const string& eventName, void* usercb); @@ -164,13 +169,13 @@ namespace FireboltSDK { void Clear(); Firebolt::Error ValidateResponse(const WPEFramework::Core::ProxyType& jsonResponse, bool& enabled) override; Firebolt::Error Dispatch(const string& eventName, const WPEFramework::Core::ProxyType& jsonResponse) override; - - private: + + private: EventMap _internalEventMap; EventMap _externalEventMap; - WPEFramework::Core::CriticalSection _adminLock; - Transport* _transport; + std::mutex _adminLock; + Transport *_transport; - static Event* _singleton; + static Event *_singleton; }; -} \ No newline at end of file +} diff --git a/languages/cpp/src/shared/src/Logger/Logger.cpp b/languages/cpp/src/shared/src/Logger/Logger.cpp index 35ffdc29..48c17c0e 100644 --- a/languages/cpp/src/shared/src/Logger/Logger.cpp +++ b/languages/cpp/src/shared/src/Logger/Logger.cpp @@ -16,10 +16,26 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include +#include #include "Module.h" #include "error.h" #include "Logger.h" +std::string GetCurrentTime24HoursFormat() +{ + auto now = std::chrono::system_clock::now(); + std::time_t now_c = std::chrono::system_clock::to_time_t(now); + std::tm *now_tm = std::localtime(&now_c); + + std::stringstream ss; + ss << std::put_time(now_tm, "%H:%M:%S"); + return ss.str(); +} + namespace WPEFramework { ENUM_CONVERSION_BEGIN(FireboltSDK::Logger::LogLevel) @@ -71,7 +87,7 @@ namespace FireboltSDK { msg[position] = '\0'; char formattedMsg[Logger::MaxBufSize]; - const string time = WPEFramework::Core::Time::Now().ToTimeOnly(true); + const string time = GetCurrentTime24HoursFormat(); const string categoryName = WPEFramework::Core::EnumerateType(category).Data(); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" diff --git a/languages/cpp/src/shared/src/Logger/Logger.h b/languages/cpp/src/shared/src/Logger/Logger.h index e8b6946a..9f22aac5 100644 --- a/languages/cpp/src/shared/src/Logger/Logger.h +++ b/languages/cpp/src/shared/src/Logger/Logger.h @@ -19,6 +19,7 @@ #pragma once #include "types.h" +#include namespace FireboltSDK { @@ -58,7 +59,17 @@ namespace FireboltSDK { template static const string Module() { - return WPEFramework::Core::ClassNameOnly(typeid(CLASS).name()).Text(); + char _name[512]; + size_t _size = sizeof(_name) - 1; + std::string demangled_name = abi::__cxa_demangle(typeid(CLASS).name(), _name, &_size, 0); + std::string method_name; + std::string full_name(demangled_name); + std::size_t last_colon = full_name.rfind("::"); + if (last_colon != std::string::npos) { + method_name = full_name.substr(last_colon + 2); + } + + return method_name; } private: