Skip to content

Commit

Permalink
Merge branch 'features/cpp' into development/enum-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul authored Oct 18, 2023
2 parents 0728bcf + 1f2854b commit 0e30429
Show file tree
Hide file tree
Showing 59 changed files with 593 additions and 780 deletions.
36 changes: 17 additions & 19 deletions languages/cpp/src/shared/include/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef FIREBOLT_ERROR_H
#define FIREBOLT_ERROR_H
#pragma once

#ifdef __cplusplus
extern "C" {
#endif
namespace Firebolt {

typedef enum FireboltSDKError {
FireboltSDKErrorNone = 0,
FireboltSDKErrorGeneral = 1,
FireboltSDKErrorUnavailable = 2,
FireboltSDKErrorTimedout = 3,
FireboltSDKErrorNotSubscribed = 4,
FireboltSDKErrorUnknown = 5,
FireboltSDKErrorInUse = 6,
FireboltSDKErrorNotSupported = 7
} FireboltSDKError_t;
enum class Error : int32_t {
None = 0,
General = 1,
Timedout = 2,
NotConnected = 3,
AlreadyConnected = 4,
//AuthenticationError, ?
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
CapabilityNotAvaialbale = -50300,
CapabilityNotSupported = -50100,
CapabilityGet = -50200,
CapabilityNotPermitted = -40300,
};

#ifdef __cplusplus
}
#endif

#endif // FIREBOLT_ERROR_H
20 changes: 5 additions & 15 deletions languages/cpp/src/shared/include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef FIREBOLT_TYPES_H
#define FIREBOLT_TYPES_H
#pragma once

#include <stdint.h>
#include <stdbool.h>
#include <string>
#include <unordered_map>
#include <vector>
#include <optional>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Firebolt_String_s* Firebolt_String_t;
const char* Firebolt_String(Firebolt_String_t handle);
void Firebolt_String_Release(Firebolt_String_t handle);

#ifdef __cplusplus
}
#endif

#endif // FIREBOLT_TYPES_H
46 changes: 12 additions & 34 deletions languages/cpp/src/shared/src/Accessor/Accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,69 +27,63 @@ namespace FireboltSDK {
, _transport(nullptr)
, _config()
{
ASSERT(_singleton == nullptr);
_singleton = this;
_config.FromString(configLine);

Logger::SetLogLevel(WPEFramework::Core::EnumerateType<Logger::LogLevel>(_config.LogLevel.Value().c_str()).Value());

FIREBOLT_LOG_INFO(Logger::Category::OpenRPC, Logger::Module<Accessor>(), "Url = %s", _config.WsUrl.Value().c_str());
CreateTransport(_config.WsUrl.Value().c_str(), _config.WaitTime.Value());
CreateEventHandler();

_workerPool = WPEFramework::Core::ProxyType<WorkerPoolImplementation>::Create(_config.WorkerPool.ThreadCount.Value(), _config.WorkerPool.StackSize.Value(), _config.WorkerPool.QueueSize.Value());
WPEFramework::Core::WorkerPool::Assign(&(*_workerPool));
_workerPool->Run();
}

Accessor::~Accessor()
{
DestroyTransport();
DestroyEventHandler();
WPEFramework::Core::IWorkerPool::Assign(nullptr);
_workerPool->Stop();

ASSERT(_singleton != nullptr);
_singleton = nullptr;
}

int32_t Accessor::CreateEventHandler()
Firebolt::Error Accessor::CreateEventHandler()
{
Event::Instance().Configure(_transport);
return FireboltSDKErrorNone;
return Firebolt::Error::None;
}

int32_t Accessor::DestroyEventHandler()
Firebolt::Error Accessor::DestroyEventHandler()
{
Event::Dispose();
return FireboltSDKErrorNone;
return Firebolt::Error::None;
}

Event& Accessor::GetEventManager()
{
return Event::Instance();
}

int32_t Accessor::CreateTransport(const string& url, const uint32_t waitTime = DefaultWaitTime)
Firebolt::Error Accessor::CreateTransport(const string& url, const Transport<WPEFramework::Core::JSON::IElement>::Listener& listener, const uint32_t waitTime = DefaultWaitTime)
{
if (_transport != nullptr) {
delete _transport;
}

_transport = new Transport<WPEFramework::Core::JSON::IElement>(static_cast<WPEFramework::Core::URL>(url), waitTime);
if (WaitForLinkReady(_transport, waitTime) != FireboltSDKErrorNone) {
delete _transport;
_transport = nullptr;
}
_transport = new Transport<WPEFramework::Core::JSON::IElement>(static_cast<WPEFramework::Core::URL>(url), waitTime, listener);

ASSERT(_transport != nullptr);
return ((_transport != nullptr) ? FireboltSDKErrorNone : FireboltSDKErrorUnavailable);
return ((_transport != nullptr) ? Firebolt::Error::None : Firebolt::Error::Timedout);
}

int32_t Accessor::DestroyTransport()
Firebolt::Error Accessor::DestroyTransport()
{
if (_transport != nullptr) {
delete _transport;
_transport = nullptr;
}
return FireboltSDKErrorNone;
return Firebolt::Error::None;
}

Transport<WPEFramework::Core::JSON::IElement>* Accessor::GetTransport()
Expand All @@ -98,20 +92,4 @@ namespace FireboltSDK {
return _transport;
}

int32_t Accessor::WaitForLinkReady(Transport<WPEFramework::Core::JSON::IElement>* transport, const uint32_t waitTime = DefaultWaitTime) {
uint32_t waiting = (waitTime == WPEFramework::Core::infinite ? WPEFramework::Core::infinite : waitTime);
static constexpr uint32_t SLEEPSLOT_TIME = 100;

// Right, a wait till connection is closed is requested..
while ((waiting > 0) && (transport->IsOpen() == false)) {

uint32_t sleepSlot = (waiting > SLEEPSLOT_TIME ? SLEEPSLOT_TIME : waiting);

// Right, lets sleep in slices of 100 ms
SleepMs(sleepSlot);

waiting -= (waiting == WPEFramework::Core::infinite ? 0 : sleepSlot);
}
return (((waiting == 0) || (transport->IsOpen() == true)) ? FireboltSDKErrorNone : FireboltSDKErrorTimedout);
}
}
29 changes: 24 additions & 5 deletions languages/cpp/src/shared/src/Accessor/Accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,34 @@ namespace FireboltSDK {
delete _singleton;
}
}

