revise the bad behavior of create effect function caused by wrong argument. 62/15162/1
authorjy910.yun <jy910.yun@samsung.com>
Fri, 31 May 2013 06:02:54 +0000 (15:02 +0900)
committerKrzysztof Sasiak <k.sasiak@samsung.com>
Thu, 16 Jan 2014 14:30:08 +0000 (15:30 +0100)
before changing the code, pass the argument to make string to interger structure.
but now, send the buffer by copying memory.

Change-Id: I1662a0232d0b93ec80656d94d57f8abe8a4f413e
Signed-off-by: jy910.yun <jy910.yun@samsung.com>
src/haptic/haptic.c
src/shared/haptic.c

index b14df05..961233c 100644 (file)
@@ -28,6 +28,7 @@
 #include "haptic-plugin-intf.h"
 
 #define HAPTIC_MODULE_PATH                     "/usr/lib/libhaptic-module.so"
+#define MAX_EFFECT_BUFFER                      16000
 
 /* Haptic Plugin Interface */
 static void *dlopen_handle;
@@ -271,14 +272,13 @@ exit:
 
 static DBusMessage *edbus_create_effect(E_DBus_Object *obj, DBusMessage *msg)
 {
-       DBusMessageIter iter;
+       static unsigned char data[MAX_EFFECT_BUFFER];
+       static unsigned char *p = data;
+       DBusMessageIter iter, arr;
        DBusMessage *reply;
        DBusError err;
        haptic_module_effect_element *elem_arr;
-       unsigned char *data;
-       unsigned char *elem;
-       unsigned char *p;
-       int i, size, cnt, ret;
+       int i, size, bufsize, cnt, ret;
 
        if (!plugin_intf || !plugin_intf->create_effect) {
                ret = -EFAULT;
@@ -286,21 +286,23 @@ static DBusMessage *edbus_create_effect(E_DBus_Object *obj, DBusMessage *msg)
        }
 
        dbus_error_init(&err);
-       if (!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &data,
-                               DBUS_TYPE_INT32, &size,
-                               DBUS_TYPE_STRING, &elem,
+       if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &bufsize,
+                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &elem_arr, &size,
                                DBUS_TYPE_INT32, &cnt, DBUS_TYPE_INVALID)) {
                ret = -EINVAL;
                goto exit;
        }
 
-       elem_arr = (haptic_module_effect_element*)malloc(sizeof(haptic_module_effect_element)*cnt);
-       for (p = elem_arr, i = 0; i < cnt; i++, p+=9) {
-               sscanf(p, "%6d%3d", &elem_arr[i].haptic_duration, &elem_arr[i].haptic_level);
-               _D("[%2d] duration : %d, level : %d", i, elem_arr[i].haptic_duration, elem_arr[i].haptic_level);
+       if (bufsize >= MAX_EFFECT_BUFFER) {
+               ret = -ENOMEM;
+               goto exit;
        }
 
-       ret = plugin_intf->create_effect(data, size, elem_arr, cnt);
+       for (i = 0; i < cnt; ++i)
+               _D("[%2d] %d %d", i, elem_arr[i].haptic_duration, elem_arr[i].haptic_level);
+
+       memset(data, 0, MAX_EFFECT_BUFFER);
+       ret = plugin_intf->create_effect(data, bufsize, elem_arr, cnt);
        if (ret < 0)
                _E("fail to create haptic effect : %d", ret);
 
@@ -309,6 +311,9 @@ static DBusMessage *edbus_create_effect(E_DBus_Object *obj, DBusMessage *msg)
 exit:
        reply = dbus_message_new_method_return(msg);
        dbus_message_iter_init_append(reply, &iter);
+       dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &arr);
+       dbus_message_iter_append_fixed_array(&arr, DBUS_TYPE_BYTE, &p, bufsize);
+       dbus_message_iter_close_container(&iter, &arr);
        dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
        return reply;
 }
@@ -451,7 +456,7 @@ static struct edbus_method {
        { "VibrateBuffer", "uayiii",   "i", edbus_vibrate_buffer },
        { "GetState",           "u",   "i", edbus_get_state },
        { "GetDuration",      "uay",   "i", edbus_get_duration },
-       { "CreateEffect",    "sisi",   "i", edbus_create_effect },
+       { "CreateEffect",    "iayi", "ayi", edbus_create_effect },
        { "SaveBinary",       "sis",   "i", edbus_save_binary },
        /* Add methods here */
 };
