Example #1
0
G_MODULE_EXPORT gboolean select_file_for_ecu_restore(GtkWidget *widget, gpointer data)
{
	MtxFileIO *fileio = NULL;
	gchar *filename = NULL;
	extern gconstpointer *global_data;

	if (!DATA_GET(global_data,"interrogated"))
		return FALSE;

	fileio = g_new0(MtxFileIO ,1);
	fileio->external_path = g_strdup("MTX_ecu_snapshots");
	fileio->parent = lookup_widget_f("main_window");
	fileio->on_top = TRUE;
	fileio->title = g_strdup("Restore your ECU Settings from which file");
	fileio->action = GTK_FILE_CHOOSER_ACTION_OPEN;
	fileio->shortcut_folders = g_strdup("ecu_snapshots,MTX_ecu_snapshots");

	filename = choose_file(fileio);
	if (filename == NULL)
	{
		update_logbar_f("tools_view","warning",_("NO FILE chosen for ECU restore\n"),FALSE,FALSE,FALSE);
		return FALSE;
	}
	update_logbar_f("tools_view",NULL,_("Full Restore of ECU Initiated\n"),FALSE,FALSE,FALSE);
	restore_all_ecu_settings(filename);
	g_free(filename);
	free_mtxfileio(fileio);
	return TRUE;

}
Example #2
0
/*!
 \brief process_header() processes portions of the header of the VEX file 
 and populates the Vex_Import datastructure for this Table
 \param vex (Vex_Import *) pointer to the Vex_Import structure
 \param arg (ImportPArserArg) enumeration of header portion to process
 \param string (gchar *) text of the header line to process
 \returns status of the operation (G_IO_STATUS_ERROR/G_IO_STATUS_NORMAL)
 */
G_MODULE_EXPORT GIOStatus process_header(Vex_Import *vex, ImportParserArg arg, gchar * string)
{
	gchar ** str_array = NULL;
	gchar *result = NULL;
	gchar *tmpbuf = NULL;

	if (!string)
	{
		dbg_func_f(CRITICAL,g_strdup(__FILE__": process_header()\n\t String passed was NULL\n"));
		return G_IO_STATUS_ERROR;
	}
	str_array = g_strsplit(string, " ", 2);
	result = g_strdup(str_array[1]);	
	g_strfreev(str_array);
	switch (arg)
	{
		case VEX_EVEME:
			vex->version = g_strdup(result);
			tmpbuf = g_strdup_printf(_("VEX Header: EVEME %s"),result);
			update_logbar_f("tools_view", NULL, tmpbuf,FALSE,FALSE,FALSE);
			g_free(tmpbuf);
			break;
		case VEX_USER_REV:	
			vex->revision = g_strdup(result);
			tmpbuf = g_strdup_printf(_("VEX Header: Revision %s"),result);
			update_logbar_f("tools_view", NULL, tmpbuf,FALSE,FALSE,FALSE);
			g_free(tmpbuf);
			break;
		case VEX_USER_COMMENT:	
			vex->comment = g_strdup(result);
			tmpbuf = g_strdup_printf(_("VEX Header: UserComment: %s"),result);
			update_logbar_f("tools_view", NULL,tmpbuf,FALSE,FALSE,FALSE);
			g_free(tmpbuf);
			break;
		case VEX_DATE:	
			vex->date = g_strdup(result);
			tmpbuf = g_strdup_printf(_("VEX Header: Date %s"),result);
			update_logbar_f("tools_view", NULL,tmpbuf,FALSE,FALSE,FALSE);
			g_free(tmpbuf);
			break;
		case VEX_TIME:	
			vex->time = g_strdup(result);
			tmpbuf =  g_strdup_printf(_("VEX Header: Time %s"),result);
			update_logbar_f("tools_view", NULL, tmpbuf,FALSE,FALSE,FALSE);
			g_free(tmpbuf);
			break;
		default:
			break;

	}
	g_free(result);
	return G_IO_STATUS_NORMAL;

}
Example #3
0
G_MODULE_EXPORT gboolean select_vex_for_export(GtkWidget *widget, gpointer data)
{
	MtxFileIO *fileio = NULL;
	gchar *filename = NULL;
	GIOChannel *iochannel = NULL;
	struct tm *tm = NULL;
	time_t *t = NULL;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");

	if (!DATA_GET(global_data,"interrogated"))
		return FALSE;

	t = g_malloc(sizeof(time_t));
	time(t);
	tm = localtime(t);
	g_free(t);


	fileio = g_new0(MtxFileIO ,1);
	fileio->external_path = g_strdup("MTX_VexFiles");
	fileio->title = g_strdup("Save your VEX file");
	fileio->parent = lookup_widget_f("main_window");
	fileio->on_top = TRUE;
	fileio->default_filename= g_strdup("VEX_Backup.vex");
	fileio->default_filename= g_strdup_printf("%s-%.4i%.2i%.2i%.2i%.2i.vex",g_strdelimit(firmware->name," ,",'_'),tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min);
	fileio->default_extension= g_strdup("vex");
	fileio->action = GTK_FILE_CHOOSER_ACTION_SAVE;

	filename = choose_file(fileio);
	if (filename == NULL)
	{
		update_logbar_f("tools_view","warning",_("NO FILE chosen for VEX export\n"),FALSE,FALSE,FALSE);
		return FALSE;
	}

	iochannel = g_io_channel_new_file(filename, "a+",NULL);
	if (!iochannel)
	{
		update_logbar_f("tools_view","warning",_("File open FAILURE! \n"),FALSE,FALSE,FALSE);
		return FALSE;
	}
	if (vex_comment == NULL)
		update_logbar_f("tools_view",NULL,_("VEX File Opened. VEX Comment undefined, exporting without one.\n"),FALSE,FALSE,FALSE);
	else
		update_logbar_f("tools_view",NULL,_("VEX File Opened. VEX Comment already stored.\n"),FALSE,FALSE,FALSE);
	all_table_export(iochannel);
	g_io_channel_shutdown(iochannel,TRUE,NULL);
	g_io_channel_unref(iochannel);
	free_mtxfileio(fileio);
	return TRUE;
}
Example #4
0
/*!
 \brief Firmware specific function that backs up the ECU to a filename passed
 \param filename is the filename to backup the ECU to
 */
G_MODULE_EXPORT void backup_all_ecu_settings(gchar *filename)
{
	ConfigFile *cfgfile;
	gchar * section = NULL;
	gchar * tmpbuf = NULL;
	gint i = 0;
	gint locID = 0;
	gint x = 0;
	gint canID = 0;
	DataSize size = MTX_U08;	 /* <<<<< BAD BAD BAD >>>>> */
	GString *string = NULL;
	extern gconstpointer *global_data;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");
	g_return_if_fail(filename);
	g_return_if_fail(firmware);

	cfgfile = cfg_open_file(filename);
	if(!cfgfile)
		cfgfile = cfg_new();

	set_file_api_f(cfgfile,BACKUP_MAJOR_API,BACKUP_MINOR_API);

	update_logbar_f("tools_view",NULL,g_strdup_printf("%s %s\n",_("Full Backup Commencing to file:\n\t"),filename),FALSE,FALSE,TRUE);
	cfg_write_string(cfgfile,"Firmware","name",firmware->name);
	for(i=0;i<firmware->total_pages;i++)
	{
		if (firmware->page_params[i]->read_only)
			continue;
		string = g_string_sized_new(64);
		section = g_strdup_printf("page_%i",i);
		cfg_write_int(cfgfile,section,"num_variables",firmware->page_params[i]->length);
		for(x=0;x<firmware->page_params[i]->length;x++)
		{
			locID = firmware->page_params[i]->phys_ecu_page;
			g_string_append_printf(string,"%i",freeems_get_ecu_data(canID,locID,x,size));
			if (x < (firmware->page_params[i]->length-1))
				string = g_string_append(string,",");
		}
		cfg_write_string(cfgfile,section,"data",string->str);
		g_free(section);
		g_string_free(string,TRUE);
	}
	update_logbar_f("tools_view",NULL,_("Full Backup Complete...\n"),FALSE,FALSE,FALSE);
	cfg_write_file(cfgfile,filename);
	cfg_free(cfgfile);
}
Example #5
0
/*!
 \brief revert_to_previous_data() reverts the VEX import by using the backup
 of the internal datastructures.
 */
