how to send packet back? - c++

I'm writing a new Agent which can be used in one host connected with at least four node.
And the host with traffic generator will send packet to client,after a delay
it will be sended to host again. And the information calculation will be in host.
However, in my test.tcl (only two nodes) the packet receive in client seems didn't
send back to host.
Could somebody give me a hand?
Did the send function any obvious wrong?
Should I assign the ip header or the target_ or anything variables to send back my packet?
AgentCPU.h
#ifndef ns_cpu_h
#define ns_cpu_h
#include <tclcl.h>
#include "agent.h"
#include "config.h"
#include "packet.h"
#include "trafgen.h"
#define SAMPLERATE 8000
struct hdr_task {
int total_time;
int size_;
int number;
int& size() { return size_;}
static int offset_;
inline static int& offset() { return offset_; }
inline static hdr_task* access (const Packet* p) {
return (hdr_task*) p->access(offset_);
}
};
class AgentCPU : public Agent {
public:
AgentCPU();
virtual void recv(Packet *, Handler *);
virtual void sendmsg(int nbytes, const char *flags = 0);
private:
double busy_time_;
float execute_time_;
float execute_time_exp;
double record_packet_Time_;
int npkts_;
};
#endif
AgentCPU.cc
#include "AgentCPU.h"
#include "packet.h"
#include "random.h"
#include "ip.h"
int hdr_task::offset_;
static class TASKHeaderClass : public PacketHeaderClass {
public:
TASKHeaderClass()
:PacketHeaderClass("PacketHeader/TASKHEAD",sizeof(hdr_task)) {
bind_offset(&hdr_task::offset_);
}
} class_task_hdr;
static class CPUClass : public TclClass {
public:
CPUClass() : TclClass("Agent/AgentCPU") {}
TclObject* create(int, const char*const*) {
return (new AgentCPU());
}
} class_cpu_agent;
AgentCPU::AgentCPU() : Agent(PT_TASK)
{
npkts_=0;
record_packet_Time_=0.0;
bind("packetSize_",&size_);
bind("record_packet_Time_",&record_packet_Time_);
bind("npkt_",&npkts_);
}
void AgentCPU::sendmsg(int nbytes, const char* flags)
{
Packet *pkt =allocpkt();
test++;
hdr_cmn *ch = hdr_cmn::access(pkt);
hdr_ip *iph= hdr_ip::access(pkt);
hdr_task *task= hdr_task::access(pkt);
task->number=Random::exponential(5000);
task->state=0;
task->total_time=0;
target_->recv(pkt);
idle();
}
void AgentCPU::recv(Packet* pkt, Handler*)
{
hdr_task *task= hdr_task::access(pkt);
if(task->state==1)
{
if(task->number >2500)
{
task -> total_time += 2500;
task -> number -=2500;
task -> state=0;
// printf("exe 2500 and the packet exe number is %d\n",task->number);
target_->recv(pkt, (Handler*)0);
}
else
{
task ->total_time += task->number ;
task ->number =0;
++npkts_;
record_packet_Time_+= task ->total_time;
Tcl& tcl =Tcl::instance();
Packet::free(pkt);
}
}
else if (task->state==0)
{
int hold_time =Random::exponential(2000);
task->total_time += hold_time;
task->state=1;
Scheduler& s =Scheduler::instance();
double delay = Random::exponential(0.2);
s.schedule(target_,pkt,delay);
}
}
I had tried to modyfied the saddr() and daddr() field in ip header
when I traced the recv packet two field , and the value is 0 and 1
I think they are represent the two node.
So I exchange the two value so the src address and the dest address will exchange
and the packet will be send back to host.
But it results in Seg fault ..

I have solved this problem
I added ip header for the packet I received
hdr_ip* ip=hdr_ip::access(pkt);
ip.daddr()=1; // this int represent the node in your tcl script
However, I have another problem
It will crash when use my tcl script
after use valgrind , the program which can help you detect the memory leak
I know how stupid this question is ,
I guess is the trace format cause this error
ie. I didnt impelment the correct "Trace::format" for my new Packet type
And I didnt comment the trace file code in my tcl script like this
set nf [open out.nam w]
$nf nametrace-all $nf
...
...
And when I comment these code (include the procedure "finish")
All things work! The packet can send to specified node And no Segment Fault
Hope this can help someone who has the same question like me

