forked from TomKing062/spreadtrum_flash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBMPlatform.cpp
More file actions
122 lines (98 loc) · 2.91 KB
/
Copy pathBMPlatform.cpp
File metadata and controls
122 lines (98 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "BMPlatform.h"
#include <iostream>
extern "C" { extern int m_bOpened; }
CBMPlatformApp::CBMPlatformApp() {
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_pfCreateChannel = NULL;
m_pfReleaseChannel = NULL;
m_hChannelLib = NULL;
}
BOOL CBMPlatformApp::InitInstance() {
m_hChannelLib = LoadLibrary(_T("Channel9.dll"));
if (m_hChannelLib == NULL) {
std::cout << "Failed to load Channel9.dll. Error code: " << GetLastError() << std::endl;
return FALSE;
}
m_pfCreateChannel = (pfCreateChannel)GetProcAddress(m_hChannelLib, "CreateChannel");
m_pfReleaseChannel = (pfReleaseChannel)GetProcAddress(m_hChannelLib, "ReleaseChannel");
if (m_pfCreateChannel == NULL || m_pfReleaseChannel == NULL) {
return FALSE;
}
return TRUE;
}
int CBMPlatformApp::ExitInstance() {
// TODO: Add your specialized code here and/or call the base class
if (m_hChannelLib) {
FreeLibrary(m_hChannelLib);
m_hChannelLib = NULL;
//m_pfCreateChannel = NULL;
//m_pfReleaseChannel = NULL;
}
return TRUE;
}
CBMPlatformApp g_theApp;
CBootModeOpr::CBootModeOpr() {
m_pChannel = NULL;
g_theApp.InitInstance();
}
CBootModeOpr::~CBootModeOpr() {
g_theApp.ExitInstance();
}
BOOL CBootModeOpr::Initialize() {
if (!g_theApp.m_pfCreateChannel) {
return FALSE;
}
if (!g_theApp.m_pfCreateChannel((ICommChannel **)&m_pChannel, CHANNEL_TYPE_COM)) {
return FALSE;
}
return TRUE;
}
void CBootModeOpr::Uninitialize() {
if (g_theApp.m_pfReleaseChannel) g_theApp.m_pfReleaseChannel(m_pChannel);
m_pChannel = NULL;
}
int CBootModeOpr::Read(UCHAR *m_RecvData, int max_len, int dwTimeout) {
ULONGLONG tBegin;
ULONGLONG tCur;
tBegin = GetTickCount64();
do {
tCur = GetTickCount64();
DWORD dwRead = m_pChannel->Read(m_RecvData, max_len, dwTimeout);
if (dwRead) {
return dwRead;
}
} while ((tCur - tBegin) < dwTimeout);
return 0;
}
int CBootModeOpr::Write(UCHAR *lpData, int iDataSize) {
return m_pChannel->Write(lpData, iDataSize);
}
BOOL CBootModeOpr::GetProperty(LONG lFlags, DWORD dwPropertyID, LPVOID pValue) {
return m_pChannel->GetProperty(lFlags, dwPropertyID, pValue);
}
BOOL CBootModeOpr::SetProperty(LONG lFlags, DWORD dwPropertyID, LPCVOID pValue) {
return m_pChannel->SetProperty(lFlags, dwPropertyID, pValue);
}
BOOL CBootModeOpr::ConnectChannel(DWORD dwPort, ULONG ulMsgId, DWORD Receiver) {
if (!dwPort) return FALSE;
if (Receiver) m_pChannel->SetReceiver(ulMsgId, TRUE, (LPVOID)Receiver);
CHANNEL_ATTRIBUTE ca;
ca.ChannelType = CHANNEL_TYPE_COM;
ca.Com.dwPortNum = dwPort;
ca.Com.dwBaudRate = 115200;
m_bOpened = m_pChannel->Open(&ca);
if (m_bOpened) std::cout << "Successfully connected to port: " << dwPort << std::endl;
return m_bOpened;
}
BOOL CBootModeOpr::DisconnectChannel() {
m_pChannel->Close();
m_bOpened = 0;
return TRUE;
}
void CBootModeOpr::Clear() {
m_pChannel->Clear();
}
void CBootModeOpr::FreeMem(LPVOID pMemBlock) {
m_pChannel->FreeMem(pMemBlock);
}