jansson icon indicating copy to clipboard operation
jansson copied to clipboard

json_dumps() problem in Jansson(v2.7.0) and ucos-II(v2.9.1) (keil 5 and stm32)

Open chenlinee opened this issue 6 years ago • 3 comments

I use the Jansson pack(v2.7.0) in keil5( v5.23.0.0 ). The hardware I use is stm32F103ZET6.

When I create a project without ucos-II(Embedded operating system), jansson works well. But jansson in ucos-II task, there is some problem of json_dumps() when integer is in Json.

 int main(void)
{	
    //hardware init
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    delay_init();
    LED_Init();
    uart_init(115200);
    /*************************************************************************************************
    *
    *                           before enter ucos2
    *
    **************************************************************************************************
    */
    {
        char a[] = "{\"id\":31,\"dataCode\":0,\"data\":{\"ap_sta_mode\": 1,\"physical_equipment_id\": 1,\"ssid\":\"wifi_name\",\"passwd\":\"wifi_password\",\"ip\":\"192.168.16.123\"}}";
        json_t *json_data;
        json_error_t json_error;
        json_data = json_loads((char*)a, JSON_DECODE_ANY, &json_error);
        
        char *out;
        out = json_dumps(json_data, JSON_ENCODE_ANY);
        printf("\r\n before enter ucos2 \r\n out:\r\n%s\r\n\r\n", out);
        free(out);
        json_decref(json_data);
    }
    
    //enter ucos-II
    OSInit();   
    OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO );//creat start task
    OSStart();	  	 
}

//start task
//create other tasks
void start_task(void *pdata)
{
    OS_CPU_SR cpu_sr=0;
	pdata = pdata; 
  	OS_ENTER_CRITICAL();			//ENTER_CRITICAL   
 	OSTaskCreate(led0_task,(void *)0,(OS_STK*)&LED0_TASK_STK[LED0_STK_SIZE-1],LED0_TASK_PRIO);						   
 	OSTaskCreate(led1_task,(void *)0,(OS_STK*)&LED1_TASK_STK[LED1_STK_SIZE-1],LED1_TASK_PRIO);	 				   
	OSTaskSuspend(START_TASK_PRIO);	//suspend start_task
	OS_EXIT_CRITICAL();				//EXIT_CRITICAL
}

//LED0 task
void led0_task(void *pdata)
{	 	
    /*************************************************************************************************
    *
    *                           after enter ucos2
    *
    **************************************************************************************************
    */
    {
        char a[] = "{\"id\":31,\"dataCode\":0,\"data\":{\"ap_sta_mode\": 1,\"physical_equipment_id\": 1,\"ssid\":\"wifi_name\",\"passwd\":\"wifi_password\",\"ip\":\"192.168.16.123\"}}";
        json_t *json_data;
        json_error_t json_error;
        json_data = json_loads((char*)a, JSON_DECODE_ANY, &json_error);
        
        char *out;
        out = json_dumps(json_data, JSON_ENCODE_ANY);
        printf("\r\n after enter ucos2 \r\n out:\r\n%s\r\n\r\n", out);
        free(out);
        json_decref(json_data);
    }
	while(1)
	{
		LED0=0;
		delay_ms(80);
		LED0=1;
		delay_ms(920);
	};
}

The output is like that

 before enter ucos2 
 out:
{"id": 31, "dataCode": 0, "data": {"ip": "192.168.16.123", "ssid": "wifi_name", "ap_sta_mode": 1, "physical_equipment_id": 1, "passwd": "wifi_password"}}


 after enter ucos2 
 out:
{"id": 133278215761, "dataCode": 134229585, "data": {"ip": "192.168.16.123", "ssid": "wifi_name", "ap_sta_mode": 4429196881, "physical_equipment_id": 4429196881, "passwd": "wifi_password"}}

chenlinee avatar Aug 13 '19 15:08 chenlinee

I also have adjusted the memory size of HEAP_SIZE and STACK_SIZE, make sure they are big enough for Jansson.

; Amount of memory (in bytes) allocated for Stack
; Tailor this value to your application needs
; <h> Stack Configuration
;   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Stack_Size      EQU     0x00004000

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp
                                                  
; <h> Heap Configuration
;   <o>  Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Heap_Size       EQU     0x00004000

chenlinee avatar Aug 13 '19 15:08 chenlinee

Can you test latest jansson? jansson-2.7.0 is about 5 years old, a very large number of fixes have been implemented since then.

coreyfarrell avatar Aug 13 '19 15:08 coreyfarrell

Some problem here. uVision should add to latest version of jansson to own library.

hexvalid avatar Apr 14 '20 13:04 hexvalid