G_MODULE_EXPORT void revert_to_previous_data(void)
{
	gint canID=0;
	gint page=0;
	gint offset=0;
	gint total = 0;
	guchar *data = NULL;
	GModule *module = NULL;
	PostFunction *pf = NULL;
	GArray *pfuncs = NULL;
	guint8 **ecu_data_backup = NULL;
	/* Called to back out a load of a VEtable from VEX import */
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");
	ecu_data_backup = firmware->ecu_data_backup;

	for (page=0;page<firmware->total_pages;page++)
	{
		if (!firmware->page_params[page]->dl_by_default)
			continue;
		if (firmware->chunk_support)
		{
			total = firmware->page_params[page]->length;
			data = g_new0(guchar,total);
			for (offset=0;offset<total;offset++)
				data[offset]=ms_get_ecu_data_backup(canID,page,offset,MTX_U08);
			ms_chunk_write(canID,page,0,total,data);

		}
		else
		{
			for (offset = 0;offset<firmware->page_params[page]->length;offset++)
			{
				if (ms_get_ecu_data_backup(canID,page,offset,MTX_U08) != ms_get_ecu_data(canID,page,offset,MTX_U08))
				{
					ms_send_to_ecu(canID,page,offset,MTX_U08,ecu_data_backup[page][offset], FALSE);
				}
			}
		}
	}
	module = g_module_open(NULL,G_MODULE_BIND_LAZY);
	pfuncs = g_array_new(FALSE,TRUE,sizeof(PostFunction *));

	pf = g_new0(PostFunction,1);
	pf->name = g_strdup("update_ecu_controls_pf");
	if (module)
		g_module_symbol(module,pf->name,(void *)&pf->function);
	pf->w_arg = FALSE;
	pfuncs = g_array_append_val(pfuncs,pf);
	g_module_close(module);
	io_cmd_f(NULL,pfuncs);

	gtk_widget_set_sensitive(lookup_widget_f("tools_undo_vex_button"),FALSE);
	update_logbar_f("tools_view","warning",_("Reverting to previous settings....\n"),FALSE,FALSE,FALSE);
	io_cmd_f(firmware->burn_all_command,NULL);
}
Example #6
0
/*!
 \brief vex_comment_parse() stores the comment field  as entered by the
 user on the Megatunix GUI for VEX export.
 \param widget (GtkWidget *) pointer to textentry widget where user enters
 the comment
 \param data (gpointer) unused
 \returns TRUE
 */
G_MODULE_EXPORT gboolean vex_comment_parse(GtkWidget *widget, gpointer data)
{
	/* Gets data from VEX comment field in tools gui and stores it 
	 * so that it gets written to the vex file 
	 */
	vex_comment = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)));
	update_logbar_f("tools_view",NULL,_("VEX Comment Stored\n"),FALSE,FALSE,FALSE);
	return TRUE;
}
Example #7
0
G_MODULE_EXPORT void select_table_for_import(gint table_num)
{
	MtxFileIO *fileio = NULL;
	gchar *filename = NULL;
	GIOChannel *iochannel = NULL;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");

	if (!DATA_GET(global_data,"interrogated"))
		return;

	if ((table_num < 0) || (table_num >= firmware->total_tables))
		return;

	fileio = g_new0(MtxFileIO ,1);
	fileio->external_path = g_strdup("MTX_VexFiles");
	fileio->parent = lookup_widget_f("main_window");
	fileio->on_top = TRUE;
	fileio->title = g_strdup("Select your VEX file to import");
	fileio->action = GTK_FILE_CHOOSER_ACTION_OPEN;

	filename = choose_file(fileio);
	if (filename == NULL)
	{
		update_logbar_f("tools_view","warning",_("NO FILE chosen for VEX import\n"),FALSE,FALSE,FALSE);
		return;
	}

	iochannel = g_io_channel_new_file(filename, "r+",NULL);
	if (!iochannel)
	{
		update_logbar_f("tools_view","warning",_("File open FAILURE! \n"),FALSE,FALSE,FALSE);
		return;
	}
	update_logbar_f("tools_view",NULL,_("VEX File Closed\n"),FALSE,FALSE,FALSE);
	gtk_entry_set_text(GTK_ENTRY(lookup_widget_f("tools_vex_comment_entry")),"");

	single_table_import(iochannel,table_num);
	g_io_channel_shutdown(iochannel,TRUE,NULL);
	g_io_channel_unref(iochannel);
	free_mtxfileio(fileio);
	return;
}
Example #8
0
G_MODULE_EXPORT gboolean select_file_for_ecu_backup(GtkWidget *widget, gpointer data)
{
	MtxFileIO *fileio = NULL;
	gchar *filename = NULL;
	struct tm *tm = NULL;
	time_t *t = NULL;
	extern gconstpointer *global_data;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");


	if (!DATA_GET(global_data,"interrogated"))
		return FALSE;

	t = g_malloc(sizeof(time_t));
	time(t);
	tm = localtime(t);
	g_free(t);

	fileio = g_new0(MtxFileIO ,1);
	fileio->external_path = g_strdup("MTX_ecu_snapshots");
	fileio->title = g_strdup("Save your ECU Settings to file");
	fileio->parent = lookup_widget_f("main_window");
	fileio->on_top = TRUE;
	fileio->default_filename = g_strdup_printf("%s-%.4i_%.2i_%.2i-%.2i%.2i.ecu",g_strdelimit(firmware->name," ,",'_'),tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min);
	fileio->default_extension = g_strdup("ecu");
	fileio->action = GTK_FILE_CHOOSER_ACTION_SAVE;
	fileio->shortcut_folders = g_strdup("MTX_ecu_snapshots");

	filename = choose_file(fileio);
	if (filename == NULL)
	{
		update_logbar_f("tools_view","warning",_("NO FILE chosen for ECU Backup\n"),FALSE,FALSE,FALSE);
		return FALSE;
	}
	update_logbar_f("tools_view",NULL,_("Full Backup of ECU Initiated\n"),FALSE,FALSE,FALSE);
	backup_all_ecu_settings(filename);
	update_logbar_f("tools_view",NULL,_("Full Backup File Closed\n"),FALSE,FALSE,FALSE);
	g_free(filename);
	free_mtxfileio(fileio);
	return TRUE;
}
Example #9
0
/*!
 \brief process_page() extracts the page variable from the VEX file and 
 stores it in the Vex_Import Structure for this table.
 \param vex (Vex_Import *) Pointer to the Vex_Import structure
 \param string (gchar *) line of VEX file in which to extract the page out of
 \returns status of the operation (G_IO_STATUS_ERROR/G_IO_STATUS_NORMAL)
 */
