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.
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
27 #if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600
28 #define _XOPEN_SOURCE 600 /* Get nftw() and S_IFSOCK declarations */
44 #include "mraa_internal.h"
48 mraa_board_t* plat = NULL;
49 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
50 mraa_adv_func_t* advance_func;
59 mraa_set_log_level(int level)
61 if (level <= 7 && level >= 0) {
62 setlogmask(LOG_UPTO(level));
63 syslog(LOG_DEBUG, "Loglevel %d is set", level);
66 syslog(LOG_NOTICE, "Invalid loglevel %d requested", level);
67 return MRAA_ERROR_INVALID_PARAMETER;
70 #if (defined SWIGPYTHON) || (defined SWIG)
73 mraa_result_t __attribute__((constructor))
78 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
81 uid_t proc_euid = geteuid();
82 struct passwd* proc_user = getpwuid(proc_euid);
85 setlogmask(LOG_UPTO(LOG_DEBUG));
87 setlogmask(LOG_UPTO(LOG_NOTICE));
90 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
91 syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
92 mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
95 // Initialise python threads, this allows use to grab the GIL when we are
101 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
102 memset(advance_func, 0, sizeof(mraa_adv_func_t));
105 // Use runtime x86 platform detection
106 platform_type = mraa_x86_platform();
107 #elif defined(ARMPLAT)
108 // Use runtime ARM platform detection
109 platform_type = mraa_arm_platform();
111 #error mraa_ARCH NOTHING
115 // This is a platform extender so create null base platform if one doesn't already exist
117 plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
118 plat->platform_name = "Unknown platform";
120 int usb_platform_type = mraa_usb_platform_extender(plat);
121 if (platform_type == MRAA_UNKNOWN_PLATFORM) {
122 platform_type = usb_platform_type;
127 printf("mraa: FATAL error, failed to initialise platform\n");
128 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
132 syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), platform_type);
140 if (plat->pins != NULL) {
149 mraa_set_priority(const unsigned int priority)
151 struct sched_param sched_s;
153 memset(&sched_s, 0, sizeof(struct sched_param));
154 if (priority > sched_get_priority_max(SCHED_RR)) {
155 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
157 sched_s.sched_priority = priority;
160 return sched_setscheduler(0, SCHED_RR, &sched_s);
164 mraa_setup_mux_mapped(mraa_pin_t meta)
168 for (mi = 0; mi < meta.mux_total; mi++) {
169 mraa_gpio_context mux_i;
170 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
172 return MRAA_ERROR_INVALID_HANDLE;
174 // this function will sometimes fail, however this is not critical as
175 // long as the write succeeds - Test case galileo gen2 pin2
176 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
177 mraa_gpio_owner(mux_i, 0);
179 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
180 mraa_gpio_close(mux_i);
181 return MRAA_ERROR_INVALID_RESOURCE;
183 mraa_gpio_close(mux_i);
190 mraa_result_print(mraa_result_t result)
194 fprintf(stdout, "MRAA: SUCCESS\n");
196 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
197 fprintf(stdout, "MRAA: Feature not implemented.\n");
199 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
200 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
202 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
203 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
205 case MRAA_ERROR_INVALID_PARAMETER:
206 fprintf(stdout, "MRAA: Invalid parameter.\n");
208 case MRAA_ERROR_INVALID_HANDLE:
209 fprintf(stdout, "MRAA: Invalid Handle.\n");
211 case MRAA_ERROR_NO_RESOURCES:
212 fprintf(stdout, "MRAA: No resources.\n");
214 case MRAA_ERROR_INVALID_RESOURCE:
215 fprintf(stdout, "MRAA: Invalid resource.\n");
217 case MRAA_ERROR_INVALID_QUEUE_TYPE:
218 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
220 case MRAA_ERROR_NO_DATA_AVAILABLE:
221 fprintf(stdout, "MRAA: No Data available.\n");
223 case MRAA_ERROR_INVALID_PLATFORM:
224 fprintf(stdout, "MRAA: Platform not recognised.\n");
226 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
227 fprintf(stdout, "MRAA: Platform not initialised.\n");
229 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
230 fprintf(stdout, "MRAA: Platform already initialised.\n");
232 case MRAA_ERROR_UNSPECIFIED:
233 fprintf(stdout, "MRAA: Unspecified Error.\n");
236 fprintf(stdout, "MRAA: Unrecognised error.\n");
242 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
247 if (pin > (plat->phy_pin_count - 1) || pin < 0)
252 if (plat->pins[pin].capabilites.valid == 1)
256 if (plat->pins[pin].capabilites.gpio == 1)
260 if (plat->pins[pin].capabilites.pwm == 1)
263 case MRAA_PIN_FAST_GPIO:
264 if (plat->pins[pin].capabilites.fast_gpio == 1)
268 if (plat->pins[pin].capabilites.spi == 1)
272 if (plat->pins[pin].capabilites.i2c == 1)
276 if (plat->pins[pin].capabilites.aio == 1)
280 if (plat->pins[pin].capabilites.uart == 1)
284 syslog(LOG_NOTICE, "requested pinmode invalid");
291 mraa_get_platform_type()
293 return platform_type;
302 if (plat->aio_count == 0)
305 return plat->adc_raw;
309 mraa_adc_supported_bits()
314 if (plat->aio_count == 0)
317 return plat->adc_supported;
321 mraa_get_platform_name()
326 return (char*) plat->platform_name;
330 mraa_get_i2c_bus_count()
335 return plat->i2c_bus_count;
339 mraa_get_i2c_bus_id(unsigned i2c_bus)
345 if (i2c_bus >= plat->i2c_bus_count) {
349 return plat->i2c_bus[i2c_bus].bus_id;
358 return plat->phy_pin_count;
362 mraa_get_pin_name(int pin)
367 if (pin > (plat->phy_pin_count - 1) || pin < 0)
369 return (char*) plat->pins[pin].name;
373 mraa_get_default_i2c_bus()
378 return plat->def_i2c_bus;
384 mraa_file_exist(const char* filename)
387 results.gl_pathc = 0;
388 glob(filename, 0, NULL, &results);
389 int file_found = results.gl_pathc == 1;
395 mraa_file_contains(const char* filename, const char* content)
397 mraa_boolean_t found = 0;
398 if ((filename == NULL) || (content == NULL)) {
402 char* file = mraa_file_unglob(filename);
405 char* line = malloc(len);
410 FILE* fh = fopen(file, "r");
416 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
417 if (strstr(line, content)) {
430 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
432 mraa_boolean_t found = 0;
433 if ((filename == NULL) || (content == NULL)) {
437 char* file = mraa_file_unglob(filename);
440 char* line = malloc(len);
445 FILE* fh = fopen(file, "r");
451 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
452 if (strstr(line, content) && strstr(line, content2)) {
465 mraa_file_unglob(const char* filename)
469 results.gl_pathc = 0;
470 glob(filename, 0, NULL, &results);
471 if (results.gl_pathc == 1)
472 res = strdup(results.gl_pathv[0]);
478 mraa_link_targets(const char* filename, const char* targetname)
483 while (nchars == 0) {
484 buffer = (char*) realloc(buffer, size);
487 nchars = readlink(filename, buffer, size);
492 buffer[nchars] = '\0';
494 if (nchars >= size) {
499 if (strstr(buffer, targetname)) {
508 static int num_i2c_devices = 0;
511 mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
513 switch (sb->st_mode & S_IFMT) {
522 mraa_find_i2c_bus(const char* devname, int startfrom)
529 // because feeding mraa_find_i2c_bus result back into the function is
530 // useful treat -1 as 0
535 // find how many i2c buses we have if we haven't already
536 if (num_i2c_devices == 0) {
537 if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) {
542 // i2c devices are numbered numerically so 0 must exist otherwise there is
544 if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
545 for (i; i < num_i2c_devices; i++) {
547 snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
548 fd = open(path, O_RDONLY);
552 size = lseek(fd, 0, SEEK_END);
554 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
558 err = lseek(fd, 0, SEEK_SET);
560 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
564 char* value = malloc(size);
566 syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
570 ssize_t r = read(fd, value, size);
572 if (strcasestr(value, devname) != NULL) {
578 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
584 syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");