/*!
 * UbusCall.cpp
 *
 * 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>	
}

#include "common.h"

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

#define kDefaultTimeoutInSecond		5

static void receive_call_result_data (struct ubus_request *aReq, int aType, struct blob_attr *aMsg);

/*!  ----------------------------------------------------------------------------
 *  @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;	
	uint32_t the_id;
	
	struct blob_buf the_buf = { 0 };
	struct ubus_context *the_ctx = ubus_connect(NULL);
	
	if (the_ctx == NULL) {
		
		fprintf(stderr, "%s - Failed to create an ubus context.\n", __PRETTY_FUNCTION__);
		return -1;
	}
	
	blob_buf_init(&the_buf, 0);
	
	if (!a_parameter.empty()) {

		if (!blobmsg_add_json_from_string(&the_buf, a_parameter.c_str())) {
			
			fprintf(stderr, "%s - Failed to parse message data\n", __PRETTY_FUNCTION__);
			return -1;
		}
	}
	
	the_ret = ubus_lookup_id(the_ctx, a_path.c_str(), &the_id);
	if (the_ret) {
		
		fprintf(stderr, "%s - ubus_lookup_id error = '%d'\n", __PRETTY_FUNCTION__, the_ret);
		ubus_free(the_ctx);
		blob_buf_free(&the_buf);
		return the_ret;
	}
	
	the_ret = ubus_invoke(the_ctx, the_id, a_method.c_str(), the_buf.head, receive_call_result_data, this, m_timeout * 1000);
	
	ubus_free(the_ctx);
	blob_buf_free(&the_buf);
	
	if (!m_data.empty()) {

		a_result = m_data;
	}

	return the_ret;
}


/*!  ----------------------------------------------------------------------------
 *  @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;
}


/*!  ----------------------------------------------------------------------------
 *  @fn receive_call_result_data
 *
 *  @brief UBus received call back.
 */
static void receive_call_result_data (struct ubus_request *aReq, int aType, struct blob_attr *aMsg)
{
	UNUSED_PARAMETER (aType);
	char *the_str;
	UBusCall *anUBusObject = static_cast<UBusCall*>(aReq->priv);
	the_str = blobmsg_format_json(aMsg, true);
	
	anUBusObject->set_result(std::string(the_str));
	
	free(the_str);
}