G_MODULE_EXPORT GIOStatus process_page(Vex_Import *vex, gchar *string)
{
	GIOStatus status = G_IO_STATUS_ERROR;
	gchar *tmpbuf = NULL;
	gchar ** str_array = NULL;
	gint page = -1;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");

	if (!string)
	{
		dbg_func_f(CRITICAL,g_strdup(__FILE__": process_page()\n\t String passed was NULL\n"));
		return G_IO_STATUS_ERROR;
	}
	str_array = g_strsplit(string, " ", 2);	
	page = atoi(str_array[1]);
	g_strfreev(str_array);
	if ((page < 0 ) || (page > firmware->total_pages))
	{
		status =  G_IO_STATUS_ERROR;
		tmpbuf = g_strdup_printf(_("VEX Import: Page %i out of range <---ERROR\n"),page);
		update_logbar_f("tools_view","warning",tmpbuf,FALSE,FALSE,FALSE);
		g_free(tmpbuf);
		return status;
	}
	else
	{
		status = G_IO_STATUS_NORMAL;
		vex->page = page;
		vex->got_page = TRUE;
		tmpbuf = g_strdup_printf(_("VEX Import: Page %i\n"),page);
		update_logbar_f("tools_view",NULL,tmpbuf,FALSE,FALSE,FALSE);
		g_free(tmpbuf);

	}

	status = process_table(vex);
	return status;
}
Example #10
0
/*!
 \brief process_table() extracts the table_number out of the comment field
 stores it in the Vex_Import Structure for this table.
 \param vex (Vex_Import *) Pointer to the Vex_Import structure
 \returns status of the operation (G_IO_STATUS_ERROR/G_IO_STATUS_NORMAL)
 */
G_MODULE_EXPORT GIOStatus process_table(Vex_Import *vex)
{
	gchar **string = NULL;
	gchar *tmpbuf = NULL;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");

	/* Search for out magic semicolon, if missing AND multi-table/page
	 * firmware, ABORT */
	if ((vex->table < 0) && (vex->table <= firmware->total_tables))
	{
		if ((!g_strrstr(vex->comment,";")) && (firmware->total_tables == firmware->total_tables))
		{
			update_logbar_f("tools_view","warning",_("VEX Import: Multi Table per page firmware,\nbut Table number is not defined in comment field, load aborted!!!\n"),FALSE,FALSE,FALSE);
			return G_IO_STATUS_ERROR;
		}

		string = g_strsplit(vex->comment,";",-1);
		if ((string[0] == NULL) || (!g_strrstr(string[0],"Table")))
		{
			update_logbar_f("tools_view","warning",_("VEX Import: Multi Table per page firmware,\n\tbut Table number is not defined in comment field, load aborted!!!\n"),FALSE,FALSE,FALSE);
			vex->table = -1;
			g_strfreev(string);
			return G_IO_STATUS_ERROR;
		}
		else
		{
			/* the +5 gets us past theword "Table" */
			vex->table = (gint)g_ascii_strtod(string[0]+5,NULL);
			g_strfreev(string);
		}
	}

	tmpbuf = g_strdup_printf(_("VEX Import: Table %i\n"),vex->table);
	update_logbar_f("tools_view",NULL,tmpbuf,FALSE,FALSE,FALSE);
	g_free(tmpbuf);
	return G_IO_STATUS_NORMAL;
}
Example #11
0
/*!
 \brief process_vex_range() is used to read the vex_ranges for RPM/LOAD 
 allocate the memory for storing the data and call the needed functions to
 read the values into the arrays.
 \see read_number_from_line
 \param vex (Vex_Import *) pointer to the Vex_Import structure
 \param arg (ImportParserArg) enumeration to decide which range we are going to
 read
 \param string (gchar *) Line of text passed to parse.
 \param iochannel (GIOChannel *) Pointer to iochannel representing VEXfile for
 retrieving additional data.
 \returns status of the operation (G_IO_STATUS_ERROR/G_IO_STATUS_NORMAL)
 */
G_MODULE_EXPORT GIOStatus process_vex_range(Vex_Import *vex, ImportParserArg arg, gchar * string, GIOChannel *iochannel)
{
	GIOStatus status = G_IO_STATUS_ERROR;
	gint i = 0;
	gint value = 0;
	gint num_bins = 0;
	gchar ** str_array = NULL;
	gchar * result = NULL;

	if (!string)
	{
		dbg_func_f(CRITICAL,g_strdup(__FILE__": process_vex_range()\n\t String passed was NULL\n"));
		return G_IO_STATUS_ERROR;
	}
	str_array = g_strsplit(string, "[", 2);
	result = g_strdup(str_array[1]);	
	g_strfreev(str_array);
	str_array = g_strsplit(result, "]", 2);
	result = g_strdup(str_array[0]);	

	num_bins = atoi(result);
	/* Allocate memory for array of values... :) */
	switch(arg)
	{
		case VEX_RPM_RANGE:
			vex->total_x_bins = num_bins;
			vex->x_bins = g_new0(gint, num_bins);
			vex->got_rpm = TRUE;
			break;
		case VEX_LOAD_RANGE:
			vex->total_y_bins = num_bins;
			vex->y_bins = g_new0(gint, num_bins);
			vex->got_load = TRUE;
			break;
		default:
			break;
	}

	for (i=0; i<num_bins; i++) 
	{
		status = read_number_from_line(&value,iochannel);
		if (status != G_IO_STATUS_NORMAL) 
		{
			update_logbar_f("tools_view","warning",_("VEX Import: File I/O Read problem, file may be incomplete <---ERROR\n"),FALSE,FALSE,FALSE);
			break;
		}
		switch (arg)
		{
			case VEX_RPM_RANGE:
				vex->x_bins[i] = value;
				break;
			case VEX_LOAD_RANGE:
				vex->y_bins[i] = value;
				break;
			default:
				break;
		}
	}
	if (status == G_IO_STATUS_NORMAL)
	{
		if (arg == VEX_RPM_RANGE)
			update_logbar_f("tools_view",NULL,_("VEX Import: RPM bins loaded successfully \n"),FALSE,FALSE,FALSE);
		if (arg == VEX_LOAD_RANGE)
			update_logbar_f("tools_view",NULL,_("VEX Import: LOAD bins loaded successfully \n"),FALSE,FALSE,FALSE);
	}
	return status;
}
Example #12
0
/*!
 \brief restore_all_ecu_settings() reads the filename passed and if all checks
 pass the file will be loaded and any values that differ from the values
 currently in the ECU will be replaced.
 \param filename (filename to read for ecu restoration
WARNING:  This function is not yet capable of handling CAN devices, and will
always restore to can ID ZERO (which can be BAD!!), backup/restore needs to
be rewritten..
 */
