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;
55 static int num_i2c_devices = 0;
56 static int num_iio_devices = 0;
65 mraa_set_log_level(int level)
67 if (level <= 7 && level >= 0) {
68 setlogmask(LOG_UPTO(level));
69 syslog(LOG_DEBUG, "Loglevel %d is set", level);
72 syslog(LOG_NOTICE, "Invalid loglevel %d requested", level);
73 return MRAA_ERROR_INVALID_PARAMETER;
77 mraa_count_iio_devices(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
79 switch (sb->st_mode & S_IFMT) {
87 #if (defined SWIGPYTHON) || (defined SWIG)
90 mraa_result_t __attribute__((constructor))
95 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
98 uid_t proc_euid = geteuid();
99 struct passwd* proc_user = getpwuid(proc_euid);
102 setlogmask(LOG_UPTO(LOG_DEBUG));
104 setlogmask(LOG_UPTO(LOG_NOTICE));
107 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
108 syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
109 mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
112 // Initialise python threads, this allows use to grab the GIL when we are
115 PyEval_InitThreads();
118 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
119 memset(advance_func, 0, sizeof(mraa_adv_func_t));
121 mraa_platform_t platform_type;
123 // Use runtime x86 platform detection
124 platform_type = mraa_x86_platform();
125 #elif defined(ARMPLAT)
126 // Use runtime ARM platform detection
127 platform_type = mraa_arm_platform();
129 #error mraa_ARCH NOTHING
133 plat->platform_type = platform_type;
135 // This is a platform extender so create null base platform if one doesn't already exist
137 plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
139 plat->platform_type = MRAA_NULL_PLATFORM;
140 plat->platform_name = "Null platform";
144 // Now detect sub platform
146 mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
147 if (plat->platform_type == MRAA_UNKNOWN_PLATFORM && usb_platform_type != MRAA_UNKNOWN_PLATFORM) {
148 plat->platform_type = usb_platform_type;
150 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
154 printf("mraa: FATAL error, failed to initialise platform\n");
155 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
159 // Now detect IIO devices, linux only
160 // find how many i2c buses we have if we haven't already
161 if (num_iio_devices == 0) {
162 if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) {
163 return MRAA_ERROR_UNSPECIFIED;
166 char name[64], filepath[64];
168 plat->iio_device_count = num_iio_devices;
169 plat->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
171 for (i=0; i < num_iio_devices; i++) {
172 device = &plat->iio_devices[i];
174 snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
175 fd = open(filepath, O_RDONLY);
177 len = read(fd, &name, 64);
180 device->name = malloc((sizeof(char) * len) + sizeof(char));
181 strncpy(device->name, name, len);
187 syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
195 if (plat->pins != NULL) {
198 mraa_board_t* sub_plat = plat->sub_platform;
199 if (sub_plat != NULL) {
200 if (sub_plat->pins != NULL) {
201 free(sub_plat->pins);
212 mraa_set_priority(const unsigned int priority)
214 struct sched_param sched_s;
216 memset(&sched_s, 0, sizeof(struct sched_param));
217 if (priority > sched_get_priority_max(SCHED_RR)) {
218 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
220 sched_s.sched_priority = priority;
223 return sched_setscheduler(0, SCHED_RR, &sched_s);
227 mraa_setup_mux_mapped(mraa_pin_t meta)
231 for (mi = 0; mi < meta.mux_total; mi++) {
232 mraa_gpio_context mux_i;
233 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
235 return MRAA_ERROR_INVALID_HANDLE;
237 // this function will sometimes fail, however this is not critical as
238 // long as the write succeeds - Test case galileo gen2 pin2
239 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
240 mraa_gpio_owner(mux_i, 0);
242 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
243 mraa_gpio_close(mux_i);
244 return MRAA_ERROR_INVALID_RESOURCE;
246 mraa_gpio_close(mux_i);
253 mraa_result_print(mraa_result_t result)
257 fprintf(stdout, "MRAA: SUCCESS\n");
259 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
260 fprintf(stdout, "MRAA: Feature not implemented.\n");
262 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
263 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
265 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
266 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
268 case MRAA_ERROR_INVALID_PARAMETER:
269 fprintf(stdout, "MRAA: Invalid parameter.\n");
271 case MRAA_ERROR_INVALID_HANDLE:
272 fprintf(stdout, "MRAA: Invalid Handle.\n");
274 case MRAA_ERROR_NO_RESOURCES:
275 fprintf(stdout, "MRAA: No resources.\n");
277 case MRAA_ERROR_INVALID_RESOURCE:
278 fprintf(stdout, "MRAA: Invalid resource.\n");
280 case MRAA_ERROR_INVALID_QUEUE_TYPE:
281 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
283 case MRAA_ERROR_NO_DATA_AVAILABLE:
284 fprintf(stdout, "MRAA: No Data available.\n");
286 case MRAA_ERROR_INVALID_PLATFORM:
287 fprintf(stdout, "MRAA: Platform not recognised.\n");
289 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
290 fprintf(stdout, "MRAA: Platform not initialised.\n");
292 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
293 fprintf(stdout, "MRAA: Platform already initialised.\n");
295 case MRAA_ERROR_UNSPECIFIED:
296 fprintf(stdout, "MRAA: Unspecified Error.\n");
299 fprintf(stdout, "MRAA: Unrecognised error.\n");
306 mraa_has_sub_platform()
308 return (plat != NULL) && (plat->sub_platform != NULL);
312 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
317 mraa_board_t* current_plat = plat;
318 if (mraa_is_sub_platform_id(pin)) {
319 current_plat = plat->sub_platform;
320 if (current_plat == NULL) {
321 syslog(LOG_ERR, "mraa_pin_mode_test: Sub platform Not Initialised");
324 pin = mraa_get_sub_platform_index(pin);
327 if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM) {
330 if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
335 if (current_plat->pins[pin].capabilites.valid == 1)
339 if (current_plat->pins[pin].capabilites.gpio == 1)
343 if (current_plat->pins[pin].capabilites.pwm == 1)
346 case MRAA_PIN_FAST_GPIO:
347 if (current_plat->pins[pin].capabilites.fast_gpio == 1)
351 if (current_plat->pins[pin].capabilites.spi == 1)
355 if (current_plat->pins[pin].capabilites.i2c == 1)
359 if (current_plat->pins[pin].capabilites.aio == 1)
363 if (current_plat->pins[pin].capabilites.uart == 1)
367 syslog(LOG_NOTICE, "requested pinmode invalid");
374 mraa_get_platform_type()
377 return MRAA_UNKNOWN_PLATFORM;
378 return plat->platform_type;
382 mraa_get_platform_combined_type()
384 int type = mraa_get_platform_type();
385 int sub_type = mraa_has_sub_platform() ? plat->sub_platform->platform_type : MRAA_UNKNOWN_PLATFORM;
386 return type | (sub_type << 8);
395 if (plat->aio_count == 0)
398 return plat->adc_raw;
402 mraa_get_platform_adc_raw_bits(uint8_t platform_offset)
404 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
405 return mraa_adc_raw_bits();
407 if (!mraa_has_sub_platform())
410 if (plat->sub_platform->aio_count == 0)
413 return plat->sub_platform->adc_raw;
419 mraa_adc_supported_bits()
424 if (plat->aio_count == 0)
427 return plat->adc_supported;
431 mraa_get_platform_adc_supported_bits(int platform_offset)
433 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
434 return mraa_adc_supported_bits();
436 if (!mraa_has_sub_platform())
439 if (plat->sub_platform->aio_count == 0)
442 return plat->sub_platform->adc_supported;
448 mraa_get_platform_name()
453 if (mraa_has_sub_platform()) {
454 snprintf(platform_name, MAX_PLATFORM_NAME_LENGTH, "%s + %s", plat->platform_name, plat->sub_platform->platform_name);
456 strncpy(platform_name, plat->platform_name, MAX_PLATFORM_NAME_LENGTH-1);
459 return platform_name;
463 mraa_get_i2c_bus_count()
468 return plat->i2c_bus_count;
472 mraa_get_i2c_bus_id(unsigned i2c_bus)
478 if (i2c_bus >= plat->i2c_bus_count) {
482 return plat->i2c_bus[i2c_bus].bus_id;
491 return plat->phy_pin_count;
495 mraa_get_platform_pin_count(uint8_t platform_offset)
497 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
498 return mraa_get_pin_count();
500 if (mraa_has_sub_platform())
501 return plat->sub_platform->phy_pin_count;
509 mraa_get_pin_name(int pin)
514 mraa_board_t* current_plat = plat;
515 if (mraa_is_sub_platform_id(pin)) {
516 current_plat = plat->sub_platform;
517 if (current_plat == NULL) {
518 syslog(LOG_ERR, "mraa_get_pin_name: Sub platform Not Initialised");
521 pin = mraa_get_sub_platform_index(pin);
524 if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
526 return (char*) current_plat->pins[pin].name;
530 mraa_get_default_i2c_bus(uint8_t platform_offset)
534 if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET) {
535 return plat->def_i2c_bus;
537 if (mraa_has_sub_platform())
538 return plat->sub_platform->def_i2c_bus;
546 mraa_file_exist(const char* filename)
549 results.gl_pathc = 0;
550 glob(filename, 0, NULL, &results);
551 int file_found = results.gl_pathc == 1;
557 mraa_file_contains(const char* filename, const char* content)
559 mraa_boolean_t found = 0;
560 if ((filename == NULL) || (content == NULL)) {
564 char* file = mraa_file_unglob(filename);
567 char* line = malloc(len);
572 FILE* fh = fopen(file, "r");
578 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
579 if (strstr(line, content)) {
592 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
594 mraa_boolean_t found = 0;
595 if ((filename == NULL) || (content == NULL)) {
599 char* file = mraa_file_unglob(filename);
602 char* line = malloc(len);
607 FILE* fh = fopen(file, "r");
613 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
614 if (strstr(line, content) && strstr(line, content2)) {
627 mraa_file_unglob(const char* filename)
631 results.gl_pathc = 0;
632 glob(filename, 0, NULL, &results);
633 if (results.gl_pathc == 1)
634 res = strdup(results.gl_pathv[0]);
640 mraa_link_targets(const char* filename, const char* targetname)
645 while (nchars == 0) {
646 buffer = (char*) realloc(buffer, size);
649 nchars = readlink(filename, buffer, size);
654 buffer[nchars] = '\0';
656 if (nchars >= size) {
661 if (strstr(buffer, targetname)) {
671 mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
673 switch (sb->st_mode & S_IFMT) {
682 mraa_find_i2c_bus(const char* devname, int startfrom)
689 // because feeding mraa_find_i2c_bus result back into the function is
690 // useful treat -1 as 0
695 // find how many i2c buses we have if we haven't already
696 if (num_i2c_devices == 0) {
697 if (nftw("/sys/class/i2c-dev/", &mraa_count_i2c_files, 20, FTW_PHYS) == -1) {
702 // i2c devices are numbered numerically so 0 must exist otherwise there is
704 if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
705 for (i; i < num_i2c_devices; i++) {
707 snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
708 fd = open(path, O_RDONLY);
712 size = lseek(fd, 0, SEEK_END);
714 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
718 err = lseek(fd, 0, SEEK_SET);
720 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
724 char* value = malloc(size);
726 syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
730 ssize_t r = read(fd, value, size);
732 if (strcasestr(value, devname) != NULL) {
738 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
744 syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");
751 mraa_is_sub_platform_id(int pin_or_bus)
753 return (pin_or_bus & MRAA_SUB_PLATFORM_MASK) != 0;
757 mraa_get_sub_platform_id(int pin_or_bus)
759 return pin_or_bus | MRAA_SUB_PLATFORM_MASK;
763 mraa_get_sub_platform_index(int pin_or_bus)
765 return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK);
769 mraa_get_iio_device_count()
771 return plat->iio_device_count;
775 mraa_find_iio_device(const char* devicename)
778 for (i; i < plat->iio_device_count; i++) {
780 // compare with devices array