As above, I'm trying to manage HTTP response with JSON payload without any library like curl or others. Everything is written inside an S-function in Simulink environment but the "main" is (hoping) perfectly C.
My procedure:
My code here:
InputPtrsType u = ssGetInputPortSignalPtrs(S,0);
InputUInt8PtrsType resp8 = (InputUInt8PtrsType)u; // resp8 is my input uint8 array
real_T *y0 = (real_T *) ssGetOutputPortRealSignal(S, 0); // y0 is the output
int j=0;
int h=0;
int start;
int end;
// Cut HTTP headers
char str[3000]; // 3000 is the resp8 size (constant)
for ( int i=0; i<=3000; i++ ){
str[i]=*resp8[i]; // Copy values in char str.
if (str[i]== 123) // If str[i]== "{"...
{
j=j+1;
if (j==2)
{
start=i; // At the second "{" in the text will start the JSON payload
}
}
if (str[i]== 125) // If str[i]== "}"...
{
h=h+1;
if (h==2)
{
end=i; // At the second "}" in the text will finish the JSON payload
}
}
}
char cut_str[1000]; // Create a new char to store the just the JSON payload
strncpy(cut_str, str+start,end-start+1); // Can be done also with a for cycle.
cut_str[end-start+1]='\0'; // Null terminated.
// JSON PARSING using tiny-json.h and tiny-json.c
json_t mem[32];
json_t const* json = json_create( cut_str, mem, sizeof mem / sizeof *mem );
json_t const* key = json_getProperty( json, "Val1" );
int const value = (int)json_getInteger( key );
printf( "Val1: %d\n", value );
y0[0]= value;
Note:
Problems:
Questions:
Thank you!
User contributions licensed under CC BY-SA 3.0