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