Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Error calling my C function from JS

olafolaf Tempe AZ US
edited March 21 in Mongoose OS

Trying to verify this guide "https://mongoose-os.com/docs/mongoose-os/quickstart/develop-in-js.md" by create a C function that takes 3 char arguments and return an double.
I implement in src/main.c
double threectoi(char a, char b, char c);

and in fs/init.js
let f = ffi('double threectoi(char,char,char)');

when building I get 'MJS error: bad ffi signature: "double threectoi( char,char,char)" : failed to parse val type "char" '

I have tried to modify function to take int arguments instead of char, but then it gives me this error: failed to call FFIed function: actual arg#2 is not an int (the type idx is : string) : actual arg#1 is not an int (the type idx is : string) : actual arg#0 is not an int (the type idx is : string)
the three arguments are single bytes from a JSON char string.

Can ffi only handle double and int arguments? Or what have I missed?

Comments

  • mamuespmamuesp Germany/Northern coast

    only declare the types, not the names in the ffi.

  • olafolaf Tempe AZ US
    edited April 3

    Thanks for your replies, it help me solve 50% of my problem...

    @mamuesp hmm, I only put the argument types and names in main.c definition, not in the init.js !

    @nliviu in https://github.com/cesanta/mjs#cc-interoperability it says that argument types are: int, double, char* and void*.
    char and char* is not exactly the same, or did the document list all of the simple argument types that it support?

    I was able to solve the problem using adding in main.c

    #include "mgos.h"
    #include "mgos_rpc.h"
    #include "stdlib.h"
    

    and in init.js (based on demo-js-master proj)

    let c_atoi = ffi('int atoi(char*)');
    //data is string containing JSON syntax
    let ble_parse = JSON.parse(data);
    let sensorCel = c_atoi(ble_parse.event_data.Temp);
    

    And it's working!!

    The other half of my problem is to go the other way. I convert Celsius to Fahrenheit, and want to convert number(float) back to a string.
    I have tried different JS method like sensorFah.toString() without luck. Not supported in mJS, right!?
    So I want to use C function itoa(), first casting float to int.

    let c_itoa = ffi('void itoa(int, char * )');
    let sensorFah = floatToInt((sensorCel*9/5)+32,10);
    // let sensorFah = parseInt((sensorCel*9/5)+32,10);
    
    let sensorFahStr = "    " ;
    c_itoa(sensorFah,sensorFahStr);
    

    but both floatToInt() and parseInt() resulted in "is not defined".
    and sensorFahStr contain just the initial values of empty spaces.

    What am I missing?
    I feel I am complicating something that should be straight forward to do.
    Being fairly new to (m)JS, please advise..

    Another Question.
    In the main.c file, how many of the standard C libraries
    can I #include besides "string.h" and "stdlib.h"

    Thanks

  • olafolaf Tempe AZ US

    if I simplify my issue/question...
    I have defined an ffi in init.js like:

    let c_itoa = ffi('void itoa(int, char*)');

    How do I create a char * variable and malloc some bytes in mJS, to get the itoa() output?

    in JS: char * mypchar; => error: [char] is not defined.

    Thanks

  • nliviunliviu Romania
    load('api_sys.js');
    
    let c_itoa = ffi('void itoa(int, char*)');
    let buf=Sys.malloc(32); // allocate a 32 bytes buffer
    c_itoa(77, buf);
    // process buf
    Sys.free(buf);
    

    Did not test it, but it should work.

    Ref: https://github.com/mongoose-os-libs/mjs/blob/03b44d70ab42cdc5d7f259031d4e39517cd6260e/fs/api_sys.js#L19-L20

    "I convert Celsius to Fahrenheit, and want to convert number(float) back to a string":

    let fahrenheitString = JSON.stringify(fahrenheitFloat);
    

    JSON.parse/JSON.stringify can be used to convert from string to numeric/numeric to string.

    "In the main.c file, how many of the standard C libraries"
    You can any include the standard C libraries you need.

Sign In or Register to comment.