Private
Public Access
2
0

imraa: add first version of imraa, the mraa initialisation daemon

Imraa allows the automatic flashing and setup of arduino101 subplatforms as
well as the initial setup of GPIO/i2c devices

Signed-off-by: Longwei Su <lsu@ics.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Longwei Su
2016-03-15 17:32:18 -04:00
committed by Brendan Le Foll
parent de42b9558f
commit a52ce5d5bb
12 changed files with 576 additions and 13 deletions

View File

@@ -64,6 +64,7 @@ option (BUILDSWIGNODE "Build swig node modules." ON)
option (BUILDSWIGJAVA "Build Java API." OFF)
option (USBPLAT "Detection USB platform." OFF)
option (FIRMATA "Add Firmata support to mraa." OFF)
option (IMRAA "Add Imraa support to mraa." OFF)
option (FTDI4222 "Build with FTDI FT4222 subplatform support." OFF)
option (IPK "Generate IPK using CPack" OFF)
option (RPM "Generate RPM using CPack" OFF)
@@ -190,6 +191,10 @@ if (ENABLEEXAMPLES)
add_subdirectory (examples)
endif ()
if (IMRAA)
add_subdirectory (imraa)
endif ()
if (BUILDTESTS)
if (${PYTHONINTERP_FOUND})
enable_testing ()

View File

@@ -275,7 +275,12 @@ int mraa_get_sub_platform_id(int pin_or_bus_index);
int mraa_get_sub_platform_index(int pin_or_bus_id);
/**
* Add mraa subplatform
*
* @param subplatform type
* @param uart device subplatform is on
*
* @return mraa_result_t indicating success
*/
mraa_result_t mraa_add_subplatform(mraa_platform_t subplatformtype, const char* uart_dev);

View File

@@ -242,8 +242,6 @@ typedef enum {
MRAA_UART_PARITY_SPACE = 4
} mraa_uart_parity_t;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,21 @@
# JSON-C_FOUND - true if library and headers were found
# JSON-C_INCLUDE_DIRS - include directories
# JSON-C_LIBRARIES - library directories
find_package (PkgConfig)
pkg_check_modules (PC_JSON-C QUIET json-c)
find_path (JSON-C_INCLUDE_DIR json.h HINTS ${PC_JSON-C_INCLUDEDIR}
${PC_JSON-C_INCLUDE_DIRS} PATH_SUFFIXES json-c json)
find_library (JSON-C_LIBRARY NAMES json-c libjson-c HINTS ${PC_JSON-C_LIBDIR}
${PC_JSON-C_LIBRARY_DIRS})
set (JSON-C_LIBRARIES ${JSON-C_LIBRARY})
set (JSON-C_INCLUDE_DIRS ${JSON-C_INCLUDE_DIR})
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (JSON-C DEFAULT_MSG JSON-C_LIBRARY JSON-C_INCLUDE_DIR)
mark_as_advanced (JSON-C_INCLUDE_DIR JSON-C_LIBRARY)

15
imraa/CMakeLists.txt Normal file
View File

@@ -0,0 +1,15 @@
add_executable (imraa imraa.c)
include_directories (${PROJECT_SOURCE_DIR}/api)
include_directories (${PROJECT_SOURCE_DIR}/include)
include_directories (${PROJECT_SOURCE_DIR}/api/mraa)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DIMRAA=1")
find_package (JSON-C REQUIRED)
include_directories (${JSON-C_INCLUDE_DIR})
target_link_libraries (imraa mraa ${JSON-C_LIBRARIES} udev)
set_property (TARGET imraa PROPERTY C_STANDARD 99)
install (TARGETS imraa DESTINATION bin)

395
imraa/imraa.c Normal file
View File