Firebolt::Error Connect(const Transport<WPEFramework::Core::JSON::IElement>::Listener& listener)
{
Firebolt::Error status = CreateTransport(_config.WsUrl.Value().c_str(), listener, _config.WaitTime.Value());
if (status == Firebolt::Error::None) {
status = CreateEventHandler();
}
return status;
}

Firebolt::Error Disconnect()
{
Firebolt::Error status = Firebolt::Error::None;
status = DestroyTransport();
if (status == Firebolt::Error::None) {
status = DestroyEventHandler();
}
return status;
}

Event& GetEventManager();
Transport<WPEFramework::Core::JSON::IElement>* GetTransport();

private:
int32_t CreateEventHandler();
int32_t DestroyEventHandler();
int32_t CreateTransport(const string& url, const uint32_t waitTime);
int32_t DestroyTransport();
int32_t WaitForLinkReady(Transport<WPEFramework::Core::JSON::IElement>* transport, const uint32_t waitTime);
Firebolt::Error CreateEventHandler();
Firebolt::Error DestroyEventHandler();
Firebolt::Error CreateTransport(const string& url, const Transport<WPEFramework::Core::JSON::IElement>::Listener& listener, const uint32_t waitTime);
Firebolt::Error DestroyTransport();

private:
WPEFramework::Core::ProxyType<WorkerPoolImplementation> _workerPool;
Expand Down
2 changes: 1 addition & 1 deletion languages/cpp/src/shared/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ target_include_directories(${TARGET}
)

set_target_properties(${TARGET} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
FRAMEWORK FALSE
LINK_WHAT_YOU_USE TRUE
Expand Down
26 changes: 14 additions & 12 deletions languages/cpp/src/shared/src/Event/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,35 @@ namespace FireboltSDK {
_transport->SetEventHandler(this);
}

int32_t Event::Unsubscribe(const string& eventName, void* usercb)
Firebolt::Error Event::Unsubscribe(const string& eventName, void* usercb)
{
int32_t status = Revoke(eventName, usercb);
Firebolt::Error status = Revoke(eventName, usercb);

if (status == FireboltSDKErrorNone) {
if (status == Firebolt::Error::None) {
if (_transport != nullptr) {

const string parameters("{\"listen\":false}");
status = _transport->Unsubscribe(eventName, parameters);
}
} else {
status = Firebolt::Error::None;
}
return ((status == FireboltSDKErrorInUse) ? FireboltSDKErrorNone: status);
return status;
}

int32_t Event::ValidateResponse(const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse, bool& enabled) /* override */
Firebolt::Error Event::ValidateResponse(const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse, bool& enabled) /* override */
{
int32_t result = FireboltSDKErrorGeneral;
Firebolt::Error result = Firebolt::Error::General;
Response response;
_transport->FromMessage((WPEFramework::Core::JSON::IElement*)&response, *jsonResponse);
if (response.Listening.IsSet() == true) {
result = FireboltSDKErrorNone;
result = Firebolt::Error::None;
enabled = response.Listening.Value();
}
return result;
}

int32_t Event::Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse) /* override */
Firebolt::Error Event::Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse) /* override */
{
string response = jsonResponse->Result.Value();
_adminLock.Lock();
Expand Down Expand Up @@ -117,12 +119,12 @@ namespace FireboltSDK {
}
_adminLock.Unlock();

return FireboltSDKErrorNone;;
return Firebolt::Error::None;;
}

int32_t Event::Revoke(const string& eventName, void* usercb)
Firebolt::Error Event::Revoke(const string& eventName, void* usercb)
{
int32_t status = FireboltSDKErrorNone;
Firebolt::Error status = Firebolt::Error::None;
_adminLock.Lock();
EventMap::iterator eventIndex = _eventMap.find(eventName);
if (eventIndex != _eventMap.end()) {
Expand All @@ -137,7 +139,7 @@ namespace FireboltSDK {
if (eventIndex->second.size() == 0) {
_eventMap.erase(eventIndex);
} else {
status = FireboltSDKErrorInUse;
status = Firebolt::Error::General;
}
}
_adminLock.Unlock();
Expand Down
Loading

0 comments on commit 0e30429

Please sign in to comment.