API changes : GPIO
[apps/native/thing-toggler.git] / src / thing_toggler.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <tizen.h>
20 #include <service_app.h>
21 #include <unistd.h>
22 #include <glib.h>
23 #include <peripheral_io.h>
24
25 #include "thing_toggler.h"
26
27 #define GPIO_NUM 25
28
29 bool service_app_create(void *data)
30 {
31     // Todo: add your code here.
32     return true;
33 }
34
35 void service_app_terminate(void *data)
36 {
37     // Todo: add your code here.
38 }
39
40 void service_app_control(app_control_h app_control, void *data)
41 {
42     // Todo: add your code here.
43 }
44
45 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
46 {
47         /*APP_EVENT_LANGUAGE_CHANGED*/
48 }
49
50 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
51 {
52         /*APP_EVENT_REGION_FORMAT_CHANGED*/
53 }
54
55 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
56 {
57         /*APP_EVENT_LOW_BATTERY*/
58 }
59
60 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
61 {
62         /*APP_EVENT_LOW_MEMORY*/
63 }
64
65 static void _control_led(peripheral_gpio_h gpio, int power_on)
66 {
67         dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO.");
68         peripheral_gpio_write(gpio, power_on);
69 }
70
71 static gboolean _toggle_led(gpointer user_data)
72 {
73         static int current_status = 0;
74
75         if (current_status) current_status = 0;
76         else current_status = 1;
77
78         _control_led(user_data, current_status);
79
80         return TRUE;
81 }
82
83 int main(int argc, char* argv[])
84 {
85     char ad[50] = {0,};
86         service_app_lifecycle_callback_s event_callback;
87         app_event_handler_h handlers[5] = {NULL, };
88         peripheral_gpio_h gpio = NULL;
89         guint timer_id = 0;
90         int ret = 0;
91
92         event_callback.create = service_app_create;
93         event_callback.terminate = service_app_terminate;
94         event_callback.app_control = service_app_control;
95
96         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
97         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
98         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
99         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
100
101         peripheral_gpio_open(GPIO_NUM, &gpio);
102         if (!gpio) {
103                 dlog_print(DLOG_INFO, LOG_TAG, "The return value of peripheral_gpio_open is null.");
104                 return 1;
105         }
106
107         ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
108         if (ret != 0) {
109                 dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
110                 return 1;
111         }
112
113         // Return value : the ID (greater than 0) of the event source.
114         timer_id = g_timeout_add_seconds(1, _toggle_led, gpio);
115         if (timer_id <= 0) {
116                 dlog_print(DLOG_INFO, LOG_TAG, "Timer id : %d", timer_id);
117                 return 1;
118         }
119
120         ret = service_app_main(argc, argv, &event_callback, ad);
121
122         g_source_remove(timer_id);
123         peripheral_gpio_close(gpio);
124
125         return ret;
126 }
127