Improve:SixFrame tcp protocol.

This commit is contained in:
Fancy code 2024-04-13 10:52:20 +08:00
parent bb0180882c
commit 3ba9078ef9
4 changed files with 231 additions and 6 deletions

View File

@ -47,6 +47,22 @@ const char *APP_GET_THUMBNAIL = "/app/getthumbnail";
// /app/exitrecorder // /app/exitrecorder
// clang-format on // clang-format on
constexpr bool SET_REQUEST_RESPONSE = true; constexpr bool SET_REQUEST_RESPONSE = true;
class CjsonAutoFree
{
public:
CjsonAutoFree(cJSON *json) : mJson(json)
{
}
~CjsonAutoFree()
{
if (mJson != nullptr) {
cJSON_Delete(mJson);
}
}
private:
cJSON *mJson;
};
SixFrameHandle::SixFrameHandle() SixFrameHandle::SixFrameHandle()
{ {
mAppMonitor = std::make_shared<VAppMonitor>(); mAppMonitor = std::make_shared<VAppMonitor>();
@ -689,7 +705,8 @@ void SixFrameHandle::RequestTcpHandle2(const std::string command, const cJSON *c
} }
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetRecordingStatus(const RecordingStatus &status) std::shared_ptr<ProtocolPacket> SixFrameHandle::SetRecordingStatus(const RecordingStatus &status)
{ {
cJSON *resultCJSON = cJSON_CreateObject(); cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) { if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n"); LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>(); return std::make_shared<ProtocolPacket>();
@ -705,24 +722,92 @@ std::shared_ptr<ProtocolPacket> SixFrameHandle::SetRecordingStatus(const Recordi
} }
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetMicrophoneStatus(const MicrophoneStatus &status) std::shared_ptr<ProtocolPacket> SixFrameHandle::SetMicrophoneStatus(const MicrophoneStatus &status)
{ {
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>(); return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "mic");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "value", static_cast<int>(status));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
} }
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetBatteryStatus(const BatteryStatus &status, std::shared_ptr<ProtocolPacket> SixFrameHandle::SetBatteryStatus(const BatteryStatus &status,
const int &batteryCapacity) const int &batteryCapacity)
{ {
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>(); return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "battery");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "charge", static_cast<int>(status));
cJSON_AddNumberToObject(info, "capacity", batteryCapacity);
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
} }
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetSdCardStatus(const SdCardStatus &status) std::shared_ptr<ProtocolPacket> SixFrameHandle::SetSdCardStatus(const SdCardStatus &status)
{ {
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>(); return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "sd");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "status", static_cast<int>(status));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
} }
std::shared_ptr<ProtocolPacket> SixFrameHandle::DeletedFileMessage(const std::string &file, const StorageFileType &type) std::shared_ptr<ProtocolPacket> SixFrameHandle::DeletedFileMessage(const std::string &file, const StorageFileType &type)
{ {
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>(); return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "file_del");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddStringToObject(info, "name", file.c_str());
cJSON_AddNumberToObject(info, "type", static_cast<int>(type));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
} }
std::shared_ptr<ProtocolPacket> SixFrameHandle::CreatedFileMessage(const std::string &file, const StorageFileType &type) std::shared_ptr<ProtocolPacket> SixFrameHandle::CreatedFileMessage(const std::string &file, const StorageFileType &type)
{ {
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>(); return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "file_add");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddStringToObject(info, "name", file.c_str());
cJSON_AddNumberToObject(info, "type", static_cast<int>(type));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
} }
cJSON *SixFrameHandle::MakeResponseResult(const ResposeResult result, const bool requestSet) cJSON *SixFrameHandle::MakeResponseResult(const ResposeResult result, const bool requestSet)
{ {

View File

@ -65,6 +65,8 @@ TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo0)
IAppManager::GetInstance()->SetAppMonitor(monitor); IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect(); MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetRecordingStatus(RecordingStatus::RECORDING_START);
std::this_thread::sleep_for(std::chrono::milliseconds(2000)); std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit(); IAppManager::GetInstance()->UnInit();
} }
@ -256,4 +258,88 @@ TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_AppPlayback)
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit(); IAppManager::GetInstance()->UnInit();
} }
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetRecordingStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetRecordingStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetRecordingStatus(RecordingStatus::RECORDING_START);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetMicrophoneStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetMicrophoneStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetMicrophoneStatus(MicrophoneStatus::ON);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetBatteryStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetBatteryStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetBatteryStatus(BatteryStatus::CHARGING, 20);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetSdCardStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetSdCardStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetSdCardStatus(SdCardStatus::NOT_INSERTED);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_DeletedFileMessage
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_DeletedFileMessage)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockDeletedFileMessage("file_name", StorageFileType::VIDEO);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_CreatedFileMessage
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_CreatedFileMessage)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockCreatedFileMessage("file_name", StorageFileType::VIDEO);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
} // namespace AppManagerTest } // namespace AppManagerTest

View File

@ -46,6 +46,12 @@ protected:
protected: protected:
void MockAppClientConnect(void); void MockAppClientConnect(void);
void MockSetRecordingStatus(const RecordingStatus &status);
void MockSetMicrophoneStatus(const MicrophoneStatus &status);
void MockSetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity);
void MockSetSdCardStatus(const SdCardStatus &status);
void MockDeletedFileMessage(const std::string &file, const StorageFileType &type);
void MockCreatedFileMessage(const std::string &file, const StorageFileType &type);
private: private:
void AppManagerMockInit(std::shared_ptr<IAppManager> &vMock); void AppManagerMockInit(std::shared_ptr<IAppManager> &vMock);

View File

@ -239,6 +239,54 @@ void AppManagerTestTool::MockAppClientConnect(void)
LogError("CreateTcpClient failed.\n"); LogError("CreateTcpClient failed.\n");
} }
} }
void AppManagerTestTool::MockSetRecordingStatus(const RecordingStatus &status)
{
if (mAppClient) {
mAppClient->SetRecordingStatus(status);
return;
}
LogWarning("mAppClient is nullptr.\n");
}
void AppManagerTestTool::MockSetMicrophoneStatus(const MicrophoneStatus &status)
{
if (mAppClient) {
mAppClient->SetMicrophoneStatus(status);
return;
}
LogWarning("mAppClient is nullptr.\n");
}
void AppManagerTestTool::MockSetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity)
{
if (mAppClient) {
mAppClient->SetBatteryStatus(status, batteryCapacity);
return;
}
LogWarning("mAppClient is nullptr.\n");
}
void AppManagerTestTool::MockSetSdCardStatus(const SdCardStatus &status)
{
if (mAppClient) {
mAppClient->SetSdCardStatus(status);
return;
}
LogWarning("mAppClient is nullptr.\n");
}
void AppManagerTestTool::MockDeletedFileMessage(const std::string &file, const StorageFileType &type)
{
if (mAppClient) {
mAppClient->DeletedFileMessage(file, type);
return;
}
LogWarning("mAppClient is nullptr.\n");
}
void AppManagerTestTool::MockCreatedFileMessage(const std::string &file, const StorageFileType &type)
{
if (mAppClient) {
mAppClient->CreatedFileMessage(file, type);
return;
}
LogWarning("mAppClient is nullptr.\n");
}
void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock) void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock)
{ {
std::shared_ptr<AppManagerMock> mock = std::dynamic_pointer_cast<AppManagerMock>(vMock); std::shared_ptr<AppManagerMock> mock = std::dynamic_pointer_cast<AppManagerMock>(vMock);