SATDevices.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. // 移除设备(需要线程加锁);
  138. it = s_vtDevices.erase(it);
  139. } else {
  140. if ( it->nType == SATDEV::Reticle ) {
  141. if ( it->nStatus == SATDEV::Offline ) {
  142. // offline只能reconnect才能重连;
  143. std::string str = "adb reconnect ";
  144. str.append(it->strName);
  145. WinExec(str.c_str(), SW_HIDE);
  146. }
  147. else if ( it->nStatus == SATDEV::Dropline ) {
  148. std::string str = "adb connect ";
  149. str.append(it->strName);
  150. WinExec(str.c_str(), SW_HIDE);
  151. }
  152. } else if ( it->nType == SATDEV::Usb ) {
  153. // 如果是usb的话,可能要kill-server,再全部重连接;
  154. WinExec("adb kill-server", SW_HIDE);
  155. // 重连所有;
  156. ReConnectAllDevices();
  157. }
  158. it++;
  159. }
  160. }
  161. else
  162. it++;
  163. }
  164. } while (WaitForSingleObject(that->m_hEvent, 2500) == WAIT_TIMEOUT);
  165. printf("end thread\n");
  166. return 0;
  167. }
  168. void CSATDevices::DelDevices(std::string name)
  169. {
  170. std::string str;
  171. AutoThreadSection ats(&s_ThreadSection);
  172. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  173. for(; it != s_vtDevices.end(); it++ ) {
  174. if ( _tcscmp(it->strName.c_str(), name.c_str()) == 0 ) {
  175. // usb设备没办法删除,此处忽略;
  176. if ( it->nType != SATDEV::Usb) {
  177. // 只有网络设备才能删除;
  178. str = "adb disconnect ";
  179. str.append(name);
  180. WinExec(str.c_str(), SW_HIDE);
  181. s_vtDevices.erase(it);
  182. CSATExecutor::GetInstance()->AddDevices(*it);
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. void CSATDevices::AddReticleDevices(std::string ip)
  189. {
  190. SATDEV::STDevice stDevice;
  191. stDevice.nType = SATDEV::Reticle;
  192. stDevice.strName = ip;
  193. stDevice.ulOfflineTime = 0;
  194. stDevice.nStatus = SATDEV::Online;
  195. stDevice.nUsageState = SATDEV::Idle;
  196. AutoThreadSection ats(&s_ThreadSection);
  197. if (!IsDeviceExist(stDevice)) {
  198. s_vtDevices.push_back(stDevice);
  199. CSATExecutor::GetInstance()->AddDevices(stDevice);
  200. }
  201. }
  202. void CSATDevices::AddVirtualDevices(std::string name)
  203. {
  204. SATDEV::STDevice stDevice;
  205. stDevice.nType = SATDEV::Virtual;
  206. stDevice.strName = name;
  207. stDevice.ulOfflineTime = 0;
  208. stDevice.nStatus = SATDEV::Online;
  209. stDevice.nUsageState = SATDEV::Idle;
  210. AutoThreadSection ats(&s_ThreadSection);
  211. if (!IsDeviceExist(stDevice)) {
  212. s_vtDevices.push_back(stDevice);
  213. CSATExecutor::GetInstance()->AddDevices(stDevice);
  214. }
  215. }
  216. bool CSATDevices::IsDeviceExist(SATDEV::STDevice &stDevice)
  217. {
  218. bool bExist = false;
  219. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  220. for(; it != s_vtDevices.end(); it++ ) {
  221. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  222. bExist = true;
  223. break;
  224. }
  225. }
  226. return bExist;
  227. }
  228. bool CSATDevices::IsDevicesOffline(SATDEV::STDevice &stDevice, std::vector<SATDEV::STDevice> &vtDevices )
  229. {
  230. bool bOffline = true;
  231. // 在当前设备列表查询;
  232. std::vector<SATDEV::STDevice>::iterator it = vtDevices.begin();
  233. for(; it != vtDevices.end(); it++ ) {
  234. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  235. // 同步状态;
  236. stDevice.nStatus = it->nStatus;
  237. // 设备是否offline或dropline;
  238. if ( it->nStatus != SATDEV::Offline && it->nStatus != SATDEV::Dropline)
  239. bOffline = false;
  240. break;
  241. }
  242. }
  243. if ( bOffline ) {
  244. if ( stDevice.ulOfflineTime == 0 ) {
  245. // 首次检测到离线;
  246. if ( stDevice.nStatus != SATDEV::Offline )
  247. stDevice.nStatus = SATDEV::Dropline;
  248. stDevice.ulOfflineTime = GetTickCount64();
  249. }
  250. } else {
  251. // 若重新连接,重置离线时间;
  252. if ( stDevice.ulOfflineTime ) {
  253. stDevice.nStatus = SATDEV::Online;
  254. stDevice.ulOfflineTime = 0;
  255. }
  256. }
  257. return bOffline;
  258. }
  259. bool CSATDevices::IsNewDevices(SATDEV::STDevice &stDevice)
  260. {
  261. bool bNewDevices = true;
  262. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  263. for(; it != s_vtDevices.end(); it++ ) {
  264. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  265. bNewDevices = false;
  266. break;
  267. }
  268. }
  269. // 如果是新设备(一般是usb连接)
  270. if ( bNewDevices ) {
  271. AutoThreadSection ats(&s_ThreadSection);
  272. s_vtDevices.push_back(stDevice);
  273. CSATExecutor::GetInstance()->AddDevices(stDevice);
  274. }
  275. return bNewDevices;
  276. }
  277. void CSATDevices::GetCurrentDevices(std::vector<SATDEV::STDevice> &vtDevices)
  278. {
  279. std::string strLines = ExecuteCMD("adb devices");
  280. std::vector<std::string> vtLine;
  281. Split(vtLine, strLines, "\r\n");
  282. int npos = -1;
  283. // offline设备也要加入,不区分usb或reticle;
  284. for ( std::vector<std::string>::iterator it = vtLine.begin(); it != vtLine.end(); it++ ) {
  285. //if ( _tcsicmp("List of devices attached ", it->c_str()) == 0 )
  286. if ( it->find("List of devices attached") != std::string::npos )
  287. continue;
  288. SATDEV::STDevice stDevice;
  289. // 设备类型;
  290. if ( it->find(":5555") != std::string::npos )
  291. stDevice.nType = SATDEV::Reticle;
  292. else if ( it->find(":5555") == std::string::npos )
  293. stDevice.nType = SATDEV::Reticle;
  294. stDevice.ulOfflineTime = 0;
  295. // 设备状态;
  296. if ( (npos = it->find(" device")) != std::string::npos )
  297. stDevice.nStatus = SATDEV::Online;
  298. else if ( (npos = it->find(" offline")) != std::string::npos )
  299. stDevice.nStatus = SATDEV::Offline;
  300. // 获取设备名;
  301. stDevice.strName = it->substr(0, npos);
  302. // 新设备否(一般用于usb设备)
  303. IsNewDevices(stDevice);
  304. // 压入容器保存;
  305. vtDevices.push_back(stDevice);
  306. }
  307. }
  308. void CSATDevices::ReConnectAllDevices()
  309. {
  310. std::string str;
  311. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  312. for ( ;it != s_vtDevices.end(); it++ ) {
  313. if ( it->nType == SATDEV::Reticle ) {
  314. str = "adb connect ";
  315. str.append(it->strName);
  316. WinExec(str.c_str(), SW_HIDE);
  317. }
  318. }
  319. }
  320. void CSATDevices::SetDeviceUsageStatus(std::string strDevName, SATDEV::DEVICE_USAGE_STATUS status)
  321. {
  322. AutoThreadSection ats(&s_ThreadSection);
  323. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  324. for ( ;it != s_vtDevices.end(); it++ ) {
  325. if ( _tcsicmp(it->strName.c_str(), strDevName.c_str() ) == 0) {
  326. it->nUsageState = status;
  327. break;
  328. }
  329. }
  330. }
  331. int CSATDevices::AttachDeviceName2Buffer(SATPROTO::Device (&pbuff)[SATPROTO::MAX_DEVS])
  332. {
  333. int count = 0;
  334. if ( pbuff ) {
  335. std::string str;
  336. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  337. for ( ;it != s_vtDevices.end(); it++ ) {
  338. pbuff[count].nType = it->nType;
  339. pbuff[count].nStatus = it->nStatus;
  340. memcpy_s(pbuff[count++].szName, MAX_PATH, it->strName.c_str(), it->strName.size());
  341. // 超过MAX_DEVS退出;
  342. if ( count == SATPROTO::MAX_DEVS )
  343. break;
  344. }
  345. }
  346. return count;
  347. }