1
0
mirror of https://gitee.com/HEYAHONG/Programer_discarded.git synced 2025-06-06 09:29:13 +08:00
Programer_discarded/Data_Programer/STM32_Data_ProgramerDlg.cpp
2019-11-21 16:32:17 +08:00

616 lines
22 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

///-----------------------------------------------------------------
///
/// @file STM32_Data_ProgramerDlg.cpp
/// @author 부饑븐
/// Created: 2019-11-18 13:18:06
/// @section DESCRIPTION
/// STM32_Data_ProgramerDlg class implementation
///
///------------------------------------------------------------------
#include "STM32_Data_ProgramerDlg.h"
#include "FilePassWord.h"
//Do not add custom headers
//wxDev-C++ designer will remove them
////Header Include Start
////Header Include End
//----------------------------------------------------------------------------
// STM32_Data_ProgramerDlg
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(STM32_Data_ProgramerDlg,wxDialog)
////Manual Code Start
////Manual Code End
EVT_CLOSE(STM32_Data_ProgramerDlg::OnClose)
EVT_SET_FOCUS(STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgSetFocus)
EVT_ENTER_WINDOW(STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgEnterWindow)
EVT_INIT_DIALOG(STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgInitDialog)
EVT_ACTIVATE(STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgActivate)
EVT_BUTTON(ID_WXBUTTON3,STM32_Data_ProgramerDlg::WxButton3Click)
EVT_BUTTON(ID_WXBUTTON2,STM32_Data_ProgramerDlg::WxButton2Click)
EVT_TIMER(ID_WXTIMER1,STM32_Data_ProgramerDlg::WxTimer1Timer)
EVT_TEXT(ID_WXEDIT1,STM32_Data_ProgramerDlg::WxEdit1Updated)
EVT_BUTTON(ID_WXBUTTON1,STM32_Data_ProgramerDlg::WxButton1Click)
END_EVENT_TABLE()
////Event Table End
STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlg(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxDialog(parent, id, title, position, size, style)
{
hex_data=NULL;//립놓令
{//쉥눗왯寧濾뒈囹렴흙뻔쓱긴좆
uint64_t Dialog_Ptr=(uint64_t)this;
char env[200];
memset(env,0,sizeof(env));
sprintf(env,"Dialog_Ptr=%016X",Dialog_Ptr);
putenv(env);
}
CreateGUIControls();
}
STM32_Data_ProgramerDlg::~STM32_Data_ProgramerDlg()
{
}
void STM32_Data_ProgramerDlg::CreateGUIControls()
{
//Do not add custom code between
//GUI Items Creation Start and GUI Items Creation End.
//wxDev-C++ designer will remove them.
//Add the custom code before or after the blocks
////GUI Items Creation Start
WxButton3 = new wxButton(this, ID_WXBUTTON3, _("<EFBFBD>"), wxPoint(242, 70), wxSize(101, 29), 0, wxDefaultValidator, _("WxButton3"));
WxCheckBox2 = new wxCheckBox(this, ID_WXCHECKBOX2, _("琦뻔<EFBFBD>"), wxPoint(158, 77), wxSize(73, 16), 0, wxDefaultValidator, _("WxCheckBox2"));
WxCheckBox1 = new wxCheckBox(this, ID_WXCHECKBOX1, _("뫘劤hex"), wxPoint(71, 77), wxSize(73, 16), 0, wxDefaultValidator, _("WxCheckBox1"));
WxCheckBox1->SetValue(true);
WxStaticLine2 = new wxStaticLine(this, ID_WXSTATICLINE2, wxPoint(0, 106), wxSize(414, -1), wxLI_HORIZONTAL);
WxStaticLine2->SetForegroundColour(wxColour(0,0,0));
WxStaticLine1 = new wxStaticLine(this, ID_WXSTATICLINE1, wxPoint(1, 65), wxSize(414, -1), wxLI_HORIZONTAL);
WxStaticLine1->SetForegroundColour(wxColour(0,0,0));
WxButton2 = new wxButton(this, ID_WXBUTTON2, _("鎧乞꿨숭"), wxPoint(353, 33), wxSize(64, 24), 0, wxDefaultValidator, _("WxButton2"));
WxStaticText3 = new wxStaticText(this, ID_WXSTATICTEXT3, _("<EFBFBD>쩌꿨숭"), wxPoint(185, 35), wxDefaultSize, 0, _("WxStaticText3"));
WxStaticText2 = new wxStaticText(this, ID_WXSTATICTEXT2, _("뫘劤꿨숭:"), wxPoint(2, 35), wxDefaultSize, 0, _("WxStaticText2"));
wxArrayString arrayStringFor_WxComboBox2;
WxComboBox2 = new wxComboBox(this, ID_WXCOMBOBOX2, _(""), wxPoint(242, 33), wxSize(107, 25), arrayStringFor_WxComboBox2, wxTE_READONLY, wxDefaultValidator, _("WxComboBox2"));
wxArrayString arrayStringFor_WxComboBox1;
WxComboBox1 = new wxComboBox(this, ID_WXCOMBOBOX1, _(""), wxPoint(60, 33), wxSize(121, 25), arrayStringFor_WxComboBox1, wxTE_READONLY, wxDefaultValidator, _("WxComboBox1"));
WxTimer1 = new wxTimer();
WxTimer1->SetOwner(this, ID_WXTIMER1);
WxTimer1->Start(300);
WxEdit1 = new wxTextCtrl(this, ID_WXEDIT1, _("data/"), wxPoint(69, 2), wxSize(272, 26), wxTE_LEFT, wxDefaultValidator, _("WxEdit1"));
WxStaticText1 = new wxStaticText(this, ID_WXSTATICTEXT1, _("鑒앴커쩌:"), wxPoint(5, 6), wxDefaultSize, 0, _("WxStaticText1"));
WxButton1 = new wxButton(this, ID_WXBUTTON1, _("댔역"), wxPoint(348, 1), wxSize(70, 26), 0, wxDefaultValidator, _("WxButton1"));
Info = new wxTextCtrl(this, ID_WXEDIT1_Info, _(""), wxPoint(2, 116), wxSize(415, 172), wxTE_READONLY | wxTE_LEFT | wxTE_MULTILINE, wxDefaultValidator, _("Info"));
SetTitle(_("STM32_Data_Programer"));
SetIcon(wxNullIcon);
SetSize(8,8,436,330);
Center();
////GUI Items Creation End
}
void STM32_Data_ProgramerDlg::OnClose(wxCloseEvent& /*event*/)
{
WxTimer1->Stop();
Destroy();
}
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "dir.h"
#include "dirent.h"
#include "unistd.h"
#ifdef WIN32
#include "windows.h"
#endif
void STM32_Data_ProgramerDlg::Data_Init()
{
//DataDir="data/";
while(access(((std::string)(DataDir+"/config.cfg").c_str()).c_str(),R_OK) )
{
Info->AppendText("覩鑒앴커쩌꼇닸瞳config.cfg\n");
wxDirDialog dlg(this,"朞嶝鑒앴닸렴커쩌","data",wxDD_DIR_MUST_EXIST|wxDD_DEFAULT_STYLE);
if(dlg.ShowModal()==wxID_OK)
{
DataDir=dlg.GetPath();
{//뫘劤鑒앴커쩌鞫刻움
wxString Temp=DataDir;
WxEdit1->Clear();
WxEdit1->AppendText(Temp);
}
Info->AppendText((wxString)"錦맣鑒앴커쩌槨:"+DataDir+"\n");
}
}
{//쉥DataDir렴흙뻔쓱긴좆
char buff[8192];
sprintf(buff,"DataDir=%s",((std::string)DataDir.c_str()).c_str());
putenv(buff);
}
{//쉥config돨코휭賈痰putenv警속돕뻔쓱긴좆
FILE *fp=NULL;
char buff[8192];
memset(buff,0,sizeof(buff));
fp=fopen(((std::string)(DataDir+"/config.cfg").c_str()).c_str(),"r");
if(fp==NULL)
{
Info->AppendText("config.cfg譚黨灌列覩凜轟랬댔역\n");
return;
}
while(NULL != fgets(buff,sizeof(buff),fp))
{
if((buff[0]=='#') || (buff[0]==';') || (buff[0]=='/'))
continue;//契법鬧姦契
for(size_t i=0;i<strlen(buff);i++)
{//뇜혼써棺돨쀼났륜
if(buff[i]=='\n' || buff[i] == '\r')
buff[i]=0;
}
if(strstr(buff,"=")!= NULL)
if(putenv(buff))//쉥벵唐된뵀돨契警속돕뻔쓱긴좆
{
Info->AppendText("警속뻔쓱긴좆呵겨");
}
// //꿎桿뗍혤코휭
// Info->AppendText(buff);
memset(buff,0,sizeof(buff));
}
fclose(fp);
}
{
wxString data=getenv("data");
wxString sub="hex";
if(!(data.Mid(data.Len()-3)).CmpNoCase(sub))
{
{//댔역hex匡숭
FILE *fp=fopen(((std::string) (DataDir+"/"+data).c_str()).c_str(),"rb");
if(fp == NULL)
{
Info->AppendText("hex匡숭꼇닸瞳!\n");
return;
}
if(hex_data!=NULL)
{
free(hex_data);
hex_data=NULL;
}
fseek(fp,0,SEEK_END);
size_t len = ftell(fp);
fseek(fp,0,SEEK_SET);
uint8_t * data=(uint8_t *)malloc(len+50);
memset(data,0,len+50);
{
size_t pos=0,read_num=0;
while(len-pos>=1)
{
read_num=fread(data+pos,1,32,fp);
pos+=read_num;
};
}
if(strlen((const char *)data)>=len)
{
hex_data=data;
}
fclose(fp);
}
{
char buff[100];
memset(buff,0,sizeof(buff));
sprintf(buff,"%d",strlen((const char *)hex_data));
Info->AppendText((wxString)"쒸멩:鑒앴匡숭"+data+"(낀똑:"+buff+")灌속쵱!\n");
}
}
else
{//쵱쯤움
FilePassWord dlg(this);
if(dlg.ShowModal()!= wxID_OK)
{
Info->AppendText("쒸멩:쵱쯤駱聯놔君댄轎,옵콘矜狼路劤<E8B7AF>零鑒앴커쩌\n");
dlg.Destroy();
}
else
{//쵱쯤駱聯냥묘,쉥匡숭속潼逞코닸
dlg.Destroy();
char *password=getenv("data_password");
uint8_t *data=NULL;
if(password==NULL)
{
Info->AppendText("코꼬댄轎,冷꼇돕쵱쯤淃\n");
return;
}
uint8_t aes_in[32],aes_out[32],aes_key[32];
memset(aes_key,0,sizeof(aes_key));
strcpy((char *)aes_key,password);
{
//uint8_t *w=aes_init(32);
//aes_key_expansion(aes_key,w);
struct aes256_ctx ctx;
aes256_set_decrypt_key(&ctx,aes_key);
{
FILE *fp=fopen(((std::string)(DataDir+"/"+getenv("data")).c_str()).c_str(),"rb");
if(fp != NULL)
{
fseek(fp,0,SEEK_END);
size_t size=ftell(fp),pos=0;
fseek(fp,0,SEEK_SET);
//fseek(fp,32,SEEK_SET);//契법匡숭庫
fread(aes_out,32,1,fp);//契법匡숭역庫
if((size%32)!=0)
{
Info->AppendText("匡숭옵콘닸瞳댄轎!\n");
}
data=(uint8_t *)malloc(size);
memset(data,0,sizeof(data));
do
{
memset(aes_in,0,sizeof(aes_in));
memset(aes_out,0,sizeof(aes_out));
size_t read_num=fread(aes_in,1,32,fp);
if(read_num != 32)
{
Info->AppendText("뗍혤댄轎\n");
}
//aes_key_expansion(aes_key,w);
//aes_inv_cipher(aes_in,aes_out,w);
aes256_decrypt(&ctx,32,aes_out,aes_in);
//Info->AppendText(aes_out);
memcpy((char *)data+pos,(char *)aes_out,32);
pos+=32;
} while(size-pos>32);
fclose(fp);
{
if(hex_data != NULL)
{
free(hex_data);
}
hex_data=data;
{
char buff[100];
memset(buff,0,sizeof(buff));
sprintf(buff,"%d",strlen((char *)hex_data));
//Info->AppendText(hex_data);
Info->AppendText((wxString)"속쵱鑒앴hex댕鬼:"+buff+"B\n");
}
}
}
}
// free(w);
}
};
}
}
{//꿎桿뻔쓱긴좆
#ifdef WIN32
//WinExec("cmd.exe",SW_SHOW);
#endif
}
}
/*
* STM32_Data_ProgramerDlgEnterWindow
*/
void STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgEnterWindow(wxMouseEvent& event)
{
// insert your code here
}
/*
* STM32_Data_ProgramerDlgInitDialog
*/
void STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgInitDialog(wxInitDialogEvent& event)
{
// insert your code here
//Data_Init();
}
/*
* WxButton1Click
*/
void STM32_Data_ProgramerDlg::WxButton1Click(wxCommandEvent& event)
{
// insert your code here
Data_Init();
}
/*
* STM32_Data_ProgramerDlgSetFocus
*/
void STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgSetFocus(wxFocusEvent& event)
{
// insert your code here
}
/*
* STM32_Data_ProgramerDlgActivate
*/
void STM32_Data_ProgramerDlg::STM32_Data_ProgramerDlgActivate(wxActivateEvent& event)
{
// insert your code here
}
/*
* WxTimer1Timer
*/
void STM32_Data_ProgramerDlg::WxTimer1Timer(wxTimerEvent& event)
{
// insert your code here
{
static bool Is_Init=false;
if(!Is_Init)
{
Is_Init=true;
WxTimer1->Stop();
WxTimer1->Start(3000);
WxEdit1->Clear();
WxEdit1->AppendText("data/");
Data_Init();
search_plugin();
}
}
{
static bool Is_Running=false;
if(!Is_Running)//꼇獵契路릿<E8B7AF>
if(WxCheckBox2->IsChecked() && WxButton3->IsEnabled())//琦뻔<E790A6>畇굳朞櫓
{
Is_Running=true;
WxButton3->Disable();
start_flash();
Is_Running=false;
}
}
}
/*
* WxEdit1Updated
*/
void STM32_Data_ProgramerDlg::WxEdit1Updated(wxCommandEvent& event)
{
// insert your code here
DataDir=WxEdit1->GetValue();
}
#include "unistd.h"
#include "dirent.h"
void STM32_Data_ProgramerDlg::search_plugin()
{//鎧乞plugin커쩌잚謹槨1돨렴흙WxComboBox1잚謹槨2돨렴흙WxComboBox2
if(access("plugin/",4))
{
Info->AppendText("寧땍꿨숭커쩌꼇닸瞳!\n");
return;
}
{//깁저커쩌
DIR *dir=NULL;
if((dir=opendir("plugin/"))==NULL)
{
Info->AppendText("댔역꿨숭커쩌呵겨!\n");
return;
}
struct dirent * file=NULL;
{//헌왕왠숭<EC99A0>覩唐돨코휭
WxComboBox1->Clear();
WxComboBox2->Clear();
}
while((file=readdir(dir))!=NULL)
{
//if(file->d_type==DT_REG)//攣끽匡숭
{
char path[8192];
memset(path,0,sizeof(path));
sprintf(path,"plugin/%s",file->d_name);
void *dll=dlopen(path,RTLD_GLOBAL);
if(dll==NULL)
{
continue;
}
get_plugin_type_t get_plugin_type=(get_plugin_type_t)dlsym(dll,"get_plugin_type");
if(dlerror()==NULL)
{
unsigned int type=(*get_plugin_type)();
if(type==1)
{
WxComboBox1->Insert(file->d_name,0);
WxComboBox1->Select(0);
};
if(type==2)
{
WxComboBox2->Insert(file->d_name,0);
WxComboBox2->Select(0);
};
}
dlclose(dll);
}
}
closedir(dir);
}
}
/*
* WxButton2Click
*/
void STM32_Data_ProgramerDlg::WxButton2Click(wxCommandEvent& event)
{
// insert your code here
search_plugin();
}
void STM32_Data_ProgramerDlg::start_flash()
{
// insert your code here
process_hex_t process_hex=NULL;
process_program_t process_program=NULL;
void * dll_hex=NULL;
void * dll_program=NULL;
WxButton3->Disable();
WxButton3->SetBackgroundColour(wxNullColour);
Info->Clear();
Info->AppendText("역迦<EFBFBD>\n");
{
Info->AppendText("쇱꿴hex鑒앴\n");
if((hex_data==NULL) || (strlen((char *)hex_data) ==0) )
{
Info->AppendText("hex鑒앴꼇닸瞳\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto EndOfStart_Flash;
}
size_t pos;
for(pos=0;pos<strlen((char *)hex_data);pos++)
{
if(!((hex_data[pos] >= 'A' && hex_data[pos] <='F')||
(hex_data[pos] >= 'a' && hex_data[pos] <='f')||
(hex_data[pos] >= '0' && hex_data[pos] <='9')||
(hex_data[pos] == ':' || hex_data[pos]=='\n' ||
hex_data[pos] =='\r') ))
{//쇱꿎돕렷랬俚륜
Info->AppendText("hex鑒앴櫓쇱꿎돕렷랬俚륜\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto EndOfStart_Flash;
}
}
{
char buff[8192];
sprintf(buff,"hex鑒앴낀똑:%d\n",strlen((char *)hex_data));
Info->AppendText(buff);
}
}
//속潼꿨숭
if(WxCheckBox1->IsChecked())
{//속潼뫘劤hex꿨숭
char path[8192];
sprintf(path,"plugin/%s",((std::string)WxComboBox1->GetValue().c_str()).c_str());
if((dll_hex=dlopen(path,RTLD_GLOBAL))==NULL)
{
Info->AppendText("댔역뫘劤꿨숭呵겨!\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto EndOfStart_Flash;
}
process_hex=(process_hex_t)dlsym(dll_hex,"process_hex");
if(dlerror()!=NULL)
{
dlclose(dll_hex);
Info->AppendText("속潼뫘劤꿨숭呵겨!\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto EndOfStart_Flash;
}
Info->AppendText("뫘劤꿨숭속潼냥묘!\n");
}
{//속潼<EC868D>쩌꿨숭
char path[8192];
sprintf(path,"plugin/%s",((std::string)WxComboBox2->GetValue().c_str()).c_str());
if((dll_hex=dlopen(path,RTLD_GLOBAL))==NULL)
{
Info->AppendText("댔역<EFBFBD>쩌꿨숭呵겨\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto EndOfStart_Flash;
}
process_program=(process_program_t)dlsym(dll_hex,"process_program");
if(dlerror()!=NULL)
{
dlclose(dll_hex);
Info->AppendText("속潼<EFBFBD>쩌꿨숭呵겨\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto EndOfStart_Flash;
}
Info->AppendText("<EFBFBD>쩌꿨숭속潼냥묘\n");
}
{//獵契뫘劤
if(WxCheckBox1->IsChecked())
{
Info->AppendText("역迦뫘劤\n");
if(process_hex(hex_data)<=0)
{
Info->AppendText("뫘劤呵겨\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto AbortOp;
}
}
}
{//獵契<E78DB5>
Info->AppendText("역迦<EFBFBD>\n");
if(process_program(hex_data)<=0)
{
Info->AppendText("<EFBFBD>쩌呵겨\n");
WxButton3->SetBackgroundColour(wxColor(255,0,0));
goto AbortOp;
}
}
WxButton3->SetBackgroundColour(wxColor(0,255,0));
Info->AppendText("꾸鱗供냥!\n");
AbortOp:
//菌潼꿨숭
if(dll_hex != NULL)
dlclose(dll_hex);
if(dll_program!=NULL)
dlclose(dll_program);
EndOfStart_Flash:
WxButton3->Enable();
}
/*
* WxButton3Click
*/
void STM32_Data_ProgramerDlg::WxButton3Click(wxCommandEvent& event)
{
// insert your code here
start_flash();
}
wxTextCtrl * STM32_Data_ProgramerDlg::Get_Info_Ctl()
{
return Info;
}