servicedatas.cpp 3.02 KB
#include "string.h"
#include "servicedatas.h"
#include <QDebug>
#include <fcntl.h>
#include <unistd.h>        // write(), close()

#define FRAM_SIZE 2048
#define FRAM_TEST_PROCESS   0

#define fRam_path  "/sys/bus/spi/devices/spi0.0/fram"
#define INIT_FRAM   1


servicedatas::servicedatas()
{
    qDebug()<< "Statics Data Size Report\r\n error_log size = " << sizeof(error_log) << " \r\nuse_static_log size = " << sizeof(use_statics_log) \
            << "\r\nsensor_statics_log size = " << sizeof(sensor_statics_log);
    memset((void*)err_log.datas,0x00,sizeof(error_log));
    memset((void*)use_log.datas,0x00,sizeof(use_statics_log));
    memset((void*)sensor_log.datas,0x00, sizeof(sensor_statics_log));
#if INIT_FRAM == 1
    saveServiceDatas();
#else
    loadServiceDatas();
#endif
}



bool servicedatas::loadServiceDatas(void){
    uint8_t buffs[FRAM_SIZE];
    int fd;

#if FRAM_TEST_PROCESS == 1
    int i;
    memset(buffs,0x00,256);
    for(i=0;i<256;i++){
        buffs[i] = i;
    }
    fd = open(fRam_path,  O_RDWR);
    if(fd>0){
        write(fd,buffs,256);
        close(fd);
    }
    else{
        qDebug()<<"FRAM open fail!";
        return false;
    }
    i=0;
    memset(buffs,0x00,256);
    fd = open(fRam_path,  O_RDONLY );
    if(fd>0){
         read(fd,buffs,256);
         close(fd);
    }else{
        qDebug()<<"FRAM open fail!";
        return false;
    }
    for(i=0;i<256;i++){
        if(i !=buffs[i]) {
            qDebug()<<"FRAM Test Fail";
            return false;
        }
    }
    qDebug()<<"FRAM Test Success!";
    return true;
#endif






    fd = open(fRam_path,  O_RDONLY );
    if(fd>0){
        memset(buffs,0x00,FRAM_SIZE);
        read(fd,buffs,FRAM_SIZE);
       if(buffs[sizeof(error_log) + sizeof(use_statics_log) + sizeof(sensor_statics_log)] != 0x9C){
            close(fd);
           return saveServiceDatas();
        }

        qDebug() << "FRAM Read, Write Size is " << sizeof(error_log)+sizeof(use_statics_log);
        memcpy((void*)err_log.datas,buffs,sizeof(error_log));
        memcpy((void*)use_log.datas, (void*)(&buffs[sizeof(error_log)]),sizeof(use_statics_log));
        memcpy((void*)sensor_log.datas,(void*)(&buffs[sizeof(error_log) + sizeof(use_statics_log)]),sizeof(sensor_statics_log));
        close(fd);
    }else{
        qDebug()<<"FRAM FILE Open fail!!";
    }
   return true;
}

bool servicedatas::saveServiceDatas(void){
    uint8_t buffs[FRAM_SIZE];
    int fd;
    fd = open(fRam_path,  O_RDWR | O_SYNC);
    if(fd>0){
        memset(buffs,0x00,FRAM_SIZE);
        memcpy(buffs,(void*)err_log.datas,sizeof(error_log));
        memcpy((void*)(&buffs[sizeof(error_log)]),(void*)use_log.datas,sizeof(use_statics_log));
        memcpy((void*)(&buffs[sizeof(error_log) + sizeof(use_statics_log)]),(void*)sensor_log.datas,sizeof(sensor_statics_log));
        buffs[sizeof(error_log) + sizeof(use_statics_log) + sizeof(sensor_statics_log)] = 0x9C;
        write(fd,buffs,FRAM_SIZE);
        close(fd);
    }else{
        qDebug()<<"FRAM FILE Open fail!!";
        return false;
    }
   return true;
}