imraa: add force flag and add safety checks
Signed-off-by: Longwei Su <lsu@ics.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
f1f578363a
commit
317c85ecb6
@@ -75,11 +75,16 @@ imraa_list_serialport()
|
|||||||
const char* path;
|
const char* path;
|
||||||
path = udev_list_entry_get_name(dev_list_entry);
|
path = udev_list_entry_get_name(dev_list_entry);
|
||||||
dev = udev_device_new_from_syspath(udev, path);
|
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);
|
ret = udev_device_get_devnode(dev);
|
||||||
}
|
}
|
||||||
udev_enumerate_unref(enumerate);
|
udev_enumerate_unref(enumerate);
|
||||||
udev_unref(udev);
|
udev_unref(udev);
|
||||||
|
|
||||||
|
if(ret) {
|
||||||
|
printf("Ardunio 101 Device Node Path: %s\n", ret);
|
||||||
|
} else {
|
||||||
|
printf("Can't detect any Ardunio 101 Device on tty\n");
|
||||||
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +107,6 @@ imraa_reset_arduino(const char* modem)
|
|||||||
int
|
int
|
||||||
imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty)
|
imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (imraa_reset_arduino(tty) != MRAA_SUCCESS) {
|
if (imraa_reset_arduino(tty) != MRAA_SUCCESS) {
|
||||||
fprintf(stderr, "Failed to reset arduino on %s for unknown reason, carrying on...\n", tty);
|
fprintf(stderr, "Failed to reset arduino on %s for unknown reason, carrying on...\n", tty);
|
||||||
}
|
}
|
||||||
@@ -114,6 +118,10 @@ imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty
|
|||||||
size_t bin_path_len = strlen(bin_path);
|
size_t bin_path_len = strlen(bin_path);
|
||||||
|
|
||||||
char* full_dfu_list = (char*) calloc((bin_path_len + strlen(dfu_list) + 1), sizeof(char));
|
char* full_dfu_list = (char*) calloc((bin_path_len + strlen(dfu_list) + 1), sizeof(char));
|
||||||
|
if(!full_dfu_list){
|
||||||
|
printf("imraa_flash_101 can't allocate string buffer for dfu list\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
strncat(full_dfu_list, bin_path, strlen(bin_path));
|
strncat(full_dfu_list, bin_path, strlen(bin_path));
|
||||||
strncat(full_dfu_list, dfu_list, strlen(dfu_list));
|
strncat(full_dfu_list, dfu_list, strlen(dfu_list));
|
||||||
|
|
||||||
@@ -159,6 +167,10 @@ imraa_flash_101(const char* bin_path, const char* bin_file_name, const char* tty
|
|||||||
const char* dfu_option = " -v -a 7 -R";
|
const char* dfu_option = " -v -a 7 -R";
|
||||||
int buffersize = bin_path_len + strlen(dfu_upload) + strlen(bin_file_name) + strlen(dfu_option) + 1;
|
int buffersize = bin_path_len + strlen(dfu_upload) + strlen(bin_file_name) + strlen(dfu_option) + 1;
|
||||||
char* full_dfu_upload = calloc(buffersize, sizeof(char));
|
char* full_dfu_upload = calloc(buffersize, sizeof(char));
|
||||||
|
if(!full_dfu_upload){
|
||||||
|
printf("imraa_flash_101 can't allocate string buffer for dfu flash\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
strncat(full_dfu_upload, bin_path, strlen(bin_path));
|
strncat(full_dfu_upload, bin_path, strlen(bin_path));
|
||||||
strncat(full_dfu_upload, dfu_upload, strlen(dfu_upload));
|
strncat(full_dfu_upload, dfu_upload, strlen(dfu_upload));
|
||||||
strncat(full_dfu_upload, bin_file_name, strlen(bin_file_name));
|
strncat(full_dfu_upload, bin_file_name, strlen(bin_file_name));
|
||||||
@@ -206,7 +218,7 @@ imraa_write_lockfile(const char* lock_file_location, const char* serialport)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
imraa_handle_subplatform(struct json_object* jobj)
|
imraa_handle_subplatform(struct json_object* jobj, bool force_update)
|
||||||
{
|
{
|
||||||
struct json_object* platform;
|
struct json_object* platform;
|
||||||
int i, ionum;
|
int i, ionum;
|
||||||
@@ -244,6 +256,9 @@ imraa_handle_subplatform(struct json_object* jobj)
|
|||||||
if (strcmp(key, "flash") == 0) {
|
if (strcmp(key, "flash") == 0) {
|
||||||
flash_loc = json_object_get_string(val);
|
flash_loc = json_object_get_string(val);
|
||||||
}
|
}
|
||||||
|
if (strcmp(key, "usbserial") == 0) {
|
||||||
|
usbserial = json_object_get_string(val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -251,19 +266,30 @@ imraa_handle_subplatform(struct json_object* jobj)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// got flash? do flash
|
// got flash? do flash
|
||||||
if (access(lockfile_loc, F_OK) != -1) {
|
if (access(lockfile_loc, F_OK) != -1 && force_update == false) {
|
||||||
printf("already exist a lock file, skip flashing\n");
|
printf("already exist a lock file, skip flashing\n");
|
||||||
printf("force upgrade? remove the lockfile first\n", lockfile_loc);
|
printf("force upgrade? remove the lockfile or run with force\n", lockfile_loc);
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stdout, "Starting to flash board\n");
|
fprintf(stdout, "Starting to flash board\n");
|
||||||
|
if(force_update) {
|
||||||
|
fprintf(stdout, "**Caution: force update mode**\n");
|
||||||
|
}
|
||||||
// dfu_loc = "/usr/bin";
|
// dfu_loc = "/usr/bin";
|
||||||
//TODO flash img checksum, and serialport validation?
|
//TODO flash img checksum, and serialport validation?
|
||||||
const char* serialport = (strcmp(usbserial, "auto") == 0)
|
const char* detected_serialport = imraa_list_serialport();
|
||||||
? imraa_list_serialport() : usbserial;
|
if (detected_serialport == NULL) {
|
||||||
|
printf("No subplatform detected, skip flashing\n");
|
||||||
if ( dfu_loc != NULL && flash_loc != NULL && serialport != NULL) {
|
return;
|
||||||
if (imraa_flash_101(dfu_loc, flash_loc, serialport) == 0) {
|
}
|
||||||
imraa_write_lockfile(lockfile_loc, serialport);
|
if (strcmp(usbserial, "auto") != 0 && strcmp(usbserial, detected_serialport) != 0) {
|
||||||
|
printf("given serial port didn't match detected serial port, skip flashing\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
detected_serialport = "/dev/ttyACM0";
|
||||||
|
if ( dfu_loc != NULL && flash_loc != NULL && usbserial != NULL) {
|
||||||
|
if (imraa_flash_101(dfu_loc, flash_loc, detected_serialport) == 0) {
|
||||||
|
imraa_write_lockfile(lockfile_loc, detected_serialport);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Flash failed, push master reset and try again\n");
|
fprintf(stderr, "Flash failed, push master reset and try again\n");
|
||||||
}
|
}
|
||||||
@@ -271,7 +297,7 @@ imraa_handle_subplatform(struct json_object* jobj)
|
|||||||
fprintf(stderr, "invalid flashing paramenters, please check agian\n");
|
fprintf(stderr, "invalid flashing paramenters, please check agian\n");
|
||||||
fprintf(stderr, "DFU Util location: %s\n", dfu_loc);
|
fprintf(stderr, "DFU Util location: %s\n", dfu_loc);
|
||||||
fprintf(stderr, "Flash Img location: %s\n", dfu_loc);
|
fprintf(stderr, "Flash Img location: %s\n", dfu_loc);
|
||||||
fprintf(stderr, "USB Serial: %s\n", dfu_loc);
|
fprintf(stderr, "USB Serial: %s\n", usbserial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -285,8 +311,12 @@ imraa_handle_IO(struct json_object* jobj)
|
|||||||
int i;
|
int i;
|
||||||
if (json_object_object_get_ex(jobj, "IO", &ioarray) == true) {
|
if (json_object_object_get_ex(jobj, "IO", &ioarray) == true) {
|
||||||
ionum = json_object_array_length(ioarray);
|
ionum = json_object_array_length(ioarray);
|
||||||
mraa_io_obj = (mraa_io_objects_t*) malloc( ionum * sizeof(mraa_io_objects_t));
|
|
||||||
printf("Length of IO array is %d\n", ionum);
|
printf("Length of IO array is %d\n", ionum);
|
||||||
|
mraa_io_obj = (mraa_io_objects_t*) malloc( 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;
|
int index2 = -1;;//optional index for io configuration;
|
||||||
if (json_object_is_type(ioarray, json_type_array)) {
|
if (json_object_is_type(ioarray, json_type_array)) {
|
||||||
for (i = 0; i < ionum; i++) {
|
for (i = 0; i < ionum; i++) {
|
||||||
@@ -366,8 +396,8 @@ imraa_handle_IO(struct json_object* jobj)
|
|||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "IO array incorrectly parsed\n");
|
fprintf(stderr, "IO array incorrectly parsed\n");
|
||||||
}
|
}
|
||||||
|
free(mraa_io_obj);
|
||||||
}
|
}
|
||||||
free(mraa_io_obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -398,6 +428,8 @@ void
|
|||||||
print_help()
|
print_help()
|
||||||
{
|
{
|
||||||
fprintf(stdout, "version Get mraa version and board name\n");
|
fprintf(stdout, "version Get mraa version and board name\n");
|
||||||
|
fprintf(stdout, "force Force update subplatform\n");
|
||||||
|
fprintf(stdout, "conf_path Force update with override configuration\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -414,6 +446,7 @@ main(int argc, char** argv)
|
|||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
char* imraa_conf_file = IMRAA_CONF_FILE;
|
char* imraa_conf_file = IMRAA_CONF_FILE;
|
||||||
long fsize;
|
long fsize;
|
||||||
|
bool force_update = false;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
uint32_t ionum = 0;
|
uint32_t ionum = 0;
|
||||||
|
|
||||||
@@ -424,10 +457,15 @@ main(int argc, char** argv)
|
|||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
if (strcmp(argv[1], "help") == 0) {
|
if (strcmp(argv[1], "help") == 0) {
|
||||||
print_help();
|
print_help();
|
||||||
|
return;
|
||||||
} else if (strcmp(argv[1], "version") == 0) {
|
} else if (strcmp(argv[1], "version") == 0) {
|
||||||
print_version();
|
print_version();
|
||||||
|
return;
|
||||||
|
} else if (strcmp(argv[1], "force") == 0) {
|
||||||
|
force_update = true;
|
||||||
} else {
|
} else {
|
||||||
imraa_conf_file = argv[1];
|
imraa_conf_file = argv[1];
|
||||||
|
force_update = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,16 +481,24 @@ main(int argc, char** argv)
|
|||||||
buffer = calloc(fsize, sizeof(char));
|
buffer = calloc(fsize, sizeof(char));
|
||||||
if (buffer != NULL) {
|
if (buffer != NULL) {
|
||||||
fread(buffer, sizeof(char), fsize, fh);
|
fread(buffer, sizeof(char), fsize, fh);
|
||||||
|
} else {
|
||||||
|
printf("imraa read_conf buffer can't allocated\n");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
imraa_init();
|
imraa_init();
|
||||||
|
|
||||||
json_object* jobj = json_tokener_parse(buffer);
|
json_object* jobj = json_tokener_parse(buffer);
|
||||||
if (check_version(jobj) != 0) {
|
if (check_version(jobj) != 0) {
|
||||||
printf("version of configuaration file is not compatible, please check again\n");
|
printf("version of configuration file is not compatible, please check again\n");
|
||||||
} else {
|
} else {
|
||||||
imraa_handle_subplatform(jobj);
|
mraa_platform_t type = mraa_get_platform_type();
|
||||||
imraa_handle_IO(jobj);
|
imraa_handle_subplatform(jobj, force_update);
|
||||||
|
if( type == MRAA_NULL_PLATFORM || type == MRAA_UNKNOWN_PLATFORM) {
|
||||||
|
printf("imraa: Failed to do IO pinmuxing on null/unkown platform\n");
|
||||||
|
} else {
|
||||||
|
imraa_handle_IO(jobj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fclose(fh);
|
fclose(fh);
|
||||||
json_object_put(jobj);
|
json_object_put(jobj);
|
||||||
|
|||||||
@@ -990,6 +990,11 @@ mraa_add_subplatform(mraa_platform_t subplatformtype, const char* uart_dev)
|
|||||||
#if defined(IMRAA)
|
#if defined(IMRAA)
|
||||||
uint32_t
|
uint32_t
|
||||||
mraa_add_from_lockfile(const char* imraa_lock_file) {
|
mraa_add_from_lockfile(const char* imraa_lock_file) {
|
||||||
|
mraa_platform_t type = plat->platform_type;
|
||||||
|
if( type== MRAA_NULL_PLATFORM || type == MRAA_UNKNOWN_PLATFORM) {
|
||||||
|
syslog(LOG_ERR, "imraa: Failed to add subplatform on null/unkown platform");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
long fsize;
|
long fsize;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -1023,7 +1028,7 @@ mraa_add_from_lockfile(const char* imraa_lock_file) {
|
|||||||
uartdev = json_object_get_string(val);
|
uartdev = json_object_get_string(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (id != -1 && uartdev != NULL) {
|
if (id != -1 && id != MRAA_NULL_PLATFORM && id != MRAA_UNKNOWN_PLATFORM && uartdev != NULL) {
|
||||||
if (mraa_add_subplatform(id, uartdev) == MRAA_SUCCESS) {
|
if (mraa_add_subplatform(id, uartdev) == MRAA_SUCCESS) {
|
||||||
syslog(LOG_NOTICE, "imraa: automatically added subplatform %d, %s", id, uartdev);
|
syslog(LOG_NOTICE, "imraa: automatically added subplatform %d, %s", id, uartdev);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user