first iteration cleanup

136-add-android-support
Kyle Johannes 2023-11-29 17:05:08 -05:00
parent 0b9491e5e3
commit aa77bbbc9e
4 changed files with 742 additions and 801 deletions

View File

@ -5,10 +5,10 @@ cmake_policy(SET CMP0074 NEW)
option(LIBICSNEO_BUILD_TESTS "Build all tests." OFF) option(LIBICSNEO_BUILD_TESTS "Build all tests." OFF)
option(LIBICSNEO_BUILD_DOCS "Build documentation. Don't use in Visual Studio." OFF) option(LIBICSNEO_BUILD_DOCS "Build documentation. Don't use in Visual Studio." OFF)
option(LIBICSNEO_BUILD_EXAMPLES "Build examples." OFF) option(LIBICSNEO_BUILD_EXAMPLES "Build examples." ON)
option(LIBICSNEO_BUILD_ICSNEOC "Build dynamic C library" ON) option(LIBICSNEO_BUILD_ICSNEOC "Build dynamic C library" ON)
option(LIBICSNEO_BUILD_ICSNEOC_STATIC "Build static C library" ON) option(LIBICSNEO_BUILD_ICSNEOC_STATIC "Build static C library" ON)
option(LIBICSNEO_BUILD_ICSNEOLEGACY "Build icsnVC40 compatibility library" OFF) option(LIBICSNEO_BUILD_ICSNEOLEGACY "Build icsnVC40 compatibility library" ON)
set(LIBICSNEO_NPCAP_INCLUDE_DIR "" CACHE STRING "Npcap include directory; set to build with Npcap") set(LIBICSNEO_NPCAP_INCLUDE_DIR "" CACHE STRING "Npcap include directory; set to build with Npcap")
# Device Drivers # Device Drivers
@ -16,12 +16,12 @@ set(LIBICSNEO_NPCAP_INCLUDE_DIR "" CACHE STRING "Npcap include directory; set to
# it is only relevant for communication between Linux and # it is only relevant for communication between Linux and
# CoreMini from the onboard processor of the device. # CoreMini from the onboard processor of the device.
option(LIBICSNEO_ENABLE_FIRMIO "Enable communication between Linux and CoreMini within the same device" OFF) option(LIBICSNEO_ENABLE_FIRMIO "Enable communication between Linux and CoreMini within the same device" OFF)
option(LIBICSNEO_ENABLE_RAW_ETHERNET "Enable devices which communicate over raw ethernet" OFF) option(LIBICSNEO_ENABLE_RAW_ETHERNET "Enable devices which communicate over raw ethernet" ON)
option(LIBICSNEO_ENABLE_CDCACM "Enable devices which communicate over USB CDC ACM" OFF) option(LIBICSNEO_ENABLE_CDCACM "Enable devices which communicate over USB CDC ACM" ON)
option(LIBICSNEO_ENABLE_ANDROIDUSB "Enable devices which communicate over USB CDC ACM on Android" ON) option(LIBICSNEO_ENABLE_ANDROIDUSB "Enable devices which communicate over USB CDC ACM on Android" ON)
option(LIBICSNEO_ENABLE_FTDI "Enable devices which communicate over USB FTDI2XX" OFF) option(LIBICSNEO_ENABLE_FTDI "Enable devices which communicate over USB FTDI2XX" ON)
option(LIBICSNEO_ENABLE_TCP "Enable devices which communicate over TCP" OFF) option(LIBICSNEO_ENABLE_TCP "Enable devices which communicate over TCP" OFF)
option(LIBICSNEO_ENABLE_FTD3XX "Enable devices which communicate over USB FTD3XX" OFF) option(LIBICSNEO_ENABLE_FTD3XX "Enable devices which communicate over USB FTD3XX" ON)
if(NOT CMAKE_CXX_STANDARD) if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
@ -166,12 +166,6 @@ else() # Darwin or Linux
endif() endif()
endif() endif()
endif() endif()
if(LIBICSNEO_ENABLE_ANDROIDUSB)
list(APPEND PLATFORM_SRC
platform/posix/android/androidusb.cpp
)
endif()
endif() endif()

View File

@ -4,13 +4,6 @@
#define ICSNEOC_MAKEDLL #define ICSNEOC_MAKEDLL
#ifdef ICSNEO_ENABLE_ANDROIDUSB
#include "icsneo/platform/android/androidusb.h"
#include <android/log.h>
#define LOG_TAG "libicsneoc"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#endif
#include "icsneo/icsneoc.h" #include "icsneo/icsneoc.h"
#include "icsneo/icsneocpp.h" #include "icsneo/icsneocpp.h"
#include "icsneo/platform/dynamiclib.h" #include "icsneo/platform/dynamiclib.h"
@ -101,7 +94,6 @@ bool icsneo_isValidNeoDevice(const neodevice_t* device) {
// return false on nullptr // return false on nullptr
if(!device) { if(!device) {
EventManager::GetInstance().add(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::RequiredParameterNull, APIEvent::Severity::Error);
LOGD("nullparameter neodevice error\n");
return false; return false;
} }
// If this neodevice_t was returned by a previous search, it will no longer be valid (as the underlying icsneo::Device is freed) // If this neodevice_t was returned by a previous search, it will no longer be valid (as the underlying icsneo::Device is freed)
@ -115,18 +107,17 @@ bool icsneo_isValidNeoDevice(const neodevice_t* device) {
} }
EventManager::GetInstance().add(APIEvent::Type::InvalidNeoDevice, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::InvalidNeoDevice, APIEvent::Severity::Error);
LOGD("Invalid neodevice error\n");
return false; return false;
} }
bool icsneo_openDevice(const neodevice_t* device) { bool icsneo_openDevice(const neodevice_t* device) {
if(!icsneo_isValidNeoDevice(device)) { if(!icsneo_isValidNeoDevice(device)) {
LOGD("Invalid neodevice error\n"); //todo error
return false; return false;
} }
if(!device->device->open()) { if(!device->device->open()) {
LOGD("Device failed to open...\n"); //todo error
return false; return false;
} }
@ -233,7 +224,6 @@ bool icsneo_getMessages(const neodevice_t* device, neomessage_t* messages, size_
return false; return false;
*items = storage.size(); *items = storage.size();
for(size_t i = 0; i < *items; i++) { for(size_t i = 0; i < *items; i++) {
// For each message, copy into neomessage_t buffer given // For each message, copy into neomessage_t buffer given
messages[i] = CreateNeoMessage(storage[i]); messages[i] = CreateNeoMessage(storage[i]);

File diff suppressed because it is too large Load Diff

View File

@ -45,57 +45,14 @@ bool ANDROIDUSB::open() {
LOGD("Failed isOpen check in open(): %ul\n", device.handle); LOGD("Failed isOpen check in open(): %ul\n", device.handle);
return false; return false;
} }
/*
struct termios tty = {};
struct termios compare = {};
if(tcgetattr(fd, &tty) != 0) {
close();
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
report(APIEvent::Type::DriverTCGetAddrFail, APIEvent::Severity::Error);
return false;
}
tty.c_cflag |= (CLOCAL | CREAD); // Ignore modem controls
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8-bit characters
tty.c_cflag &= ~PARENB; // No parity bit
tty.c_cflag &= ~CSTOPB; // One stop bit
tty.c_cflag &= ~CRTSCTS; // No hardware flow control
// Non-canonical mode
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
// Fetch bytes as they become available
// See http://man7.org/linux/man-pages/man3/termios.3.html
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 1; // 100ms timeout (1 decisecond, what?)
if(tcsetattr(fd, TCSAFLUSH, &tty) != 0) { // Flushes input and output buffers as well as setting settings
close();
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
report(APIEvent::Type::DriverTCSetAddrFail, APIEvent::Severity::Error);
return false;
}
if(tcgetattr(fd, &compare) != 0 || memcmp(&tty, &compare, sizeof(struct termios)) != 0) {
close();
return false;
}
*/
// Create threads // Create threads
readThread = std::thread(&ANDROIDUSB::readTask, this); readThread = std::thread(&ANDROIDUSB::readTask, this);
writeThread = std::thread(&ANDROIDUSB::writeTask, this); writeThread = std::thread(&ANDROIDUSB::writeTask, this);
return true; return true;
} }
bool ANDROIDUSB::isOpen() { bool ANDROIDUSB::isOpen() {
LOGD("isOpen handle: %i, openStatus: %s\n", device.handle, openStatus?"true":"false"); //LOGD("isOpen handle: %i, openStatus: %s\n", device.handle, openStatus?"true":"false");
return ((device.handle >= 0) && (openStatus)); // Negative fd indicates error or not opened yet return ((device.handle >= 0) && (openStatus)); // Negative fd indicates error or not opened yet
} }
@ -117,6 +74,7 @@ bool ANDROIDUSB::close() {
disconnected = false; disconnected = false;
systemFDs[device.handle] = nullptr; systemFDs[device.handle] = nullptr;
openStatus = false;
device.handle = -1; device.handle = -1;
uint8_t flush; uint8_t flush;
@ -137,7 +95,7 @@ void ANDROIDUSB::readTask() {
devh = mapItr->second; devh = mapItr->second;
} }
while(!closing && !isDisconnected()) { while(!closing && !isDisconnected()) {
int bytesRead = 0; // ::read(fd, readbuf, READ_BUFFER_SIZE); int bytesRead = 0;
auto ret = libusb_bulk_transfer(devh, ep_in_addr, readbuf, READ_BUFFER_SIZE, &bytesRead, 50); auto ret = libusb_bulk_transfer(devh, ep_in_addr, readbuf, READ_BUFFER_SIZE, &bytesRead, 50);
if (ret == LIBUSB_ERROR_TIMEOUT) { if (ret == LIBUSB_ERROR_TIMEOUT) {
continue; continue;
@ -239,11 +197,11 @@ void ANDROIDUSB::Find(std::vector<FoundDevice>& found) {
LOGD("libusb_init failed: %d\n", ret); LOGD("libusb_init failed: %d\n", ret);
return; return;
} }
LOGD("libusb_init completed: %d\n", ret); //LOGD("libusb_init completed: %d\n", ret);
LOGD("SYSTEM FDs size: %d\n", systemFDs.size()); //LOGD("SYSTEM FDs size: %d\n", systemFDs.size());
for (auto & [fd, libUsbHandle]: systemFDs) { for (auto & [fd, libUsbHandle]: systemFDs) {
ret = libusb_wrap_sys_device(ctx, (intptr_t) fd, &libusbDeviceHandle); ret = libusb_wrap_sys_device(ctx, (intptr_t) fd, &libusbDeviceHandle);
LOGD("Wrapping system FD: %d, return: %s\n", fd, libusb_strerror(ret)); //LOGD("Wrapping system FD: %d, return: %s\n", fd, libusb_strerror(ret));
if (ret == 0) { if (ret == 0) {
FoundDevice device = {}; FoundDevice device = {};
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
@ -252,14 +210,13 @@ void ANDROIDUSB::Find(std::vector<FoundDevice>& found) {
ret = libusb_get_device_descriptor(libusb_get_device(libusbDeviceHandle), &desc); ret = libusb_get_device_descriptor(libusb_get_device(libusbDeviceHandle), &desc);
if (ret == 0) { if (ret == 0) {
device.productId = desc.idProduct; device.productId = desc.idProduct;
LOGD("ProductID: %x\n", device.productId); //LOGD("ProductID: %x\n", device.productId);
ret = libusb_get_string_descriptor_ascii(libusbDeviceHandle, desc.iSerialNumber, ret = libusb_get_string_descriptor_ascii(libusbDeviceHandle, desc.iSerialNumber,
outString, sizeof(outString)); outString, sizeof(outString));
if (ret > 0) { if (ret > 0) {
std::strncpy(device.serial, reinterpret_cast<char *>(outString), std::strncpy(device.serial, reinterpret_cast<char *>(outString),
sizeof(device.serial)); sizeof(device.serial));
LOGD("Serial Number: %s\n", device.serial);
} else { } else {
LOGD("Serial number fetch failed!\n"); LOGD("Serial number fetch failed!\n");
} }