MTKSocket例子分析
MTK自带了一个,在EngineerModeSrc.c 和 EngineerModeSocketDemoApp.c 里,里面有好几个socket使用的例子,DNS,ECHO,DAYTIME,TRACERT。文件 EngineerModeSrc.c 里面主要是界面显示逻辑,EngineerModeSocketDemoApp.c 里面是真正的socket 逻辑代码。
在 EngineerModeSrc.c 里面,选择了相应的选项后,最终会走到函数 EntryEmSocketInProgress 显示正在进行网络连接的界面,它调用 EmSocketSendReq 函数来简单区分,是选择了哪种应用。EmSocketSendReq 根据相关选择信息,调用mmi_soc_demo_app_request进行真正的socket处理。
View Code
// 这个函数主要是根据不同配置,初始化全局信息。
// app_id, 是哪一种操作 DNS,HTTP等等
// account_id , gprs 帐户id
// server_ip_address ip地址
// url url 地址,根据不同类型,确定使用ip地址还是url
// echo_txt ,如果是 ECHO,这个表示要发送的内容
// callback 操作的回调函数
int mmi_soc_demo_app_request( mmi_soc_demo_app_enum app_id, int account_id, char server_ip_address[4],
char *url, int url_len, char *echo_text, int echo_text_len, mmi_soc_demo_app_rsp_t callback)
{
// soc_demo_transaction 是一个全局变量,存放当前信息
// 判断是否有连接正在进行
if (soc_demo_transaction)
{
kal_print("Transaction existed!!");
return EN_SOC_BUSY;
}
else
{
// 分配内存,如果出错调用
en_soc_output_result 通知注册的函数
// 同时释放相应内存,防止泄露。
if ((soc_demo_transaction = OslMalloc(sizeof(soc_demo_app_transaction_struct))) == NULL)
{
kal_print("No memory");
en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
return EN_SOC_NO_MEMORY;
}
else
{
// 分配收发内容的
buffer memset(soc_demo_transaction, 0, sizeof(soc_demo_app_transaction_struct));
if ((soc_demo_transaction->rcvd_buffer = OslMalloc(MAX_RCV_BUFFER_SIZE)) == NULL)
{
kal_print("No memory");
en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
return EN_SOC_NO_MEMORY;
}
else
{
if ((soc_demo_transaction->snd_buffer = OslMalloc(MAX_SND_BUFFER_SIZE)) == NULL)
{
kal_print("No memory"); en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
return EN_SOC_NO_MEMORY;
}
else
{
/* Set initial values to soc_demo_transaction */
memset((kal_int8*) soc_demo_transaction->rcvd_buffer, 0, MAX_RCV_BUFFER_SIZE);
memset((kal_int8*) soc_demo_transaction->snd_buffer, 0, MAX_SND_BUFFER_SIZE);
//计算接入点,具体下次分析
account_id = cbm_encode_app_id_data_account_id(account_id, app_id);
soc_demo_transaction->soc_demo_app_id = app_id;
soc_demo_transaction->nwt_acount_id = account_id;
// 保存相应信息
// 保存相应信息
if (server_ip_address)
{
memcpy(soc_demo_transaction->server_ip_addr.addr, server_ip_address, 4);
soc_demo_transaction->server_ip_addr.addr_len = 4;
}
soc_demo_transaction->url = (kal_int8*) url;
soc_demo_transaction->url_len = url_len;
soc_demo_transaction->snd_counter = 0;
soc_demo_transaction->rcvd_counter = 0;
soc_demo_transaction->callback = callback;
if (echo_text)
{
if (echo_text_len > MAX_SND_BUFFER_SIZE)
{
memcpy(soc_demo_transaction->snd_buffer, echo_text, MAX_SND_BUFFER_SIZE);
soc_demo_transaction->snd_data_len = MAX_SND_BUFFER_SIZE;
}
else
{
memcpy(soc_demo_transaction->snd_buffer, echo_text, echo_text_len);
soc_demo_transaction->snd_data_len = echo_text_len;
}
OslMfree(echo_text);
}
if (en_soc_demo_app_create_socket() == KAL_FALSE)
{
en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
return EN_SOC_NO_MEMORY;
}
// 根据不同id,进行操作
switch (app_id)
{
case HTTP:
{
soc_demo_transaction->state = HTTP_DNS_QUERY;
return en_soc_demo_http_send_request();
}
case DNS:
{
(void)en_soc_demo_get_host_by_name(DNS, (kal_uint8*) soc_demo_transaction->url);
return EN_SOC_SUCCESS;
}
case DAYTIME:
{
soc_demo_transaction->server_ip_addr.port = SOC_DAYTIME_RESVD_PORT;
soc_demo_transaction->snd_data_len = 10;
/* garbage data */
return en_soc_demo_udp_app_send_request();
}
case ECHO:
{
soc_demo_transaction->server_ip_addr.port = SOC_ECHO_RESVD_PORT;
return en_soc_demo_udp_app_send_request();
}
case TRACERT:
{
if (em_soc_icmp_init_soc() < 0)
{
return EN_SOC_ERROR;
}
memset(soc_demo_transaction->server_ip_addr.addr,0,MAX_SOCK_ADDR_LEN);
if ( em_soc_demo_app_get_addr_type((kal_uint8*)url, url_len, soc_demo_transaction-> server_ip_addr.addr) == SOC_ADDR_TYPE_DNAME )
{
soc_demo_transaction->state = HTTP_DNS_QUERY;
en_soc_demo_get_host_by_name(TRACERT, (kal_uint8*)soc_demo_transaction->url);
return EN_SOC_SUCCESS;
}
else
{
char str[64];
memset(str,0,64);
kal_sprintf( str, " %d.%d.%d.%d/n", soc_demo_transaction->server_ip_addr.addr[0], soc_demo_transaction->server_ip_addr.addr[1], soc_demo_transaction->server_ip_addr.addr[2],
soc_demo_transaction->server_ip_addr.addr[3] );
EmStartTraceRtResult(str);
soc_demo_transaction->state = REQ_SENT;
StartTimer(EM_GPRS_SOC_DEMO_APP_TIMER, SOC_DEMO_APP_POST_TIMEOUT, em_soc_icmp
_timer_hdlr);
em_soc_icmp_send_hdlr(SOC_ICMP_SEND_NORMAL);
return EN_SOC_SUCCESS;
}
}
default:
{
en_soc_output_result(EN_SOC_UNKNOWN_APP, NULL, 0);
return EN_SOC_UNKNOWN_APP;
}
}
}
}
}
}
}