/*!
 * capabilities.cpp
 *
 * Copyright (c) 2015, 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.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 (void) : 
	UBusObject (gCapabilitiesModelUbus_types, "domo.capabilities")
{
}


/*!  ----------------------------------------------------------------------------
 *  @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 *aCtx, struct ubus_request_data *aReq, 
							struct blob_attr *aMsg)
{
	int theResult;
	struct blob_buf theBuf = {0};

	std::ifstream theCapFile ("./rsc/capabilities.json");
	std::stringstream theBuffer;
	theBuffer << theCapFile.rdbuf();

	blob_buf_init (&theBuf, 0);
	
	blobmsg_add_json_from_string (&theBuf, theBuffer.str().c_str());
	
	theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
	
	blob_buf_free (&theBuf);
	
	return theResult;
}
