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 #define MAX_PLATFORM_NAME_LENGTH 128
49 mraa_board_t* plat = NULL;
50 // static mraa_board_t* current_plat = NULL;
52 static char platform_name[MAX_PLATFORM_NAME_LENGTH];
53 mraa_adv_func_t* advance_func;
62 mraa_set_log_level(int level)
64 if (level <= 7 && level >= 0) {
65 setlogmask(LOG_UPTO(level));
66 syslog(LOG_DEBUG, "Loglevel %d is set", level);
69 syslog(LOG_NOTICE, "Invalid loglevel %d requested", level);
70 return MRAA_ERROR_INVALID_PARAMETER;
73 #if (defined SWIGPYTHON) || (defined SWIG)
76 mraa_result_t __attribute__((constructor))
81 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
84 uid_t proc_euid = geteuid();
85 struct passwd* proc_user = getpwuid(proc_euid);
88 setlogmask(LOG_UPTO(LOG_DEBUG));
90 setlogmask(LOG_UPTO(LOG_NOTICE));
93 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
94 syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
95 mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
98 // Initialise python threads, this allows use to grab the GIL when we are
101 PyEval_InitThreads();
104 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
105 memset(advance_func, 0, sizeof(mraa_adv_func_t));
107 mraa_platform_t platform_type;
109 // Use runtime x86 platform detection
110 platform_type = mraa_x86_platform();
111 #elif defined(ARMPLAT)
112 // Use runtime ARM platform detection
113 platform_type = mraa_arm_platform();
115 #error mraa_ARCH NOTHING
119 plat->platform_type = platform_type;
122 // This is a platform extender so create null base platform if one doesn't already exist
124 plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
126 plat->platform_type = MRAA_NULL_PLATFORM;
127 plat->platform_name = "Null platform";
130 // Now detect sub platform
132 mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
133 if (plat->platform_type == MRAA_UNKNOWN_PLATFORM && usb_platform_type != MRAA_UNKNOWN_PLATFORM) {
134 plat->platform_type = usb_platform_type;
136 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
140 printf("mraa: FATAL error, failed to initialise platform\n");
141 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
145 syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
153 if (plat->pins != NULL) {
156 mraa_board_t* sub_plat = plat->sub_platform;
157 if (sub_plat != NULL) {
158 if (sub_plat->pins != NULL) {
159 free(sub_plat->pins);
170 mraa_set_priority(const unsigned int priority)
172 struct sched_param sched_s;
174 memset(&sched_s, 0, sizeof(struct sched_param));
175 if (priority > sched_get_priority_max(SCHED_RR)) {
176 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
178 sched_s.sched_priority = priority;
181 return sched_setscheduler(0, SCHED_RR, &sched_s);
185 mraa_setup_mux_mapped(mraa_pin_t meta)
189 for (mi = 0; mi < meta.mux_total; mi++) {
190 mraa_gpio_context mux_i;
191 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
193 return MRAA_ERROR_INVALID_HANDLE;
195 // this function will sometimes fail, however this is not critical as
196 // long as the write succeeds - Test case galileo gen2 pin2
197 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
198 mraa_gpio_owner(mux_i, 0);
200 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
201 mraa_gpio_close(mux_i);
202 return MRAA_ERROR_INVALID_RESOURCE;
204 mraa_gpio_close(mux_i);
211 mraa_result_print(mraa_result_t result)
215 fprintf(stdout, "MRAA: SUCCESS\n");
217 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
218 fprintf(stdout, "MRAA: Feature not implemented.\n");
220 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
221 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
223 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
224 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
226 case MRAA_ERROR_INVALID_PARAMETER:
227 fprintf(stdout, "MRAA: Invalid parameter.\n");
229 case MRAA_ERROR_INVALID_HANDLE:
230 fprintf(stdout, "MRAA: Invalid Handle.\n");
232 case MRAA_ERROR_NO_RESOURCES:
233 fprintf(stdout, "MRAA: No resources.\n");
235 case MRAA_ERROR_INVALID_RESOURCE:
236 fprintf(stdout, "MRAA: Invalid resource.\n");
238 case MRAA_ERROR_INVALID_QUEUE_TYPE:
239 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
241 case MRAA_ERROR_NO_DATA_AVAILABLE:
242 fprintf(stdout, "MRAA: No Data available.\n");
244 case MRAA_ERROR_INVALID_PLATFORM:
245 fprintf(stdout, "MRAA: Platform not recognised.\n");
247 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
248 fprintf(stdout, "MRAA: Platform not initialised.\n");
250 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
251 fprintf(stdout, "MRAA: Platform already initialised.\n");
253 case MRAA_ERROR_UNSPECIFIED:
254 fprintf(stdout, "MRAA: Unspecified Error.\n");
257 fprintf(stdout, "MRAA: Unrecognised error.\n");
264 mraa_has_sub_platform()
266 return (plat != NULL) && (plat->sub_platform != NULL);
270 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
275 mraa_board_t* current_plat = plat;
276 if (mraa_is_sub_platform_id(pin)) {
277 current_plat = plat->sub_platform;
278 if (current_plat == NULL) {
279 syslog(LOG_ERR, "mraa_pin_mode_test: Sub platform Not Initialised");
282 pin = mraa_get_sub_platform_index(pin);
285 if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM) {
288 if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
293 if (current_plat->pins[pin].capabilites.valid == 1)
297 if (current_plat->pins[pin].capabilites.gpio == 1)
301 if (current_plat->pins[pin].capabilites.pwm == 1)
304 case MRAA_PIN_FAST_GPIO:
305 if (current_plat->pins[pin].capabilites.fast_gpio == 1)
309 if (current_plat->pins[pin].capabilites.spi == 1)
313 if (current_plat->pins[pin].capabilites.i2c == 1)
317 if (current_plat->pins[pin].capabilites.aio == 1)
321 if (current_plat->pins[pin].capabilites.uart == 1)
325 syslog(LOG_NOTICE, "requested pinmode invalid");
332 mraa_get_platform_type()
335 return MRAA_UNKNOWN_PLATFORM;
336 return plat->platform_type;
340 mraa_get_platform_combined_type()
342 int type = mraa_get_platform_type();
343 int sub_type = mraa_has_sub_platform() ? plat->sub_platform->platform_type : MRAA_UNKNOWN_PLATFORM;
344 return type | (sub_type << 8);
353 if (plat->aio_count == 0)
356 return plat->adc_raw;
360 mraa_get_platform_adc_raw_bits(uint8_t platform_offset)
362 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
363 return mraa_adc_raw_bits();
365 if (!mraa_has_sub_platform())
368 if (plat->sub_platform->aio_count == 0)
371 return plat->sub_platform->adc_raw;
377 mraa_adc_supported_bits()
382 if (plat->aio_count == 0)
385 return plat->adc_supported;
389 mraa_get_platform_adc_supported_bits(int platform_offset)
391 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
392 return mraa_adc_supported_bits();
394 if (!mraa_has_sub_platform())
397 if (plat->sub_platform->aio_count == 0)
400 return plat->sub_platform->adc_supported;
406 mraa_get_platform_name()
411 if (mraa_has_sub_platform()) {
412 snprintf(platform_name, MAX_PLATFORM_NAME_LENGTH, "%s + %s", plat->platform_name, plat->sub_platform->platform_name);
414 strncpy(platform_name, plat->platform_name, MAX_PLATFORM_NAME_LENGTH-1);
417 return platform_name;
421 mraa_get_i2c_bus_count()
426 return plat->i2c_bus_count;
430 mraa_get_i2c_bus_id(unsigned i2c_bus)
436 if (i2c_bus >= plat->i2c_bus_count) {
440 return plat->i2c_bus[i2c_bus].bus_id;
449 return plat->phy_pin_count;
453 mraa_get_platform_pin_count(uint8_t platform_offset)
455 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
456 return mraa_get_pin_count();
458 if (mraa_has_sub_platform())
459 return plat->sub_platform->phy_pin_count;
467 mraa_get_pin_name(int pin)
472 mraa_board_t* current_plat = plat;
473 if (mraa_is_sub_platform_id(pin)) {
474 current_plat = plat->sub_platform;
475 if (current_plat == NULL) {
476 syslog(LOG_ERR, "mraa_get_pin_name: Sub platform Not Initialised");
479 pin = mraa_get_sub_platform_index(pin);
482 if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
484 return (char*) current_plat->pins[pin].name;
488 mraa_get_default_i2c_bus(uint8_t platform_offset)
492 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET) {
493 return plat->def_i2c_bus;
495 if (mraa_has_sub_platform())
496 return plat->sub_platform->def_i2c_bus;
504 mraa_file_exist(const char* filename)
507 results.gl_pathc = 0;
508 glob(filename, 0, NULL, &results);
509 int file_found = results.gl_pathc == 1;
515 mraa_file_contains(const char* filename, const char* content)
517 mraa_boolean_t found = 0;
518 if ((filename == NULL) || (content == NULL)) {
522 char* file = mraa_file_unglob(filename);
525 char* line = malloc(len);
530 FILE* fh = fopen(file, "r");
536 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
537 if (strstr(line, content)) {
550 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
552 mraa_boolean_t found = 0;
553 if ((filename == NULL) || (content == NULL)) {
557 char* file = mraa_file_unglob(filename);
560 char* line = malloc(len);
565 FILE* fh = fopen(file, "r");
571 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
572 if (strstr(line, content) && strstr(line, content2)) {
585 mraa_file_unglob(const char* filename)
589 results.gl_pathc = 0;
590 glob(filename, 0, NULL, &results);
591 if (results.gl_pathc == 1)
592 res = strdup(results.gl_pathv[0]);
598 mraa_link_targets(const char* filename, const char* targetname)
603 while (nchars == 0) {
604 buffer = (char*) realloc(buffer, size);
607 nchars = readlink(filename, buffer, size);
612 buffer[nchars] = '\0';
614 if (nchars >= size) {
619 if (strstr(buffer, targetname)) {
628 static int num_i2c_devices = 0;
631 mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
633 switch (sb->st_mode & S_IFMT) {
642 mraa_find_i2c_bus(const char* devname, int startfrom)
649 // because feeding mraa_find_i2c_bus result back into the function is
650 // useful treat -1 as 0
655 // find how many i2c buses we have if we haven't already
656 if (num_i2c_devices == 0) {
657 if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) {
662 // i2c devices are numbered numerically so 0 must exist otherwise there is
664 if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
665 for (i; i < num_i2c_devices; i++) {
667 snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
668 fd = open(path, O_RDONLY);
672 size = lseek(fd, 0, SEEK_END);
674 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
678 err = lseek(fd, 0, SEEK_SET);
680 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
684 char* value = malloc(size);
686 syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
690 ssize_t r = read(fd, value, size);
692 if (strcasestr(value, devname) != NULL) {
698 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
704 syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");
711 mraa_is_sub_platform_id(int pin_or_bus)
713 return (pin_or_bus & MRAA_SUB_PLATFORM_MASK) != 0;
717 mraa_get_sub_platform_id(int pin_or_bus)
719 return pin_or_bus | MRAA_SUB_PLATFORM_MASK;
723 mraa_get_sub_platform_index(int pin_or_bus)
725 return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK);