SATDevices.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "StdAfx.h"
  2. #include "SATDevices.h"
  3. #include "SATExecutor.h"
  4. bool CSATDevices::s_bEnableAndroid = true;
  5. ULONGLONG CSATDevices::s_ulEraseDuration = 30000;
  6. ThreadSection CSATDevices::s_ThreadSection;
  7. std::vector<SATDEV::STDevice> CSATDevices::s_vtDevices;
  8. void Split(vector<std::string>& vtLine, std::string strLines, const std::string strSplit)
  9. {
  10. if (strLines.size() == 0 || strSplit.size() == 0)
  11. return;
  12. int nIndex = 0;
  13. vtLine.clear();
  14. std::string strtmp;
  15. while (std::string::npos != (nIndex = strLines.find(strSplit))) {
  16. strtmp = strLines.substr(0, nIndex);
  17. if (strtmp.size())vtLine.push_back(strtmp);
  18. strLines = strLines.substr(nIndex + strSplit.size());
  19. }
  20. if (strLines.size())
  21. vtLine.push_back(strLines);
  22. }
  23. std::string ExecuteCMD(std::string cmd)
  24. {
  25. SECURITY_ATTRIBUTES sa = {0};
  26. HANDLE hRead = NULL, hWrite = NULL;
  27. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  28. sa.lpSecurityDescriptor = NULL;
  29. sa.bInheritHandle = TRUE;
  30. // 创建管道;
  31. if ( !CreatePipe(&hRead, &hWrite, &sa, 1024) )
  32. {
  33. return "";
  34. }
  35. STARTUPINFO si = {0};
  36. PROCESS_INFORMATION pi = {0};
  37. si.cb = sizeof(STARTUPINFO);
  38. GetStartupInfo(&si);
  39. si.hStdError = hWrite;
  40. si.hStdOutput = hWrite;
  41. si.wShowWindow = SW_HIDE;
  42. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  43. // 创建子进程;
  44. TCHAR szCommand[MAX_PATH] = {0};
  45. _tcscpy_s(szCommand,cmd.c_str());
  46. if ( !CreateProcess(NULL, szCommand, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi) )
  47. {
  48. CloseHandle(hRead);
  49. CloseHandle(hWrite);
  50. }
  51. // 等待进程结束;
  52. WaitForSingleObject(pi.hProcess, INFINITE);
  53. // 释放所有句柄;
  54. CloseHandle(pi.hProcess);
  55. CloseHandle(pi.hThread);
  56. CloseHandle(hWrite);
  57. // 等待缓冲写入;
  58. Sleep(200);
  59. // 读取内容;
  60. DWORD dwBytesRead;
  61. TCHAR szBuffer[1024] = {0};
  62. ReadFile(hRead, szBuffer, 1024, &dwBytesRead, NULL);
  63. CloseHandle(hRead);
  64. // 返回结果;
  65. return std::string(szBuffer);
  66. }
  67. CSATDevices::CSATDevices(void)
  68. {
  69. m_hEvent = NULL;
  70. m_hWorkThread = NULL;
  71. // 添加一个虚拟设备;
  72. AddVirtualDevices("192.168.1.999:5555");
  73. }
  74. CSATDevices::~CSATDevices(void)
  75. {
  76. EndofWork();
  77. }
  78. void CSATDevices::StartWork()
  79. {
  80. m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  81. if ( m_hEvent == NULL ) {
  82. return;
  83. }
  84. m_hWorkThread = CreateThread(NULL, 0, WorkThread, this, 0, NULL);
  85. if ( m_hWorkThread == NULL) {
  86. SetEvent(m_hEvent);
  87. CloseHandle(m_hEvent);
  88. CloseHandle(m_hWorkThread);
  89. m_hEvent = NULL;
  90. m_hWorkThread = NULL;
  91. return;
  92. }
  93. }
  94. void CSATDevices::EndofWork()
  95. {
  96. // 设置事件有信号;
  97. if ( m_hEvent )
  98. SetEvent(m_hEvent);
  99. // 等待线程结束;
  100. if ( m_hWorkThread ) {
  101. WaitForSingleObject(m_hWorkThread, INFINITE);
  102. CloseHandle(m_hWorkThread);
  103. m_hWorkThread = NULL;
  104. }
  105. // 关闭事件句柄;
  106. if ( m_hEvent ) {
  107. CloseHandle(m_hEvent);
  108. m_hEvent = NULL;
  109. }
  110. }
  111. DWORD CSATDevices::WorkThread(LPVOID lpVoid)
  112. {
  113. CSATDevices *that = (CSATDevices*)lpVoid;
  114. do
  115. {
  116. if ( !s_bEnableAndroid )
  117. continue;
  118. // 刷新当前设备列表;
  119. std::vector<SATDEV::STDevice> vtDevices;
  120. #ifdef _DEBUG
  121. printf("GetCurrentDevices\n");
  122. #endif
  123. // 获取当前设备列表;
  124. GetCurrentDevices(vtDevices);
  125. // 遍历设备列表是否有下线设备;
  126. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  127. for ( ;it != s_vtDevices.end(); ) {
  128. if ( it->nType == SATDEV::Virtual ) {
  129. it++;
  130. continue;
  131. }
  132. if ( IsDevicesOffline(*it, vtDevices) ) {
  133. #ifdef _DEBUG
  134. printf("IsDevicesOffline\n");
  135. #endif
  136. if ( GetTickCount64() - it->ulOfflineTime > GLOBAL::g_stSATConfig.dwAdbTimeout ) {
  137. GLOBAL::WriteTextLog("移除设备->设备类型:%d, 设备名称:%s, 状态:%d", it->nType, it->strName.c_str(), it->nStatus);
  138. // 移除设备(需要线程加锁);
  139. it = s_vtDevices.erase(it);
  140. } else {
  141. if ( it->nType == SATDEV::Reticle ) {
  142. if ( it->nStatus == SATDEV::Offline ) {
  143. // offline只能reconnect才能重连;
  144. std::string str = "adb reconnect offline";
  145. WinExec(str.c_str(), SW_HIDE);
  146. // 重连后,break此次循环;
  147. GLOBAL::WriteTextLog("offline重连->设备类型:%d, 设备名称:%s, 状态:%d, 命名:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  148. // 等待重连;
  149. Sleep(3500);
  150. break;
  151. }
  152. else if ( it->nStatus == SATDEV::Dropline ) {
  153. std::string str = "adb connect ";
  154. str.append(it->strName);
  155. WinExec(str.c_str(), SW_HIDE);
  156. GLOBAL::WriteTextLog("dropline重连->设备类型:%d, 设备名称:%s, 状态:%d, 命名:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  157. // 等待重连;
  158. Sleep(3500);
  159. }
  160. } else if ( it->nType == SATDEV::Usb ) {
  161. #if 0
  162. // offline只能reconnect才能重连;
  163. std::string str = "adb reconnect offline";
  164. WinExec(str.c_str(), SW_HIDE);
  165. #else
  166. std::string str = "adb kill-server";
  167. // 如果是usb的话,可能要kill-server,再全部重连接;
  168. WinExec(str.c_str(), SW_HIDE);
  169. // 重连所有;
  170. ReConnectAllDevices();
  171. // 重连后,break此次循环;
  172. GLOBAL::WriteTextLog("usb重连->设备类型:%d, 设备名称:%s, 状态:%d, 命名:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  173. #endif
  174. // 等待adb启动;
  175. Sleep(3500);
  176. break;
  177. }
  178. it++;
  179. }
  180. }
  181. else
  182. it++;
  183. }
  184. } while (WaitForSingleObject(that->m_hEvent, 2500) == WAIT_TIMEOUT);
  185. printf("end thread\n");
  186. return 0;
  187. }
  188. void CSATDevices::DelDevices(std::string name)
  189. {
  190. std::string str;
  191. AutoThreadSection ats(&s_ThreadSection);
  192. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  193. for(; it != s_vtDevices.end(); it++ ) {
  194. if ( _tcscmp(it->strName.c_str(), name.c_str()) == 0 ) {
  195. // usb设备没办法删除,此处忽略;
  196. if ( it->nType != SATDEV::Usb) {
  197. // 只有网络设备才能删除;
  198. str = "adb disconnect ";
  199. str.append(name);
  200. WinExec(str.c_str(), SW_HIDE);
  201. s_vtDevices.erase(it);
  202. CSATExecutor::GetInstance()->AddDevices(*it);
  203. }
  204. break;
  205. }
  206. }
  207. }
  208. void CSATDevices::AddReticleDevices(std::string ip)
  209. {
  210. SATDEV::STDevice stDevice;
  211. stDevice.nType = SATDEV::Reticle;
  212. stDevice.strName = ip;
  213. stDevice.ulOfflineTime = 0;
  214. stDevice.nStatus = SATDEV::Online;
  215. stDevice.nUsageState = SATDEV::Idle;
  216. AutoThreadSection ats(&s_ThreadSection);
  217. if (!IsDeviceExist(stDevice)) {
  218. s_vtDevices.push_back(stDevice);
  219. CSATExecutor::GetInstance()->AddDevices(stDevice);
  220. }
  221. }
  222. void CSATDevices::AddVirtualDevices(std::string name)
  223. {
  224. SATDEV::STDevice stDevice;
  225. stDevice.nType = SATDEV::Virtual;
  226. stDevice.strName = name;
  227. stDevice.ulOfflineTime = 0;
  228. stDevice.nStatus = SATDEV::Online;
  229. stDevice.nUsageState = SATDEV::Idle;
  230. AutoThreadSection ats(&s_ThreadSection);
  231. if (!IsDeviceExist(stDevice)) {
  232. s_vtDevices.push_back(stDevice);
  233. CSATExecutor::GetInstance()->AddDevices(stDevice);
  234. }
  235. }
  236. bool CSATDevices::IsDeviceExist(SATDEV::STDevice &stDevice)
  237. {
  238. bool bExist = false;
  239. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  240. for(; it != s_vtDevices.end(); it++ ) {
  241. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  242. bExist = true;
  243. break;
  244. }
  245. }
  246. return bExist;
  247. }
  248. bool CSATDevices::IsDevicesOffline(SATDEV::STDevice &stDevice, std::vector<SATDEV::STDevice> &vtDevices )
  249. {
  250. bool bFound = false;
  251. bool bOffline = true;
  252. // 在当前设备列表查询;
  253. std::vector<SATDEV::STDevice>::iterator it = vtDevices.begin();
  254. for(; it != vtDevices.end(); it++ ) {
  255. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  256. // 找到设备;
  257. bFound = true;
  258. // 同步状态;
  259. stDevice.nStatus = it->nStatus;
  260. // 设备是否offline或dropline;
  261. if ( it->nStatus != SATDEV::Offline && it->nStatus != SATDEV::Dropline)
  262. bOffline = false;
  263. break;
  264. }
  265. }
  266. if ( bOffline ) {
  267. if ( stDevice.ulOfflineTime == 0 ) {
  268. // 首次检测到离线;
  269. if ( stDevice.nStatus != SATDEV::Offline )
  270. stDevice.nStatus = SATDEV::Dropline;
  271. stDevice.ulOfflineTime = GetTickCount64();
  272. }
  273. // 如果没找到设备,彻底离线;
  274. if ( !bFound )
  275. stDevice.nStatus = SATDEV::Dropline;
  276. GLOBAL::WriteTextLog("离线->设备类型:%d, 设备名称:%s, 状态:%d", stDevice.nType, stDevice.strName.c_str(), stDevice.nStatus);
  277. } else {
  278. // 若重新连接,重置离线时间;
  279. if ( stDevice.ulOfflineTime ) {
  280. stDevice.nStatus = SATDEV::Online;
  281. stDevice.ulOfflineTime = 0;
  282. GLOBAL::WriteTextLog("重连成功->设备类型:%d, 设备名称:%s, 状态:%d", stDevice.nType, stDevice.strName.c_str(), stDevice.nStatus);
  283. }
  284. }
  285. return bOffline;
  286. }
  287. bool CSATDevices::IsNewDevices(SATDEV::STDevice &stDevice)
  288. {
  289. bool bNewDevices = true;
  290. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  291. for(; it != s_vtDevices.end(); it++ ) {
  292. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  293. bNewDevices = false;
  294. break;
  295. }
  296. }
  297. // 如果是新设备(一般是usb连接)
  298. if ( bNewDevices ) {
  299. AutoThreadSection ats(&s_ThreadSection);
  300. s_vtDevices.push_back(stDevice);
  301. CSATExecutor::GetInstance()->AddDevices(stDevice);
  302. }
  303. return bNewDevices;
  304. }
  305. void CSATDevices::GetCurrentDevices(std::vector<SATDEV::STDevice> &vtDevices)
  306. {
  307. std::string strLines = ExecuteCMD("adb devices");
  308. std::vector<std::string> vtLine;
  309. Split(vtLine, strLines, "\r\n");
  310. int npos = -1;
  311. // offline设备也要加入,不区分usb或reticle;
  312. for ( std::vector<std::string>::iterator it = vtLine.begin(); it != vtLine.end(); it++ ) {
  313. //if ( _tcsicmp("List of devices attached ", it->c_str()) == 0 )
  314. if ( it->find("List of devices attached") != std::string::npos ||
  315. it->find("* daemon started successfully") != std::string::npos ||
  316. it->find("* daemon not running; starting now at tcp:") != std::string::npos )
  317. continue;
  318. SATDEV::STDevice stDevice;
  319. // 设备类型;
  320. if ( it->find(":5555") != std::string::npos )
  321. stDevice.nType = SATDEV::Reticle;
  322. else if ( it->find(":5555") == std::string::npos )
  323. stDevice.nType = SATDEV::Usb;
  324. stDevice.ulOfflineTime = 0;
  325. // 设备状态;
  326. if ( (npos = it->find(" device")) != std::string::npos )
  327. stDevice.nStatus = SATDEV::Online;
  328. else if ( (npos = it->find(" offline")) != std::string::npos )
  329. stDevice.nStatus = SATDEV::Offline;
  330. // 获取设备名;
  331. stDevice.strName = it->substr(0, npos);
  332. // 如果是网络,去掉5555;
  333. if ( stDevice.nType == SATDEV::Reticle ) {
  334. npos = stDevice.strName.find(":5555");
  335. stDevice.strName = stDevice.strName.substr(0, npos);
  336. }
  337. // 新设备否(一般用于usb设备)
  338. IsNewDevices(stDevice);
  339. // 压入容器保存;
  340. vtDevices.push_back(stDevice);
  341. }
  342. }
  343. void CSATDevices::ReConnectAllDevices()
  344. {
  345. std::string str;
  346. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  347. for ( ;it != s_vtDevices.end(); it++ ) {
  348. if ( it->nType == SATDEV::Reticle ) {
  349. str = "adb connect ";
  350. str.append(it->strName);
  351. WinExec(str.c_str(), SW_HIDE);
  352. }
  353. }
  354. }
  355. void CSATDevices::SetDeviceUsageStatus(std::string strDevName, SATDEV::DEVICE_USAGE_STATUS status)
  356. {
  357. AutoThreadSection ats(&s_ThreadSection);
  358. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  359. for ( ;it != s_vtDevices.end(); it++ ) {
  360. if ( _tcsicmp(it->strName.c_str(), strDevName.c_str() ) == 0) {
  361. it->nUsageState = status;
  362. break;
  363. }
  364. }
  365. }
  366. int CSATDevices::AttachDeviceName2Buffer(SATPROTO::Device (&pbuff)[SATPROTO::MAX_DEVS])
  367. {
  368. int count = 0;
  369. if ( pbuff ) {
  370. std::string str;
  371. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  372. for ( ;it != s_vtDevices.end(); it++ ) {
  373. pbuff[count].nType = it->nType;
  374. pbuff[count].nStatus = it->nStatus;
  375. pbuff[count].nUsageState = it->nUsageState;
  376. memcpy_s(pbuff[count++].szName, MAX_PATH, it->strName.c_str(), it->strName.size());
  377. // 超过MAX_DEVS退出;
  378. if ( count == SATPROTO::MAX_DEVS )
  379. break;
  380. }
  381. }
  382. return count;
  383. }