ConnectionManager.cpp 9.13 KB
Newer Older
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
/**
    @file ConnectionManager.cpp
    @author Lime Microsystems (www.limemicro.com)
    @brief Implementation of various connection types to devices
*/

#include "ConnectionManager.h"
#include "ConnectionCOM.h"

#ifdef ENABLE_USB_CONNECTION
    #include "ConnectionUSB.h"
#endif
#ifdef ENABLE_SPI_CONNECTION
    #include "ConnectionSPI.h"
#endif

#include <sstream>
#include <iomanip>
#include <iostream>

/** @brief Creates connection interfaces
*/
ConnectionManager::ConnectionManager(const IConnection::eConnectionType port_type) : activeControlPort(NULL)
{
    mLogData = false;
    mOpenedDevice = -1;
    if (port_type == IConnection::COM_PORT)
        m_connections[IConnection::COM_PORT] = new ConnectionCOM();
#ifdef ENABLE_USB_CONNECTION
    if (port_type == IConnection::USB_PORT)
        m_connections[IConnection::USB_PORT] = new ConnectionUSB();
#endif
#ifdef ENABLE_SPI_CONNECTION
    m_connections[IConnection::SPI_PORT] = new ConnectionSPI();
#endif
}

/** @brief Destroys connection interfaces
*/
ConnectionManager::~ConnectionManager()
{
	for (auto iter = m_connections.begin(); iter != m_connections.end(); ++iter)
	{
		delete iter->second;
	}
}

/** @brief Checks if connection to device is opened
    @return True if device is connected
*/
bool ConnectionManager::IsOpen()
{
    return activeControlPort ? activeControlPort->IsOpen() : false;
}

/** @brief Opens connection to first available device
    @return True if connected to device
*/
bool ConnectionManager::Open()
{
    return Open(0);
}

/** @brief Connects to selected device
    @param i device index from device list
    @return 1:Success, 0:failure
*/
int ConnectionManager::Open(unsigned i)
{
    if(i >= mDevices.size())
        return 0;

    if(activeControlPort)
        activeControlPort->Close();
    switch(mDevices[i].port)
    {
    case IConnection::COM_PORT:
        activeControlPort = m_connections[IConnection::COM_PORT];
        break;
    case IConnection::USB_PORT:
		activeControlPort = m_connections[IConnection::USB_PORT];
        break;
    case IConnection::SPI_PORT:
		activeControlPort = m_connections[IConnection::SPI_PORT];
        break;
    default:
        return 0;
    }
    mOpenedDevice = -1;
    if( i < mDevices.size() )
    {
        if (activeControlPort->Open(mDevices[i].portIndex) == IConnection::SUCCESS)
        {
            mOpenedDevice = i;
            return 1;
        }
    }
    return 0;
}

/** @brief Closes connection to device
*/
void ConnectionManager::Close()
{
    if(activeControlPort)
    {
        activeControlPort->Close();
		//Notify(LMS_Message(MSG_BOARD_DISCONNECTED, "", 0, 0));
    }
    mOpenedDevice = -1;
}

/** @brief Finds all currently connected devices and forms device list
    @return number of devices found
*/
int ConnectionManager::RefreshDeviceList()
{
    mDeviceList.clear();
    mDevices.clear();
    DeviceInfo dev;
    for (auto iter = m_connections.begin(); iter != m_connections.end(); ++iter)
    {
        vector<string> names;
        IConnection *port = iter->second;
        if(port->RefreshDeviceList() > 0)
        {
            names = port->GetDeviceNames();
            for(unsigned i=0; i<names.size(); ++i)
            {
                dev.name = names[i];
                dev.port = iter->first;
                dev.portIndex = i;
                mDevices.push_back(dev);
            }
        }
    }
    for(unsigned i=0; i<mDevices.size(); ++i)
        mDeviceList.push_back(mDevices[i].name);
    return mDevices.size();
}

/** @brief Returns currently opened connection index
*/
int ConnectionManager::GetOpenedIndex()
{
    return mOpenedDevice;
}

/** @brief Writes given data to currently opened connection
    @param buffer outcomming data buffer
    @param length bytes to write
    @param timeout_ms timeout in milliseconds
    @return number of bytes written, on failure negative values
*/
int ConnectionManager::Write(const unsigned char *buffer, const int length, int timeout_ms)
{
    if(activeControlPort)
    {
        int bytesTransferred = activeControlPort->Write(buffer, length, timeout_ms);
#ifndef NDEBUG
        if(mLogData)
        {
            stringstream ss;
            ss << "WR(" <<  (bytesTransferred>=0?bytesTransferred: 0) << "): ";
            ss << std::hex << std::setfill('0');
            int repeatedZeros = 0;
            for(int i=length-1; i>=0; --i)
                if(buffer[i] == 0)
                    ++repeatedZeros;
                else break;
            if(repeatedZeros == 1)
                repeatedZeros = 0;
            repeatedZeros = repeatedZeros - (repeatedZeros & 0x1);
            for(int i=0; i<length-repeatedZeros; ++i)
                //casting to short to print as numbers
                ss << " " << std::setw(2) << (unsigned short)buffer[i];
            if(repeatedZeros > 1)
                ss << " (00 x " << std::dec << repeatedZeros << " times)";
            cout << ss.str() << endl;
#ifndef __unix__
            OutputDebugString(ss.str().c_str());
#endif
        }
#endif
        return bytesTransferred;
    }
    return -1;
}