Related

Integration between Node.js and C++

I have a Node.js application that I want to be able to send a JSON-object into a C++ application.
The C++ application will use the Poco-libraries (pocoproject.org).
I want the interaction to be lighting fast, so preferably no files or network-sockets.
I have been looking into these areas:
Pipes
Shared memory
unixSockets
What should I focus on, and can someone point my direction to docs. and samples?
First of all, some more data is needed to give good advice.
In general shared memory is the fastest, since there's no transfer required, but it's also the hardest to keep fine. I'm not sure you'd be able to do that with Node though.
If this program is just running for this one task and closing it might be worth just sending your JSON to the CPP program as a startup param
myCPPProgram.exe "JsonDataHere"
The simplest thing with decent performance should be a socket connection using Unix domain sockets with some low-overhead data frame format. E.g., two-byte length followed by UTF-8 encoded JSON. On the C++ side this should be easy to implement using the Poco::Net::TCPServer framework. Depending on where your application will go in the future you may run into limits of this format, but if it's basically just streaming JSON objects it should be fine.
To make it even simpler, you can use a WebSocket, which will take care of the framing for you, at the cost of the overhead for the initial connection setup (HTTP upgrade request). May even be possible to run the WebSocket protocol over a Unix domain socket.
However, the performance difference between a (localhost only) TCP socket and a Unix domain socket may not even be significant, given all the JavaScript/node.js overhead. Also, if performance is really a concern, JSON may not even be the right serialization format to begin with.
Anyway, without more detailed information (size of JSON data, message frequency) it's hard to give a definite recommendation.
I created a TCPServer, which seems to work. However if I close the server and start it again I get this error:
Net Exception: Address already in use: /tmp/app.SocketTest
Is it not possible to re-attach to the socket if it exists?
Here is the code for the TCPServer:
#include "Poco/Util/ServerApplication.h"
#include "Poco/Net/TCPServer.h"
#include "Poco/Net/TCPServerConnection.h"
#include "Poco/Net/TCPServerConnectionFactory.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/File.h"
#include <fstream>
#include <iostream>
using Poco::Net::ServerSocket;
using Poco::Net::StreamSocket;
using Poco::Net::TCPServer;
using Poco::Net::TCPServerConnection;
using Poco::Net::TCPServerConnectionFactory;
using Poco::Net::SocketAddress;
using Poco::Util::ServerApplication;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
class UnixSocketServerConnection: public TCPServerConnection
/// This class handles all client connections.
{
public:
UnixSocketServerConnection(const StreamSocket& s):
TCPServerConnection(s)
{
}
void run()
{
try
{
/*char buffer[1024];
int n = 1;
while (n > 0)
{
n = socket().receiveBytes(buffer, sizeof(buffer));
EchoBack(buffer);
}*/
std::string message;
char buffer[1024];
int n = 1;
while (n > 0)
{
n = socket().receiveBytes(buffer, sizeof(buffer));
buffer[n] = '\0';
message += buffer;
if(sizeof(buffer) > n && message != "")
{
EchoBack(message);
message = "";
}
}
}
catch (Poco::Exception& exc)
{
std::cerr << "Error: " << exc.displayText() << std::endl;
}
std::cout << "Disconnected." << std::endl;
}
private:
inline void EchoBack(std::string message)
{
std::cout << "Message: " << message << std::endl;
socket().sendBytes(message.data(), message.length());
}
};
class UnixSocketServerConnectionFactory: public TCPServerConnectionFactory
/// A factory
{
public:
UnixSocketServerConnectionFactory()
{
}
TCPServerConnection* createConnection(const StreamSocket& socket)
{
std::cout << "Got new connection." << std::endl;
return new UnixSocketServerConnection(socket);
}
private:
};
class UnixSocketServer: public Poco::Util::ServerApplication
/// The main application class.
{
public:
UnixSocketServer(): _helpRequested(false)
{
}
~UnixSocketServer()
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
ServerApplication::initialize(self);
}
void uninitialize()
{
ServerApplication::uninitialize();
}
void defineOptions(OptionSet& options)
{
ServerApplication::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false));
}
void handleOption(const std::string& name, const std::string& value)
{
ServerApplication::handleOption(name, value);
if (name == "help")
_helpRequested = true;
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A server application to test unix domain sockets.");
helpFormatter.format(std::cout);
}
int main(const std::vector<std::string>& args)
{
if (_helpRequested)
{
displayHelp();
}
else
{
// set-up unix domain socket
Poco::File socketFile("/tmp/app.SocketTest");
SocketAddress unixSocket(SocketAddress::UNIX_LOCAL, socketFile.path());
// set-up a server socket
ServerSocket svs(unixSocket);
// set-up a TCPServer instance
TCPServer srv(new UnixSocketServerConnectionFactory, svs);
// start the TCPServer
srv.start();
// wait for CTRL-C or kill
waitForTerminationRequest();
// Stop the TCPServer
srv.stop();
}
return Application::EXIT_OK;
}
private:
bool _helpRequested;
};
int main(int argc, char **argv) {
UnixSocketServer app;
return app.run(argc, argv);
}
The solution I have gone for, is to use unix domain sockets. The solution will run on a Raspbian-setup and the socket-file is placed in /dev/shm, which is mounted into RAM.
On the C++ side, I use the Poco::Net::TCPServer framework as described elsewhere in this post.
On the Node.js side, I use the node-ipc module (http://riaevangelist.github.io/node-ipc/).

no matching function for call unresolved overloaded function type

First I'm a newbie to C++ so my question might be already answered somewhere but I couldn't find a straightforward answer to it.
I'm creating a simple library for my hardware. I'm using a Scheduler library which is working fine on Arduino IDE (here is the example), but when I compile the code with my own IDE (Atom+PlatformIO) this error comes up:
lib\SRF08\SRF08.cpp:43:30: error: no matching function for call to 'SchedulerClass::startLoop(<unresolved overloaded functi
on type>)'
I removed some of the codes but if you need the rest I can put it.
SRF08.h
#ifndef SRF08_h
#define SRF08_h
#include "Arduino.h"
class SRF08
{
public:
//main constructor
SRF08(uint8_t address=address_1);
// init the sensor
void begin(void);
//change sensor address from oldAddress to newAddress
void changeAddress(uint16_t oldAddress, uint16_t newAddress);
// scan for a single sensor address
int8_t scanner(void);
// scan for multiple sensors and return the table of addresses
struct table_value scan_all(void);
uint16_t output_value;
void read(void);
private:
// the main I2C address of Sensor
uint16_t _address;
//read sansor value base on centimiter
};
#endif
SRF08.cpp
#include "Wire.h"
#include "SRF08.h"
// Include Scheduler since we want to manage multiple tasks.
#include "Scheduler.h"
SRF08::SRF08(uint8_t address)
{
//main constructor, address is the sensor address if u dont know it try scanner first
//address must be an integer number between 1 to 9
if (address == 1) _address = address_1;
else _address = address_1;
}
void SRF08::begin(){
//initilize I2C
Wire.begin();
output_value = 0;
Scheduler.startLoop(SRF08::read); //here is my error
}
void SRF08::read(){
int reading = 0;
// step 1: instruct sensor to read echoes
Wire.beginTransmission(_address); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x51)); // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(70); // datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(_address); // transmit to device #112
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(_address, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor
if (2 <= Wire.available()) { // if two bytes were received
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
output_value = reading; // print the reading
}
//yield();
}
Scheduler.h
#ifndef _SCHEDULER_H_
#define _SCHEDULER_H_
#include <Arduino.h>
extern "C" {
typedef void (*SchedulerTask)(void);
typedef void (*SchedulerParametricTask)(void *);
}
class SchedulerClass {
public:
SchedulerClass();
static void startLoop(SchedulerTask task, uint32_t stackSize = 1024);
static void start(SchedulerTask task, uint32_t stackSize = 1024);
static void start(SchedulerParametricTask task, void *data, uint32_t stackSize = 1024);
static void yield() { ::yield(); };
};
extern SchedulerClass Scheduler;
#endif
Scheduler.cpp
#include "Scheduler.h"
extern "C" {
#define NUM_REGS 10 // r4-r11, sp, pc
typedef struct CoopTask {
uint32_t regs[NUM_REGS];
void* stackPtr;
struct CoopTask* next;
struct CoopTask* prev;
} CoopTask;
static CoopTask *cur = 0;
...
void yield(void) {
coopDoYield(cur);
}
}; // extern "C"
SchedulerClass::SchedulerClass() {
coopInit();
}
static void startLoopHelper(void *taskData) {
SchedulerTask task = reinterpret_cast<SchedulerTask>(taskData);
while (true)
task();
}
void SchedulerClass::startLoop(SchedulerTask task, uint32_t stackSize) {
coopSpawn(startLoopHelper, reinterpret_cast<void *>(task), stackSize);
}
static void startTaskHelper(void *taskData) {
SchedulerTask task = reinterpret_cast<SchedulerTask>(taskData);
task();
}
void SchedulerClass::start(SchedulerTask task, uint32_t stackSize) {
coopSpawn(startTaskHelper, reinterpret_cast<void *>(task), stackSize);
}
void SchedulerClass::start(SchedulerParametricTask task, void *taskData, uint32_t stackSize) {
coopSpawn(task, taskData, stackSize);
}
SchedulerClass Scheduler;
Thanks to #Someprogrammerdude to help. I needed to declare the read function as static.
SRF08.h
#ifndef SRF08_h
#define SRF08_h
#include "Arduino.h"
class SRF08
{
public:
//main constructor
SRF08(uint8_t address=address_1);
// init the sensor
void begin(void);
//change sensor address from oldAddress to newAddress
void changeAddress(uint16_t oldAddress, uint16_t newAddress);
// scan for a single sensor address
int8_t scanner(void);
// scan for multiple sensors and return the table of addresses
struct table_value scan_all(void);
static uint16_t output_value;
static void read(void);
static uint16_t static_address;
private:
// the main I2C address of Sensor
uint16_t _address;
//read sansor value base on centimiter
};
#endif
SRF08.cpp
#include "Wire.h"
#include "SRF08.h"
// Include Scheduler since we want to manage multiple tasks.
#include "Scheduler.h"
//initilize static members
uint16_t SRF08::output_value;
uint16_t SRF08::static_address;
SRF08::SRF08(uint8_t address)
{
//main constructor, address is the sensor address if u dont know it try scanner first
//address must be an integer number between 1 to 9
if (address == 1) _address = address_1;
else _address = address_1;
static_address = _address;
//begin();
}
void SRF08::begin(){
//initilize I2C
Wire.begin();
output_value = 0;
Scheduler.startLoop(read); //here is my error
}
void SRF08::read(){
int reading = 0;
// step 1: instruct sensor to read echoes
Wire.beginTransmission(static_address); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x51)); // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(70); // datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(static_address); // transmit to device #112
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(static_address, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor
if (2 <= Wire.available()) { // if two bytes were received
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
output_value = reading; // print the reading
//output_value = reading;
}
yield();
}

