DataJSON.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 <time.h>
15 #include <string>
16 #include <sstream>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <iomanip>
20 #include <iostream>
21 #include <fstream>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <inttypes.h>
26 #include <math.h>
27 #include "DataJSON.h"
28 #include "ISLogger.h"
29 #include "data_sets.h"
30 #include "ISDataMappings.h"
31 #include "ISUtilities.h"
32 #include "ISConstants.h"
33 
34 #ifdef USE_IS_INTERNAL
35 # include "../../libs/IS_internal.h"
36 #endif
37 
38 int cDataJSON::WriteDataToFile(cISLogFileBase* pFile, const p_data_hdr_t& dataHdr, const uint8_t* dataBuf, const char* prefix)
39 {
40  // Verify file pointer
41  if (pFile == NULLPTR || cISDataMappings::GetSize(dataHdr.id) == 0)
42  {
43  return 0;
44  }
45 
46  string s;
47  if (!DataToStringJSON(dataHdr, dataBuf, s))
48  {
49  return 0;
50  }
51  if (prefix != NULLPTR)
52  {
53  pFile->puts(prefix);
54  }
55  pFile->puts(s.c_str());
56  return (int)s.length();
57 }
58 
59 bool cDataJSON::StringJSONToData(string& s, p_data_hdr_t& hdr, uint8_t* buf, uint32_t bufSize)
60 {
61  (void)s;
62  (void)buf;
63  (void)bufSize;
64 
65  size_t pos = s.find("\"id\":");
66  if (pos == string::npos)
67  {
68  return false;
69  }
70  uint32_t id = strtoul(s.c_str() + pos + 5, NULLPTR, 10);
71  hdr.id = id;
72  const map_name_to_info_t* offsetMap = cISDataMappings::GetMapInfo(hdr.id);
73  if (offsetMap == NULLPTR)
74  {
75  return false;
76  }
77  hdr.size = cISDataMappings::GetSize(hdr.id);
78  char c;
79  char pc = 0;
80  map_name_to_info_t::const_iterator offset;
81  string fieldName;
82  size_t fieldStart = 0;
83  bool inName = true;
84  bool inQuote = false;
85  for (size_t i = 0; i < s.size(); i++)
86  {
87  c = s[i];
88  if (c == '"' && pc != '\\')
89  {
90  if ((inQuote = !inQuote))
91  {
92  fieldStart = i + 1;
93  pc = c;
94  continue;
95  }
96  }
97 
98  if (inName)
99  {
100  if (c == ':')
101  {
102  fieldStart = i + 1;
103  inName = inQuote = false;
104  pc = c;
105  continue;
106  }
107  else if (inQuote)
108  {
109  fieldName.append(1, c);
110  }
111  }
112  else if ((c == '}' || c == '"' || (!inQuote && c == ',')) && pc != '\\')
113  {
114  if (fieldStart != 0)
115  {
116  offset = offsetMap->find(fieldName);
117  string json = s.substr(fieldStart, i - fieldStart);
118  if (offset != offsetMap->end() && !cISDataMappings::StringToData(json.c_str(), (int)json.size(), &hdr, buf, offset->second, 10, true))
119  {
120  return false;
121  }
122  }
123  fieldName.clear();
124  fieldStart = 0;
125  inName = true;
126  }
127  pc = c;
128  }
129 
130  return true;
131 }
132 
133 
134 bool cDataJSON::DataToStringJSON(const p_data_hdr_t& hdr, const uint8_t* buf, string& json)
135 {
136  json.clear();
137  const map_name_to_info_t* offsetMap = cISDataMappings::GetMapInfo(hdr.id);
138  if (offsetMap == NULLPTR)
139  {
140  return false;
141  }
143  const uint8_t* bufPtr = buf;
144  uint8_t tmpBuffer[MAX_DATASET_SIZE];
145  uint32_t size = cISDataMappings::GetSize(hdr.id);
146  if (size > hdr.size)
147  {
148  // copy into temp buffer, zeroing out bytes that are not part of this packet
149  memset(tmpBuffer, 0, hdr.offset);
150  memcpy(tmpBuffer + hdr.offset, buf, hdr.size);
151  uint32_t dataEnd = hdr.offset + hdr.size;
152  memset(tmpBuffer + dataEnd, 0, size - dataEnd);
153  bufPtr = tmpBuffer;
154  }
155 
156  // copy header as we are now storing an entire struct, even if we got a partial hdr and buf
157  p_data_hdr_t hdrCopy = hdr;
158  hdrCopy.offset = 0;
159  hdrCopy.size = size;
160 
161  SNPRINTF(tmp, sizeof(tmp), "{\"id\":%d", (int)hdr.id);
162  json.append(tmp);
163  for (map_name_to_info_t::const_iterator offset = offsetMap->begin(); offset != offsetMap->end(); offset++)
164  {
165  cISDataMappings::DataToString(offset->second, &hdrCopy, bufPtr, tmp, true);
166  json.append(1, ',');
167  json.append(1, '"');
168  json.append(offset->second.name);
169  json.append(1, '"');
170  json.append(1, ':');
171  json.append(tmp);
172  }
173  json.append(1, '}');
174  return true;
175 }
static const map_name_to_info_t * GetMapInfo(uint32_t dataId)
uint32_t id
Definition: ISComm.h:375
uint32_t size
Definition: ISComm.h:378
XmlRpcServer s
#define NULLPTR
Definition: ISConstants.h:426
static bool StringToData(const char *stringBuffer, int stringLength, const p_data_hdr_t *hdr, uint8_t *dataBuffer, const data_info_t &info, int radix=10, bool json=false)
bool DataToStringJSON(const p_data_hdr_t &hdr, const uint8_t *buf, string &json)
Definition: DataJSON.cpp:134
virtual int puts(const char *str)=0
static uint32_t GetSize(uint32_t dataId)
int WriteDataToFile(cISLogFileBase *pFile, const p_data_hdr_t &dataHdr, const uint8_t *dataBuf, const char *prefix)
Definition: DataJSON.cpp:38
#define IS_DATA_MAPPING_MAX_STRING_LENGTH
Definition: DataCSV.h:24
#define SNPRINTF
Definition: ISConstants.h:146
static bool DataToString(const data_info_t &info, const p_data_hdr_t *hdr, const uint8_t *dataBuffer, data_mapping_string_t stringBuffer, bool json=false)
map< string, data_info_t, sCaseInsensitiveCompare > map_name_to_info_t
uint32_t offset
Definition: ISComm.h:381
#define MAX_DATASET_SIZE
Definition: ISComm.h:91
bool StringJSONToData(string &s, p_data_hdr_t &hdr, uint8_t *buf, uint32_t bufSize)
Definition: DataJSON.cpp:59


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