Skip to content

Commit

Permalink
fix: Added Lifecycle template in cpp (#292)
Browse files Browse the repository at this point in the history
* fix: Added Lifecycle template in cpp

Added Lifecycle template in cpp

* Added comments
  • Loading branch information
kdivya153 authored Jul 29, 2024
1 parent 92e1687 commit 046adc1
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/sdks/core/src/cpp/sdk/cpptest/CoreSDKTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>
#include "CoreSDKTest.h"


using namespace std;
bool CoreSDKTest::_connected;
CoreSDKTest::OnPolicyChangedNotification CoreSDKTest::_policyChangedNotification;
Expand Down Expand Up @@ -727,7 +728,41 @@ EnumMap<Firebolt::Lifecycle::LifecycleEventSource> lifecycleEventSourceMap = {
{ Firebolt::Lifecycle::LifecycleEventSource::REMOTE, "remote" }
};

void CoreSDKTest::OnBackgroundNotification::onBackground(const Firebolt::Lifecycle::LifecycleEvent& lifecycleEvent)
void CoreSDKTest::LifecycleReady()
{
Firebolt::Error error = Firebolt::Error::None;
Firebolt::IFireboltAccessor::Instance().LifecycleInterface().ready(&error);
if (error == Firebolt::Error::None) {
cout << "Lifecycle ready is success" << endl;
} else {
cout << "Lifecycle ready status = " << static_cast<int>(error) << endl;
}
}

void CoreSDKTest::LifecycleFinished()
{
Firebolt::Error error = Firebolt::Error::None;
Firebolt::IFireboltAccessor::Instance().LifecycleInterface().finished(&error);
if (error == Firebolt::Error::None) {
cout << "Lifecycle finished is success" << endl;
} else {
cout << "Lifecycle finished status = " << static_cast<int>(error) << endl;
}
}

void CoreSDKTest::LifecycleState()
{
Firebolt::Error error = Firebolt::Error::None;
const std::string state = Firebolt::IFireboltAccessor::Instance().LifecycleInterface().state(&error);

if (error == Firebolt::Error::None) {
cout << "State of the App = " << state.c_str() << endl;
} else {
cout << "State of the App throws an error = " << static_cast<int>(error) << endl;
}
}

void CoreSDKTest::OnBackgroundNotification::onBackground( const Firebolt::Lifecycle::LifecycleEvent& lifecycleEvent)
{
cout <<"onBackground event is triggered" << endl;
cout <<"\tstate: " << ConvertFromEnum<Firebolt::Lifecycle::LifecycleState>(lifecycleStateMap, lifecycleEvent.state) << endl;
Expand Down
3 changes: 3 additions & 0 deletions src/sdks/core/src/cpp/sdk/cpptest/CoreSDKTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class CoreSDKTest {
static void GetProfileFlags();

static void LifecycleClose();
static void LifecycleFinished();
static void LifecycleReady();
static void LifecycleState();
static void SubscribeLifecycleBackgroundNotification();
static void UnsubscribeLifecycleBackgroundNotification();
static void SubscribeLifecycleForegroundNotification();
Expand Down
5 changes: 4 additions & 1 deletion src/sdks/core/src/cpp/sdk/cpptest/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ void RunAllTests() {

// Lifecycle methods
// runTest(CoreSDKTest::LifecycleClose, "LifecycleClose");
runTest(CoreSDKTest::LifecycleReady, "LifecycleReady");
runTest(CoreSDKTest::LifecycleFinished, "LifecycleFinished");
runTest(CoreSDKTest::LifecycleState, "LifecycleState");
runTest(CoreSDKTest::SubscribeLifecycleBackgroundNotification, "SubscribeLifecycleBackgroundNotification");
runTest(CoreSDKTest::UnsubscribeLifecycleBackgroundNotification, "UnsubscribeLifecycleBackgroundNotification");
runTest(CoreSDKTest::SubscribeLifecycleForegroundNotification, "SubscribeLifecycleForegroundNotification");
Expand Down Expand Up @@ -167,4 +170,4 @@ int main(int argc, char* argv[]) {
CoreSDKTest::DestroyFireboltInstance();

return 0;
}
}
60 changes: 60 additions & 0 deletions src/sdks/core/src/cpp/templates/Lifecycle/include/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "error.h"
#include <string>
/* ${IMPORTS} */

${if.declarations}namespace Firebolt {
namespace ${info.Title} {

${if.enums}
// Enums
/* ${ENUMS} */${end.if.enums}

${if.types}
// Types
/* ${TYPES} */${end.if.types}

${if.providers}/* ${PROVIDERS} */${end.if.providers}${if.xuses}/* ${XUSES} */${end.if.xuses}

${if.methods}struct I${info.Title} {

virtual ~I${info.Title}() = default;
virtual void ready(Firebolt::Error *err = nullptr) = 0;
virtual void finished(Firebolt::Error *err = nullptr) = 0;
virtual std::string state(Firebolt::Error *err = nullptr) = 0;

// Methods & Events
/* ${METHODS:declarations} */
};${end.if.methods}

// Template for mapping enums to strings
template<typename T>
using EnumMap = std::unordered_map<T, std::string>;

// Function to convert enum values to string representations
template <typename T>
inline const std::string& ConvertEnum(EnumMap<T> enumMap, T type)
{
return enumMap[type];
}

} //namespace ${info.Title}
}${end.if.declarations}
152 changes: 152 additions & 0 deletions src/sdks/core/src/cpp/templates/Lifecycle/src/module_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* Copyright 2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/


#include "${info.title.lowercase}_impl.h"

${if.implementations}
namespace Firebolt {
namespace ${info.Title} {
${if.providers}
/* ${PROVIDERS} */${end.if.providers}

EnumMap<Firebolt::Lifecycle::LifecycleState> lifecycleStateMap = {
{ Firebolt::Lifecycle::LifecycleState::INITIALIZING, "initializing" },
{ Firebolt::Lifecycle::LifecycleState::INACTIVE, "inactive" },
{ Firebolt::Lifecycle::LifecycleState::FOREGROUND, "foreground" },
{ Firebolt::Lifecycle::LifecycleState::BACKGROUND, "background" },
{ Firebolt::Lifecycle::LifecycleState::UNLOADING, "unloading" },
{ Firebolt::Lifecycle::LifecycleState::SUSPENDED, "suspended" }
};


/* ready - Notify the platform that the app is ready */
static void readyDispatcher(const void* result) {
Firebolt::IFireboltAccessor::Instance().MetricsInterface().ready();
}

// localCallback to update the state
static void onReadyInnerCallback(void* notification, const void* userData, void* jsonResponse )
{
const LifecycleImpl* selfConst = static_cast<const LifecycleImpl*>(userData);
LifecycleImpl* self = const_cast<LifecycleImpl*>(selfConst);

WPEFramework::Core::ProxyType<JsonData_LifecycleEvent>& proxyResponse = *(reinterpret_cast<WPEFramework::Core::ProxyType<JsonData_LifecycleEvent>*>(jsonResponse));
ASSERT(proxyResponse.IsValid() == true);

if (proxyResponse.IsValid() == true) {
LifecycleEvent value;

value.state = proxyResponse->State;
std::string stateStr = ConvertEnum<Firebolt::Lifecycle::LifecycleState>(lifecycleStateMap, value.state);
// Assign stateStr to currentState in ${info.Title}Impl instance
self->currentState = stateStr;
std::cout << "Updated the Current State to: " << self->currentState << std::endl;

proxyResponse.Release();

}
}



/* ready - Notify the platform that the app is ready */
void ${info.Title}Impl::ready(Firebolt::Error *err) {
Firebolt::Error status = Firebolt::Error::NotConnected;

JsonObject jsonParameters;

// Call Prioritize to subscribe to corresponding event, add to internalMap, and prioritize its callback
status = FireboltSDK::Event::Instance().Prioritize<JsonData_LifecycleEvent>("lifecycle.onForeground", jsonParameters, onReadyInnerCallback, (void*)nullptr, this);
status = FireboltSDK::Event::Instance().Prioritize<JsonData_LifecycleEvent>("lifecycle.onBackground", jsonParameters, onReadyInnerCallback, (void*)nullptr, this);
status = FireboltSDK::Event::Instance().Prioritize<JsonData_LifecycleEvent>("lifecycle.onInactive", jsonParameters, onReadyInnerCallback, (void*)nullptr, this);
status = FireboltSDK::Event::Instance().Prioritize<JsonData_LifecycleEvent>("lifecycle.onSuspended", jsonParameters, onReadyInnerCallback, (void*)nullptr, this);
status = FireboltSDK::Event::Instance().Prioritize<JsonData_LifecycleEvent>("lifecycle.onUnloading", jsonParameters, onReadyInnerCallback, (void*)nullptr, this);

FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {
WPEFramework::Core::JSON::VariantContainer jsonResult;
status = transport->Invoke("lifecycle.ready", jsonParameters, jsonResult);
if (status == Firebolt::Error::None) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Lifecycle.ready is successfully invoked");

WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch> job = WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch>(WPEFramework::Core::ProxyType<FireboltSDK::Worker>::Create(readyDispatcher, nullptr));
WPEFramework::Core::IWorkerPool::Instance().Submit(job);
} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in invoking lifecycle.ready: %d", status);
if (err != nullptr) {
*err = status;
}
}

} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
if (err != nullptr) {
*err = status;
}
}
}


/* state - return the state of the app */
std::string ${info.Title}Impl::state(Firebolt::Error *err) {
std::cout << "**CURRENT STATE OF THE APP::::**" << currentState << std::endl;
return currentState;
}



/* finished - Notify the platform that the app is done unloading */
void ${info.Title}Impl::finished(Firebolt::Error *err)
{
Firebolt::Error status = Firebolt::Error::NotConnected;
if(currentState == "unloading")
{
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {

JsonObject jsonParameters;

WPEFramework::Core::JSON::VariantContainer jsonResult;
status = transport->Invoke("lifecycle.finished", jsonParameters, jsonResult);
if (status == Firebolt::Error::None) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Lifecycle.finished is successfully invoked");

}

} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}
}

return;
}

