123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- #include "stdafx2.h"
- #include "WirelessAgentSafeClient.h"
- namespace MySpace {
- WirelessAgentSafeClient::WirelessAgentSafeClient(CMobileDialFSM* fsm)
- :_client(NULL),_connected(FALSE),_lastError(Error_NotInit),_fsm(fsm)
- {
- InitializeCriticalSection(&cs_4_connect);
- _client = new WirelessDevClient(fsm->GetEntityBase());
- if(_client) {
- _lastError = Error_Succeed;
- }
- BeSureClientConnected();
- }
- WirelessAgentSafeClient::~WirelessAgentSafeClient(void)
- {
- CloseSession();
- DeleteCriticalSection(&cs_4_connect);
- }
- ErrorCodeEnum WirelessAgentSafeClient::IsActive(BOOL& isActive, BOOL& isConnected)
- {
- if(!BeSureClientConnected()) {
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_IsActive_Req req = {};
- WirelessDevAgent::WirelessDevService_IsActive_Ans ans = {};
- _lastError = _client->IsActive(req, ans, 10000);
- if(_lastError == Error_Succeed) {
- isActive = ans.activeStatus >= 1 ? TRUE : FALSE;
- isConnected = ans.connStatus >= 1 ? TRUE : FALSE;
- } else {
- Dbg("IsActive request failed: %d", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 IsActive 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- ErrorCodeEnum WirelessAgentSafeClient::GetAPNConfigs(int slotID, int& count, bool onlySimInfo, bool bClear)
- {
- if(!BeSureClientConnected()) {
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_GetAPNConfigs_Req req = {};
- WirelessDevAgent::WirelessDevService_GetAPNConfigs_Ans ans = {};
- Dbg("Start to GetAPNConfigs...");
- req.ComponentType = onlySimInfo ? 1 : 0;
- req.FilterName = "";
- req.FilterSlotNum = slotID;
- _lastError = _client->GetAPNConfigs(req, ans, 10000);
- if(_lastError == Error_Succeed) {
- Dbg("Invoke GetAPNConfigs succ, result code: %d", ans.ResultCode);
- count = ans.ResultCode;
- if(ans.ResultCode > 0) {
- DongleList donglist;
- donglist.Init(ans.ResultCode);
- int profileCounts = 0;
- CAutoArray<int> curProfileIdx(ans.ResultCode);
- for(int i=0; i<ans.ResultCode; ++i) {
- memset(&donglist[i], 0, sizeof(MbnDongleInfo));
- InitMbnDongleSubInfo(&donglist[i].subInfo);
- donglist[i].subInfo.ulSingnal = 0xFFFFFFFF;
- donglist[i].csInterfaceId = CSimpleStringA::Format("%02d%s",
- ans.SlotName[i], ans.MCCMNC[i]);
- donglist[i].subInfo.strProviderName = CSimpleStringA::Format("[卡%d]%s",
- ans.SlotName[i],
- MBN::ConvertOperatorToString(ans.OperatorName[i]));
- Dbg("interface_id: %s", (LPCTSTR)donglist[i].csInterfaceId);
- Dbg("provider_name: %s", (LPCTSTR)donglist[i].subInfo.strProviderName);
- Dbg("sim state: %d", ans.SIMState[i]);
- Dbg("active state: %d", ans.ActiveState[i]);
- donglist[i].subInfo.strIMSI = ans.IMSI[i];
- donglist[i].subInfo.mbnReadyState = MBN::ConvertToMBNState(ans.SIMState[i]);
- donglist[i].subInfo.strProfileName = "";
- if(ans.SlotName[i] == 1) {
- donglist[i].usDeviceType = DEVTYPE_ANDROID_MAIN;
- } else if(ans.SlotName[i] == 2){
- donglist[i].usDeviceType = DEVTYPE_ANDROID_VICE;
- } else {
- donglist[i].usDeviceType = DEVTYPE_ANDROID_UKNOWN;
- }
- donglist[i].subInfo.strDataClass = ans.MCCMNC[i];
- donglist[i].priority = ans.ActiveState[i];
- if(ans.ActiveState[i] > 0) {
- if(donglist[i].subInfo.mbnReadyState == MBN_READY_STATE_INITIALIZED) {
- donglist[i].state = MBD_ACTIVATION_CONNECTED;
- donglist[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_ACTIVATED;
- } else {
- donglist[i].state = MBN::ConvertToMCS(donglist[i].subInfo.mbnReadyState);
- donglist[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
- }
- } else {
- donglist[i].subInfo.mbnCntState = MBN_ACTIVATION_STATE_DEACTIVATED;
- if(donglist[i].subInfo.mbnReadyState == MBN_READY_STATE_INITIALIZED) {
- donglist[i].state = MBD_ACTIVATION_DISCONNECTED;
- } else {
- donglist[i].state = MBN::ConvertToMCS(donglist[i].subInfo.mbnReadyState);
- }
- }
- Dbg("profile count: %d", ans.ProfileCount[i]);
- donglist[i].profileList.Init(ans.ProfileCount[i]);
- profileCounts += ans.ProfileCount[i];
- curProfileIdx[i] = 0;
- }
- Dbg("profileCounts: %d vs %d", profileCounts, ans.ProfileID.GetCount());
- profileCounts = ans.ProfileID.GetCount();
- for(int i=0; i<profileCounts; ++i) {
- for(int j=0; j<ans.ResultCode; ++j) {
- if(ans.SlotNum[i] == ans.SlotName[j] && curProfileIdx[j] < ans.ProfileCount[j]) {
- ptMbnProfileInfo profileInfo = &donglist[j].profileList[curProfileIdx[j]];
- Dbg("%d, %d, %d", j, curProfileIdx[j], i);
- curProfileIdx[j]++;
- _UpdateProfileInfo(profileInfo, ans, i);
- if(profileInfo->AccessInfo.IsDefault) {
- donglist[j].subInfo.strProfileName = profileInfo->strProfileName.GetData();
- }
- } else {
- Dbg("[%d]%d, [%d]%d", i, ans.SlotNum[i], j, ans.SlotName[j]);
- }
- }
- }
- if(bClear) {
- Dbg("Clear...");
- ResetDongleInfos();
- g_DongleInfos.Append(donglist);
- } else {
- if(g_DongleInfos.GetCount() > 0 && g_DongleInfos.GetCount() != ans.ResultCode) {
- Dbg("Reset...");
- ResetDongleInfos();
- g_DongleInfos.Append(donglist);
- } else {
- for(int i=0; i<ans.ResultCode; ++i) {
- for(int j=0; j<g_DongleInfos.GetCount(); ++j) {
- if(donglist[i].usDeviceType == g_DongleInfos[j].usDeviceType) {
- Dbg("Set %d", j);
- g_DongleInfos[j].subInfo.mbnReadyState = donglist[i].subInfo.mbnReadyState;
- g_DongleInfos[j].priority = donglist[i].priority;
- g_DongleInfos[j].state = donglist[i].state;
- g_DongleInfos[j].subInfo.mbnCntState = donglist[i].subInfo.mbnCntState;
- }
- }
- }
- }
- }
- }
- } else {
- Dbg("Invoke GetAPNConfigs failed, EC=%u", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 GetAPNConfigs 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- ErrorCodeEnum WirelessAgentSafeClient::GetAPNConfigsPure(int nIndex, int& count)
- {
- if(nIndex < 0 || nIndex >= g_DongleInfos.GetCount()) {
- return Error_Param;
- }
- if(!BeSureClientConnected()) {
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_GetAPNConfigs_Req req = {};
- WirelessDevAgent::WirelessDevService_GetAPNConfigs_Ans ans = {};
- Dbg("Start to GetAPNConfigsPure...");
- req.ComponentType = 2;
- req.FilterName = "";
- if(g_DongleInfos[nIndex].usDeviceType == DEVTYPE_ANDROID_MAIN) {
- req.FilterSlotNum = 1; // sim_slot_main
- } else if(g_DongleInfos[nIndex].usDeviceType == DEVTYPE_ANDROID_VICE) {
- req.FilterSlotNum = 2; // sim_slot_vice
- } else {
- req.FilterSlotNum = 0; // sim_slot_unknown
- }
- _lastError = _client->GetAPNConfigs(req, ans, 10000);
- if(_lastError == Error_Succeed) {
- Dbg("Invoke GetAPNConfigsPure succ, result code: %d", ans.ResultCode);
- count = ans.ResultCode;
- if(ans.ResultCode > 0) {
- g_DongleInfos[nIndex].profileList.Clear();
- g_DongleInfos[nIndex].profileList.Init(count);
- for(int i=0; i<count; ++i) {
- _UpdateProfileInfo(&g_DongleInfos[nIndex].profileList[i], ans, i);
- if(g_DongleInfos[nIndex].profileList[i].AccessInfo.IsDefault) {
- g_DongleInfos[nIndex].subInfo.strProfileName = g_DongleInfos[nIndex].profileList[i].strProfileName.GetData();
- }
- }
- }
- } else {
- Dbg("Invoke GetAPNConfigs failed, EC=%u", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 GetAPNConfigs 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- ErrorCodeEnum WirelessAgentSafeClient::SetDefaultSlot(int slotIdx)
- {
- if(!BeSureClientConnected()) {
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_SwitchDefaultSlot_Req req = {};
- WirelessDevAgent::WirelessDevService_SwitchDefaultSlot_Ans ans = {};
- req.SlotIdx = slotIdx;
- req.Subjoin = "";
- Dbg("Start to SetDefaultSlot...");
- _lastError = _client->SwitchDefaultSlot(req, ans, 10000);
- if(_lastError == Error_Succeed) {
- if(ans.ResultCode != 0) {
- Dbg("Detect SwitchDefaultSlot returned unexpected error: %d", ans.ResultCode);
- return (_lastError = (ErrorCodeEnum)ans.ResultCode);
- } else {
- Dbg("Invoke SwitchDefaultSlot successfully!");
- return _lastError;
- }
- } else {
- Dbg("Invoke SwitchDefaultSlot failed, EC=%u", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 SwitchDefaultSlot 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- ErrorCodeEnum WirelessAgentSafeClient::DeleteProfile(int profileID)
- {
- if(!BeSureClientConnected()) {
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_DeleteAPN_Req req = {};
- WirelessDevAgent::WirelessDevService_DeleteAPN_Ans ans = {};
- req.ProfileID = profileID;
- Dbg("Start to DeleteProfile...");
- _lastError = _client->DeleteAPN(req, ans, 10000);
- if(_lastError == Error_Succeed) {
- if(ans.ResultCode != 0) {
- Dbg("Detect DeleteAPN returned unexpected error: %d", ans.ResultCode);
- return (_lastError = (ErrorCodeEnum)ans.ResultCode);
- } else {
- Dbg("Invoke DeleteAPN successfully!");
- return _lastError;
- }
- } else {
- Dbg("Invoke DeleteAPN failed, EC=%u", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 DeleteAPN 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- ErrorCodeEnum WirelessAgentSafeClient::UpdateOrInsertAPN(int slotID, const AccessKeyInfo& newInfo, bool fApplyNow, int updateProfileID)
- {
- if(!BeSureClientConnected()) {
- _fsm->m_csErrMsg = CSimpleStringA::Format("连接管控实体失败(EC=%d)", _lastError);
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_UpdateOrInsertAPN_Req req = {};
- WirelessDevAgent::WirelessDevService_UpdateOrInsertAPN_Ans ans = {};
- req.ProfileID = updateProfileID;
- req.Name = newInfo.strProfileDesc;
- req.APNString = newInfo.strAccessString;
- req.Password = newInfo.strPassword;
- req.User = newInfo.strUserName;
- if(newInfo.usAPType == 0) {
- req.AuthType = -1;
- } else {
- req.AuthType = newInfo.usAPType;
- }
- req.Numeric = "";
- req.SlotNum = slotID;
- req.Server = "";
- req.Type = "";
- req.Port = 0;
- req.ApplyNow = fApplyNow;
- req.reserved1 = 0;
- req.reserved2 = "";
- req.reserved3 = 0;
- req.reserved4.Init();
- Dbg("Start to UpdateOrInsertAPN...");
- _lastError = _client->UpdateOrInsertAPN(req, ans, 10000);
- if(_lastError == Error_Succeed) {
- if(ans.ResultCode != 0) {
- Dbg("Detect UpdateOrInsertAPN returned unexpected error: %d", ans.ResultCode);
- return (_lastError = (ErrorCodeEnum)ans.ResultCode);
- } else {
- Dbg("Invoke UpdateOrInsertAPN successfully!");
- return _lastError;
- }
- } else {
- Dbg("Invoke UpdateOrInsertAPN failed, EC=%u", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 UpdateOrInsertAPN 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- ErrorCodeEnum WirelessAgentSafeClient::ConnectWithDefault()
- {
- if(!BeSureClientConnected()) {
- _fsm->m_csErrMsg = CSimpleStringA::Format("连接管控实体失败(EC=%d)", _lastError);
- return _lastError;
- }
- WirelessDevAgent::WirelessDevService_ConnectWlan_Req req = {};
- WirelessDevAgent::WirelessDevService_ConnectWlan_Ans ans = {};
- req.SSID = "";
- req.Password = "";
- req.ResetMode = true;
- req.HiddenMode = true;
- req.ForceUpdate = true;
- req.Reserved1 = 0;
- req.Reserved2 = "";
- Dbg("Start to ConnectWlan...");
- _lastError = _client->ConnectWlan(req, ans, 62000);
- if(_lastError == Error_Succeed) {
- if(ans.Result != 0) {
- Dbg("Detect ConnectWlan returned unexpected error: %d", ans.Result);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用接口失败:%d", ans.ErroCode);
- return (_lastError = (ErrorCodeEnum)ans.Result);
- } else {
- Dbg("Invoke ConnectWlan successfully!");
- return _lastError;
- }
- } else {
- Dbg("Invoke ConnectWlan failed, EC=%u", _lastError);
- _fsm->m_csErrMsg = CSimpleStringA::Format("调用 ConnectWlan 接口失败(EC=%d)", _lastError);
- }
- return _lastError;
- }
- BOOL WirelessAgentSafeClient::BeSureClientConnected(BOOL reConnnect)
- {
- if(_connected && (reConnnect || _lastError == Error_NetBroken)) {
- CloseSession();
- }
- if(_client == NULL) {
- EnterCriticalSection(&cs_4_connect);
- if(_client == NULL) {
- _client = new WirelessDevClient(_fsm->GetEntityBase());
- }
- LeaveCriticalSection(&cs_4_connect);
- }
- if(!_connected) {
- EnterCriticalSection(&cs_4_connect);
- if(!_connected) {
- Dbg("Start to connect WirelessDevAgent.");
- ErrorCodeEnum ec = _client->Connect();
- if(ec == Error_Succeed) {
- _connected = TRUE;
- } else {
- Dbg("Connect to WirelessDevAgent failed, EC=%u", ec);
- _fsm->m_csErrMsg = CSimpleStringA::Format("连接管控实体失败(EC=%d)", _lastError);
- _lastError = Error_Break;
- }
- }
- LeaveCriticalSection(&cs_4_connect);
- }
- return _connected;
- }
- void WirelessAgentSafeClient::CloseSession()
- {
- EnterCriticalSection(&cs_4_connect);
- if(_connected) {
- Dbg("close session for WirelessDevAgent client.");
- _client->GetFunction()->CloseSession();
- _connected = FALSE;
- }
- if(_client != NULL) {
- _client->SafeDelete();
- _client = NULL;
- }
- LeaveCriticalSection(&cs_4_connect);
- }
- void WirelessAgentSafeClient::_UpdateProfileInfo(
- ptMbnProfileInfo profileInfo, WirelessDevAgent::WirelessDevService_GetAPNConfigs_Ans& ans, int i)
- {
- InitAccessKeyInfo(&profileInfo->AccessInfo);
- profileInfo->bUserDefined = true;
- profileInfo->AccessInfo.usCntType = CNT_ANDROID;
-
- profileInfo->AccessInfo.strProfileDesc = ans.Name[i];
- profileInfo->AccessInfo.strPhoneNum = ans.Numeric[i];
- profileInfo->strProfileName = CSimpleStringA::Format("%d", ans.ProfileID[i]);
- profileInfo->AccessInfo.IsDefault = ans.ProfileState[i] != 0;
- profileInfo->AccessInfo.strAccessString = ans.APNString[i];
- profileInfo->AccessInfo.strUserName = ans.User[i];
- Dbg("Profile ADD: %s - Actived: %d", (LPCTSTR)profileInfo->strProfileName, ans.ProfileState[i]);
- Dbg("profile desc: %s", (LPCTSTR)profileInfo->AccessInfo.strProfileDesc);
- profileInfo->AccessInfo.usAPType = 0;
- if(ans.AuthType[i] == -1) {
- } else {
- //1 PAP;2 CHAP;3 PAPorCHAP
- if(ans.AuthType[i] & 1) {
- profileInfo->AccessInfo.usAPType |= AUPR_PAP;
- }
- if(ans.AuthType[i] & 2) {
- profileInfo->AccessInfo.usAPType |= AUPR_CHAP;
- }
- }
- profileInfo->AccessInfo.strPassword = ans.Password[i];
- return;
- }
- }
|