index 7874749..6360a0e 100644 (file)
@@ -48,8 +48,6 @@
 #define METHOD_CREATE_EFFECT           "CreateEffect"
 #define METHOD_SAVE_BINARY                     "SaveBinary"
 
-#define DURATION_CHAR  6
-#define LEVEL_CHAR             3
 
 /* START of Static Function Section */
 static unsigned char* convert_file_to_buffer(const char *file_name, int *size)
@@ -639,10 +637,11 @@ API int haptic_create_effect(unsigned char *vibe_buffer,
        DBusError err;
        DBusMessage *msg;
        char str_bufsize[32];
-       char *str_elem;
        char str_elemcnt[32];
        char *arr[4];
+       char *data;
        int i, temp, size, ret, ret_val;
+       struct dbus_byte bytes;
 
        /* check if passed arguments are valid */
        if (vibe_buffer == NULL) {
@@ -665,40 +664,46 @@ API int haptic_create_effect(unsigned char *vibe_buffer,
                return HAPTIC_ERROR_INVALID_PARAMETER;
        }
 
-       arr[0] = vibe_buffer;
-       snprintf(str_bufsize, sizeof(str_bufsize), "%d", max_bufsize);
-       arr[1] = str_bufsize;
-       size = (DURATION_CHAR+LEVEL_CHAR)*max_elemcnt;
-       str_elem = (unsigned char *)malloc(size+1);
-       memset(str_elem, 0, size);
+       /* convert to proper feedback level in case of auto */
        for (i = 0; i < max_elemcnt; i++) {
                if (elem_arr[i].haptic_level == HAPTIC_FEEDBACK_AUTO) {
                        vconf_get_int(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, &temp);
                        elem_arr[i].haptic_level = temp*20;
                }
-               snprintf(str_elem, size, "%s%6d%3d", str_elem,
-                               elem_arr[i].haptic_duration, elem_arr[i].haptic_level);
        }
-       str_elem[size] = '\0';
-       arr[2] = str_elem;
+
+       snprintf(str_bufsize, sizeof(str_bufsize), "%d", max_bufsize);
+       arr[0] = str_bufsize;
+       bytes.size = sizeof(haptic_effect_element_s)*max_elemcnt;
+       bytes.data = (unsigned char*)elem_arr;
+       arr[2] = &bytes;
        snprintf(str_elemcnt, sizeof(str_elemcnt), "%d", max_elemcnt);
        arr[3] = str_elemcnt;
 
        /* request to deviced to open haptic device */
        msg = deviced_dbus_method_sync(BUS_NAME, DEVICED_PATH_HAPTIC, DEVICED_INTERFACE_HAPTIC,
-                       METHOD_CREATE_EFFECT, "sisi", arr);
-       free(str_elem);
+                       METHOD_CREATE_EFFECT, "iayi", arr);
        if (!msg)
                return HAPTIC_ERROR_OPERATION_FAILED;
 
        dbus_error_init(&err);
 
-       ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
-       if (!ret) {
+       ret = dbus_message_get_args(msg, &err, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, &size,
+                       DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
+       if (!ret || ret_val < 0) {
                _E("no message : [%s:%s]", err.name, err.message);
-               ret = HAPTIC_ERROR_OPERATION_FAILED;
+               dbus_message_unref(msg);
+               dbus_error_free(&err);
+               return HAPTIC_ERROR_OPERATION_FAILED;
+       }
+
+       if (max_bufsize < size) {
+               _E("max_bufsize(%d) is smaller than effect buffer size(%d)", max_bufsize, size);
+               return HAPTIC_ERROR_INVALID_PARAMETER;
        }
 
+       memcpy(vibe_buffer, data, max_bufsize);
+
        dbus_message_unref(msg);
        dbus_error_free(&err);
 
@@ -736,14 +741,8 @@ API int haptic_save_effect(const unsigned char *vibe_buffer,
                return HAPTIC_ERROR_FILE_EXISTS;
        }
 
-       size = strlen(vibe_buffer);
-       if (size <= 0) {
-               _E("fail to get buffer size");
-               return HAPTIC_MODULE_OPERATION_FAILED;
-       }
-
        _D("file path : %s", file_path);
-       ret = save_data(vibe_buffer, size, file_path);
+       ret = save_data(vibe_buffer, max_bufsize, file_path);
        if (ret < 0) {
                _E("fail to save data");
                return HAPTIC_MODULE_OPERATION_FAILED;