Issues using EventQueue within a class

30 Mar 2019

Hi there,

I'm trying to use the shared event queue for socket events as detailed in the example: https://os.mbed.com/docs/mbed-os/v5.11/apis/network-socket.html

I have a class that takes data from a TCPSocket and processes it. So I have a server socket that presents client connections to my class for sending/receiving data.

In main() I have a TCPSocket that I call open, bind and then listen and I pass a method of my class as the event handler.

EventQueue *queue = mbed_event_queue();
Event<void()> handler = queue->event(&DaliMaster, &Dali::server_sigio, &socket);
socket.set_blocking(false);
socket.sigio(handler);
handler();

What I want to do is have my class handle the socket->accept() and create a new event handler for the client connection. So I have the following code after I've got a new socket from accept():

Class::method() {
    Event<void()> client_handler = queue->event(this, &Dali::client_sigio, client);
    client->set_blocking(false);
    client->sigio(client_handler); 
    client_handler();
}

What I'm getting is a runtime exception in Event.h on line 158 which is an assert to check "id". It fires when I call client_handler() the first time so perhaps I can't set up the event like I've done?

In the second call, client is a pointer to a TCPSocket.

Any ideas what I'm doing wrong?

30 Mar 2019

This is the exception:

++ MbedOS Error Info ++
Error Status: 0x80FF0144 Code: 324 Module: 255
Error Message: Assertion failed: id
Location: 0xF777
File: ./mbed-os/events/Event.h+158
Error Value: 0x0
Current Thread: shared_event_queue  Id: 0x10000EEC Entry: 0x12B59 StackSize: 0x800 StackMem: 0x10000F38 SP: 0x1000161C 
For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF0144
-- MbedOS Error Info --
30 Mar 2019

Hmm, okay, after some reading of the high level description of the API I thought I might be in a context where I can't call the event. So I changed the following and now it works. If anyone wants to explain to me why that would help. Thanks.

	Event<void()> client_handler = queue->event(this, &Dali::client_sigio, client);
	client->set_blocking(false);
	client->sigio(client_handler);
	client_handler.post();
31 Mar 2019

I think I've exhausted my C++ foo. I think I have an issue of scope maybe. I'm trying to create the client_handler event when in interrupt context after socket->accept but is the event lost as soon as I exit the current method?

I can connect to the server, but as soon as I send any data or disconnect I get an assertion firing.

	switch (next_state) {
        case LISTENING:
			this->client = socket->accept(&err);
			switch(err) {
                case NSAPI_ERROR_WOULD_BLOCK:
                    // Continue to listen
					break;
				case NSAPI_ERROR_OK: {
					// Accepted connection
					//client_init();
					eventFlags.set(FLAG_CLIENT_CONNECT);
					Event<void()> client_handler = this->queue->event(this, &Dali::client_sigio, this->client);
					this->client->set_blocking(false);
					this->client->sigio(client_handler);
					client_handler.post();
					next_state = ACCEPT;
					break;
				}
                default:
                    // Error in connection phase
					_uart->printf("Dali::server_sigio: err = %d\n\r", err);	
					next_state = CLOSE;
					break;
            }
			break;
02 Apr 2019

Perhaps a simpler question is in order.

Given I can declare an event in the above example like this:

Event<void()> client_handler = this->queue->event(this, &Dali::client_sigio, this->client);

How do I declare the same event in my class and assign to it later on in some method call? I can't get the syntax of the template correct.

class myClass {
  private:
  EventQueue *queue;
  Event<void()> client_handler;
};

myClass::my_method() {
  queue = mbed_event_queue();
  client_handler = queue->event(this, &myClass::some_other_method, this->socket);
}
07 Apr 2019

I've created an example of what I'm trying to do. In the following example, I'm posting the client_handler event to the shared queue but it never gets called. What am I doing wrong?

https://os.mbed.com/users/sjalloq/code/tcp-socket-server/

06 Nov 2019

I have a similar issue with Event.h and I get the same exact error code. I haven't been able to get around it, but I used a very similar implementation to the example under called "Using mbed-events" at https://os.mbed.com/blog/entry/Simplify-your-code-with-mbed-events/. I don't know if it will help you with your issue.