G_MODULE_EXPORT void restore_all_ecu_settings(gchar *filename)
{
	ConfigFile *cfgfile;
	/*
	GArray *pfuncs = NULL;
	PostFunction *pf = NULL;
	*/
	gchar * section = NULL;
	gchar * msgbuf = NULL;
	gint canID = 0;
	DataSize size = MTX_U08;
	gint page = 0;
	gint offset = 0;
	gint tmpi = 0;
	gint major = 0;
	gint minor = 0;
	gboolean restart = FALSE;
	gchar *tmpbuf = NULL;
	guint8 *data = NULL;
	gchar **keys = NULL;
	gint num_keys = 0;
	gint dload_val = 0;
	PostFunction *pf = NULL;
	GArray *pfuncs = NULL;
	extern gconstpointer *global_data;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");
	g_return_if_fail(filename);
	g_return_if_fail(firmware);
	canID = firmware->canID;

	cfgfile = cfg_open_file(filename);
	if (!cfgfile)
	{
		update_logbar_f("tools_view","warning",g_strdup_printf(_(":restore_all_ecu_settings()\n\t Unable to open this file (%s)\n"),filename),FALSE,FALSE,TRUE);
		return;
	}
	if (cfgfile)
	{
		get_file_api_f(cfgfile,&major,&minor);
		if (major != BACKUP_MAJOR_API) 
		{
			update_logbar_f("tools_view","warning",g_strdup_printf(_(":restore_all_ecu_settings()\n\tAPI MAJOR version mismatch: \"%i\" != \"%i\"\n can not load this file for restoration\n"),major,BACKUP_MAJOR_API),FALSE,FALSE,TRUE);
			cfg_free(cfgfile);
			return;
		}
		if (minor != BACKUP_MINOR_API) 
			update_logbar_f("tools_view","warning",g_strdup_printf(_(": restore_all_ecu_settings()\n\tAPI MINOR version mismatch: \"%i\" != \"%i\"\n Will try to load this file for restoration, expect issues\n"),minor,BACKUP_MINOR_API),FALSE,FALSE,TRUE);

		cfg_read_string(cfgfile,"Firmware","name",&tmpbuf);
		if (g_ascii_strcasecmp(g_strdelimit(tmpbuf," ,",'_'),g_strdelimit(firmware->name," ,",'_')) != 0)
		{
			dbg_func_f(CRITICAL,g_strdup_printf(__FILE__": restore_all_ecu_settings()\nFirmware name mismatch:\n\"%s\" != \"%s\",\ncannot load this file for restoration\n",tmpbuf,firmware->name));

			update_logbar_f("tools_view","warning",g_strdup_printf(_(": restore_all_ecu_settings()\nFirmware name mismatch: \"%s\" != \"%s\"\ncan NOT load this file for restoration!\n"),tmpbuf,firmware->name),FALSE,FALSE,TRUE);
			if (tmpbuf)
				g_free(tmpbuf);
			cfg_free(cfgfile);
			return;
		}
		g_free(tmpbuf);
		set_title_f(g_strdup(_("Restoring ECU settings from File")));
		if (DATA_GET(global_data,"realtime_id"))
		{
			stop_tickler_f(RTV_TICKLER);
			restart = TRUE;
		}
		for (page=0;page<firmware->total_pages;page++)
		{
			if (!(firmware->page_params[page]->dl_by_default))
				continue;

			section = g_strdup_printf("page_%i",page);
			if(cfg_read_int(cfgfile,section,"num_variables",&tmpi))
				if (tmpi != firmware->page_params[page]->length)
				{
					update_logbar_f("tools_view","warning",g_strdup_printf(_(": restore_all_ecu_settings()\n\tNumber of variables in backup \"%i\" and firmware specification \"%i\" do NOT match,\n\tcorruption SHOULD be expected\n"),tmpi,firmware->page_params[page]->length),FALSE,FALSE,TRUE);
					dbg_func_f(CRITICAL,g_strdup_printf(_(": restore_all_ecu_settings()\n\tNumber of variables in backup \"%i\" and firmware specification \"%i\" do NOT match,\n\tcorruption SHOULD be expected\n"),tmpi,firmware->page_params[page]->length));
				}
			if (cfg_read_string(cfgfile,section,"data",&tmpbuf))
			{
				keys = parse_keys_f(tmpbuf,&num_keys,",");
				if (num_keys != firmware->page_params[page]->length)
				{
					update_logbar_f("tools_view","warning",g_strdup_printf(_(": restore_all_ecu_settings()\n\tNumber of variables in this backup \"%i\" does NOT match the length of the table \"%i\", expect a crash!!!\n"),num_keys,firmware->page_params[page]->length),FALSE,FALSE,TRUE);
					dbg_func_f(CRITICAL,g_strdup_printf(_(": restore_all_ecu_settings()\n\tNumber of variables in this backup \"%i\" does NOT match the length of the table \"%i\", expect a crash!!!\n"),num_keys,firmware->page_params[page]->length));
				}
				if (firmware->chunk_support)
				{
					data = g_new0(guint8, firmware->page_params[page]->length);
					for (offset=0;offset<num_keys;offset++)
						data[offset]=(guint8)atoi(keys[offset]);
					if (DATA_GET(global_data,"offline"))
						ms_store_new_block(canID,page,0,data,num_keys);
					else
						ms_chunk_write(canID,page,0,num_keys,data);
				}
				else
				{
					if (DATA_GET(global_data,"offline"))
					{
						for (offset=0;offset<num_keys;offset++)
						{
							dload_val = atoi(keys[offset]);
							ms_set_ecu_data(canID,page,offset,size,dload_val);
						}
					}
					else
					{
						for (offset=0;offset<num_keys;offset++)
						{
							dload_val = atoi(keys[offset]);
							if (dload_val != ms_get_ecu_data_last(canID,page,offset,size))
							{
								/*printf("writing data for page %i, offset %i\n",page,offset);*/
								ms_send_to_ecu(canID,page,offset,size,dload_val, FALSE);
							}
						}
						queue_burn_ecu_flash(page);
					}
				}
				g_strfreev(keys);
				g_free(tmpbuf);
			}
			g_free(section);
		}
		start_restore_monitor();
		cfg_free(cfgfile);
	}
	if (DATA_GET(global_data,"offline"))
	{
		pfuncs = g_array_new(FALSE,TRUE,sizeof(PostFunction *));
		pf = g_new0(PostFunction,1);
		pf->name = g_strdup("update_ecu_controls_pf");
		get_symbol_f(pf->name,(void *)&pf->function);
		pf->w_arg = FALSE;
		pfuncs = g_array_append_val(pfuncs,pf);

		pf = g_new0(PostFunction,1);
		pf->name = g_strdup("set_store_black_pf");
		get_symbol_f(pf->name,(void *)&pf->function);
		pf->w_arg = FALSE;
		pfuncs = g_array_append_val(pfuncs,pf);

		io_cmd_f(NULL,pfuncs);
	}
	if (restart)
		start_tickler_f(RTV_TICKLER);
}
Example #13
0
/*!
 \brief check_for_match() compares the resutls of the interrogation with the
 ECU to the canidates in turn. When a match occurs TRUE is returned
 otherwise it returns FALSE
 \param cmd_array (GArray *) array of commands
 \param potential (Canidate *) potential 
 \param canidate (Canidate *) Canidate
 \returns TRUE on match, FALSE on failure
 */
