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