mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Improve shared data module.
This commit is contained in:
parent
8ff62492e0
commit
1cbcbe5e69
|
@ -56,16 +56,17 @@ namespace SharedDataTest
|
|||
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
|
||||
if (nullptr != sharedDataMinor)
|
||||
{
|
||||
int writableData = 122;
|
||||
int writableData = 189;
|
||||
IMakeSharedData(sharedDataMinor, sizeof(int) * 200, sizeof(int));
|
||||
ISetWritableData(sharedDataMinor, &writableData, sizeof(int));
|
||||
}
|
||||
if (nullptr != sharedDataPrimary)
|
||||
{
|
||||
int readableData[200] = {0};
|
||||
IMakeSharedData(sharedDataPrimary, sizeof(int) * 200, sizeof(int));
|
||||
IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
|
||||
LogInfo("readableData = %d\n", readableData[0]);
|
||||
int readableData[2] = {0};
|
||||
IMakeSharedData(sharedDataPrimary, sizeof(int) * 2, sizeof(int));
|
||||
StatusCode code = IGetReadableData(sharedDataPrimary, readableData, sizeof(int) * 2);
|
||||
PrintStringCode(code);
|
||||
LogInfo("readableData = %d\n", readableData);
|
||||
IFree(sharedDataPrimary);
|
||||
}
|
||||
if (nullptr != sharedDataMinor)
|
||||
|
|
|
@ -19,6 +19,12 @@
|
|||
extern "C"
|
||||
{
|
||||
#endif
|
||||
enum SHARED_DATA_CODE
|
||||
{
|
||||
SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END,
|
||||
SHARED_DATA_CODE_WRONG_PEER_PARAMETERS,
|
||||
SHARED_DATA_CODE_END
|
||||
};
|
||||
enum SHARER_NAME
|
||||
{
|
||||
SHARER_NAME_PRIMARY = 0,
|
||||
|
@ -30,11 +36,11 @@ extern "C"
|
|||
{
|
||||
const StatusCode (*mMakeSharedData)(SharedData *, const unsigned int, const unsigned int);
|
||||
const StatusCode (*mCleanSharedData)(SharedData *);
|
||||
void (*mGetReadableData)(SharedData *, void *, const unsigned int);
|
||||
const StatusCode (*mGetReadableData)(SharedData *, void *, const unsigned int);
|
||||
void (*mSetWritableData)(SharedData *, void *, const unsigned int);
|
||||
void (*mFree)(void *);
|
||||
} SharedData;
|
||||
SharedData *CreateSharedData(const SHARER_NAME name, const char *path, const int projectId);
|
||||
SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId);
|
||||
static inline const StatusCode IMakeSharedData(SharedData *object,
|
||||
const unsigned int readableSize,
|
||||
const unsigned int writableSize)
|
||||
|
@ -45,11 +51,11 @@ extern "C"
|
|||
{
|
||||
return object->mCleanSharedData(object);
|
||||
}
|
||||
static inline void IGetReadableData(SharedData *object, void *buf, const unsigned int &bufLength)
|
||||
static inline const StatusCode IGetReadableData(SharedData *object, void *buf, const unsigned int bufLength)
|
||||
{
|
||||
object->mGetReadableData(object, buf, bufLength);
|
||||
return object->mGetReadableData(object, buf, bufLength);
|
||||
}
|
||||
static inline void ISetWritableData(SharedData *object, void *buf, const unsigned int &bufLength)
|
||||
static inline void ISetWritableData(SharedData *object, void *buf, const unsigned int bufLength)
|
||||
{
|
||||
object->mSetWritableData(object, buf, bufLength);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
*/
|
||||
#include "SharedData.h"
|
||||
#include "SharedDataImpl.h"
|
||||
SharedData *CreateSharedData(const SHARER_NAME name, const char *path, const int projectId)
|
||||
SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId)
|
||||
{
|
||||
return (SharedData *)NewSharedDataImpl(name, path, projectId);
|
||||
}
|
64
utils/SharedData/src/SharedDataCode.c
Normal file
64
utils/SharedData/src/SharedDataCode.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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.
|
||||
*/
|
||||
#include "SharedDataCode.h"
|
||||
#include "ILog.h"
|
||||
#include <string.h>
|
||||
static const char *StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END + 1] = {
|
||||
"SHARED_DATA_CODE_INIT_FAILED",
|
||||
"SHARED_DATA_CODE_WRONG_PEER_PARAMETERS",
|
||||
"SHARED_DATA_CODE_END"};
|
||||
static const char *_PrintStringCode_(const StatusCode this)
|
||||
{
|
||||
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= SHARED_DATA_CODE_END)
|
||||
{
|
||||
LogInfo("Status code = [ %s ]\n", StatusCodeString[this.mStatusCode - STATUS_CODE_END]);
|
||||
return StatusCodeString[this.mStatusCode - STATUS_CODE_END];
|
||||
}
|
||||
LogError("Status code = [ %s ]\n", StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END]);
|
||||
return StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END];
|
||||
}
|
||||
static const bool CodeEqual(const StatusCode code, const char *value)
|
||||
{
|
||||
if (STATUS_CODE_END >= code.mStatusCode || code.mStatusCode >= SHARED_DATA_CODE_END)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (strlen(value) != strlen(StatusCodeString[code.mStatusCode - STATUS_CODE_END]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (memcmp(value, StatusCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static StatusCode NewStatusCode(const long int code)
|
||||
{
|
||||
StatusCode result = {
|
||||
_PrintStringCode_,
|
||||
CodeEqual,
|
||||
code};
|
||||
return result;
|
||||
}
|
||||
const StatusCode CreateSharedDataCode(const long int code)
|
||||
{
|
||||
if (STATUS_CODE_END <= code && code <= SHARED_DATA_CODE_END)
|
||||
{
|
||||
return NewStatusCode(code);
|
||||
}
|
||||
LogError("undefined code.\n");
|
||||
return NewStatusCode(SHARED_DATA_CODE_END);
|
||||
}
|
40
utils/SharedData/src/SharedDataCode.h
Normal file
40
utils/SharedData/src/SharedDataCode.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* 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.
|
||||
*/
|
||||
#ifndef SHARED_DATA_CODE_H
|
||||
#define SHARED_DATA_CODE_H
|
||||
#include "SharedData.h"
|
||||
#include "StatusCode.h"
|
||||
#include <stdbool.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
const StatusCode CreateSharedDataCode(const long int code);
|
||||
// static inline const char *PrintStringCode(const StatusCode code)
|
||||
// {
|
||||
// return code.mPrintStringCode(code);
|
||||
// }
|
||||
// static inline bool IsCodeOK(const StatusCode code)
|
||||
// {
|
||||
// return SHARED_DATA_CODE_OK == code.mStatusCode ? true : false;
|
||||
// }
|
||||
// static inline bool StatusCodeEqual(const StatusCode code, const char *value)
|
||||
// {
|
||||
// return code.mCodeEqual(code, value);
|
||||
// }
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
|
@ -14,6 +14,7 @@
|
|||
*/
|
||||
#include "SharedDataImpl.h"
|
||||
#include "ILog.h"
|
||||
#include "SharedDataCode.h"
|
||||
#include <cstring>
|
||||
static const char *SHARED_DATA_NAME = "shared_data";
|
||||
constexpr short THERE_TWO_USER_DATA_HEADER = 2;
|
||||
|
@ -57,11 +58,13 @@ void SharedDataCpp::WritableDataInit(void)
|
|||
{
|
||||
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
||||
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
||||
writableHeader->mDataLength = mMinorReadSize;
|
||||
}
|
||||
else if (SHARER_NAME_MINOR == mSharerName)
|
||||
{
|
||||
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
||||
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
||||
writableHeader->mDataLength = mPrimaryReadSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -69,29 +72,34 @@ void SharedDataCpp::WritableDataInit(void)
|
|||
return;
|
||||
}
|
||||
}
|
||||
void SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength)
|
||||
const StatusCode SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength)
|
||||
{
|
||||
if (nullptr == mSharedMemeory)
|
||||
{
|
||||
LogError("mSharedMemeory is nullptr, failed.\n");
|
||||
return;
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize)
|
||||
{
|
||||
if (memcmp((char *)mSharedMemeory, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0)
|
||||
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
||||
LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength);
|
||||
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 &&
|
||||
bufLength == readableHeader->mDataLength)
|
||||
{
|
||||
memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogError("Readable memory didn't init yet.\n");
|
||||
LogError("Readable memory didn't init yet or init error.\n");
|
||||
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
||||
}
|
||||
}
|
||||
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize)
|
||||
{
|
||||
if (memcmp((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize,
|
||||
USER_NAME_INIT_NAME,
|
||||
USER_NAME_BUF_LENGTH) == 0)
|
||||
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
||||
LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength);
|
||||
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 &&
|
||||
bufLength == readableHeader->mDataLength)
|
||||
{
|
||||
memcpy(buf,
|
||||
(char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
||||
|
@ -99,13 +107,16 @@ void SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength)
|
|||
}
|
||||
else
|
||||
{
|
||||
LogError("Readable memory didn't init yet.\n");
|
||||
LogError("Readable memory didn't init yet or init error.\n");
|
||||
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogError("Get readable memory failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
void SharedDataCpp::SetWritableMemory(void *buf, const unsigned int &bufLength)
|
||||
{
|
||||
|
@ -135,10 +146,10 @@ static const StatusCode MakeSharedData(SharedData *object, const unsigned int re
|
|||
impl->mSharedData->MakeSharedMemory(readableSize, writableSize);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
static void GetSharedReadableMemory(SharedData *object, void *buf, const unsigned int bufLength)
|
||||
static const StatusCode GetSharedReadableMemory(SharedData *object, void *buf, const unsigned int bufLength)
|
||||
{
|
||||
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
||||
impl->mSharedData->GetReadableMemory(buf, bufLength);
|
||||
return impl->mSharedData->GetReadableMemory(buf, bufLength);
|
||||
}
|
||||
static void SetSharedWritableMemory(SharedData *object, void *buf, const unsigned int bufLength)
|
||||
{
|
||||
|
|
|
@ -32,6 +32,7 @@ typedef struct shared_data_header
|
|||
typedef struct user_data_header
|
||||
{
|
||||
char mUserName[USER_NAME_BUF_LENGTH];
|
||||
unsigned int mDataLength;
|
||||
int mIsInit;
|
||||
} UserDataHeader;
|
||||
class SharedDataCpp : public SharedMemory
|
||||
|
@ -40,7 +41,7 @@ public:
|
|||
SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId);
|
||||
virtual ~SharedDataCpp() = default;
|
||||
void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize);
|
||||
void GetReadableMemory(void *buf, const unsigned int &bufLength);
|
||||
const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength);
|
||||
void SetWritableMemory(void *buf, const unsigned int &bufLength);
|
||||
|
||||
private:
|
||||
|
|
Loading…
Reference in New Issue
Block a user