HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
saleae_file.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include <fstream>
29 #include <functional>
30 #ifdef STANDALONE_PARSER
31 #include "saleae_directory.h"
32 #else
34 #endif
35 
36 namespace hal
37 {
42  {
43  public:
47  enum ErrorCode { ErrorOpenFile = -4,
51  Ok = 0};
52  };
53 
58  {
59  public:
61  enum StorageFormat { Double = 0,
62  Uint64 = 0x206c6168,
63  Coded = 0x786c6168 };
64  char mIdent[9];
65  int32_t mVersion;
67  int32_t mValue;
68  uint64_t mBeginTime;
69  uint64_t mEndTime;
70  uint64_t mNumTransitions;
71 
72  static const char* sIdent;
73  public:
74  SaleaeHeader();
75 
81  SaleaeStatus::ErrorCode read(std::ifstream& ff);
82 
88  SaleaeStatus::ErrorCode write(std::ofstream& of) const;
89 
92 
95 
97  int32_t value() const { return mValue; }
98 
100  void setValue(int32_t v) { mValue = v; }
101 
103  uint64_t beginTime() const { return mBeginTime; }
104 
106  void setBeginTime(uint64_t t) { mBeginTime = t; }
107 
109  uint64_t endTime() const { return mEndTime; }
110 
112  void setEndTime(uint64_t t) { mEndTime = t; }
113 
115  uint64_t numTransitions() const { return mNumTransitions; }
116 
118  void setNumTransitions(uint64_t n) { mNumTransitions = n; }
119 
122  };
123 
128  {
129  public:
131  SaleaeDataBuffer(uint64_t cnt = 0);
132 
135 
137  uint64_t mCount;
138 
140  uint64_t* mTimeArray;
141 
144 
146  bool isNull() const { return mCount == 0; }
147 
149  void convertCoded();
150 
152  void dump() const;
153  };
154 
159  {
160  public:
162  SaleaeDataTuple(uint64_t t=0, int val=sReadError) : mTime(t), mValue(val) {;}
163 
165  static const int sReadError = -99;
166 
168  uint64_t mTime;
169 
171  int mValue;
172 
174  bool readError() const { return mValue == sReadError; }
175  };
176 
180  class SaleaeInputFile : public std::ifstream
181  {
182  SaleaeHeader mHeader;
183  uint64_t mReadPointer;
184  SaleaeStatus::ErrorCode mStatus;
185 
186  std::function<uint64_t(bool*)> mReader;
187 
188  void seekTransition(uint64_t pos) { seekg(pos*sizeof(uint64_t) + 44); }
189  public:
190  SaleaeInputFile(const std::string& filename);
191 
194 
196  std::string get_last_error() const;
197 
199  SaleaeDataBuffer* get_buffered_data(uint64_t nread);
200 
201 
202  int64_t get_file_position(double t, bool successor=false);
203 
205  void set_file_position(int64_t pos);
206 
208  void skip_transitions(int64_t delta);
209 
211  int get_int_value(double t);
212 
214  const SaleaeHeader* header() const { return &mHeader; }
215  };
216 
217 
221  class SaleaeOutputFile : public std::ofstream
222  {
223  int mIndex;
224  std::string mFilename;
225  SaleaeHeader mHeader;
226  SaleaeStatus::ErrorCode mStatus;
227  bool mFirstValue;
228  int mLastWrittenValue;
229  uint64_t mLastWrittenTime;
230 
231  void convertToCoded();
232  public:
233  SaleaeOutputFile(const std::string& filename, int index_);
235 
237  void writeTimeValue(uint64_t t, int32_t val);
238 
240  void close();
241 
243  int index() const { return mIndex; }
244 
246  void put_data(SaleaeDataBuffer* buf);
247 
250  };
251 }
void convertCoded()
Convert data buffer from "hal " format to "halx".
uint64_t mCount
Number of elements in buffer.
Definition: saleae_file.h:137
SaleaeDataBuffer(uint64_t cnt=0)
Constructor. Generates null instance when no argument is given.
Definition: saleae_file.cpp:19
~SaleaeDataBuffer()
Destructor takes care of disposing allocated memory.
Definition: saleae_file.cpp:29
uint64_t * mTimeArray
Buffer for mCount transition time values.
Definition: saleae_file.h:140
int * mValueArray
Buffer for mCount data values.
Definition: saleae_file.h:143
void dump() const
Dump buffer content to stdout.
bool isNull() const
Returns whether buffer has data. Will be false when e.g. reading behind EOF.
Definition: saleae_file.h:146
int mValue
Data value.
Definition: saleae_file.h:171
static const int sReadError
Fake data value to indicate all kind of errors.
Definition: saleae_file.h:165
SaleaeDataTuple(uint64_t t=0, int val=sReadError)
Constructor. Generates null/read error instance when no arguments are given.
Definition: saleae_file.h:162
uint64_t mTime
Transition time.
Definition: saleae_file.h:168
bool readError() const
Indicates that there was an error and no data was returned.
Definition: saleae_file.h:174
The SaleaeDirectoryFileIndex class represents a single SALEAE data file. The class comprises the inde...
uint64_t mNumTransitions
Definition: saleae_file.h:70
StorageFormat
SALEAE storage format for transition time values.
Definition: saleae_file.h:61
@ Uint64
Double values,.
Definition: saleae_file.h:62
void setEndTime(uint64_t t)
Setter for time of last event.
Definition: saleae_file.h:112
uint64_t endTime() const
Getter for time of last event.
Definition: saleae_file.h:109
uint64_t beginTime() const
Getter for time of first event.
Definition: saleae_file.h:103
StorageFormat mStorageFormat
Definition: saleae_file.h:66
uint64_t numTransitions() const
Getter for number of transitions stored in file. Remember that the total number of events is numTrans...
Definition: saleae_file.h:115
uint64_t mEndTime
Definition: saleae_file.h:69
int32_t value() const
Getter for initial value.
Definition: saleae_file.h:97
StorageFormat storageFormat() const
Getter for storage format, see above.
Definition: saleae_file.h:91
void setNumTransitions(uint64_t n)
Setter for number of transitions stored in file.
Definition: saleae_file.h:118
static const char * sIdent
Definition: saleae_file.h:72
SaleaeStatus::ErrorCode write(std::ofstream &of) const
Definition: saleae_file.cpp:85
SaleaeStatus::ErrorCode read(std::ifstream &ff)
Definition: saleae_file.cpp:42
uint64_t mBeginTime
Definition: saleae_file.h:68
void setBeginTime(uint64_t t)
Setter for time of first event.
Definition: saleae_file.h:106
void setValue(int32_t v)
Setter for initial value.
Definition: saleae_file.h:100
void incrementTransitions()
Increment number of transitions by one.
Definition: saleae_file.h:121
void setStorageFormat(StorageFormat sf)
Setter for storage format, see above.
Definition: saleae_file.h:94
int64_t get_file_position(double t, bool successor=false)
SaleaeDataTuple get_next_value()
Getter for next (time,value) tuple in sequential read.
void skip_transitions(int64_t delta)
Goto position for next read access, skip number of transitions indicated by 'delta',...
SaleaeDataBuffer * get_buffered_data(uint64_t nread)
Returns pointer to data buffer reading from current file position up to nread events....
SaleaeInputFile(const std::string &filename)
Definition: saleae_file.cpp:98
const SaleaeHeader * header() const
Getter for header information.
Definition: saleae_file.h:214
int get_int_value(double t)
Get waveform value for time t.
std::string get_last_error() const
Get verbose error message based on internal status, empty string if no error.
void set_file_position(int64_t pos)
Goto position for next read access. Pos=0 is start value from header.
void writeTimeValue(uint64_t t, int32_t val)
Write single data tuple to disk.
void close()
Update header on disk and close file.
SaleaeOutputFile(const std::string &filename, int index_)
SaleaeDirectoryFileIndex fileIndex() const
File index summary for saleae directory.
int index() const
Getter for data file index (XXX in digital_XXX.bin)
Definition: saleae_file.h:243
void put_data(SaleaeDataBuffer *buf)
Write buffered data.
Definition: defines.h:45