Libtracker refactoring for multi-tracker 41/126641/1 accepted/tizen/unified/20170429.024303 submit/tizen/20170428.074418 tizen_4.0.m1_release
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 24 Apr 2017 11:10:12 +0000 (20:10 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Mon, 24 Apr 2017 11:17:04 +0000 (20:17 +0900)
- Modify API
  - Rename get functions
  - Require tracker type : no more only power lock's

- Modularization
  - Each constructor adds their own tracker
  - Iterate all trackers and run only for loaded trackers

Change-Id: Ifdcc55d04643b2d6694222fec5afd128e37d78f5
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
include/tracker.h
include/tracker_private.h
src/power_lock.c
src/services.c
src/tracker.c

index e157657..ff0c41e 100644 (file)
@@ -52,9 +52,15 @@ typedef enum {
        TRACKER_SERVICE_IOT      = 1 << 5, /**< IoT services */
 } tracker_service_e;
 
+typedef enum {
+       TRACKER_TYPE_POWER_LOCK = 0,
+       TRACKER_TYPE_MAX
+} tracker_type_e;
+
 /**
  * @brief   Gets the reference count of the Power Lock operation
  * @since_tizen 3.0
+ * @param[in] type Tracker type
  * @param[out] count The reference count of the Power Lock operation
  * @return  @c 0 on success,
  *          otherwise a negative error value
@@ -62,11 +68,12 @@ typedef enum {
  * @retval  #TRACKER_ERROR_INVALID_PARAMETER The input parameter is invalid
  * @retval  #TRACKER_ERROR_PERMISSION_DENIED No permission to use the API
  */
-int tracker_get_power_lock_ref(int *count);
+int tracker_get_ref_counter(tracker_type_e type, int *count);
 
 /**
  * @brief   Gets the total count of the Power Lock operation
  * @since_tizen 3.0
+ * @param[in] type Tracker type
  * @param[out] count The total count of the Power Lock operation
  * @return  @c 0 on success,
  *          otherwise a negative error value
@@ -74,7 +81,7 @@ int tracker_get_power_lock_ref(int *count);
  * @retval  #TRACKER_ERROR_INVALID_PARAMETER The input parameter is invalid
  * @retval  #TRACKER_ERROR_PERMISSION_DENIED No permission to use the API
  */
-int tracker_get_power_lock_total(int *count);
+int tracker_get_tick(tracker_type_e type, int *count);
 
 /**
  * @brief   Start tracking for information of services
index 3291e5c..59ad674 100644 (file)
 
 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
 
-int get_power_lock_ref(void);
-int get_power_lock_total(void);
+struct ops {
+       tracker_type_e type;
+       void (*ref)(tracker_service_e);
+       void (*unref)(tracker_service_e);
+       int (*get_ref_counter)(void);
+       int (*get_tick)(void);
+};
+
+struct counter {
+       tracker_service_e service;
+       unsigned int ref_counter;
+       int tick;
+};
+
+int power_lock_get_ref_counter(void);
+int power_lock_get_tick(void);
 void power_lock_ref(tracker_service_e service);
 void power_lock_unref(tracker_service_e service);
 
@@ -70,5 +84,14 @@ void stop_service_sensor(void);
 void start_service_iot(void);
 void stop_service_iot(void);
 
+/****************************************************************/
+
+struct ops *trackers[TRACKER_TYPE_MAX];
+
+#define REGISTER_TRACKER(name) \
+       static void __attribute__ ((constructor)) add_tracker(void)     \
+       {       \
+               trackers[(name)->type] = name;  \
+       }
 
 #endif /* __LIBTRACKER_TRACKER_PRIVATE_H__ */
index 643d94e..ded7750 100644 (file)
 #define POWERLOCK_DEFAULT_REF   0
 #define POWERLOCK_DEFAULT_COUNT 0
 
-struct powerlock_ref {
-       tracker_service_e service;
-       unsigned int ref;
-       int total;
-} lock_refs[] = {
+static struct counter lock_counters[] = {
        { TRACKER_SERVICE_DOWNLOAD, POWERLOCK_DEFAULT_REF , POWERLOCK_DEFAULT_COUNT },
        { TRACKER_SERVICE_MEDIA,    POWERLOCK_DEFAULT_REF , POWERLOCK_DEFAULT_COUNT },
        { TRACKER_SERVICE_NETWORK,  POWERLOCK_DEFAULT_REF , POWERLOCK_DEFAULT_COUNT },
@@ -35,14 +31,14 @@ struct powerlock_ref {
        { TRACKER_SERVICE_IOT,      POWERLOCK_DEFAULT_REF , POWERLOCK_DEFAULT_COUNT },
 };
 
-#define COUNT_MAX (INT_MAX/ARRAY_SIZE(lock_refs))
+#define COUNT_MAX (INT_MAX/ARRAY_SIZE(lock_counters))
 
-int get_power_lock_ref(void)
+int power_lock_get_ref_counter(void)
 {
        int sum = 0, i;
 
-       for (i = 0 ; i < ARRAY_SIZE(lock_refs) ; i++)
-               sum += lock_refs[i].ref;
+       for (i = 0 ; i < ARRAY_SIZE(lock_counters) ; i++)
+               sum += lock_counters[i].ref_counter;
 
        _I("Total reference count of pid(%d) is (%d)",
                                getpid(), sum);
@@ -50,12 +46,12 @@ int get_power_lock_ref(void)
        return sum;
 }
 
-int get_power_lock_total(void)
+int power_lock_get_tick(void)
 {
        int sum = 0, i;
 
-       for (i = 0 ; i < ARRAY_SIZE(lock_refs) ; i++)
-               sum += lock_refs[i].total;
+       for (i = 0 ; i < ARRAY_SIZE(lock_counters) ; i++)
+               sum += lock_counters[i].tick;
 
        _I("Total lock count of pid(%d) is (%d)",
                                getpid(), sum);
@@ -68,19 +64,19 @@ void power_lock_ref(tracker_service_e service)
        int i;
        char buf[32];
 
-       for (i = 0 ; i < ARRAY_SIZE(lock_refs) ; i++) {
-               if (lock_refs[i].service != service)
+       for (i = 0 ; i < ARRAY_SIZE(lock_counters) ; i++) {
+               if (lock_counters[i].service != service)
                        continue;
-               lock_refs[i].ref++;
+               lock_counters[i].ref_counter++;
 
-               if (lock_refs[i].total == COUNT_MAX)
-                       lock_refs[i].total = POWERLOCK_DEFAULT_COUNT;
+               if (lock_counters[i].tick == COUNT_MAX)
+                       lock_counters[i].tick = POWERLOCK_DEFAULT_COUNT;
                else
-                       lock_refs[i].total++;
+                       lock_counters[i].tick++;
 
                _I("[%s] pid(%d) increases power lock ref count to (%d, %d)",
                                service_name_str(service, buf, sizeof(buf)),
-                               getpid(), lock_refs[i].ref, lock_refs[i].total);
+                               getpid(), lock_counters[i].ref_counter, lock_counters[i].tick);
                break;
        }
 }
@@ -90,14 +86,24 @@ void power_lock_unref(tracker_service_e service)
        int i;
        char buf[32];
 
-       for (i = 0 ; i < ARRAY_SIZE(lock_refs) ; i++) {
-               if (lock_refs[i].service != service)
+       for (i = 0 ; i < ARRAY_SIZE(lock_counters) ; i++) {
+               if (lock_counters[i].service != service)
                        continue;
-               if (lock_refs[i].ref > 0)
-                       lock_refs[i].ref--;
+               if (lock_counters[i].ref_counter > 0)
+                       lock_counters[i].ref_counter--;
                _I("[%s] pid(%d) decreases power lock ref count to (%d)",
                                service_name_str(service, buf, sizeof(buf)),
-                               getpid(), lock_refs[i].ref);
+                               getpid(), lock_counters[i].ref_counter);
                break;
        }
 }
+
+static struct ops power_lock_ops = {
+       .type = TRACKER_TYPE_POWER_LOCK,
+       .ref = power_lock_ref,
+       .unref = power_lock_unref,
+       .get_ref_counter = power_lock_get_ref_counter,
+       .get_tick = power_lock_get_tick,
+};
+
+REGISTER_TRACKER(&power_lock_ops)
index e9daa62..31c0a46 100644 (file)
@@ -55,60 +55,120 @@ char *service_name_str(tracker_service_e svc, char *buf, size_t len)
 
 void start_service_download(void)
 {
-       power_lock_ref(TRACKER_SERVICE_DOWNLOAD);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->ref)
+                       continue;
+               trackers[i]->ref(TRACKER_SERVICE_DOWNLOAD);
+       }
 }
 
 void stop_service_download(void)
 {
-       power_lock_unref(TRACKER_SERVICE_DOWNLOAD);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->unref)
+                       continue;
+               trackers[i]->unref(TRACKER_SERVICE_DOWNLOAD);
+       }
 }
 
 void start_service_media(void)
 {
-       power_lock_ref(TRACKER_SERVICE_MEDIA);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->ref)
+                       continue;
+               trackers[i]->ref(TRACKER_SERVICE_MEDIA);
+       }
 }
 
 void stop_service_media(void)
 {
-       power_lock_unref(TRACKER_SERVICE_MEDIA);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->unref)
+                       continue;
+               trackers[i]->unref(TRACKER_SERVICE_MEDIA);
+       }
 }
 
 void start_service_network(void)
 {
-       power_lock_ref(TRACKER_SERVICE_NETWORK);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->ref)
+                       continue;
+               trackers[i]->ref(TRACKER_SERVICE_NETWORK);
+       }
 }
 
 void stop_service_network(void)
 {
-       power_lock_unref(TRACKER_SERVICE_NETWORK);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->unref)
+                       continue;
+               trackers[i]->unref(TRACKER_SERVICE_NETWORK);
+       }
 }
 
 void start_service_location(void)
 {
-       power_lock_ref(TRACKER_SERVICE_LOCATION);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->ref)
+                       continue;
+               trackers[i]->ref(TRACKER_SERVICE_LOCATION);
+       }
 }
 
 void stop_service_location(void)
 {
-       power_lock_unref(TRACKER_SERVICE_LOCATION);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->unref)
+                       continue;
+               trackers[i]->unref(TRACKER_SERVICE_LOCATION);
+       }
 }
 
 void start_service_sensor(void)
 {
-       power_lock_ref(TRACKER_SERVICE_SENSOR);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->ref)
+                       continue;
+               trackers[i]->ref(TRACKER_SERVICE_SENSOR);
+       }
 }
 
 void stop_service_sensor(void)
 {
-       power_lock_unref(TRACKER_SERVICE_SENSOR);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->unref)
+                       continue;
+               trackers[i]->unref(TRACKER_SERVICE_SENSOR);
+       }
 }
 
 void start_service_iot(void)
 {
-       power_lock_ref(TRACKER_SERVICE_IOT);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->ref)
+                       continue;
+               trackers[i]->ref(TRACKER_SERVICE_IOT);
+       }
 }
 
 void stop_service_iot(void)
 {
-       power_lock_unref(TRACKER_SERVICE_IOT);
+       int i;
+       for (i = 0; i < TRACKER_TYPE_MAX; i++) {
+               if (!trackers[i] || !trackers[i]->unref)
+                       continue;
+               trackers[i]->unref(TRACKER_SERVICE_IOT);
+       }
 }
