# Cannot get callback between components (IPC) to work properly

**URL:** <https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653>\
**Category:** Legato Application Framework\
**Created:** [August 28, 2019, 12:33pm UTC](https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653 "2019-08-28T12:33:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BCUbing](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@BCUbing](https://forum.legato.io/u/BCUbing)\
**Post date:** [August 28, 2019, 12:33pm UTC](https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653/1 "2019-08-28T12:33:52Z")

</div>

Hi,

I’m currently working on an application that must send data from one component (component A) to the other (component B) (via IPC).  
The sending part is implemented via API in component B.

_communication.api_

> FUNCTION send  
> (  
> char buffer[BUFFER\_SIZE] IN  
> );

However component B can have data ready at any time for component A. For this case I want to use a callback to send data from component B to component A.

I tried the following:  
_communication.api_

> DEFINE BUFFER\_SIZE = 64;
> 
> HANDLER onDataAvailable  
> (  
> char buffer[BUFFER\_SIZE] IN  
> );
> 
> FUNCTION register\_handler  
> (  
> onDataAvailable dataEventHandler IN  
> );

_component\_b.c_

> …  
> communication\_onDataAvailableFunc\_t communicatorEventHandler;  
> …  
> void communication\_register\_handler(communication\_onDataAvailableFunc\_t callback, void\* contextPtr)  
> {  
> communicatorEventHandler = callback;  
> }

In component A call the communication\_register\_handler function to associate the callback to a function. This work… sort off: it only works once. When the handler is called a second time I got an error:

> Handler passed to communication\_register\_callback() can’t be called more than once

* * *

* * *

So after some research I found out that Legato has an [Event Loop API](https://docs.legato.io/latest/c_eventLoop.html) that can provide such functionality.

I tried to implement this as described in the API reference, but i run into some issues.  
_The code below is from [Layered Publish-Subscribe Handlers example](https://docs.legato.io/latest/c_eventLoop.html#c_event_layeredPublishSubscribe) and is used as reference._

The mktool creates a tempSensor\_server.h based on the tempSensor.api file (such as the first code block on the API reference page).

Then the implementation in component B (same as the second code blocK on the API reference page) gives me an error on the following line:

> clientHandlerFunc(\*temperaturePtr, le\_event\_GetContextPtr())

The error I got is:  
_error: called object ‘clientHandlerFunc’ is not a function or function pointer_

The _clientHandlerFunc_ is of type: tempSensor\_ChangeHandlerRef\_t which is generated:

> typedef struct tempSensor\_ChangeHandler\* tempSensor\_ChangeHandlerRef\_t;

Which should make sense that this gives an error because it is a pointer to a struct and not e.g. a void.

Has anybody encountered this issue, or is there an other way to implement a callback between two components via IPC?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [August 29, 2019, 1:44am UTC](https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653/2 "2019-08-29T01:44:43Z")

</div>

you can refer to my application.[IPC\_step4.rar](https://forum.legato.io/uploads/short-url/syTZydAV5mv0Ydf7OXQe8cI8euc.rar) (303.8 KB)

---

<div class="post-metadata">

**Author:** ![BCUbing](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@BCUbing](https://forum.legato.io/u/BCUbing)\
**Post date:** [August 29, 2019, 10:08am UTC](https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653/3 "2019-08-29T10:08:24Z")

</div>

Thanks for the response [jyijyi](https://forum.legato.io/u/jyijyi)!  
I got the callback working based on your application.

* * *

The reference code (as seen [here](https://docs.legato.io/latest/c_eventLoop.html#c_event_layeredPublishSubscribe)) is not correct, the following line:

> tempSensor\_ChangeHandlerRef\_t clientHandlerFunc = secondLayerHandlerFunc;

has to be:

> tempSensor\_ChangeHandlerFunc\_t clientHandlerFunc = secondLayerHandlerFunc;

Thanks again 🙂

---

<div class="post-metadata">

**Author:** ![french.bulldog](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@french.bulldog](https://forum.legato.io/u/french.bulldog)\
**Post date:** [January 24, 2024, 12:45am UTC](https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653/4 "2024-01-24T00:45:18Z")

</div>

Hi

I use your application IPC\_step4.rar and it works very well. Thank you.  
I am trying to use string as payload, instead of uint32 in your example, but I get buffer overflow problems.

I change sizeof(uint32) to sizeof(strPtr), and change all the variable types, and allocate enough buffer, but still having trouble.

Can you help?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [January 24, 2024, 4:12am UTC](https://forum.legato.io/t/cannot-get-callback-between-components-ipc-to-work-properly/4653/5 "2024-01-24T04:12:43Z")

</div>

Here is an example to return STRUCT inside handler:

> <https://github.com/legatoproject/legato-af/blob/master/apps/sample/gpsApp/ctrlGPS.api>

You can see if it can be ported to your existing application.

On the other hand, you can do another IPC call inside the handler to get the required data.  
Please see this sample application:  
[IPC\_step4\_payload\_test.7z](https://forum.legato.io/uploads/short-url/i7Rcvb2YVwwfUrcJSbeN4LzZPO6.7z) (3.0 KB)
