DeviceLogSerial.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2020 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <ctime>
14 #include <string>
15 #include <sstream>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <iomanip>
19 #include <iostream>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 
24 #include "DeviceLogSerial.h"
25 #include "ISLogger.h"
26 #include "ISLogFileFactory.h"
27 
28 using namespace std;
29 
30 void cDeviceLogSerial::InitDeviceForWriting(int pHandle, std::string timestamp, std::string directory, uint64_t maxDiskSpace, uint32_t maxFileSize)
31 {
32 // m_chunk.Init(chunkSize);
33  m_chunk.m_hdr.pHandle = pHandle;
34 
35  cDeviceLog::InitDeviceForWriting(pHandle, timestamp, directory, maxDiskSpace, maxFileSize);
36 }
37 
38 
40 {
42 
43  // Write any remaining chunk data to file
44  WriteChunkToFile();
45 
46  // Close file
47  CloseISLogFile(m_pFile);
48 
49  return true;
50 }
51 
52 
53 bool cDeviceLogSerial::SaveData(p_data_hdr_t* dataHdr, const uint8_t* dataBuf)
54 {
55  cDeviceLog::SaveData(dataHdr, dataBuf);
56 
57  // Add serial number if available
58  if (dataHdr->id == DID_DEV_INFO && !copyDataPToStructP2(&m_devInfo, dataHdr, dataBuf, sizeof(dev_info_t)))
59  {
60  int start = dataHdr->offset;
61  int end = dataHdr->offset + dataHdr->size;
62  int snOffset = offsetof(dev_info_t, serialNumber);
63 
64  // Did we really get the serial number?
65  if (start <= snOffset && (int)(snOffset + sizeof(uint32_t)) <= end)
66  {
67  m_chunk.m_hdr.devSerialNum = m_devInfo.serialNumber;
68  }
69  }
70 
71  // Ensure data will fit in chunk. If not, create new chunk
72  uint32_t dataBytes = sizeof(p_data_hdr_t) + dataHdr->size;
73  if (dataBytes > m_chunk.GetBuffFree())
74  {
75  // Save chunk to file and clear
76  if (!WriteChunkToFile())
77  {
78  return false;
79  }
80  else if (m_fileSize >= m_maxFileSize)
81  {
82  // Close existing file
83  CloseAllFiles();
84  }
85  }
86  // Add data header and data buffer to chunk
87  if (!m_chunk.PushBack((unsigned char*)dataHdr, sizeof(p_data_hdr_t), (unsigned char*)dataBuf, dataHdr->size))
88  {
89  return false;
90  }
91 
92  return true;
93 }
94 
95 
97 {
98  // Make sure we have data to write
99  if (m_chunk.GetDataSize() == 0)
100  {
101  return false;
102  }
103 
104  // Create first file if it doesn't exist
105  if (m_pFile == NULLPTR)
106  {
107  OpenNewSaveFile();
108  }
109 
110  // Validate file pointer
111  if (m_pFile == NULLPTR)
112  {
113  return false;
114  }
115 
116  // Write chunk to file
117  int fileBytes = m_chunk.WriteToFile(m_pFile, 0);
118  if (!m_pFile->good())
119  {
120  return false;
121  }
122 
123  // File byte size
124  m_fileSize += fileBytes;
125  m_logSize += fileBytes;
126 
127  return true;
128 }
129 
130 
132 {
133  p_data_t* data = NULL;
134 
135  // Read data from chunk
136  while (!(data = ReadDataFromChunk()))
137  {
138  // Read next chunk from file
139  if (!ReadChunkFromFile())
140  {
141  return NULL;
142  }
143  }
144 
145  // Read is good
147  return data;
148 }
149 
150 
152 {
153  // Ensure chunk has data
154  if (m_chunk.GetDataSize() <= 0)
155  {
156  return NULL;
157  }
158 
159  p_data_t* data = (p_data_t*)m_chunk.GetDataPtr();
160  int size = data->hdr.size + sizeof(p_data_hdr_t);
161  if (m_chunk.PopFront(size))
162  {
163  return data;
164  }
165  else
166  {
167  return NULL;
168  }
169 }
170 
171 
173 {
174  // Read next chunk from file
175  while (m_chunk.ReadFromFile(m_pFile) < 0)
176  {
177  if (!OpenNextReadFile())
178  {
179  // No more data or error opening next file
180  return false;
181  }
182  }
183  return true;
184 }
185 
186 
187 void cDeviceLogSerial::SetSerialNumber(uint32_t serialNumber)
188 {
189  m_devInfo.serialNumber = serialNumber;
190  m_chunk.m_hdr.devSerialNum = serialNumber;
191 }
192 
193 
195 {
196  if (WriteChunkToFile())
197  {
198  m_pFile->flush();
199  }
200 }
201 
202 
p_data_t * ReadDataFromChunk()
ROSCPP_DECL void start()
void OnReadData(p_data_t *data)
Definition: DeviceLog.cpp:267
virtual bool CloseAllFiles()
Definition: DeviceLog.cpp:77
void InitDeviceForWriting(int pHandle, std::string timestamp, std::string directory, uint64_t maxDiskSpace, uint32_t maxFilesize) OVERRIDE
uint32_t id
Definition: ISComm.h:375
uint32_t size
Definition: ISComm.h:378
not_this_one end(...)
if(udd_ctrl_interrupt())
Definition: usbhs_device.c:688
#define DID_DEV_INFO
Definition: data_sets.h:35
#define NULL
Definition: nm_bsp.h:52
virtual bool SaveData(p_data_hdr_t *dataHdr, const uint8_t *dataBuf)
Definition: DeviceLog.cpp:106
void CloseISLogFile(cISLogFileBase *&logFile)
#define NULLPTR
Definition: ISConstants.h:426
bool CloseAllFiles() OVERRIDE
void Flush() OVERRIDE
void SetSerialNumber(uint32_t serialNumber) OVERRIDE
virtual void InitDeviceForWriting(int pHandle, string timestamp, string directory, uint64_t maxDiskSpace, uint32_t maxFileSize)
Definition: DeviceLog.cpp:57
p_data_t * ReadData() OVERRIDE
p_data_hdr_t hdr
Definition: ISComm.h:388
USBInterfaceDescriptor data
uint32_t offset
Definition: ISComm.h:381
char copyDataPToStructP2(void *sptr, const p_data_hdr_t *dataHdr, const uint8_t *dataBuf, const unsigned int maxsize)
Definition: ISComm.c:989
bool SaveData(p_data_hdr_t *dataHdr, const uint8_t *dataBuf) OVERRIDE


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04