Remove HTTP headers and parse JSON payload in C with tiny-json (no libcurl)

1

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:

  • take the uint8 array with ASCII response (headers + JSON text) like: [72 84 84 80 32 50 48 48 32 79 75....] //HTTP 200 OK....{"Val":100,"Val2":2,....}
  • copy this array in char array "str" and find the second "{" and the last "}" because there are other braces for cookies. I don't care about "\r\n\r\n" because it's always a JSON response.
  • I save my start and end indexes
  • I create another char array cut_str and I copy the str values from start to end adding a '\0' at the end.
  • Take cut_str and use it inside json_create function define in tiny-json.
  • Some other code to extract the value of the JSON fields and...voila!

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:

  • everything works perfectly inside Simulink offline simulation.

Problems:

  • when I try to compile the complete project inside my embedded system there are some warnings about the loops and possible uninitialized with end and start
  • if I try to create cut_str[end-start+1] gives me errors.
  • and more important, Memory violation exception! Real-time application (ID 0x008748b1) crashed due to an illegal memory access at address 0x00000bb8. Execution stopped at address 0x0804c3eb near symbol 'Application_008748b1.x86@_btext+0x24eb and application stops!

Questions:

  • where am I wrong? It seems like I'm doing something illegal...but why in Simulink works everything?
  • I have some doubts about end,start,str and cut_str...should they be pointers? Why I can't use normal array?

Thank you!

c
json
for-loop
s-function
asked on Stack Overflow Aug 27, 2020 by nick_bl

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0