ISCommDataBuffer.cpp
Go to the documentation of this file.
1 #include "ISCommDataBuffer.h"
2 #include "ISDataMappings.h"
3 #include "ISFileManager.h"
4 
5 #include <stdio.h>
6 #include <sstream>
7 
8 #define MAX_ITEM_HISTORY 2048
9 
11 
13 {
14  m_lastTimestamp = 0.0;
15  basePath = tempPath();
16  _MKDIR(basePath.c_str());
17  basePath += "InertialSense/";
18  _MKDIR(basePath.c_str());
19  basePath += "ComDataBuffer/";
20  Reset();
21 }
22 
23 
25 {
26  Reset();
27 }
28 
29 
31 {
32  for (size_t i = 0; i < m_buffers.size(); i++)
33  {
34  for (size_t j = 0; j < m_buffers[i].size(); j++)
35  {
36  fclose(m_buffers[i][j].file);
37  fclose(m_timestamps[i][j].file);
38  }
39  }
40  m_buffers.clear();
41  m_timestamps.clear();
42  m_lastTimestamp = 0.0;
44  _MKDIR(basePath.c_str());
45 }
46 
47 
48 int cComDataBuffer::PushData(int pHandle, const p_data_t* data)
49 {
50  if (data->hdr.id == 0 || data->hdr.id >= DID_MAX_COUNT)
51  {
52  return -1;
53  }
54 
55  cMutexLocker lock(&m_mutex);
56 
57  EnsureBuffers(pHandle);
58  float timestamp = (float)cISDataMappings::GetTimestamp(&data->hdr, data->buf);
59  if (timestamp != 0.0f)
60  {
61  m_lastTimestamp = timestamp;
62  }
63 
64  uint32_t structSize = cISDataMappings::GetSize(data->hdr.id);
65  vector<uint8_t> dataBuffer;
66  dataBuffer.reserve(structSize);
67 
68  if (data->hdr.offset + data->hdr.size > structSize)
69  {
70  // push a completely zeroed out data struct, out of bounds / corrupt data
71  for (size_t i = 0; i < structSize; i++)
72  {
73  dataBuffer.push_back(0);
74  }
75  }
76  else
77  {
78  // add 0 for every byte before offset
79  for (size_t i = 0; i < data->hdr.offset; i++)
80  {
81  dataBuffer.push_back(0);
82  }
83  // add the actual data bytes
84  for (size_t i = 0; i < data->hdr.size; i++)
85  {
86  dataBuffer.push_back(data->buf[i]);
87  }
88  // add 0 for every byte after offset + size but before total size
89  for (size_t i = 0; i < structSize - data->hdr.size; i++)
90  {
91  dataBuffer.push_back(0);
92  }
93  }
94 
95  // write timestamp and increment write offset
96  size_t written = fwrite(&timestamp, 1, sizeof(float), m_timestamps[pHandle][data->hdr.id].file);
97  if (written != sizeof(float))
98  {
99  return -1;
100  }
101  else if ((m_timestamps[pHandle][data->hdr.id].writeOffset += sizeof(float)) / sizeof(float) >= MAX_ITEM_HISTORY)
102  {
103  m_timestamps[pHandle][data->hdr.id].writeOffset = 0;
104  fseek(m_timestamps[pHandle][data->hdr.id].file, 0, SEEK_SET);
105  }
106 
107  // write data and increment offset
108  written = fwrite(&dataBuffer[0], 1, dataBuffer.size(), m_buffers[pHandle][data->hdr.id].file);
109  if (written != dataBuffer.size())
110  {
111  return -1;
112  }
113  else if ((m_buffers[pHandle][data->hdr.id].writeOffset += structSize) / structSize >= MAX_ITEM_HISTORY)
114  {
115  m_buffers[pHandle][data->hdr.id].writeOffset = 0;
116  fseek(m_buffers[pHandle][data->hdr.id].file, 0, SEEK_SET);
117  }
118 
119  return 0;
120 }
121 
122 
123 int cComDataBuffer::ReadData(int pHandle, uint32_t dataId, vector<uint8_t>& data)
124 {
125  data.clear();
126 
127  if (dataId == 0 || dataId >= DID_MAX_COUNT)
128  {
129  return -1;
130  }
131 
132  cMutexLocker lock(&m_mutex);
133 
134  struct stat buf;
135  FILE* file = m_buffers[pHandle][dataId].file;
136  fflush(file);
137 #ifdef __linux__
138  fstat(fileno(file), &buf);
139 #else
140  fstat(_fileno(file), &buf);
141 #endif
142  long size = buf.st_size;
143  long pos = ftell(file);
144  long afterWriteOffset = size - pos;
145  size_t read = 0;
146 
147  // read everything after write offset
148  data.resize(size);
149  if (pos < size)
150  {
151  read += fread(&data[0], 1, afterWriteOffset, file);
152  }
153 
154  // read in anything before write offset
155  if (pos != 0)
156  {
157  fseek(file, 0, SEEK_SET);
158  read += fread(&data[afterWriteOffset], 1, pos, file);
159  }
160 
161  if (read != size)
162  {
163  return ferror(file);
164  }
165 
166  // reset file pointer to correct location for next read or write
167  fseek(file, (long)m_buffers[pHandle][dataId].writeOffset, SEEK_SET);
168 
169  return 0;
170 }
171 
172 
174 {
175  while (m_buffers.size() <= (size_t)pHandle)
176  {
177  size_t i = m_buffers.size();
178  m_buffers.push_back(vector<buffer_file_t>());
179  m_timestamps.push_back(vector<buffer_file_t>());
180 
181  stringstream out;
182  out << i;
183  string num = out.str();
184 
185  for (uint32_t dataIndex = 1; dataIndex < DID_COUNT; dataIndex++)
186  {
187  stringstream out2;
188  out2 << dataIndex;
189  string num2 = out2.str();
190 
191  buffer_file_t file = { 0 };
192 
193  file.path = basePath + "databuffer_" + num + "_" + num2 + string(".buf");
194  file.file = openFile(file.path.c_str(), "wb+");
195  m_buffers[i].push_back(file);
196 
197  file.path = basePath + "timebuffer_" + num + "_" + num2 + string(".buf");
198  file.file = openFile(file.path.c_str(), "wb+");
199  m_timestamps[i].push_back(file);
200  }
201  }
202 }
const char * tempPath()
uint32_t id
Definition: ISComm.h:376
uint32_t size
Definition: ISComm.h:379
int ReadData(int pHandle, uint32_t dataId, vector< uint8_t > &data)
vector< vector< buffer_file_t > > m_timestamps
#define DID_COUNT
Definition: data_sets.h:138
cComDataBuffer g_comDataBuffer
void EnsureBuffers(int pHandle)
static double GetTimestamp(const p_data_hdr_t *hdr, const uint8_t *buf)
#define MAX_ITEM_HISTORY
FILE * openFile(const char *path, const char *mode)
virtual ~cComDataBuffer()
static uint32_t GetSize(uint32_t dataId)
vector< vector< buffer_file_t > > m_buffers
p_data_hdr_t hdr
Definition: ISComm.h:389
USBInterfaceDescriptor data
int PushData(int pHandle, const p_data_t *data)
#define _MKDIR(dir)
Definition: ISConstants.h:213
void DeleteDirectory(const std::string &directory, bool recursive)
uint8_t buf[MAX_DATASET_SIZE]
Definition: ISComm.h:392
uint32_t offset
Definition: ISComm.h:382
#define DID_MAX_COUNT
Definition: data_sets.h:141


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57