示例#1
0
文件: handle.cpp 项目: bratkov/tmos
RES_CODE CHandle::tsk_read(CSTRING& str, unsigned int size)
{
	char * buf;

	str.clear();
	if(str.reserve(size) >= size)
	{
		buf = (char*)str.c_str();
		res = tsk_read(buf, size);
		buf[size-len] = 0;
		str.m_set_size(strlen(buf));
	} else
		res = RES_OUT_OF_MEMORY;
	return res;
}
示例#2
0
NET_CODE esp8266_module::wifi_esp8266_init_net(CSocket * sock)
{
	NET_CODE res;
	CSTRING cmd;
	wifi_AP_t AP;
	uint32_t sig = 0;
	bool found;

	res = wifi_send_cmd("+CIFSR", 50);

	for(int i =0; i < 5; i++)
	{
		found = false;
		cmd_state |= WIFI_CMD_STATE_ROW_STOP;
		res = wifi_send_cmd("+CWLAP", 15);
		cmd_state &= ~WIFI_CMD_STATE_ROW_STOP;

		do
		{
			if(sig)
			{
				process_input(sig, "+CWLAP");
				res = cmd_state;
			}

			if (res >= WIFI_CMD_STATE_OK)
				break;

			if(res & WIFI_CMD_STATE_RETURNED)
			{
				cmd_state = res & ~WIFI_CMD_STATE_RETURNED;
				if(!found)
				{
					res = wifi_on_get_AP(this, sock, &AP);
					if(res == RES_OK)
						found = true;
				}
				row_start = 0;
				row_end = 0;
			}

			sig = tsk_resume_wait_signal(rcv_hnd.signal);
		} while(sig);

		if(cmd_state & WIFI_CMD_STATE_OK)
		{
			if(!found)
				res = NET_ERR_WIFI_NET_NAME;
			else
				res = NET_OK;
			break;
		}
	}
	if (res != NET_OK)
		return wifi_error(res);

	// If the wifi is already connected to a network different than the one
	// requested - return NET_IDLE - unavailable
	if (NET_OK == wifi_get_network_name(cmd))
	{
		if (0 != strcmp(cmd.c_str(), AP.name.c_str()))
		{
			return NET_IDLE;
		}
	}

	cmd.format("+CWJAP=\"%s\",\"%s\"", AP.name.c_str(), AP.pass.c_str());
	for(int i=0; i < 3; i++)
	{
		res = wifi_send_cmd(cmd.c_str(), 50);
		if (WIFI_CMD_STATE_OK == res)
		{
			res = wifi_send_cmd("+CIFSR", 50);
			if (WIFI_CMD_STATE_ROK == res)
			{
				connected_network_name = AP.name;
				return NET_OK;
			}
			break;
		}
	}
	return NET_ERR_WIFI_REGISTER;

	if (WIFI_CMD_STATE_OK == res)
	{
		res = wifi_send_cmd("+CIFSR", 50);
		if (WIFI_CMD_STATE_ROK == res)
		{
			connected_network_name = AP.name;
			wifi_send_cmd("+CIPSTART=\"TCP\",\"www.tmos-arm.com\",80", 50);
			//wifi_send_cmd("+CIPSTART=\"TCP\",\"192.168.147.100\",6112", 50);
			cmd.clear();
			cmd.append("+CIPSEND=");
			cmd.appendf("%d", strlen(message) + 1);
		    wifi_send_cmd(cmd.c_str(), 200);

			// make sure the handle is working if it is open
		    while(rcv_hnd.res < FLG_BUSY)
		    {
				process_input(0, NULL);
		    }

			wifi_sleep(20); //(the recommended value is at least 20 ms)

		    // make sure no URC is coming and the buf is empty
		    if( cmd_state & WIFI_CMD_STATE_STARTED)
		    {
		        if (tsk_wait_signal(rcv_hnd.signal, 8192))
		        {
		    		do
		    		{
		    			process_input(rcv_hnd.signal, NULL);

		    		} while ( (cmd_state & WIFI_CMD_STATE_STARTED) &&
		    				tsk_resume_wait_signal(rcv_hnd.signal) );
		        }
		    }
		    //start clean
		    cmd_state &= (WIFI_CMD_STATE_ROW_STOP | WIFI_CMD_STATE_HND);
		   	row_start = row_end = 0;

		    snd_hnd.tsk_write(message, strlen(message) + 1, WIFI_WRITE_TOT);
			return NET_OK;
		}
	}
	return wifi_net_error(res);
}