servicedata.cpp
3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "string.h"
#include "servicedata.h"
#include <QDebug>
#include <fcntl.h>
#include <unistd.h> // write(), close()
#include <dirtylevel.h>
#define FRAM_SIZE 2048
#define FRAM_TEST_PROCESS 0
#define fRam_path "/sys/bus/spi/devices/spi0.0/fram"
#define INIT_FRAM 0 //시작시 RRAM 초기화
ServiceData::ServiceData()
{
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.data,0x00,sizeof(error_log));
memset((void*)use_log.data,0x00,sizeof(use_statics_log));
memset((void*)sensor_log.data,0x00, sizeof(sensor_statics_log));
DirtyLevel::setCookingCount(0);
DirtyLevel::setCookingTime(0);
#if INIT_FRAM == 1
saveServiceData();
#else
loadServiceData();
#endif
}
bool ServiceData::loadServiceData(void){
uint8_t buffs[FRAM_SIZE];
uint8_t* pBuff;
int fd;
qint64 temp_cookingTime = 0;
int temp_cookingCount = 0;
pBuff = buffs;
#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) + sizeof(qint64) + sizeof(int)] != 0x9C){
qDebug() << "service data read incorrected!";
close(fd);
return saveServiceData();
}
qDebug() << "FRAM Read, Write Size is " << sizeof(error_log)+sizeof(use_statics_log);
memcpy((void*)err_log.data,pBuff,sizeof(error_log)); pBuff += sizeof(error_log);
memcpy((void*)use_log.data, pBuff,sizeof(use_statics_log));pBuff += sizeof(use_statics_log);
memcpy((void*)sensor_log.data,pBuff,sizeof(sensor_statics_log)); pBuff+= sizeof(sensor_statics_log);
memcpy(&temp_cookingTime,pBuff, sizeof(qint64)); pBuff += sizeof(qint64);
memcpy(&temp_cookingCount,pBuff, sizeof(int));
DirtyLevel::setCookingCount(temp_cookingCount);
DirtyLevel::setCookingTime(temp_cookingTime);
close(fd);
qDebug() <<"FRAM Load Success";
}else{
qDebug()<<"FRAM FILE Open fail!!";
}
return true;
}
bool ServiceData::resetSensorlogData()
{
memset((void*)sensor_log.data,0x00, sizeof(sensor_statics_log));
saveServiceData();
}
bool ServiceData::saveServiceData(void){
uint8_t buffs[FRAM_SIZE];
uint8_t* pBuff;
int fd;
qint64 temp_cookingTime = DirtyLevel::cookingTime();
int temp_cookingCount = DirtyLevel::cookingCount();
pBuff = buffs;
fd = open(fRam_path, O_RDWR | O_SYNC);
if(fd>0){
memset(buffs,0x00,FRAM_SIZE);
memcpy(pBuff,(void*)err_log.data,sizeof(error_log)); pBuff += sizeof(err_log);
memcpy(pBuff,(void*)use_log.data,sizeof(use_statics_log)); pBuff += sizeof(use_statics_log);
memcpy(pBuff,(void*)sensor_log.data,sizeof(sensor_statics_log)); pBuff += sizeof(sensor_statics_log);
memcpy(pBuff, &temp_cookingTime, sizeof(qint64)); pBuff += sizeof(qint64);
memcpy(pBuff, &temp_cookingCount, sizeof(int)); pBuff += sizeof(int);
*pBuff = 0x9C;
write(fd,buffs,FRAM_SIZE);
close(fd);
}else{
qDebug()<<"FRAM FILE Open fail!!";
return false;
}
return true;
}