G_MODULE_EXPORT gboolean check_for_match(GHashTable *tests_hash, gchar *filename)
{
	ConfigFile *cfgfile = NULL;
	Detection_Test *test = NULL;
	guint i = 0;
	gint len = 0;
	gboolean pass = FALSE;
	gchar * tmpbuf = NULL;
	gchar ** vector = NULL;
	gchar ** match_on = NULL;
	gint major = 0;
	gint minor = 0;
	MatchClass class = 0;

	cfgfile = cfg_open_file(filename);
	if (!cfgfile)
		return FALSE;

	get_file_api_f(cfgfile,&major,&minor);
	if ((major != INTERROGATE_MAJOR_API) || (minor != INTERROGATE_MINOR_API))
	{
		update_logbar_f("interr_view","warning",g_strdup_printf(_("Interrogation profile API mismatch (%i.%i != %i.%i):\n\tFile %s will be skipped\n"),major,minor,INTERROGATE_MAJOR_API,INTERROGATE_MINOR_API,filename),FALSE,FALSE,TRUE);
		cfg_free(cfgfile);
		return FALSE;
	}

	if (cfg_read_string(cfgfile,"interrogation","match_on",&tmpbuf) == FALSE)
		printf(_("ERROR:!! \"match_on\" key missing from interrogation profile [interrogation] section\n"));
	match_on = g_strsplit(tmpbuf,",",-1);
	g_free(tmpbuf);

	for (i=0;i<g_strv_length(match_on);i++)
	{
		pass = FALSE;
		/*printf("checking for match on %s\n",match_on[i]);*/
		test = g_hash_table_lookup(tests_hash,match_on[i]);
		if (!test)
		{
			printf(_("ERROR test data not found for test \"%s\"\n"),match_on[i]);
			continue;
		}

		/* If the test_name is NOT IN the interrogation profile,  we 
		 * abort as it's NOT match
		 */
		if (!cfg_read_string(cfgfile,"interrogation",test->test_name,&tmpbuf))
		{
			dbg_func_f(INTERROGATOR,g_strdup_printf("\n"__FILE__": check_for_match()\n\tMISMATCH,\"%s\" is NOT a match...\n\n",filename));
			cfg_free(cfgfile);
			g_strfreev(match_on);
			return FALSE;
		}
		vector = g_strsplit(tmpbuf,",",-1);
		g_free(tmpbuf);
		/* Possible choices are "Count", "submatch" and "fullmatch", so
		 * stringparse to get them into a consistent form
		 */
		if (g_strv_length(vector) != 2)
			printf(_("ERROR interrogation check_for match vector does NOT have two args it has %i\n"),g_strv_length(vector));
		class = translate_string_f(vector[0]);
		/*printf("potential data is %s\n",vector[1]);*/
		switch (class)
		{
			case COUNT:
				if (test->num_bytes == atoi(vector[1]))
					pass=TRUE;
				break;
			case NUMMATCH:
				if ((GINT)(test->result_str[0]) == atoi(vector[1]))
					pass=TRUE;
				break;
			case SUBMATCH:
				if (strstr(test->result_str,vector[1]) != NULL)
					pass=TRUE;
				break;
			case FULLMATCH:
				if (g_ascii_strcasecmp(test->result_str,vector[1]) == 0)
					pass=TRUE;
				break;
			case REGEX:
				if (regex(vector[1],test->result_str,&len))
					pass=TRUE;
				break;
			default:
				pass=FALSE;
		}
		g_strfreev(vector);
		if (pass == TRUE)
			continue;
		else
		{
			dbg_func_f(INTERROGATOR,g_strdup_printf("\n"__FILE__": check_for_match()\n\tMISMATCH,\"%s\" is NOT a match...\n\n",filename));
			g_strfreev(match_on);
			cfg_free(cfgfile);
			return FALSE;
		}

	}
	g_strfreev(match_on);
	dbg_func_f(INTERROGATOR,g_strdup_printf("\n"__FILE__": check_for_match()\n\t\"%s\" is a match for all conditions ...\n\n",filename));
	cfg_free(cfgfile);
	return TRUE;
}
Example #14
0
G_MODULE_EXPORT gboolean determine_ecu(GArray *tests, GHashTable *tests_hash)
{
	gboolean retval = TRUE;
	gint i = 0;
	Detection_Test *test = NULL;
	gint num_tests = tests->len;
	gboolean match = FALSE;
	gchar * filename = NULL;
	gchar ** filenames = NULL;
	GArray *classes = NULL;
	Firmware_Details *firmware = NULL;
	extern gconstpointer *global_data;

	filenames = get_files(g_build_filename(INTERROGATOR_DATA_DIR,"Profiles",DATA_GET(global_data,"ecu_family"),NULL),g_strdup("prof"),&classes);
	if (!filenames)
	{
		dbg_func_f(INTERROGATOR|CRITICAL,g_strdup(__FILE__": determine_ecu()\n\t NO Interrogation profiles found,  was MegaTunix installed properly?\n\n"));
		return FALSE;
	}

	i = 0;
	while (filenames[i])
	{
		if (check_for_match(tests_hash,filenames[i]))
		{
			match = TRUE;
			filename = g_strdup(filenames[i]);
			break;
		}
		i++;
	}
	g_strfreev(filenames);
	g_array_free(classes,TRUE);
	/* Update the screen with the data... */
	for (i=0;i<num_tests;i++)
	{
		test = g_array_index(tests,Detection_Test *,i);
		if (test->result_type == RESULT_DATA)
		{
			dbg_func_f(INTERROGATOR,g_strdup_printf("\tCommand (%s), returned %i byts\n",test->test_desc,test->num_bytes));
			thread_update_logbar_f("interr_view","info",g_strdup_printf("\tCommand (%s), returned %i bytes)\n",test->test_desc,test->num_bytes),FALSE,FALSE);
		}
		if (test->result_type == RESULT_TEXT)
		{
			dbg_func_f(INTERROGATOR,g_strdup_printf("\tCommand (%s), returned (%s)\n",test->test_desc,test->result_str));
			thread_update_logbar_f("interr_view","info",g_strdup_printf("\tCommand (%s), returned (%s)\n",test->test_desc,test->result_str),FALSE,FALSE);
		}
		else if (test->result_type == RESULT_LIST)
		{
			dbg_func_f(INTERROGATOR,g_strdup_printf("\tCommand (%s), returned %i elements\n",test->test_desc,g_list_length((GList *)test->result)));
			thread_update_logbar_f("interr_view","info",g_strdup_printf("\tCommand (%s), returned %i elements\n",test->test_desc,g_list_length((GList *)test->result)),FALSE,FALSE);
		}
	}
	if (match == FALSE) /* (we DID NOT find one) */
	{
		dbg_func_f(INTERROGATOR|CRITICAL,g_strdup(__FILE__":\n\tdetermine_ecu()\n\tFirmware NOT DETECTED, Enable Interrogation debugging, retry interrogation,\nclose megatunix, and send ~/MTXlog.txt to the author for analysis with a note\ndescribing which firmware you are attempting to talk to.\n"));
		update_logbar_f("interr_view","warning",g_strdup("Firmware NOT DETECTED, Enable Interrogation debugging, retry interrogation,\nclose megatunix, and send ~/MTXlog.txt to the author for analysis with a note\ndescribing which firmware you are attempting to talk to.\n"),FALSE,FALSE,TRUE);
		retval = FALSE;
	}
	else
	{
		if (!firmware)
		{
			firmware = g_new0(Firmware_Details,1);
			DATA_SET(global_data,"firmware",firmware);
		}

		if (!load_firmware_details(firmware,filename))
			retval = FALSE;
	}
	g_free(filename);
	return(retval);
}
Example #15
0
/*!
 \brief validate_and_load_tests() loads the list of tests from the system
 checks them for validity, populates and array and returns it
 command tested against the ECU arestored
 \returns a dynamic GArray for commands
 */
