harryw
harryw

Reputation: 23

ESP32 ESP-IDF cJSON read Buffer into data structure

I'm currently facing the problem to allocate memory dynamically for a JSON file.

The file is already been read correct into a char buffer.
The buffer size is defined global with 64000.

On on hand I would like to allocate the file dynamically when read in the file.
For those I've found the function fseek but I don't know how to implement the malloc function properly.

char *read(const char *path)
{
   //printf("%s",path);
    
    //ESP_LOGI(TAG, "%s", path);
    FILE *f = fopen(path, "r");
    if (f == NULL) {
        ESP_LOGE(TAG, "Could not open file for reading");
    }
    fseek(f, 0, SEEK_END); // seek to end of file
    int size = ftell(f); // get current file pointer
    fseek(f, 0, SEEK_SET);
    fread(buffer, 1, sizeof(size), f);
   
    // strip newline
    return buffer;
}

After this I'm using the cJSON library to pass the buffer into a data structure.
But when I run the program on esp32 the cjson doesn't seemed to have the correct size at all.

#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>
#include "cJSON.c"
#include "cJSON.h"

//#include "esp_spiffs.h"
#include "esp_log.h"
//#include "esp_vfs.h"

typedef struct caststruct{
    int value1;
    int value2;
    int value3;
    int value4;
    int value5;
};
 
//#define TAG "spiffs"
int maxobj = 0;

struct caststruct sdata[40];

void jsonread(){
    char filename[] = "/test1";
    ESP_LOGI("JSON","Step1");
    //buffer[sizeof(loadfile(filename))] = loadfile(filename);
    //int len = fread(buffer, 1, sizeof(buffer), fp);
    //    printf("%i",len);
    //fclose(fp);
    loadfile(filename);
    printf("%i",sizeof(buffer));
    //size_t strlen()
    //ssize_t bufsz = snprintf(NULL, 0, "%s",buffer);
    
    //= malloc(bufsz + 1);;
    ESP_LOGI("JSON","Step2");
    // parse the JSON data
    cJSON *json = cJSON_Parse(*buffer);
    int sizjson = sizeof(json);
    printf("%i",sizjson);
    ESP_LOGI("JSON","Step3");

    if (json == NULL) {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL) {
            printf("Error: %s\n", error_ptr);
        }
        cJSON_Delete(json);
    }
    int sizdstruct = sizeof(sdata);
    printf("%i",sizdstruct);
    //cJSON_Print(json);
    int count = 0;
    
    cJSON *iter;
    printf("%i", sizeof(*iter));

    int i = -1;
    
    cJSON_ArrayForEach(iter, json){
        ESP_LOGI("JSON","Step6");
        if(cJSON_IsNumber(cJSON_GetObjectItemCaseSensitive(iter,"valuex"))==true) {
            i++;
            ESP_LOGI("JSON","Step7");

            sdata[i].value1= cJSON_GetNumberValue((cJSON_GetObjectItemCaseSensitive(iter,"valuex")));
            sdata[i].value2= 34;
            sdata[i].value3=cJSON_GetNumberValue(cJSON_GetObjectItemCaseSensitive(iter,"valuei"));
        }

        cJSON_Delete(json);
    }

    for(int j=0 ; j<=i;j++) {
        printf("value1%i  = %i\n",j,sdata[j].value1);
        printf("value2%i  = %i\n",j,sdata[j].value2);
        printf("value3%i   = %i\n",j,sdata[j].value3);
    }
    
   //free(md);
}

That's the error what I get

[0;32mI (5325) gpio: GPIO[25]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 [0m
[0;32mI (5335) sdspi_transaction: cmd=52, R1 response: command not supported[0m
[0;32mI (5385) sdspi_transaction: cmd=5, R1 response: command not supported[0m
[0;32mI (5435) Test: Filesystem mounted[0m
64000[0;32mI (5665) JSON: Step2[0m
4[0;32mI (5685) JSON: Step3[0m
700[0;32mI (5685) JSON: Step4[0m
40[0;32mI (5685) JSON: Step5[0m
[0;32mI (5685) JSON: Step6[0m
[0;32mI (5685) JSON: Step7[0m
[0;32mI (5695) JSON: Step6[0m

assert failed: tlsf_free tlsf.c:1120 (!block_is_free(block) && "block already marked as free")

Testing should be possible with the following program

#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"

void app_main(void){
    jsonread();
}

This would be the relevant JSON data

[{"valuex":5434,"Bytes":1,"valuei": 58},
  {"valuex":61578,"Bytes":1,"valuei": 213},
 {"valuex":54343,"Bytes":1,"valuei": 23},
 {"valuex":23213,"Bytes":1,"valuei": 34},
 {"valuex":21313,"Bytes":1,"valuei": 5458},
  {"valuex":61258,"Bytes":2,"valuei": 5621}
]

How can I allocate the memory correctly for cJSON, sdata and buffer? Is there an easier json parser within esp32?

Upvotes: 1

Views: 217

Answers (0)

Related Questions