Merge pull request #4 from emea-ssg-drd/dumpc++
[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 static int
33 maa_gpio_get_valfp(maa_gpio_context *dev)
34 {
35     char bu[64];
36     sprintf(bu, "/sys/class/gpio/gpio%d/value", dev->pin);
37
38     if ((dev->value_fp = fopen(bu, "r+b")) == NULL) {
39         return 1;
40     }
41     return 0;
42 }
43
44 maa_gpio_context*
45 maa_gpio_init(int pin)
46 {
47     FILE *export_f;
48     maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
49
50     if ((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
51         fprintf(stderr, "Failed to open export for writing!\n");
52     } else {
53         fprintf(export_f, "%d", pin);
54         fclose(export_f);
55     }
56     dev->pin = pin;
57     return dev;
58 }
59
60 void
61 maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
62 {
63     //gpio->pin
64 }
65
66 void
67 maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
68 {
69     if (dev->value_fp != NULL) {
70          dev->value_fp = NULL;
71     }
72     char filepath[64];
73     snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", dev->pin);
74
75     FILE *direction;
76     if ((direction = fopen(filepath, "w")) == NULL) {
77         fprintf(stderr, "Failed to open direction for writing!\n");
78     } else {
79         fprintf(direction, dir);
80         fclose(direction);
81         dev->value_fp = NULL;
82     }
83 }
84
85 int
86 maa_gpio_read(maa_gpio_context *dev)
87 {
88     if (dev->value_fp == NULL) {
89         maa_gpio_get_valfp(dev);
90     }
91     fseek(dev->value_fp, SEEK_SET, 0);
92     char buffer[2];
93     fread(buffer, 2, 1, dev->value_fp);
94     fseek(dev->value_fp, SEEK_SET, 0);
95     return atoi(buffer);
96 }
97
98 void
99 maa_gpio_write(maa_gpio_context *dev, int value)
100 {
101     if (dev->value_fp == NULL) {
102         maa_gpio_get_valfp(dev);
103     }
104     fseek(dev->value_fp, SEEK_SET, 0);
105     fprintf(dev->value_fp, "%d", value);
106     fseek(dev->value_fp, SEEK_SET, 0);
107
108 }
109
110 void
111 maa_gpio_close(maa_gpio_context *dev)
112 {
113     FILE *unexport_f;
114
115     if ((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
116         fprintf(stderr, "Failed to open unexport for writing!\n");
117     } else {
118         fprintf(unexport_f, "%d", dev->pin);
119         fclose(unexport_f);
120     }
121 }