G_MODULE_EXPORT gboolean validate_and_load_tests(GArray **tests, GHashTable **tests_hash)
{
	ConfigFile *cfgfile;
	Detection_Test *test = NULL;
	gchar * filename = NULL;
	gchar *section = NULL;
	gchar * tmpbuf = NULL;
	gchar ** vector = NULL;
	gint total_tests = 0;
	gint result = 0;
	gint major = 0;
	gint minor = 0;
	gint i = 0;
	gint j = 0;

	filename = get_file(g_build_filename(INTERROGATOR_DATA_DIR,"Profiles",DATA_GET(global_data,"ecu_family"),"tests.cfg",NULL),NULL);
	if (!filename)
	{
		update_logbar_f("interr_view","warning",g_strdup_printf(_("Interrogation profile tests file %s not found!\n"),filename),FALSE,FALSE,TRUE);
		return FALSE;
	}

	cfgfile = cfg_open_file(filename);
	if (!cfgfile)
		return FALSE;
	get_file_api_f(cfgfile,&major,&minor);
	if ((major != INTERROGATE_MAJOR_API) || (minor != INTERROGATE_MINOR_API))
	{
		update_logbar_f("interr_view","warning",g_strdup_printf(_("Interrogation profile tests API mismatch (%i.%i != %i.%i):\n\tFile %s.\n"),major,minor,INTERROGATE_MAJOR_API,INTERROGATE_MINOR_API,filename),FALSE,FALSE,TRUE);
		return FALSE;
	}

	*tests_hash = g_hash_table_new_full(g_str_hash,g_str_equal,NULL,test_cleanup);

	dbg_func_f(INTERROGATOR,g_strdup_printf(__FILE__": validate_and_load_tests()\n\tfile %s, opened successfully\n",filename));
	*tests = g_array_new(FALSE,TRUE,sizeof(Detection_Test *));
	cfg_read_int(cfgfile,"interrogation_tests","total_tests",&total_tests);
	for (i=0;i<total_tests;i++)
	{
		test = g_new0(Detection_Test, 1);
		section = g_strdup_printf("test_%.2i",i);
		if (!cfg_read_string(cfgfile,section,"test_name",&test->test_name))
		{
			dbg_func_f(INTERROGATOR|CRITICAL,g_strdup_printf(__FILE__": validate_and_load_tests(),\n\ttest_name for %s is NULL\n",section));
			g_free(section);
			break;
		}
		if (!cfg_read_string(cfgfile,section,"test_result_type",&tmpbuf))
		{
			dbg_func_f(INTERROGATOR|CRITICAL,g_strdup_printf(__FILE__": validate_and_load_tests(),\n\ttest_result_type for %s is NULL\n",section));
			g_free(section);
			break;
		}
		else
		{
			test->result_type=translate_string_f(tmpbuf);
			g_free(tmpbuf);
		}
		if (!cfg_read_string(cfgfile,section,"test_func",&test->test_func))
		{
			dbg_func_f(INTERROGATOR|CRITICAL,g_strdup_printf(__FILE__": validate_and_load_tests(),\n\ttest_function for %s is NULL\n",section));
			g_free(section);
			break;
		}
		get_symbol_f(test->test_func,(void *)&test->function);
		cfg_read_string(cfgfile,section,"test_desc",
				&test->test_desc);
		g_free(section);
		g_array_append_val(*tests,test);
		g_hash_table_insert(*tests_hash,test->test_name,test);
	}
	cfg_free(cfgfile);
	g_free(filename);
	return TRUE;
}
Example #16
0
/*!
 \brief interrogate_ecu() interrogates the target ECU to determine what
 firmware it is running.  It does this by reading a list of tests, sending
 those tests in turn, reading the responses and them comparing the group of
 responses against a list of interrogation profiles until it finds a match.
 */
G_MODULE_EXPORT gboolean interrogate_ecu(void)
{
	GAsyncQueue *queue = NULL;
	GHashTable *tests_hash = NULL;
	GArray *tests = NULL;
	Detection_Test *test = NULL;
	gint i = 0;
	gboolean interrogated = FALSE;
	GList *locations = NULL;
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;

	/* prevent multiple runs of interrogator simultaneously */
	g_static_mutex_lock(&mutex);
	dbg_func_f(INTERROGATOR,g_strdup("\n"__FILE__": interrogate_ecu() ENTERED\n\n"));

	/* ECU has already been detected via comms test
	   Now we need to figure out its variant and adapt to it
	 */
	/* Send stream disable command */
	stop_streaming();

	/* Load tests from tests.cfg file */
	if (!validate_and_load_tests(&tests,&tests_hash))
	{
		dbg_func_f(INTERROGATOR|CRITICAL,g_strdup(__FILE__": interrogate_ecu()\n\t validate_and_load_tests() didn't return a valid list of commands\n\t MegaTunix was NOT installed correctly, Aborting Interrogation\n"));
		update_logbar_f("interr_view",NULL,g_strdup(__FILE__": interrogate_ecu()\n\t validate_and_load_tests() didn't return a valid list of commands\n\t MegaTunix was NOT installed correctly, Aborting Interrogation\n"),FALSE,FALSE,TRUE);
		g_static_mutex_unlock(&mutex);
		return FALSE;
	}
	thread_widget_set_sensitive_f("offline_button",FALSE);
	thread_widget_set_sensitive_f("interrogate_button",FALSE);
	/* how many tests.... */

	for (i=0;i<tests->len;i++)
	{
		/* Get the test */
		test = g_array_index(tests,Detection_Test *, i);
		/* Check func existance and run it */
		if (test->function)
		{
			test->result = test->function(&test->num_bytes);
			if (test->result_type == RESULT_TEXT)
				test->result_str = g_strndup((const gchar *)(test->result),test->num_bytes);
		}
		else
			test->result = NULL;
	}
	interrogated = determine_ecu(tests,tests_hash);
	DATA_SET(global_data,"interrogated",GINT_TO_POINTER(interrogated));
	if (interrogated)
	{
		thread_widget_set_sensitive_f("interrogate_button",FALSE);
		thread_widget_set_sensitive_f("offline_button",FALSE);
	}

	g_array_free(tests,TRUE);
	g_hash_table_destroy(tests_hash);

	g_static_mutex_unlock(&mutex);
	return interrogated;
}
Example #17
0
/*!
 \brief process_vex_table() reads, processes and stores the Table data into
 the Vex_Import structure in preparation for import to the ECU.
 \param vex (stuct Vex_Import *) pointer to the Vex_Import structure
 \param string (gchar *) pointer to the current line of the VEXfile
 \param iochannel (GIOChannel *), Pointer to the iochannel representing the
 VEX file for reading more data
 \returns status of the operation (G_IO_STATUS_ERROR/G_IO_STATUS_NORMAL)
 */
