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

📄 sockettest.c

📁 MTK联网
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "MMI_features.h"
/* MTK End */


#if !defined(__MTK_TARGET__)
#include <windows.h>
#endif 

/**********************/
/*****  Header Files *****/
/**********************/

#ifdef FIRST_PASS
#include "BuildCfg.h"
#endif 

#include "CustDataRes.h"
#include "EventsGprot.h"
#include "historyGprot.h"
#include "ProtocolEvents.h"
#include "CustMenuRes.h"
#include "wgui_categories.h"
#include "HardwareEvents.h"
#include "Thrdosinterface.h"
#include "OSThreadInterfaceGprot.h"

#include "TimerEvents.h"
#include "MainMenuDef.h"
#include "EngineerModeDef.h"
#include "EngineerModeType.h"
#include "EngineerModeProt.h"
#include "PixtelDataTypes.h"
#include "CustomCfg.h"
#include "Unicodexdcl.h"
#include "audioInc.h"
#include "IdleAppDef.h"
#include "BootUp.h"
#include "GlobalDefs.h"

#include "soc_api.h"
#include "app2soc_struct.h"
#include "EngineerModeSocketApp.h"
#include "EngineerModeSocketAppPing.h"

#include "OrganizerDef.h"


soc_demo_app_transaction_struct *test_soc_demo_transaction = NULL;


void mmi_test_socket();
int test_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);
void tst_EmSocketRsp(mmi_soc_demo_app_enum app_id_enum, int result, char *response, int response_len);
kal_bool test_en_soc_demo_app_create_socket();
int test_en_soc_demo_http_send_request(void);
void test_em_soc_demo_app_socket_notify(void *inMsg);

void tst_em_soc_demo_app_start_timer(void);
void tst_em_soc_demo_app_timer_handler(void);


void tst_en_soc_demo_tcp_app_recv_response(void);

void tst_en_soc_output_result(int ret, char *out_str, int len);


int tst_mmi_soc_demo_app_request_abort(void);

void tst_em_soc_demo_app_stop_timer(void);

extern void StartTimer(U16 timerid, U32 delay, FuncPtr funcPtr);
extern void StopTimer(U16 timerid);




void tst_EmSocketRsp(mmi_soc_demo_app_enum app_id_enum, int result, char *response, int response_len)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/
	U16 i,j,k;
	U16 databgin, namebgin,preClsbgin,curpricebgin;
	U16 pack_byte;
	U8 socknum;
	/*----------------------------------------------------------------*/
	/* Object											  */
	/*----------------------------------------------------------------*/

	for(i = 0; i < response_len; i++)
	{
		if(response[i] == 0x20 && response[i+1] == 0x20 )
		{
			pack_byte = response[i-2];
			pack_byte = ((pack_byte & 0x00ff)<<8) + response[i -1];
			kal_prompt_trace(MOD_WAP,"##########pack_byte[%d]",pack_byte);
			break;
		}
	}
	
	databgin = i + 2;
	namebgin = databgin + 6;
	//curpricebgin = namebgin + 52;
	pack_byte -=2;
	socknum = pack_byte / 258;
	kal_prompt_trace(MOD_WAP,"##########socknum[%d]",socknum);
	
	for(j = 0; j < socknum; j++)
	{
	  U8 cur[10][4];
	  U8 preCls[10][4];
	  U8 stockname[10][24];
	  U16 index = 0;
	  float preprice[10];
	  float currentprice[10];
	  float updown[10];
	  ////////////////////////////////////name/////////////////////////////

	  for(k = namebgin + j*258; k <namebgin+ j*258 + 24; k++)
	  {
		stockname[j][index++] = response[k];
	  }
	  ////////////////////////////////////preprice////////////////////////////
	  {  
		  index = 0;
		  k = k;
		  preClsbgin =k;
	  }
	  for(;k <preClsbgin + 4;k++ )
	  {
		  preCls[j][index++] = response[k];
		  kal_prompt_trace(MOD_WAP,"#############preprice = %x",response[k]);
		
	  }
	  {
		U32 prevalue;
		prevalue = (preCls[j][0]<<24) + (preCls[j][1]<<16) +(preCls[j][2]<<8) + (preCls[j][3]);
		preprice[j] = ((float)prevalue) /10000;

		
		 kal_prompt_trace(MOD_WAP,"---------------------prevalue = %d",prevalue);
		
	  }
	  
	  ////////////////////////////////////current price/////////////////////////

	  {  
		  index = 0;
		  k = k + 24;
		  curpricebgin =k;
	  }
	  
	   for(; k < curpricebgin + 4; k++)
	  {
		cur[j][index++] = response[k];
	  }
	   
	 {
		U32 value;
		value = ( cur[j][0]<<24) + (cur[j][1]<<16) + (cur[j][2]<<8) + (cur[j][3]);
		currentprice[j] = ((float)value) /10000;
	 }
	  /////////////////////////////////////end///////////////////////////////

	}
	   
}


