Compile libmraa for Android and Brillo
Add makefile for building libmraa library. Import glob functionality from the NetBSD project where the glob.c and glob.h files are released under BSD 3-clause License. Minor changes were applied in order to make them compile for Android and Brillo. This patch adds a control pipe used to interrupt the poll() function as an alternative to pthread cancellation API. Change-Id: Id719da4c839acbd320b0cc0e0113c2a5239c8029 Signed-off-by: Mihai Serban <mihai.serban@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
2dd94cda3b
commit
ccafc77641
57
Android.mk
Normal file
57
Android.mk
Normal file
@@ -0,0 +1,57 @@
|
||||
# Copyright (C) 2015 Intel Corporation
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_MODULE := libmraa
|
||||
LOCAL_CFLAGS += -Wno-unused-parameter -DX86PLAT=1
|
||||
LOCAL_SHARED_LIBRARIES := libcutils libutils libdl libc
|
||||
LOCAL_SRC_FILES := \
|
||||
src/mraa.c \
|
||||
src/gpio/gpio.c \
|
||||
src/i2c/i2c.c \
|
||||
src/pwm/pwm.c \
|
||||
src/spi/spi.c \
|
||||
src/aio/aio.c \
|
||||
src/uart/uart.c \
|
||||
src/x86/x86.c \
|
||||
src/x86/intel_galileo_rev_d.c \
|
||||
src/x86/intel_galileo_rev_g.c \
|
||||
src/x86/intel_edison_fab_c.c \
|
||||
src/x86/intel_de3815.c \
|
||||
src/x86/intel_nuc5.c \
|
||||
src/x86/intel_minnow_byt_compatible.c
|
||||
|
||||
# glob.c pulled in from NetBSD project (BSD 3-clause License)
|
||||
LOCAL_SRC_FILES += \
|
||||
src/glob/glob.c
|
||||
|
||||
LOCAL_C_INCLUDES := \
|
||||
$(LOCAL_PATH)/include \
|
||||
$(LOCAL_PATH)/include/linux \
|
||||
$(LOCAL_PATH)/include/x86 \
|
||||
$(LOCAL_PATH)/api \
|
||||
$(LOCAL_PATH)/api/mraa \
|
||||
$(LOCAL_PATH)/src/glob
|
||||
|
||||
LOCAL_EXPORT_C_INCLUDE_DIRS := \
|
||||
$(LOCAL_PATH)/api \
|
||||
$(LOCAL_PATH)/api/mraa
|
||||
|
||||
LOCAL_MODULE_TAGS := optional
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
@@ -30,6 +30,11 @@
|
||||
#include "mraa_func.h"
|
||||
#include "mraa_adv_func.h"
|
||||
|
||||
// Bionic does not implement pthread cancellation API
|
||||
#ifndef __BIONIC__
|
||||
#define HAVE_PTHREAD_CANCEL
|
||||
#endif
|
||||
|
||||
// general status failures for internal functions
|
||||
#define MRAA_PLATFORM_NO_INIT -3
|
||||
#define MRAA_IO_SETUP_FAILURE -2
|
||||
@@ -47,6 +52,9 @@ struct _gpio {
|
||||
void *isr_args; /**< args return when interupt service request triggered */
|
||||
pthread_t thread_id; /**< the isr handler thread id */
|
||||
int isr_value_fp; /**< the isr file pointer on the value */
|
||||
#ifndef HAVE_PTHREAD_CANCEL
|
||||
int isr_control_pipe[2]; /**< a pipe used to interrupt the isr from polling the value fd*/
|
||||
#endif
|
||||
mraa_boolean_t isr_thread_terminating; /**< is the isr thread being terminated? */
|
||||
mraa_boolean_t owner; /**< If this context originally exported the pin */
|
||||
mraa_result_t (*mmap_write) (mraa_gpio_context dev, int value);
|
||||
|
||||
1149
src/glob/glob.c
Normal file
1149
src/glob/glob.c
Normal file
File diff suppressed because it is too large
Load Diff
113
src/glob/glob.h
Normal file
113
src/glob/glob.h
Normal file
@@ -0,0 +1,113 @@
|
||||
/* $NetBSD: glob.h,v 1.26 2010/09/06 14:38:56 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Guido van Rossum.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)glob.h 8.1 (Berkeley) 6/2/93
|
||||
*/
|
||||
|
||||
#ifndef _GLOB_H_
|
||||
#define _GLOB_H_
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
//#include <sys/featuretest.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifndef __gl_size_t
|
||||
#define __gl_size_t size_t
|
||||
#endif
|
||||
#ifndef __gl_stat_t
|
||||
#define __gl_stat_t struct stat
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
__gl_size_t gl_pathc; /* Count of total paths so far. */
|
||||
__gl_size_t gl_matchc; /* Count of paths matching pattern. */
|
||||
__gl_size_t gl_offs; /* Reserved at beginning of gl_pathv. */
|
||||
int gl_flags; /* Copy of flags parameter to glob. */
|
||||
char **gl_pathv; /* List of paths matching pattern. */
|
||||
/* Copy of errfunc parameter to glob. */
|
||||
int (*gl_errfunc)(const char *, int);
|
||||
|
||||
/*
|
||||
* Alternate filesystem access methods for glob; replacement
|
||||
* versions of closedir(3), readdir(3), opendir(3), stat(2)
|
||||
* and lstat(2).
|
||||
*/
|
||||
void (*gl_closedir)(void *);
|
||||
struct dirent *(*gl_readdir)(void *);
|
||||
void *(*gl_opendir)(const char *);
|
||||
int (*gl_lstat)(const char *, __gl_stat_t *);
|
||||
int (*gl_stat)(const char *, __gl_stat_t *);
|
||||
} glob_t;
|
||||
|
||||
#define GLOB_APPEND 0x0001 /* Append to output from previous call. */
|
||||
#define GLOB_DOOFFS 0x0002 /* Use gl_offs. */
|
||||
#define GLOB_ERR 0x0004 /* Return on error. */
|
||||
#define GLOB_MARK 0x0008 /* Append / to matching directories. */
|
||||
#define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */
|
||||
#define GLOB_NOSORT 0x0020 /* Don't sort. */
|
||||
#define GLOB_NOESCAPE 0x1000 /* Disable backslash escaping. */
|
||||
|
||||
#define GLOB_NOSPACE (-1) /* Malloc call failed. */
|
||||
#define GLOB_ABORTED (-2) /* Unignored error. */
|
||||
#define GLOB_NOMATCH (-3) /* No match, and GLOB_NOCHECK was not set. */
|
||||
#define GLOB_NOSYS (-4) /* Implementation does not support function. */
|
||||
|
||||
#if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H) || defined(__BIONIC__)
|
||||
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
|
||||
#define GLOB_BRACE 0x0080 /* Expand braces ala csh. */
|
||||
#define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */
|
||||
#define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */
|
||||
#define GLOB_LIMIT 0x0400 /* Limit memory used by matches to ARG_MAX */
|
||||
#define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */
|
||||
/* GLOB_NOESCAPE 0x1000 above */
|
||||
#define GLOB_PERIOD 0x2000 /* Allow metachars to match leading periods. */
|
||||
#define GLOB_NO_DOTDIRS 0x4000 /* Make . and .. vanish from wildcards. */
|
||||
#define GLOB_STAR 0x8000 /* Use glob ** to recurse directories */
|
||||
#define GLOB_QUOTE 0 /* source compatibility */
|
||||
|
||||
#define GLOB_ABEND GLOB_ABORTED /* source compatibility */
|
||||
#endif
|
||||
|
||||
__BEGIN_DECLS
|
||||
#ifndef __LIBC12_SOURCE__
|
||||
int glob(const char * __restrict, int,
|
||||
int (*)(const char *, int), glob_t * __restrict) __RENAME(__glob30);
|
||||
void globfree(glob_t *) __RENAME(__globfree30);
|
||||
#endif
|
||||
#ifdef _NETBSD_SOURCE
|
||||
int glob_pattern_p(const char *, int);
|
||||
#endif
|
||||
__END_DECLS
|
||||
|
||||
#endif /* !_GLOB_H_ */
|
||||
@@ -87,6 +87,9 @@ mraa_gpio_init_internal(mraa_adv_func_t* func_table, int pin)
|
||||
|
||||
dev->value_fp = -1;
|
||||
dev->isr_value_fp = -1;
|
||||
#ifndef HAVE_PTHREAD_CANCEL
|
||||
dev->isr_control_pipe[0] = dev->isr_control_pipe[1] = -1;
|
||||
#endif
|
||||
dev->isr_thread_terminating = 0;
|
||||
dev->phy_pin = -1;
|
||||
|
||||
@@ -182,26 +185,47 @@ mraa_gpio_init_raw(int pin)
|
||||
|
||||
|
||||
static mraa_result_t
|
||||
mraa_gpio_wait_interrupt(int fd)
|
||||
mraa_gpio_wait_interrupt(int fd
|
||||
#ifndef HAVE_PTHREAD_CANCEL
|
||||
, int control_fd
|
||||
#endif
|
||||
)
|
||||
{
|
||||
unsigned char c;
|
||||
struct pollfd pfd;
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
struct pollfd pfd[1];
|
||||
#else
|
||||
struct pollfd pfd[2];
|
||||
|
||||
if (control_fd < 0) {
|
||||
return MRAA_ERROR_INVALID_RESOURCE;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (fd < 0) {
|
||||
return MRAA_ERROR_INVALID_RESOURCE;
|
||||
}
|
||||
|
||||
// setup poll on POLLPRI
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLPRI;
|
||||
pfd[0].fd = fd;
|
||||
pfd[0].events = POLLPRI;
|
||||
|
||||
// do an initial read to clear interrupt
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
read(fd, &c, 1);
|
||||
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
// Wait for it forever or until pthread_cancel
|
||||
// poll is a cancelable point like sleep()
|
||||
int x = poll(&pfd, 1, -1);
|
||||
int x = poll(pfd, 1, -1);
|
||||
#else
|
||||
// setup poll on the controling fd
|
||||
pfd[1].fd = control_fd;
|
||||
pfd[1].events = 0; // POLLHUP, POLLERR, and POLLNVAL
|
||||
|
||||
// Wait for it forever or until control fd is closed
|
||||
int x = poll(pfd, 2, -1);
|
||||
#endif
|
||||
|
||||
// do a final read to clear interrupt
|
||||
read(fd, &c, 1);
|
||||
@@ -226,12 +250,27 @@ mraa_gpio_interrupt_handler(void* arg)
|
||||
syslog(LOG_ERR, "gpio: failed to open gpio%d/value", dev->pin);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifndef HAVE_PTHREAD_CANCEL
|
||||
if (pipe(dev->isr_control_pipe)) {
|
||||
syslog(LOG_ERR, "gpio: failed to create isr control pipe");
|
||||
close(fp);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
dev->isr_value_fp = fp;
|
||||
|
||||
for (;;) {
|
||||
ret = mraa_gpio_wait_interrupt(dev->isr_value_fp);
|
||||
ret = mraa_gpio_wait_interrupt(dev->isr_value_fp
|
||||
#ifndef HAVE_PTHREAD_CANCEL
|
||||
, dev->isr_control_pipe[0]
|
||||
#endif
|
||||
);
|
||||
if (ret == MRAA_SUCCESS && !dev->isr_thread_terminating) {
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
#endif
|
||||
#ifdef SWIGPYTHON
|
||||
// In order to call a python object (all python functions are objects) we
|
||||
// need to aquire the GIL (Global Interpreter Lock). This may not always be
|
||||
@@ -297,10 +336,14 @@ mraa_gpio_interrupt_handler(void* arg)
|
||||
#else
|
||||
dev->isr(dev->isr_args);
|
||||
#endif
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||
#endif
|
||||
} else {
|
||||
// we must have got an error code or exit request so die nicely
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
#endif
|
||||
close(dev->isr_value_fp);
|
||||
dev->isr_value_fp = -1;
|
||||
return NULL;
|
||||
@@ -392,9 +435,18 @@ mraa_gpio_isr_exit(mraa_gpio_context dev)
|
||||
ret = mraa_gpio_edge_mode(dev, MRAA_GPIO_EDGE_NONE);
|
||||
|
||||
if ((dev->thread_id != 0)) {
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
if ((pthread_cancel(dev->thread_id) != 0) || (pthread_join(dev->thread_id, NULL) != 0)) {
|
||||
ret = MRAA_ERROR_INVALID_HANDLE;
|
||||
}
|
||||
#else
|
||||
close(dev->isr_control_pipe[1]);
|
||||
if (pthread_join(dev->thread_id, NULL) != 0)
|
||||
ret = MRAA_ERROR_INVALID_HANDLE;
|
||||
|
||||
close(dev->isr_control_pipe[0]);
|
||||
dev->isr_control_pipe[0] = dev->isr_control_pipe[1] = -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
// close the filehandle in case it's still open
|
||||
|
||||
Reference in New Issue
Block a user