Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/StSRecordFile.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_StSRecordFile_h_)
31 #define _StSRecordFile_h_
32 
33 //#include <stdint.h>
34 #include "stdafx.h"
35 #include <istream>
36 #include <string>
37 #include <vector>
38 #include <stdexcept>
39 
40 enum
41 {
43  SRECORD_START_CHAR = 'S',
44 
46  SRECORD_MIN_LENGTH = 10,
47 
49  SRECORD_ADDRESS_START_CHAR_INDEX = 4
50 };
51 
61 class StSRecordFile
62 {
63 public:
67  struct SRecord
68  {
69  unsigned m_type;
70  unsigned m_count;
71  uint32_t m_address;
72  unsigned m_dataCount;
73  uint8_t *m_data;
74  uint8_t m_checksum;
75  };
76 
78  typedef std::vector<SRecord>::const_iterator const_iterator;
79 
80 public:
82  StSRecordFile(std::istream &inStream);
83 
85  virtual ~StSRecordFile();
86 
88 
89  virtual void setName(const std::string &inName) { m_name = inName; }
90  virtual std::string getName() const { return m_name; }
92 
94 
95  virtual bool isSRecordFile();
97 
99  virtual void parse();
101 
103 
104  inline unsigned getRecordCount() const { return static_cast<unsigned>(m_records.size()); }
107  inline const_iterator getBegin() const { return m_records.begin(); }
108  inline const_iterator getEnd() const { return m_records.end(); }
110 
112 
113  inline const SRecord &operator[](unsigned inIndex) { return m_records[inIndex]; }
115 
116 protected:
117  std::istream &m_stream;
118  std::vector<SRecord> m_records;
119 
120  std::string m_name;
121 
123 
124  virtual void parseLine(std::string &inLine);
125 
126  bool isHexDigit(char c);
127  int hexDigitToInt(char digit);
128  int readHexByte(std::string &inString, int inIndex);
130 };
131 
135 class StSRecordParseException : public std::runtime_error
136 {
137 public:
139  StSRecordParseException(const std::string &inMessage)
140  : std::runtime_error(inMessage)
141  {
142  }
143 };
144 
145 #endif // _StSRecordFile_h_
uint8_t m_checksum
The checksum byte present in the S-record.
Definition: apps/elftosb/common/StSRecordFile.h:51
Definition: apps/elftosb/common/StSRecordFile.h:44
virtual void parse()
Parses the entire S-record input stream.
Definition: apps/elftosb/common/StSRecordFile.cpp:54
uint32_t m_address
The address specified as part of the S-record.
Definition: apps/elftosb/common/StSRecordFile.h:48
std::istream & m_stream
The input stream for the S-record data.
Definition: apps/elftosb/common/StSRecordFile.h:94
STL namespace.
unsigned m_dataCount
Number of bytes of data.
Definition: apps/elftosb/common/StSRecordFile.h:49
S-record parser.
Definition: apps/elftosb/common/StSRecordFile.h:38
int readHexByte(std::string &inString, int inIndex)
Definition: apps/elftosb/common/StSRecordFile.cpp:108
std::string m_name
File name. (optional)
Definition: apps/elftosb/common/StSRecordFile.h:97
Simple exception thrown to indicate an error in the input SRecord data format.
Definition: apps/elftosb/common/StSRecordFile.h:112
std::vector< SRecord >::const_iterator const_iterator
Iterator type.
Definition: src/blfwk/StSRecordFile.h:78
std::vector< SRecord > m_records
Vector of S-records in the input data.
Definition: apps/elftosb/common/StSRecordFile.h:95
const_iterator getBegin() const
Definition: src/blfwk/StSRecordFile.h:107
unsigned getRecordCount() const
Definition: apps/elftosb/common/StSRecordFile.h:82
unsigned m_count
Number of character pairs (bytes) from address through checksum.
Definition: apps/elftosb/common/StSRecordFile.h:47
virtual ~StSRecordFile()
Destructor.
Definition: apps/elftosb/common/StSRecordFile.cpp:18
StSRecordFile(std::istream &inStream)
Constructor.
Definition: apps/elftosb/common/StSRecordFile.cpp:12
StSRecordParseException(const std::string &inMessage)
Default constructor.
Definition: src/blfwk/StSRecordFile.h:139
unsigned m_type
Record number type, such as 9 for "S9", 3 for "S3" and so on.
Definition: apps/elftosb/common/StSRecordFile.h:46
virtual void parseLine(std::string &inLine)
Parses individual S-records.
Definition: apps/elftosb/common/StSRecordFile.cpp:128
virtual bool isSRecordFile()
Determine if the file is an S-record file.
Definition: apps/elftosb/common/StSRecordFile.cpp:33
uint8_t * m_data
Pointer to data, or NULL if no data for this record type.
Definition: apps/elftosb/common/StSRecordFile.h:50