From e3e4f97a94d382abba3adfa68bc94dec299649f4 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 13 Jun 2009 15:40:07 +0200 Subject: [PATCH] work --- gtk2-gui.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++-------- i2c.c | 28 +++----------------- i2c.h | 8 +++--- 3 files changed, 71 insertions(+), 41 deletions(-) diff --git a/gtk2-gui.c b/gtk2-gui.c index cdf1871..8d78289 100644 --- a/gtk2-gui.c +++ b/gtk2-gui.c @@ -16,8 +16,15 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include +#include #include +#include +#include +#include + #include #include "gtk2-gui.h" #include "i2c.h" @@ -181,12 +188,6 @@ static void on_button103_clicked(GtkButton *button, gpointer user_data) g_timeout_add(150, application_cb, NULL); } -/* FLASH-READ Button */ -static void on_button201_clicked(GtkButton *button, gpointer user_data) -{ - add_message("sorry, not implemented yet\n"); -} - /* progress-bar callback */ static void progress_cb(int pos, int size) { @@ -198,21 +199,74 @@ static void progress_cb(int pos, int size) gdk_window_process_updates(gtk_widget_get_parent_window(progressbar201), 1); } +static char * load_file(const char *filename, int *filesize) +{ + int fd = open(filename, O_RDONLY); + if (fd < 0) { + perror("open()"); + return NULL; + } + + struct stat statbuf; + if (fstat(fd, &statbuf) == -1) { + perror("stat()"); + close(fd); + return NULL; + } + + *filesize = statbuf.st_size; + + char *data = malloc(*filesize); + if (data == NULL) { + perror("malloc()"); + close(fd); + return NULL; + } + + read(fd, data, *filesize); + close(fd); + + return data; +} + +/* FLASH-READ Button */ +static void on_button201_clicked(GtkButton *button, gpointer user_data) +{ + add_message("sorry, not implemented yet\n"); +} + /* FLASH-WRITE Button */ static void on_button202_clicked(GtkButton *button, gpointer user_data) { char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filechooserbutton201)); - int size = i2c_write_flash(i2c_fd, filename, progress_cb); - char msg_buf[64]; - snprintf(msg_buf, sizeof(msg_buf), "FLASH: %d bytes written and verified\n", size); - add_message(msg_buf); + int filesize; + char *filedata = load_file(filename, &filesize); + if (filedata != NULL) { + int size = i2c_write_flash(i2c_fd, filedata, filesize, progress_cb); + free(filedata); + + char msg_buf[64]; + snprintf(msg_buf, sizeof(msg_buf), "FLASH: %d bytes written\n", size); + add_message(msg_buf); + } } /* FLASH-VERIFY Button */ static void on_button203_clicked(GtkButton *button, gpointer user_data) { - add_message("sorry, not implemented yet\n"); + char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filechooserbutton201)); + + int filesize; + char *filedata = load_file(filename, &filesize); + if (filedata != NULL) { + int size = i2c_verify_flash(i2c_fd, filedata, filesize, progress_cb); + free(filedata); + + char msg_buf[64]; + snprintf(msg_buf, sizeof(msg_buf), "FLASH: %d bytes verified\n", size); + add_message(msg_buf); + } } /* FLASH-CLEAR Button */ diff --git a/i2c.c b/i2c.c index 4f4e9ab..6b1dbf4 100644 --- a/i2c.c +++ b/i2c.c @@ -201,7 +201,7 @@ void i2c_cmd_setparameters(int fd, struct blmc_parameter *blmc, int persistent) write(fd, cmd, sizeof(struct blmc_parameter) + (persistent ? 1 : -1)); } -int i2c_write_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) +int i2c_write_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) { int pos = 0; while (pos < size) { @@ -227,7 +227,7 @@ int i2c_write_flash_buf(int fd, char *data, int size, void (*progress_cb)(int po return pos; } -int i2c_read_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) +int i2c_read_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) { int pos = 0; while (pos < size) { @@ -249,7 +249,7 @@ int i2c_read_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos return pos; } -int i2c_verify_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) +int i2c_verify_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) { int pos = 0; while (pos < size) { @@ -275,28 +275,6 @@ int i2c_verify_flash_buf(int fd, char *data, int size, void (*progress_cb)(int p return pos; } -int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(int pos, int size)) -{ - int fd_file = open(filename, O_RDONLY); - if (fd_file < 0) { - perror("open()"); - return -1; - } - - struct stat statbuf; - if (fstat(fd_file, &statbuf) == -1) { - perror("stat()"); - close(fd_file); - return -1; - } - - char *data = malloc(statbuf.st_size); - if (data == NULL) - return -1; - - return i2c_write_flash_buf(fd, data, statbuf.st_size, progress_cb); -} - void i2c_cmd_setpwm(int fd, int pwm) { char cmd[] = { CMD_SET_PWM, pwm }; diff --git a/i2c.h b/i2c.h index 268bf57..7a1f587 100644 --- a/i2c.h +++ b/i2c.h @@ -37,11 +37,9 @@ struct blmc_parameter { void i2c_cmd_getparameters(int fd, struct blmc_parameter *blmc); void i2c_cmd_setparameters(int fd, struct blmc_parameter *blmc, int persistent); -int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(int pos, int size)); - -int i2c_write_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); -int i2c_read_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); -int i2c_verify_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); +int i2c_write_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); +int i2c_read_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); +int i2c_verify_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); void i2c_cmd_setpwm(int fd, int pwm);