G_MODULE_EXPORT GIOStatus process_vex_table(Vex_Import *vex, gchar * string, GIOChannel *iochannel)
{
	gint i = 0, j = 0;
	GString *a_line;
	GIOStatus status = G_IO_STATUS_ERROR;
	gchar ** str_array = NULL;
	gchar ** str_array2 = NULL;
	gchar * result = NULL;
	gchar *pos = NULL;
	gchar *numbers = NULL;
	gint value = 0;
	gint x_bins = 0;
	gint y_bins = 0;

	if (!string)
	{
		dbg_func_f(CRITICAL,g_strdup(__FILE__": process_vex_table()\n\t String passed was NULL\n"));
		return G_IO_STATUS_ERROR;
	}
	/* Get first number of [  x][  y] in the string line */
	str_array = g_strsplit(string, "[", 3);
	result = g_strdup(str_array[1]);	
	str_array2 = g_strsplit(result, "]", 2);
	result = g_strdup(str_array2[0]);	
	g_strfreev(str_array2);
	x_bins = atoi(result);

	/* Get first number of [  x][  y] in the string line */
	result = g_strdup(str_array[2]);
	g_strfreev(str_array);
	str_array2 = g_strsplit(result, "]", 2);
	result = g_strdup(str_array2[0]);
	g_strfreev(str_array2);
	y_bins = atoi(result);	

	vex->total_tbl_bins = x_bins*y_bins;
	vex->tbl_bins = g_new0(gint,(x_bins * y_bins));

	/* Need to skip down one line to the actual data.... */
	a_line = g_string_new("\0");
	status = g_io_channel_read_line_string(iochannel, a_line, NULL, NULL);
	if (status != G_IO_STATUS_NORMAL) 
	{
		g_string_free(a_line, TRUE);
		update_logbar_f("tools_view","warning",_("VEX Import: VE-Table I/O Read problem, file may be incomplete <---ERROR\n"),FALSE,FALSE,FALSE);
		return status;
	}
	g_string_free(a_line, TRUE);

	/* iterate over table */
	for (i=0; i<y_bins; i++) {
		a_line = g_string_new("\0");
		status = g_io_channel_read_line_string(iochannel, a_line, NULL, NULL);
		if (status != G_IO_STATUS_NORMAL) 
		{
			g_string_free(a_line, TRUE);
			update_logbar_f("tools_view","warning", _("VEX Import: VE-Table I/O Read problem, file may be incomplete <---ERROR\n"),FALSE,FALSE,FALSE);
			break;
		}
		pos = g_strrstr(a_line->str,"=\0");
		numbers = g_strdup(pos+sizeof(char));

		/* process a row */
		for (j=0; j<x_bins; j++) 
		{
			value = (int)strtol(numbers,&numbers,10);
			vex->tbl_bins[j+(i*x_bins)] = value;
		}		
		g_string_free(a_line, TRUE);
	}
	if (status == G_IO_STATUS_NORMAL)
	{
		vex->got_ve = TRUE;
		update_logbar_f("tools_view",NULL,_("VEX Import: VE-Table loaded successfully\n"),FALSE,FALSE,FALSE);
	}
	return status;
}
Example #18
0
/*!
 \brief feed_import_data_to_ecu() Forwards the data in the VEX file to the
 ECU.  NOTE this may have problems with firmware using multiple tables in
 a page, as the VEX format 1.0 does NOT handle that condition.
 \param vex (Vex_Import *) pointer to the Vex_Impot datastructure.
 */
G_MODULE_EXPORT void feed_import_data_to_ecu(Vex_Import *vex)
{
	gint i = 0;
	gchar *tmpbuf = NULL;
	guint8 **ecu_data = NULL;
	guint8 **ecu_data_backup = NULL;
	void *data = NULL;
	gchar * msgbuf = NULL;
	guchar *ptr = NULL;
	guint16 *ptr16 = NULL;
	guint32 *ptr32 = NULL;
	gint total = 0;
	gint canID = 0;
	gint page = -1;
	gint base = 0;
	DataSize size = 0;
	gint mult = 0;
	gint table = -1;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");

	ecu_data = firmware->ecu_data;
	ecu_data_backup = firmware->ecu_data_backup;

	/* Since we assume the page is where the table is this can cause
	 * major problems with some firmwares that use two tables inside
	 * of one page....
	 */
	page = vex->page;
	table = vex->table;
	if ((table < 0) || (table >= firmware->total_tables))
	{
		dbg_func_f(CRITICAL,g_strdup_printf(__FILE__": feed_import_data_to_ecu()\n\ttable passed (%i) is out of range(%i)\n",table,firmware->total_tables));
		return;
	}

	/* If dimensions do NOT match, ABORT!!! */
	if (firmware->table_params[table]->x_bincount != vex->total_x_bins)
	{
		msgbuf = g_strdup_printf(_("VEX Import: number of RPM bins inside VEXfile and FIRMWARE DO NOT MATCH (%i!=%i), aborting!!!\n"),firmware->table_params[table]->x_bincount,vex->total_x_bins);
		update_logbar_f("tools_view","warning",msgbuf,FALSE,FALSE,FALSE);
		dbg_func_f(CRITICAL,g_strdup(msgbuf));
		g_free(msgbuf);
		return;
	}
	if (firmware->table_params[table]->y_bincount != vex->total_y_bins)
	{
		msgbuf = g_strdup_printf(_("VEX Import: number of LOAD bins inside VEXfile and FIRMWARE DO NOT MATCH (%i!=%i), aborting!!!\n"),firmware->table_params[table]->y_bincount,vex->total_y_bins);
		update_logbar_f("tools_view","warning",msgbuf,FALSE,FALSE,FALSE);
		dbg_func_f(CRITICAL,g_strdup(msgbuf));
		g_free(msgbuf);
		return;
	}

	/* Backup the ALL pages of data first... */
	for (i=0;i<firmware->total_pages;i++)
	{
		if (!firmware->page_params[i]->dl_by_default)
			continue;
		memset((void *)ecu_data_backup[i], 0, firmware->page_params[i]->length);
		memcpy(ecu_data_backup[i], ecu_data[i],firmware->page_params[i]->length);
	}

	canID = firmware->canID;
	page = firmware->table_params[table]->x_page;
	base = firmware->table_params[table]->x_base;
	size = firmware->table_params[table]->x_size;
	mult = get_multiplier_f(size);
	if (firmware->chunk_support)
	{
		total = vex->total_x_bins;
		data = g_malloc0(mult*total);;
		if (mult == 1)
		{
			ptr = (guchar *)data;
			for (i=0;i<total;i++)
				ptr[i]=vex->x_bins[i];
		}
		if (mult == 2)
		{
			ptr16 = (guint16 *)data;
			for (i=0;i<total;i++)
			{
				if (firmware->bigendian)
					ptr16[i]=GINT16_TO_BE(vex->x_bins[i]);
				else
					ptr16[i]=GINT16_TO_LE(vex->x_bins[i]);
			}
		}
		if (mult == 4)
		{
			ptr32 = (guint32 *)data;
			for (i=0;i<total;i++)
			{
				if (firmware->bigendian)
					ptr32[i]=GINT_TO_BE(vex->x_bins[i]);
				else
					ptr32[i]=GINT_TO_LE(vex->x_bins[i]);
			}
		}
		ms_chunk_write(canID,page,base,(total*mult),data);
	}
	else
	{
		for (i=0;i<vex->total_x_bins;i++)
		{
			if (vex->x_bins[i] != ms_get_ecu_data_last(canID,page,base+(i*mult),size))
				ms_send_to_ecu(canID,page,base+i,size, vex->x_bins[i], TRUE);
		}
	}

	canID = firmware->canID;
	page = firmware->table_params[table]->y_page;
	base = firmware->table_params[table]->y_base;
	size = firmware->table_params[table]->y_size;
	mult = get_multiplier_f(size);
	if (firmware->chunk_support)
	{
		total = vex->total_y_bins;
		data = g_malloc0(mult*total);
		if (mult == 1)
		{
			ptr = (guchar *)data;
			for (i=0;i<total;i++)
				ptr[i]=vex->y_bins[i];
		}
		if (mult == 2)
		{
			ptr16 = (guint16 *)data;
			for (i=0;i<total;i++)
			{
				if (firmware->bigendian)
					ptr16[i]=GINT16_TO_BE(vex->y_bins[i]);
				else
					ptr16[i]=GINT16_TO_LE(vex->y_bins[i]);
			}
		}
		if (mult == 4)
		{
			ptr32 = (guint32 *)data;
			for (i=0;i<total;i++)
			{
				if (firmware->bigendian)
					ptr32[i]=GINT_TO_BE(vex->y_bins[i]);
				else
					ptr32[i]=GINT_TO_LE(vex->y_bins[i]);
			}
		}
		ms_chunk_write(canID,page,base,(total*mult),data);
	}
	else
	{
		for (i=0;i<vex->total_y_bins;i++)
		{
			if (vex->y_bins[i] != ms_get_ecu_data_last(canID,page,base+i,size))
				ms_send_to_ecu(canID,page,base+(i*mult),size,vex->y_bins[i], TRUE);
		}
	}

	canID = firmware->canID;
	page = firmware->table_params[table]->z_page;
	base = firmware->table_params[table]->z_base;
	size = firmware->table_params[table]->z_size;
	mult = get_multiplier_f(size);
	if (firmware->chunk_support)
	{
		total = (vex->total_y_bins)*(vex->total_x_bins);
		data = g_malloc0(mult*total);
		if (mult == 1)
		{
			ptr = (guchar *)data;
			for (i=0;i<total;i++)
				ptr[i]=vex->tbl_bins[i];
		}
		if (mult == 2)
		{
			ptr16 = (guint16 *)data;
			for (i=0;i<total;i++)
			{
				if (firmware->bigendian)
					ptr16[i]=GINT16_TO_BE(vex->tbl_bins[i]);
				else
					ptr16[i]=GINT16_TO_LE(vex->tbl_bins[i]);
			}
		}
		if (mult == 4)
		{
			ptr32 = (guint32 *)data;
			for (i=0;i<total;i++)
			{
				if (firmware->bigendian)
					ptr32[i]=GINT_TO_BE(vex->tbl_bins[i]);
				else
					ptr32[i]=GINT_TO_LE(vex->tbl_bins[i]);
			}
		}
		ms_chunk_write(canID,page,base,(total*mult),data);
	}
	else
	{
		for (i=0;i<((vex->total_y_bins)*(vex->total_x_bins));i++)
		{
			if (vex->tbl_bins[i] != ms_get_ecu_data_last(canID,page,base+i,size))
				ms_send_to_ecu(canID,page,base+(i*mult),size,vex->tbl_bins[i], TRUE);
		}
	}
	io_cmd_f(firmware->burn_all_command,NULL);

	tmpbuf = g_strdup_printf(_("VEX Import: VEtable on page %i updated with data from the VEX file\n"),vex->page);
	update_logbar_f("tools_view",NULL,tmpbuf,FALSE,FALSE,FALSE);
	g_free(tmpbuf);
}
Example #19
0
/*!
 \brief single_table_export() is the export function to dump one Tables
 to a "vex" file.  
 \param iochannel a pointer to the output channel 
 \param table_num, integer number of table to export
 \see single_table_import
 \returns TRUE on success, FALSE on failure
 */
