Ejemplo n.º 1
0
int json_return(evhtp_request_t *req, int err_no, const char *md5sum, int post_size)
{
    //json sample:
    //{"ret":true,"info":{"size":"1024", "md5":"edac35fd4b0059d3218f0630bc56a6f4"}}
    //{"ret":false,"error":{"code":"1","message":"\u9a8c\u8bc1\u5931\u8d25"}}
    cJSON *j_ret = cJSON_CreateObject();
    cJSON *j_ret_info = cJSON_CreateObject();
    if(err_no == -1)
    {
        cJSON_AddBoolToObject(j_ret, "ret", 1);
        cJSON_AddStringToObject(j_ret_info, "md5", md5sum);
        cJSON_AddNumberToObject(j_ret_info, "size", post_size);
        cJSON_AddItemToObject(j_ret, "info", j_ret_info);
    }
    else
    {
        cJSON_AddBoolToObject(j_ret, "ret", 0);
        cJSON_AddNumberToObject(j_ret_info, "code", err_no);
        LOG_PRINT(LOG_DEBUG, "post_error_list[%d]: %s", err_no, post_error_list[err_no]);
        cJSON_AddStringToObject(j_ret_info, "message", post_error_list[err_no]);
        cJSON_AddItemToObject(j_ret, "error", j_ret_info);
    }
    char *ret_str_unformat = cJSON_PrintUnformatted(j_ret);
    LOG_PRINT(LOG_DEBUG, "ret_str_unformat: %s", ret_str_unformat);
    evbuffer_add_printf(req->buffer_out, "%s", ret_str_unformat);
    evhtp_headers_add_header(req->headers_out, evhtp_header_new("Content-Type", "application/json", 0, 0));
    cJSON_Delete(j_ret);
    free(ret_str_unformat);
    return 0;
}
Ejemplo n.º 2
0
static void cjson_add_bool_should_fail_with_null_pointers(void)
{
    cJSON *root = cJSON_CreateObject();

    TEST_ASSERT_NULL(cJSON_AddBoolToObject(NULL, "false", false));
    TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, NULL, false));

    cJSON_Delete(root);
}
Ejemplo n.º 3
0
/**
* @brief add_info Added image info to the request
*
* @param im The image struct
* @param req The evhtp request
*/
void add_info(MagickWand *im, evhtp_request_t *req)
{
    MagickSizeType size = MagickGetImageSize(im);
    unsigned long width = MagickGetImageWidth(im);
    unsigned long height = MagickGetImageHeight(im);
    size_t quality = MagickGetImageCompressionQuality(im);
    quality = (quality == 0 ? 100 : quality);
    char *format = MagickGetImageFormat(im);

    //{"ret":true,"info":{"size":195135,"width":720,"height":480,"quality":75,"format":"JPEG"}}
    cJSON *j_ret = cJSON_CreateObject();
    cJSON *j_ret_info = cJSON_CreateObject();
    cJSON_AddBoolToObject(j_ret, "ret", 1);
    cJSON_AddNumberToObject(j_ret_info, "size", size);
    cJSON_AddNumberToObject(j_ret_info, "width", width);
    cJSON_AddNumberToObject(j_ret_info, "height", height);
    cJSON_AddNumberToObject(j_ret_info, "quality", quality);
    cJSON_AddStringToObject(j_ret_info, "format", format);
    cJSON_AddItemToObject(j_ret, "info", j_ret_info);
    char *ret_str_unformat = cJSON_PrintUnformatted(j_ret);
    LOG_PRINT(LOG_DEBUG, "ret_str_unformat: %s", ret_str_unformat);
    evbuffer_add_printf(req->buffer_out, "%s", ret_str_unformat);
    cJSON_Delete(j_ret);
    free(ret_str_unformat);
    free(format);
}
Ejemplo n.º 4
0
int http_wifi_api_get_status(http_connection *c)
{
    CGI_WIFI_DBG("wifi get_status\n");

    //wait for whole body
    if(c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    api_cgi_status * status = c->cgi.data;

    //first call, send headers
    if(status==NULL)
    {
        status = (api_cgi_status*)os_malloc(sizeof(api_cgi_status));
        status->state=1;
        c->cgi.data=status;

        http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
        http_response_OK(c);

        return HTTPD_CGI_MORE;
    }
    else if(status->state==1)
    {
        wifi_station_get_config(&wifi_status.station_config);
        uint8_t c_status = wifi_station_get_connect_status();

        //return JSON
        cJSON *root = cJSON_CreateObject();
        cJSON_AddBoolToObject(root,"scanning",wifi_status.scanning);
        cJSON_AddStringToObject(root,"ssid",(const char *)wifi_status.station_config.ssid);
        cJSON_AddNumberToObject(root,"mode",wifi_get_opmode());
        cJSON_AddNumberToObject(root,"station_status",c_status);

        //got ip
        if(c_status==5)
        {
            struct ip_info ip;
            wifi_get_ip_info(0x0,&ip);
            char *ip_str = (char*)ipaddr_ntoa(&ip.ip);
            cJSON_AddStringToObject(root,"ip",ip_str);
        }
        else {
            cJSON_AddStringToObject(root,"ip","");
        }

        http_write_json(c,root);
        cJSON_Delete(root);

        status->state=99;
        return HTTPD_CGI_MORE;
    }
    else
    {
        os_free(c->cgi.data);
        return HTTPD_CGI_DONE;
    }
}
Ejemplo n.º 5
0
static cJSON * cJSON_CreateRcvyCapabilityInfo(rcvy_capability_t *pRcvyCapability)
{	
	cJSON * pRcvyCapabilityInfoItem = NULL;

	if(pRcvyCapability) {
		cJSON * pRcvyCapabilityArrayItem = NULL;
		//Array
		cJSON * pRcvyCapabilityArray = cJSON_CreateArray();		
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_IS_INSTALLED, "bv", \
			&pRcvyCapability->rcvy_status.isInstalled));
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_IS_ACTIVATED, "bv", \
			&pRcvyCapability->rcvy_status.isActivated));
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_IS_EXPIRED, "bv", \
			&pRcvyCapability->rcvy_status.isExpired));
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_IS_EXIST_ASZ, "bv", \
			&pRcvyCapability->rcvy_status.isExistASZ));
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_IS_NEWERVER, "bv", \
			&pRcvyCapability->rcvy_status.isExistNewerVer));
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_IS_ACRREADY, "bv", \
			&pRcvyCapability->rcvy_status.isAcrReady));
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_VERSION, "sv", \
			pRcvyCapability->rcvy_status.version));//rcvy_status.version is an address of string array
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_LATEST_BK_TIME, "sv", \
			pRcvyCapability->rcvy_status.lastBackupTime));//Same as above
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_ACTION_MSG, "sv", \
			pRcvyCapability->rcvy_status.actionMsg));//Same as above
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem(RCVY_STATUS_LWARNING_MSG, "sv", \
			pRcvyCapability->rcvy_status.lastWarningMsg));//Same as above
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem("functionList", "sv", \
			pRcvyCapability->pRcvyFuncList));//pRcvyCapability->pRcvyFuncList is a pointer and point to string
		cJSON_AddItemToArray(pRcvyCapabilityArray, \
			cJSON_CreateRcvyCapabilityItem("functionCode", "v", \
			&pRcvyCapability->rcvyFuncCode));
		//add array to array item
		pRcvyCapabilityArrayItem = cJSON_CreateObject();
		cJSON_AddItemToObject(pRcvyCapabilityArrayItem, "e", pRcvyCapabilityArray);

		//add 'non-sensor data flag'	
		cJSON_AddStringToObject(pRcvyCapabilityArrayItem, "bn", "Information");
		cJSON_AddBoolToObject(pRcvyCapabilityArrayItem, "nonSensorData", 1);

		//add array item to "Information" item
		pRcvyCapabilityInfoItem = cJSON_CreateObject();
		cJSON_AddItemToObject(pRcvyCapabilityInfoItem, "Information", pRcvyCapabilityArrayItem);
	}
	return pRcvyCapabilityInfoItem;
}
Ejemplo n.º 6
0
/**
*TASK:based on the gesture, create the command and control the lamps
*stateType:ON_OFF_TYPE, BRI_UP_TYPE, BRI_DOWN_TYPE, HUE_UP_TYPE, HUE_DOWN_TYPE
*target:the number of  lamp, start from 0
*/
bool createCommand(int stateType, int target)
{
    cJSON * pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot)
    {
        //error happend here
        printf("error when create json");
        return false;
    }

    switch(stateType)
    {
    case ON_TYPE:
    {
        onValue[target - 1] = (onValue[target - 1] == true ? false : true);
        cJSON_AddBoolToObject(pJsonRoot, ON_STRING, onValue[target - 1]);
        break;
    }
    case BRI_UP_TYPE:
    {
        int value = (briValue[target - 1] + BRI_VALUE_UP) > 254 ? 254 : (briValue[target - 1]
                    + BRI_VALUE_UP < 0 ? 0 : briValue[target - 1] + BRI_VALUE_UP);
        briValue[target - 1] = value;
        cJSON_AddNumberToObject(pJsonRoot, BRI_STRING, value);
        break;
    }
    case BRI_DOWN_TYPE:
    {
        int value = (briValue[target - 1] + BRI_VALUE_DOWN) > 254 ? 254 : (briValue[target - 1]
                    + BRI_VALUE_DOWN < 0 ? 0 : briValue[target - 1] + BRI_VALUE_DOWN);
        briValue[target - 1] = value;
        cJSON_AddNumberToObject(pJsonRoot, BRI_STRING, value);
        break;
    }
    case HUE_UP_TYPE:
    {
        int value = (hueValue[target - 1] + HUE_VALUE_UP);
        value = value < 0 ? 65535 + value : value % 65535;
        hueValue[target - 1] = value;
        cJSON_AddNumberToObject(pJsonRoot, HUE_STRING, value);
        break;
    }
    case HUE_DOWN_TYPE:
    {
        int value = (hueValue[target - 1] + HUE_VALUE_DOWN);
        value = value < 0 ? 65535 + value : value % 65535;
        hueValue[target - 1] = value;
        cJSON_AddNumberToObject(pJsonRoot, HUE_STRING, value);
        break;
    }
    }

    printf("%s",cJSON_Print(pJsonRoot));
    setLightState(target, cJSON_Print(pJsonRoot));
    cJSON_Delete(pJsonRoot);
}
Ejemplo n.º 7
0
static void cjson_add_bool_should_add_bool(void)
{
    cJSON *root = cJSON_CreateObject();
    cJSON *true_item = NULL;
    cJSON *false_item = NULL;

    /* true */
    cJSON_AddBoolToObject(root, "true", true);
    TEST_ASSERT_NOT_NULL(true_item = cJSON_GetObjectItemCaseSensitive(root, "true"));
    TEST_ASSERT_EQUAL_INT(true_item->type, cJSON_True);

    /* false */
    cJSON_AddBoolToObject(root, "false", false);
    TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
    TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);

    cJSON_Delete(root);
}
Ejemplo n.º 8
0
char * BinToPstatJSON(const OicSecPstat_t * pstat, const bool isIncResName)
{
    if(NULL == pstat)
    {
        return NULL;
    }

    cJSON *jsonPstat = NULL;
    char *jsonStr = NULL;
    cJSON *jsonSmArray = NULL;
    char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*) 0)->id)) + 1] = {};
    uint32_t outLen = 0;
    B64Result b64Ret = B64_OK;
    cJSON *jsonRoot = NULL;

    if(isIncResName)
    {
        jsonRoot = cJSON_CreateObject();
        VERIFY_NON_NULL(TAG, jsonRoot, INFO);
        cJSON_AddItemToObject(jsonRoot, OIC_JSON_PSTAT_NAME, jsonPstat = cJSON_CreateObject());
    }
    else
    {
        jsonPstat = cJSON_CreateObject();
        jsonRoot = jsonPstat;
    }
    VERIFY_NON_NULL(TAG, jsonPstat, INFO);

    cJSON_AddBoolToObject(jsonPstat, OIC_JSON_ISOP_NAME, pstat->isOp);

    b64Ret = b64Encode(pstat->deviceID.id,
            sizeof(pstat->deviceID.id), base64Buff, sizeof(base64Buff), &outLen);
    VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);

    cJSON_AddStringToObject(jsonPstat, OIC_JSON_DEVICE_ID_NAME, base64Buff);
    cJSON_AddNumberToObject(jsonPstat, OIC_JSON_COMMIT_HASH_NAME, pstat->commitHash);
    cJSON_AddNumberToObject(jsonPstat, OIC_JSON_CM_NAME, (int)pstat->cm);
    cJSON_AddNumberToObject(jsonPstat, OIC_JSON_TM_NAME, (int)pstat->tm);
    cJSON_AddNumberToObject(jsonPstat, OIC_JSON_OM_NAME, (int)pstat->om);

    cJSON_AddItemToObject(jsonPstat, OIC_JSON_SM_NAME, jsonSmArray = cJSON_CreateArray());
    VERIFY_NON_NULL(TAG, jsonSmArray, INFO);
    for (size_t i = 0; i < pstat->smLen; i++)
    {
        cJSON_AddItemToArray(jsonSmArray, cJSON_CreateNumber((int )pstat->sm[i]));
    }
    jsonStr = cJSON_Print(jsonRoot);

