Help converting unit8_t * param to const char * for use in cJSON

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Help converting unit8_t * param to const char * for use in cJSON

Postby wegunterjr » Sat Sep 01, 2018 1:55 am

In the GATTS Write event, I get the param

Code: Select all

 case ESP_GATTS_WRITE_EVT:
                cJSON *root = cJSON_Parse((const char*)param->write.value);
                cJSON *firstValue = cJSON_GetObjectItem(root, "first");
                char *format = cJSON_GetObjectItem(firstValue, "super")->valuestring;
                char string = cJSON_Print(root);
                printf("fun is here %s \n", param->write.value);
                printf("fun is here %s \n", string );
                
The problem is that I must be doing something wrong with the pointer and causing the ESP32 to reboot.

Suggestions?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Help converting unit8_t * param to const char * for use in cJSON

Postby kolban » Sat Sep 01, 2018 4:06 am

What source statement is the failure occuring at? What is the error?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Re: Help converting unit8_t * param to const char * for use in cJSON

Postby wegunterjr » Sat Sep 01, 2018 4:22 am

It appears to be null, so when I put these checks in to check for NULL, it doesn't reboot.
Trying to recreate it now... but for now, here is the code sample:

Code: Select all

 cJSON *root = cJSON_Parse((const char*)param->write.value);
                if (root == NULL) {
		            fprintf(stderr, "Failed to parse JSON.");
                }
                else
                {

                    cJSON *firstValue = cJSON_GetObjectItem(root, "first");
                    char *format = cJSON_GetObjectItem(firstValue, "super")->valuestring;
                    char string = cJSON_Print(root);
                    if(string == NULL)
                    {
                        fprintf(stderr, "Failed to print JSON.");

                    }
                    else
                    {
                        printf("fun is here %s \n", param->write.value);
                        //printf("this is new %s \n", string);
                        cJSON_Delete(root);
                        free(string);
                    }
                }   

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Re: Help converting unit8_t * param to const char * for use in cJSON

Postby wegunterjr » Sat Sep 01, 2018 4:56 am

I send over a simple string: hello, and it reboots.

Here is the code that was running when it crashed.

Code: Select all

 cJSON *firstValue = cJSON_GetObjectItem(root, "first");
                char *format = cJSON_GetObjectItem(firstValue, "super")->valuestring;
                char *theData = cJSON_Print(root);
                printf("fun is here %s \n", param->write.value);
                printf("this is new %s \n", theData);
                cJSON_Delete(root);
                free(theData);

Code: Select all

