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 mraa_adv_func_t* advance_func;
58 mraa_set_log_level(int level)
60 if (level <= 7 && level >= 0) {
61 setlogmask(LOG_UPTO(level));
62 syslog(LOG_DEBUG, "Loglevel %d is set", level);
65 syslog(LOG_NOTICE, "Invalid loglevel %d requested", level);
66 return MRAA_ERROR_INVALID_PARAMETER;
69 #if (defined SWIGPYTHON) || (defined SWIG)
72 mraa_result_t __attribute__((constructor))
77 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
80 uid_t proc_euid = geteuid();
81 struct passwd* proc_user = getpwuid(proc_euid);
84 setlogmask(LOG_UPTO(LOG_DEBUG));
86 setlogmask(LOG_UPTO(LOG_NOTICE));
89 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
90 syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
91 mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
94 // Initialise python threads, this allows use to grab the GIL when we are
100 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
101 memset(advance_func, 0, sizeof(mraa_adv_func_t));
103 mraa_platform_t platform_type;
105 // Use runtime x86 platform detection
106 platform_type = mraa_x86_platform();
107 // x86 platforms have advanced_func stable in board config structure
109 advance_func = plat->adv_func;
110 #elif defined(ARMPLAT)
111 // Use runtime ARM platform detection
112 platform_type = mraa_arm_platform();
114 #error mraa_ARCH NOTHING
118 plat->platform_type = platform_type;
121 // This is a platform extender so create null base platform if one doesn't already exist
123 plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
125 plat->platform_type = MRAA_UNKNOWN_PLATFORM;
126 plat->platform_name = "Unknown platform";
129 // Now detect sub platform
131 mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
132 if (plat->platform_type == MRAA_UNKNOWN_PLATFORM) {
133 plat->platform_type = usb_platform_type;
137 printf("mraa: FATAL error, failed to initialise platform\n");
138 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
142 syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
150 if (plat->pins != NULL) {
159 mraa_set_priority(const unsigned int priority)
161 struct sched_param sched_s;
163 memset(&sched_s, 0, sizeof(struct sched_param));
164 if (priority > sched_get_priority_max(SCHED_RR)) {
165 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
167 sched_s.sched_priority = priority;
170 return sched_setscheduler(0, SCHED_RR, &sched_s);
174 mraa_setup_mux_mapped(mraa_pin_t meta)
178 for (mi = 0; mi < meta.mux_total; mi++) {
179 mraa_gpio_context mux_i;
180 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
182 return MRAA_ERROR_INVALID_HANDLE;
184 // this function will sometimes fail, however this is not critical as
185 // long as the write succeeds - Test case galileo gen2 pin2
186 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
187 mraa_gpio_owner(mux_i, 0);
189 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
190 mraa_gpio_close(mux_i);
191 return MRAA_ERROR_INVALID_RESOURCE;
193 mraa_gpio_close(mux_i);
200 mraa_result_print(mraa_result_t result)
204 fprintf(stdout, "MRAA: SUCCESS\n");
206 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
207 fprintf(stdout, "MRAA: Feature not implemented.\n");
209 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
210 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
212 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
213 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
215 case MRAA_ERROR_INVALID_PARAMETER:
216 fprintf(stdout, "MRAA: Invalid parameter.\n");
218 case MRAA_ERROR_INVALID_HANDLE:
219 fprintf(stdout, "MRAA: Invalid Handle.\n");
221 case MRAA_ERROR_NO_RESOURCES:
222 fprintf(stdout, "MRAA: No resources.\n");
224 case MRAA_ERROR_INVALID_RESOURCE:
225 fprintf(stdout, "MRAA: Invalid resource.\n");
227 case MRAA_ERROR_INVALID_QUEUE_TYPE:
228 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
230 case MRAA_ERROR_NO_DATA_AVAILABLE:
231 fprintf(stdout, "MRAA: No Data available.\n");
233 case MRAA_ERROR_INVALID_PLATFORM:
234 fprintf(stdout, "MRAA: Platform not recognised.\n");
236 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
237 fprintf(stdout, "MRAA: Platform not initialised.\n");
239 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
240 fprintf(stdout, "MRAA: Platform already initialised.\n");
242 case MRAA_ERROR_UNSPECIFIED:
243 fprintf(stdout, "MRAA: Unspecified Error.\n");
246 fprintf(stdout, "MRAA: Unrecognised error.\n");
252 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
257 if (pin > (plat->phy_pin_count - 1) || pin < 0)
262 if (plat->pins[pin].capabilites.valid == 1)
266 if (plat->pins[pin].capabilites.gpio == 1)
270 if (plat->pins[pin].capabilites.pwm == 1)
273 case MRAA_PIN_FAST_GPIO:
274 if (plat->pins[pin].capabilites.fast_gpio == 1)
278 if (plat->pins[pin].capabilites.spi == 1)
282 if (plat->pins[pin].capabilites.i2c == 1)
286 if (plat->pins[pin].capabilites.aio == 1)
290 if (plat->pins[pin].capabilites.uart == 1)
294 syslog(LOG_NOTICE, "requested pinmode invalid");
301 mraa_get_platform_type()
304 return MRAA_UNKNOWN_PLATFORM;
305 return plat->platform_type;
314 if (plat->aio_count == 0)
317 return plat->adc_raw;
321 mraa_adc_supported_bits()
326 if (plat->aio_count == 0)
329 return plat->adc_supported;
333 mraa_get_platform_name()
338 return (char*) plat->platform_name;
342 mraa_get_i2c_bus_count()
347 return plat->i2c_bus_count;
351 mraa_get_i2c_bus_id(unsigned i2c_bus)
357 if (i2c_bus >= plat->i2c_bus_count) {
361 return plat->i2c_bus[i2c_bus].bus_id;
370 return plat->phy_pin_count;
374 mraa_get_pin_name(int pin)
379 if (pin > (plat->phy_pin_count - 1) || pin < 0)
381 return (char*) plat->pins[pin].name;
385 mraa_get_default_i2c_bus()
390 return plat->def_i2c_bus;
396 mraa_file_exist(const char* filename)
399 results.gl_pathc = 0;
400 glob(filename, 0, NULL, &results);
401 int file_found = results.gl_pathc == 1;
407 mraa_file_contains(const char* filename, const char* content)
409 mraa_boolean_t found = 0;
410 if ((filename == NULL) || (content == NULL)) {
414 char* file = mraa_file_unglob(filename);
417 char* line = malloc(len);
422 FILE* fh = fopen(file, "r");
428 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
429 if (strstr(line, content)) {
442 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
444 mraa_boolean_t found = 0;
445 if ((filename == NULL) || (content == NULL)) {
449 char* file = mraa_file_unglob(filename);
452 char* line = malloc(len);
457 FILE* fh = fopen(file, "r");
463 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
464 if (strstr(line, content) && strstr(line, content2)) {
477 mraa_file_unglob(const char* filename)
481 results.gl_pathc = 0;
482 glob(filename, 0, NULL, &results);
483 if (results.gl_pathc == 1)
484 res = strdup(results.gl_pathv[0]);
490 mraa_link_targets(const char* filename, const char* targetname)
495 while (nchars == 0) {
496 buffer = (char*) realloc(buffer, size);
499 nchars = readlink(filename, buffer, size);
504 buffer[nchars] = '\0';
506 if (nchars >= size) {
511 if (strstr(buffer, targetname)) {
520 static int num_i2c_devices = 0;
523 mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
525 switch (sb->st_mode & S_IFMT) {
534 mraa_find_i2c_bus(const char* devname, int startfrom)
541 // because feeding mraa_find_i2c_bus result back into the function is
542 // useful treat -1 as 0
547 // find how many i2c buses we have if we haven't already
548 if (num_i2c_devices == 0) {
549 if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) {
554 // i2c devices are numbered numerically so 0 must exist otherwise there is
556 if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
557 for (i; i < num_i2c_devices; i++) {
559 snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
560 fd = open(path, O_RDONLY);
564 size = lseek(fd, 0, SEEK_END);
566 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
570 err = lseek(fd, 0, SEEK_SET);
572 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
576 char* value = malloc(size);
578 syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
582 ssize_t r = read(fd, value, size);
584 if (strcasestr(value, devname) != NULL) {
590 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
596 syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");
603 mraa_is_on_sub_platform(int pin_or_bus)
605 return (pin_or_bus & MRAA_SUB_PLATFORM_MASK) != 0;
609 mraa_use_sub_platform(int pin_or_bus)
611 return pin_or_bus | MRAA_SUB_PLATFORM_MASK;
615 mraa_get_sub_platform_index(int pin_or_bus)
617 return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK);