Format state machine module.

This commit is contained in:
fancy 2023-11-11 17:06:46 -08:00
parent 9fe9bba31a
commit f008337684
13 changed files with 61 additions and 55 deletions

View File

@ -38,10 +38,19 @@ add_custom_target(
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
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(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make StateMahince_code_check
COMMAND make StateMahince_code_format
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
)
endif()

View File

@ -57,7 +57,10 @@ public:
virtual void SendMessage(int msgName) {}
virtual void StopHandlerThread() {}
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 StopTimer(int timerName) {}
virtual void DelayMessage(VStateMachineData *msg) {}

View File

@ -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 bool instanceChanging = false;
if (impl && false == instanceChanging)
{
if (impl && false == instanceChanging) {
instanceChanging = true;
if (instance.use_count() == 1)
{
if (instance.use_count() == 1) {
LogInfo("Instance change succeed.\n");
instance = *impl;
}
else
{
else {
LogError("Instance change failed, using by some one.\n");
}
instanceChanging = false;
}
if (instanceChanging)
{
if (instanceChanging) {
static std::shared_ptr<IStateMachine> tmporaryInstance = std::make_shared<IStateMachine>();
return tmporaryInstance;
}

View File

@ -16,15 +16,16 @@
#ifndef OHOS_HANDLER_H
#define OHOS_HANDLER_H
#include <pthread.h>
#include "internal_message.h"
#include "message_queue.h"
#include <pthread.h>
// namespace OHOS {
// namespace Wifi {
const int USEC_1000 = 1000;
class Handler {
class Handler
{
public:
/**
* @Description : Construct a new Handler:: Handler object.

View File

@ -26,7 +26,8 @@
// namespace OHOS {
// namespace Wifi {
const int MAX_POOL_SIZE_INIT = 50;
class MessageBody {
class MessageBody
{
public:
/**
* @Description : Save an Integer Data.
@ -76,7 +77,8 @@ private:
std::list<std::string> stringArray_;
};
class InternalMessage : public VStateMachineData {
class InternalMessage : public VStateMachineData
{
public:
/**
* @Description : Construct a new Internal Message object.
@ -196,10 +198,7 @@ public:
// mMessageObj = T(messageObj);
// }
void SetMessageObj(const std::shared_ptr<VStateMessage> &messageObj)
{
mMessageObj = messageObj;
}
void SetMessageObj(const std::shared_ptr<VStateMessage> &messageObj) { mMessageObj = messageObj; }
/**
* @DescriptionGet the Message Obj object
@ -224,10 +223,7 @@ public:
// {
// return mMessageObj;
// }
const std::shared_ptr<VStateMessage> &GetMessageObj(void) const override
{
return mMessageObj;
}
const std::shared_ptr<VStateMessage> &GetMessageObj(void) const override { return mMessageObj; }
/**
* @Description : Release Message Object.
@ -286,7 +282,8 @@ private:
/* Message execution time */
int64_t mHandleTime;
};
class MessageManage {
class MessageManage
{
public:
/**
* @Description : Obtains a single instance.
@ -346,7 +343,8 @@ public:
* @param messageObj - Message pointer.[in]
* @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.

View File

@ -16,16 +16,17 @@
#ifndef OHOS_MESSAGE_QUEUE_H
#define OHOS_MESSAGE_QUEUE_H
#include "internal_message.h"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include "internal_message.h"
// namespace OHOS {
// namespace Wifi {
#define TIME_USEC_1000 1000
#define TIME_INTERVAL 30000
class MessageQueue {
class MessageQueue
{
public:
/**
* @Description : Construct a new Message Queue object.

View File

@ -16,8 +16,8 @@
#ifndef OHOS_STATE_H
#define OHOS_STATE_H
#include <iostream>
#include "message_queue.h"
#include <iostream>
// namespace OHOS {
// namespace Wifi {

View File

@ -16,12 +16,12 @@
#ifndef OHOS_STA_MACHINE_H
#define OHOS_STA_MACHINE_H
#include "IStateMachine.h"
#include "handler.h"
#include "message_queue.h"
#include <map>
#include <string>
#include <vector>
#include "handler.h"
#include "message_queue.h"
#include "IStateMachine.h"
// namespace OHOS {
// namespace Wifi {
@ -140,7 +140,8 @@ public:
* @param messageObj - User-defined data
* @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
@ -246,7 +247,8 @@ public:
* @param messageObj -User-defined data
* @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
@ -259,7 +261,8 @@ public:
* @param messageObj - User-defined data
* @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.

View File

@ -14,16 +14,15 @@
*/
#include "StateMachineMakePtr.h"
#include "ILog.h"
#include "state_machine.h"
#include "IStateMachine.h"
#include "StateMachineImpl.h"
#include "state_machine.h"
#include <thread>
bool CreateStateMachine(void)
{
auto instance = std::make_shared<IStateMachine>();
StatusCode code = StateMachineMakePtr::GetInstance()->CreateStateMachine(instance);
if (IsCodeOK(code))
{
if (IsCodeOK(code)) {
LogInfo("State machine instance is ok.\n");
IStateMachine::GetInstance(&instance);
return true;
@ -33,8 +32,7 @@ bool CreateStateMachine(void)
std::shared_ptr<StateMachineMakePtr> &StateMachineMakePtr::GetInstance(std::shared_ptr<StateMachineMakePtr> *impl)
{
static auto instance = std::make_shared<StateMachineMakePtr>();
if (impl)
{
if (impl) {
instance = *impl;
}
return instance;
@ -43,8 +41,7 @@ StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<VStateMachine
{
// TODO: need a name ?
auto tmp = std::make_shared<StateMachine>("TODO");
if (tmp)
{
if (tmp) {
stateMachine = std::move(tmp);
return CreateStatusCode(STATUS_CODE_OK);
}
@ -54,8 +51,7 @@ StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<VStateMachine
StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr<IStateMachine> &instance)
{
auto tmp = std::make_shared<StateMachineImpl>();
if (tmp)
{
if (tmp) {
instance = std::move(tmp);
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -14,8 +14,8 @@
*/
#ifndef STATE_MACHINE_MAKE_PTR_H
#define STATE_MACHINE_MAKE_PTR_H
#include "StatusCode.h"
#include "IStateMachine.h"
#include "StatusCode.h"
#include <memory>
class StateMachineMakePtr
{

View File

@ -4,4 +4,3 @@ add_subdirectory(Config)
add_subdirectory(StatusCode)
add_subdirectory(Log)
add_subdirectory(SharedData)
add_subdirectory(SharedMemory)