Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding button to export stm32 flash memory to a file #691

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/tools/gui/stlink-gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ stlink_gui_set_sensitivity (STlinkGUI *gui, gboolean sensitivity)

if (sensitivity && gui->sl && gui->filename)
gtk_widget_set_sensitive (GTK_WIDGET (gui->flash_button), sensitivity);

gtk_widget_set_sensitive (GTK_WIDGET (gui->export_button), sensitivity && (gui->sl != NULL));
}

static void
Expand Down Expand Up @@ -575,6 +577,7 @@ static void stlink_gui_set_disconnected (STlinkGUI *gui)

gtk_widget_set_sensitive (GTK_WIDGET (gui->device_frame), FALSE);
gtk_widget_set_sensitive (GTK_WIDGET (gui->flash_button), FALSE);
gtk_widget_set_sensitive (GTK_WIDGET (gui->export_button), FALSE);
gtk_widget_set_sensitive (GTK_WIDGET (gui->disconnect_button), FALSE);
gtk_widget_set_sensitive (GTK_WIDGET (gui->connect_button), TRUE);
}
Expand Down Expand Up @@ -712,6 +715,51 @@ flash_button_cb (GtkWidget *widget, gpointer data)
}
}

int export_to_file(const char*filename, const struct mem_t flash_mem)
{
printf("%s\n", filename);
FILE * f=fopen(filename, "w");
if(f==NULL)
return -1;
for(gsize i=0;i<flash_mem.size;i++)
if(fputc(flash_mem.memory[i], f)==EOF)
return -1;
fclose(f);
return 0;
}

static void
export_button_cb (GtkWidget *widget, gpointer data)
{
(void)widget;
STlinkGUI * gui = STLINK_GUI (data);
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Save as",
gui->window,
GTK_FILE_CHOOSER_ACTION_SAVE,
"_Cancel",
GTK_RESPONSE_CANCEL,
"_Open",
GTK_RESPONSE_ACCEPT,
NULL);
GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
gint res = gtk_dialog_run (GTK_DIALOG (dialog));
if (res == GTK_RESPONSE_ACCEPT)
{
char *filename;

filename = gtk_file_chooser_get_filename (chooser);
if(export_to_file (filename, gui->flash_mem)!=0)
stlink_gui_set_info_error_message(gui, "Failed to export flash");
else
stlink_gui_set_info_error_message(gui, "Export successful");
g_free (filename);
}

gtk_widget_destroy (dialog);
}

static gboolean
progress_pulse_timeout (STlinkGUI *gui) {
if (gui->progress.activity_mode) {
Expand Down Expand Up @@ -857,6 +905,11 @@ stlink_gui_build_ui (STlinkGUI *gui) {
g_signal_connect (G_OBJECT (gui->flash_button), "clicked",
G_CALLBACK (flash_button_cb), gui);

gui->export_button =
GTK_TOOL_BUTTON (gtk_builder_get_object (builder, "export_button"));
g_signal_connect (G_OBJECT (gui->export_button), "clicked",
G_CALLBACK (export_button_cb), gui);

gui->devmem_treeview =
GTK_TREE_VIEW (gtk_builder_get_object (builder, "devmem_treeview"));
mem_view_init_headers (gui->devmem_treeview);
Expand Down
2 changes: 2 additions & 0 deletions src/tools/gui/stlink-gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct _STlinkGUI
GtkToolButton *connect_button;
GtkToolButton *disconnect_button;
GtkToolButton *flash_button;
GtkToolButton *export_button;
GtkToolButton *open_button;

/* flash dialog */
Expand All @@ -89,5 +90,6 @@ struct _STlinkGUIClass
};

GType stlink_gui_get_type (void);
int export_to_file(const char*filename, const struct mem_t flash_mem);

#endif
14 changes: 14 additions & 0 deletions src/tools/gui/stlink-gui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="export_button">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Export device memory</property>
<property name="label" translatable="yes">__glade_unnamed_9</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-save</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolProgressbar">
<property name="visible">True</property>
Expand Down