数据存储和补发,最多存储7天数据
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#define MAX_STORE_FREAM 12/*120960*/ //5S每帧,7天 (7 * 60 * 24 * 60/5) = 120960//604800
#define MSG_PER_FREAM_VEHI 500
#define MAX_STORE_SIZE_VEHI (MAX_STORE_FREAM * MSG_PER_FREAM_VEHI) //60480 帧
pthread_mutex_t vehiLocker;
static const char* vehiFileName="vehiMsgStorage.txt"; //BMPro
int vehiWriteIndex;
int vehiReadIndex;
pthread_cond_t vehiNotEmpty;
bool vehiIsFull()
{
if((vehiWriteIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI == vehiReadIndex)
{
return true;
}
return false;
}
int vehiStoreMsg(uint8_t* msg)
{
if(!msg)
{
return -1;
}
pthread_mutex_lock(&vehiLocker);
int fd = open(vehiFileName, O_RDWR|O_CREAT, 0777);
if(fd < 0)
{
goto FAILED;
}
printf("vehiStoreMsg,write---%d \n", vehiWriteIndex);
if(vehiIsFull())
{
printf("isFull read--%d write-- %d\n", vehiReadIndex, vehiWriteIndex);
vehiReadIndex = (vehiReadIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI;
}
if(lseek(fd,vehiWriteIndex, SEEK_SET) < 0)
{
goto FAILED;
}
if(write(fd, msg, MSG_PER_FREAM_VEHI) < MSG_PER_FREAM_VEHI)
{
goto FAILED;
}
vehiWriteIndex = (vehiWriteIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI; //下一个写指针位置!!!
printf("vehiStoreMsg is OK read = %ld write = %ld \n",vehiReadIndex,vehiWriteIndex);
close(fd);
pthread_cond_signal(&vehiNotEmpty);
pthread_mutex_unlock(&vehiLocker);
return 0;
FAILED:
if(fd >= 0)
{
close(fd);
}
printf("[ERROR]vehiStoreMsg read--%d write-- %d\n", vehiReadIndex,vehiWriteIndex);
pthread_mutex_unlock(&vehiLocker);
return -1;
}
bool vehiIsEmpty()
{
if(vehiWriteIndex == vehiReadIndex)
{
return true;
}
return false;
}
int vehiRestoreMsg(uint8_t* msg)
{
if(!msg)
{
return -1;
}
pthread_mutex_lock(&vehiLocker);
while(vehiIsEmpty())
{
printf("wait the signal at vehiIsEmpty \n");
pthread_cond_wait(&vehiNotEmpty, &vehiLocker);
}
int fd = open(vehiFileName, O_RDONLY,0777);
if(fd < 0)
{
goto FAILED;
}
if(lseek(fd,vehiReadIndex, SEEK_SET) < 0)
{
goto FAILED;
}
if(read(fd, msg, MSG_PER_FREAM_VEHI ) < MSG_PER_FREAM_VEHI)
{
goto FAILED;
}
close(fd);
//20200923 应该是去整除的结果
if (vehiReadIndex % MSG_PER_FREAM_VEHI > 0)
//if(vehiReadIndex / MAX_STORE_SIZE_VEHI > 0)
{
vehiReadIndex = 0;
}
if (vehiWriteIndex % MSG_PER_FREAM_VEHI > 0)
//if(vehiWriteIndex / MAX_STORE_SIZE_VEHI > 0)
{
vehiWriteIndex = 0;
}
printf("vehiRestoreMsg is OK ;read = %d write = %d \n",vehiReadIndex,vehiWriteIndex);
//printRawData(ELOG_LVL_DEBUG,TAG_NONE," read repush data is ",msg,MSG_PER_FREAM_VEHI);
pthread_mutex_unlock(&vehiLocker);
return 0;
FAILED:
if(fd >= 0)
{
close(fd);
}
printf("vehiRestoreMsg is fail,close fd \n");
pthread_mutex_unlock(&vehiLocker);
return -1;
}
bool vehiPopMsg()
{
pthread_mutex_lock(&vehiLocker);
vehiReadIndex = (vehiReadIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI; //将读指针指向下一个位置
printf("vehiPopMsg,manager.vehiInfo.readIndex is---%d // writeIndex is %d,\n", vehiReadIndex,vehiWriteIndex);
pthread_mutex_unlock(&vehiLocker);
return true;
}
int main() {
pthread_mutex_init(&vehiLocker,NULL);
pthread_cond_init(&vehiNotEmpty,NULL);
const char *data = "11111111111111";
unsigned char tmpBuf[MSG_PER_FREAM_VEHI] = {0};
while (true){
sleep(1);
vehiStoreMsg((uint8_t *) data);
vehiRestoreMsg(tmpBuf);
vehiPopMsg();
}
return 0;
}