diff --git a/imraa/CMakeLists.txt b/imraa/CMakeLists.txt index 6bbb9e4..de90e98 100644 --- a/imraa/CMakeLists.txt +++ b/imraa/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable (imraa imraa.c imraa_arduino.c) +add_executable (imraa imraa.c imraa_arduino.c imraa_io.c imraa_lock.c) include_directories (${PROJECT_SOURCE_DIR}/api) include_directories (${PROJECT_SOURCE_DIR}/include) diff --git a/imraa/imraa.c b/imraa/imraa.c index 7c7cc83..603cbee 100644 --- a/imraa/imraa.c +++ b/imraa/imraa.c @@ -36,152 +36,14 @@ #include -#include - -#include -#include -#include -#include - #include #include +#include +#include #define IMRAA_CONF_FILE "/etc/imraa.conf" -#define SYSFS_CLASS_GPIO "/sys/class/gpio" -#define MAX_SIZE 64 - -typedef struct mraa_io_objects_t { - const char* type; - int index; - bool raw; - const char* label; - const char* user; -} mraa_io_objects_t; - -void -imraa_handle_IO(struct json_object* jobj) -{ - struct mraa_io_objects_t* mraa_io_obj; - 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); - printf("Length of IO array is %d\n", ionum); - mraa_io_obj = (mraa_io_objects_t*) calloc(ionum, sizeof(mraa_io_objects_t)); - if (!mraa_io_obj) { - printf("imraa_handle_IO malloc failed\n"); - exit(1); - } - int index2 = -1; - // optional index for io configuration; - 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) { - mraa_io_obj[i].type = json_object_get_string(x); - } - if (json_object_object_get_ex(ioobj, "index", &x) == true) { - mraa_io_obj[i].index = json_object_get_int(x); - } - if (json_object_object_get_ex(ioobj, "raw", &x) == true) { - mraa_io_obj[i].raw = json_object_get_boolean(x); - } - if (json_object_object_get_ex(ioobj, "label", &x) == true) { - mraa_io_obj[i].label = json_object_get_string(x); - } - if (json_object_object_get_ex(ioobj, "index2", &x) == true) { - index2 = json_object_get_int(x); - } - if (json_object_object_get_ex(ioobj, "user", &x) == true) { - mraa_io_obj[i].user = json_object_get_string(x); - } - // starting io configuration - if (strcmp(mraa_io_obj[i].type, "gpio") == 0) { - mraa_gpio_context gpio = NULL; - if (mraa_io_obj[i].raw) { - gpio = mraa_gpio_init_raw(mraa_io_obj[i].index); - } else { - gpio = mraa_gpio_init(mraa_io_obj[i].index); - } - if (gpio != NULL) { - if (mraa_io_obj[i].user != NULL) { - int sysfs_gpio_pin = mraa_gpio_get_pin_raw(gpio); - if (sysfs_gpio_pin >= 0) { - char bu[MAX_SIZE]; - snprintf(bu, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/value", sysfs_gpio_pin); - struct passwd* user = getpwnam(mraa_io_obj[i].user); - if (user != NULL) { - if (chown(bu, user->pw_uid, user->pw_gid) == -1) { - fprintf(stderr, "Couldn't change ownership of file %s to %d:%d\n", bu, user->pw_uid, user->pw_gid); - } else { - fprintf(stderr, "Invalid user %s\n", mraa_io_obj[i].user); - } - } - } - mraa_result_t r = mraa_gpio_owner(gpio, 0); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - mraa_gpio_close(gpio); - } - } else if (strcmp(mraa_io_obj[i].type, "i2c") == 0) { - mraa_i2c_context i2c = NULL; - if (mraa_io_obj[i].raw) { - i2c = mraa_i2c_init_raw(mraa_io_obj[i].index); - } else { - i2c = mraa_i2c_init(mraa_io_obj[i].index); - } - if (i2c != NULL) { - mraa_i2c_stop(i2c); - } - } else if (strcmp(mraa_io_obj[i].type, "pwm") == 0) { - mraa_pwm_context pwm = NULL; - if (mraa_io_obj[i].raw) { - pwm = mraa_pwm_init_raw(index2, mraa_io_obj[i].index); - } else { - pwm = mraa_pwm_init(mraa_io_obj[i].index); - } - if (pwm != NULL) { - mraa_result_t r = mraa_pwm_owner(pwm, 0); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - mraa_pwm_close(pwm); - } - } else if (strcmp(mraa_io_obj[i].type, "spi") == 0) { - mraa_spi_context spi = NULL; - if (mraa_io_obj[i].raw) { - spi = mraa_spi_init_raw(mraa_io_obj[i].index, index2); - } else { - spi = mraa_spi_init(mraa_io_obj[i].index); - } - if (spi != NULL) { - mraa_spi_stop(spi); - } - } else if (strcmp(mraa_io_obj[i].type, "uart") == 0) { - mraa_uart_context uart = NULL; - if (mraa_io_obj[i].raw) { - uart = mraa_uart_init_raw(mraa_io_obj[i].label); - } else { - uart = mraa_uart_init(mraa_io_obj[i].index); - } - if (uart != NULL) { - mraa_uart_stop(uart); - } - } - } - } - } else { - fprintf(stderr, "IO array incorrectly parsed\n"); - } - free(mraa_io_obj); - } -} - int imraa_check_version(struct json_object* jobj) { diff --git a/imraa/imraa_arduino.c b/imraa/imraa_arduino.c index 35147e3..650272f 100644 --- a/imraa/imraa_arduino.c +++ b/imraa/imraa_arduino.c @@ -41,6 +41,7 @@ #include #include +#include const char* imraa_list_serialport() @@ -182,35 +183,6 @@ imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty return 0; } -void -imraa_write_lockfile(const char* lock_file_location, const char* serialport) -{ - 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(serialport)); - - 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); - } else { - fprintf(stderr, "can't write to lock file\n"); - } - json_object_put(lock_file); -} - void imraa_handle_subplatform(struct json_object* jobj, bool force_update) { diff --git a/imraa/imraa_io.c b/imraa/imraa_io.c new file mode 100644 index 0000000..b5b5302 --- /dev/null +++ b/imraa/imraa_io.c @@ -0,0 +1,178 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2015-2016 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 +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#define SYSFS_CLASS_GPIO "/sys/class/gpio" +#define MAX_SIZE 64 + +typedef struct mraa_io_objects_t { + const char* type; + int index; + bool raw; + const char* label; + const char* user; +} mraa_io_objects_t; + +void +imraa_handle_IO(struct json_object* jobj) +{ + struct mraa_io_objects_t* mraa_io_obj; + 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); + printf("Length of IO array is %d\n", ionum); + mraa_io_obj = (mraa_io_objects_t*) calloc(ionum, sizeof(mraa_io_objects_t)); + if (!mraa_io_obj) { + printf("imraa_handle_IO malloc failed\n"); + exit(1); + } + int index2 = -1; + // optional index for io configuration; + 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) { + mraa_io_obj[i].type = json_object_get_string(x); + } + if (json_object_object_get_ex(ioobj, "index", &x) == true) { + mraa_io_obj[i].index = json_object_get_int(x); + } + if (json_object_object_get_ex(ioobj, "raw", &x) == true) { + mraa_io_obj[i].raw = json_object_get_boolean(x); + } + if (json_object_object_get_ex(ioobj, "label", &x) == true) { + mraa_io_obj[i].label = json_object_get_string(x); + } + if (json_object_object_get_ex(ioobj, "index2", &x) == true) { + index2 = json_object_get_int(x); + } + if (json_object_object_get_ex(ioobj, "user", &x) == true) { + mraa_io_obj[i].user = json_object_get_string(x); + } + // starting io configuration + if (strcmp(mraa_io_obj[i].type, "gpio") == 0) { + mraa_gpio_context gpio = NULL; + if (mraa_io_obj[i].raw) { + gpio = mraa_gpio_init_raw(mraa_io_obj[i].index); + } else { + gpio = mraa_gpio_init(mraa_io_obj[i].index); + } + if (gpio != NULL) { + if (mraa_io_obj[i].user != NULL) { + int sysfs_gpio_pin = mraa_gpio_get_pin_raw(gpio); + if (sysfs_gpio_pin >= 0) { + char bu[MAX_SIZE]; + snprintf(bu, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/value", sysfs_gpio_pin); + struct passwd* user = getpwnam(mraa_io_obj[i].user); + if (user != NULL) { + if (chown(bu, user->pw_uid, user->pw_gid) == -1) { + fprintf(stderr, "Couldn't change ownership of file %s to %d:%d\n", bu, user->pw_uid, user->pw_gid); + } else { + fprintf(stderr, "Invalid user %s\n", mraa_io_obj[i].user); + } + } + } + mraa_result_t r = mraa_gpio_owner(gpio, 0); + if (r != MRAA_SUCCESS) { + mraa_result_print(r); + } + mraa_gpio_close(gpio); + } + } else if (strcmp(mraa_io_obj[i].type, "i2c") == 0) { + mraa_i2c_context i2c = NULL; + if (mraa_io_obj[i].raw) { + i2c = mraa_i2c_init_raw(mraa_io_obj[i].index); + } else { + i2c = mraa_i2c_init(mraa_io_obj[i].index); + } + if (i2c != NULL) { + mraa_i2c_stop(i2c); + } + } else if (strcmp(mraa_io_obj[i].type, "pwm") == 0) { + mraa_pwm_context pwm = NULL; + if (mraa_io_obj[i].raw) { + pwm = mraa_pwm_init_raw(index2, mraa_io_obj[i].index); + } else { + pwm = mraa_pwm_init(mraa_io_obj[i].index); + } + if (pwm != NULL) { + mraa_result_t r = mraa_pwm_owner(pwm, 0); + if (r != MRAA_SUCCESS) { + mraa_result_print(r); + } + mraa_pwm_close(pwm); + } + } else if (strcmp(mraa_io_obj[i].type, "spi") == 0) { + mraa_spi_context spi = NULL; + if (mraa_io_obj[i].raw) { + spi = mraa_spi_init_raw(mraa_io_obj[i].index, index2); + } else { + spi = mraa_spi_init(mraa_io_obj[i].index); + } + if (spi != NULL) { + mraa_spi_stop(spi); + } + } else if (strcmp(mraa_io_obj[i].type, "uart") == 0) { + mraa_uart_context uart = NULL; + if (mraa_io_obj[i].raw) { + uart = mraa_uart_init_raw(mraa_io_obj[i].label); + } else { + uart = mraa_uart_init(mraa_io_obj[i].index); + } + if (uart != NULL) { + mraa_uart_stop(uart); + } + } + } + } + } else { + fprintf(stderr, "IO array incorrectly parsed\n"); + } + free(mraa_io_obj); + } +} diff --git a/imraa/imraa_io.h b/imraa/imraa_io.h new file mode 100644 index 0000000..1bfea6a --- /dev/null +++ b/imraa/imraa_io.h @@ -0,0 +1,28 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2016 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. + */ + +#pragma once + +void imraa_handle_IO(struct json_object* jobj); + diff --git a/imraa/imraa_lock.c b/imraa/imraa_lock.c new file mode 100644 index 0000000..2f7ac5e --- /dev/null +++ b/imraa/imraa_lock.c @@ -0,0 +1,71 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2016 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 +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + +void +imraa_write_lockfile(const char* lock_file_location, const char* serialport) +{ + 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(serialport)); + + 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); + } else { + fprintf(stderr, "can't write to lock file\n"); + } + json_object_put(lock_file); +} diff --git a/imraa/imraa_lock.h b/imraa/imraa_lock.h new file mode 100644 index 0000000..a915f3c --- /dev/null +++ b/imraa/imraa_lock.h @@ -0,0 +1,28 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2016 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. + */ + +#pragma once + +void imraa_write_lockfile(const char* lock_file_location, const char* serialport); + diff --git a/src/mraa.c b/src/mraa.c index 326c695..fd957d2 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -1116,7 +1116,15 @@ mraa_add_from_lockfile(const char* imraa_lock_file) uartdev = NULL; } } - } else { + if (json_object_object_get_ex(jobj_lock, "IO", &ioarray) == true && + json_object_is_type(ioarray, json_type_array)) { + /* assume we have declared IO so we are preinitialised and wipe the + * advance func array + */ + memset(plat->adv_func, 0, sizeof(mraa_adv_func_t)); + } + } + else { ret = MRAA_ERROR_INVALID_RESOURCE; } json_object_put(jobj_lock);