Callback function not called? - c++

I have 2 callback functions in an Arduino file (.ino, c++):
void incomingPacket(const char* subject, const char* message){
Serial.print("subject: ");
Serial.println(subject);
Serial.print("message: ");
Serial.println(message);
}
void connectedProtocol(bool connected){
if(connected){
Serial.println("protocol server connected");
}else{
Serial.println("protocol server disconnected");
}
}
And in my Arduino file I have a custom class UDPProtocol protocol(Udp) which calls the callback functions after certain events, like:
_connectedcb(setTo); //this calls connectedProtocol callback in Arduino from UDPProtocol class
This above works fine, it calls the callback function connectedProtocol() with correct boolean value. However, when trying to call incomingPacket() callback function, nothing happens in my Arduino file. Method that calls the callback:
//this is from UDPProtocol class
void UDPProtocol::readPacketContents(char* content){
if(strlen(content)){ //if content size is greater than 0
const char* somemessage = "Hellomessage";
const char* subject;
const char* message;
subject = &somemessage[0]; //this depends on the actual format of udp packet
message = &somemessage[1];
_packetcb(subject,message);
}
_buffer[0] = 0;
}
Whenever this method runs, callback is not called in Arduino. Even if I try to fake the message like:
void UDPProtocol::readPacketContents(char* content){
if(strlen(content)){ //if content size is greater than 0
const char* somemessage = "Hellomessage";
const char* subject;
const char* message;
subject = &somemessage[0]; //this depends on the actual format of udp packet
message = &somemessage[1];
_packetcb(subject,message);
}
_buffer[0] = 0;
}
Still nothing happens (if(strlen(content)) does equal true). Any suggestions?
EDIT:
UDPProtocol.h file:
typedef void (*PackedCallback)(const char* subject, const char* message);
PackedCallback _packetcb;
UDPProtocol.cpp file:
void UDPProtocol::setPacketCallback(PackedCallback func){
this->_packetcb = func;
}
In Arduino.ino:
protocol.setPacketCallback(incomingPacket); //defined in this question above
EDIT 2:
I created a gist with the full code:
https://gist.github.com/aliamid93/64ba2ed0e06b1f9b4401474646f8083e

Related

How to do IPC using Unix Domain Socket in D?

Here I have a program that wants to
detect whether if it's the only instance
1.1. it does that by trying to create a Unix Domain Socket
and trying to binding it to a specific address.
if a duplicate program is not running, establish an UDS
and then listen to the socket.
2.1. if any message comes through that socket, the program will log the incoming message
2.2. otherwise it should keep listening to the socket forever
if there's a duplicate program it should send a message and then exit.
Here's what I have:
import std.socket, std.experimental.logger;
immutable string socketAddress = "\0/tmp/com.localserver.myapp";
void main()
{
auto socket = new std.socket.Socket(std.socket.AddressFamily.UNIX,
std.socket.SocketType.STREAM);
auto addr = new std.socket.UnixAddress(socketAddress);
auto isUnique = () {
bool result;
scope (success)
log("returns: ", result);
try
{
socket.bind(addr);
result = true;
}
catch (std.socket.SocketOSException e)
result = false;
// else throw error
return result;
}();
if (isUnique)
{
log("Unique instance detected. Listening...");
// works upto now
char[] buffer = [];
while (1)
{
socket.listen(0);
socket.receive(buffer);
if (buffer != []) {
log("Received message: ", buffer);
}
buffer = [];
}
}
else
{
log("Duplicate instance detected.");
socket.connect(addr);
import std.stdio;
stdout.write("Enter your message:\t");
socket.send(readln());
log("Message has been sent. Exiting.");
}
}
The documentation does not seem very friendly to those who does not have any experience in socket programming. How can I send and receive message with std.socket.Socket?
After binding, you actually need to accept. It will return a new Socket instance which you can actually receive from. Your client side branch looks ok. I think that is your key mistake here.
I also have a code sample in my book that shows basic functionality of std.socket which can help as an example:
http://arsdnet.net/dcode/book/chapter_02/03/
it is tcp, but making it unix just means changing the family, like you already did in your code.
You can also look up socket tutorials for C and so on, the D socket is just a thin wrapper around those same BSD style socket functions.
As Adam pointed out I had use listen() method first and then apply the accept() method which returns a socket that can receive message. Then the receiver socket takes a char[N] buffer.
import std.socket, std.experimental.logger;
class UDSIPC
{
private:
static immutable string socketAddress = "\0/tmp/com.localserver.myapp";
static immutable size_t messageBufferSize = 64;
static immutable string socketAddressName = "\0/tmp/com.localserver.myapp";
Socket socket;
UnixAddress uaddr;
public:
this(in string socketAddressName = socketAddressName)
{
socket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
uaddr = new UnixAddress(socketAddress);
}
bool getUniqueness()
{
bool result;
scope (success)
log("returns: ", result);
try
{
socket.bind(uaddr);
result = true;
}
catch (SocketOSException e)
result = false;
// else throw error
return result;
}
string getMessage()
{
socket.listen(0);
auto receiverSocket = socket.accept();
char[messageBufferSize] buffer;
auto amount = receiverSocket.receive(buffer);
import std.string;
return format!"%s"(buffer[0 .. amount]);
}
void sendMessage(in string message)
{
socket.connect(uaddr);
socket.send(message);
}
}
void main()
{
auto ipc = new UDSIPC();
if (ipc.getUniqueness())
{
while (true)
{
log(ipc.getMessage());
}
}
else
{
import std.stdio, std.string;
ipc.sendMessage(readln().chomp());
}
}

