⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mobilesyncdlg.cpp

📁 A program that communicates with mobile, but need data line
💻 CPP
📖 第 1 页 / 共 4 页
字号:
					m_nOffset += m_nLength;
					if (m_nOffset < m_nfSize)
					{
						//send download data command
						if ((m_nfSize-m_nOffset) > 1500)
						{
							m_nLength = 1500;
						}
						else
						{
							m_nLength = m_nfSize - m_nOffset;
						}
						memcpy(Cmd+2, &m_nOffset, 4);
						memcpy(Cmd+6, &m_nLength, 4);
						memcpy(Cmd+10, m_pfBuff+m_nOffset, m_nLength);
						
						OutText("Send Download Data Command.\n");
						comm_send_command(Cmd, m_nLength+10);
						
					}
					else
					{
						//send download data done command.
						OP_UINT8 Cmd[3] = {0x01, 0x05, 0x00};
						free(m_pfBuff);
						m_pfBuff = NULL;
						m_nfSize = 0;
						m_nOffset = 0;
						m_nLength = 0;
						m_strfName = "";
						
						OutText("Send Download Done Command.\n");
						comm_send_command(Cmd, m_nLength+6);
					}
				}
				

				///////////////////////////////////////////////////////////	
				//receive download done rsp
				//melody
				if ((tempBuffer[0] == 0x01) && (tempBuffer[1] == 0x25) && (tempBuffer[2] == 0x00))
				{
					OutText("DownLoad File to Phone Succeed.");
				}

				/////// PBOOK /////////
				//////////////////////////////////////////////////////
				//get the status
				if ((tempBuffer[0] == 0x02) && (tempBuffer[1] == 0x21) && (tempBuffer[2] == 0x00))
				{
					OP_UINT16 total, used;
					CString strText;
					memcpy(&total, tempBuffer+3, 2);
					memcpy(&used, tempBuffer+5, 2);
					strText.Format("PhoneBook Status Total=%d, Used=%d\n", total, used);
					OutText(strText);
				}
				
				//phone read
				if ((tempBuffer[0] == 0x02) && (tempBuffer[1] == 0x22) && (tempBuffer[2] == 0x00))
				{
					OP_UINT16 RestLen, ItemRestLen;
					OP_UINT16 ItemLen;
					OP_UINT8 *pItem;
					CString strtext;
					OP_UINT8 Tag;
					OP_UINT16 TagLen;
					PBOOK_ITEM_T pbitem;

					RestLen = DataLen-3;
					while (RestLen>2)
					{
						memset(&pbitem, 0x00, sizeof(pbitem));
						memcpy(&ItemLen, tempBuffer+DataLen-RestLen, 2);
						pItem = tempBuffer+DataLen-RestLen + 2;

						memcpy(&pbitem.Device, pItem, 1);
						memcpy(&pbitem.Record, pItem+1, 2);

						ItemRestLen = ItemLen-3;
						while(ItemRestLen>0)
						{
							memcpy(&Tag, pItem+ItemLen-ItemRestLen, 1);
							memcpy(&TagLen, pItem+ItemLen-ItemRestLen+1, 2);
							switch (Tag)
							{
							case 1:
								memcpy(pbitem.Name, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 2:
								memcpy(pbitem.Mobile, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 3:
								memcpy(pbitem.Home, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 4:
								memcpy(pbitem.Office, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 5:
								memcpy(pbitem.Email, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 6:
								memcpy(pbitem.Qq, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 7:
								memcpy(pbitem.Memo, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 8:
								memcpy(&pbitem.Group, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
							case 9:
								memcpy(&pbitem.Portrait, pItem+ItemLen-ItemRestLen+3, TagLen);
								break;
									
							}

							ItemRestLen = ItemRestLen - TagLen - 3;
						}
						
						OutPbook(&pbitem);
						RestLen =RestLen - ItemLen - 2;
					}
				}

				//Portrait
				if ((tempBuffer[0] == 0x02) && (tempBuffer[1] == 0x27) && (tempBuffer[2] == 0x00))
				{
					char ftype[4];
					memcpy(ftype, tempBuffer+3, 4);
					memcpy(&m_nfSize, tempBuffer+7, 4);

					OP_UINT32 length;
					OP_UINT8 Cmd[10] = {0x02, 0x08};
					m_nOffset = 0;
					if (m_nfSize > 1500)
					{
						length = 1500;
					}
					else
					{
						length = m_nfSize;
					}
					
					memcpy(Cmd+2, (char*)&m_nOffset, 4);
					memcpy(Cmd+6, (char*)&length, 4);
					
					if (m_pfBuff != NULL) free(m_pfBuff);
					m_pfBuff = (OP_UINT8*)malloc(m_nfSize);
					
					OutText("Send Get Portrait Data Command.\n");
					comm_send_command(Cmd, sizeof(Cmd));
				}


				//receive portrait data rsp
				if ((tempBuffer[0] == 0x02) && (tempBuffer[1] == 0x28) && (tempBuffer[2] == 0x00))
				{
					OP_UINT32 length;
					
					memcpy(m_pfBuff+m_nOffset, tempBuffer+3, DataLen-5);
					m_nOffset += DataLen-5;
					
					if (m_nOffset < m_nfSize)
					{
						//send next upload data
						OP_UINT8 Cmd[10] = {0x02, 0x08};
						
						if ((m_nfSize-m_nOffset) > 1500)
						{
							length = 1500;
						}
						else
						{
							length = m_nfSize - m_nOffset;
						}
						memcpy(Cmd+2, &m_nOffset, 4);
						memcpy(Cmd+6, &length, 4);
						
						OutText("Send Get Portrait Data Command.\n");
						comm_send_command(Cmd, sizeof(Cmd));
					}
					else
					{
						//send upload done
						OP_UINT8 Cmd[2] = {0x02, 0x09};
						CFile f("e:\\portrait.jpg", CFile::modeCreate|CFile::modeWrite);
						f.Write(m_pfBuff, m_nfSize);
						f.Close();
						OutText("Upload Data Finished.\n");
						OutText("Save to File e:\\portrait.jpg");
						OutText(m_strfName);
						OutText("\n");
						
						
						
						if (m_pfBuff != OP_NULL) 
						{
							free(m_pfBuff);
							m_pfBuff = NULL;
						}
						m_nfSize = 0;
						m_nOffset = 0;
						m_strfName = "";
						
						OutText("Send Upload Done Command.\n");
						comm_send_command(Cmd, sizeof(Cmd));	
					}
					
				}

				/////// SMS /////////
				//////////////////////////////////////////////////////
				//get the status
				if ((tempBuffer[0] == 0x03) && (tempBuffer[1] == 0x21) && (tempBuffer[2] == 0x00))
				{
					WORD Total, Used;
					memcpy(&Total, tempBuffer+3, 2);
					memcpy(&Used, tempBuffer+5, 2);
					CString strText;
					strText.Format("Total = %d, Used = %d \n", Total, Used);
					OutText(strText);
				}

				//////////////////////////////////////////////////////
				//read
				if ((tempBuffer[0] == 0x03) && (tempBuffer[1] == 0x22) && (tempBuffer[2] == 0x00))
				{
					OP_UINT16 RestLen;
					SMS_ITEM_T smsitem;
					CHAR Content[2000];
					
					
					RestLen = DataLen-3;
					while (RestLen>2)
					{
						memset(&smsitem, 0x00, sizeof(smsitem));
						memcpy(&smsitem.ListType, tempBuffer+DataLen-RestLen, 1);
						memcpy(&smsitem.StorageType, tempBuffer+DataLen-RestLen+1, 1);
						memcpy(&smsitem.MsgType, tempBuffer+DataLen-RestLen+2, 1);
						memcpy(&smsitem.MsgRef, tempBuffer+DataLen-RestLen+3, 1);
						memcpy(&smsitem.Status, tempBuffer+DataLen-RestLen+4, 1);
						memcpy(smsitem.ServiceCenter, tempBuffer+DataLen-RestLen+5, 22);
						memcpy(smsitem.TimeStamp, tempBuffer+DataLen-RestLen+27, 7);
						memcpy(smsitem.Number, tempBuffer+DataLen-RestLen+34, 22);
						memcpy(&smsitem.MsgLen, tempBuffer+DataLen-RestLen+56, 4);
						if (smsitem.MsgLen > 0)
						{
							memset(Content, 0x00, 2000);
							memcpy(Content, tempBuffer+DataLen-RestLen+60, smsitem.MsgLen);
							smsitem.pContent = (OP_UINT8*)Content;
						}
						else
						{
							smsitem.pContent = NULL;
						}
						
						
						OutSms(&smsitem);
						RestLen =RestLen - smsitem.MsgLen - 60;
					}
				}
				
			}
			else
			{
				OutText("Checksum Error!\n");
			}
		}
		else
		{
			OutText("Base64 Decode Error!\n");
		}


		
	}
}


void CMobileSyncDlg::OutText(LPCSTR pText)
{
	int nLen = m_EditCtrl.GetWindowTextLength();
	m_EditCtrl.SetSel(nLen, nLen);
	m_EditCtrl.ReplaceSel(pText);
	
	
}

void CMobileSyncDlg::OutText(BYTE btText)
{
	CString strText;
	if (btText > 0x0f)
		{
			strText.Format("%x", btText);
		}
		else
		{
			strText.Format("0%x", btText);
		}
		
		int nLen = m_EditCtrl.GetWindowTextLength();
		m_EditCtrl.SetSel(nLen, nLen);
		m_EditCtrl.ReplaceSel(strText);
	
}




void CMobileSyncDlg::OnOpenPort() 
{
	// TODO: Add your control notification handler code here
	//OutText("Pressed open port button.\n");

	//if(m_comm.GetPortOpen()) m_comm.SetPortOpen(FALSE);
	CString string;
	UpdateData();

	if (m_nComboPort < 0)
	{
		MessageBox("Please select a com port.");
		return;
	}
	
	m_comm.SetCommPort(m_nComboPort + 1); //选择com4
	m_comm.SetSettings("115200,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
	m_comm.SetInBufferSize(2048);
	m_comm.SetInputMode(1);  // 以二进制方式检取数据
	m_comm.SetRThreshold(1); //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
	m_comm.SetInputLen(0); //设置当前接收区数据长度为0
	
	m_comm.SetPortOpen(TRUE);//打开串口

	if (m_comm.GetPortOpen())
	{
		m_comm.GetInput();	//先预读缓冲区以清除残留数据
		m_nMode = 0;	//at mode

		GetDlgItem(IDC_OPEN_PORT)->EnableWindow(TRUE);
//		GetDlgItem(IDC_SEND_CMD)->EnableWindow(TRUE);
//		GetDlgItem(IDC_CHANGE_MODE)->EnableWindow(TRUE);
		GetDlgItem(IDC_COUNT)->EnableWindow(TRUE);
		
		GetDlgItem(IDC_INQUIRE)->EnableWindow(TRUE);
		GetDlgItem(IDC_DELETE)->EnableWindow(TRUE);
		GetDlgItem(IDC_DOWNLOAD)->EnableWindow(TRUE);
		GetDlgItem(IDC_UPLOAD)->EnableWindow(TRUE);
		string.Format("Open COM%d port succeed.\n", m_nComboPort+1);
		OutText(string);

	}
	else
	{
		string.Format("Open COM%d port fail!\n", m_nComboPort+1);
		OutText(string);
	}
	
}

/*
void CMobileSyncDlg::OnSendCmd() 
{
	// TODO: Add your control notification handler code here
	CString strText;
	UpdateData(TRUE);
	
	if (m_strAtCmd != "")
	{
		m_strAtCmd += "\x0d";
		m_comm.SetOutput(COleVariant(m_strAtCmd));
		strText = "SEND ATCMD DATA: " + m_strAtCmd + "\n";
		OutText(strText);
	}
	else
	{
		MessageBox("Please input AT command.");
	}
}
*/



///////////////////////////////////////////////////////////////////////////////////
OP_BOOLEAN CMobileSyncDlg::comm_base64_decode
(
	const OP_UINT8 *pSrc, 
	OP_UINT16 SrcLen, 
	OP_UINT8 *pDest, 
	OP_UINT16 *pDestLen
)
{
	OP_UINT16 count;
	OP_UINT16 i, j;
	OP_UINT8 temp6asc[4];
	OP_UINT8 temp6bit[4];
	OP_UINT8 temp8bit[3];
	OP_UINT8 *ptr;
	
	if ((pSrc == OP_NULL) || (pDest == OP_NULL))
	{
		return OP_FALSE;
	}
	
	if (SrcLen == 0)
	{
		*pDestLen = 0;
		return OP_TRUE;
	}
	
	//check if destination buffer length is enough
	if (SrcLen%4 == 0)
	{
		count = SrcLen / 4;
		if (count*3 > *pDestLen)
		{
			return OP_FALSE;
		}
	}
	else
	{
		return OP_FALSE;
	}
	
	//decode the data
	for (i=0; i<count-1; i++)
	{
		memcpy(temp6asc, pSrc+4*i, 4);
		for (j=0; j<4; j++)
		{
			ptr = (OP_UINT8*)strchr((char*)base64_asc, temp6asc[j]);
			if (ptr != OP_NULL)
			{
				temp6bit[j] = ptr - base64_asc;
			}
			else
			{
				return FALSE;
			}
		}
		
		temp8bit[0] = temp6bit[0]<<2 | temp6bit[1]>>4;
		temp8bit[1] = temp6bit[1]<<4 | temp6bit[2]>>2;
		temp8bit[2] = temp6bit[2]<<6 | temp6bit[3];
		
		memcpy(pDest+3*i, temp8bit, 3);
	}
	memcpy(temp6asc, pSrc+4*(count-1), 4);

	for (j=0; j<4; j++)
	{
		ptr = (OP_UINT8*)strchr((char*)base64_asc, temp6asc[j]);
		if (ptr != OP_NULL)
		{
			temp6bit[j] = ptr - base64_asc;
			if (j == 1)
			{
				temp8bit[0] = temp6bit[0]<<2 |temp6bit[1]>>4;
			}
			else if (j == 2)
			{
				temp8bit[1] = temp6bit[1]<<4 | temp6bit[2]>>2;
			}
			else if (j == 3)
			{
				temp8bit[2] = temp6bit[2]<<6 | temp6bit[3];
			}
		}
		else if(temp6asc[j] == '=')
		{
			//temp6bit[j] = temp6asc[j];
			//op_memcpy(pDest+3*(count-1), temp8bit, j+1);
			//*pDestLen = count * 3 - (4-j);
			break;
		}
		else
		{
			return OP_FALSE;
		}
		
	}
	
	memcpy(pDest+3*(count-1), temp8bit, j-1);
	*pDestLen = count*3 - (4-j) ;
	return OP_TRUE;
	
	/*
	for (j=0; j<4; j++)
		{
			ptr = (OP_UINT8*)strchr((char*)base64_asc, temp6asc[j]);
			if (ptr != OP_NULL)
			{
				temp6bit[j] = ptr - base64_asc;
			}
			else if(temp6asc[j] == '=')
			{
				temp6bit[j] = temp6asc[j];
			}
			else
			{
				return OP_FALSE;
			}
		}
		temp8bit[0] = temp6bit[0]<<2 |temp6bit[1]>>4;
		if (temp6bit[2] == '=')
		{
			memcpy(pDest+3*(count-1), temp8bit, 1);
			*pDestLen = count * 3 - 2;
			return OP_TRUE;
		}
		temp8bit[1] = temp6bit[1]<<4 | temp6bit[2]>>2;
		if (temp6bit[3] == '=')
		{
			memcpy(pDest+3*(count-1), temp8bit, 2);
			*pDestLen = count * 3 - 1;
			return OP_TRUE;
		}
		temp8bit[2] = temp6bit[2]<<6 | temp6bit[3];
		
		memcpy(pDest+3*(count-1), temp8bit, 3);
		*pDestLen = count * 3;
		return OP_TRUE;*/
	
	
}



OP_BOOLEAN CMobileSyncDlg::comm_base64_encode
(
	const OP_UINT8 *pSrc, 
	OP_UINT16 SrcLen, 
	OP_UINT8 *pDest, 
	OP_UINT16 *pDestLen
)
{
	OP_UINT16 count;
	OP_UINT16 remainder;
	OP_UINT16 i;
	OP_UINT8 temp8bit[3];
	OP_UINT8 temp6bit[4];
	OP_UINT8 temp6asc[4];
	
	if ((pSrc == OP_NULL) || (pDest == OP_NULL))
	{
		return OP_FALSE;
	}
	
	if (SrcLen == 0)
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -