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