WirelessAgentSafeClient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "stdafx2.h"
  2. #include "WirelessAgentSafeClient.h"
  3. namespace MySpace {
  4. WirelessAgentSafeClient::WirelessAgentSafeClient(CMobileDialFSM* fsm)
  5. :_client(NULL),_connected(FALSE),_lastError(Error_NotInit),_fsm(fsm)
  6. {
  7. InitializeCriticalSection(&cs_4_connect);
  8. _client = new WirelessDevClient(fsm->GetEntityBase());
  9. if(_client) {
  10. _lastError = Error_Succeed;
  11. }
  12. BeSureClientConnected();
  13. }
  14. WirelessAgentSafeClient::~WirelessAgentSafeClient(void)
  15. {
  16. CloseSession();
  17. DeleteCriticalSection(&cs_4_connect);
  18. }
  19. ErrorCodeEnum WirelessAgentSafeClient::IsActive(BOOL& isActive, BOOL& isConnected)
  20. {
  21. if(!BeSureClientConnected()) {
  22. return _lastError;
  23. }
  24. WirelessDevAgent::WirelessDevService_IsActive_Req req = {};
  25. WirelessDevAgent::WirelessDevService_IsActive_Ans ans = {};
  26. _lastError = _client->IsActive(req, ans, 10000);
  27. if(_lastError == Error_Succeed) {
  28. isActive = ans.activeStatus >= 1 ? TRUE : FALSE;
  29. isConnected = ans.connStatus >= 1 ? TRUE : FALSE;
  30. } else {
  31. Dbg("IsActive request failed: %d", _lastError);
  32. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 IsActive 接口失败(EC=%d)", _lastError);
  33. }
  34. return _lastError;
  35. }
  36. ErrorCodeEnum WirelessAgentSafeClient::GetAPNConfigs(int slotID, int& count, bool onlySimInfo, bool bClear)
  37. {
  38. if(!BeSureClientConnected()) {
  39. return _lastError;
  40. }
  41. WirelessDevAgent::WirelessDevService_GetAPNConfigs_Req req = {};
  42. WirelessDevAgent::WirelessDevService_GetAPNConfigs_Ans ans = {};
  43. Dbg("Start to GetAPNConfigs...");
  44. req.ComponentType = onlySimInfo ? 1 : 0;
  45. req.FilterName = "";
  46. req.FilterSlotNum = slotID;
  47. _lastError = _client->GetAPNConfigs(req, ans, 10000);
  48. if(_lastError == Error_Succeed) {
  49. Dbg("Invoke GetAPNConfigs succ, result code: %d", ans.ResultCode);
  50. count = ans.ResultCode;
  51. if(ans.ResultCode > 0) {
  52. DongleList donglist;
  53. donglist.Init(ans.ResultCode);
  54. int profileCounts = 0;
  55. CAutoArray<int> curProfileIdx(ans.ResultCode);
  56. for(int i=0; i<ans.ResultCode; ++i) {
  57. memset(&donglist[i], 0, sizeof(MbnDongleInfo));
  58. InitMbnDongleSubInfo(&donglist[i].subInfo);
  59. donglist[i].subInfo.ulSingnal = 0xFFFFFFFF;
  60. donglist[i].csInterfaceId = CSimpleStringA::Format("%02d%s",
  61. ans.SlotName[i], ans.MCCMNC[i]);
  62. donglist[i].subInfo.strProviderName = CSimpleStringA::Format("[卡%d]%s",
  63. ans.SlotName[i],
  64. MBN::ConvertOperatorToString(ans.OperatorName[i]));
  65. Dbg("interface_id: %s", (LPCTSTR)donglist[i].csInterfaceId);
  66. Dbg("provider_name: %s", (LPCTSTR)donglist[i].subInfo.strProviderName);
  67. Dbg("sim state: %d", ans.SIMState[i]);
  68. Dbg("active state: %d", ans.ActiveState[i]);
  69. donglist[i].subInfo.strIMSI = ans.IMSI[i];
  70. donglist[i].subInfo.mbnReadyState = MBN::ConvertToMBNState(ans.SIMState[i]);
  71. donglist[i].subInfo.strProfileName = "";
  72. if(ans.SlotName[i] == 1) {
  73. donglist[i].usDeviceType = DEVTYPE_ANDROID_MAIN;
  74. } else if(ans.SlotName[i] == 2){
  75. donglist[i].usDeviceType = DEVTYPE_ANDROID_VICE;
  76. } else {
  77. donglist[i].usDeviceType = DEVTYPE_ANDROID_UKNOWN;
  78. }
  79. donglist[i].subInfo.strDataClass = ans.MCCMNC[i];
  80. donglist[i].priority = ans.ActiveState[i];
  81. if(ans.ActiveState[i] > 0) {
  82. if(donglist[i].subInfo.mbnReadyState == MBN_READY_STATE_INITIALIZED) {
  83. donglist[i].state = MBD_ACTIVATION_CONNECTED;
  84. donglist[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_ACTIVATED;
  85. } else {
  86. donglist[i].state = MBN::ConvertToMCS(donglist[i].subInfo.mbnReadyState);
  87. donglist[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
  88. }
  89. } else {
  90. donglist[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
  91. if(donglist[i].subInfo.mbnReadyState == MBN_READY_STATE_INITIALIZED) {
  92. donglist[i].state = MBD_ACTIVATION_DISCONNECTED;
  93. } else {
  94. donglist[i].state = MBN::ConvertToMCS(donglist[i].subInfo.mbnReadyState);
  95. }
  96. }
  97. Dbg("profile count: %d", ans.ProfileCount[i]);
  98. donglist[i].profileList.Init(ans.ProfileCount[i]);
  99. profileCounts += ans.ProfileCount[i];
  100. curProfileIdx[i] = 0;
  101. }
  102. Dbg("profileCounts: %d vs %d", profileCounts, ans.ProfileID.GetCount());
  103. profileCounts = ans.ProfileID.GetCount();
  104. for(int i=0; i<profileCounts; ++i) {
  105. for(int j=0; j<ans.ResultCode; ++j) {
  106. if(ans.SlotNum[i] == ans.SlotName[j] && curProfileIdx[j] < ans.ProfileCount[j]) {
  107. ptMbnProfileInfo profileInfo = &donglist[j].profileList[curProfileIdx[j]];
  108. Dbg("%d, %d, %d", j, curProfileIdx[j], i);
  109. curProfileIdx[j]++;
  110. _UpdateProfileInfo(profileInfo, ans, i);
  111. if(profileInfo->AccessInfo.IsDefault) {
  112. donglist[j].subInfo.strProfileName = profileInfo->strProfileName.GetData();
  113. }
  114. } else {
  115. Dbg("[%d]%d, [%d]%d", i, ans.SlotNum[i], j, ans.SlotName[j]);
  116. }
  117. }
  118. }
  119. if(bClear) {
  120. Dbg("Clear...");
  121. ResetDongleInfos();
  122. g_DongleInfos.Append(donglist);
  123. } else {
  124. if(g_DongleInfos.GetCount() > 0 && g_DongleInfos.GetCount() != ans.ResultCode) {
  125. Dbg("Reset...");
  126. ResetDongleInfos();
  127. g_DongleInfos.Append(donglist);
  128. } else {
  129. for(int i=0; i<ans.ResultCode; ++i) {
  130. for(int j=0; j<g_DongleInfos.GetCount(); ++j) {
  131. if(donglist[i].usDeviceType == g_DongleInfos[j].usDeviceType) {
  132. Dbg("Set %d", j);
  133. g_DongleInfos[j].subInfo.mbnReadyState = donglist[i].subInfo.mbnReadyState;
  134. g_DongleInfos[j].priority = donglist[i].priority;
  135. g_DongleInfos[j].state = donglist[i].state;
  136. g_DongleInfos[j].subInfo.mbnCntState = donglist[i].subInfo.mbnCntState;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. } else {
  144. Dbg("Invoke GetAPNConfigs failed, EC=%u", _lastError);
  145. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 GetAPNConfigs 接口失败(EC=%d)", _lastError);
  146. }
  147. return _lastError;
  148. }
  149. ErrorCodeEnum WirelessAgentSafeClient::GetAPNConfigsPure(int nIndex, int& count)
  150. {
  151. if(nIndex < 0 || nIndex >= g_DongleInfos.GetCount()) {
  152. return Error_Param;
  153. }
  154. if(!BeSureClientConnected()) {
  155. return _lastError;
  156. }
  157. WirelessDevAgent::WirelessDevService_GetAPNConfigs_Req req = {};
  158. WirelessDevAgent::WirelessDevService_GetAPNConfigs_Ans ans = {};
  159. Dbg("Start to GetAPNConfigsPure...");
  160. req.ComponentType = 2;
  161. req.FilterName = "";
  162. if(g_DongleInfos[nIndex].usDeviceType == DEVTYPE_ANDROID_MAIN) {
  163. req.FilterSlotNum = 1; // sim_slot_main
  164. } else if(g_DongleInfos[nIndex].usDeviceType == DEVTYPE_ANDROID_VICE) {
  165. req.FilterSlotNum = 2; // sim_slot_vice
  166. } else {
  167. req.FilterSlotNum = 0; // sim_slot_unknown
  168. }
  169. _lastError = _client->GetAPNConfigs(req, ans, 10000);
  170. if(_lastError == Error_Succeed) {
  171. Dbg("Invoke GetAPNConfigsPure succ, result code: %d", ans.ResultCode);
  172. count = ans.ResultCode;
  173. if(ans.ResultCode > 0) {
  174. g_DongleInfos[nIndex].profileList.Clear();
  175. g_DongleInfos[nIndex].profileList.Init(count);
  176. for(int i=0; i<count; ++i) {
  177. _UpdateProfileInfo(&g_DongleInfos[nIndex].profileList[i], ans, i);
  178. if(g_DongleInfos[nIndex].profileList[i].AccessInfo.IsDefault) {
  179. g_DongleInfos[nIndex].subInfo.strProfileName = g_DongleInfos[nIndex].profileList[i].strProfileName.GetData();
  180. }
  181. }
  182. }
  183. } else {
  184. Dbg("Invoke GetAPNConfigs failed, EC=%u", _lastError);
  185. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 GetAPNConfigs 接口失败(EC=%d)", _lastError);
  186. }
  187. return _lastError;
  188. }
  189. ErrorCodeEnum WirelessAgentSafeClient::SetDefaultSlot(int slotIdx)
  190. {
  191. if(!BeSureClientConnected()) {
  192. return _lastError;
  193. }
  194. WirelessDevAgent::WirelessDevService_SwitchDefaultSlot_Req req = {};
  195. WirelessDevAgent::WirelessDevService_SwitchDefaultSlot_Ans ans = {};
  196. req.SlotIdx = slotIdx;
  197. req.Subjoin = "";
  198. Dbg("Start to SetDefaultSlot...");
  199. _lastError = _client->SwitchDefaultSlot(req, ans, 10000);
  200. if(_lastError == Error_Succeed) {
  201. if(ans.ResultCode != 0) {
  202. Dbg("Detect SwitchDefaultSlot returned unexpected error: %d", ans.ResultCode);
  203. return (_lastError = (ErrorCodeEnum)ans.ResultCode);
  204. } else {
  205. Dbg("Invoke SwitchDefaultSlot successfully!");
  206. return _lastError;
  207. }
  208. } else {
  209. Dbg("Invoke SwitchDefaultSlot failed, EC=%u", _lastError);
  210. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 SwitchDefaultSlot 接口失败(EC=%d)", _lastError);
  211. }
  212. return _lastError;
  213. }
  214. ErrorCodeEnum WirelessAgentSafeClient::DeleteProfile(int profileID)
  215. {
  216. if(!BeSureClientConnected()) {
  217. return _lastError;
  218. }
  219. WirelessDevAgent::WirelessDevService_DeleteAPN_Req req = {};
  220. WirelessDevAgent::WirelessDevService_DeleteAPN_Ans ans = {};
  221. req.ProfileID = profileID;
  222. Dbg("Start to DeleteProfile...");
  223. _lastError = _client->DeleteAPN(req, ans, 10000);
  224. if(_lastError == Error_Succeed) {
  225. if(ans.ResultCode != 0) {
  226. Dbg("Detect DeleteAPN returned unexpected error: %d", ans.ResultCode);
  227. return (_lastError = (ErrorCodeEnum)ans.ResultCode);
  228. } else {
  229. Dbg("Invoke DeleteAPN successfully!");
  230. return _lastError;
  231. }
  232. } else {
  233. Dbg("Invoke DeleteAPN failed, EC=%u", _lastError);
  234. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 DeleteAPN 接口失败(EC=%d)", _lastError);
  235. }
  236. return _lastError;
  237. }
  238. ErrorCodeEnum WirelessAgentSafeClient::UpdateOrInsertAPN(int slotID, const AccessKeyInfo& newInfo, bool fApplyNow, int updateProfileID)
  239. {
  240. if(!BeSureClientConnected()) {
  241. _fsm->m_csErrMsg = CSimpleStringA::Format("连接管控实体失败(EC=%d)", _lastError);
  242. return _lastError;
  243. }
  244. WirelessDevAgent::WirelessDevService_UpdateOrInsertAPN_Req req = {};
  245. WirelessDevAgent::WirelessDevService_UpdateOrInsertAPN_Ans ans = {};
  246. req.ProfileID = updateProfileID;
  247. req.Name = newInfo.strProfileDesc;
  248. req.APNString = newInfo.strAccessString;
  249. req.Password = newInfo.strPassword;
  250. req.User = newInfo.strUserName;
  251. if(newInfo.usAPType == 0) {
  252. req.AuthType = -1;
  253. } else {
  254. req.AuthType = newInfo.usAPType;
  255. }
  256. req.Numeric = "";
  257. req.SlotNum = slotID;
  258. req.Server = "";
  259. req.Type = "";
  260. req.Port = 0;
  261. req.ApplyNow = fApplyNow;
  262. req.reserved1 = 0;
  263. req.reserved2 = "";
  264. req.reserved3 = 0;
  265. req.reserved4.Init();
  266. Dbg("Start to UpdateOrInsertAPN...");
  267. _lastError = _client->UpdateOrInsertAPN(req, ans, 10000);
  268. if(_lastError == Error_Succeed) {
  269. if(ans.ResultCode != 0) {
  270. Dbg("Detect UpdateOrInsertAPN returned unexpected error: %d", ans.ResultCode);
  271. return (_lastError = (ErrorCodeEnum)ans.ResultCode);
  272. } else {
  273. Dbg("Invoke UpdateOrInsertAPN successfully!");
  274. return _lastError;
  275. }
  276. } else {
  277. Dbg("Invoke UpdateOrInsertAPN failed, EC=%u", _lastError);
  278. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 UpdateOrInsertAPN 接口失败(EC=%d)", _lastError);
  279. }
  280. return _lastError;
  281. }
  282. ErrorCodeEnum WirelessAgentSafeClient::ConnectWithDefault()
  283. {
  284. if(!BeSureClientConnected()) {
  285. _fsm->m_csErrMsg = CSimpleStringA::Format("连接管控实体失败(EC=%d)", _lastError);
  286. return _lastError;
  287. }
  288. WirelessDevAgent::WirelessDevService_ConnectWlan_Req req = {};
  289. WirelessDevAgent::WirelessDevService_ConnectWlan_Ans ans = {};
  290. req.SSID = "";
  291. req.Password = "";
  292. req.ResetMode = true;
  293. req.HiddenMode = true;
  294. req.ForceUpdate = true;
  295. req.Reserved1 = 0;
  296. req.Reserved2 = "";
  297. Dbg("Start to ConnectWlan...");
  298. _lastError = _client->ConnectWlan(req, ans, 62000);
  299. if(_lastError == Error_Succeed) {
  300. if(ans.Result != 0) {
  301. Dbg("Detect ConnectWlan returned unexpected error: %d", ans.Result);
  302. _fsm->m_csErrMsg = CSimpleStringA::Format("调用接口失败:%d", ans.ErroCode);
  303. return (_lastError = (ErrorCodeEnum)ans.Result);
  304. } else {
  305. Dbg("Invoke ConnectWlan successfully!");
  306. return _lastError;
  307. }
  308. } else {
  309. Dbg("Invoke ConnectWlan failed, EC=%u", _lastError);
  310. _fsm->m_csErrMsg = CSimpleStringA::Format("调用 ConnectWlan 接口失败(EC=%d)", _lastError);
  311. }
  312. return _lastError;
  313. }
  314. BOOL WirelessAgentSafeClient::BeSureClientConnected(BOOL reConnnect)
  315. {
  316. if(_connected && (reConnnect || _lastError == Error_NetBroken)) {
  317. CloseSession();
  318. }
  319. if(_client == NULL) {
  320. EnterCriticalSection(&cs_4_connect);
  321. if(_client == NULL) {
  322. _client = new WirelessDevClient(_fsm->GetEntityBase());
  323. }
  324. LeaveCriticalSection(&cs_4_connect);
  325. }
  326. if(!_connected) {
  327. EnterCriticalSection(&cs_4_connect);
  328. if(!_connected) {
  329. Dbg("Start to connect WirelessDevAgent.");
  330. ErrorCodeEnum ec = _client->Connect();
  331. if(ec == Error_Succeed) {
  332. _connected = TRUE;
  333. } else {
  334. Dbg("Connect to WirelessDevAgent failed, EC=%u", ec);
  335. _fsm->m_csErrMsg = CSimpleStringA::Format("连接管控实体失败(EC=%d)", _lastError);
  336. _lastError = Error_Break;
  337. }
  338. }
  339. LeaveCriticalSection(&cs_4_connect);
  340. }
  341. return _connected;
  342. }
  343. void WirelessAgentSafeClient::CloseSession()
  344. {
  345. EnterCriticalSection(&cs_4_connect);
  346. if(_connected) {
  347. Dbg("close session for WirelessDevAgent client.");
  348. _client->GetFunction()->CloseSession();
  349. _connected = FALSE;
  350. }
  351. if(_client != NULL) {
  352. _client->SafeDelete();
  353. _client = NULL;
  354. }
  355. LeaveCriticalSection(&cs_4_connect);
  356. }
  357. void WirelessAgentSafeClient::_UpdateProfileInfo(
  358. ptMbnProfileInfo profileInfo, WirelessDevAgent::WirelessDevService_GetAPNConfigs_Ans& ans, int i)
  359. {
  360. InitAccessKeyInfo(&profileInfo->AccessInfo);
  361. profileInfo->bUserDefined = true;
  362. profileInfo->AccessInfo.usCntType = CNT_ANDROID;
  363. profileInfo->AccessInfo.strProfileDesc = ans.Name[i];
  364. profileInfo->AccessInfo.strPhoneNum = ans.Numeric[i];
  365. profileInfo->strProfileName = CSimpleStringA::Format("%d", ans.ProfileID[i]);
  366. profileInfo->AccessInfo.IsDefault = ans.ProfileState[i] != 0;
  367. profileInfo->AccessInfo.strAccessString = ans.APNString[i];
  368. profileInfo->AccessInfo.strUserName = ans.User[i];
  369. Dbg("Profile ADD: %s - Actived: %d", (LPCTSTR)profileInfo->strProfileName, ans.ProfileState[i]);
  370. Dbg("profile desc: %s", (LPCTSTR)profileInfo->AccessInfo.strProfileDesc);
  371. profileInfo->AccessInfo.usAPType = 0;
  372. if(ans.AuthType[i] == -1) {
  373. } else {
  374. //1 PAP;2 CHAP;3 PAPorCHAP
  375. if(ans.AuthType[i] & 1) {
  376. profileInfo->AccessInfo.usAPType |= AUPR_PAP;
  377. }
  378. if(ans.AuthType[i] & 2) {
  379. profileInfo->AccessInfo.usAPType |= AUPR_CHAP;
  380. }
  381. }
  382. profileInfo->AccessInfo.strPassword = ans.Password[i];
  383. return;
  384. }
  385. }