Skip to content

Commit

Permalink
wip, working examples, TODO: clean code, add documentation and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei-Fabian-Pop <[email protected]>
  • Loading branch information
Andrei-Fabian-Pop committed Mar 11, 2024
1 parent 5e3920e commit efe8355
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 232 deletions.
2 changes: 1 addition & 1 deletion examples/analog/analog_in_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ int main(int argc, char* argv[])
{
cout<<val<<endl;
}
cout <<"end";
cout <<"end\n";
aout->stop();
contextClose(ctx);
}
3 changes: 3 additions & 0 deletions include/libm2k/context_private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ class LIBM2K_API ContextPrivate {
*/
virtual void setTimeout(unsigned int timeout) = 0;


// TODO: add brief
virtual int getRefCount() = 0;
};
}
}
Expand Down
18 changes: 13 additions & 5 deletions include/libm2k/contextbuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#ifndef DEVICEBUILDER_HPP
#define DEVICEBUILDER_HPP

#include "context_private.hpp"
#include "context.hpp"

#include <vector>
#include <string>
#include <libm2k/m2kglobal.hpp>
Expand Down Expand Up @@ -113,11 +116,16 @@ class LIBM2K_API ContextBuilder {
bool ownsContext = false);
static bool m_disable_logging;

static std::map<std::string, int> reference_count;
static void incrementReferenceCount(std::string uri);
static void decrementReferenceCount(std::string uri);
static bool checkLastReference(std::string uri);
static Context* searchInConnectedDevices(std::string uri);
// static std::map<std::string, int> reference_count;

// new maps
static std::map<std::string, ContextPrivate*> m_connectedDevices;
static std::vector<Context*> m_sessions;

// static void incrementReferenceCount(std::string uri);
// static void decrementReferenceCount(std::string uri);
// static bool checkLastReference(std::string uri);
static ContextPrivate* searchInConnectedDevices(std::string uri);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/analog/m2kanalogin_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,4 @@ void M2kAnalogInImpl::setCalibrateHDL(std::string calibrate_val)
std::string M2kAnalogInImpl::getCalibrateHDL()
{
return m_m2k_adc->getStringValue("calibrate");
}
}
19 changes: 16 additions & 3 deletions src/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ ContextImpl::ContextImpl(ContextImplPrivate *p)
{
m_contextPrivate = p;
m_contextPrivate->m_refCount++;
std::cout << "refcount++ " << m_contextPrivate->getRefCount() << " " << m_contextPrivate->getUri() << "\n";
}

ContextImpl::~ContextImpl()
{
m_contextPrivate->m_refCount--;
std::cout << "refcount-- " << m_contextPrivate->getRefCount() << " " << m_contextPrivate->getUri() << "\n";
}

void ContextImpl::reset()
Expand Down Expand Up @@ -129,12 +131,23 @@ std::string ContextImpl::getSerialNumber()

M2k* ContextImpl::toM2k()
{
return m_contextPrivate->toM2k();
libm2k::context::M2k* dev = dynamic_cast<M2k*>(this);
if(dev) {
dev->calibrateADC();
return dev;
} else {
return nullptr;
}
}

Generic *ContextImpl::toGeneric()
{
return m_contextPrivate->toGeneric();
libm2k::context::Generic* dev = dynamic_cast<Generic*>(this);
if(dev) {
return dev;
} else {
return nullptr;
}
}

std::string ContextImpl::getUri()
Expand Down Expand Up @@ -164,5 +177,5 @@ void ContextImpl::setTimeout(unsigned int timeout)

void ContextImpl::setContextOwnership(bool ownsContext)
{
m_contextPrivate->setTimeout(ownsContext);
m_contextPrivate->setContextOwnership(ownsContext);
}
1 change: 0 additions & 1 deletion src/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <vector>
#include <memory>
#include <map>
// #include "libm2k/context_private.hpp"

