From 05aae5261b91d6153c362f6c48f0340e7b8db139 Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Wed, 31 Jul 2013 15:17:15 -0700 Subject: [PATCH] added set command to vehicle --- src/Vehicle/JSVehicle.cpp | 65 +++++++++++++++++++++++++++++++++++++ src/Vehicle/JSVehicle.h | 7 ++++ src/Vehicle/Vehicle.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++ src/Vehicle/Vehicle.h | 7 ++++ src/Vehicle/Vehicle.idl | 2 +- src/Vehicle/config.xml | 2 +- 6 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src/Vehicle/JSVehicle.cpp b/src/Vehicle/JSVehicle.cpp index 104c73c..237fa38 100644 --- a/src/Vehicle/JSVehicle.cpp +++ b/src/Vehicle/JSVehicle.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -20,6 +21,9 @@ using namespace DeviceAPI::Common; using namespace WrtDeviceApis::Commons; using namespace WrtDeviceApis::CommonsJavaScript; + + + JSClassDefinition JSVehicle::m_classInfo = { 0, kJSClassAttributeNone, @@ -44,6 +48,7 @@ JSStaticFunction JSVehicle::m_function[] = { { "get", JSVehicle::get, kJSPropertyAttributeNone }, { "subscribe", JSVehicle::subscribe, kJSPropertyAttributeNone }, { "supported", JSVehicle::supported, kJSPropertyAttributeNone }, + { "set", JSVehicle::set, kJSPropertyAttributeNone }, { 0, 0, 0 } }; @@ -245,5 +250,65 @@ JSValueRef JSVehicle::supported(JSContextRef context, 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(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 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); +} + } } diff --git a/src/Vehicle/JSVehicle.h b/src/Vehicle/JSVehicle.h index 8992f8b..b12edbf 100644 --- a/src/Vehicle/JSVehicle.h +++ b/src/Vehicle/JSVehicle.h @@ -59,6 +59,13 @@ private: 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. diff --git a/src/Vehicle/Vehicle.cpp b/src/Vehicle/Vehicle.cpp index 8109e50..699c269 100644 --- a/src/Vehicle/Vehicle.cpp +++ b/src/Vehicle/Vehicle.cpp @@ -14,6 +14,44 @@ namespace Vehicle { 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() { @@ -328,5 +366,48 @@ GVariant* VehicleMaster::listObjects() return supportedList; } +VehicleMaster::ErrorType VehicleMaster::set(std::string objectName, std::map 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 diff --git a/src/Vehicle/Vehicle.h b/src/Vehicle/Vehicle.h index 2dc200f..68ef39b 100644 --- a/src/Vehicle/Vehicle.h +++ b/src/Vehicle/Vehicle.h @@ -26,6 +26,11 @@ public: class VehicleMaster { public: + enum ErrorType { + None = 0, + NotSupported = 1, + PermissionDenied = 2 + }; VehicleMaster(); @@ -37,6 +42,8 @@ public: void onSignalReceived(std::string objectName, std::map propertyMap); + VehicleMaster::ErrorType set(std::string objectName, std::map values, JSContextRef context); + private: /// methods: GDBusProxy* automotiveManager(); diff --git a/src/Vehicle/Vehicle.idl b/src/Vehicle/Vehicle.idl index 8430e2c..1cd223c 100644 --- a/src/Vehicle/Vehicle.idl +++ b/src/Vehicle/Vehicle.idl @@ -80,7 +80,7 @@ interface Vehicle { * \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' diff --git a/src/Vehicle/config.xml b/src/Vehicle/config.xml index 912f8cd..9ec1b59 100644 --- a/src/Vehicle/config.xml +++ b/src/Vehicle/config.xml @@ -2,7 +2,7 @@ libwrt-plugins-tizen-vehicle.so - time.install.uri + vehicle.install.uri INTEL plugin group INTEL certificate authority AAAABBBBCCCCDDDEEEE0000 -- 2.7.4