-
Notifications
You must be signed in to change notification settings - Fork 17
/
MSPBSL_Factory.cpp
270 lines (239 loc) · 9.15 KB
/
MSPBSL_Factory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* MSPBSL_Factory
*
* A factory class designed to create BSL Connections based on standard parameters
*
* Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "MSPBSL_Factory.h"
// Physical Connections Handlers
#include "MSPBSL_PhysicalInterfaceSerialUART.h"
#include "MSPBSL_PhysicalInterfaceUSB.h"
// Connections
#include "MSPBSL_Connection1xx_2xx_4xx.h"
#include "MSPBSL_ConnectionFRAMFamily.h"
#include "MSPBSL_Connection5438Family.h"
#include "MSPBSL_Connection5xxUSB.h"
#include "MSPBSL_Connection5xxUART.h"
#include "MSPBSL_Connection5xx.h"
#include "MSPBSL_Connection1xx_2xx_4xx.h"
#include "MSPBSL_Connection_v1_10.h"
#include "MSPBSL_Connection_v1_3x.h"
#include "MSPBSL_Connection_v1_4x.h"
#include "MSPBSL_Connection_v1_6x.h"
#include "MSPBSL_Connection_v2_xx.h"
#include "MSPBSL_Connection_v2_1x.h"
// Packet Handlers
#include "MSPBSL_PacketHandler5xxUART.h"
#include "MSPBSL_PacketHandler5xxUSB.h"
#include "MSPBSL_PacketHandler1xx_2xx_4xxUART.h"
//Official List of acceptable inputs:
//
//5XX_UART
//
//PARITY:
// EVEN
// ODD
// NONE
//
//BUG:
// SHORT_PASSWORD
#define UART_5XX_STRING "UART_5XX"
#define UART_FRAM_STRING "UART_FRAM"
#define USB_5XX_STRING "USB_5XX"
#define UART_1XX_2XX_4XX_STRING "UART_1XX2XX4XX"
#define UART_110_STRING "1.10_UART"
#define UART_130_STRING "1.30_UART"
#define UART_140_STRING "1.40_UART"
#define UART_160_STRING "1.60_UART"
#define UART_161_STRING "1.61_UART"
#define UART_202_STRING "2.02_UART"
#define UART_21X_STRING "2.1X_UART"
#define BUG_SHORT_PASSWORD "SHORT_PASSWORD"
/**************************************************************************//**
* Factory Class Constructor.
*
* Creates a BSL Factory which can supply multiple connections.
*
* \return a MSPBSL_Factory class
******************************************************************************/
MSPBSL_Factory::MSPBSL_Factory(void)
{
}
/**************************************************************************//**
* Factory Class Destructor.
*
* Class Destructor
*
******************************************************************************/
MSPBSL_Factory::~MSPBSL_Factory(void)
{
}
/**************************************************************************//**
* BSL Connection creator
*
* Creates a BSL connection based on the desired parameters in the init string.
*
* \param init a string containing configuration parameters
*
* \return a MSPBSL_Connection class
******************************************************************************/
MSPBSL_Connection* MSPBSL_Factory::getMSPBSL_Connection(string initString)
{
initString = MSPBSL_Factory::expandInitString( initString );
MSPBSL_Connection* theBSLConnection;
//
if ( (initString.find( UART_5XX_STRING ) !=string::npos) || (initString.find( UART_FRAM_STRING ) !=string::npos)) // if it's a 5xx UART....
{
if(initString.find( BUG_SHORT_PASSWORD ) !=string::npos ) // short password means 5438
{
theBSLConnection = new MSPBSL_Connection5438Family( initString );
}
else if(initString.find( UART_FRAM_STRING ) !=string::npos ) // FRAM Device
{
theBSLConnection = new MSPBSL_ConnectionFRAMFamily( initString );
}
else // otherwise, any other 5xx UART device
{
theBSLConnection = new MSPBSL_Connection5xx( initString );
}
MSPBSL_PhysicalInterfaceSerialUART* s = new MSPBSL_PhysicalInterfaceSerialUART( initString ); // Parity handled in object;
MSPBSL_PacketHandler5xxUART* p = new MSPBSL_PacketHandler5xxUART( initString );
p->setPhysicalInterface( s );
theBSLConnection->setPacketHandler(p);
} // all 5XX UART BSLs handled
else if (initString.find( USB_5XX_STRING ) !=string::npos) // if it's a 5xx USB....
{
theBSLConnection = new MSPBSL_Connection5xx( initString );
MSPBSL_PhysicalInterfaceUSB* s = new MSPBSL_PhysicalInterfaceUSB( initString ); // Parity handled in object;
MSPBSL_PacketHandler5xxUSB* p = new MSPBSL_PacketHandler5xxUSB( initString );
p->setPhysicalInterface( s );
theBSLConnection->setPacketHandler(p);
} // all 5XX USB BSLs handled
else if ( initString.find( UART_1XX_2XX_4XX_STRING ) != string::npos) //if it's a 1xx/2xx/4xx UART device
{
if(initString.find( UART_110_STRING ) !=string::npos )
{
theBSLConnection = new MSPBSL_Connection_v1_10( initString );
}
else if(initString.find( UART_130_STRING ) !=string::npos )
{
theBSLConnection = new MSPBSL_Connection_v1_3x( initString );
}
else if(initString.find( UART_140_STRING ) !=string::npos )
{
theBSLConnection = new MSPBSL_Connection_v1_4x( initString );
}
else if( (initString.find( UART_160_STRING ) != string::npos) || (initString.find( UART_161_STRING ) != string::npos) )
{
theBSLConnection = new MSPBSL_Connection_v1_6x( initString );
}
else if(initString.find( UART_202_STRING ) != string::npos )
{
theBSLConnection = new MSPBSL_Connection_v2_xx( initString );
}
else if(initString.find( UART_21X_STRING ) != string::npos )
{
theBSLConnection = new MSPBSL_Connection_v2_1x( initString );
}
else
{
theBSLConnection = new MSPBSL_Connection1xx_2xx_4xx( initString );
}
//theBSLConnection = dynamic_cast<MSPBSL_Connection*>(theBSLConnection);
MSPBSL_PhysicalInterfaceSerialUART* s = new MSPBSL_PhysicalInterfaceSerialUART( initString ); // Parity handled in object;
MSPBSL_PacketHandler1xx_2xx_4xxUART* p = new MSPBSL_PacketHandler1xx_2xx_4xxUART( initString );
p->setPhysicalInterface( s );
theBSLConnection->setPacketHandler(p);
} // all 1xx/2xx/4xx UART BSLs handled
else
{
return NULL; //no init String was found in the DeviceList.txt
}
return theBSLConnection;
}
/**************************************************************************//**
* Open the device list file and expand init string.
*
* Mostly used internally by the factory. This takes a initialization string
* and returns the expanded value which is used to initialize the sub-layers
*
* \param init a string containing configuration parameters
*
*
* \return an expanded parameter string
******************************************************************************/
string MSPBSL_Factory::expandInitString( string init )
{
uint32_t i,j,k;
string ignore = "\b\t\n\r\f\v "; //ignore those characters if they are between the strings.
ifstream t("MSPBSL_Device_List.txt", ifstream::out); // for testing purposes with VisualStudio, this file should be in the same
// folder as the *.vcxproj files. e.g. in $ProjectDir\BSL_DLL
// Absolute data paths work as well.
stringstream s;
s << t.rdbuf();
string replaceList = s.str();
t.close();
init += ","; // add string at back since we search for strings with comma following (to distinguish between A/non-A versions)
k=0;
if(replaceList.size() == 0)
{
return init;
}
while( (replaceList.find(init) != string::npos) && (k < 100) )
{
i = replaceList.find( init ) + init.length();
i = replaceList.find_first_not_of(ignore , i);
j = replaceList.find(";",i);
init = replaceList.substr(i, (j-i));
init += ",";
k++; //avoid endless loops
}
init.resize(init.size()-1); //erase the "," at the end of the string
init += " "; //space at the end is necesary for correct parsing in the PhysicalInterface.
return init;
}
/**************************************************************************//**
* errorCode.
*
* Used to transform an error code into a human readable form
*
* \param error a 16 bit error code
*
*
* \return a string explanation of that error code
******************************************************************************/
string MSPBSL_Factory::errorCode (uint16_t error )
{
return "to do";
}