@@ -0,0 +1,395 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Author: Longwei Su <lsu@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <json-c/json.h>
#include <libudev.h>
#include <mraa/uart.h>
#include <mraa/gpio.h>
#include <mraa_internal.h>
#define IMRAA_CONF_FILE "/etc/imraa.conf"
typedef struct mraa_io_objects_t {
const char* type;
int index;
bool raw;
char* label;
} mraa_io_objects_t;
const char*
imraa_list_serialport()
{
struct udev* udev;
struct udev_enumerate* enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device* dev;
const char* ret;
udev = udev_new();
if (!udev) {
printf("Can't create udev, check libudev\n");
exit(1);
}
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "tty");
udev_enumerate_add_match_property(enumerate, "ID_VENDOR_ID", "8087");
udev_enumerate_add_match_property(enumerate, "ID_MODEL_ID", "0ab6");
//udev_enumerate_add_match_property(enumerate, "ID_SERIAL", "Intel_ARDUINO_101_AE6642SQ55000RS");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices)
{
const char* path;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
printf("Ardunio 101 Device Node Path: %s\n", udev_device_get_devnode(dev));
ret = udev_device_get_devnode(dev);
}
udev_enumerate_unref(enumerate);
udev_unref(udev);
return ret;
}
mraa_result_t
imraa_reset_arduino(const char* modem)
{
mraa_uart_context uart;
uart = mraa_uart_init_raw(modem);
mraa_uart_set_baudrate(uart, 1200);
if (uart == NULL) {
fprintf(stderr, "UART failed to setup\n");
return MRAA_ERROR_UNSPECIFIED;
}
mraa_uart_stop(uart);
mraa_deinit();
return MRAA_SUCCESS;
}
int
imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty)
{
if (imraa_reset_arduino(tty) != MRAA_SUCCESS) {
fprintf(stderr, "Failed to reset arduino on %s for unknown reason, carrying on...\n", tty);
}
char* ln = NULL;
size_t len = 0;
bool board_found = false;
const char* dfu_list = "/dfu-util -d ,8087:0ABA -l";
size_t bin_path_len = strlen(bin_path);
char* full_dfu_list = (char*) calloc((bin_path_len + strlen(dfu_list) + 1), sizeof(char));
strncat(full_dfu_list, bin_path, strlen(bin_path));
strncat(full_dfu_list, dfu_list, strlen(dfu_list));
int i;
// dfu list is still needed, as the time for reset and recognized is varies from platform to
// platform.
// one dfu able to query available device, then it is ready to flash
for (i = 0; i < 10 && board_found == false; i++) {
printf("Waiting for device...\n");
// dfu-util -d,8087:0ABA -l
FILE* dfu_result = popen(full_dfu_list, "r");
if (dfu_result == NULL) {
printf("Failed to run command\n");
exit(1);
}
if (i == 4) {
printf("Flashing is taking longer than expected\n");
printf("Try pressing MASTER_RESET button\n");
}
while (getline(&ln, &len, dfu_result) != -1) {
if (strstr(ln, "sensor_core")) {
board_found = true;
printf("Device found!\n");
break;
}
}
sleep(1);
if (pclose(dfu_result) != 0) {
printf("Failed to close command\n");
exit(1);
}
}
free(ln);
if (board_found == false) {
printf("ERROR: Device is not responding.\n");
exit(1);
}
const char* dfu_upload = "/dfu-util -d ,8087:0ABA -D ";
const char* dfu_option = " -v -a 7 -R";
int buffersize = bin_path_len + strlen(dfu_upload) + strlen(bin_file_name) + strlen(dfu_option) + 1;
char* full_dfu_upload = calloc(buffersize, sizeof(char));
strncat(full_dfu_upload, bin_path, strlen(bin_path));
strncat(full_dfu_upload, dfu_upload, strlen(dfu_upload));
strncat(full_dfu_upload, bin_file_name, strlen(bin_file_name));
strncat(full_dfu_upload, dfu_option, strlen(dfu_option));
printf("flash cmd: %s\n", full_dfu_upload);
int status = system(full_dfu_upload);
free(full_dfu_upload);
if (status != 0) {
printf("ERROR: Upload failed on %s\n", tty);
exit(1);
}
printf("SUCCESS: Sketch will execute in about 5 seconds.\n");
sleep(5);
return 0;
}
void
imraa_write_lockfile(const char* lock_file_location)
{
FILE* fh;
char str[10];
json_object* platform1 = json_object_new_object();
snprintf(str, 10, "%d", MRAA_NULL_PLATFORM);
json_object_object_add(platform1, "id", json_object_new_string(str));
json_object* platform2 = json_object_new_object();
snprintf(str, 10, "%d", MRAA_GENERIC_FIRMATA);
json_object_object_add(platform2, "id", json_object_new_string(str));
json_object_object_add(platform2, "uart", json_object_new_string("/dev/ttyACM0"));
json_object* platfroms = json_object_new_array();
json_object_array_add(platfroms, platform1);
json_object_array_add(platfroms, platform2);
json_object* lock_file = json_object_new_object();
json_object_object_add(lock_file, "Platform", platfroms);
fh = fopen(lock_file_location, "w");
if (fh != NULL) {
fputs(json_object_to_json_string_ext(lock_file, JSON_C_TO_STRING_PRETTY), fh);
fclose(fh);
}
}
void
imraa_handle_subplatform(struct json_object* jobj)
{
struct json_object* platform;
int i, ionum;
const char* dfu_loc;
const char* lockfile_loc;
const char* flash_loc;
struct json_object* dfu_location;
if (json_object_object_get_ex(jobj, "dfu-utils-location", &dfu_location) == true) {
if (json_object_is_type(dfu_location, json_type_string)) {
printf("dfu location: %s\n", json_object_get_string(dfu_location));
dfu_loc = json_object_get_string(dfu_location);
} else {
fprintf(stderr, "dfu location string incorrectly parsed\n");
}
}
struct json_object* lockfile_location;
if (json_object_object_get_ex(jobj, "lockfile-location", &lockfile_location) == true) {
if (json_object_is_type(lockfile_location, json_type_string)) {
printf("lock file location: %s\n", json_object_get_string(lockfile_location));
lockfile_loc = json_object_get_string(lockfile_location);
} else {
fprintf(stderr, "lock file string incorrectly parsed\n");
}
}
if (json_object_object_get_ex(jobj, "Platform", &platform) == true) {
if (json_object_is_type(platform, json_type_array)) {
ionum = json_object_array_length(platform);
for (i = 0; i < ionum; i++) {
struct json_object* ioobj = json_object_array_get_idx(platform, i);
json_object_object_foreach(ioobj, key, val)
{
if (strcmp(key, "flash") == 0) {
flash_loc = json_object_get_string(val);
}
}
}
} else {
fprintf(stderr, "platform string incorrectly parsed\n");
}
}
// got flash? do flash
if (access(lockfile_loc, F_OK) != -1) {
printf("already exist lock file, skip flashing\n");
} else {
fprintf(stdout, "Starting to flash board\n");
// TODO define dfu location in conf?
// dfu_loc = "/usr/bin";
int flash_result = imraa_flash_101(dfu_loc, flash_loc, imraa_list_serialport());
imraa_write_lockfile(lockfile_loc);
}
}
void
imraa_handle_IO(struct json_object* jobj)
{
struct mraa_io_objects_t* mraaobjs;
struct json_object* ioarray;
int ionum = 0;
int i;
if (json_object_object_get_ex(jobj, "IO", &ioarray) == true) {
ionum = json_object_array_length(ioarray);
mraaobjs = malloc(sizeof(ioarray) * sizeof(mraa_io_objects_t));
printf("Length of IO array is %d\n", ionum);
if (json_object_is_type(ioarray, json_type_array)) {
for (i = 0; i < ionum; i++) {
struct json_object* ioobj = json_object_array_get_idx(ioarray, i);
struct json_object* x;
if (json_object_object_get_ex(ioobj, "type", &x) == true) {
mraaobjs[i].type = json_object_get_string(x);
}
if (json_object_object_get_ex(ioobj, "index", &x) == true) {
mraaobjs[i].index = json_object_get_int(x);
}
if (json_object_object_get_ex(ioobj, "raw", &x) == true) {
mraaobjs[i].raw = json_object_get_boolean(x);
}
if (json_object_object_get_ex(ioobj, "label", &x) == true) {
mraaobjs[i].index = json_object_get_int(x);
}
json_object_object_foreach(ioobj, key, val)
{
// fprintf(stderr, "key: %s\n", key);
// fprintf(stderr, "val: %s\n", json_object_get_string(val));
if (strncmp(key, "type", 4) == 0) {
if (strncmp(json_object_get_string(val), "gpio", 4) == 0) {
// mraa_gpio_context gpio = mraa_gpio_init(13);
// mraa_result_t r = mraa_gpio_owner(gpio, 0);
// if (r != MRAA_SUCCESS) {
// mraa_result_print(r);
// }
printf("set up gpio here\n");
} else if (strncmp(json_object_get_string(val), "i2c", 3) == 0) {
printf("set up i2c here\n");
}
}
}
}
} else {
fprintf(stderr, "IO array incorrectly parsed\n");
}
}
}
int
check_version(struct json_object* jobj)
{
struct json_object* imraa_version;
if (json_object_object_get_ex(jobj, "version", &imraa_version) == true) {
if (json_object_is_type(imraa_version, json_type_string)) {
printf("imraa version is %s good\n", json_object_get_string(imraa_version));
// TODO check version?
} else {
fprintf(stderr, "version string incorrectly parsed\n");
return -1;
}
}
return 0;
}
void
print_version()
{
fprintf(stdout, "Version %s on %s\n", mraa_get_version(), mraa_get_platform_name());
}
void
print_help()
{
fprintf(stdout, "version Get mraa version and board name\n");
}
void
print_command_error()
{
fprintf(stdout, "Invalid command, options are:\n");
print_help();
exit(EXIT_FAILURE);
}
int
main(int argc, char** argv)
{
char* buffer = NULL;
char* imraa_conf_file = IMRAA_CONF_FILE;
long fsize;
int i = 0;
uint32_t ionum = 0;
if (argc > 2) {
print_command_error();
}
if (argc > 1) {
if (strcmp(argv[1], "help") == 0) {
print_help();
} else if (strcmp(argv[1], "version") == 0) {
print_version();
} else {
imraa_conf_file = argv[1];
}
}
FILE* fh = fopen(imraa_conf_file, "r");
if (fh == NULL) {
fprintf(stderr, "Failed to open configuration file\n");
return EXIT_FAILURE;
}
fseek(fh, 0, SEEK_END);
fsize = ftell(fh) + 1;
fseek(fh, 0, SEEK_SET);
buffer = calloc(fsize, sizeof(char));
if (buffer != NULL) {
fread(buffer, sizeof(char), fsize, fh);
}
imraa_init();
json_object* jobj = json_tokener_parse(buffer);
if (check_version(jobj) != 0) {
printf("version of configuaration file is not compatible, please check again\n");
} else {
imraa_handle_subplatform(jobj);
imraa_handle_IO(jobj);
}
fclose(fh);
return EXIT_SUCCESS;
}

