Set recent value to current value when callback is added 38/97138/3 accepted/tizen/common/20161114.171314 accepted/tizen/ivi/20161114.010412 accepted/tizen/mobile/20161114.010246 accepted/tizen/tv/20161114.010313 accepted/tizen/wearable/20161114.010342 submit/tizen/20161113.015118
authorKichan Kwon <k_c.kwon@samsung.com>
Fri, 11 Nov 2016 08:24:11 +0000 (17:24 +0900)
committertaeyoung <ty317.kim@samsung.com>
Sat, 12 Nov 2016 04:21:06 +0000 (13:21 +0900)
- Until now, initial recent value is NULL
- Therefore, when key is changed, callback always called
- It is usually correct, but we have to do exception handling
  - If value is set 0 -> 1, most keys are considered to be changed
  - But USB connection isn't changed (0,1=false, 2=true)

Change-Id: If78913539a4eb8c27dad5de40413e0edd5fc24ff
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
Signed-off-by: taeyoung <ty317.kim@samsung.com>
src/runtime_info.c

index 4fc9471..237895b 100644 (file)
@@ -335,6 +335,7 @@ API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed
        runtime_info_item_h runtime_info_item;
        runtime_info_func_set_event_cb set_event_cb;
        bool subscribe_event = false;
+       int ret;
 
        if (callback == NULL) {
                LOGE("INVALID_PARAMETER(0x%08x)", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
@@ -357,7 +358,7 @@ API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed
                subscribe_event = true;
 
                runtime_info_event_subscription_h event_subscription;
-               event_subscription = malloc(sizeof(runtime_info_event_subscription_s));
+               event_subscription = calloc(1, sizeof(runtime_info_event_subscription_s));
 
                if (event_subscription == NULL) {
                        LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
@@ -365,7 +366,6 @@ API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed
                }
 
                runtime_info_item->event_subscription = event_subscription;
-               runtime_info_item->event_subscription->most_recent_value = NULL;
        }
 
        runtime_info_item->event_subscription->changed_cb = callback;
@@ -373,13 +373,35 @@ API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed
 
        if (runtime_info_item->event_subscription->most_recent_value != NULL)
                free(runtime_info_item->event_subscription->most_recent_value);
-
        runtime_info_item->event_subscription->most_recent_value = NULL;
 
-       if (subscribe_event == true)
-               return set_event_cb();
-       else
+       if (!subscribe_event)
                return RUNTIME_INFO_ERROR_NONE;
+
+       ret = set_event_cb();
+       if (ret != RUNTIME_INFO_ERROR_NONE) {
+               _E("Failed to set event hadler (%d)", ret);
+               return ret;
+       }
+
+       runtime_info_item->event_subscription->most_recent_value =
+               calloc(1, sizeof(runtime_info_value_u));
+       if (!runtime_info_item->event_subscription->most_recent_value) {
+               LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
+               runtime_info_item->unset_event_cb();
+               return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
+       }
+
+       ret = runtime_info_item->get_value(runtime_info_item->event_subscription->most_recent_value);
+       if (ret != RUNTIME_INFO_ERROR_NONE) {
+               LOGE("Failed to get current value (%d)", ret);
+               runtime_info_item->unset_event_cb();
+               free(runtime_info_item->event_subscription->most_recent_value);
+               runtime_info_item->event_subscription->most_recent_value = NULL;
+               return ret;
+       }
+
+       return RUNTIME_INFO_ERROR_NONE;
 }
 
 API int runtime_info_unset_changed_cb(runtime_info_key_e key)