Refactor icsneoc to icsneo_functionName to avoid conflicts with legacy

pull/4/head
Paul Hollinsky 2018-09-26 16:38:28 -04:00
parent c391bb97a4
commit b0f5ad0f7f
6 changed files with 122 additions and 112 deletions

View File

@ -22,13 +22,22 @@ static std::vector<std::shared_ptr<Device>> connectableFoundDevices, connectedDe
// We store an array of shared_ptr messages per device, this is the owner of the shared_ptr on behalf of the C interface // We store an array of shared_ptr messages per device, this is the owner of the shared_ptr on behalf of the C interface
static std::map<devicehandle_t, std::vector<std::shared_ptr<Message>>> polledMessageStorage; static std::map<devicehandle_t, std::vector<std::shared_ptr<Message>>> polledMessageStorage;
void icsneoFindAllDevices(neodevice_t* devices, size_t* count) { void icsneo_findAllDevices(neodevice_t* devices, size_t* count) {
icsneoFreeUnconnectedDevices(); // Mark previous results as freed so they can no longer be connected to std::vector<std::shared_ptr<Device>> foundDevices = icsneo::FindAllDevices();
auto foundDevices = icsneo::FindAllDevices();
if(count == nullptr)
return;
auto inputSize = *count; if(devices == nullptr) {
*count = foundDevices.size();
return;
}
icsneo_freeUnconnectedDevices(); // Mark previous results as freed so they can no longer be connected to
size_t inputSize = *count;
*count = foundDevices.size(); *count = foundDevices.size();
auto outputSize = *count; size_t outputSize = *count;
if(outputSize > inputSize) { if(outputSize > inputSize) {
// TODO an error should be returned that the data was truncated // TODO an error should be returned that the data was truncated
outputSize = inputSize; outputSize = inputSize;
@ -40,11 +49,11 @@ void icsneoFindAllDevices(neodevice_t* devices, size_t* count) {
} }
} }
void icsneoFreeUnconnectedDevices() { void icsneo_freeUnconnectedDevices() {
connectableFoundDevices.clear(); connectableFoundDevices.clear();
} }
bool icsneoSerialNumToString(uint32_t num, char* str, size_t* count) { bool icsneo_serialNumToString(uint32_t num, char* str, size_t* count) {
auto result = Device::SerialNumToString(num); auto result = Device::SerialNumToString(num);
if(*count <= result.length()) { if(*count <= result.length()) {
*count = result.length() + 1; // This is how big of a buffer we need *count = result.length() + 1; // This is how big of a buffer we need
@ -56,11 +65,11 @@ bool icsneoSerialNumToString(uint32_t num, char* str, size_t* count) {
return true; return true;
} }
uint32_t icsneoSerialStringToNum(const char* str) { uint32_t icsneo_serialStringToNum(const char* str) {
return Device::SerialStringToNum(str); return Device::SerialStringToNum(str);
} }
bool icsneoIsValidNeoDevice(const neodevice_t* device) { bool icsneo_isValidNeoDevice(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) // If this neodevice_t was returned by a previous search, it will no longer be valid (as the underlying icsneo::Device is freed)
for(auto& dev : connectedDevices) { for(auto& dev : connectedDevices) {
if(dev.get() == device->device) if(dev.get() == device->device)
@ -73,8 +82,8 @@ bool icsneoIsValidNeoDevice(const neodevice_t* device) {
return false; return false;
} }
bool icsneoOpenDevice(const neodevice_t* device) { bool icsneo_openDevice(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->open()) if(!device->device->open())
@ -94,8 +103,8 @@ bool icsneoOpenDevice(const neodevice_t* device) {
return true; return true;
} }
bool icsneoCloseDevice(const neodevice_t* device) { bool icsneo_closeDevice(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->close()) if(!device->device->close())
@ -113,44 +122,44 @@ bool icsneoCloseDevice(const neodevice_t* device) {
return true; return true;
} }
bool icsneoGoOnline(const neodevice_t* device) { bool icsneo_goOnline(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
return device->device->goOnline(); return device->device->goOnline();
} }
bool icsneoGoOffline(const neodevice_t* device) { bool icsneo_goOffline(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
return device->device->goOffline(); return device->device->goOffline();
} }
bool icsneoIsOnline(const neodevice_t* device) { bool icsneo_isOnline(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
return device->device->isOnline(); return device->device->isOnline();
} }
bool icsneoEnableMessagePolling(const neodevice_t* device) { bool icsneo_enableMessagePolling(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
device->device->enableMessagePolling(); device->device->enableMessagePolling();
return true; return true;
} }
bool icsneoDisableMessagePolling(const neodevice_t* device) { bool icsneo_disableMessagePolling(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
return device->device->disableMessagePolling(); return device->device->disableMessagePolling();
} }
bool icsneoGetMessages(const neodevice_t* device, neomessage_t* messages, size_t* items) { bool icsneo_getMessages(const neodevice_t* device, neomessage_t* messages, size_t* items) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(items == nullptr) if(items == nullptr)
@ -174,20 +183,20 @@ bool icsneoGetMessages(const neodevice_t* device, neomessage_t* messages, size_t
messages[i] = CreateNeoMessage(*(storage[i])); messages[i] = CreateNeoMessage(*(storage[i]));
} }
// The user now has until the next call of icsneoGetMessages (for this device) to use the data, after which point it's freed // The user now has until the next call of icsneo_getMessages (for this device) to use the data, after which point it's freed
// The user should copy the data out if they want it // The user should copy the data out if they want it
return true; return true;
} }
size_t icsneoGetPollingMessageLimit(const neodevice_t* device) { size_t icsneo_getPollingMessageLimit(const neodevice_t* device) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return 0; return 0;
return device->device->getPollingMessageLimit(); return device->device->getPollingMessageLimit();
} }
bool icsneoSetPollingMessageLimit(const neodevice_t* device, size_t newLimit) { bool icsneo_setPollingMessageLimit(const neodevice_t* device, size_t newLimit) {
if(!icsneoIsValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
device->device->setPollingMessageLimit(newLimit); device->device->setPollingMessageLimit(newLimit);

View File

@ -12,127 +12,128 @@
extern "C" { extern "C" {
#endif #endif
extern void DLLExport icsneoFindAllDevices(neodevice_t* devices, size_t* count);
extern void DLLExport icsneoFreeUnconnectedDevices(); extern void DLLExport icsneo_findAllDevices(neodevice_t* devices, size_t* count);
extern bool DLLExport icsneoSerialNumToString(uint32_t num, char* str, size_t* count); extern void DLLExport icsneo_freeUnconnectedDevices();
extern uint32_t DLLExport icsneoSerialStringToNum(const char* str); extern bool DLLExport icsneo_serialNumToString(uint32_t num, char* str, size_t* count);
extern bool DLLExport icsneoIsValidNeoDevice(const neodevice_t* device); extern uint32_t DLLExport icsneo_serialStringToNum(const char* str);
extern bool DLLExport icsneoOpenDevice(const neodevice_t* device); extern bool DLLExport icsneo_isValidNeoDevice(const neodevice_t* device);
extern bool DLLExport icsneoCloseDevice(const neodevice_t* device); extern bool DLLExport icsneo_openDevice(const neodevice_t* device);
extern bool DLLExport icsneoGoOnline(const neodevice_t* device); extern bool DLLExport icsneo_closeDevice(const neodevice_t* device);
extern bool DLLExport icsneoGoOffline(const neodevice_t* device); extern bool DLLExport icsneo_goOnline(const neodevice_t* device);
extern bool DLLExport icsneoIsOnline(const neodevice_t* device); extern bool DLLExport icsneo_goOffline(const neodevice_t* device);
extern bool DLLExport icsneoEnableMessagePolling(const neodevice_t* device); extern bool DLLExport icsneo_isOnline(const neodevice_t* device);
extern bool DLLExport icsneoDisableMessagePolling(const neodevice_t* device); extern bool DLLExport icsneo_enableMessagePolling(const neodevice_t* device);
extern bool DLLExport icsneoGetMessages(const neodevice_t* device, neomessage_t* messages, size_t* items); extern bool DLLExport icsneo_disableMessagePolling(const neodevice_t* device);
extern size_t DLLExport icsneoGetPollingMessageLimit(const neodevice_t* device); extern bool DLLExport icsneo_getMessages(const neodevice_t* device, neomessage_t* messages, size_t* items);
extern bool DLLExport icsneoSetPollingMessageLimit(const neodevice_t* device, size_t newLimit); extern size_t DLLExport icsneo_getPollingMessageLimit(const neodevice_t* device);
extern bool DLLExport icsneo_setPollingMessageLimit(const neodevice_t* device, size_t newLimit);
#ifdef __cplusplus #ifdef __cplusplus
} } // extern "C"
#endif #endif
#else // ICSNEOC_DYNAMICLOAD #else // ICSNEOC_DYNAMICLOAD
typedef void(*fn_icsneoFindAllDevices)(neodevice_t* devices, size_t* count); typedef void(*fn_icsneo_findAllDevices)(neodevice_t* devices, size_t* count);
fn_icsneoFindAllDevices icsneoFindAllDevices; fn_icsneo_findAllDevices icsneo_findAllDevices;
typedef void(*fn_icsneoFreeUnconnectedDevices)(); typedef void(*fn_icsneo_freeUnconnectedDevices)();
fn_icsneoFreeUnconnectedDevices icsneoFreeUnconnectedDevices; fn_icsneo_freeUnconnectedDevices icsneo_freeUnconnectedDevices;
typedef bool(*fn_icsneoSerialNumToString)(uint32_t num, char* str, size_t* count); typedef bool(*fn_icsneo_serialNumToString)(uint32_t num, char* str, size_t* count);
fn_icsneoSerialNumToString icsneoSerialNumToString; fn_icsneo_serialNumToString icsneo_serialNumToString;
typedef uint32_t(*fn_icsneoSerialStringToNum)(const char* str); typedef uint32_t(*fn_icsneo_serialStringToNum)(const char* str);
fn_icsneoSerialStringToNum icsneoSerialStringToNum; fn_icsneo_serialStringToNum icsneo_serialStringToNum;
typedef bool(*fn_icsneoIsValidNeoDevice)(const neodevice_t* device); typedef bool(*fn_icsneo_isValidNeoDevice)(const neodevice_t* device);
fn_icsneoIsValidNeoDevice icsneoIsValidNeoDevice; fn_icsneo_isValidNeoDevice icsneo_isValidNeoDevice;
typedef bool(*fn_icsneoOpenDevice)(const neodevice_t* device); typedef bool(*fn_icsneo_openDevice)(const neodevice_t* device);
fn_icsneoOpenDevice icsneoOpenDevice; fn_icsneo_openDevice icsneo_openDevice;
typedef bool(*fn_icsneoCloseDevice)(const neodevice_t* device); typedef bool(*fn_icsneo_closeDevice)(const neodevice_t* device);
fn_icsneoCloseDevice icsneoCloseDevice; fn_icsneo_closeDevice icsneo_closeDevice;
typedef bool(*fn_icsneoGoOnline)(const neodevice_t* device); typedef bool(*fn_icsneo_goOnline)(const neodevice_t* device);
fn_icsneoGoOnline icsneoGoOnline; fn_icsneo_goOnline icsneo_goOnline;
typedef bool(*fn_icsneoGoOffline)(const neodevice_t* device); typedef bool(*fn_icsneo_goOffline)(const neodevice_t* device);
fn_icsneoGoOffline icsneoGoOffline; fn_icsneo_goOffline icsneo_goOffline;
typedef bool(*fn_icsneoIsOnline)(const neodevice_t* device); typedef bool(*fn_icsneo_isOnline)(const neodevice_t* device);
fn_icsneoIsOnline icsneoIsOnline; fn_icsneo_isOnline icsneo_isOnline;
typedef bool(*fn_icsneoEnableMessagePolling)(const neodevice_t* device); typedef bool(*fn_icsneo_enableMessagePolling)(const neodevice_t* device);
fn_icsneoEnableMessagePolling icsneoEnableMessagePolling; fn_icsneo_enableMessagePolling icsneo_enableMessagePolling;
typedef bool(*fn_icsneoDisableMessagePolling)(const neodevice_t* device); typedef bool(*fn_icsneo_disableMessagePolling)(const neodevice_t* device);
fn_icsneoDisableMessagePolling icsneoDisableMessagePolling; fn_icsneo_disableMessagePolling icsneo_disableMessagePolling;
typedef bool(*fn_icsneoGetMessages)(const neodevice_t* device, neomessage_t* messages, size_t* items); typedef bool(*fn_icsneo_getMessages)(const neodevice_t* device, neomessage_t* messages, size_t* items);
fn_icsneoGetMessages icsneoGetMessages; fn_icsneo_getMessages icsneo_getMessages;
typedef size_t(*fn_icsneoGetPollingMessageLimit)(const neodevice_t* device); typedef size_t(*fn_icsneo_getPollingMessageLimit)(const neodevice_t* device);
fn_icsneoGetPollingMessageLimit icsneoGetPollingMessageLimit; fn_icsneo_getPollingMessageLimit icsneo_getPollingMessageLimit;
typedef bool(*fn_icsneoSetPollingMessageLimit)(const neodevice_t* device, size_t newLimit); typedef bool(*fn_icsneo_setPollingMessageLimit)(const neodevice_t* device, size_t newLimit);
fn_icsneoSetPollingMessageLimit icsneoSetPollingMessageLimit; fn_icsneo_setPollingMessageLimit icsneo_setPollingMessageLimit;
#define ICSNEO_IMPORT(func) func = (fn_##func)icsneoDynamicLibraryGetFunction(icsneoLibraryHandle, #func) #define ICSNEO_IMPORT(func) func = (fn_##func)icsneo_dynamicLibraryGetFunction(icsneo_libraryHandle, #func)
#define ICSNEO_IMPORTASSERT(func) if((ICSNEO_IMPORT(func)) == NULL) return 3 #define ICSNEO_IMPORTASSERT(func) if((ICSNEO_IMPORT(func)) == NULL) return 3
void* icsneoLibraryHandle = NULL; void* icsneo_libraryHandle = NULL;
bool icsneoInitialized = false; bool icsneo_initialized = false;
bool icsneoDestroyed = false; bool icsneo_destroyed = false;
int icsneoInit() { int icsneo_init() {
icsneoDestroyed = false; icsneo_destroyed = false;
if(icsneoInitialized) if(icsneo_initialized)
return 1; return 1;
icsneoLibraryHandle = icsneoDynamicLibraryLoad(); icsneo_libraryHandle = icsneo_dynamicLibraryLoad();
if(icsneoLibraryHandle == NULL) if(icsneo_libraryHandle == NULL)
return 2; return 2;
ICSNEO_IMPORTASSERT(icsneoFindAllDevices); ICSNEO_IMPORTASSERT(icsneo_findAllDevices);
ICSNEO_IMPORTASSERT(icsneoFreeUnconnectedDevices); ICSNEO_IMPORTASSERT(icsneo_freeUnconnectedDevices);
ICSNEO_IMPORTASSERT(icsneoSerialNumToString); ICSNEO_IMPORTASSERT(icsneo_serialNumToString);
ICSNEO_IMPORTASSERT(icsneoSerialStringToNum); ICSNEO_IMPORTASSERT(icsneo_serialStringToNum);
ICSNEO_IMPORTASSERT(icsneoIsValidNeoDevice); ICSNEO_IMPORTASSERT(icsneo_isValidNeoDevice);
ICSNEO_IMPORTASSERT(icsneoOpenDevice); ICSNEO_IMPORTASSERT(icsneo_openDevice);
ICSNEO_IMPORTASSERT(icsneoCloseDevice); ICSNEO_IMPORTASSERT(icsneo_closeDevice);
ICSNEO_IMPORTASSERT(icsneoGoOnline); ICSNEO_IMPORTASSERT(icsneo_goOnline);
ICSNEO_IMPORTASSERT(icsneoGoOffline); ICSNEO_IMPORTASSERT(icsneo_goOffline);
ICSNEO_IMPORTASSERT(icsneoIsOnline); ICSNEO_IMPORTASSERT(icsneo_isOnline);
ICSNEO_IMPORTASSERT(icsneoEnableMessagePolling); ICSNEO_IMPORTASSERT(icsneo_enableMessagePolling);
ICSNEO_IMPORTASSERT(icsneoDisableMessagePolling); ICSNEO_IMPORTASSERT(icsneo_disableMessagePolling);
ICSNEO_IMPORTASSERT(icsneoGetMessages); ICSNEO_IMPORTASSERT(icsneo_getMessages);
ICSNEO_IMPORTASSERT(icsneoGetPollingMessageLimit); ICSNEO_IMPORTASSERT(icsneo_getPollingMessageLimit);
ICSNEO_IMPORTASSERT(icsneoSetPollingMessageLimit); ICSNEO_IMPORTASSERT(icsneo_setPollingMessageLimit);
icsneoInitialized = true; icsneo_initialized = true;
return 0; return 0;
} }
bool icsneoClose() ICSNEO_DESTRUCTOR { bool icsneo_close() ICSNEO_DESTRUCTOR {
icsneoInitialized = false; icsneo_initialized = false;
if(icsneoDestroyed) if(icsneo_destroyed)
return true; return true;
return icsneoDestroyed = icsneoDynamicLibraryClose(icsneoLibraryHandle); return icsneo_destroyed = icsneo_dynamicLibraryClose(icsneo_libraryHandle);
} }
#endif // ICSNEOC_DYNAMICLOAD #endif // ICSNEOC_DYNAMICLOAD

View File

@ -1,6 +1,6 @@
#ifndef __DYNAMICLIB_DARWIN_H_ #ifndef __DYNAMICLIB_DARWIN_H_
#define __DYNAMICLIB_DARWIN_H_ #define __DYNAMICLIB_DARWIN_H_
#define icsneoDynamicLibraryLoad() dlopen("/home/paulywog/Code/icsneonext/build/libicsneoc.dylib", RTLD_LAZY) #define icsneo_dynamicLibraryLoad() dlopen("/home/paulywog/Code/icsneonext/build/libicsneoc.dylib", RTLD_LAZY)
#endif #endif

View File

@ -18,7 +18,7 @@
#define ICSNEO_DESTRUCTOR #define ICSNEO_DESTRUCTOR
// #endif // #endif
#define icsneoDynamicLibraryGetFunction(handle, func) dlsym(handle, func) #define icsneo_dynamicLibraryGetFunction(handle, func) dlsym(handle, func)
#define icsneoDynamicLibraryClose(handle) (dlclose(handle) == 0) #define icsneo_dynamicLibraryClose(handle) (dlclose(handle) == 0)
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef __DYNAMICLIB_LINUX_H_ #ifndef __DYNAMICLIB_LINUX_H_
#define __DYNAMICLIB_LINUX_H_ #define __DYNAMICLIB_LINUX_H_
#define icsneoDynamicLibraryLoad() dlopen("/home/paulywog/Code/icsneonext/build/libicsneoc.so", RTLD_LAZY) #define icsneo_dynamicLibraryLoad() dlopen("/home/paulywog/Code/icsneonext/build/libicsneoc.so", RTLD_LAZY)
#endif #endif

View File

@ -12,8 +12,8 @@
// MSVC does not have the ability to specify a destructor // MSVC does not have the ability to specify a destructor
#define ICSNEO_DESTRUCTOR #define ICSNEO_DESTRUCTOR
#define icsneoDynamicLibraryLoad() LoadLibrary(L"C:\\Users\\Phollinsky\\Code\\icsneonext\\build\\icsneoc.dll") #define icsneo_dynamicLibraryLoad() LoadLibrary(L"C:\\Users\\Phollinsky\\Code\\icsneonext\\build\\icsneoc.dll")
#define icsneoDynamicLibraryGetFunction(handle, func) GetProcAddress((HMODULE) handle, func) #define icsneo_dynamicLibraryGetFunction(handle, func) GetProcAddress((HMODULE) handle, func)
#define icsneoDynamicLibraryClose(handle) FreeLibrary((HMODULE) handle) #define icsneo_dynamicLibraryClose(handle) FreeLibrary((HMODULE) handle)
#endif #endif