mraa.c: mraa_pin_mode_test should check against b->gpio_count -1
[contrib/mraa.git] / src / mraa.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
4  * Copyright (c) 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 <stddef.h>
27 #include <stdlib.h>
28 #include <sched.h>
29 #include <string.h>
30 #include <pwd.h>
31
32 #include "mraa_internal.h"
33 #include "gpio.h"
34 #include "version.h"
35
36 mraa_board_t* plat = NULL;
37 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
38 mraa_adv_func_t* advance_func;
39
40 const char *
41 mraa_get_version()
42 {
43     return gVERSION;
44 }
45
46 mraa_result_t
47 mraa_set_log_level(int level)
48 {
49     if (level <= 7 && level >= 0) {
50         setlogmask(LOG_UPTO(level));
51         return MRAA_SUCCESS;
52     }
53     return MRAA_ERROR_INVALID_PARAMETER;
54 }
55
56 mraa_result_t __attribute__((constructor))
57 mraa_init()
58 {
59     if (plat != NULL) {
60         return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
61     }
62
63     uid_t proc_euid = geteuid();
64     struct passwd *proc_user = getpwuid(proc_euid);
65
66 #ifdef DEBUG
67     setlogmask(LOG_UPTO(LOG_DEBUG));
68 #else
69     setlogmask(LOG_UPTO(LOG_NOTICE));
70 #endif
71
72     openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
73     syslog(LOG_DEBUG,
74            "libmraa initialised by user '%s' with EUID %d",
75            (proc_user != NULL) ? proc_user->pw_name : "<unknown>",
76            proc_euid);
77
78     if (proc_euid != 0) {
79         char *err_msg = "mraa: FATAL error, "
80                         "libmraa program must be run as root (EUID 0), "
81                         "cannot proceed\n";
82         syslog(LOG_ERR, err_msg);
83         fprintf(stderr, err_msg);
84         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
85     }
86
87 #ifdef SWIGPYTHON
88     // Initialise python threads, this allows use to grab the GIL when we are
89     // required to do so
90     Py_InitializeEx(0);
91     PyEval_InitThreads();
92 #endif
93     advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
94     memset(advance_func, 0, sizeof(mraa_adv_func_t));
95
96 #ifdef X86PLAT
97     // Use runtime x86 platform detection
98     platform_type = mraa_x86_platform();
99 #elif ARMPLAT
100     // Use runtime ARM platform detection
101     platform_type = mraa_arm_platform();
102 #else
103     #error mraa_ARCH NOTHING
104 #endif
105
106     if (plat == NULL) {
107         printf("mraa: FATAL error, failed to initialise platform\n");
108         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
109     }
110
111     syslog(LOG_INFO,
112            "libmraa initialised for platform '%s' of type %d",
113            mraa_get_platform_name(),
114            platform_type);
115     return MRAA_SUCCESS;
116 }
117
118 void
119 mraa_deinit()
120 {
121     if (plat != NULL) {
122         if (plat->pins != NULL) {
123             free(plat->pins);
124         }
125         free(plat);
126     }
127     closelog();
128 }
129
130 int
131 mraa_set_priority(const unsigned int priority)
132 {
133     struct sched_param sched_s;
134
135     memset(&sched_s, 0, sizeof(struct sched_param));
136     if (priority > sched_get_priority_max(SCHED_RR)) {
137         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
138     }
139     else {
140         sched_s.sched_priority = priority;
141     }
142
143     return sched_setscheduler(0, SCHED_RR, &sched_s);
144 }
145
146 mraa_result_t
147 mraa_setup_mux_mapped(mraa_pin_t meta)
148 {
149     int mi;
150
151     for (mi = 0; mi < meta.mux_total; mi++) {
152         mraa_gpio_context mux_i;
153         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
154         if (mux_i == NULL) {
155             return MRAA_ERROR_INVALID_HANDLE;
156         }
157         // this function will sometimes fail, however this is not critical as
158         // long as the write succeeds - Test case galileo gen2 pin2
159         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
160
161         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
162             mraa_gpio_close(mux_i);
163             return MRAA_ERROR_INVALID_RESOURCE;
164         }
165         mraa_gpio_close(mux_i);
166     }
167
168     return MRAA_SUCCESS;
169 }
170
171 void
172 mraa_result_print(mraa_result_t result)
173 {
174     switch (result) {
175         case MRAA_SUCCESS:
176             fprintf(stdout, "MRAA: SUCCESS\n");
177             break;
178         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
179             fprintf(stdout, "MRAA: Feature not implemented.\n");
180             break;
181         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
182             fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
183             break;
184         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
185             fprintf(stdout, "MRAA: Invalid verbosity level.\n");
186             break;
187         case MRAA_ERROR_INVALID_PARAMETER:
188             fprintf(stdout, "MRAA: Invalid parameter.\n");
189             break;
190         case MRAA_ERROR_INVALID_HANDLE:
191             fprintf(stdout, "MRAA: Invalid Handle.\n");
192             break;
193         case MRAA_ERROR_NO_RESOURCES:
194             fprintf(stdout, "MRAA: No resources.\n");
195             break;
196         case MRAA_ERROR_INVALID_RESOURCE:
197             fprintf(stdout, "MRAA: Invalid resource.\n");
198             break;
199         case MRAA_ERROR_INVALID_QUEUE_TYPE:
200             fprintf(stdout, "MRAA: Invalid Queue Type.\n");
201             break;
202         case MRAA_ERROR_NO_DATA_AVAILABLE:
203             fprintf(stdout, "MRAA: No Data available.\n");
204             break;
205         case MRAA_ERROR_INVALID_PLATFORM:
206             fprintf(stdout, "MRAA: Platform not recognised.\n");
207             break;
208         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
209             fprintf(stdout, "MRAA: Platform not initialised.\n");
210             break;
211         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
212             fprintf(stdout, "MRAA: Platform already initialised.\n");
213             break;
214         case MRAA_ERROR_UNSPECIFIED:
215             fprintf(stdout, "MRAA: Unspecified Error.\n");
216             break;
217         default:
218             fprintf(stdout, "MRAA: Unrecognised error.\n");
219             break;
220     }
221 }
222
223 mraa_boolean_t
224 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
225 {
226     if (plat == NULL) {
227         mraa_init();
228         if (plat == NULL)
229             return 0;
230     }
231     if (pin > (plat->phy_pin_count -1) || pin < 0)
232         return 0;
233
234     switch(mode) {
235         case MRAA_PIN_VALID:
236             if (plat->pins[pin].capabilites.valid == 1)
237                 return 1;
238             break;
239         case MRAA_PIN_GPIO:
240             if (plat->pins[pin].capabilites.gpio ==1)
241                 return 1;
242             break;
243         case MRAA_PIN_PWM:
244             if (plat->pins[pin].capabilites.pwm ==1)
245                 return 1;
246             break;
247         case MRAA_PIN_FAST_GPIO:
248             if (plat->pins[pin].capabilites.fast_gpio ==1)
249                 return 1;
250             break;
251         case MRAA_PIN_SPI:
252             if (plat->pins[pin].capabilites.spi ==1)
253                 return 1;
254             break;
255         case MRAA_PIN_I2C:
256             if (plat->pins[pin].capabilites.i2c ==1)
257                 return 1;
258             break;
259         case MRAA_PIN_AIO:
260             if (plat->pins[pin].capabilites.aio ==1)
261                 return 1;
262             break;
263         case MRAA_PIN_UART:
264             if (plat->pins[pin].capabilites.uart == 1)
265                 return 1;
266         default:
267             syslog(LOG_NOTICE, "requested pinmode invalid");
268             break;
269     }
270     return 0;
271 }
272
273 mraa_platform_t mraa_get_platform_type()
274 {
275     return platform_type;
276 }
277
278 unsigned int
279 mraa_adc_raw_bits()
280 {
281     if (plat == NULL)
282         return 0;
283
284     if (plat->aio_count == 0)
285         return 0;
286
287     return plat->adc_raw;
288 }
289
290 unsigned int
291 mraa_adc_supported_bits()
292 {
293     if (plat == NULL)
294         return 0;
295
296     if (plat->aio_count == 0)
297         return 0;
298
299     return plat->adc_supported;
300 }
301
302 mraa_result_t
303 mraa_setup_uart(int index)
304 {
305     if (plat == NULL)
306         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
307
308     if (plat->uart_dev_count == 0)
309         return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
310
311     if (plat->uart_dev_count <= index)
312         return MRAA_ERROR_NO_RESOURCES;
313
314     int pos = plat->uart_dev[index].rx;
315     if (pos >= 0) {
316         if (plat->pins[pos].uart.mux_total > 0)
317             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
318                 return MRAA_ERROR_INVALID_RESOURCE;
319     }
320
321     if (pos >= 0) {
322         pos = plat->uart_dev[index].tx;
323         if (plat->pins[pos].uart.mux_total > 0)
324             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
325                 return MRAA_ERROR_INVALID_RESOURCE;
326     }
327
328     return MRAA_SUCCESS;
329 }
330
331 char*
332 mraa_get_platform_name()
333 {
334     if (plat == NULL)
335         return "Unknown";
336     return plat->platform_name;
337 }