void mmi_test_socket(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/

#ifdef MMI_ON_HARDWARE_P
	U8 emSocketAccountId = custom_get_csd_profile_num();
#else 
	U8 emSocketAccountId = 5;
#endif 
	mmi_soc_demo_app_result emSocketTmpResult = EN_SOC_SUCCESS;
	S8 AscTmp[16];

	kal_prompt_trace(MOD_WAP," mmi_test_socket __________________ emSocketAccountId=%d",emSocketAccountId);
		emSocketTmpResult = test_mmi_soc_demo_app_request(
								HTTP,
								10,
								NULL,
								NULL,//emSocketInputdata,
								0,//strlen(emSocketInputdata),
								NULL,
								0,
							  tst_EmSocketRsp);
}




int test_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)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body													  */
	/*----------------------------------------------------------------*/
	if (test_soc_demo_transaction)
	{
		kal_prompt_trace(MOD_WAP,"###########################Transaction existed");
#ifdef MMI_ON_HARDWARE_P
		kal_print("Transaction existed!!");
#endif 
		return EN_SOC_BUSY;
	}
	else
	{
		if ((test_soc_demo_transaction = OslMalloc(sizeof(soc_demo_app_transaction_struct))) == NULL)
		{
    #ifdef MMI_ON_HARDWARE_P
			kal_print("No memory");
    #endif 
			en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
			return EN_SOC_NO_MEMORY;
		}
		else
		{
			memset(test_soc_demo_transaction, 0, sizeof(soc_demo_app_transaction_struct));
			if ((test_soc_demo_transaction->rcvd_buffer = OslMalloc(MAX_RCV_BUFFER_SIZE)) == NULL)
			{
        #ifdef MMI_ON_HARDWARE_P
				kal_print("No memory");
        #endif 
				en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
				return EN_SOC_NO_MEMORY;
			}
			else
			{
				if ((test_soc_demo_transaction->snd_buffer = OslMalloc(MAX_SND_BUFFER_SIZE)) == NULL)
				{
            #ifdef MMI_ON_HARDWARE_P
					kal_print("No memory");
            #endif 
					en_soc_output_result(EN_SOC_NO_MEMORY, NULL, 0);
					return EN_SOC_NO_MEMORY;
				}
				else
				{
					/* Set	initial values to test_soc_demo_transaction */
					memset((kal_int8*) test_soc_demo_transaction->rcvd_buffer, 0, MAX_RCV_BUFFER_SIZE);
					memset((kal_int8*) test_soc_demo_transaction->snd_buffer, 0, MAX_SND_BUFFER_SIZE);

					test_soc_demo_transaction->soc_demo_app_id = app_id;
					test_soc_demo_transaction->nwt_acount_id = account_id;
					if (server_ip_address)
					{
						memcpy(test_soc_demo_transaction->server_ip_addr.addr, server_ip_address, 4);
						test_soc_demo_transaction->server_ip_addr.addr_len = 4;
					}
					test_soc_demo_transaction->url = (kal_int8*) url;
					test_soc_demo_transaction->url_len = url_len;
					test_soc_demo_transaction->snd_counter = 0;
					test_soc_demo_transaction->rcvd_counter = 0;
					test_soc_demo_transaction->callback = callback;
					if (echo_text)
					{
						if (echo_text_len > MAX_SND_BUFFER_SIZE)
						{
							memcpy(test_soc_demo_transaction->snd_buffer, echo_text, MAX_SND_BUFFER_SIZE);
							test_soc_demo_transaction->snd_data_len = MAX_SND_BUFFER_SIZE;
						}
						else
						{
							memcpy(test_soc_demo_transaction->snd_buffer, echo_text, echo_text_len);
							test_soc_demo_transaction->snd_data_len = echo_text_len;
						}

						OslMfree(echo_text);
					}
					if (test_en_soc_demo_app_create_socket() == KAL_FALSE)
					{
						return EN_SOC_NO_MEMORY;
					}
					else
						kal_prompt_trace(MOD_WAP,"_________________________ CREATE SOCKET OK");
					switch (app_id)
					{
						case HTTP:
						{
							test_soc_demo_transaction->state = HTTP_DNS_QUERY;
							return test_en_soc_demo_http_send_request();
						}
						default:
						{
							return EN_SOC_UNKNOWN_APP;
						}

					}
				}

			}
		}
	}
}


