Add an indent for consistency
[apps/native/position-finder-server.git] / src / resource / resource_ultrasonic_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 <unistd.h>
24 #include <peripheral_io.h>
25 #include <sys/time.h>
26 #include <gio/gio.h>
27
28 #include "log.h"
29 #include "resource_internal.h"
30
31 static resource_read_s *resource_read_info = NULL;
32 static unsigned long long triggered_time = 0;
33
34 void resource_close_ultrasonic_sensor_trig(int trig_pin_num)
35 {
36         if (!resource_get_info(trig_pin_num)->opened) return;
37
38         _I("Ultrasonic sensor's trig is finishing...");
39
40         peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
41         resource_get_info(trig_pin_num)->opened = 0;
42 }
43
44 void resource_close_ultrasonic_sensor_echo(int echo_pin_num)
45 {
46         if (!resource_get_info(echo_pin_num)->opened) return;
47
48         _I("Ultrasonic sensor's echo is finishing...");
49
50         peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
51         resource_get_info(echo_pin_num)->opened = 0;
52         free(resource_read_info);
53         resource_read_info = NULL;
54 }
55
56 static unsigned long long _get_timestamp(void)
57 {
58         struct timespec t;
59         clock_gettime(CLOCK_REALTIME, &t);
60         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
61 }
62
63 static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
64 {
65         float dist = 0;
66         uint32_t value;
67         unsigned long long returned_time = 0;
68         resource_read_s *resource_read_info = user_data;
69
70         ret_if(!resource_read_info);
71         ret_if(!resource_read_info->cb);
72
73         peripheral_gpio_read(gpio, &value);
74
75         if (value == 1) {
76                 triggered_time = _get_timestamp();
77         } else if (value == 0) {
78                 returned_time = _get_timestamp();
79         }
80
81         if (triggered_time > 0 && value == 0) {
82                 dist = returned_time - triggered_time;
83                 if (dist < 150 || dist > 25000) {
84                         dist = -1;
85                 } else {
86                         dist = (dist * 34300) / 2000000;
87                 }
88
89                 resource_read_info->cb(dist, resource_read_info->data);
90         }
91 }
92
93 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
94 {
95         int ret = 0;
96
97         triggered_time = 0;
98
99         if (resource_read_info == NULL) {
100                 resource_read_info = calloc(1, sizeof(resource_read_s));
101                 retv_if(!resource_read_info, -1);
102         } else {
103                 peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
104         }
105         resource_read_info->cb = cb;
106         resource_read_info->data = data;
107         resource_read_info->pin_num = echo_pin_num;
108
109         if (!resource_get_info(trig_pin_num)->opened) {
110                 _I("Ultrasonic sensor's trig is initializing...");
111
112                 ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
113                 retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);
114
115                 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
116                 retv_if(ret != 0, -1);
117
118                 resource_get_info(trig_pin_num)->opened = 1;
119                 resource_get_info(trig_pin_num)->close = resource_close_ultrasonic_sensor_trig;
120         }
121
122         if (!resource_get_info(echo_pin_num)->opened) {
123                 _I("Ultrasonic sensor's echo is initializing...");
124
125                 ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
126                 retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
127
128                 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
129                 retv_if(ret != 0, -1);
130
131                 ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
132                 retv_if(ret != 0, -1);
133
134                 resource_get_info(echo_pin_num)->opened = 1;
135                 resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
136         }
137
138         if (resource_get_info(echo_pin_num)->sensor_h) {
139                 ret = peripheral_gpio_set_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
140                 retv_if(ret != 0, -1);
141         }
142
143         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
144         retv_if(ret < 0, -1);
145
146         usleep(20000);
147
148         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
149         retv_if(ret < 0, -1);
150
151         usleep(20000);
152
153         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
154         retv_if(ret < 0, -1);
155
156         return 0;
157 }