/*!
 * capabilities.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: 23/03/2015
 *
 */

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

#include <fstream>
#include <string>
#include <sstream>

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

#include "capabilities_model.h"

namespace
{

using namespace UBus;

static ObjectType gCapabilitiesModelUbus_types(
	"capabilities",
	Method("get", UBUS_CPP(CapabilitiesModel, get)));
}

/*!  ----------------------------------------------------------------------------
 *  @fn CapabilitiesModel
 *
 *  @brief Constructor of the UBus Mixer Volume.
 */
CapabilitiesModel::CapabilitiesModel(const std::string &a_capability_file) : UBusObject(gCapabilitiesModelUbus_types, "domo.capabilities"),
																			 m_capability_file(a_capability_file)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn ~CapabilitiesModel
 *
 *  @brief Destructor of the UBus Mixer Volume.
 */
CapabilitiesModel::~CapabilitiesModel(void)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn get
 *
 *  @brief Get the List of the Capabilities.
 */
int CapabilitiesModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *)
{
	int the_result;
	struct blob_buf the_buf = {0};

	std::ifstream the_cap_file(m_capability_file);
	std::stringstream the_buffer;
	the_buffer << the_cap_file.rdbuf();

	blob_buf_init(&the_buf, 0);

	blobmsg_add_json_from_string(&the_buf, the_buffer.str().c_str());

	the_result = ubus_send_reply(a_ctx, a_req, the_buf.head);

	blob_buf_free(&the_buf);

	return the_result;
}