// Methods
/* ${METHODS} */

// Events
/* ${EVENTS} */

}//namespace Lifecycle
}${end.if.implementations}
${if.enums}

namespace WPEFramework {

/* ${ENUMS} */
}${end.if.enums}
58 changes: 58 additions & 0 deletions src/sdks/core/src/cpp/templates/Lifecycle/src/module_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright 2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#include "FireboltSDK.h"
#include "IModule.h"
#include "firebolt.h"
#include "jsondata_lifecycle.h"
#include "${info.title.lowercase}.h"

/* ${IMPORTS} */


${if.implementations}
namespace Firebolt {
namespace ${info.Title} {
${if.enums}

/* ${ENUMS:json-types} */${end.if.enums}
${if.types}
// Types
/* ${TYPES:json-types} */${end.if.types}
${if.methods}class ${info.Title}Impl : public I${info.Title}, public IModule {

public:
${info.Title}Impl() = default;
${info.Title}Impl(const ${info.Title}Impl&) = delete;
${info.Title}Impl& operator=(const ${info.Title}Impl&) = delete;
~${info.Title}Impl() override = default;

std::string currentState = "INITIALIZING";

// Methods & Events
/* ${METHODS:declarations-override} */

void finished(Firebolt::Error *err = nullptr) override ;
void ready(Firebolt::Error *err = nullptr) override;
std::string state(Firebolt::Error *err = nullptr) override;

};${end.if.methods}

} // namespace ${info.Title}
} // namespace Firebolt
${end.if.implementations}

0 comments on commit 046adc1

Please sign in to comment.