+/*
+* Copyright (c) 2018 Samsung Electronics Co., Ltd.
+*
+* Licensed under the Flora License, Version 1.1 (the License);
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an AS IS BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#include <glib.h>
+#include <stdio.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <peripheral_io.h>
+#include <time.h>
+#include "log.h"
+#include "resource/resource_led.h"
+#include "config.h"
+
+#define DEFAULT_LED_RED 17
+#define DEFAULT_LED_GREEN 27
+#define CONFIG_KEY_RPI_PIN_LED_RED "led.red"
+#define CONFIG_KEY_RPI_PIN_LED_GREEN "led.green"
+#define CONFIG_GRP_RPI "Rpi"
+
+typedef struct _led_s {
+ peripheral_gpio_h gpio_red;
+ peripheral_gpio_h gpio_green;
+ led_color_e current_color;
+} led_s;
+
+static led_s s_info = { 0, };
+
+#define CHECK_GPIO_ERROR(pin, ret) \
+ if (ret != PERIPHERAL_ERROR_NONE) { \
+ peripheral_gpio_close(pin); \
+ _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
+ return ; \
+ }
+
+
+static inline void _led_set(led_color_e color)
+{
+ int red;
+ int green;
+
+
+ switch (color) {
+ case LED_COLOR_RED:
+ red = 1;
+ green = 0;
+ break;
+ case LED_COLOR_GREEN:
+ red = 0;
+ green = 1;
+ break;
+ default:
+ red = 0;
+ green = 0;
+ break;
+ }
+
+ int ret = peripheral_gpio_write(s_info.gpio_red, red);
+ CHECK_GPIO_ERROR(s_info.gpio_red, ret);
+
+ peripheral_gpio_write(s_info.gpio_green, green);
+ CHECK_GPIO_ERROR(s_info.gpio_green, ret);
+}
+
+static gboolean _restore_color_cb(gpointer data)
+{
+ _led_set(s_info.current_color);
+ return false;
+}
+
+static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
+{
+ peripheral_gpio_h gpio;
+// int ret = PERIPHERAL_ERROR_NONE;
+ int pin = 0;
+ bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
+
+ _D("gpio: %d", pin);
+
+ if (modified) {
+ config_save();
+ }
+
+ peripheral_gpio_open(pin, &gpio);
+// ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
+// CHECK_GPIO_ERROR(gpio, ret);
+
+ return gpio;
+
+}
+
+void resource_led_init(void)
+{
+ _D("Initialize Led");
+
+ s_info.gpio_red = _init_gpio(DEFAULT_LED_RED, CONFIG_KEY_RPI_PIN_LED_RED);
+ s_info.gpio_green = _init_gpio(DEFAULT_LED_GREEN, CONFIG_KEY_RPI_PIN_LED_GREEN);
+
+
+ int ret = peripheral_gpio_write(s_info.gpio_green, 1);
+ CHECK_GPIO_ERROR(s_info.gpio_green, ret);
+}
+
+void resource_led_destroy(void)
+{
+ peripheral_gpio_close(s_info.gpio_red);
+ peripheral_gpio_close(s_info.gpio_green);
+}
+
+void resource_led_set(led_color_e color)
+{
+ _D("Set led to: %d", color);
+ if (color == s_info.current_color) {
+ return;
+ }
+
+ s_info.current_color = color;
+ _led_set(color);
+}
+
+void resource_led_blink(led_color_e color, unsigned timeout)
+{
+ if (color == s_info.current_color) {
+ return;
+ }
+
+ _led_set(color);
+ g_timeout_add(timeout, _restore_color_cb, NULL);
+}
+