Emulate a Hexabus device using libhexabus

paalsteek edited this page May 21, 2014 · 1 revision

Libhexabus provides you with an abstraction class to make the emulation of hexabus devices on a linux machine simple.

Example

A hello world example could look like this:

#include <iostream>
#include <boost/asio/io_service.hpp>
#include <libhexabus/device.hpp>

boost::asio::io_service io_service;
std::string myDeviceName = "example";

std::string readDeviceName() {
	return myDeviceName;
}

void writeDeviceName(const std::string& name) {
	myDeviceName = name;
}

std::string helloWorld() {
	return "Hello, World!";
}

int main() {
	hexabus::Device device(io_service, "eth1", "bbbb::1");

	device.onReadName(&readDeviceName);
	device.onWriteName(&writeDeviceName);

	hexabus::EndpointRegistry ep_registry;
	hexabus::EndpointRegistry::const_iterator ep_it;

	ep_it = ep_registry.find(90);
	hexabus::TypedEndpointFunctions<std::string>::Ptr helloWorldEP = ep_it != ep_registry.end()
		? hexabus::TypedEndpointFunctions<std::string>::fromEndpointDescriptor(ep_it->second)
		: hexabus::TypedEndpointFunctions<std::string>::Ptr(new hexabus::TypedEndpointFunctions<std::string>(90, "Hello World!"));
	helloWorldEP->onRead(&helloWorld);
	device.addEndpoint(helloWorldEP);

	io_service.run();
}
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.