kal_bool test_en_soc_demo_app_create_socket()
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/
	kal_uint8 val = 1;

	/*----------------------------------------------------------------*/
	/* Code Body													  */
	/*----------------------------------------------------------------*/
	if (test_soc_demo_transaction)
	{
		test_soc_demo_transaction->state = SOC_CREATING;
		switch (test_soc_demo_transaction->soc_demo_app_id)
		{
			case HTTP:
			{
				test_soc_demo_transaction->socket_id = soc_create(PF_INET, SOCK_STREAM, 0, MOD_MMI, test_soc_demo_transaction->nwt_acount_id);
				if (test_soc_demo_transaction->socket_id >= 0)
				{
					if (soc_setsockopt(test_soc_demo_transaction->socket_id, SOC_NBIO, &val, sizeof(val)) < 0)
					{
                #ifdef MMI_ON_HARDWARE_P
						kal_print("Set socket to nonblock mode error !!");
                #endif 
						return KAL_FALSE;
					}
					val = SOC_READ | SOC_WRITE | SOC_CLOSE | SOC_CONNECT;
					if (soc_setsockopt(test_soc_demo_transaction->socket_id, SOC_ASYNC, &val, sizeof(val)) < 0)
					{
                #ifdef MMI_ON_HARDWARE_P
						kal_print("Set socket to nonblock mode error !!");
                #endif 
						return KAL_FALSE;
					}
					test_soc_demo_transaction->state = SOC_CREATED;
					return KAL_TRUE;
				}
				else
				{
					return KAL_FALSE;
				}
				break;
			}
			default:
				return KAL_FALSE;
		}
	}
	else
	{
		return KAL_FALSE;
	}
}

int test_en_soc_demo_http_send_request(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body													  */
	/*----------------------------------------------------------------*/
	if (test_soc_demo_transaction)
	{
		if (test_soc_demo_transaction->state == HTTP_DNS_QUERY &&
			(soc_demo_app_http_url_struct*) test_soc_demo_transaction->url_parsed_info == NULL)
		{
			kal_prompt_trace(MOD_WAP," test_en_soc_demo_http_send_request __________________________________");
			test_soc_demo_transaction->server_ip_addr.addr[0] = 10;
			test_soc_demo_transaction->server_ip_addr.addr[1] = 0;
			test_soc_demo_transaction->server_ip_addr.addr[2] = 0;
			test_soc_demo_transaction->server_ip_addr.addr[3] = 172;
			test_soc_demo_transaction->server_ip_addr.port = 80;
			
			test_soc_demo_transaction->rcvd_counter = 0;
			test_soc_demo_transaction->server_ip_addr.addr_len = 4;
			test_soc_demo_transaction->state = HTTP_TCP_CON_CREATING;
			test_en_soc_demo_http_send_request();

			
		}
		else if (test_soc_demo_transaction->state == HTTP_TCP_CON_CREATING)
		{
			kal_int8 ret;
			char CRLF[3] = {0x0d, 0x0a, 0x00};
			
			test_soc_demo_transaction->state = HTTP_TCP_CON_CREATED;
			test_soc_demo_transaction->snd_data_len=0;

			strcat(test_soc_demo_transaction->snd_buffer,"GET ");
			test_soc_demo_transaction->snd_data_len+=4;


			strcat((char*)test_soc_demo_transaction->snd_buffer,"http://61.135.159.21/cgi-bin/weather?024 ");				
			test_soc_demo_transaction->snd_data_len+=41; 			

			strcat((char*)test_soc_demo_transaction->snd_buffer,"HTTP/1.1");			
			test_soc_demo_transaction->snd_data_len+=8; 			

			strcat((char*)test_soc_demo_transaction->snd_buffer,CRLF);
			test_soc_demo_transaction->snd_data_len+=2;

			strcat((char*)test_soc_demo_transaction->snd_buffer,"Host: 10.0.0.172:80");
			test_soc_demo_transaction->snd_data_len+=19;

			strcat((char*)test_soc_demo_transaction->snd_buffer,CRLF);
			test_soc_demo_transaction->snd_data_len+=2; 
			

			strcat((char*)test_soc_demo_transaction->snd_buffer,"User-Agent: */*");
			test_soc_demo_transaction->snd_data_len+=15;

			
			strcat((char*)test_soc_demo_transaction->snd_buffer,CRLF);
			test_soc_demo_transaction->snd_data_len+=2;


			strcat((char*)test_soc_demo_transaction->snd_buffer,"Accept: */*");
			test_soc_demo_transaction->snd_data_len+=11;

			strcat((char*)test_soc_demo_transaction->snd_buffer,CRLF);
			test_soc_demo_transaction->snd_data_len+=2;
			
			strcat((char*)test_soc_demo_transaction->snd_buffer,"X-Online-Host: 61.135.159.21");
			test_soc_demo_transaction->snd_data_len+=28;
			
			strcat((char*)test_soc_demo_transaction->snd_buffer,CRLF);
			test_soc_demo_transaction->snd_data_len+=2; 


			strcat((char*)test_soc_demo_transaction->snd_buffer,"Connection: Keep-Alive");
			test_soc_demo_transaction->snd_data_len+=22;

			strcat((char*)test_soc_demo_transaction->snd_buffer,CRLF);

⌨️ 快捷键说明

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