Program exit is reached in IAR RL78 V2.20

I am working with YRDKRL78 G13 board. I am using IAR v.2.20 for compiler and ı have created a C++ project on it. And I am using one framework. Anyway I have implemented all my codes and which is working correctly but after 2 minutes IAR is giving information as "The Application is aborted" and "Program exit is reached." I am really confuse about it I used very large stack as 512 and near is 1024 far is the 4096.
Here is my main.cpp
#include "System.h"
extern "C"
{
#include "r_cg_macrodriver.h"
}
#pragma location = "OPTBYTE"
__root const uint8_t opbyte0 = 0x7EU;
#pragma location = "OPTBYTE"
__root const uint8_t opbyte1 = 0xFFU;
#pragma location = "OPTBYTE"
__root const uint8_t opbyte2 = 0xE8U;
#pragma location = "OPTBYTE"
__root const uint8_t opbyte3 = 0x85U;
/* Set security ID */
#pragma location = "SECUID"
__root const uint8_t secuid[10] =
{0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U};
void main(void)
{
System::SystemInstance().SYS_vInit();
System::SystemInstance().SYS_vStart();
System::SystemInstance().SYS_vRun();
while(1)
{
;
}
}
And here is the iar output. IAR Output Console
If somebody faced like this problem or if somebody has got any solution or idea could you please share with me here
Thanks for the help :)
EDIT:
#rjp Firstly thanks for response. I am using special framework on my board which Quantum leaps. these 3 functions are calling framwork functions and SYS_vRUN is calling endless loop
int_t QF::run(void) {
onStartup(); // startup callback
// the combined event-loop and background-loop of the QV kernel
for (;;) {
R_WDT_Restart();
RepaintLCD();
delay_ms(50); /* Leave some room for the system to respond */
QF_INT_DISABLE();
if (QV_readySet_.notEmpty()) {
uint_fast8_t p = QV_readySet_.findMax();
QMActive *a = active_[p];
QF_INT_ENABLE();
// perform the run-to-completion (RTS) step...
// 1. retrieve the event from the AO's event queue, which by this
// time must be non-empty and The "Vanialla" kernel asserts it.
// 2. dispatch the event to the AO's state machine.
// 3. determine if event is garbage and collect it if so
//
QEvt const *e = a->get_();
a->dispatch(e);
gc(e);
}
else {
// QV::onIdle() must be called with interrupts DISABLED because
// the determination of the idle condition (no events in the
// queues) can change at any time by an interrupt posting events
// to a queue. QV::onIdle() MUST enable interrupts internally,
// perhaps at the same time as putting the CPU into a power-saving
// mode.
QP::QV::onIdle();
}
}
}
End I searched all code for exit() function there is no any. But you mentioned another issue about assertion. here you can see assertion macro and custome implemented assertion function.
#define Q_ASSERT_ID(id_, test_) ((test_) \
? (void)0 : Q_onAssert(&Q_this_module_[0], (int_t)(id_)))
Function;
void Q_onAssert(char const Q_ROM * const file, int line) {
// implement the error-handling policy for your application!!!
QF_INT_DISABLE(); // disable all interrupts
// cause the reset of the CPU...
//WDTCTL = WDTPW | WDTHOLD;
//__asm(" push &0xFFFE");
// return from function does the reset
}
EDIT2:
Most dynamic memory processes are done in the LedFactory Class.
Header
/*
* LedFactory.h
*
* Created on: Aug 3, 2016
* Author: Dev
*/
#ifndef APPLICATION_LED_LEDFACTORY_H_
#define APPLICATION_LED_LEDFACTORY_H_
#include "LedController.h"
class LedFactory {
public:
typedef enum{
LED1,
LED2,
LED3,
LED4,
LED5,
LED6,
}LedTypes;
public:
LedFactory();
virtual ~LedFactory();
LedController * FirstLedFactory(LedTypes ledtype);
LedController * SecondLedFactory(LedTypes ledtype);
LedController * ThirdLedFactory(LedTypes ledtype);
LedController * FourthLedFactory(LedTypes ledtype);
LedController * FifthLedFactory(LedTypes ledtype);
LedController * SixthLedFactory(LedTypes ledtype);
public:
static LedFactory& instance();
};
#endif /* APPLICATION_LED_LEDFACTORY_H_ */
Source file.
/*
* LedFactory.cpp
*
* Created on: Aug 3, 2016
* Author: Dev
*/
#include <LedFactory.h>
#include "FirstLed.h"
#include "SecondLed.h"
#include "ThirdLed.h"
#include "FourthLed.h"
#include "FifthLed.h"
#include "SixthLed.h"
LedFactory::LedFactory() {
// TODO Auto-generated constructor stub
}
LedFactory::~LedFactory() {
// TODO Auto-generated destructor stub
}
LedFactory& LedFactory::instance()
{
static LedFactory instance;
return instance;
}
LedController * LedFactory::FirstLedFactory(LedTypes ledtype)
{
if(ledtype == (LedTypes)LED1)
{
return new FirstLed;
}
return NULL;
}
LedController * LedFactory::SecondLedFactory(LedTypes ledtype)
{
if(ledtype == (LedTypes)LED2)
return new SecondLed;
return NULL;
}
LedController * LedFactory::ThirdLedFactory(LedTypes ledtype)
{
if(ledtype == (LedTypes)LED3)
{
return new ThirdLed;
}
return NULL;
}
LedController * LedFactory::FourthLedFactory(LedTypes ledtype)
{
if(ledtype == (LedTypes)LED4)
{
return new FourthLed;
}
return NULL;
}
LedController * LedFactory::FifthLedFactory(LedTypes ledtype)
{
if(ledtype == (LedTypes)LED5)
{
return new FifthLed;
}
return NULL;
}
LedController * LedFactory::SixthLedFactory(LedTypes ledtype)
{
if(ledtype ==(LedTypes)LED6)
{
return new SixthLed;
}
return NULL;
}
Should I remove that class for increase the dynamic allocation memory issue ?
Or how can I fix this class ?
Problem has been solved. Main reason was the dynamic memory allocation processes which is used at the factory class.

