Communication between 2 applications using IPC mechanism

Hi Team,

I am currently working on establishing bi-directional communication between 2 application (client & server). My objective is that when client calls the server API (i.e FUNCTION fname defined in .api file) , I am also passing a HANDLER (a client function) so that server can respond back to the client by calling the callback from the server side i.e send response back to the client.

Scenerio:

Below are my 2 application source file and API file:

printer.api:

HANDLER callback
(
string msg[100] IN
);

FUNCTION request
(
string message[100] IN,
callback callbackHandler IN
);

printServer/server.c:

#include “legato.h”
#include “interfaces.h”

void Fx(const char* message);
static printer_printClientHandler_t cbkHandler;

void printer_request(const char* message, printer_printClientHandler_t callback, void contextPtr)
{
LE_INFO("
******* Client says ‘%s’", message);
Fx(message);
cbkHandler = callback;
cbkHandler(“How are you client”);
}

void Fx(const char* message)
{
LE_INFO(“******** Internal message: ‘%s’”, message);
}

COMPONENT_INIT
{

}

printClient/client.c:

#include “legato.h”
#include “interfaces.h”

static void internal_fn(const char *message);

COMPONENT_INIT
{
LE_INFO(“Asking server to print ‘Hello, world!’”);
internal_fn(“Hello, world!”);
}

void callback(const char *rsp)
{
LE_INFO(“Response received from server: ‘%s’”, rsp);
}

void internal_fn(const char *message)
{
char response[100];
LE_INFO(“Calling server API from internal fn!!!”);
printer_request(message, callback, NULL);
LE_INFO(“Response received from server: ‘%s’!!!”, response);
}

Output:

I am observing the crash at the server side when the callback function is getting called saying the address is ‘null’.

Is this approach the correct approach for communication between 2 seperate legato applications each having one component each. If not please provide correct approach with some source code.

Thanks and regards,
Kartik

The server.c file and client.c files are not proper as per Legato framework API specifications.
See the modified server.c file:

#include “legato.h”
#include “interfaces.h”

void Fx(const char* message);
static printer_callbackFunc_t cbkHandler; // The type of cbkHandler corrected to printer_callbackFunc_t

void printer_request(const char* message, printer_callbackFunc_t callback, void* contextPtr) // The type of callback corrected to printer_callbackFunc_t
{
LE_INFO(“******* Client says %s”, message);
Fx(message);
cbkHandler = callback;
cbkHandler(“How are you client”, contextPtr); // Added contextPtr as second argument
}

void Fx(const char* message)
{
LE_INFO(“******** Internal message: %s”, message);
}

COMPONENT_INIT
{

}

See the modified client.c file:

#include “legato.h”
#include “interfaces.h”

static void internal_fn(const char *message);

COMPONENT_INIT
{
LE_INFO(“Asking server to print Hello, world!”);
internal_fn(“Hello, world!”);
}

void callback(const char rsp, void contextPtr) // Added contextPtr as second argument
{
LE_INFO(“Response received from server: %s”, rsp);
}

void internal_fn(const char *message)
{
char response[100];
LE_INFO(“Calling server API from internal fn!!!”);
printer_request(message, callback, NULL);
LE_INFO(“Response received from server: %s!!!”, response);
}

I am able to compile this application properly.

Thanks,
Pinkesh Shah

Hello,

I have tested this code in WP85 module and I am able to get the response from callback function.

client.c file:
#include “legato.h”
#include “interfaces.h”

static void internal_fn(const char *message);

COMPONENT_INIT
{
LE_INFO(“Asking server to print Hello, world!”);
internal_fn(“Hello, world!”);
}

void callback(const char* rsp, void *contextPtr) // Added contextPtr as second argument
{
LE_INFO(“Response received from server: %s”, rsp);
}

void internal_fn(const char *message)
{
char response[100];
LE_INFO(“Calling server API from internal fn!!!”);
printer_request(message, callback, NULL);
LE_INFO(“Response received from server: %s!!!”, response);
}

server.c file:
#include “legato.h”
#include “interfaces.h”

void Fx(const char* message);
static printer_callbackFunc_t cbkHandler; // The type of cbkHandler corrected to printer_callbackFunc_t

void printer_request(const char* message, printer_callbackFunc_t callback, void* contextPtr) // The type of callback corrected to printer_callbackFunc_t
{
LE_INFO(“******* Client says %s”, message);
Fx(message);
cbkHandler = callback;
cbkHandler(“How are you client”, contextPtr); // Added contextPtr as second argument
}

void Fx(const char* message)
{
LE_INFO(“******** Internal message: %s”, message);
}

COMPONENT_INIT
{

}

Start client app in manual manner (start: manual) to make the control on it.
Start server app in automatic manner.

Install server app first on the target and then install client app.
Start client app on the target.

See the log from the target console:
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | supervisor[548]/supervisor T=main | proc.c proc_Start() 1394 | Starting process ‘client’ with pid 5304
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | supervisor[5304]/supervisor T=main | proc.c proc_Start() 1359 | Execing ‘client’
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | client[5304]/printClient T=main | client.c _printClient_COMPONENT_INIT() 8 | Asking server to print Hello, world!
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | client[5304]/printClient T=main | client.c internal_fn() 20 | Calling server API from internal fn!!!
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | server[5213]/printServer T=main | server.c printer_request() 9 | ******* Client says Hello, world!
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | server[5213]/printServer T=main | server.c Fx() 17 | ******** Internal message: Hello, world!
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | client[5304]/printClient T=main | client.c internal_fn() 22 | Response received from server: !!!
Jan 1 04:27:10 swi-mdm9x15 user.info Legato: INFO | client[5304]/printClient T=main | client.c callback() 14 | Response received from server: How are you client

Thanks,
Pinkesh Shah

1 Like