Change App Life Cycle 33/133433/3
authorjunkyu han <junkyu.han@samsung.com>
Mon, 12 Jun 2017 07:57:58 +0000 (16:57 +0900)
committerjunkyu han <junkyu.han@samsung.com>
Mon, 12 Jun 2017 10:06:08 +0000 (19:06 +0900)
Change-Id: I4babc29cfc44537341938db98f15efd2081e1f48

CMakeLists.txt
packaging/org.tizen.thing-toggler.spec
src/thing_toggler.c

index 0c3a186fee160f77db5ef9fd92f7befeed045542..5131b1628d768e1152591b50a52306de03bf1107 100644 (file)
@@ -15,6 +15,7 @@ pkg_check_modules(APP_PKGS REQUIRED
        capi-appfw-application
        capi-appfw-service-application
        capi-system-peripheral-io
+       ecore
 )
 
 FOREACH (flag ${APP_PKGS_CFLAGS})
index 5d0cab219ec2dd419b61949fa8356598af27e66e..d51eaa5670e965997f9f2120fe55e384eb2ab241 100644 (file)
@@ -14,7 +14,8 @@ BuildRequires:  pkgconfig(capi-appfw-application)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(capi-appfw-service-application)
-BuildRequires: pkgconfig(capi-system-peripheral-io)
+BuildRequires:  pkgconfig(capi-system-peripheral-io)
+BuildRequires:  pkgconfig(ecore)
 
 %description
 Thing Toggler
index f80b73aa1a9a604ac31b944f8d4fb76b0dc1ed57..f45a1f999296f3b144a6120ae672293147ba065e 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <tizen.h>
+#include <Ecore.h>
 #include <service_app.h>
 #include <unistd.h>
 #include <glib.h>
 
 #include "thing_toggler.h"
 
-#define GPIO_NUM 25
+#define GPIO_NUM 20
+
+static Eina_Bool _toggle_led(void *data);
+
+typedef struct app_data_s {
+       int current_status;
+       peripheral_gpio_h gpio;
+       Ecore_Timer *on_off_timer;
+} app_data;
 
 bool service_app_create(void *data)
 {
+       app_data *ad = (app_data *)data;
+       int ret = 0;
+
     // Todo: add your code here.
+       peripheral_gpio_open(GPIO_NUM, &ad->gpio);
+       if (!ad->gpio) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "The return value of peripheral_gpio_open is null.");
+               return false;
+       }
+
+       ret = peripheral_gpio_set_direction(ad->gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
+       if (ret != 0) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot set direction error.");
+               return false;
+       }
+
+       // Return value : the ID (greater than 0) of the event source.
+       ad->on_off_timer = ecore_timer_add(1, _toggle_led, ad);
+       if (!ad->on_off_timer) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add ON/OFF timer");
+               return false;
+       }
+
     return true;
 }
 
 void service_app_terminate(void *data)
 {
+       app_data *ad = (app_data *)data;
     // Todo: add your code here.
+       ecore_timer_del(ad->on_off_timer);
+       peripheral_gpio_close(ad->gpio);
+
+       free(ad);
 }
 
 void service_app_control(app_control_h app_control, void *data)
@@ -62,32 +98,14 @@ static void service_app_low_memory(app_event_info_h event_info, void *user_data)
        /*APP_EVENT_LOW_MEMORY*/
 }
 
-static void _control_led(peripheral_gpio_h gpio, int power_on)
-{
-       dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO.");
-       peripheral_gpio_write(gpio, power_on);
-}
-
-static gboolean _toggle_led(gpointer user_data)
-{
-       static int current_status = 0;
-
-       if (current_status) current_status = 0;
-       else current_status = 1;
-
-       _control_led(user_data, current_status);
-
-       return TRUE;
-}
-
 int main(int argc, char* argv[])
 {
-    char ad[50] = {0,};
+       app_data *ad = NULL;
+       int ret = 0;
        service_app_lifecycle_callback_s event_callback;
        app_event_handler_h handlers[5] = {NULL, };
-       peripheral_gpio_h gpio = NULL;
-       guint timer_id = 0;
-       int ret = 0;
+
+       ad = (app_data *)calloc(1, sizeof(app_data));
 
        event_callback.create = service_app_create;
        event_callback.terminate = service_app_terminate;
@@ -98,30 +116,25 @@ int main(int argc, char* argv[])
        service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
        service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
 
-       peripheral_gpio_open(GPIO_NUM, &gpio);
-       if (!gpio) {
-               dlog_print(DLOG_INFO, LOG_TAG, "The return value of peripheral_gpio_open is null.");
-               return 1;
-       }
+       ret = service_app_main(argc, argv, &event_callback, ad);
 
-       ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
-       if (ret != 0) {
-               dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
-               return 1;
-       }
+       return ret;
+}
 
-       // Return value : the ID (greater than 0) of the event source.
-       timer_id = g_timeout_add_seconds(1, _toggle_led, gpio);
-       if (timer_id <= 0) {
-               dlog_print(DLOG_INFO, LOG_TAG, "Timer id : %d", timer_id);
-               return 1;
-       }
+static Eina_Bool _toggle_led(void *data)
+{
+       app_data *ad = (app_data *)data;
+       int on_off = 0;
 
-       ret = service_app_main(argc, argv, &event_callback, ad);
+       on_off = ad->current_status % 2;
 
-       g_source_remove(timer_id);
-       peripheral_gpio_close(gpio);
+       if (peripheral_gpio_write(ad->gpio, on_off) < 0) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Failed to write to GPIO");
+               return EINA_FALSE;
+       }
 
-       return ret;
+       ad->current_status++;
+
+       return TRUE;
 }