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