/*!
 *
 * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 * 
 * @Author:  NADAL Jean-Baptiste
 * @Date: 21/03/2015
 *
 */

/*-------------------------------- INCLUDES ---------------------------------*/

extern "C" {
#include <libubus.h>
#include <libubox/blobmsg_json.h>
}

extern "C" {
#include "ubus-sync.h"
}

#include "common.h"

#include "ubus-cpp/ubus-exec-receiver.h"

#include "ubus-cpp/ubus-call.h"

#define kDefaultTimeoutInSecond 5
// #define UBUS_PROFILE 1

/*!  ----------------------------------------------------------------------------
 *  @fn receive_call_result_data
 *
 *  @brief callback of result.
 */
static void receive_call_result_data(struct ubus_request *a_req, int a_type, struct blob_attr *a_msg)
{
	char *the_str;
	UBusCall *a_caller = static_cast<UBusCall *>(a_req->priv);

	the_str = blobmsg_format_json(a_msg, true);

	a_caller->set_result(the_str);

	free(the_str);
}

/*!  ----------------------------------------------------------------------------
 *  @fn UBusCall
 *
 *  @brief Constructor of the UBus Call Command.
 */
UBusCall::UBusCall(void) : m_timeout(kDefaultTimeoutInSecond)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn exec
 *
 *  @brief execute a.
 */
int UBusCall::exec(const std::string &a_path, const std::string &a_method,
				   const std::string &a_parameter, std::string &a_result)
{
	int the_ret = 0;
#ifdef UBUS_PROFILE
	struct timeval the_start_request, the_current_timeval;
	gettimeofday(&the_start_request, NULL);
#endif
	the_ret = ubus_sync_exec(a_path.c_str(), a_method.c_str(), a_parameter.c_str(), receive_call_result_data, this);

	if (!m_data.empty())
	{
		a_result = m_data.c_str();
	}

#ifdef UBUS_PROFILE
	gettimeofday(&the_current_timeval, NULL);
	printf("ubus_exec [%s]-[%s] duree: %.0lf ms\n", a_path.c_str(), a_method.c_str(), time_diff(the_start_request, the_current_timeval) / 1000);
#endif
	return the_ret;
}

/*!  ----------------------------------------------------------------------------
 *  @fn exec-async
 *
 *  @brief Execute an UBus Method Asynchronously;
 */
int UBusCall::exec_async(struct ubus_context *a_ctx, uint32_t an_object, const std::string &a_method, const std::string &a_parameter, UBusExecReceiver *a_receiver)
{
	int the_return = -1;
	struct blob_buf the_parameter = {0};
	a_receiver->abort(a_ctx);

	blob_buf_init(&the_parameter, 0);

	if (!a_parameter.empty())
		blobmsg_add_json_from_string(&the_parameter, a_parameter.c_str());

	if (!ubus_invoke_async(a_ctx, an_object, a_method.c_str(), the_parameter.head, a_receiver))
	{
		a_receiver->complete_request_async(a_ctx);
		the_return = 0;
	}

	blob_buf_free(&the_parameter);

	return the_return;
}

/*!  ----------------------------------------------------------------------------
 *  @fn set_result
 *
 *  @brief Save the Result received by UBus.
 */
int UBusCall::set_result(const std::string &a_result)
{
	m_data = a_result;
	return 0;
}