exit:
    if (jsonRoot)
    {
        cJSON_Delete(jsonRoot);
    }
    return jsonStr;
}
Ejemplo n.º 9
0
static void cjson_add_bool_should_fail_on_allocation_failure(void)
{
    cJSON *root = cJSON_CreateObject();

    cJSON_InitHooks(&failing_hooks);

    TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, "false", false));

    cJSON_InitHooks(NULL);

    cJSON_Delete(root);
}
Ejemplo n.º 10
0
static cJSON * cJSON_CreatePrttCapabilityInfo(prtt_capability_t *pPrttCapability)
{	
	cJSON * pPrttCapabilityInfoItem = NULL;

	if(pPrttCapability) {
		cJSON * pPrttCapabilityArrayItem = NULL;
		//Array
		cJSON * pPrttCapabilityArray = cJSON_CreateArray();		
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_IS_INSTALLED, "bv", \
			&pPrttCapability->prtt_status.isInstalled));
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_IS_ACTIVATED, "bv", \
			&pPrttCapability->prtt_status.isActivated));
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_IS_EXPIRED, "bv", \
			&pPrttCapability->prtt_status.isExpired));
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_IS_PROTECTION, "bv", \
			&pPrttCapability->prtt_status.isProtection));
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_IS_NEWERVER, "bv", \
			&pPrttCapability->prtt_status.isExistNewerVer));
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_VERSION, "sv", \
			pPrttCapability->prtt_status.version));//prtt_status.version is an address of string array
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_ACTION_MSG, "sv", \
			pPrttCapability->prtt_status.actionMsg));//Same as above
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem(PRTT_STATUS_LWARNING_MSG, "sv", \
			pPrttCapability->prtt_status.lastWarningMsg));//Same as above
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem("functionList", "sv", \
			pPrttCapability->pPrttFuncList));//pPrttCapability->pPrttFuncList is a pointer and point to string
		cJSON_AddItemToArray(pPrttCapabilityArray, \
			cJSON_CreatePrttCapabilityItem("functionCode", "v", \
			&pPrttCapability->prttFuncCode));
		//add array to array item
		pPrttCapabilityArrayItem = cJSON_CreateObject();
		cJSON_AddItemToObject(pPrttCapabilityArrayItem, "e", pPrttCapabilityArray);

		//add 'non-sensor data flag'	
		cJSON_AddStringToObject(pPrttCapabilityArrayItem, "bn", "Information");
		cJSON_AddBoolToObject(pPrttCapabilityArrayItem, "nonSensorData", 1);

		//add array item to "Information" item
		pPrttCapabilityInfoItem = cJSON_CreateObject();
		cJSON_AddItemToObject(pPrttCapabilityInfoItem, "Information", pPrttCapabilityArrayItem);
	}
	return pPrttCapabilityInfoItem;
}
Ejemplo n.º 11
0
int Parser_CreateCapabilityRep(char flagCode, char ** outJsonStr)
{
	int len = 0;
	cJSON * root = NULL;
	cJSON * parent = NULL;
	cJSON * child = NULL;
	if (NULL == outJsonStr) return len;

	root = cJSON_CreateObject();
	parent = root;   child = cJSON_CreateObject();  cJSON_AddItemToObject(parent, MyTopic, child);
	parent = child;  child = cJSON_CreateObject();  cJSON_AddItemToObject(parent, SCREENSHOT_INFOMATION, child);
	parent = child;  child = cJSON_CreateArray();   cJSON_AddItemToObject(parent, "e", child);
	cJSON_AddStringToObject(parent, "bn", SCREENSHOT_INFOMATION);
	cJSON_AddBoolToObject(parent, "nonSensorData", 1);

	parent = child;  child = NULL;
	switch (flagCode)
	{
	case SCREENSHOT_FLAGCODE_NONE:
		child = cJSON_CreateObject();
		cJSON_AddStringToObject(child, "n", SCREENSHOT_FUNCTION_LIST);
		cJSON_AddStringToObject(child, "sv", SCREENSHOT_FLAG_NONE);
		cJSON_AddItemToArray(parent, child);

		child = cJSON_CreateObject();
		cJSON_AddStringToObject(child, "n", SCREENSHOT_FUNCTION_CODE);
		cJSON_AddNumberToObject(child, "v", SCREENSHOT_FLAGCODE_NONE);
		cJSON_AddItemToArray(parent, child);
		break;
	
	default:
	case SCREENSHOT_FLAGCODE_INTERNAL:
		child = cJSON_CreateObject();
		cJSON_AddStringToObject(child, "n", SCREENSHOT_FUNCTION_LIST);
		cJSON_AddStringToObject(child, "sv", SCREENSHOT_FLAG_INTERNAL);
		cJSON_AddItemToArray(parent, child);

		child = cJSON_CreateObject();
		cJSON_AddStringToObject(child, "n", SCREENSHOT_FUNCTION_CODE);
		cJSON_AddNumberToObject(child, "v", SCREENSHOT_FLAGCODE_INTERNAL);
		cJSON_AddItemToArray(parent, child);
		break;
	}

	*outJsonStr = cJSON_PrintUnformatted(root);
	if (*outJsonStr) len = strlen(*outJsonStr);
	else             len = 0;
	cJSON_Delete(root);
	return len;	
}
Ejemplo n.º 12
0
struct cJSON *
treasure_map_create_json_object(struct treasure_map *treasure_map)
{
    struct cJSON *json_object = cJSON_CreateObject();
    cJSON_AddStringToObject(json_object, "struct", "treasure_map");
    cJSON_AddNumberToObject(json_object, "rev", 0);
    cJSON_AddBoolToObject(json_object, "is_false", treasure_map->is_false);

    struct cJSON *treasure = treasure_create_json_object(&treasure_map->treasure);
    cJSON_AddItemToObject(json_object, "treasure", treasure);

    cJSON_AddStringToObject(json_object, "true_description", treasure_map->true_description);

    return json_object;
}
Ejemplo n.º 13
0
static cJSON * cJSON_CreatePrttCapabilityItem(const char *itemName, const char *valueType, const void *value)
{
	cJSON * pPrttCapabilityItem = NULL;
	if (itemName && valueType && value) {
		pPrttCapabilityItem = cJSON_CreateObject();
		cJSON_AddStringToObject(pPrttCapabilityItem, "n", itemName);
		if (!strncmp(valueType, "bv", 2)) {
			cJSON_AddBoolToObject(pPrttCapabilityItem, "bv", *(int *)value);
		} else if (!strncmp(valueType, "sv", 2)) {
			cJSON_AddStringToObject(pPrttCapabilityItem, "sv", (const char *)value);//must be careful
		} else {
			cJSON_AddNumberToObject(pPrttCapabilityItem, "v", *(int *)value);
		}
	}
	return pPrttCapabilityItem;
}
Ejemplo n.º 14
0
char * BinToDoxmJSON(const OicSecDoxm_t * doxm)
{
    if (NULL == doxm)
    {
        return NULL;
    }

    char *jsonStr = NULL;
    cJSON *jsonDoxm = NULL;
    char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {};
    uint32_t outLen = 0;
    B64Result b64Ret = B64_OK;

    cJSON *jsonRoot = cJSON_CreateObject();
    VERIFY_NON_NULL(TAG, jsonRoot, ERROR);

    jsonDoxm = cJSON_CreateObject();
    VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
    cJSON_AddItemToObject(jsonRoot, OIC_JSON_DOXM_NAME, jsonDoxm );

    //OxmType -- Not Mandatory
    if(doxm->oxmTypeLen > 0)
    {
        cJSON *jsonOxmTyArray = cJSON_CreateArray();
        VERIFY_NON_NULL(TAG, jsonOxmTyArray, ERROR);
        cJSON_AddItemToObject (jsonDoxm, OIC_JSON_OXM_TYPE_NAME, jsonOxmTyArray );
        for (size_t i = 0; i < doxm->oxmTypeLen; i++)
        {
            cJSON_AddItemToArray (jsonOxmTyArray, cJSON_CreateString(doxm->oxmType[i]));
        }
    }

    //Oxm -- Not Mandatory
    if(doxm->oxmLen > 0)
    {
        cJSON *jsonOxmArray = cJSON_CreateArray();
        VERIFY_NON_NULL(TAG, jsonOxmArray, ERROR);
        cJSON_AddItemToObject (jsonDoxm, OIC_JSON_OXM_NAME,jsonOxmArray );
        for (size_t i = 0; i < doxm->oxmLen; i++)
        {
            cJSON_AddItemToArray (jsonOxmArray, cJSON_CreateNumber(doxm->oxm[i]));
        }
    }

    //OxmSel -- Mandatory
    cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_OXM_SEL_NAME, (int)doxm->oxmSel);

    //sct -- Mandatory
    cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME, (int)doxm->sct);

    //Owned -- Mandatory
    cJSON_AddBoolToObject(jsonDoxm, OIC_JSON_OWNED_NAME, doxm->owned);

    //TODO: Need more clarification on deviceIDFormat field type.
