d8e86e331f9c3d97a54ee9e7c1eeed8501d2a0e3
[apps/native/position-finder-server.git] / src / resource / resource_infrared_motion_sensor.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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 Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
27 #include "log.h"
28 #include "resource_internal.h"
29
30 void resource_close_infrared_motion_sensor(int pin_num)
31 {
32         ret_if(!resource_get_info(pin_num)->opened);
33
34         _I("Infrared Motion Sensor is finishing...");
35         resource_get_info(pin_num)->opened = 0;
36 }
37
38 int resource_read_infrared_motion_sensor(int pin_num, int *out_value)
39 {
40         int ret = PERIPHERAL_ERROR_NONE;
41
42         if (!resource_get_info(pin_num)->opened) {
43                 ret = peripheral_gpio_open(pin_num, &resource_get_info(pin_num)->sensor_h);
44                 retv_if(!resource_get_info(pin_num)->sensor_h, -1);
45
46                 ret = peripheral_gpio_set_direction(resource_get_info(pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
47                 retv_if(ret != 0, -1);
48
49                 resource_get_info(pin_num)->opened = 1;
50         }
51
52         ret = peripheral_gpio_read(resource_get_info(pin_num)->sensor_h, out_value);
53         retv_if(ret < 0, -1);
54
55         _I("Infrared Motion Sensor Value : %d", *out_value);
56
57         return 0;
58 }