␛[0;32mI (46577) GATTS_TABLE: GATT_WRITE_EVT, handle = 42, value len = 5, value :␛[0m
fun is here Hello
Guru Meditation Error: Core  0 panic'ed (LoadProhibited)
. Exception was unhandled.
Register dump:
PC      : 0x400014fd  PS      : 0x00060c30  A0      : 0x801617b0  A1      : 0x3ffb30a0
A2      : 0x00000000  A3      : 0xfffffffc  A4      : 0x000000ff  A5      : 0x0000ff00
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x3ffb3050
A10     : 0x00000000  A11     : 0xffffffff  A12     : 0x00000004  A13     : 0x00000001
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff

Backtrace: 0x400014fd:0x3ffb30a0 0x401617ad:0x3ffb30b0 0x4015728d:0x3ffb33c0 0x400d3c35:0x3ffb3410 0x400d3945:0x3ffb3450 0x400d7f3e:0x3ffb3480 0x400d8cd5:0x3ffb34a0 0x400d565a:0x3ffb34e0

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5520
ho 0 tail 12 room 4
load:0x40078000,len:0
load:0x40078000,len:13560
entry 0x400790a8
␛[0;32mI (30) boot: ESP-IDF 3.30003.0 2nd stage bootloader␛[0m
␛[0;32mI (30) boot: compile time 21:00:43␛[0m
␛[0;32mI (62) boot: Enabling RNG early entropy source...␛[0m
␛[0;32mI (62) boot: SPI Speed      : 40MHz␛[0m
␛[0;32mI (62) boot: SPI Mode       : DIO␛[0m
␛[0;32mI (64) boot: SPI Flash Size : 16MB␛[0m
␛[0;32mI (68) boot: Partition Table:␛[0m
␛[0;32mI (72) boot: ## Label            Usage          Type ST Offset   Length␛[0m
␛[0;32mI (79) boot:  0 nvs              WiFi data        01 02 00009000 00005000␛[0m
␛[0;32mI (87) boot:  1 otadata          OTA data         01 00 0000e000 00002000␛[0m
␛[0;32mI (94) boot:  2 app0             OTA app          00 10 00010000 001b0000␛[0m
␛[0;32mI (102) boot:  3 app1             OTA app          00 11 001c0000 001b0000␛[0m
␛[0;32mI (109) boot:  4 eeprom           Unknown data     01 99 00370000 00001000␛[0m
␛[0;32mI (117) boot:  5 spiffs           Unknown data     01 82 00371000 0008f000␛[0m
␛[0;32mI (124) boot: End of partition table␛[0m
␛[0;32mI (129) boot: No factory image, trying OTA 0␛[0m
␛[0;32mI (134) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x398a0 (235680) map␛[0m
␛[0;32mI (229) esp_image: segment 1: paddr=0x000498c8 vaddr=0x3ffc0000 size=0x02ef0 ( 12016) load␛[0m
␛[0;32mI (234) esp_image: segment 2: paddr=0x0004c7c0 vaddr=0x40080000 size=0x00400 (  1024) load␛[0m
␛[0;32mI (236) esp_image: segment 3: paddr=0x0004cbc8 vaddr=0x40080400 size=0x03448 ( 13384) load␛[0m
␛[0;32mI (250) esp_image: segment 4: paddr=0x00050018 vaddr=0x400d0018 size=0xc81f4 (819700) map␛[0m
␛[0;32mI (553) esp_image: segment 5: paddr=0x00118214 vaddr=0x40083848 size=0x0ad8c ( 44428) load␛[0m
␛[0;32mI (572) esp_image: segment 6: paddr=0x00122fa8 vaddr=0x400c0000 size=0x00000 (     0) load␛[0m
␛[0;32mI (581) boot: Loaded app from partition at offset 0x10000␛[0m
␛[0;32mI (581) boot: Disabling RNG early entropy source...␛[0m
␛[0;32mI (584) cpu_start: Pro cpu up.␛[0m
␛[0;32mI (588) cpu_start: Starting app cpu, entry point is 0x40081178␛[0m
␛[0;32mI (579) cpu_start: App cpu up.␛[0m
␛[0;32mI (598) heap_init: Initializing. RAM available for dynamic allocation:␛[0m
␛[0;32mI (605) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM␛[0m
␛[0;32mI (611) heap_init: At 3FFCF968 len 00010698 (65 KiB): DRAM␛[0m
␛[0;32mI (617) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM␛[0m
␛[0;32mI (624) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM␛[0m
␛[0;32mI (630) heap_init: At 4008E5D4 len 00011A2C (70 KiB): IRAM␛[0m
␛[0;32mI (636) cpu_start: Pro cpu start user code␛[0m
␛[0;32mI (319) cpu_start: Starting scheduler on PRO CPU.␛[0m
␛[0;32mI (0) cpu_start: Starting scheduler on APP CPU.␛[0m
␛[0;32mI (368) BTDM_INIT: BT controller compile version [1f3c6ae]
␛[0m
␛[0;32mI (368) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE␛[0m
␛[0;32mI (638) phy: phy_version: 3662, 711a97c, May  9 2018, 14:29:06, 0, 0␛[0m
heap size after gatts_event_handler = 176252 ...
heap size in the gatts idx = 176252 ...
heap size after gatts_event_handler = 174620 ...
heap size in the gatts idx = 174620 ...
␛[0;32mI (718) GATTS_TABLE: create attribute table successfully, the number handle = 8
␛[0m
␛[0;32mI (728) GATTS_TABLE: switch ␛[0m
␛[0;32mI (728) GATTS_TABLE: switch ␛[0m
heap size after gatts_event_handler = 174940 ...
heap size in the gatts idx = 174940 ...
␛[0;32mI (738) GATTS_TABLE: SERVICE_START_EVT, status 0, service_handle 40␛[0m
␛[0;32mI (748) GATTS_TABLE: switch ␛[0m
␛[0;32mI (748) GATTS_TABLE: advertising start successfully␛[0m
heap size after BLE 01 = 175088 ...
I (758) wifi: wifi firmware version: de47fad
I (758) wifi: config NVS flash: enabled
I (768) wifi: config nano formating: disabled
␛[0;32mI (768) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE␛[0m
␛[0;32mI (778) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE␛[0m
I (808) wifi: Init dynamic tx buffer num: 32
I (808) wifi: Init data frame dynamic rx buffer num: 32
I (808) wifi: Init management frame dynamic rx buffer num: 32
I (808) wifi: wifi driver task: 3ffd8918, prio:23, stack:3584
I (818) wifi: Init static rx buffer num: 10
I (818) wifi: Init dynamic rx buffer num: 32
I (828) wifi: wifi power manager task: 0x3ffdd628 prio: 21 stack: 2560
␛[0;32mI (828) subpub: Setting WiFi configuration SSID CC-Guest...␛[0m
I (848) wifi: mode : sta (b4:e6:2d:94:d5:89)
heap size after WIFI 02 = 138064 ...
heap size after AWS 04 = 128224 ...
␛[0;32mI (848) subpub: AWS IoT SDK Version 2.1.1-␛[0m
heap size after init MQTT 03 = 133772 ...

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Help converting unit8_t * param to const char * for use in cJSON

Postby loboris » Sat Sep 01, 2018 6:57 am

char string = cJSON_Print(root); -> char *string = cJSON_Print(root);

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Re: Help converting unit8_t * param to const char * for use in cJSON

Postby wegunterjr » Sat Sep 01, 2018 3:03 pm

I made the suggested change to use the char string* = cJSON_Print(root);

and Put the NULL checks back in. It seems to not be NULL.

Who is online

Users browsing this forum: Bing [Bot] and 146 guests