Passing const char* into constructor gives null

I'm trying to make a simple logger to log to a file to give me debug information about my program. I want to avoid using a library so I'm making one myself.
logging.cpp
#include <string.h> // String stuff
#include <time.h> // Time
#include "logging.hpp"
// Cathooks main logging util
CatLogger g_CatLogging("/tmp/nekohook.log");
//CatLogger g_CatLogging;
CatLogger::CatLogger(const char* _file_path, bool _ptime) : ptime(_ptime) {
file_path = _file_path;
}
CatLogger::~CatLogger() { fclose(log_handle); }
void CatLogger::log(const char* fmt, ...) {
// Basicly an init, because this cant be done on construct
if (log_handle == nullptr) {
log_handle = fopen(file_path, "w");
}
// Print our time if needed
if (ptime) {
// Get our time
time_t current_time = time(0);
struct tm* time_info = localtime(&current_time);
// print it to a string
char timeString[10];
strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info);
// Print the time into the log
fprintf(log_handle, "%% [%s] ", timeString);
}
// Get the string we want to log
char buffer[1024];
va_list list;
va_start(list, fmt);
vsprintf(buffer, fmt, list);
va_end(list);
// Write our log to the file
fprintf(log_handle, "%s\n", file_path, buffer);
fflush(log_handle);
// Push result var to a console here, if i ever make a console api
}
logging.hpp
#include <stdarg.h> // ... arg
#include <stdio.h> // fopen(), fprint(), fputs()
class CatLogger {
public:
CatLogger(const char* _file_path, bool _ptime = false);
~CatLogger();
void log(const char* fmt, ...); // Use to log with
private:
FILE* log_handle = 0; // Handle used to log to files with
const char* file_path; // Path to log file
const bool ptime; // Whether to print time
};
// Use this to log
extern CatLogger g_CatLogging;
When I use the log function, it fails. I have no idea why. I made a dummy function that crashes when ran to get info from gdb of the input. I input the file_path variable into it and it returns 0x0. I'm not sure why this happens, I've made a sample executable separate from the library I'm using this in and it works flawlessly. Could this be due to the way I'm linking libraries or the lack of?
Here is the library I am working on with a link directly to the logging file.
https://github.com/oneechanhax/nekohook/blob/master/src/util/logging.cpp
It crashes on fprintf() on both due to fopen not returning a file handle, which is in turn because const char* isn't being passes for some reason.
Please tell me a way to debug this or point out where this went wrong as I'm at a loss trying for myself.
EDIT:
If i replace the following in CatLogger::log
if (log_handle == nullptr) {
log_handle = fopen(file_path, "w");
}
With the following
if (log_handle == nullptr) {
log_handle = fopen("/tmp/nekohook.log", "w");
}
It now works but i cant change the log location for other log classes now...
EDIT2:
Here is some debug info. Somehow the const char* doesnt get saved into the class. Thats the main issue that i have...
example
Maybe the string becomes null after constructing...
There are a lot of potential bugs.
if (log_handle == nullptr) {
log_handle = fopen(file_path, "w");
if(!log_handle) {
perror("File opening failed"); // check your console output.
return EXIT_FAILURE;
}
}
// Get the string we want to log
char buffer[1024];
va_list list;
va_start(list, fmt);
vsprintf(buffer, fmt, list); // potential segmentation fault
va_end(list);
use this instead
int vsnprintf( char* buffer, std::size_t buf_size, const char* format, va_list vlist ); // (since C++11)
And more it the program is multithreaded.
This was a case of static init order fiasco where the const char* wouldn't get initialized before the function was called.
The solution was to make the file link first compared to other files and the object works now.

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.