Segmentation Fault Accessing Singleton C++

Overview
I am writing a helper class to make calls to Redis easier with the xRedis drivers in C++, but I am continually receiving a segmentation fault upon requesting or sending any information to the instance.
I think this has to do with the way I'm storing the xRedis and RedisDBIdx instances, and possibly the way I'm storing the RedisAdmin instance within the main application, but I'm unable to see the right way to set these up after several attempts.
Relevant Code is below, and a few notes on debugging steps I have taken myself.
Debugging Notes
Redis Server is started successfully, and log output shows successful connection to server on instance startup
The call fails whether a set or exists command is sent to the server
GDB Output shows below, and logs show the same happenning on either exists or set calls:
Program received signal SIGSEGV, Segmentation fault.
RedisPool::GetConnection (this=0x0, cahcetype=0, dbindex=0, ioType=0) at src/xRedisPool.cpp:124
124 || (ioType>SLAVE)
Code
redis_admin.h
#include "xredis/xRedisClient.h"
#include <string.h>
#include <string>
class xRedisAdmin
{
xRedisClient xRed;
public:
xRedisAdmin(RedisNode conn_list[], int conn_list_size);
~xRedisAdmin();
const char * load ( const char * key );
bool save ( const char * key, const char * msg );
bool exists ( const char * key );
bool del ( const char * key );
};
redis_admin.cpp
xRedisAdmin::xRedisAdmin(RedisNode conn_list[], int conn_list_size)
{
enum {
CACHE_TYPE_1,
CACHE_TYPE_2,
CACHE_TYPE_MAX,
};
xRed.Init(CACHE_TYPE_MAX);
bool bret = xRed.ConnectRedisCache(conn_list, conn_list_size, CACHE_TYPE_1);
//Log results
}
//Exists
bool xRedisAdmin::exists(const char * key)
{
RedisDBIdx d(&xRedis);
char szKey[256] = {0};
sprintf(szKey, key);
return xRed.exists(d, szKey);
}
//Save
bool xRedisAdmin::save(const char * key, const char * val)
{
RedisDBIdx d(&xRed);
char szKey[256] = {0};
sprintf(szKey, key);
bool ret_val = xRed.set(d, szKey, val);
//Log output
return ret_val;
}
Main.cpp
xRedisAdmin *xRed;
void example_callback() {
bool bRet = xRed->save("key", "c_str");
}
int main()
{
xRedisAdmin x (RedisList1, conn_list_size);
xRed = &x;
example_callback();
return 0;
}
As it turns out, this is what I get for violating the Rule of Threes.
I did not have a copy constructor for the Redis Admin, and the xRed object inside it became null on assignment because of this. Use of new & delete, rather than creating in the main method, did resolve the issue. proved the solution to the issue
Thanks all who commented and gave me answers & support!
Alex

