thincloud.h
IoT_Error_t command_response(char *buffer, const char *requestId, uint16_t statusCode, bool isErrorResponse, char *errorMessage, json_object *body)
{
    json_object *obj = json_object_new_object();
    if (requestId != NULL)
    {
        json_object *value = json_object_new_string(requestId);
        json_object_object_add(obj, "id", value);
    }

    json_object *statusCodeValue = json_object_new_int(statusCode);

    if (isErrorResponse)
    {
        json_object *error = json_object_new_object();
        json_object_object_add(error, "statusCode", statusCodeValue);
        if (errorMessage != NULL)
        {
            json_object *errorMessageValue = json_object_new_string(errorMessage);
            json_object_object_add(error, "message", errorMessageValue);
        }

        json_object_object_add(obj, "error", error);
    }
    else
    {
        json_object *result = json_object_new_object();
        json_object_object_add(result, "statusCode", statusCodeValue);

        if (body != NULL)
        {
            json_object_object_add(result, "body", body);
        }

        json_object_object_add(obj, "result", result);
    }

    const char *str = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN);

    strcpy(buffer, str);

    json_object_put(obj);

    FUNC_EXIT_RC(SUCCESS);
}
/**
 * @brief Marshal a command response
 * 
 * Marshal a command response into a JSON string.
 * 
 * @param[out]  buffer           Pointer to a string buffer to write to.
 * @param[in]   requestId        Command request's ID.
 * @param[in]   statusCode       Command's response status.
 * @param[in]   isErrorResponse  Signals if a command responds with an error.
 * @param[in]   errorMessage     Response error message. Only used if isErrorResponse is set to true.
 * @param[in]   body             Command response body.
 * 
 * @return Zero on success, a negative value otherwise 
 */
@brief Marshal a command response

Marshal a command response into a JSON string.

@param[out]  buffer           Pointer to a string buffer to write to.
@param[in]   requestId        Command request's ID.
@param[in]   statusCode       Command's response status.
@param[in]   isErrorResponse  Signals if a command responds with an error.
@param[in]   errorMessage     Response error message. Only used if isErrorResponse is set to true.
@param[in]   body             Command response body.

@return Zero on success, a negative value otherwise
@brief Marshal a command response Marshal a command response into a JSON string . @param [ out ] buffer Pointer to a string buffer to write to . @param [ in ] requestId Command request ' s ID . @param [ in ] statusCode Command ' s response status . @param [ in ] isErrorResponse Signals if a command responds with an error . @param [ in ] errorMessage Response error message . Only used if isErrorResponse is set to true . @param [ in ] body Command response body . @return Zero on success a negative value otherwise
IoT_Error_t command_response(char *buffer, const char *requestId, uint16_t statusCode, bool isErrorResponse, char *errorMessage, json_object *body)
{
    json_object *obj = json_object_new_object();
    if (requestId != NULL)
    {
        json_object *value = json_object_new_string(requestId);
        json_object_object_add(obj, "id", value);
    }
    json_object *statusCodeValue = json_object_new_int(statusCode);
    if (isErrorResponse)
    {
        json_object *error = json_object_new_object();
        json_object_object_add(error, "statusCode", statusCodeValue);
        if (errorMessage != NULL)
        {
            json_object *errorMessageValue = json_object_new_string(errorMessage);
            json_object_object_add(error, "message", errorMessageValue);
        }
        json_object_object_add(obj, "error", error);
    }
    else
    {
        json_object *result = json_object_new_object();
        json_object_object_add(result, "statusCode", statusCodeValue);
        if (body != NULL)
        {
            json_object_object_add(result, "body", body);
        }
        json_object_object_add(obj, "result", result);
    }
    const char *str = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN);
    strcpy(buffer, str);
    json_object_put(obj);
    FUNC_EXIT_RC(SUCCESS);
}
IoT_Error_t command_response ( char * buffer , const char * requestId , uint16_t statusCode , bool isErrorResponse , char * errorMessage , json_object * body ) { json_object * obj = json_object_new_object ( ) ; if ( requestId != NULL ) { json_object * value = json_object_new_string ( requestId ) ; json_object_object_add ( obj , " " , value ) ; } json_object * statusCodeValue = json_object_new_int ( statusCode ) ; if ( isErrorResponse ) { json_object * error = json_object_new_object ( ) ; json_object_object_add ( error , " " , statusCodeValue ) ; if ( errorMessage != NULL ) { json_object * errorMessageValue = json_object_new_string ( errorMessage ) ; json_object_object_add ( error , " " , errorMessageValue ) ; } json_object_object_add ( obj , " " , error ) ; } else { json_object * result = json_object_new_object ( ) ; json_object_object_add ( result , " " , statusCodeValue ) ; if ( body != NULL ) { json_object_object_add ( result , " " , body ) ; } json_object_object_add ( obj , " " , result ) ; } const char * str = json_object_to_json_string_ext ( obj , JSON_C_TO_STRING_PLAIN ) ; strcpy ( buffer , str ) ; json_object_put ( obj ) ; FUNC_EXIT_RC ( SUCCESS ) ; }
@brief Marshal a command response
@brief Marshal a command response