DeviceLogJSON.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 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 <fstream>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <limits.h>
25 
26 #include "ISPose.h"
27 #include "DeviceLogJSON.h"
28 #include "ISLogger.h"
29 #include "ISDataMappings.h"
30 #include "ISLogFileFactory.h"
31 
32 #if PLATFORM_IS_EVB_2
33 #include "ff.h"
34 #endif
35 
36 using namespace std;
37 
39 {
41  if (m_pFile != NULLPTR)
42  {
43  m_pFile->putch(']');
44  CloseISLogFile(m_pFile);
45  }
46  return true;
47 }
48 
50 {
51  m_jsonString.clear();
52  if (m_pFile == NULLPTR)
53  {
54  return false;
55  }
56  int stack = 0;
57  int pc = 0;
58  int i;
59  while ((i = m_pFile->getch()) != EOF)
60  {
61  char c = (char)i;
62  if (c == '{' && pc != '\\')
63  {
64  stack++;
65  }
66  else if (c == '}' && pc != '\\')
67  {
68  stack--;
69  if (stack == 0)
70  {
71  m_jsonString.append(1, c);
72  break;
73  }
74  }
75 
76  if (stack != 0)
77  {
78  m_jsonString.append(1, c);
79  }
80 
81  pc = c;
82  }
83  return (m_jsonString.size() != 0);
84 }
85 
86 
87 bool cDeviceLogJSON::SaveData(p_data_hdr_t* dataHdr, const uint8_t* dataBuf)
88 {
89  cDeviceLog::SaveData(dataHdr, dataBuf);
90 
91  bool needsComma = true;
92 
93  // Create first file it it doesn't exist, return out if failure
94  if (m_pFile == NULLPTR)
95  {
96  needsComma = false;
97  if (!OpenNewSaveFile())
98  {
99  return false;
100  }
101  m_pFile->putch('[');
102  }
103  else if (dataHdr->id == DID_DEV_INFO)
104  {
105  memcpy(&m_devInfo, dataBuf, sizeof(dev_info_t));
106  }
107 
108  // Write date to file
109  int nBytes = m_json.WriteDataToFile(m_pFile, *dataHdr, dataBuf, (needsComma ? ",\n" : NULLPTR));
110  if (!m_pFile->good())
111  {
112  return false;
113  }
114 
115  // File byte size
116  m_logSize += nBytes;
117 
118  if (m_logSize >= m_maxFileSize)
119  {
120  // Close existing file
121  m_pFile->putch(']');
122  CloseISLogFile(m_pFile);
123  m_logSize = 0;
124  }
125 
126  return true;
127 }
128 
129 
131 {
132  if (m_pFile == NULLPTR)
133  {
134  if (!OpenNextReadFile())
135  {
136  return NULLPTR;
137  }
138  }
139  p_data_t* data = ReadDataFromFile();
141  return data;
142 }
143 
144 
146 {
147  if (m_pFile == NULLPTR)
148  {
149  assert(false);
150  return NULLPTR;
151  }
152  while (!GetNextItemForFile() && OpenNextReadFile()) {}
153  if (m_json.StringJSONToData(m_jsonString, m_dataBuffer.hdr, m_dataBuffer.buf, _ARRAY_BYTE_COUNT(m_dataBuffer.buf)))
154  {
155  if (m_dataBuffer.hdr.id == DID_DEV_INFO)
156  {
157  memcpy(&m_devInfo, m_dataBuffer.buf, sizeof(dev_info_t));
158  }
159  return &m_dataBuffer;
160  }
161  return NULLPTR;
162 }
163 
164 
165 void cDeviceLogJSON::SetSerialNumber(uint32_t serialNumber)
166 {
167  m_devInfo.serialNumber = serialNumber;
168 }
169 
170 
171 
void OnReadData(p_data_t *data)
Definition: DeviceLog.cpp:267
virtual bool CloseAllFiles()
Definition: DeviceLog.cpp:77
uint32_t id
Definition: ISComm.h:376
#define DID_DEV_INFO
Definition: data_sets.h:35
bool CloseAllFiles() OVERRIDE
virtual bool SaveData(p_data_hdr_t *dataHdr, const uint8_t *dataBuf)
Definition: DeviceLog.cpp:106
void CloseISLogFile(cISLogFileBase *&logFile)
bool SaveData(p_data_hdr_t *dataHdr, const uint8_t *dataBuf) OVERRIDE
#define NULLPTR
Definition: ISConstants.h:426
p_data_t * ReadData() OVERRIDE
bool GetNextItemForFile()
#define _ARRAY_BYTE_COUNT(a)
Definition: ISConstants.h:322
USBInterfaceDescriptor data
#define EOF
Definition: ff.h:241
p_data_t * ReadDataFromFile()
void SetSerialNumber(uint32_t serialNumber) OVERRIDE


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