XBee, external libraries and passing structures as arguments

I have very weird problem with a library I am creating. The library will be used to communicate between Arduino modules using XBee Series 1 modules. Library is very simple wrapper library around Arduino XBee library.
I have one function that reads received packet and sends it back. At the moment it is implemented as a simple "echo" service - the function just displays the data received and sends it back to per-defined address.
At the moment I have three versions of this function, out of which one is not working.
A function taking no arguments: void processPacket()
A function taking structure as a value as an argument: void processPacket(valuesStruct valuesStructData) - THIS VERSION OF THE FUNCTION IS NOT WORKING!
A function taking pointer to the structure as an argument: void processPacket(valuesStruct* valuesStructData)
At this moment I noticed strange behavior in the 2nd version of the function. I do nothing with the passed argument - the content of all three functions is the same. In 2nd case the function reads wrong values from received XBee packet. In the 1st and 3rd case the function performs correctly.
Code:
ExampleLib.h
#ifndef ExampleLib_h
#define ExampleLib_h
#include "Arduino.h"
#include <XBee.h>
#define ADDRESS_BROADCAST 0xffff
#define ADDRESS_PC 0x3333
typedef struct
{
int valA;
int valB;
int valC;
} valuesStruct;
class ExampleLib
{
public:
ExampleLib();
void setSerial(Stream &serial);
boolean tryReceivePacket();
void processPacket();
// THIS FUNCTION IS NOT WORKING!
void processPacket(valuesStruct valuesStructData);
void processPacket(valuesStruct* valuesStructData);
private:
XBee xbee;
Rx16Response rx16;
};
#endif
ExampleLib.cpp
The value read in line byte* packetData = rx16.getData(); is wrong when we trigger processPacket(valuesStruct valuesStructData) function. In other cases the behavior is correct.
#include "Arduino.h"
#include <XBee.h>
#include "ExampleLib.h"
ExampleLib::ExampleLib()
{
xbee = XBee();
rx16 = Rx16Response();
}
void ExampleLib::setSerial(Stream &serial)
{
xbee.setSerial(serial);
}
boolean ExampleLib::tryReceivePacket()
{
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
// got a rx packet
xbee.getResponse().getRx16Response(rx16);
return true;
}
else {
return false;
}
}
else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
// or flash error led
return false;
}
return false;
}
void ExampleLib::processPacket()
{
byte* packetData = rx16.getData();
byte dataLength = rx16.getDataLength();
Serial.print("START L:");
Serial.println(dataLength);
for (int i = 0; i < dataLength; i++) {
Serial.print(packetData[i]);
Serial.print(" - ");
}
Serial.println("END");
//16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(ADDRESS_PC, packetData, sizeof(packetData));
xbee.send(tx);
}
void ExampleLib::processPacket(valuesStruct valuesStructData)
{
processPacket();
}
void ExampleLib::processPacket(valuesStruct* valuesStructData)
{
processPacket();
}
Arduino sketch
#include <XBee.h>
#include <ExampleLib.h>
ExampleLib exampleLibObj = ExampleLib();
void setup()
{
Serial.begin(9600);
exampleLibObj.setSerial(Serial);
}
void loop()
{
boolean isPacketReceived = exampleLibObj.tryReceivePacket();
if (isPacketReceived) {
// leave only one section, the rest should be commented
//Section 1: working
exampleLibObj.processPacket();
//Section 2: not working
// valuesStruct test;
// test.valA = 0;
// test.valB = 0;
// test.valC = 0;
// exampleLibObj.processPacket(test);
//Section 3: working
// valuesStruct* test;
// test->valA = 0;
// test->valB = 0;
// test->valC = 0;
// exampleLibObj.processPacket(test);
}
}
I am really puzzled why in this one case function is performing differently. Looking forward to any suggestions to that issue.
Thanks,
Michal
Are you sure it isn't your section 3 that's causing problems? Because you're declaring a pointer to a structure, but not allocating memory for that structure.
You'd typically write your code like this:
valuesStruct test;
test.valA = 0;
test.valB = 0;
test.valC = 0;
//Section 2: not working
exampleLibObj.processPacket(test);
//Section 3: working
exampleLibObj.processPacket(&test);
But you also wouldn't typically pass a structure to a function -- you'd pass a pointer to that structure. There really isn't a need for your second sample.