Error occured when sending message through Google Play Game Service with Cocos2d-x

I'm developing a multiplayer game with Google Play Services and Cocos2d-x. I already set up communication between Java and C++ using JNI, and can run processes like Sign-in, Create room, Invitation... Everything is fine until I need to send some struct to another players. When receive data from other, an error has occurred.
Here's the structs:
typedef enum
{
MESSAGE_TYPE_PING = -1,
MESSAGE_TYPE_PING_BACK = 0,
MESSAGE_TYPE_RTT = 3
} MESSAGE_TYPE;
typedef struct
{
MESSAGE_TYPE messType;
} MESSAGE;
typedef struct
{
MESSAGE mess;
timeval sendTime;
const char* text;
} PING_MESSAGE;
And in this code snippet I convert struct and send it to another players:
void MyClass::sendData()
{
// Send package
PING_MESSAGE ping;
ping.mess.messType = MESSAGE_TYPE_PING;
ping.sendTime = startTime;
ping.text = "Ohhhhhh";
char byte[sizeof(ping)];
memcpy(byte, &ping, sizeof(ping));
GCHelper::sendReliableRealTimeMessage(&byte[0]);
}
// In GCHelper's implementation
void GCHelper::sendReliableRealTimeMessage(const char* byteMess)
{
// Convert char* to jbyteArray
jbyteArray bArray = env->NewByteArray(16); //env is a static JNIEnv* in this class; 16 since my PING_MESSAGE struct is 16 bytes in size;
env->SetByteArrayRegion(bArray,0,16,(jbyte*)byteMess);
// Make Java call
env->CallStaticVoidMethod(classID, methodID, bArray);
methodInfo.env->DeleteLocalRef(bArray);
methodInfo.env->DeleteLocalRef(classID);
}
Now, Java code take responsibility to send the byte array to participants in room. At receiver side, I'm continue send received data to C++. And error occured here when I convert jbyteArray back to struct:
void Java_package_name_TestMulti_nativeOnRealTimeMessageReceived(JNIEnv *env, jobject thiz, jbyteArray receivedMess)
{
CCLOG("Called from java");
jboolean isCopy;
jbyte * pCData = env->GetByteArrayElements(receivedMess, &isCopy);
jsize size = env->GetArrayLength(receivedMess);
const char* sd = (const char*)pCData;
PING_MESSAGE result;
memcpy(&result, sd, sizeof(result));
CCLOG("TEST size: %d", size); // This log out: "TEST size: 16"
CCLOG("TEST type: %d", result.mess.messType); // This log out: "TEST type: -1"
CCLOG("TEST text: %s", result.text); // AND ERROR!!!!!!!
cocos2d::CCByteArray* data = cocos2d::CCByteArray::createWithData(sd);
cocos2d::CCNotificationCenter::sharedNotificationCenter()->postNotification("onRealTimeMessageReceived", data);
if(isCopy)
{
env->ReleaseByteArrayElements(receivedMess,pCData,JNI_ABORT);
}
}
I'm not understand here. If I don't send byte array to another players yet send that array back to C++ by calling nativeOnRealTimeMessageReceived() method from Java side, it runs fine and logs correctly. It's mean that with the same byte[] package converted from char* in C++, if I just pass it back to C++, it's correct, but if I send it through Google Play Game Services, it goes wrong. What does this mean?