/** @brief Receives data from currently opened connection
    @param buffer incomming data buffer, must be big enough for length bytes
    @param length bytes to read
    @param timeout_ms timeout in milliseconds
    @return number of bytes received
*/
int ConnectionManager::Read(unsigned char *buffer, int length, int timeout_ms)
{
    if(activeControlPort)
    {
        int bytesTransferred = activeControlPort->Read(buffer, length, timeout_ms);
#ifndef NDEBUG
        if(mLogData)
        {
            stringstream ss;
            ss << "RD(" <<  (bytesTransferred>=0?bytesTransferred: 0) << "): ";
            ss << std::hex << std::setfill('0');
            int repeatedZeros = 0;
            for(int i=length-1; i>=0; --i)
                if(buffer[i] == 0)
                    ++repeatedZeros;
                else break;
            if(repeatedZeros == 2)
                repeatedZeros = 0;
            repeatedZeros = repeatedZeros - (repeatedZeros & 0x1);
            for(int i=0; i<length-repeatedZeros; ++i)
                //casting to short to print as numbers
                ss << " " << std::setw(2) << (unsigned short)buffer[i];
            if(repeatedZeros > 2)
                ss << " (00 x " << std::dec << repeatedZeros << " times)";
            cout << ss.str() << endl;
        }
#endif
        return bytesTransferred;
    }
    return -1;
}

int ConnectionManager::WriteStream(const char *buffer, int length)
{
	return 0;
}

int ConnectionManager::ReadStream(char *buffer, int length, unsigned int timeout_ms)
{
	/*int handle = activeControlPort->BeginDataReading(buffer, length);
    activeControlPort->WaitForReading(handle, timeout_ms);
	long received = length;
	activeControlPort->FinishDataReading(buffer, received, handle);
	return received;
    */
    long len = length;
    int status = activeControlPort->ReadDataBlocking(buffer, len, 0);
    return len;
}


int ConnectionManager::BeginDataReading(char *buffer, long length)
{
    return activeControlPort->BeginDataReading(buffer, length);
}
/**
@brief Blocks until data is received or set number of milliseconds have passed.
@param contextHandle handle returned by BeginDataReading()
@param timeout_ms number of milliseconds to wait
@return 1-data received, 0-data not received
*/
int ConnectionManager::WaitForReading(int contextHandle, unsigned int timeout_ms)
{
    return activeControlPort->WaitForReading(contextHandle, timeout_ms);
}
/**
@brief Finished asynchronous data reading.
@param buffer where to put received data
@param length number of bytes to read, will be changed to actual number of bytes received
@param contextHandle context handle returned by BeginDataReading()
@return received data length
*/
int ConnectionManager::FinishDataReading(char *buffer, long &length, int contextHandle)
{
    return activeControlPort->FinishDataReading(buffer, length, contextHandle);
}

/**
@brief Aborts reading operations
*/
void ConnectionManager::AbortReading()
{
    activeControlPort->AbortReading();
}

/**
@brief Start asynchronous data sending.
@param buffer data buffer to be sent
@param length number of bytes to send.
@return context handle
*/
int ConnectionManager::BeginDataSending(const char *buffer, long length)
{
    return activeControlPort->BeginDataSending(buffer, length);
}
/**
@brief Blocks until data is sent or set number of miliseconds have passed.
@param contextHandle handle returned by BeginDataReading()
@param timeout_ms number of miliseconds to wait
@return 1-data sent, 0-data not sent
*/
int ConnectionManager::WaitForSending(int contextHandle, unsigned int timeout_ms)
{
    return activeControlPort->WaitForSending(contextHandle, timeout_ms);
}
/**
@brief Finished asynchronous data sending.
@param buffer where to put received data
@param length number of bytes to send, will be changed to actual number of bytes sent
@param contextHandle context handle returned by BeginDataReading()
@return sent data length
*/
int ConnectionManager::FinishDataSending(const char *buffer, long &length, int contextHandle)
{
    return activeControlPort->FinishDataSending(buffer, length, contextHandle);
}

/**
@brief Aborts sending operations
*/
void ConnectionManager::AbortSending()
{
    activeControlPort->AbortSending();
}