API function with return type 'string'

Hiya,

TL;DR: Is it possible to define an API function with the return type ‘string’?


I would like to build an API function to return the text representation of an enum value (similar to what LE_RESULT_TXT() uses).

I have defined an api function like so:

FUNCTION string ModeToString
(
    int32 pMode IN
);

where pMode is my input value to translate.

I’ve implemented this in my C file like this:

const char *myapi_ModeToString ( int32_t pMode )
{
    switch (pMode)
    {
        case 0:  return "MODE 0";
        case 1:  return "MODE 1";
        default: return "(unknown)";
    }
}

but the build tools are not happy and throw this error when parsing the .api file:

In file included from /home/developer/legato-18.10.3-octave/framework/include/legato.h:200:0,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.h:17,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c:12:
/home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c: In function ‘Handle_myapi_ModeToString’:
/home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c:356:15: error: too few arguments to function ‘le_pack_PackString’
/home/developer/legato-18.10.3-octave/framework/include/le_log.h:561:15: note: in definition of macro ‘LE_ASSERT’
In file included from /home/developer/legato-18.10.3-octave/framework/include/legato.h:215:0,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.h:17,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c:12:
/home/developer/legato-18.10.3-octave/framework/include/le_pack.h:358:20: note: declared here
[7/84] Generating IPC interface code
ninja: build stopped: subcommand failed.
Makefile:84: recipe for target 'build' failed
make: *** [build] Error 1

Any suggestions gratefully accepted.

ciao, Dave

Hi @davidc,

You can return a string from an API function, however, only as an OUT parameter to the function and not as a return type.

From here: Syntax - Legato Docs

The returnType is optional, and if specified, can be any type that’s not an array, string, or handler.

Maybe try something like this?

.api file

DEFINE MAX_STR_LEN = 32;

FUNCTION ModeToString
(
    int32 mode IN,
    string modeStr[MAX_STR_LEN] OUT
);

.c file

void myapi_ModeToString(int32_t mode, char* modeStr, size_t modeStrNumElements)
{
    // evaluate 'mode'
    // write string to 'modeStr'
}

Raf

Hi @raf

Ta. I knew how to do that … was wanting to know if I could have a string as the function return type, not as a function parameter.

ciao, Dave

hi @davidc, your request is currently not possible, and the solution is given into the post from raf,
best regards,