mongoose web server helloworld program

I came across an embedded web server named mongoose and http://code.google.com/p/mongoose/ and I read the wiki it was great and i searched for some sample hello world program but i couldn't find it... i found some example but that was written in c++ for windows and can any one provide an example c program to run this webserver..
It is quite simple, first you need to implement the call back function:
void *event_handler(enum mg_event event,
struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
static void* done = "done";
if (event == MG_NEW_REQUEST) {
if (strcmp(request_info->uri, "/hello") == 0) {
// handle c[renderer] request
if(strcmp(request_info->request_method, "GET") != 0) {
// send error (we only care about HTTP GET)
mg_printf(conn, "HTTP/1.1 %d Error (%s)\r\n\r\n%s",
500,
"we only care about HTTP GET",
"we only care about HTTP GET");
// return not null means we handled the request
return done;
}
// handle your GET request to /hello
char* content = "Hello World!";
char* mimeType = "text/plain";
int contentLength = strlen(content);
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Cache: no-cache\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"\r\n",
mimeType,
contentLength);
mg_write(conn, content, contentLength);
return done;
}
}
// in this example i only handle /hello
mg_printf(conn, "HTTP/1.1 %d Error (%s)\r\n\r\n%s",
500, /* This the error code you want to send back*/
"Invalid Request.",
"Invalid Request.");
return done;
}
// No suitable handler found, mark as not processed. Mongoose will
// try to serve the request.
return NULL;
}
Then you need to start the server:
int main(int argc, char **argv) {
/* Default options for the HTTP server */
const char *options[] = {
"listening_ports", "8081",
"num_threads", "10",
NULL
};
/* Initialize HTTP layer */
static struct mg_context *ctx;
ctx = mg_start(&event_handler, options);
if(ctx == NULL) {
exit(EXIT_FAILURE);
}
puts("Server running, press enter to exit\n");
getchar();
mg_stop(ctx);
return EXIT_SUCCESS;
}
I wrote a C++ REST service library that uses Mongoose. Here's a simple example:
#include <iostream>
#include <server/server.hpp>
int main()
{
using namespace pwned::server;
Server server;
server.Get("/", [](mg_event*, Params const &) {
return Server::response("Hello!");
});
std::cin.get();
}
Based on
https://github.com/nurettin/pwned/blob/master/examples/server/basics/server.cpp
Compile application: $ cc my_app.c mongoose.c
#include "mongoose.h" // Include Mongoose API definitions
static const char *s_http_port = "8089";
// Define an event handler function
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
{
struct mbuf *io = &nc->recv_mbuf;
switch (ev)
{
case MG_EV_RECV:
// This event handler implements simple TCP echo server
mg_send(nc, io->buf, io->len); // Echo received data back
mbuf_remove(io, io->len); // Discard data from recv buffer
break;
default:
break;
}
}
int main(void)
{
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL); // Initialize event manager object
// Note that many connections can be added to a single event manager
// Connections can be created at any point, e.g. in event handler function
mg_bind(&mgr, s_http_port, ev_handler); // Create listening connection and add it to the event manager
for (;;)
{
// Start infinite event loop
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
For anyone looking at this thread - from the mongoose library author (me).
Mongoose web server has long ago moved to https://github.com/cesanta/mongoose
It has a comprehensive list of examples, starting from a simplest ones, to the more complex ones, see examples directory in the repository. The minimal static HTTP server goes as follows:
#include "mongoose.h"
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_http_serve_opts opts = {.root_dir = "."}; // Serve local dir
if (ev == MG_EV_HTTP_MSG) mg_http_serve_dir(c, ev_data, &opts);
}
int main(int argc, char *argv[]) {
struct mg_mgr mgr;
mg_mgr_init(&mgr); // Init manager
mg_http_listen(&mgr, "http://localhost:8000", fn, &mgr); // Setup listener
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop
mg_mgr_free(&mgr); // Cleanup
return 0;
}