Hi community,

I'm playing arounf with eclipse MITA and created a program that should periodically post the status of the XDK sensors to a REST api. Here's my Mita code:

struct AccelSensor {
    var x: int32;
    var y: int32;
    var z: int32;
}

struct GyroSensor {
    var x: int32;
    var y: int32;
    var z: int32;    
}

struct LightSensor {
    var i: uint32;
}

struct DeviceState {
    var accel : AccelSensor;
    var gyro : GyroSensor;
    var light : LightSensor;
}

fn toJSON(data : AccelSensor) {
    return `{"x":${data.x},"y":${data.y},"z":${data.z}}`;
}

fn toJSON(data : GyroSensor) {
    return `{"x":${data.x},"y":${data.y},"z":${data.z}}`;
}

fn toJSON(data : LightSensor) {
    return  `{"i":${data.i}}`;
}

fn toJSON(data : DeviceState) {
    var light = toJSON(data.light);
    var gyro = toJSON(data.gyro);
    var accel = toJSON(data.accel);
    return `{"light":${light},"gyro":${gyro},"accel":${accel}}`;
}

every button_one.pressed {
    print("Hello World, too\n");
    rest.restSignal.write(toJSON(currentState));
}

Pushing the button_one gets me into (including some debug messages added by me):

 INFO | XDK DEVICE 1: Hello World, too
 INFO | XDK DEVICE 1: 3rd-party/ServalStack/src/Msg/OutMsgFactory.c (Line: 223) factory_ptr->size =0x2
 INFO | XDK DEVICE 1: 3rd-party/ServalStack/src/Msg/OutMsgFactory.c (Line: 256) iter 0, cursor 1: written (0)+= items.len (108)
 INFO | XDK DEVICE 1: 3rd-party/ServalStack/src/Msg/OutMsgFactory.c (Line: 223) factory_ptr->size =0x1
 INFO | XDK DEVICE 1: 3rd-party/ServalStack/src/Msg/OutMsgFactory.c (Line: 256) iter 0, cursor 0: written (0)+= items.len (0)
 INFO | XDK DEVICE 1: 3rd-party/ServalStack/src/Http/HttpUtil.c (Line: 101) len =0x0
 INFO | XDK DEVICE 1: asserted at Filename 3rd-party/ServalStack/src/Http/HttpUtil.c , line no  102
 INFO | XDK DEVICE 1: Error in ServalPAL package.
 INFO | XDK DEVICE 1:     Package ID: 30    Module ID: 33    Severity code: 2    Error code: 10.

...[last messages repeating forever until I turn off hte XDK]

Apparently the Serval package has an issue with an empty chunk. Any clue, how to circumvent/solve this?

0 (0 Votes)
RE: Using eclipse.Mita: Error when posting JSON to HTTP Server
Answer
8/10/18 11:57 AM as a reply to Holger Smolinski.

dug a bit into the issue and found the issue in the toJSON functions - yes, I know - there is a warning about non-primitive types, but the C-code looks great (added some printf debugging):

Retcode_T toJSON_LightSensor(char** _result, LightSensor data)
{

    Retcode_T exception = NO_EXCEPTION;
    char result2136509236[18];
    snprintf(result2136509236, sizeof(result2136509236), "{\"i\":%" PRIu32 "}", data.i);
    printf("Printed '%s' to %p\n",result2136509236,result2136509236);
    printf("Copying '%s' to %p(%p)\n",result2136509236,*_result,_result);
    memcpy(*_result, result2136509236, sizeof(result2136509236));
    printf("Copied  '%s' to %p\n",*_result,*_result);
    return exception;

    return exception;
}

...

    char result1361666499[19] = {0};
    printf("Printing light to %p (%p)\n",result1361666499,&result1361666499);
    exception = toJSON_LightSensor(&result1361666499, data.light);
    printf("Printed '%s' to %p\n",result1361666499,result1361666499);

...

 

Ande here we go, the output is:

 INFO | XDK DEVICE 1: Printing light to 0x20002ed4 (0x20002ed4)
 INFO | XDK DEVICE 1: Printed '{"i":260640}' to 0x20002d00
 INFO | XDK DEVICE 1: Copying '{"i":260640}' to 0x0(0x20002ed4)
 INFO | XDK DEVICE 1: Copied  '(null)' to 0x0
 INFO | XDK DEVICE 1: Printed '' to 0x20002ed4

Note the interesting fact, that in the first output line

result1361666499 and its address &result1361666499 share the same value. That is weird, because the first thing the function does is dereferencing the address, which in turn leads to dereferenceing the value of the pointer, which contains all 0s.

Supposedly this behavior is not conforming to the C standard, is it?

Maybe the optimzer in the compiler overdoes? ...holding the address in a register and not representing the address correctly? Any other points of view?

0 (0 Votes)
RE: Using eclipse.Mita: Error when posting JSON to HTTP Server
Answer
8/10/18 1:47 PM as a reply to Holger Smolinski.
Hello Holger,

As far as I can observe, the error might be related to the underlying generated C code. As you already took a look into it, there is a C generator running which transforms Mita in respective C code.

Since the C code is throwing the errors, you would have to fix that error there and it should work, when building it again. Furthermore, these changes are only present if you do not make any changes to the Mita code since the generated code will then lose all changes you made directly to it.

Please note that Mita is still in development process since it does not cover all features of the XDK. As such, the generators might lead to unexpected behavior as you noticed it.

To analyze this in detail and to propose possible solutions for that it would be necessary if you could also post the other parts of your code. For example, the code where you are setting up the REST client.

Kind regards,
Franjo
0 (0 Votes)