#include <iostream>
//#include <string>
#include <lusb0_usb.h>
//#pragma comment(linker, “/subsystem:\”windows\” /entry:\”mainCRTStartup\””)
#define CMD_GET_DATETIME 0x01
#define CMD_SET_EMPID 0x02
#define CMD_SET_TRNID 0x03
#define CMD_SET_HNDID 0x04
#define CMD_SET_CLSID 0x05
#define CMD_SET_PCMSC 0x06
#define CMD_SET_WGUID 0x07
void OnListDevice();
bool OnOpenDevice();
usb_dev_handle *OpenDevice(int vid, int pid);
bool SimpleClassSendMessage(char* buf, int bytes_to_send);
bool SimpleClassReceiveMessage(char* buf, int bytes_received);
void SimpleClassCleanUp();
bool CheckMachineTime();
void WaitForRemove();
bool WaitForContentTXT();
int main()
{
int dir_err;
//OnListDevice();
while (1) {
if (OnOpenDevice() == true) {
//dir_err = _wremove(L”D:\\amba_tmp\\cmd.txt”);
//dir_err = _wremove(L”D:\\amba_tmp\\content.txt”);
dir_err = _wrmdir(L”D:\\amba_tmp”);
dir_err = _wmkdir(L”D:\\amba_tmp”);
}
else
continue;
if (CheckMachineTime() == false) {//not only the time ,but also received &send fail,will trigger “wait for remove the machine!”
WaitForRemove();
}
else {
WaitForContentTXT();
WaitForRemove();
}
SimpleClassCleanUp();
}
//wsprintf(str, L”Test Tread for address 0x%X exit.\r\n”, 1);
//std::wcout << str;
}
void OnListDevice()
{
struct usb_bus* bus;
struct usb_device* dev;
wchar_t str[1024];
int count = 0;
usb_init();
usb_find_busses();
usb_find_devices();
std::cout << “Device List:\r\n”;
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
wsprintf(str, L”[%02d] VID: 0x%X, PID: 0x%X\r\n”,
count,
dev->descriptor.idVendor,
dev->descriptor.idProduct);
std::wcout << str;
wsprintf(str, L”bNumInterfaces:%d\r\n”, dev->config->bNumInterfaces);
std::wcout << str;
wsprintf(str, L”bmAttribute: :0x%X\r\n”, dev->config->bmAttributes);
std::wcout << str;
wsprintf(str, L”MaxPower :0x%X\r\n”, dev->config->MaxPower);
std::wcout << str;
wsprintf(str, L”bNumInterface :%d\r\n”, dev->config->bNumInterfaces);
std::wcout << str;
for (int ifnum = 0; ifnum < dev->config->bNumInterfaces; ifnum++) {
usb_interface* interface = &dev->config->interface[ifnum];
wsprintf(str, L” Interface %d\r\n”, ifnum);
std::wcout << str;
wsprintf(str, L” AlternateSettings: %d\r\n”, interface->num_altsetting);
std::wcout << str;
for (int altnum = 0; altnum < interface->num_altsetting; altnum++) {
usb_interface_descriptor* ifd = &interface->altsetting[altnum];
int bNumEndpoints = ifd->bNumEndpoints;
wsprintf(str, L” AlternateSetting[%d]\r\n”, altnum);
std::wcout << str;
wsprintf(str, L” bNumEndpoints:%d\r\n”, bNumEndpoints);
std::wcout << str;
for (int epnum = 0; epnum < bNumEndpoints; epnum++) {
usb_endpoint_descriptor* epd = &ifd->endpoint[epnum];
int wMaxPacketSize = epd->wMaxPacketSize;
wsprintf(str, L” Endpoint[%d]:\r\n”, epnum);
std::wcout << str;
wsprintf(str, L” bEndpointAddress:0x%X\r\n”, epd->bEndpointAddress);
std::wcout << str;
wsprintf(str, L” wMaxPacketSize:0x%X(%d)\r\n”, wMaxPacketSize, wMaxPacketSize);
std::wcout << str;
wsprintf(str, L” bmAttributes:0x%X\r\n”, epd->bmAttributes);
std::wcout << str;
}
}
count++;
}
}
}
}
usb_dev_handle *m_DeviceHandle;
unsigned int usb_in_endpoint1;
unsigned int usb_out_endpoint1;
unsigned int usb_in_endpoint2;
unsigned int usb_out_endpoint2;
bool OnOpenDevice()
{
if (m_DeviceHandle != NULL) {
usb_close(m_DeviceHandle);
m_DeviceHandle = NULL;
}
while (1) {
m_DeviceHandle = OpenDevice(0x4255, 0x0001);
if (m_DeviceHandle != NULL)
break;
}
if (m_DeviceHandle == NULL) {
std::cout << “No Device found.”;
return false;
}
else {
//std::wcout << L”Device Opend.\r\n”;
if (usb_set_configuration(m_DeviceHandle, 1) < 0) {
std::cout << “Cant’t Set Configuration 1”;
m_DeviceHandle = NULL;
usb_close(m_DeviceHandle);
return false;
}
if (usb_claim_interface(m_DeviceHandle, 0) < 0) {
std::cout << “Cant’t Set Interface 0”;
m_DeviceHandle = NULL;
usb_close(m_DeviceHandle);
return false;
}
usb_clear_halt(m_DeviceHandle, 1);
usb_clear_halt(m_DeviceHandle, 0x81);
usb_clear_halt(m_DeviceHandle, 2);
usb_clear_halt(m_DeviceHandle, 0x82);
std::cout << “Open Device Success.\r\n”;
return true;
}
}
usb_dev_handle *OpenDevice(int vid, int pid)
{
struct usb_bus* bus;
struct usb_device* dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if ((dev->descriptor.idVendor == vid) && (dev->descriptor.idProduct == pid)) {
return usb_open(dev);
}
}
}
return NULL;
}
bool SimpleClassSendMessage(char *buf, int bytes_to_send)
{
usb_dev_handle* device_handle = m_DeviceHandle;
if (usb_out_endpoint1 & 0x80) {
std::wcout << L”Wrong send addr”;
return false;
}
else {
int nRet = 0;
wchar_t str[1024];
int count = 0;
count = 0;
do {
nRet = usb_bulk_write(device_handle, 0x1, buf, bytes_to_send, 5000);
if (nRet < 0) {
wchar_t uni_err[255];
MultiByteToWideChar(CP_OEMCP, 0, usb_strerror(), -1, uni_err, 255);
wsprintf(str, L”[Error][0x%X] Usb Write Error: %d (%s).\r\n”, usb_out_endpoint1, nRet, uni_err);
//std::wcout << str;
return false;
}
else {
//Sleep(tranfer_delay);
//std::cout << “Send the fuck success!!!\r\n”;
return true;
}
} while (1);
}
}
bool SimpleClassReceiveMessage(char *buf, int bytes_received)
{
int nRet;
usb_dev_handle* device_handle = m_DeviceHandle;
wchar_t str[1024];
do {
nRet = usb_bulk_read(device_handle, 0x81, buf, bytes_received, 5000);
if (nRet < 0) {
wchar_t uni_err[255];
MultiByteToWideChar(CP_OEMCP, 0, usb_strerror(), -1, uni_err, 255);
wsprintf(str, L”[0x%X]Usb Read Error: %d (%s)\r\n”, 0x81, nRet, uni_err);
//std::wcout << str;
return false;
}
else {
//std::cout << “Received success!”;
return true;
}
} while (1);
}
void SimpleClassCleanUp()
{
if (m_DeviceHandle != NULL) {
usb_close(m_DeviceHandle);
m_DeviceHandle = NULL;
}
}
FILE* SafeOpenFile(const wchar_t* fstr)
{
static int i = 0;
FILE* fp = NULL;
while (1) {
_wfopen_s(&fp, fstr, L”a+”);
if (fp == NULL) {
if (i++ > 10) {
break;
}
Sleep(20);
continue;
}
break;
}
i = 0;
return fp;
}
bool CheckMachineTime()
{
char buf[5];
FILE* fp = NULL;
//errno_t ferror;
time_t cur_time_host;
UINT32 cur_time_slave;
//wchar_t str[1024];
time_t time_diff;
bool ret = false;
wchar_t file_str[1024] = {0};
fp = SafeOpenFile(L”D:\\amba_tmp\\cmd.txt”);
if (fp == NULL) {
std::cout << “Cant not open cmd.txt”;
return false;
}
cur_time_host = time(NULL) + 3600 * 8;
buf[0] = CMD_GET_DATETIME;
buf[1] = cur_time_host & 0xff;
buf[2] = (cur_time_host >> 8) & 0xff;
buf[3] = (cur_time_host >> 16) & 0xff;
buf[4] = (cur_time_host >> 24) & 0xff;
if (SimpleClassSendMessage(buf, 5)) {
char buf1[5];
if (SimpleClassReceiveMessage(buf1, 5)) {
cur_time_slave = 0;
cur_time_slave |= UINT8(buf1[0]);
cur_time_slave |= UINT8(buf1[1]) << 8;
cur_time_slave |= UINT8(buf1[2]) << 16;
cur_time_slave |= UINT8(buf1[3]) << 24;
//wsprintf(str, L”host time=0x%x,slave time=0x%x,diff=%d”, cur_time_host, cur_time_slave,cur_time_host-cur_time_slave);
//std::wcout << str;
time_diff = abs(cur_time_host – cur_time_slave);
if (buf1[4] > 0) {//(time_diff > 300) {
wsprintf(file_str, L”DATE_TIME=ERROR%d;\n”, buf1[4]);
fputws(file_str, fp);
ret = true;
}
else {
fputws(L”DATE_TIME=CORRECT;\n”, fp);
ret = true;
}
fflush(fp);
fclose(fp);
}
else
ret = false;
}
else
ret = false;
fputws(L”DATE_TIME=DISCONNECT;\n”, fp);
return ret;
}
void WaitForRemove()
{
char buf[1];
FILE* fp = NULL;
errno_t ferror;
buf[0] = 0;
while (1) {
Sleep(200);
if (SimpleClassSendMessage(buf, 1) == false) {
break;
}
}
fp = SafeOpenFile(L”D:\\amba_tmp\\cmd.txt”);
if (fp == NULL) {
std::cout << “Can not open cmd.txt”;
return;
}
fputws(L”REMOVE=OK;\n”, fp);
fflush(fp);
fclose(fp);
}
static void SetSndMsg(wchar_t *buf_src, char *buf_des, char cmd, int len)
{
int i;
buf_des[0] = cmd;
for (i = 0; i < len; i++) {
buf_des[i + 1] = char(buf_src[i]);
}
buf_des[i+1] = 0;
}
bool AnalysisTXTContent(wchar_t *str)
{
int i;
char msg[64] = {0};
int len = 0;
FILE* fp = NULL;
static int cmdi = 0;
if (str[0] != L’#’)
return false;
for (i = 0; str[i] != 0; i++) {
if (i > 1024)
return false;
if (str[i] == L’#’) {
//std::cout << “read id success\r\n”;
len = 0;
if (wcsncmp(str + i + 1, L”EMPID”, 5) == 0) {
SetSndMsg(str + i + 7, msg, CMD_SET_EMPID, len = 6);
}
else if (wcsncmp(str + i + 1, L”TRNID”, 5) == 0) {
SetSndMsg(str + i + 7, msg, CMD_SET_TRNID, len = 4);
}
else if (wcsncmp(str + i + 1, L”HNDID”, 5) == 0) {
SetSndMsg(str + i + 7, msg, CMD_SET_HNDID, len = 6);
}
else if (wcsncmp(str + i + 1, L”CLSID”, 5) == 0) {
SetSndMsg(str + i + 7, msg, CMD_SET_CLSID, len = 2);
}
else if (wcsncmp(str + i + 1, L”WGUID”, 5) == 0) {
SetSndMsg(str + i + 7, msg, CMD_SET_WGUID, len = 32);
}
else if (wcsncmp(str + i + 1, L”PCMSC”, 5) == 0) {
SetSndMsg(str + i + 7, msg, CMD_SET_PCMSC, len = 0);
if (SimpleClassSendMessage(msg, len + 1) == false) {
std::cout << “send PCMSC msg failed”;
return false;
}
return true;
}
else {
std::cout << “id wrong\r\n”;
return false;
}
if (len != 0) {
if (SimpleClassSendMessage(msg, len + 1) == false) {
std::cout << “send ID msg failed”;
return false;
}
if (++cmdi >= 5) {
cmdi = 0;
fp = SafeOpenFile(L”D:\\amba_tmp\\cmd.txt”);
if (fp == NULL) {
std::cout << “id set,Cant not open cmd.txt”;
return false;
}
fputws(L”SET=OK;\n”, fp);
fflush(fp);
fclose(fp);
return true;
}
}
}
}
return false;
}
bool WaitForContentTXT()
{
char buf[1];
FILE* fp1 = NULL;
FILE* fp2 = NULL;
errno_t ferror;
wchar_t fstr[1024];
while (1) {
buf[0] = 0;
if (SimpleClassSendMessage(buf, 1) == false) {
//ferror = _wfopen_s(&fp2, L”D:\\amba_tmp\\cmd.txt”, L”a+”);
fp2 = SafeOpenFile(L”D:\\amba_tmp\\cmd.txt”);
if (fp2 == NULL) {
std::cout << “Cant’t open file cmd.txt\r\n”;
return false;
}
fputws(L”REMOVE=OK;\n”, fp2);
fflush(fp2);
fclose(fp2);
return false;
}
//ferror = _wfopen_s(&fp1, L”D:\\amba_tmp\\content.txt”, L”a+”);
fp1 = SafeOpenFile(L”D:\\amba_tmp\\content.txt”);
if (fp1 == NULL) {
continue;
}
fgetws(fstr, 1024, fp1);
fclose(fp1);
if (AnalysisTXTContent(fstr) == true) {
break;
}
Sleep(100);
}
return true;
}
本文最后更新于 136 天前,其中的信息可能已经有所发展或是发生改变。