12
imraa/imraa.conf Normal file
View File

@@ -0,0 +1,12 @@
{
"version": "0.9.6",
"lockfile-location": "/tmp/imraa.lock",
"dfu-utils-location": "/usr/bin",
"Platform" :[
{"id":"1280", "flash":"/usr/share/mraa/firmata101.ino.bin", "usbserial": "auto"}
],
"IO": [
{"type":"gpio", "index":0, "raw": false, "label": "none"},
{"type":"i2c", "index":0, "raw": false, "label": "i2c-0"}
]
}

9
imraa/imraa.service Normal file
View File

@@ -0,0 +1,9 @@
[Unit]
Description=imraa
[Service]
Type=oneshot
ExecStart=/usr/bin/imraa
[Install]
WantedBy = multi-user.target

View File

@@ -132,6 +132,24 @@ mraa_boolean_t mraa_link_targets(const char* filename, const char* targetname);
*/
int mraa_find_i2c_bus(const char* devname, int startfrom);
#if defined(IMRAA)
/**
* read Imraa subplatform lock file, caller is responsible to free return
* struct array
*
* @param imraa lockfile location
* @return the number of subplatforms added
*/
uint32_t mraa_add_from_lockfile(const char* imraa_lock_file);
/**
* Internal imraa init function
*
* @return mraa_result_t indicating success
*/
mraa_result_t imraa_init();
#endif
#ifdef __cplusplus
}
#endif