index c9b7bde..f972dd9 100644 (file)
 #include "tracker.h"
 #include "tracker_private.h"
 
-API int tracker_get_power_lock_ref(int *cnt)
+API int tracker_get_ref_counter(tracker_type_e type, int *cnt)
 {
        int ref;
 
-       ref = get_power_lock_ref();
+       if (type < 0 || type >= TRACKER_TYPE_MAX || !trackers[type]) {
+               _E("Try to access not existed tracker %d", type);
+               return TRACKER_ERROR_INVALID_PARAMETER;
+       }
+
+       ref = trackers[type]->get_ref_counter();
        if (ref < 0) {
                _E("Failed to get power lock reference count(%d)", ref);
                return ref;
@@ -32,11 +37,16 @@ API int tracker_get_power_lock_ref(int *cnt)
        return TRACKER_ERROR_NONE;
 }
 
-API int tracker_get_power_lock_total(int *cnt)
+API int tracker_get_tick(tracker_type_e type, int *cnt)
 {
        int count;
 
-       count = get_power_lock_total();
+       if (type < 0 || type >= TRACKER_TYPE_MAX || !trackers[type]) {
+               _E("Try to access not existed tracker %d", type);
+               return TRACKER_ERROR_INVALID_PARAMETER;
+       }
+
+       count = trackers[type]->get_tick();
        if (count < 0) {
                _E("Failed to get power lock total count(%d)", count);
                return count;