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