#include <JSWebAPIErrorFactory.h>
#include <ArgumentValidator.h>
#include <CommonsJavaScript/Converter.h>
+#include <dpl/scoped_ptr.h>
#include <sstream>
#include <json-glib/json-gvariant.h>
using namespace WrtDeviceApis::Commons;
using namespace WrtDeviceApis::CommonsJavaScript;
+
+
+
JSClassDefinition JSVehicle::m_classInfo = {
0,
kJSClassAttributeNone,
{ "get", JSVehicle::get, kJSPropertyAttributeNone },
{ "subscribe", JSVehicle::subscribe, kJSPropertyAttributeNone },
{ "supported", JSVehicle::supported, kJSPropertyAttributeNone },
+ { "set", JSVehicle::set, kJSPropertyAttributeNone },
{ 0, 0, 0 }
};
return result;
}
+JSValueRef JSVehicle::set(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("Entered");
+ VehiclePrivObject* privateObject = static_cast<VehiclePrivObject*>(JSObjectGetPrivate(thisObject));
+ if (NULL == privateObject)
+ {
+ LoggerE("private object is null");
+ //return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
+ }
+
+ VehiclePtr vehicle(privateObject->getObject());
+
+ ArgumentValidator validator(context, argumentCount, arguments);
+
+ std::string objectName = validator.toString(0);
+
+
+ JSObjectRef obj = validator.toObject(1, true);
+
+ JSObjectRef errorCallback = validator.toFunction(2,true);
+
+ JSPropertyNameArrayRef keys = JSObjectCopyPropertyNames(context, obj);
+ size_t count = JSPropertyNameArrayGetCount(keys);
+
+ map<std::string, JSValueRef> objectMap;
+
+ for(size_t i=0; i< count; i++)
+ {
+ JSValueRef val = JSObjectGetPropertyAtIndex(context,obj, (unsigned int)i, NULL);
+
+ std::string result;
+ JSStringRef str = JSPropertyNameArrayGetNameAtIndex(keys, i);
+ if (NULL == str) {
+ ThrowMsg(ConversionException, "Couldn't cast to a string.");
+ }
+
+ Converter converter(context);
+
+ result = converter.toString(str);
+
+ objectMap[result] = val;
+ }
+
+ VehicleMaster::ErrorType error = vehicle->set(objectName, objectMap, context);
+
+ if(error != VehicleMaster::None)
+ {
+ const JSValueRef arguments[1] = { JSValueMakeNumber(context, (int)error) };
+
+ JSObjectCallAsFunction(context, errorCallback, NULL, 1, arguments, NULL);
+ }
+
+ return JSValueMakeUndefined(context);
+}
+
}
}
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception);
+
+ static JSValueRef set(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception);
/**
* This structure contains properties and callbacks that define a type of object.
{
using namespace WrtDeviceApis::Commons;
+using namespace WrtDeviceApis::CommonsJavaScript;
+
+GVariant* jsValueToGVariant(JSContextRef ctx, JSValueRef jsVal, std::string signature)
+{
+ JSType type = JSValueGetType(ctx, jsVal);
+
+ Converter converter(ctx);
+
+ if(type == kJSTypeString)
+ {
+ std::string result = converter.toString(jsVal);
+
+ return g_variant_new(signature.c_str(), result.c_str());
+ }
+
+ else if(type == kJSTypeNumber)
+ {
+ if(signature == "i" || signature == "y")
+ {
+ return g_variant_new(signature.c_str(), converter.toInt(jsVal));
+ }
+
+ else if(signature == "q")
+ {
+ return g_variant_new(signature.c_str(), converter.toULong(jsVal));
+ }
+ else if(signature == "d")
+ {
+ return g_variant_new(signature.c_str(), converter.toDouble(jsVal));
+ }
+
+ }
+
+ else if(type == kJSTypeBoolean)
+ {
+ return g_variant_new(signature.c_str(), converter.toBool(jsVal));
+ }
+}
VehicleMaster::VehicleMaster()
{
return supportedList;
}
+VehicleMaster::ErrorType VehicleMaster::set(std::string objectName, std::map<std::string, JSValueRef> values, JSContextRef context)
+{
+ LoggerD("Entered");
+ GDBusProxy* managerProxy = automotiveManager();
+
+ std::string objectPath = findProperty(objectName);
+
+ GError* error = nullptr;
+
+ GDBusProxy* propertiesProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ "org.automotive.message.broker",
+ objectPath.c_str(),
+ "org.freedesktop.DBus.Properties",
+ NULL,
+ &error);
+ std::string interfaceName = "org.automotive." + objectName;
+
+ for(auto itr = values.begin(); itr != values.end(); itr++)
+ {
+ GError* err = nullptr;
+ std::string property = (*itr).first;
+
+ /// First, get the value so we know the signature:
+
+ GVariant* getValue = g_dbus_proxy_call_sync(propertiesProxy, "Get", g_variant_new("(ss)", interfaceName.c_str(), property.c_str()), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+
+ GVariant *value = jsValueToGVariant(context, (*itr).second, g_variant_get_type_string(getValue));
+
+ g_dbus_proxy_call_sync(propertiesProxy, "Set", g_variant_new("(ssv)", interfaceName.c_str(), property.c_str(), value), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+
+ if(err)
+ {
+ LoggerD("error setting property:" << error->message);
+ /// TODO: return proper ErrorType here:
+ g_assert(!err);
+ }
+ }
+
+ g_object_unref(managerProxy);
+
+ return None;
+}
+
}
}
\ No newline at end of file
class VehicleMaster
{
public:
+ enum ErrorType {
+ None = 0,
+ NotSupported = 1,
+ PermissionDenied = 2
+ };
VehicleMaster();
void onSignalReceived(std::string objectName, std::map<std::string, GVariant*> propertyMap);
+ VehicleMaster::ErrorType set(std::string objectName, std::map<std::string, JSValueRef> values, JSContextRef context);
+
private: /// methods:
GDBusProxy* automotiveManager();
* \arg VehiclePropertyCallback successCallback callback if operation is successfull
* \arg VehiclePropertyErrorCallback errorCallback callback if error has been called.
**/
- set(DOMString property, VehiclePropertyType value, optional VehiclePropertyCallback successCallback, optional VehiclePropertyErrorCallback errorCallback);
+ set(DOMString property, VehiclePropertyType value, optional VehiclePropertyErrorCallback errorCallback);
/**
* \brief get values for a given property within a certain past time period between 'startTime' and 'endTime'
<!DOCTYPE plugin-properties SYSTEM "/usr/etc/tizen-apis/config.dtd">
<plugin-properties>
<library-name>libwrt-plugins-tizen-vehicle.so</library-name>
- <feature-install-uri>time.install.uri</feature-install-uri>
+ <feature-install-uri>vehicle.install.uri</feature-install-uri>
<feature-key-cn>INTEL plugin group</feature-key-cn>
<feature-root-cn>INTEL certificate authority</feature-root-cn>
<feature-root-fingerprint>AAAABBBBCCCCDDDEEEE0000</feature-root-fingerprint>