G_MODULE_EXPORT void single_table_export(GIOChannel *iochannel, gint table_num)
{
	struct tm *tm = NULL;
	time_t *t = NULL;
	gint i = 0;
	gint j = 0;
	gint table = -1;
	gsize count = 0;
	gint index = 0;
	gint x_page = 0;
	gint y_page = 0;
	gint z_page = 0;
	gint x_base = 0;
	gint y_base = 0;
	gint z_base = 0;
	DataSize x_size = 0;
	DataSize y_size = 0;
	DataSize z_size = 0;
	gint x_mult = 0;
	gint y_mult = 0;
	gint z_mult = 0;
	gint x_bincount = 0;
	gint y_bincount = 0;
	GIOStatus status;
	GString *output = NULL;
	gint canID = 0;
	Firmware_Details *firmware = NULL;

	firmware = DATA_GET(global_data,"firmware");
	canID = firmware->canID;


	/* For Page 0.... */
	output = g_string_sized_new(64); /*pre-allocate 64 chars */
	/*printf("total tables %i\n",firmware->total_tables);*/
	table = table_num;
	x_page = firmware->table_params[table]->x_page;
	y_page = firmware->table_params[table]->y_page;
	z_page = firmware->table_params[table]->z_page;
	x_base = firmware->table_params[table]->x_base;
	y_base = firmware->table_params[table]->y_base;
	z_base = firmware->table_params[table]->z_base;
	x_size = firmware->table_params[table]->x_size;
	y_size = firmware->table_params[table]->y_size;
	z_size = firmware->table_params[table]->z_size;
	x_mult = get_multiplier_f(x_size);
	y_mult = get_multiplier_f(y_size);
	z_mult = get_multiplier_f(z_size);
	x_bincount = firmware->table_params[table]->x_bincount;
	y_bincount = firmware->table_params[table]->y_bincount;

	t = g_malloc(sizeof(time_t));
	time(t);
	tm = localtime(t);
	g_free(t);
	if (vex_comment == NULL)
		vex_comment = g_strdup("No comment given");

	g_string_append_printf(output, "EVEME 1.0\n");
	g_string_append_printf(output, "UserRev: 1.00\n");
	g_string_append_printf(output, "UserComment: Table %i; (%s) %s\n",table,firmware->table_params[table]->table_name,vex_comment);
	g_string_append_printf(output, "Date: %i-%.2i-%i\n",1+(tm->tm_mon),tm->tm_mday,1900+(tm->tm_year));

	g_string_append_printf(output, "Time: %.2i:%.2i\n",tm->tm_hour,tm->tm_min);
	g_string_append_printf(output, "Page %i\n",z_page);
	g_string_append_printf(output, "VE Table RPM Range              [%2i]\n",x_bincount);

	for (i=0;i<x_bincount;i++)
		g_string_append_printf(output,"   [%3d] = %3d\n",i,ms_get_ecu_data(canID,x_page,x_base+(i*x_mult),x_size));

	g_string_append_printf(output, "VE Table Load Range (MAP)       [%2i]\n",y_bincount);
	for (i=0;i<y_bincount;i++)
		g_string_append_printf(output, "   [%3d] = %3d\n",i,ms_get_ecu_data(canID,y_page,y_base+(i*y_mult),y_size));

	g_string_append_printf(output, "VE Table                        [%3i][%3i]\n",x_bincount,y_bincount);
	g_string_append_printf(output, "           ");
	for (i=0;i<x_bincount;i++)
	{
		g_string_append_printf(output, "[%3d]",i);
		if (i < (x_bincount-1))
			g_string_append_printf(output, " ");
	}
	g_string_append_printf(output, "\n");
	index = 0;
	for (i=0;i<x_bincount;i++)
	{
		g_string_append_printf(output, "   [%3d] =",i);
		for (j=0;j<y_bincount;j++)
		{
			if (j == 0)
				g_string_append_printf (output, "  %3d",ms_get_ecu_data(canID,z_page,z_base+(index*z_mult),z_size));
			else
				g_string_append_printf (output, "   %3d",ms_get_ecu_data(canID,z_page,z_base+(index*z_mult),z_size));
			index++;
		}
		g_string_append_printf(output,"\n");
	}
	status = g_io_channel_write_chars(
			iochannel,output->str,output->len,&count,NULL);
	if (status != G_IO_STATUS_NORMAL)
	{
		dbg_func_f(CRITICAL,g_strdup(__FILE__": all_table_export()\n\tError exporting VEX file\n"));
	}
	g_string_free(output,TRUE);

	update_logbar_f("tools_view",NULL,_("VE-Table(s) Exported Successfully\n"),FALSE,FALSE,FALSE);

	if (vex_comment)
		g_free(vex_comment);
	vex_comment = NULL;
	return; /* return TRUE on success, FALSE on failure */
}