Format state machine module.
This commit is contained in:
parent
9fe9bba31a
commit
f008337684
|
@ -38,10 +38,19 @@ add_custom_target(
|
||||||
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
|
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
|
||||||
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/StateMachine
|
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/StateMachine
|
||||||
)
|
)
|
||||||
|
file(GLOB_RECURSE HEADER_FILES *.h)
|
||||||
|
add_custom_target(
|
||||||
|
StateMahince_code_format
|
||||||
|
COMMAND ${CLANG_FORMAT_EXE}
|
||||||
|
-style=file
|
||||||
|
-i ${SRC_FILES} ${HEADER_FILES}
|
||||||
|
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/StateMachine
|
||||||
|
)
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
TARGET ${TARGET_NAME}
|
TARGET ${TARGET_NAME}
|
||||||
PRE_BUILD
|
PRE_BUILD
|
||||||
COMMAND make StateMahince_code_check
|
COMMAND make StateMahince_code_check
|
||||||
|
COMMAND make StateMahince_code_format
|
||||||
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
|
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
|
@ -57,7 +57,10 @@ public:
|
||||||
virtual void SendMessage(int msgName) {}
|
virtual void SendMessage(int msgName) {}
|
||||||
virtual void StopHandlerThread() {}
|
virtual void StopHandlerThread() {}
|
||||||
virtual void SendMessage(int msgName, const std::shared_ptr<VStateMessage> &messageObj) {}
|
virtual void SendMessage(int msgName, const std::shared_ptr<VStateMessage> &messageObj) {}
|
||||||
virtual void MessageExecutedLater(int msgName, const std::shared_ptr<VStateMessage> &messageObj, int64_t delayTimeMs) {}
|
virtual void MessageExecutedLater(int msgName, const std::shared_ptr<VStateMessage> &messageObj,
|
||||||
|
int64_t delayTimeMs)
|
||||||
|
{
|
||||||
|
}
|
||||||
virtual void SwitchState(State *targetState) {}
|
virtual void SwitchState(State *targetState) {}
|
||||||
virtual void StopTimer(int timerName) {}
|
virtual void StopTimer(int timerName) {}
|
||||||
virtual void DelayMessage(VStateMachineData *msg) {}
|
virtual void DelayMessage(VStateMachineData *msg) {}
|
||||||
|
|
|
@ -19,22 +19,18 @@ std::shared_ptr<IStateMachine> &IStateMachine::GetInstance(std::shared_ptr<IStat
|
||||||
{
|
{
|
||||||
static std::shared_ptr<IStateMachine> instance = std::make_shared<IStateMachine>();
|
static std::shared_ptr<IStateMachine> instance = std::make_shared<IStateMachine>();
|
||||||
static bool instanceChanging = false;
|
static bool instanceChanging = false;
|
||||||
if (impl && false == instanceChanging)
|
if (impl && false == instanceChanging) {
|
||||||
{
|
|
||||||
instanceChanging = true;
|
instanceChanging = true;
|
||||||
if (instance.use_count() == 1)
|
if (instance.use_count() == 1) {
|
||||||
{
|
|
||||||
LogInfo("Instance change succeed.\n");
|
LogInfo("Instance change succeed.\n");
|
||||||
instance = *impl;
|
instance = *impl;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Instance change failed, using by some one.\n");
|
LogError("Instance change failed, using by some one.\n");
|
||||||
}
|
}
|
||||||
instanceChanging = false;
|
instanceChanging = false;
|
||||||
}
|
}
|
||||||
if (instanceChanging)
|
if (instanceChanging) {
|
||||||
{
|
|
||||||
static std::shared_ptr<IStateMachine> tmporaryInstance = std::make_shared<IStateMachine>();
|
static std::shared_ptr<IStateMachine> tmporaryInstance = std::make_shared<IStateMachine>();
|
||||||
return tmporaryInstance;
|
return tmporaryInstance;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,15 +16,16 @@
|
||||||
#ifndef OHOS_HANDLER_H
|
#ifndef OHOS_HANDLER_H
|
||||||
#define OHOS_HANDLER_H
|
#define OHOS_HANDLER_H
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
#include "internal_message.h"
|
#include "internal_message.h"
|
||||||
#include "message_queue.h"
|
#include "message_queue.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
// namespace OHOS {
|
// namespace OHOS {
|
||||||
// namespace Wifi {
|
// namespace Wifi {
|
||||||
const int USEC_1000 = 1000;
|
const int USEC_1000 = 1000;
|
||||||
|
|
||||||
class Handler {
|
class Handler
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @Description : Construct a new Handler:: Handler object.
|
* @Description : Construct a new Handler:: Handler object.
|
||||||
|
|
|
@ -26,7 +26,8 @@
|
||||||
// namespace OHOS {
|
// namespace OHOS {
|
||||||
// namespace Wifi {
|
// namespace Wifi {
|
||||||
const int MAX_POOL_SIZE_INIT = 50;
|
const int MAX_POOL_SIZE_INIT = 50;
|
||||||
class MessageBody {
|
class MessageBody
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @Description : Save an Integer Data.
|
* @Description : Save an Integer Data.
|
||||||
|
@ -76,7 +77,8 @@ private:
|
||||||
std::list<std::string> stringArray_;
|
std::list<std::string> stringArray_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class InternalMessage : public VStateMachineData {
|
class InternalMessage : public VStateMachineData
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @Description : Construct a new Internal Message object.
|
* @Description : Construct a new Internal Message object.
|
||||||
|
@ -196,10 +198,7 @@ public:
|
||||||
// mMessageObj = T(messageObj);
|
// mMessageObj = T(messageObj);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
void SetMessageObj(const std::shared_ptr<VStateMessage> &messageObj)
|
void SetMessageObj(const std::shared_ptr<VStateMessage> &messageObj) { mMessageObj = messageObj; }
|
||||||
{
|
|
||||||
mMessageObj = messageObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @DescriptionGet the Message Obj object
|
* @DescriptionGet the Message Obj object
|
||||||
|
@ -224,10 +223,7 @@ public:
|
||||||
// {
|
// {
|
||||||
// return mMessageObj;
|
// return mMessageObj;
|
||||||
// }
|
// }
|
||||||
const std::shared_ptr<VStateMessage> &GetMessageObj(void) const override
|
const std::shared_ptr<VStateMessage> &GetMessageObj(void) const override { return mMessageObj; }
|
||||||
{
|
|
||||||
return mMessageObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description : Release Message Object.
|
* @Description : Release Message Object.
|
||||||
|
@ -286,7 +282,8 @@ private:
|
||||||
/* Message execution time */
|
/* Message execution time */
|
||||||
int64_t mHandleTime;
|
int64_t mHandleTime;
|
||||||
};
|
};
|
||||||
class MessageManage {
|
class MessageManage
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @Description : Obtains a single instance.
|
* @Description : Obtains a single instance.
|
||||||
|
@ -346,7 +343,8 @@ public:
|
||||||
* @param messageObj - Message pointer.[in]
|
* @param messageObj - Message pointer.[in]
|
||||||
* @return InternalMessage*
|
* @return InternalMessage*
|
||||||
*/
|
*/
|
||||||
InternalMessage *CreateMessage(int messageName, int param1, int param2, const std::shared_ptr<VStateMessage> &messageObj);
|
InternalMessage *CreateMessage(int messageName, int param1, int param2,
|
||||||
|
const std::shared_ptr<VStateMessage> &messageObj);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description :Recycle message.
|
* @Description :Recycle message.
|
||||||
|
|
|
@ -16,16 +16,17 @@
|
||||||
#ifndef OHOS_MESSAGE_QUEUE_H
|
#ifndef OHOS_MESSAGE_QUEUE_H
|
||||||
#define OHOS_MESSAGE_QUEUE_H
|
#define OHOS_MESSAGE_QUEUE_H
|
||||||
|
|
||||||
|
#include "internal_message.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "internal_message.h"
|
|
||||||
|
|
||||||
// namespace OHOS {
|
// namespace OHOS {
|
||||||
// namespace Wifi {
|
// namespace Wifi {
|
||||||
#define TIME_USEC_1000 1000
|
#define TIME_USEC_1000 1000
|
||||||
#define TIME_INTERVAL 30000
|
#define TIME_INTERVAL 30000
|
||||||
class MessageQueue {
|
class MessageQueue
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @Description : Construct a new Message Queue object.
|
* @Description : Construct a new Message Queue object.
|
||||||
|
|
|
@ -16,8 +16,8 @@
|
||||||
#ifndef OHOS_STATE_H
|
#ifndef OHOS_STATE_H
|
||||||
#define OHOS_STATE_H
|
#define OHOS_STATE_H
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include "message_queue.h"
|
#include "message_queue.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// namespace OHOS {
|
// namespace OHOS {
|
||||||
// namespace Wifi {
|
// namespace Wifi {
|
||||||
|
|
|
@ -16,12 +16,12 @@
|
||||||
#ifndef OHOS_STA_MACHINE_H
|
#ifndef OHOS_STA_MACHINE_H
|
||||||
#define OHOS_STA_MACHINE_H
|
#define OHOS_STA_MACHINE_H
|
||||||
|
|
||||||
|
#include "IStateMachine.h"
|
||||||
|
#include "handler.h"
|
||||||
|
#include "message_queue.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "handler.h"
|
|
||||||
#include "message_queue.h"
|
|
||||||
#include "IStateMachine.h"
|
|
||||||
|
|
||||||
// namespace OHOS {
|
// namespace OHOS {
|
||||||
// namespace Wifi {
|
// namespace Wifi {
|
||||||
|
@ -140,7 +140,8 @@ public:
|
||||||
* @param messageObj - User-defined data
|
* @param messageObj - User-defined data
|
||||||
* @return InternalMessage* : Pointer to the constructed internal message.
|
* @return InternalMessage* : Pointer to the constructed internal message.
|
||||||
*/
|
*/
|
||||||
InternalMessage *CreateMessage(int msgName, int param1, int param2, const std::shared_ptr<VStateMessage> &messageObj);
|
InternalMessage *CreateMessage(int msgName, int param1, int param2,
|
||||||
|
const std::shared_ptr<VStateMessage> &messageObj);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description : Constructs internal messages and places the
|
* @Description : Constructs internal messages and places the
|
||||||
|
@ -246,7 +247,8 @@ public:
|
||||||
* @param messageObj -User-defined data
|
* @param messageObj -User-defined data
|
||||||
* @param delayTimeMs - Delay time, in milliseconds.[in]
|
* @param delayTimeMs - Delay time, in milliseconds.[in]
|
||||||
*/
|
*/
|
||||||
void MessageExecutedLater(int msgName, const std::shared_ptr<VStateMessage> &messageObj, int64_t delayTimeMs) override;
|
void MessageExecutedLater(int msgName, const std::shared_ptr<VStateMessage> &messageObj,
|
||||||
|
int64_t delayTimeMs) override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description : Constructs internal messages and places them in the
|
* @Description : Constructs internal messages and places them in the
|
||||||
|
@ -259,7 +261,8 @@ public:
|
||||||
* @param messageObj - User-defined data
|
* @param messageObj - User-defined data
|
||||||
* @param delayTimeMs - Delay time, in milliseconds.[in]
|
* @param delayTimeMs - Delay time, in milliseconds.[in]
|
||||||
*/
|
*/
|
||||||
void MessageExecutedLater(int msgName, int param1, int param2, const std::shared_ptr<VStateMessage> &messageObj, int64_t delayTimeMs);
|
void MessageExecutedLater(int msgName, int param1, int param2, const std::shared_ptr<VStateMessage> &messageObj,
|
||||||
|
int64_t delayTimeMs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description : Construct a new State Machine:: State Machine object.
|
* @Description : Construct a new State Machine:: State Machine object.
|
||||||
|
|
|
@ -14,16 +14,15 @@
|
||||||
*/
|
*/
|
||||||
#include "StateMachineMakePtr.h"
|
#include "StateMachineMakePtr.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include "state_machine.h"
|
|
||||||
#include "IStateMachine.h"
|
#include "IStateMachine.h"
|
||||||
#include "StateMachineImpl.h"
|
#include "StateMachineImpl.h"
|
||||||
|
#include "state_machine.h"
|
||||||
#include <thread>
|
#include <thread>
|
||||||
bool CreateStateMachine(void)
|
bool CreateStateMachine(void)
|
||||||
{
|
{
|
||||||
auto instance = std::make_shared<IStateMachine>();
|
auto instance = std::make_shared<IStateMachine>();
|
||||||
StatusCode code = StateMachineMakePtr::GetInstance()->CreateStateMachine(instance);
|
StatusCode code = StateMachineMakePtr::GetInstance()->CreateStateMachine(instance);
|
||||||
if (IsCodeOK(code))
|
if (IsCodeOK(code)) {
|
||||||
{
|
|
||||||
LogInfo("State machine instance is ok.\n");
|
LogInfo("State machine instance is ok.\n");
|
||||||
IStateMachine::GetInstance(&instance);
|
IStateMachine::GetInstance(&instance);
|
||||||
return true;
|
return true;
|
||||||
|
@ -33,8 +32,7 @@ bool CreateStateMachine(void)
|
||||||
std::shared_ptr<StateMachineMakePtr> &StateMachineMakePtr::GetInstance(std::shared_ptr<StateMachineMakePtr> *impl)
|
std::shared_ptr<StateMachineMakePtr> &StateMachineMakePtr::GetInstance(std::shared_ptr<StateMachineMakePtr> *impl)
|
||||||
{
|
{
|
||||||
static auto instance = std::make_shared<StateMachineMakePtr>();
|
static auto instance = std::make_shared<StateMachineMakePtr>();
|
||||||
if (impl)
|
if (impl) {
|
||||||
{
|
|
||||||
instance = *impl;
|
instance = *impl;
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
|
@ -43,8 +41,7 @@ StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<VStateMachine
|
||||||
{
|
{
|
||||||
// TODO: need a name ?
|
// TODO: need a name ?
|
||||||
auto tmp = std::make_shared<StateMachine>("TODO");
|
auto tmp = std::make_shared<StateMachine>("TODO");
|
||||||
if (tmp)
|
if (tmp) {
|
||||||
{
|
|
||||||
stateMachine = std::move(tmp);
|
stateMachine = std::move(tmp);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
@ -54,8 +51,7 @@ StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<VStateMachine
|
||||||
StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<IStateMachine> &instance)
|
StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<IStateMachine> &instance)
|
||||||
{
|
{
|
||||||
auto tmp = std::make_shared<StateMachineImpl>();
|
auto tmp = std::make_shared<StateMachineImpl>();
|
||||||
if (tmp)
|
if (tmp) {
|
||||||
{
|
|
||||||
instance = std::move(tmp);
|
instance = std::move(tmp);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
*/
|
*/
|
||||||
#ifndef STATE_MACHINE_MAKE_PTR_H
|
#ifndef STATE_MACHINE_MAKE_PTR_H
|
||||||
#define STATE_MACHINE_MAKE_PTR_H
|
#define STATE_MACHINE_MAKE_PTR_H
|
||||||
#include "StatusCode.h"
|
|
||||||
#include "IStateMachine.h"
|
#include "IStateMachine.h"
|
||||||
|
#include "StatusCode.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
class StateMachineMakePtr
|
class StateMachineMakePtr
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,4 +4,3 @@ add_subdirectory(Config)
|
||||||
add_subdirectory(StatusCode)
|
add_subdirectory(StatusCode)
|
||||||
add_subdirectory(Log)
|
add_subdirectory(Log)
|
||||||
add_subdirectory(SharedData)
|
add_subdirectory(SharedData)
|
||||||
add_subdirectory(SharedMemory)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user