Initial commit

This commit is contained in:
Paul Hollinsky
2018-09-10 20:28:29 -04:00
commit e2e5017331
133 changed files with 24597 additions and 0 deletions
+130
View File
@@ -0,0 +1,130 @@
#ifndef __cplusplus
#error "icsneoc.cpp must be compiled with a C++ compiler!"
#endif
#define ICSNEOC_MAKEDLL
#include "include/icsneoc.h"
#include "api/icsneocpp/include/icsneocpp.h"
#include "platform/include/dynamiclib.h"
#include <string>
#include <vector>
#include <memory>
#include <algorithm>
#include <cstring>
using namespace icsneo;
// Holds references for the shared_ptrs so they do not get freed until we're ready
static std::vector<std::shared_ptr<Device>> connectableFoundDevices, connectedDevices;
// Any shared_ptrs we've let go should be placed here so they're not accessed
static std::vector<Device*> freedDevices;
void icsneoFindAllDevices(neodevice_t* devices, size_t* count) {
icsneoFreeUnconnectedDevices(); // Mark previous results as freed so they can no longer be connected to
auto foundDevices = icsneo::FindAllDevices();
auto inputSize = *count;
*count = foundDevices.size();
auto outputSize = *count;
if(outputSize > inputSize) {
// TODO an error should be returned that the data was truncated
outputSize = inputSize;
}
for(size_t i = 0; i < outputSize; i++) {
connectableFoundDevices.push_back(foundDevices[i]);
devices[i] = foundDevices[i]->getNeoDevice();
}
}
void icsneoFreeUnconnectedDevices() {
for(auto& devptr : connectableFoundDevices) {
freedDevices.push_back(devptr.get());
}
connectableFoundDevices.clear();
}
bool icsneoSerialNumToString(uint32_t num, char* str, size_t* count) {
auto result = Device::SerialNumToString(num);
if(*count <= result.length()) {
*count = result.length() + 1; // This is how big of a buffer we need
return false;
}
strcpy(str, result.c_str()); // TODO bad
*count = result.length();
return true;
}
uint32_t icsneoSerialStringToNum(const char* str) {
return Device::SerialStringToNum(str);
}
bool icsneoIsValidNeoDevice(const neodevice_t* device) {
// If this neodevice_t was returned by a previous search, it will no longer be valid (as the underlying icsneo::Device is freed)
return std::find(freedDevices.begin(), freedDevices.end(), device->device) == freedDevices.end();
}
bool icsneoOpenDevice(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device))
return false;
if(!device->device->open())
return false;
// We connected successfully, move the device to connected devices
std::vector<std::vector<std::shared_ptr<Device>>::iterator> itemsToMove;
for(auto it = connectableFoundDevices.begin(); it < connectableFoundDevices.end(); it++) {
if((*it).get() == device->device)
itemsToMove.push_back(it);
}
for(auto it : itemsToMove) {
connectedDevices.push_back(*it);
connectableFoundDevices.erase(it);
}
return true;
}
bool icsneoCloseDevice(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device))
return false;
if(!device->device->close())
return false;
// We disconnected successfully, free the device and mark it as freed
std::vector<std::vector<std::shared_ptr<Device>>::iterator> itemsToDelete;
for(auto it = connectedDevices.begin(); it < connectedDevices.end(); it++) {
if((*it).get() == device->device)
itemsToDelete.push_back(it);
}
for(auto it : itemsToDelete)
connectedDevices.erase(it);
freedDevices.push_back(device->device);
return true;
}
bool icsneoGoOnline(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device))
return false;
return device->device->goOnline();
}
bool icsneoGoOffline(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device))
return false;
return device->device->goOffline();
}
bool icsneoIsOnline(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device))
return false;
return device->device->isOnline();
}
+109
View File
@@ -0,0 +1,109 @@
#ifndef __ICSNEOC_H_
#define __ICSNEOC_H_
#include <stddef.h>
#include "device/include/neodevice.h" // For neodevice_t
#include "platform/include/dynamiclib.h" // Dynamic library loading and exporting
#ifndef ICSNEOC_DYNAMICLOAD
#ifdef __cplusplus
extern "C" {
#endif
extern void DLLExport icsneoFindAllDevices(neodevice_t* devices, size_t* count);
extern void DLLExport icsneoFreeUnconnectedDevices();
extern bool DLLExport icsneoSerialNumToString(uint32_t num, char* str, size_t* count);
extern uint32_t DLLExport icsneoSerialStringToNum(const char* str);
extern bool DLLExport icsneoIsValidNeoDevice(const neodevice_t* device);
extern bool DLLExport icsneoOpenDevice(const neodevice_t* device);
extern bool DLLExport icsneoCloseDevice(const neodevice_t* device);
extern bool DLLExport icsneoGoOnline(const neodevice_t* device);
extern bool DLLExport icsneoGoOffline(const neodevice_t* device);
extern bool DLLExport icsneoIsOnline(const neodevice_t* device);
#ifdef __cplusplus
}
#endif
#else // ICSNEOC_DYNAMICLOAD
typedef void(*fn_icsneoFindAllDevices)(neodevice_t* devices, size_t* count);
fn_icsneoFindAllDevices icsneoFindAllDevices;
typedef void(*fn_icsneoFreeUnconnectedDevices)();
fn_icsneoFreeUnconnectedDevices icsneoFreeUnconnectedDevices;
typedef bool(*fn_icsneoSerialNumToString)(uint32_t num, char* str, size_t* count);
fn_icsneoSerialNumToString icsneoSerialNumToString;
typedef uint32_t(*fn_icsneoSerialStringToNum)(const char* str);
fn_icsneoSerialStringToNum icsneoSerialStringToNum;
typedef bool(*fn_icsneoIsValidNeoDevice)(const neodevice_t* device);
fn_icsneoIsValidNeoDevice icsneoIsValidNeoDevice;
typedef bool(*fn_icsneoOpenDevice)(const neodevice_t* device);
fn_icsneoOpenDevice icsneoOpenDevice;
typedef bool(*fn_icsneoCloseDevice)(const neodevice_t* device);
fn_icsneoCloseDevice icsneoCloseDevice;
typedef bool(*fn_icsneoGoOnline)(const neodevice_t* device);
fn_icsneoGoOnline icsneoGoOnline;
typedef bool(*fn_icsneoGoOffline)(const neodevice_t* device);
fn_icsneoGoOffline icsneoGoOffline;
typedef bool(*fn_icsneoIsOnline)(const neodevice_t* device);
fn_icsneoIsOnline icsneoIsOnline;
#define ICSNEO_IMPORT(func) func = (fn_##func)icsneoDynamicLibraryGetFunction(icsneoLibraryHandle, #func)
#define ICSNEO_IMPORTASSERT(func) if((ICSNEO_IMPORT(func)) == NULL) return 3
void* icsneoLibraryHandle = NULL;
bool icsneoInitialized = false;
bool icsneoDestroyed = false;
int icsneoInit() {
icsneoDestroyed = false;
if(icsneoInitialized)
return 1;
icsneoLibraryHandle = icsneoDynamicLibraryLoad();
if(icsneoLibraryHandle == NULL)
return 2;
ICSNEO_IMPORTASSERT(icsneoFindAllDevices);
ICSNEO_IMPORTASSERT(icsneoFreeUnconnectedDevices);
ICSNEO_IMPORTASSERT(icsneoSerialNumToString);
ICSNEO_IMPORTASSERT(icsneoSerialStringToNum);
ICSNEO_IMPORTASSERT(icsneoIsValidNeoDevice);
ICSNEO_IMPORTASSERT(icsneoOpenDevice);
ICSNEO_IMPORTASSERT(icsneoCloseDevice);
ICSNEO_IMPORTASSERT(icsneoGoOnline);
ICSNEO_IMPORTASSERT(icsneoGoOffline);
ICSNEO_IMPORTASSERT(icsneoIsOnline);
icsneoInitialized = true;
return 0;
}
bool icsneoClose() ICSNEO_DESTRUCTOR {
icsneoInitialized = false;
if(icsneoDestroyed)
return true;
return icsneoDestroyed = icsneoDynamicLibraryClose(icsneoLibraryHandle);
}
#endif // ICSNEOC_DYNAMICLOAD
#endif // __ICSNEOC_H_