View File

@@ -111,6 +111,13 @@ if (FIRMATA)
add_subdirectory (firmata)
endif ()
if (IMRAA)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DIMRAA=1")
find_package (JSON-C REQUIRED)
include_directories (${JSON-C_INCLUDE_DIR})
set (mraa_LIBS ${mraa_LIBS} ${JSON-C_LIBRARIES})
endif ()
set (mraa_LIB_SRCS
${mraa_LIB_PLAT_SRCS_NOAUTO}
# autogenerated version file

View File

@@ -91,7 +91,6 @@ mraa_firmata_i2c_init_bus_replace(mraa_i2c_context dev)
{
int delay = 1; // this should be either 1 or 0, I don't know :)
uint8_t buff[4];
printf("i2c init\n");
buff[0] = FIRMATA_START_SYSEX;
buff[1] = FIRMATA_I2C_CONFIG;
buff[2] = delay & 0xFF, (delay >> 8) & 0xFF;

View File

@@ -40,6 +40,11 @@
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#if defined(IMRAA
#include <json-c/json.h>
#endif
#include "mraa_internal.h"
#include "firmata/firmata_mraa.h"
@@ -75,16 +80,15 @@ mraa_set_log_level(int level)
return MRAA_ERROR_INVALID_PARAMETER;
}
#if (defined SWIGPYTHON) || (defined SWIG)
/**
* Whilst the actual mraa init function is now called imraa_init, it's only
* callable externally if IMRAA is enabled
*/
mraa_result_t
#else
mraa_result_t __attribute__((constructor))
#endif
mraa_init()
imraa_init()
{
if (plat != NULL) {
return MRAA_SUCCESS;
return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
}
uid_t proc_euid = geteuid();
@@ -142,8 +146,14 @@ mraa_init()
}
#endif
#if defined(FIRMATA)
// look for USB id 8087:0aba -> genuino/arduino 101
#if defined(IMRAA)
const char* subplatform_lockfile = "/tmp/imraa.lock";
if (access(subplatform_lockfile, F_OK) != -1 ){
uint32_t plat_n = mraa_add_from_lockfile(subplatform_lockfile);
syslog(LOG_INFO, "imraa: added %d platforms", plat_n);
} else {
syslog(LOG_DEBUG, "imraa: no lockfile found");
}
#endif
// Look for IIO devices
@@ -172,6 +182,20 @@ mraa_init()
return MRAA_SUCCESS;
}
#if (defined SWIGPYTHON) || (defined SWIG)
mraa_result_t
#else
mraa_result_t __attribute__((constructor))
#endif
mraa_init()
{
if (plat != NULL) {
return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
} else {
return imraa_init();
}
}
void
mraa_deinit()
{
@@ -950,14 +974,69 @@ mraa_get_iio_device_count()
mraa_result_t
mraa_add_subplatform(mraa_platform_t subplatformtype, const char* uart_dev)
{
#ifdef FIRMATA
#if defined(FIRMATA)
if (subplatformtype == MRAA_GENERIC_FIRMATA) {
if (mraa_firmata_platform(plat, uart_dev) == MRAA_GENERIC_FIRMATA) {
syslog(LOG_NOTICE, "mraa: Added firmata subplatform");
return MRAA_SUCCESS;
}
syslog(LOG_NOTICE, "mraa: Failed to add firmata subplatform");
}
#endif
return MRAA_ERROR_INVALID_PARAMETER;
}
#if defined(IMRAA)
uint32_t
mraa_add_from_lockfile(const char* imraa_lock_file) {
char* buffer = NULL;
long fsize;
int i = 0;
uint32_t subplat_num = 0;
FILE* flock = fopen(imraa_lock_file, "r");
if (flock == NULL) {
fprintf(stderr, "Failed to open lock file\n");
return 0;
}
fseek(flock, 0, SEEK_END);
fsize = ftell(flock) + 1;
fseek(flock, 0, SEEK_SET);
buffer = (char*) calloc(fsize, sizeof(char));
if (buffer != NULL) {
fread(buffer, sizeof(char), fsize, flock);
}
json_object* jobj_lock = json_tokener_parse(buffer);
struct json_object* ioarray;
if (json_object_object_get_ex(jobj_lock, "Platform", &ioarray) == true &&
json_object_is_type(ioarray, json_type_array)) {
subplat_num = json_object_array_length(ioarray);
int id = -1;
const char* uartdev = NULL;
for (i = 0; i < subplat_num; i++) {
struct json_object *ioobj = json_object_array_get_idx(ioarray, i);
json_object_object_foreach(ioobj, key, val) {
if (strcmp(key, "id") == 0) {
id = atoi(json_object_get_string(val));
} else if (strcmp(key, "uart") == 0) {
uartdev = json_object_get_string(val);
}
}
if (id != -1 && uartdev != NULL) {
if (mraa_add_subplatform(id, uartdev) == MRAA_SUCCESS) {
syslog(LOG_NOTICE, "imraa: automatically added subplatform %d, %s", id, uartdev);
} else {
syslog(LOG_ERR, "imraa: Failed to add subplatform (%d on %s) from lockfile", id, uartdev);
}
id = -1;
uartdev = NULL;
}
}
} else {
fprintf(stderr, "lockfile string incorrectly parsed\n");
}
free(buffer);
return subplat_num;
}
#endif