extern "C" {
struct iio_context;
Expand Down
7 changes: 6 additions & 1 deletion src/context_impl_private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,10 @@ void ContextImplPrivate::setTimeout(unsigned int timeout)

void ContextImplPrivate::setContextOwnership(bool ownsContext)
{
m_ownsContext = ownsContext;
m_ownsContext = ownsContext;
}

int ContextImplPrivate::getRefCount()
{
return m_refCount;
}
1 change: 1 addition & 0 deletions src/context_impl_private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class ContextImplPrivate : public virtual ContextPrivate {
struct iio_context *getIioContext() override;
void setTimeout(unsigned int timeout) override;
void setContextOwnership(bool ownsContext);
int getRefCount() override;

protected:
struct iio_context *m_context;
Expand Down
150 changes: 88 additions & 62 deletions src/contextbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
using namespace libm2k::context;
using namespace libm2k::utils;

std::vector<Context*> ContextBuilder::s_connectedDevices = {};
// std::vector<Context*> ContextBuilder::s_connectedDevices = {};
std::map<ContextTypes, std::vector<std::string>> ContextBuilder::m_dev_map = {
{ContextTypes::CtxFMCOMMS, {"cf-ad9361-lpc", "cf-ad9361-dds-core-lpc", "ad9361-phy"}},
{ContextTypes::CtxM2K, {"m2k-adc", "m2k-dac-a",
Expand All @@ -51,8 +51,9 @@ std::map<ContextTypes, std::string> ContextBuilder::m_dev_name_map = {
{Other, "Generic"}
};

bool ContextBuilder::m_disable_logging = true;
std::map<std::string, int> ContextBuilder::reference_count = {};
bool ContextBuilder::m_disable_logging = false;
std::map<std::string, ContextPrivate*> ContextBuilder::m_connectedDevices = {};
std::vector<Context*> ContextBuilder::m_sessions = {};

ContextBuilder::ContextBuilder()
{
Expand Down Expand Up @@ -135,10 +136,10 @@ Context* ContextBuilder::buildContext(ContextTypes type, std::string uri,
switch (type) {
case CtxM2K:
{
// TODO: add logic
auto m2kPrivate = new M2kImplPrivate(uri, ctx, name, sync);
auto m2k = new M2kImpl(m2kPrivate);
m2k->setContextOwnership(ownsContext);
m_connectedDevices.insert({uri, m2kPrivate});
return m2k;
}
case Other:
Expand All @@ -147,57 +148,62 @@ Context* ContextBuilder::buildContext(ContextTypes type, std::string uri,
auto genericPrivate = new ContextImplPrivate(uri, ctx, name, sync);
auto generic = new ContextImpl(genericPrivate);
generic->setContextOwnership(ownsContext);
m_connectedDevices.insert({uri, genericPrivate});
return generic;
}
}
}

void ContextBuilder::incrementReferenceCount(std::string uri)
{
if (reference_count.find(uri) != reference_count.end()) {
reference_count[uri]++;
} else {
reference_count.emplace(uri, 1);
}
}

void ContextBuilder::decrementReferenceCount(std::string uri)
{
if (reference_count.find(uri) != reference_count.end()) {
reference_count[uri]--;
}
}

bool ContextBuilder::checkLastReference(std::string uri)
{
if (reference_count.find(uri) != reference_count.end()) {
return (reference_count[uri] == 0);
}
return false;
}

Context* ContextBuilder::searchInConnectedDevices(std::string uri)
{
for (auto dev : s_connectedDevices) {
if (dev->getUri() == uri) {
return dev;
// void ContextBuilder::incrementReferenceCount(std::string uri)
// {
// if (reference_count.find(uri) != reference_count.end()) {
// reference_count[uri]++;
// } else {
// reference_count.emplace(uri, 1);
// }
// }

// void ContextBuilder::decrementReferenceCount(std::string uri)
// {
// if (reference_count.find(uri) != reference_count.end()) {
// reference_count[uri]--;
// }
// }

// bool ContextBuilder::checkLastReference(std::string uri)
// {
// if (reference_count.find(uri) != reference_count.end()) {
// return (reference_count[uri] == 0);
// }
// return false;
// }

ContextPrivate* ContextBuilder::searchInConnectedDevices(std::string uri)
{
for (auto dev : m_connectedDevices) {
if (dev.second->getUri() == uri) {
return dev.second;
}
}
return nullptr;
}

Context* ContextBuilder::contextOpen(const char *uri)
{
Context* dev = nullptr;
ContextPrivate* dev = nullptr;
if (m_disable_logging) {
enableLogging(false);
}
LIBM2K_LOG(INFO, "libm2k version: " + getVersion());
// use saved device is possible
dev = searchInConnectedDevices(uri);
if (dev) {
incrementReferenceCount(uri);
return dev;
// incrementReferenceCount(uri);
// FIXME: ugly, fix this, this should not be a cast
auto context = new ContextImpl(dynamic_cast<ContextImplPrivate *>(dev));
m_sessions.push_back(context);
return context;
// return dev;
}
// create and save device during first call
struct iio_context* ctx = iio_create_context_from_uri(uri);
Expand All @@ -221,16 +227,16 @@ Context* ContextBuilder::contextOpen(const char *uri)

ContextTypes dev_type = ContextBuilder::identifyContext(ctx);

dev = buildContext(dev_type, std::string(uri), ctx, true, true);
s_connectedDevices.push_back(dev);
incrementReferenceCount(uri);
auto context = buildContext(dev_type, std::string(uri), ctx, true, true);
m_sessions.push_back(context);
// incrementReferenceCount(uri);

return dev;
return context;
}

Context* ContextBuilder::contextOpen(struct iio_context* ctx, const char* uri)
{
Context* dev = nullptr;
ContextPrivate* dev = nullptr;
if (m_disable_logging) {
enableLogging(false);
}
Expand All @@ -251,11 +257,13 @@ Context* ContextBuilder::contextOpen(struct iio_context* ctx, const char* uri)
if (hw_fw_version != nullptr) {
LIBM2K_LOG(INFO, "Firmware version: " + std::string(hw_fw_version));
}
// use saved device is possible
// use saved device is possiblede
dev = searchInConnectedDevices(uri);
if (dev) {
incrementReferenceCount(uri);
return dev;
// incrementReferenceCount(uri);
auto context = new ContextImpl(dynamic_cast<ContextImplPrivate *>(dev));
m_sessions.push_back(context);
return context;
}

if (!ctx) {
Expand All @@ -264,11 +272,11 @@ Context* ContextBuilder::contextOpen(struct iio_context* ctx, const char* uri)
// create and save device during first call
ContextTypes dev_type = ContextBuilder::identifyContext(ctx);

dev = buildContext(dev_type, std::string(uri), ctx, true);
s_connectedDevices.push_back(dev);
incrementReferenceCount(uri);
auto context = buildContext(dev_type, std::string(uri), ctx, true);
m_sessions.push_back(context);
// incrementReferenceCount(uri);

return dev;
return context;
}

/* Connect to the first usb device that was found
Expand Down Expand Up @@ -315,12 +323,12 @@ M2k *ContextBuilder::m2kOpen(const char *uri)

M2k *ContextBuilder::m2kOpen()
{
auto dev = contextOpen();
Context *dev = contextOpen();
if (!dev) {
return nullptr;
}

auto m2k = dev->toM2k();
M2k *m2k = dev->toM2k();
if (m2k) {
return m2k;
}
Expand All @@ -331,34 +339,52 @@ M2k *ContextBuilder::m2kOpen()
void ContextBuilder::contextClose(Context* device, bool deinit)
{
auto uri = device->getUri();
if (searchInConnectedDevices(uri) == nullptr) {
// if (searchInConnectedDevices(uri) == nullptr) {
// return;
// }

if (std::find(m_sessions.begin(), m_sessions.end(), device) == m_sessions.end()) {
return;
}
decrementReferenceCount(uri);
bool isLastReference = checkLastReference(uri);

m_sessions.erase(std::remove(m_sessions.begin(),
m_sessions.end(),
device), m_sessions.end());

delete device;

// decrementReferenceCount(uri);
bool isLastReference = false;
auto privateDev = m_connectedDevices.at(uri);
if (privateDev->getRefCount() == 0) {
isLastReference = true;
}

if (!isLastReference) {
return;
}

reference_count.erase(uri);
s_connectedDevices.erase(std::remove(s_connectedDevices.begin(),
s_connectedDevices.end(),
device), s_connectedDevices.end());

// reference_count.erase(uri);
// s_connectedDevices.erase(std::remove(s_connectedDevices.begin(),
// s_connectedDevices.end(),
// device), s_connectedDevices.end());
try {
if (deinit) {
device->deinitialize();
privateDev->deinitialize();
}
} catch (std::exception &e ){
delete device;
delete privateDev;
THROW_M2K_EXCEPTION("Context deinit: " + std::string(e.what()), libm2k::EXC_RUNTIME_ERROR);
}
delete device;
m_connectedDevices.erase(uri);
delete privateDev;
}

void ContextBuilder::contextCloseAll()
{
while (s_connectedDevices.size() > 0) {
contextClose(s_connectedDevices.at(0));
while (m_sessions.size() > 0) {
contextClose(m_sessions.at(0));
}
}

Expand Down
Loading

0 comments on commit efe8355

Please sign in to comment.