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