fix code rule
[apps/native/gear-racing-car.git] / src / resource / resource_infrared_obstacle_avoidance_sensor.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Flora License, Version 1.1 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://floralicense.org/license/
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdlib.h>
23 #include <peripheral_io.h>
24 #include "log.h"
25 #include "resource_internal.h"
26
27 void resource_close_infrared_obstacle_avoidance_sensor(int pin_num)
28 {
29         if (!resource_get_info(pin_num)->opened) return;
30
31         _I("Infrared Obstacle Avoidance Sensor is finishing...");
32         if (resource_get_info(pin_num)->resource_changed_info) {
33                 free(resource_get_info(pin_num)->resource_changed_info);
34                 resource_get_info(pin_num)->resource_changed_info = NULL;
35         }
36         peripheral_gpio_unset_interrupted_cb(resource_get_info(pin_num)->sensor_h);
37         peripheral_gpio_close(resource_get_info(pin_num)->sensor_h);
38         resource_get_info(pin_num)->sensor_h = NULL;
39         resource_get_info(pin_num)->opened = 0;
40
41         return;
42 }
43
44 static int _init_pin(int pin_num)
45 {
46         int ret = PERIPHERAL_ERROR_NONE;
47
48         ret = peripheral_gpio_open(pin_num, &resource_get_info(pin_num)->sensor_h);
49         retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
50
51         ret = peripheral_gpio_set_direction(resource_get_info(pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
52         if (ret != PERIPHERAL_ERROR_NONE) {
53                 peripheral_gpio_close(resource_get_info(pin_num)->sensor_h);
54                 resource_get_info(pin_num)->sensor_h = NULL;
55                 return -1;
56         }
57
58         resource_get_info(pin_num)->opened = 1;
59         resource_get_info(pin_num)->close = resource_close_infrared_obstacle_avoidance_sensor;
60
61         return 0;
62 }
63
64 int resource_read_infrared_obstacle_avoidance_sensor(int pin_num, unsigned int *out_value)
65 {
66         int ret = PERIPHERAL_ERROR_NONE;
67
68         if (!resource_get_info(pin_num)->opened) {
69                 ret = _init_pin(pin_num);
70                 retv_if(ret < 0, -1);
71         }
72
73         ret = peripheral_gpio_read(resource_get_info(pin_num)->sensor_h, out_value);
74         retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
75
76         *out_value = !*out_value;
77
78         _I("Infrared Obstacle Avoidance Sensor Value : %d", *out_value);
79
80         return 0;
81 }
82
83
84 static void __ioa_sensor_interrupt_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
85 {
86         uint32_t value;
87         resource_changed_s *resource_changed_info = (resource_changed_s *)user_data;
88
89         /* Detected : 0, Non-detected : 1 */
90         peripheral_gpio_read(gpio, &value);
91         _D("interrupt value = %d", !value);
92
93         resource_changed_info->cb(!value, resource_changed_info->data);
94
95         return;
96 }
97
98 int resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(int pin_num, resource_changed_cb cb, void *data)
99 {
100         int ret = PERIPHERAL_ERROR_NONE;
101
102         if (resource_get_info(pin_num)->resource_changed_info == NULL) {
103                 resource_get_info(pin_num)->resource_changed_info = calloc(1, sizeof(resource_changed_s));
104                 retv_if(!resource_get_info(pin_num)->resource_changed_info, -1);
105         } else {
106                 if (resource_get_info(pin_num)->sensor_h)
107                         peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_get_info(pin_num)->resource_changed_info->pin_num)->sensor_h);
108         }
109
110         resource_get_info(pin_num)->resource_changed_info->cb = cb;
111         resource_get_info(pin_num)->resource_changed_info->data = data;
112         resource_get_info(pin_num)->resource_changed_info->pin_num = pin_num;
113
114         if (!resource_get_info(pin_num)->opened) {
115                 ret = _init_pin(pin_num);
116                 goto_if(ret < 0, FREE_RESOURCE);
117         }
118
119         if (resource_get_info(pin_num)->sensor_h) {
120                 ret = peripheral_gpio_set_edge_mode(resource_get_info(pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
121                 goto_if(ret != PERIPHERAL_ERROR_NONE, FREE_RESOURCE);
122
123                 ret = peripheral_gpio_set_interrupted_cb(resource_get_info(pin_num)->sensor_h, __ioa_sensor_interrupt_cb, resource_get_info(pin_num)->resource_changed_info);
124                 goto_if(ret != PERIPHERAL_ERROR_NONE, FREE_RESOURCE);
125         }
126
127         return 0;
128
129 FREE_RESOURCE:
130         if (resource_get_info(pin_num)->resource_changed_info) {
131                 free(resource_get_info(pin_num)->resource_changed_info);
132                 resource_get_info(pin_num)->resource_changed_info = NULL;
133         }
134
135         if (resource_get_info(pin_num)->sensor_h) {
136                 peripheral_gpio_unset_interrupted_cb(resource_get_info(pin_num)->sensor_h);
137                 peripheral_gpio_close(resource_get_info(pin_num)->sensor_h);
138                 resource_get_info(pin_num)->sensor_h = NULL;
139                 resource_get_info(pin_num)->opened = 0;
140         }
141
142         return -1;
143 }