Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Platforms/dotnet/Uart.cpp

Committer:
IanBenzMaxim
Date:
2017-11-06
Revision:
0:f77ad7f72d04

File content as of revision 0:f77ad7f72d04:

/*******************************************************************************
* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************/

#include <msclr/auto_gcroot.h>
#include <msclr/marshal_cppstd.h>
#include <MaximInterface/Utilities/Error.hpp>
#include "ChangeSizeType.hpp"
#include "Sleep.hpp"
#include "Uart.hpp"

using msclr::interop::marshal_as;
using std::string;
using namespace System;
using System::IO::Ports::SerialPort;
using System::Runtime::InteropServices::Marshal;

namespace MaximInterface {
namespace dotnet {

template <typename Type, typename Input> static bool isType(Input in) {
  return dynamic_cast<Type>(in) != nullptr;
}

template <typename Func>
static error_code executeTryCatchOperation(Func tryOperation) {
  return executeTryCatchOperation(tryOperation, [] {});
}

template <typename TryFunc, typename CatchFunc>
static error_code executeTryCatchOperation(TryFunc tryOperation,
                                           CatchFunc catchOperation) {
  error_code result;
  try {
    tryOperation();
  } catch (Exception ^ e) {
    catchOperation();
    if (isType<ArgumentException ^>(e)) {
      result = make_error_code(Uart::ArgumentError);
    } else if (isType<InvalidOperationException ^>(e)) {
      result = make_error_code(Uart::InvalidOperationError);
    } else if (isType<System::IO::IOException ^>(e)) {
      result = make_error_code(Uart::IOError);
    } else if (isType<UnauthorizedAccessException ^>(e)) {
      result = make_error_code(Uart::UnauthorizedAccessError);
    } else if (isType<TimeoutException ^>(e)) {
      result = make_error_code(Uart::TimeoutError);
    } else {
      throw;
    }
  }
  return result;
}

template <typename Func>
static error_code executeIfConnected(const Uart & serial, Func operation) {
  return serial.connected() ? operation()
                            : make_error_code(Uart::NotConnectedError);
}

struct Uart::Data {
  msclr::auto_gcroot<SerialPort ^> port;
};

Uart::Uart() : data(std::make_unique<Data>()) {}

Uart::~Uart() = default;

Uart::Uart(Uart &&) noexcept = default;

Uart & Uart::operator=(Uart &&) noexcept = default;

error_code Uart::connect(const string & portName) {
  data->port = gcnew SerialPort;
  return executeTryCatchOperation(
      [this, &portName] {
        data->port->PortName = marshal_as<String ^>(portName);
        data->port->DtrEnable = true;
        data->port->Open();
      },
      [this] { data->port.reset(); });
}

void Uart::disconnect() { data->port.reset(); }

bool Uart::connected() const { return data->port.get() != nullptr; }

string Uart::portName() const {
  return connected() ? marshal_as<string>(data->port->PortName) : "";
}

error_code Uart::setBaud(int_least32_t baud) {
  return executeIfConnected(*this, [this, baud] {
    return executeTryCatchOperation(
        [this, baud] { data->port->BaudRate = baud; });
  });
}

error_code Uart::sendBreak() {
  return executeIfConnected(*this, [this] {
    return executeTryCatchOperation([this] {
      data->port->BreakState = true;
      sleep(1);
      data->port->BreakState = false;
    });
  });
}

error_code Uart::clearReadBuffer() {
  return executeIfConnected(*this, [this] {
    return executeTryCatchOperation([this] { data->port->ReadExisting(); });
  });
}

error_code Uart::writeByte(uint_least8_t data) { return writeBlock(&data, 1); }

error_code Uart::writeBlock(const uint_least8_t * data, size_t dataLen) {
  return executeIfConnected(*this, [this, data, dataLen] {
    return executeTryCatchOperation([this, data, dataLen] {
      changeSizeType<int>(
          [this](const uint_least8_t * dataChunk, int dataChunkSize) {
            auto dataManaged = gcnew array<uint_least8_t>(dataChunkSize);
            Marshal::Copy(
                static_cast<IntPtr>(const_cast<uint_least8_t *>(dataChunk)),
                dataManaged, 0, dataChunkSize);
            this->data->port->Write(dataManaged, 0, dataChunkSize);
          },
          data, dataLen);
    });
  });
}

error_code Uart::readByte(uint_least8_t & data) {
  return executeIfConnected(*this, [this, &data] {
    return executeTryCatchOperation(
        [this, &data] { data = this->data->port->ReadByte(); });
  });
}

error_code Uart::readBlock(uint_least8_t * data, size_t dataLen) {
  return executeIfConnected(*this, [this, data, dataLen] {
    return executeTryCatchOperation([this, data, dataLen] {
      changeSizeType<int>(
          [this](uint_least8_t * dataChunk, int dataChunkSize) {
            auto dataManaged = gcnew array<uint_least8_t>(dataChunkSize);
            int bytesRead = 0;
            do {
              bytesRead += this->data->port->Read(dataManaged, bytesRead,
                                                  dataChunkSize - bytesRead);
            } while (bytesRead < dataChunkSize);
            Marshal::Copy(dataManaged, 0, static_cast<IntPtr>(dataChunk),
                          dataChunkSize);
          },
          data, dataLen);
    });
  });
}

const error_category & Uart::errorCategory() {
  static class : public error_category {
  public:
    virtual const char * name() const { return "dotnet UART"; }

    virtual string message(int condition) const {
      switch (condition) {
      case NotConnectedError:
        return "Not Connected Error";

      case ArgumentError:
        return "Argument Error";

      case InvalidOperationError:
        return "Invalid Operation Error";

      case IOError:
        return "IO Error";

      case UnauthorizedAccessError:
        return "Unauthorized Access Error";

      default:
        return defaultErrorMessage(condition);
      }
    }
  } instance;
  return instance;
}

} // namespace dotnet
} // namespace MaximInterface