DeviceLogJSON.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 <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  char c;
58  int pc = 0;
59  while ((c = (char)m_pFile->getch()) >= 0)
60  {
61  if (c == '{' && pc != '\\')
62  {
63  stack++;
64  }
65  else if (c == '}' && pc != '\\')
66  {
67  stack--;
68  if (stack == 0)
69  {
70  m_jsonString.append(1, c);
71  break;
72  }
73  }
74 
75  if (stack != 0)
76  {
77  m_jsonString.append(1, c);
78  }
79 
80  pc = c;
81  }
82  return (m_jsonString.size() != 0);
83 }
84 
85 
86 bool cDeviceLogJSON::SaveData(p_data_hdr_t* dataHdr, const uint8_t* dataBuf)
87 {
88  cDeviceLog::SaveData(dataHdr, dataBuf);
89 
90  bool needsComma = true;
91 
92  // Create first file it it doesn't exist, return out if failure
93  if (m_pFile == NULLPTR)
94  {
95  needsComma = false;
96  if (!OpenNewSaveFile())
97  {
98  return false;
99  }
100  m_pFile->putch('[');
101  }
102  else if (dataHdr->id == DID_DEV_INFO)
103  {
104  memcpy(&m_devInfo, dataBuf, sizeof(dev_info_t));
105  }
106 
107  // Write date to file
108  int nBytes = m_json.WriteDataToFile(m_pFile, *dataHdr, dataBuf, (needsComma ? ",\n" : NULLPTR));
109  if (!m_pFile->good())
110  {
111  return false;
112  }
113 
114  // File byte size
115  m_logSize += nBytes;
116 
117  if (m_logSize >= m_maxFileSize)
118  {
119  // Close existing file
120  m_pFile->putch(']');
121  CloseISLogFile(m_pFile);
122  m_logSize = 0;
123  }
124 
125  return true;
126 }
127 
128 
130 {
131  if (m_pFile == NULLPTR)
132  {
133  if (!OpenNextReadFile())
134  {
135  return NULLPTR;
136  }
137  }
138  p_data_t* data = ReadDataFromFile();
140  return data;
141 }
142 
143 
145 {
146  if (m_pFile == NULLPTR)
147  {
148  assert(false);
149  return NULLPTR;
150  }
151  while (!GetNextItemForFile() && OpenNextReadFile()) {}
152  if (m_json.StringJSONToData(m_jsonString, m_dataBuffer.hdr, m_dataBuffer.buf, _ARRAY_BYTE_COUNT(m_dataBuffer.buf)))
153  {
154  if (m_dataBuffer.hdr.id == DID_DEV_INFO)
155  {
156  memcpy(&m_devInfo, m_dataBuffer.buf, sizeof(dev_info_t));
157  }
158  return &m_dataBuffer;
159  }
160  return NULLPTR;
161 }
162 
163 
164 void cDeviceLogJSON::SetSerialNumber(uint32_t serialNumber)
165 {
166  m_devInfo.serialNumber = serialNumber;
167 }
168 
169 
170 
void OnReadData(p_data_t *data)
Definition: DeviceLog.cpp:267
virtual bool CloseAllFiles()
Definition: DeviceLog.cpp:77
uint32_t id
Definition: ISComm.h:375
#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
p_data_t * ReadDataFromFile()
void SetSerialNumber(uint32_t serialNumber) OVERRIDE


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