gpio.c: fix styling of functions
[contrib/mraa.git] / src / gpio / gpio.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <unistd.h>
29
30 #include "gpio.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 static int
37 gpio_get_valfp(gpio_t *gpio)
38 {
39     char bu[64];
40     sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin);
41
42     if((gpio->value_fp = fopen(bu, "r+b")) == NULL) {
43         return 1;
44     }
45     return 0;
46 }
47
48 void
49 gpio_init(gpio_t *gpio, int pin)
50 {
51     FILE *export_f;
52
53     if((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
54         fprintf(stderr, "Failed to open export for writing!\n");
55     } else {
56         fprintf(export_f, "%d", pin);
57         fclose(export_f);
58     }
59     gpio->pin = pin;
60 }
61
62 int
63 gpio_set(int pin)
64 {
65     //Stuff
66     return 0;
67 }
68
69 void
70 gpio_mode(gpio_t *gpio, gpio_mode_t mode)
71 {
72     //gpio->pin
73 }
74
75 void
76 gpio_dir(gpio_t *gpio, gpio_dir_t dir)
77 {
78     if(gpio->value_fp != NULL) {
79          gpio->value_fp = NULL;
80     }
81     char filepath[64];
82     snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin);
83
84     FILE *direction;
85     if((direction = fopen(filepath, "w")) == NULL) {
86         fprintf(stderr, "Failed to open direction for writing!\n");
87     } else {
88         fprintf(direction, dir);
89         fclose(direction);
90         gpio->value_fp = NULL;
91     }
92 }
93
94 int
95 gpio_read(gpio_t *gpio)
96 {
97     if(gpio->value_fp == NULL) {
98         gpio_get_valfp(gpio);
99     }
100     fseek(gpio->value_fp, SEEK_SET, 0);
101     char buffer[2];
102     fread(buffer, 2, 1, gpio->value_fp);
103     fseek(gpio->value_fp, SEEK_SET, 0);
104     return atoi(buffer);
105 }
106
107 void
108 gpio_write(gpio_t *gpio, int value)
109 {
110     if(gpio->value_fp == NULL) {
111         gpio_get_valfp(gpio);
112     }
113     fseek(gpio->value_fp, SEEK_SET, 0);
114     fprintf(gpio->value_fp, "%d", value);
115     fseek(gpio->value_fp, SEEK_SET, 0);
116
117 }
118
119 void
120 gpio_close(gpio_t *gpio)
121 {
122     FILE *unexport_f;
123
124     if((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
125         fprintf(stderr, "Failed to open unexport for writing!\n");
126     } else {
127         fprintf(unexport_f, "%d", gpio->pin);
128         fclose(unexport_f);
129     }
130 }
131
132 #ifdef __cplusplus
133 }
134 #endif