mraa.c: remove DEBUG define
[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     free(plat->pins);
122     free(plat);
123     closelog();
124 }
125
126 int
127 mraa_set_priority(const unsigned int priority)
128 {
129     struct sched_param sched_s;
130
131     memset(&sched_s, 0, sizeof(struct sched_param));
132     if (priority > sched_get_priority_max(SCHED_RR)) {
133         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
134     }
135     else {
136         sched_s.sched_priority = priority;
137     }
138
139     return sched_setscheduler(0, SCHED_RR, &sched_s);
140 }
141
142 mraa_result_t
143 mraa_setup_mux_mapped(mraa_pin_t meta)
144 {
145     int mi;
146
147     for (mi = 0; mi < meta.mux_total; mi++) {
148         mraa_gpio_context mux_i;
149         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
150         if (mux_i == NULL) {
151             return MRAA_ERROR_INVALID_HANDLE;
152         }
153         // this function will sometimes fail, however this is not critical as
154         // long as the write succeeds - Test case galileo gen2 pin2
155         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
156
157         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
158             mraa_gpio_close(mux_i);
159             return MRAA_ERROR_INVALID_RESOURCE;
160         }
161         mraa_gpio_close(mux_i);
162     }
163
164     return MRAA_SUCCESS;
165 }
166
167 void
168 mraa_result_print(mraa_result_t result)
169 {
170     switch (result) {
171         case MRAA_SUCCESS:
172             fprintf(stdout, "MRAA: SUCCESS\n");
173             break;
174         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
175             fprintf(stdout, "MRAA: Feature not implemented.\n");
176             break;
177         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
178             fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
179             break;
180         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
181             fprintf(stdout, "MRAA: Invalid verbosity level.\n");
182             break;
183         case MRAA_ERROR_INVALID_PARAMETER:
184             fprintf(stdout, "MRAA: Invalid parameter.\n");
185             break;
186         case MRAA_ERROR_INVALID_HANDLE:
187             fprintf(stdout, "MRAA: Invalid Handle.\n");
188             break;
189         case MRAA_ERROR_NO_RESOURCES:
190             fprintf(stdout, "MRAA: No resources.\n");
191             break;
192         case MRAA_ERROR_INVALID_RESOURCE:
193             fprintf(stdout, "MRAA: Invalid resource.\n");
194             break;
195         case MRAA_ERROR_INVALID_QUEUE_TYPE:
196             fprintf(stdout, "MRAA: Invalid Queue Type.\n");
197             break;
198         case MRAA_ERROR_NO_DATA_AVAILABLE:
199             fprintf(stdout, "MRAA: No Data available.\n");
200             break;
201         case MRAA_ERROR_INVALID_PLATFORM:
202             fprintf(stdout, "MRAA: Platform not recognised.\n");
203             break;
204         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
205             fprintf(stdout, "MRAA: Platform not initialised.\n");
206             break;
207         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
208             fprintf(stdout, "MRAA: Platform already initialised.\n");
209             break;
210         case MRAA_ERROR_UNSPECIFIED:
211             fprintf(stdout, "MRAA: Unspecified Error.\n");
212             break;
213         default:
214             fprintf(stdout, "MRAA: Unrecognised error.\n");
215             break;
216     }
217 }
218
219 mraa_boolean_t
220 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
221 {
222     if (plat == NULL) {
223         mraa_init();
224         if (plat == NULL)
225             return 0;
226     }
227     if (pin > plat->phy_pin_count || pin < 0)
228         return 0;
229
230     switch(mode) {
231         case MRAA_PIN_VALID:
232             if (plat->pins[pin].capabilites.valid == 1)
233                 return 1;
234             break;
235         case MRAA_PIN_GPIO:
236             if (plat->pins[pin].capabilites.gpio ==1)
237                 return 1;
238             break;
239         case MRAA_PIN_PWM:
240             if (plat->pins[pin].capabilites.pwm ==1)
241                 return 1;
242             break;
243         case MRAA_PIN_FAST_GPIO:
244             if (plat->pins[pin].capabilites.fast_gpio ==1)
245                 return 1;
246             break;
247         case MRAA_PIN_SPI:
248             if (plat->pins[pin].capabilites.spi ==1)
249                 return 1;
250             break;
251         case MRAA_PIN_I2C:
252             if (plat->pins[pin].capabilites.i2c ==1)
253                 return 1;
254             break;
255         case MRAA_PIN_AIO:
256             if (pin < plat->aio_count)
257                 pin = pin + plat->gpio_count;
258             if (plat->pins[pin].capabilites.aio ==1)
259                 return 1;
260             break;
261         default: break;
262     }
263     return 0;
264 }
265
266 mraa_platform_t mraa_get_platform_type()
267 {
268     return platform_type;
269 }
270
271 unsigned int
272 mraa_adc_raw_bits()
273 {
274     if (plat == NULL)
275         return 0;
276
277     if (plat->aio_count == 0)
278         return 0;
279
280     return plat->adc_raw;
281 }
282
283 unsigned int
284 mraa_adc_supported_bits()
285 {
286     if (plat == NULL)
287         return 0;
288
289     if (plat->aio_count == 0)
290         return 0;
291
292     return plat->adc_supported;
293 }
294
295 mraa_result_t
296 mraa_setup_uart(int index)
297 {
298     if (plat == NULL)
299         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
300
301     if (plat->uart_dev_count == 0)
302         return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
303
304     if (plat->uart_dev_count <= index)
305         return MRAA_ERROR_NO_RESOURCES;
306
307     int pos = plat->uart_dev[index].rx;
308     if (pos >= 0) {
309         if (plat->pins[pos].uart.mux_total > 0)
310             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
311                 return MRAA_ERROR_INVALID_RESOURCE;
312     }
313
314     if (pos >= 0) {
315         pos = plat->uart_dev[index].tx;
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     return MRAA_SUCCESS;
322 }
323
324 char*
325 mraa_get_platform_name()
326 {
327     if (plat == NULL)
328         return "Unknown";
329     return plat->platform_name;
330 }