-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathUsbCdcIoChannel.cpp
More file actions
567 lines (463 loc) · 13.7 KB
/
Copy pathUsbCdcIoChannel.cpp
File metadata and controls
567 lines (463 loc) · 13.7 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* UsbCdcIoChannel.cpp
*
* IOChannel via CDC (VCOM) over USB communication.
*
* Copyright (C) 2007 - 2011 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.
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS //disabling warnings to use secure c-function versions (e.g. strcpy_s) as this is not compatible with none MS development
#endif
#endif
#include "UsbCdcIoChannel.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include "logging/Logging.h"
#include <boost/thread/thread.hpp>
#if defined(_WIN32) || defined(_WIN64)
extern "C" {
#include <setupapi.h>
#include <dbt.h>
}
static const std::string portPrefix("\\\\.\\");
#else
#include <unistd.h>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
static const std::string portPrefix("");
#endif
using namespace TI::DLL430;
using namespace std;
using namespace boost::asio;
#define XOFF 0x13
#define XON 0x11
#define XMASK 0x10
#define XMASK_OFF 0x03
#define XMASK_ON 0x01
#define XMASK_MASK 0x00
#ifndef NDEBUG
#define DB_PRINT
#endif
UsbCdcIoChannel::UsbCdcIoChannel(std::string name, std::string id)
: inputReportSize(255)
, actSize(0)
, expSize(0)
, name(name)
, isXoffFlowOn(true)
, status(freeForUse)
, ioService(0)
, port(0)
, comState(ComStateRcv)
{
serial = retrieveSerialFromId(id);
retrieveStatus();
}
UsbCdcIoChannel::~UsbCdcIoChannel ()
{
this->cleanup();
}
void UsbCdcIoChannel::enumeratePorts (PortMap & portList, bool open)
{
#if defined(_WIN32) || defined(_WIN64)
typedef boost::array<BYTE, maxBufferLength> RegistryProperty;
const std::string tiVendorId("VID_2047");
const std::string cdcClassDev("PID_0010"); ///< product id 0010 means CDC
const std::string cdcId(UsbDevClass() + '\\' + tiVendorId + '&' + cdcClassDev);
char deviceId[maxBufferLength];
HDEVINFO hDevInfo = ::SetupDiGetClassDevs(NULL, UsbDevClass().c_str(), 0, DIGCF_PRESENT | DIGCF_ALLCLASSES );
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (int i = 0; ::SetupDiEnumDeviceInfo(hDevInfo,i,&devInfoData); ++i )
{
deviceId[0]=0;
BOOL result = ::SetupDiGetDeviceInstanceId(hDevInfo,&devInfoData,deviceId,maxBufferLength,NULL);
//not TI and/or not CDC
if( !result || 0 != cdcId.compare( std::string( deviceId).substr( 0, cdcId.size() ) ) )
{
continue;
}
else
{
DWORD propertyType = 0;
RegistryProperty property;
::SetupDiGetDeviceRegistryProperty(
hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME,
&propertyType, &property[0], maxBufferLength, NULL
);
std::stringstream sstr;
for(size_t i = 0; i < property.size(); ++i)
{
sstr << property[i];
if(0 == property[i])
{
break;
}
}
const size_t idBegin = sstr.str().find_last_of('(') + 1;
const size_t idEnd = sstr.str().find_last_of(')');
assert(idEnd > idBegin);
const string name = sstr.str().substr(idBegin, idEnd - idBegin);
UsbCdcIoChannel * cdcElem = new UsbCdcIoChannel(name,deviceId);
portList.insert(std::make_pair(name,cdcElem));
}
}
::SetupDiDestroyDeviceInfoList(hDevInfo);//free resources
#else
// Implementation based on Debian
path p(string("/dev/serial/by-id/"));
if ( exists(p) && is_directory(p) )
{
const directory_iterator end;
for ( directory_iterator it(p); it != end; ++it )
{
const string deviceId = it->path().filename().c_str();
if ( is_symlink(*it) &&
deviceId.find("usb-Texas_Instruments_Texas_Instruments_MSP430-JTAG_") != string::npos )
{
char linkPath[255] = {0};
const int len = readlink( it->path().string().c_str(), linkPath, sizeof(linkPath) );
if ( len > 0 )
{
string portPath = (linkPath[0] == '/') ? string(linkPath) : p.string() + linkPath;
portList[portPath] = new UsbCdcIoChannel(portPath, deviceId);
}
}
}
}
// Use workaround for distributions without /dev/serial/by-id
else
{
for (int i = 0; i < 16; ++i)
{
stringstream portName;
portName << "ttyACM" << i;
string portPath = string("/dev/") + portName.str();
if ( !access(portPath.c_str(), F_OK) )
{
portList[portPath] = new UsbCdcIoChannel(portPath, portName.str());
}
}
}
#endif
}
std::string UsbCdcIoChannel::retrieveSerialFromId(const std::string& id)
{
#if defined(_WIN32) || defined(_WIN64)
std::stringstream sstr(id);
const size_t idBegin = sstr.str().find_last_of('\\') + 1;
return sstr.str().substr(idBegin, 16);
#else
const size_t begin = id.find_last_of('_') + 1;
const size_t end = id.find_last_of('-');
return id.substr( begin, end - begin );
#endif
}
long UsbCdcIoChannel::getStatus()
{
return status;
}
bool UsbCdcIoChannel::openPort()
{
const std::string dev = portPrefix + name;
ioService = new boost::asio::io_service;
port = new boost::asio::serial_port(*ioService);
if ( boost::system::error_code ec = port->open(dev, ec) )
{
int retry = 5;
while (ec && --retry )
{
boost::this_thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(5));
ec = port->open(dev, ec);
}
if ( ec == boost::system::error_condition(boost::system::errc::permission_denied) )
status = inUseByAnotherInstance;
if (ec)
{
close();
return false;
}
}
return true;
}
void UsbCdcIoChannel::retrieveStatus()
{
status = freeForUse;
if (!isOpen())
{
openPort();
close();
}
}
bool UsbCdcIoChannel::open()
{
if (!isOpen() && !openPort())
{
return false;
}
status = freeForUse;
try
{
const int baudrate = 460800;
port->set_option( serial_port::baud_rate( baudrate ) );
port->set_option( serial_port::flow_control( serial_port::flow_control::none ) );
port->set_option( serial_port::parity( serial_port::parity::none ) );
port->set_option( serial_port::stop_bits( serial_port::stop_bits::one ) );
port->set_option( serial_port::character_size(8) );
}
catch (const boost::system::error_code&) {}
inputBuffer.resize(inputReportSize+3);
return true;
}
void UsbCdcIoChannel::cleanup ()
{
if (isOpen())
{
boost::system::error_code ec = port->close(ec);
}
delete port;
port = 0;
delete ioService;
ioService = 0;
}
bool UsbCdcIoChannel::close ()
{
cleanup();
return true;
}
bool UsbCdcIoChannel::isOpen ()
{
return port && port->is_open();
}
boost::mutex readWriteMutex;
#if BOOST_VERSION >= 107000
#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
#else
#define GET_IO_SERVICE(s) ((s).get_io_service())
#endif
class AsyncTransferHandler
{
public:
AsyncTransferHandler(serial_port& port) :
port(port),
timer(GET_IO_SERVICE(port)),
result(RES_NONE),
bytesTransfered(0) {}
int read(unsigned char* buf, size_t bufSize, uint32_t timeout)
{
timer.expires_from_now(boost::posix_time::milliseconds(timeout));
timer.async_wait(boost::bind(&AsyncTransferHandler::onTimeout, this, _1));
async_read(port, buffer(buf, bufSize),
boost::bind(&AsyncTransferHandler::onTransferComplete, this, _1, _2) );
GET_IO_SERVICE(port).reset();
#if defined(_WIN32) || defined(_WIN64)
while ( result == RES_NONE )
GET_IO_SERVICE(port).run_one();
if (result != RES_COMPLETE)
{
readWriteMutex.lock();
port.cancel();
readWriteMutex.unlock();
}
if (result != RES_TIMEOUT)
timer.cancel();
#else
while ( result == RES_NONE )
GET_IO_SERVICE(port).run();
#endif
GET_IO_SERVICE(port).stop();
return (result == RES_COMPLETE) ? static_cast<int>(bytesTransfered) : -1;
}
private:
enum Result { RES_NONE, RES_ERROR, RES_TIMEOUT, RES_COMPLETE };
serial_port& port;
deadline_timer timer;
Result result;
int bytesTransfered;
void onTransferComplete(const boost::system::error_code& ec, size_t numBytes)
{
#ifdef UNIX
timer.cancel();
#endif
if ( ec != error::operation_aborted && result == RES_NONE )
result = ec ? RES_ERROR : RES_COMPLETE;
bytesTransfered = (result == RES_COMPLETE) ? static_cast<int>(numBytes) : -1;
}
void onTimeout(const boost::system::error_code& ec)
{
#ifdef UNIX
port.cancel();
#endif
if( ec != error::operation_aborted && result == RES_NONE )
result = ec ? RES_ERROR : RES_TIMEOUT;
}
};
int UsbCdcIoChannel::read(HalResponse& resp, uint32_t timeout)
{
if (!isOpen())
return 0;
if(actSize == 0)
expSize = 6;
if (expSize > inputBuffer.size())
inputBuffer.resize(expSize);
int bytesRead = -1;
try
{
AsyncTransferHandler readHandler(*port);
bytesRead = readHandler.read(&inputBuffer[actSize], expSize-actSize, timeout);
} catch(const std::exception&) {}
if (bytesRead < 0)
{
const std::string dev = portPrefix + name;
//Important, test port must be gone before close can be called
boost::system::error_code ec = serial_port(*ioService).open(dev, ec);
if (ec == boost::system::error_condition(boost::system::errc::no_such_file_or_directory))
{
if (comState != ComStateDisconnect)
{
this->close();
}
comState = ComStateDisconnect;
}
}
if (bytesRead > 0)
{
if (actSize == 0)
expSize = inputBuffer[0] + ( (inputBuffer[0] & 0x01) ? 3 : 4);
actSize += static_cast<uint16_t>(bytesRead & 0xFFFF);
//Perform sanity check on message size before trying to read it
if (expSize < actSize || (expSize % 2) != 0)
{
resp.setError(HalResponse::Error_Size);
expSize = actSize = 0;
return bytesRead;
}
if(expSize && actSize == expSize)
{
#ifdef DB_PRINT
Logging::DefaultLogger().PrintReceiveBuffer(&inputBuffer[0], expSize);
#endif // DB_PRINT
const uint16_t msgSize = expSize;
actSize=0;
expSize=0;
resp.setType(inputBuffer[1]);
resp.setId(inputBuffer[2] & 0x7f); //Don't mask async bit (0x40)
resp.setIsComplete(inputBuffer[2]);
if(msgSize >= 2)
{
resp.append(&inputBuffer[1], inputBuffer[0]);
}
const uint16_t expectedCRC = createCrc(&inputBuffer[0]);
const uint16_t actualCRC = inputBuffer[msgSize-2] + (inputBuffer[msgSize-1] << 8);
if( actualCRC != expectedCRC)
{
resp.setError(HalResponse::Error_CRC);
}
return bytesRead;
}
}
return 0;
}
enum ComState UsbCdcIoChannel::poll ()
{
return comState;
}
int UsbCdcIoChannel::write (const uint8_t* payload, size_t len)
{
if (!isOpen())
return 0;
int ret_len = static_cast<int>(len);
uint8_t report[256] = {0};
if (payload)
memcpy(report, payload, len);
// test for fill byte
if (!(report[0] & 0x01))
report[len++] = 0x00;
//create crc and append it to data
uint16_t crc = createCrc(report);
report[len++] = crc & 0x00ff;
report[len++] = (crc & 0xff00) >> 8;
size_t n_write = 0;
uint8_t send_buf[512];
if (isXoffFlowOn)
{
//mask XOFF, XON and MASK in data stream
size_t j = 0;
for (size_t i = 0; i < len; i++)
{
const uint8_t ch = report[i];
switch (ch)
{
case XOFF:
case XON:
case XMASK:
send_buf[j] = XMASK;
j++;
send_buf[j] = ch & 0x3;
break;
default:
send_buf[j] = ch;
}
j++;
}
n_write = j;
}
else
{
n_write = len;
memcpy(send_buf, report, n_write);
}
readWriteMutex.lock();
boost::system::error_code ec;
const size_t nWritten = boost::asio::write(*port, buffer(send_buf, n_write), transfer_all(), ec);
readWriteMutex.unlock();
if (nWritten != n_write)
{
return 0;
}
#ifdef DB_PRINT
Logging::DefaultLogger().PrintSendBuffer(send_buf, n_write);
#endif // DB_PRINT
return ret_len;
}
const char* UsbCdcIoChannel::getName ()
{
return name.c_str();
}
const char* UsbCdcIoChannel::getInterfaceName ()
{
return name.c_str();
}
const std::string UsbCdcIoChannel::getSerial()
{
return serial;
}