#if 0
    //DeviceIdFormat -- Mandatory
    cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_DEVICE_ID_FORMAT_NAME, doxm->deviceIDFormat);
#endif

    //DeviceId -- Mandatory
    outLen = 0;
    b64Ret = b64Encode(doxm->deviceID.id, sizeof(doxm->deviceID.id), base64Buff,
                    sizeof(base64Buff), &outLen);
    VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
    cJSON_AddStringToObject(jsonDoxm, OIC_JSON_DEVICE_ID_NAME, base64Buff);

    //DPC -- Mandatory
    cJSON_AddBoolToObject(jsonDoxm, OIC_JSON_DPC_NAME, doxm->dpc);

    //Owner -- Mandatory
    outLen = 0;
    b64Ret = b64Encode(doxm->owner.id, sizeof(doxm->owner.id), base64Buff,
                    sizeof(base64Buff), &outLen);
    VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
    cJSON_AddStringToObject(jsonDoxm, OIC_JSON_OWNER_NAME, base64Buff);

    jsonStr = cJSON_PrintUnformatted(jsonRoot);

exit:
    if (jsonRoot)
    {
        cJSON_Delete(jsonRoot);
    }
    return jsonStr;
}
Ejemplo n.º 15
0
cJSON* CHPRepPayloadToJson(OCRepPayload* repData)
{
    cJSON *outJson = cJSON_CreateObject();
    if (outJson == NULL)
    {
        return NULL;
    }

    OCRepPayloadValue* val = repData->values;
    while (val)
    {
        switch (val->type)
        {
            case OCREP_PROP_NULL:
                break;
            case OCREP_PROP_INT:
                OIC_LOG_V(DEBUG, TAG, "%s(int):%d", val->name, (int)val->i);
                cJSON_AddNumberToObject(outJson,val->name,(int)val->i);
                break;
            case OCREP_PROP_DOUBLE:
                OIC_LOG_V(DEBUG, TAG, "%s(double):%f", val->name, val->d);
                cJSON_AddNumberToObject(outJson,val->name,val->d);
                break;
            case OCREP_PROP_BOOL:
                OIC_LOG_V(DEBUG, TAG, "%s(bool):%s", val->name, val->b ? "true" : "false");
                cJSON_AddBoolToObject(outJson,val->name,val->b);
                break;
            case OCREP_PROP_STRING:
                OIC_LOG_V(DEBUG, TAG, "%s(string):%s", val->name, val->str);
                cJSON_AddStringToObject(outJson,val->name,val->str);
                break;
            case OCREP_PROP_OBJECT:
            {
                cJSON *objJson = CHPRepPayloadToJson(val->obj);
                if (objJson != NULL)
                {
                    cJSON_AddItemToObject(outJson,val->name,objJson);
                }
                break;
            }
            case OCREP_PROP_ARRAY:
            {
                unsigned int i = 0;
                int arraySize = (int)val->arr.dimensions[0];
                switch (val->arr.type)
                {
                    case OCREP_PROP_INT:
                        OIC_LOG_V(DEBUG, TAG, "%s(int array)", val->name);
                        if (arraySize > 0)
                        {
                            int castVal[val->arr.dimensions[0]];
                            for (i = 0 ; i < (unsigned int)arraySize ; i++)
                            {
                                castVal[i] = (int)val->arr.iArray[i];
                            }
                            cJSON *array = cJSON_CreateIntArray(castVal,arraySize);
                            if (array != NULL)
                            {
                                cJSON_AddItemToObject(outJson,val->name,array);
                            }
                        }
                        break;
                    case OCREP_PROP_DOUBLE:
                        OIC_LOG_V(DEBUG, TAG, "%s(double array)", val->name);
                        if (arraySize > 0)
                        {
                            cJSON *array = cJSON_CreateDoubleArray(val->arr.dArray,arraySize);
                            if (array != NULL)
                            {
                                cJSON_AddItemToObject(outJson,val->name,array);
                            }
                        }
                        break;
                    case OCREP_PROP_STRING:
                        OIC_LOG_V(DEBUG, TAG, "%s(string array)", val->name);
                        if (arraySize > 0)
                        {
                            cJSON *array = cJSON_CreateStringArray((const char**)val->arr.strArray,
                                                                   arraySize);
                            if (array != NULL)
                            {
                                cJSON_AddItemToObject(outJson,val->name,array);
                            }
                        }
                        break;
                    case OCREP_PROP_OBJECT:
                        if (arraySize > 0)
                        {
                            cJSON *arrayJson = cJSON_CreateArray();
                            for (i = 0 ; i < (unsigned int)arraySize ; i++)
                            {
                                cJSON *objJson = CHPRepPayloadToJson(val->arr.objArray[i]);
                                if (objJson != NULL && arrayJson != NULL)
                                {
                                    cJSON_AddItemToArray(arrayJson, objJson);
                                }
                            }
                            if (arrayJson != NULL)
                            {
                                cJSON_AddItemToObject(outJson,val->name,arrayJson);
                            }
                        }
                        break;
                    case OCREP_PROP_BOOL:
                        //TODO : Not support - cJSON_CreateBoolArray
                        break;
                    default:
                        OIC_LOG_V(ERROR, TAG, "Unknown/unsupported array type: %s", val->name);
                        break;
                }
                break;
            }
            default:
                OIC_LOG_V(ERROR, TAG, "Unknown type: %s", val->name);
                break;
        }
        val = val->next;
    }

    if( repData->values != NULL)
    {
        return outJson;
    }
    else
    {
        cJSON_Delete(outJson);
        return NULL;
    }
}