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.
34 #include "mraa_internal.h"
38 mraa_board_t* plat = NULL;
39 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
40 mraa_adv_func_t* advance_func;
49 mraa_set_log_level(int level)
51 if (level <= 7 && level >= 0) {
52 setlogmask(LOG_UPTO(level));
55 return MRAA_ERROR_INVALID_PARAMETER;
58 #if (defined SWIGPYTHON) || (defined SWIG)
61 mraa_result_t __attribute__((constructor))
66 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
69 uid_t proc_euid = geteuid();
70 struct passwd* proc_user = getpwuid(proc_euid);
73 setlogmask(LOG_UPTO(LOG_DEBUG));
75 setlogmask(LOG_UPTO(LOG_NOTICE));
78 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
79 syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
80 mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
83 // Initialise python threads, this allows use to grab the GIL when we are
88 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
89 memset(advance_func, 0, sizeof(mraa_adv_func_t));
92 // Use runtime x86 platform detection
93 platform_type = mraa_x86_platform();
95 // Use runtime ARM platform detection
96 platform_type = mraa_arm_platform();
98 #error mraa_ARCH NOTHING
102 printf("mraa: FATAL error, failed to initialise platform\n");
103 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
106 syslog(LOG_INFO, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), platform_type);
114 if (plat->pins != NULL) {
123 mraa_set_priority(const unsigned int priority)
125 struct sched_param sched_s;
127 memset(&sched_s, 0, sizeof(struct sched_param));
128 if (priority > sched_get_priority_max(SCHED_RR)) {
129 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
131 sched_s.sched_priority = priority;
134 return sched_setscheduler(0, SCHED_RR, &sched_s);
138 mraa_setup_mux_mapped(mraa_pin_t meta)
142 for (mi = 0; mi < meta.mux_total; mi++) {
143 mraa_gpio_context mux_i;
144 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
146 return MRAA_ERROR_INVALID_HANDLE;
148 // this function will sometimes fail, however this is not critical as
149 // long as the write succeeds - Test case galileo gen2 pin2
150 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
151 mraa_gpio_owner(mux_i, 0);
153 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
154 mraa_gpio_close(mux_i);
155 return MRAA_ERROR_INVALID_RESOURCE;
157 mraa_gpio_close(mux_i);
164 mraa_result_print(mraa_result_t result)
168 fprintf(stdout, "MRAA: SUCCESS\n");
170 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
171 fprintf(stdout, "MRAA: Feature not implemented.\n");
173 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
174 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
176 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
177 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
179 case MRAA_ERROR_INVALID_PARAMETER:
180 fprintf(stdout, "MRAA: Invalid parameter.\n");
182 case MRAA_ERROR_INVALID_HANDLE:
183 fprintf(stdout, "MRAA: Invalid Handle.\n");
185 case MRAA_ERROR_NO_RESOURCES:
186 fprintf(stdout, "MRAA: No resources.\n");
188 case MRAA_ERROR_INVALID_RESOURCE:
189 fprintf(stdout, "MRAA: Invalid resource.\n");
191 case MRAA_ERROR_INVALID_QUEUE_TYPE:
192 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
194 case MRAA_ERROR_NO_DATA_AVAILABLE:
195 fprintf(stdout, "MRAA: No Data available.\n");
197 case MRAA_ERROR_INVALID_PLATFORM:
198 fprintf(stdout, "MRAA: Platform not recognised.\n");
200 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
201 fprintf(stdout, "MRAA: Platform not initialised.\n");
203 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
204 fprintf(stdout, "MRAA: Platform already initialised.\n");
206 case MRAA_ERROR_UNSPECIFIED:
207 fprintf(stdout, "MRAA: Unspecified Error.\n");
210 fprintf(stdout, "MRAA: Unrecognised error.\n");
216 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
221 if (pin > (plat->phy_pin_count - 1) || pin < 0)
226 if (plat->pins[pin].capabilites.valid == 1)
230 if (plat->pins[pin].capabilites.gpio == 1)
234 if (plat->pins[pin].capabilites.pwm == 1)
237 case MRAA_PIN_FAST_GPIO:
238 if (plat->pins[pin].capabilites.fast_gpio == 1)
242 if (plat->pins[pin].capabilites.spi == 1)
246 if (plat->pins[pin].capabilites.i2c == 1)
250 if (plat->pins[pin].capabilites.aio == 1)
254 if (plat->pins[pin].capabilites.uart == 1)
258 syslog(LOG_NOTICE, "requested pinmode invalid");
265 mraa_get_platform_type()
267 return platform_type;
276 if (plat->aio_count == 0)
279 return plat->adc_raw;
283 mraa_adc_supported_bits()
288 if (plat->aio_count == 0)
291 return plat->adc_supported;
295 mraa_get_platform_name()
300 return (char*) plat->platform_name;
309 return plat->phy_pin_count;
313 mraa_get_pin_name(int pin)
318 if (pin > (plat->phy_pin_count - 1) || pin < 0)
320 return (char*) plat->pins[pin].name;
324 mraa_file_exist(const char* filename)
327 results.gl_pathc = 0;
328 glob(filename, 0, NULL, &results);
329 int file_found = results.gl_pathc == 1;
335 mraa_file_contains(const char* filename, const char* content)
337 mraa_boolean_t found = 0;
338 if ((filename == NULL) || (content == NULL)) {
342 char* file = mraa_file_unglob(filename);
345 char* line = malloc(len);
346 FILE* fh = fopen(file, "r");
347 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
348 if (strstr(line, content)) {
361 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
363 mraa_boolean_t found = 0;
364 if ((filename == NULL) || (content == NULL)) {
368 char* file = mraa_file_unglob(filename);
371 char* line = malloc(len);
372 FILE* fh = fopen(file, "r");
373 while ((getline(&line, &len, fh) != -1) && (found == 0)) {
374 if (strstr(line, content) && strstr(line, content2)) {
387 mraa_file_unglob(const char* filename)
391 results.gl_pathc = 0;
392 glob(filename, 0, NULL, &results);
393 if (results.gl_pathc == 1)
394 res = strdup(results.gl_pathv[0]);
400 mraa_link_targets(const char* filename, const char* targetname)
405 while (nchars == 0) {
406 buffer = (char*) realloc(buffer, size);
409 nchars = readlink(filename, buffer, size);
414 buffer[nchars] = '\0';
416 if (nchars >= size) {
421 if (strstr(buffer, targetname)) {