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));
65 return MRAA_ERROR_INVALID_PARAMETER;
68 #if (defined SWIGPYTHON) || (defined SWIG)
71 mraa_result_t __attribute__((constructor))
76 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
79 uid_t proc_euid = geteuid();
80 struct passwd* proc_user = getpwuid(proc_euid);
83 setlogmask(LOG_UPTO(LOG_DEBUG));
85 setlogmask(LOG_UPTO(LOG_NOTICE));
88 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
89 syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
90 mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
93 // Initialise python threads, this allows use to grab the GIL when we are
98 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
99 memset(advance_func, 0, sizeof(mraa_adv_func_t));
102 // Use runtime x86 platform detection
103 platform_type = mraa_x86_platform();
105 // Use runtime ARM platform detection
106 platform_type = mraa_arm_platform();
108 #error mraa_ARCH NOTHING
112 printf("mraa: FATAL error, failed to initialise platform\n");
113 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
116 syslog(LOG_INFO, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), platform_type);
124 if (plat->pins != NULL) {
133 mraa_set_priority(const unsigned int priority)
135 struct sched_param sched_s;
137 memset(&sched_s, 0, sizeof(struct sched_param));
138 if (priority > sched_get_priority_max(SCHED_RR)) {
139 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
141 sched_s.sched_priority = priority;
144 return sched_setscheduler(0, SCHED_RR, &sched_s);
148 mraa_setup_mux_mapped(mraa_pin_t meta)
152 for (mi = 0; mi < meta.mux_total; mi++) {
153 mraa_gpio_context mux_i;
154 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
156 return MRAA_ERROR_INVALID_HANDLE;
158 // this function will sometimes fail, however this is not critical as
159 // long as the write succeeds - Test case galileo gen2 pin2
160 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
161 mraa_gpio_owner(mux_i, 0);
163 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
164 mraa_gpio_close(mux_i);
165 return MRAA_ERROR_INVALID_RESOURCE;
167 mraa_gpio_close(mux_i);
174 mraa_result_print(mraa_result_t result)
178 fprintf(stdout, "MRAA: SUCCESS\n");
180 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
181 fprintf(stdout, "MRAA: Feature not implemented.\n");
183 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
184 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
186 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
187 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
189 case MRAA_ERROR_INVALID_PARAMETER:
190 fprintf(stdout, "MRAA: Invalid parameter.\n");
192 case MRAA_ERROR_INVALID_HANDLE:
193 fprintf(stdout, "MRAA: Invalid Handle.\n");
195 case MRAA_ERROR_NO_RESOURCES:
196 fprintf(stdout, "MRAA: No resources.\n");
198 case MRAA_ERROR_INVALID_RESOURCE:
199 fprintf(stdout, "MRAA: Invalid resource.\n");
201 case MRAA_ERROR_INVALID_QUEUE_TYPE:
202 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
204 case MRAA_ERROR_NO_DATA_AVAILABLE:
205 fprintf(stdout, "MRAA: No Data available.\n");
207 case MRAA_ERROR_INVALID_PLATFORM:
208 fprintf(stdout, "MRAA: Platform not recognised.\n");
210 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
211 fprintf(stdout, "MRAA: Platform not initialised.\n");
213 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
214 fprintf(stdout, "MRAA: Platform already initialised.\n");
216 case MRAA_ERROR_UNSPECIFIED:
217 fprintf(stdout, "MRAA: Unspecified Error.\n");
220 fprintf(stdout, "MRAA: Unrecognised error.\n");
226 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
231 if (pin > (plat->phy_pin_count - 1) || pin < 0)
236 if (plat->pins[pin].capabilites.valid == 1)
240 if (plat->pins[pin].capabilites.gpio == 1)
244 if (plat->pins[pin].capabilites.pwm == 1)
247 case MRAA_PIN_FAST_GPIO:
248 if (plat->pins[pin].capabilites.fast_gpio == 1)
252 if (plat->pins[pin].capabilites.spi == 1)
256 if (plat->pins[pin].capabilites.i2c == 1)
260 if (plat->pins[pin].capabilites.aio == 1)
264 if (plat->pins[pin].capabilites.uart == 1)
268 syslog(LOG_NOTICE, "requested pinmode invalid");
275 mraa_get_platform_type()
277 return platform_type;
286 if (plat->aio_count == 0)
289 return plat->adc_raw;
293 mraa_adc_supported_bits()
298 if (plat->aio_count == 0)
301 return plat->adc_supported;
305 mraa_get_platform_name()
310 return (char*) plat->platform_name;
319 return plat->phy_pin_count;
323 mraa_get_pin_name(int pin)
328 if (pin > (plat->phy_pin_count - 1) || pin < 0)
330 return (char*) plat->pins[pin].name;
334 mraa_file_exist(const char* filename)
337 results.gl_pathc = 0;
338 glob(filename, 0, NULL, &results);
339 int file_found = results.gl_pathc == 1;
345 mraa_file_contains(const char* filename, const char* content)
347 mraa_boolean_t found = 0;
348 if ((filename == NULL) || (content == NULL)) {
352 char* file = mraa_file_unglob(filename);
355 char* line = malloc(len);
356 FILE* fh = fopen(file, "r");
357 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
358 if (strstr(line, content)) {
371 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
373 mraa_boolean_t found = 0;
374 if ((filename == NULL) || (content == NULL)) {
378 char* file = mraa_file_unglob(filename);
381 char* line = malloc(len);
382 FILE* fh = fopen(file, "r");
383 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
384 if (strstr(line, content) && strstr(line, content2)) {
397 mraa_file_unglob(const char* filename)
401 results.gl_pathc = 0;
402 glob(filename, 0, NULL, &results);
403 if (results.gl_pathc == 1)
404 res = strdup(results.gl_pathv[0]);
410 mraa_link_targets(const char* filename, const char* targetname)
415 while (nchars == 0) {
416 buffer = (char*) realloc(buffer, size);
419 nchars = readlink(filename, buffer, size);
424 buffer[nchars] = '\0';
426 if (nchars >= size) {
431 if (strstr(buffer, targetname)) {
440 static int num_i2c_devices = 0;
443 mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
445 switch (sb->st_mode & S_IFMT) {
454 mraa_find_i2c_bus(const char* devname, int startfrom)
461 // because feeding mraa_find_i2c_bus result back into the function is
462 // useful treat -1 as 0
467 // find how many i2c buses we have if we haven't already
468 if (num_i2c_devices == 0) {
469 if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) {
474 // i2c devices are numbered numerically so 0 must exist otherwise there is
476 if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
477 for (i; i < num_i2c_devices; i++) {
479 snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
480 fd = open(path, O_RDONLY);
484 size = lseek(fd, 0, SEEK_END);
486 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
490 err = lseek(fd, 0, SEEK_SET);
492 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
496 char* value = malloc(size);
498 syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
502 ssize_t r = read(fd, value, size);
504 if (strcasestr(value, devname) != NULL) {
510 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
516 syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");