Example #1
0
void esp8266_module::wifi_esp8266_socket_close(unsigned int sid)
{
	CSTRING cmd;

	if(get_socket_state(sid))
	{
		cmd.format("+CIPCLOSE=%u", sid);
		if(wifi_send_cmd(cmd.c_str(), 45) & WIFI_CMD_STATE_OK)
		{
			if( (sid < WIFI_ESP8266_MAX_SOCKETS) && alloc_sockets[sid])
			{
				pending_socket = alloc_sockets[sid];
				for(int i=0; i< 500; i++)
				{
					wifi_sleep(10);
					if(pending_socket->sock_state == SOCKET_OPEN)
					{
						TRACE_WIFI("\r\nUnlinked %d", sid);
						break;
					}
				}
				pending_socket = NULL;
			}
		}
	}
}
Example #2
0
CSTRING& CSTRING::insert (char ch, unsigned int index)
{
	CSTRING tmp (c_str(), index);
	tmp.append (ch);
	tmp.append (substr(index, length() - index));
	return (*this = tmp);
}
Example #3
0
/**
 * Read a line from the cached object
 * @param var - CSTRING where to store the line
 * @return RES_OK or error returned from load()
 */
RES_CODE CCache::readline(CSTRING& var)
{
	unsigned int res;
	int i;

	do
	{
		if (pos)
		{
			res = buf.c_str() + buf.length() - pos; //max size
			i = 0;
			while (res--)
			{
				if (!pos[i] || pos[i] == '\n')
				{
					//we found \n or \0
					var.append(pos, i);
					pos += i + 1;
					return RES_OK;
				}
				i++;
			}
			var.append(pos, i);
		}
		res = load();
	} while (res == RES_OK);

	return (res);
}
Example #4
0
/**
 * Test for substring
 * @param sub
 * @param pos
 * @return true if begins with sub
 */
unsigned int CSTRING::contains(CSTRING& sub, unsigned int pos) const
{
	unsigned int len;

	len = sub.length();
	if( pos+len <= length())
		if( !memcmp(c_str()+pos, sub.c_str(), len))
			return (len);
	return (0);
}
Example #5
0
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;
}
Example #6
0
/**
 * Concatenate two c-strings
 * @param s1
 * @param s2
 * @return CSTRING object
 */
CSTRING operator+(const CSTRING& s1, const CSTRING& s2 )
{
	unsigned int len1;
	unsigned int len2;

	len1 = s1.length();
	len2 = len1+ s2.length();

	CSTRING s(len2);

	if(s.storage.adr)
	{
		s.storage.ram->len = len2;
		strcpy(s.storage.ram->buf, s1.c_str());
		strcpy(s.storage.ram->buf + len1, s2.c_str());
	}
	return (s);
}
Example #7
0
/**
 * Concatenates char and CSTRING
 * @param c
 * @param s2
 * @return CSTRING object
 */
CSTRING operator+( char c, const CSTRING& s2 )
{
	unsigned int len1;
	unsigned int len2;

	len1 = c?1:0;
	len2 = len1 + s2.length();

	CSTRING res(len2);

	if(res.storage.adr)
	{
		res.storage.ram->len = len2;
		res.storage.ram->buf[0] = c;
		strcpy(res.storage.ram->buf + len1, s2.c_str());
	}
	return (res);
}
Example #8
0
/**
 * Concatenates CSTRING and char*
 * @param s1
 * @param s
 * @return CSTRING object
 */
CSTRING operator+( const CSTRING& s1, const char* s )
{
	unsigned int len1;
	unsigned int len2;

	len1 = s1.length();
	len2 = len1 + strlen(s);

	CSTRING res(len2);

	if(res.storage.adr)
	{
		res.storage.ram->len = len2;
		strcpy(res.storage.ram->buf, s1.c_str());
		strcpy(res.storage.ram->buf+ len1, s);
	}
	return (res);
}
Example #9
0
RES_CODE esp8266_module::wifi_sock_connect_url(CSocket* sock)
{
	CSTRING cmd;;

	for(int try_cnt=0; sock && try_cnt <3; try_cnt++ )
	{
		if(sock->sock_state & SOCKET_OPEN)
		{
			unsigned int sid = sock->sock_id;
			cmd.format("+CIPSTART=%u,\"%s\",\"%s\",%u", sid,
					(((sock_mode_t*)sock->mode.as_voidptr)->sock_type == IP_SOCKET_TCP)?"TCP":"UDP",
					sock->src.as_charptr,
					sock->dst.as_int);
			if (wifi_send_cmd(cmd.c_str(), 40) & WIFI_CMD_STATE_OK)
			{
				pending_socket = sock;
				for(int i=0; i<500; i++)
				{
					wifi_sleep(10);
					if(sock->sock_state == SOCKET_CONECTED)
						break;
				}
				pending_socket = NULL;
				received_size[sock->sock_id] = 0;
				if(sock->sock_state == SOCKET_CONECTED)
				{
					TRACE_WIFI("\r\nLink %d", sid);
					return RES_SIG_OK;
				}
				wifi_esp8266_socket_close(sid);
			}

			//Debug
			get_socket_state(sid);
		} else
		{
			wifi_net_error(NET_ERR_SOCK_CLOSED);
			return RES_SIG_ERROR;
		}
	}
	wifi_net_error(NET_ERR_SOCK_CONNECT);
	return RES_SIG_ERROR;
}
Example #10
0
/**
 * Concatenates CSTRING and char
 * @param s1
 * @param c
 * @return CSTRING object
 */
CSTRING operator+( const CSTRING& s1, char c )
{
	unsigned int len1;
	unsigned int len2;

	len1 = s1.length();
	len2 = len1 + c?1:0;

	CSTRING res(len2);

	if(res.storage.adr)
	{
		res.storage.ram->len = len2;
		strcpy(res.storage.ram->buf, s1.c_str());
		if(c)
		{
			res.storage.ram->buf[len2--] = 0;
			res.storage.ram->buf[len2] = c;
		}
	}
	return (res);
}
Example #11
0
/**
 * Reads a name from the cache
 *
 * Description:
 * 		Name	   		::=   	NameStartChar (NameChar)*
 * 		NameStartChar	::=   	":" | [A-Z] | "_" | [a-z]	//add "?"
 * 		NameChar	   	::=   	NameStartChar | "-" | "." | [0-9]
 * @param var
 * @return RES_OK if the char was skipped, RES_EOF if no more tags, errors..
 */
RES_CODE CCache::get_name(CSTRING& var)
{
	char ch;
	RES_CODE res;

	res = get_pc(ch);
	if (res == RES_OK)
	{
		if (ch == ':' || ch == '_' || ch == '?' || IS_ALPHA(ch))
		{
			var += ch;

			while (!var.empty())
			{
				res = getc(ch);
				switch (res)
				{
				case RES_OK:
					if (ch == ':' || ch == '_' || ch == '-' || ch == '.'
							|| IS_ALPHANUM(ch))
					{
						var += ch;
					}
					else
					{
						ungetc();
						return RES_OK;
					}
					break;

				case RES_EOF:
					return RES_OK;

				default:
					return (res);
				}

			}
			return RES_OUT_OF_MEMORY;
		}
		ungetc();
		res = RES_INVALID_DATA;
	}
	return (res);
}
Example #12
0
/**
 * Puts a string in the cache line
 * @param var
 * @return
 */
RES_CODE CCache::buffer(CSTRING &var)
{
	unsigned int len;

	if (!var.empty())
	{
		if (pos)
		{
			len = buf.length() - (pos - buf.c_str());
			if (len)
			{
				CSTRING s(pos, len);
				if (next.empty())
					next = s;
				else
					next = s + next;
			}
		}
		buf = var;
		pos = (char*) buf.c_str();
	}
	return RES_OK;
}
Example #13
0
RES_CODE esp8266_module::process_write(CSocket* sock)
{
	unsigned size;//, newsize, id;
//	unsigned int start_size, write_size, sock_state;
	CSTRING cmd;
	unsigned char snd_pending;


	while(1)
	{
		size = sock->len;
		if(!size)
		{
		    TRACELN1("WIFI: write OK");
			return RES_SIG_OK;
		}
		if(size >1022)
			size = 1022;

		if(sock->sock_state != SOCKET_CONECTED)
			break;

		// Send command
		cmd.format("+CIPSEND=%u,%u", sock->sock_id, size);
	    TRACELN("WIFI: WRITE %d?", size);
	    cmd_state |= WIFI_CMD_STATE_HND;
	    if(wifi_send_cmd(cmd.c_str(), 20) != WIFI_CMD_STATE_RETURNED)
	    {
	    	wifi_net_error(NET_ERR_SOCK_WRITE);
	    	return RES_SIG_ERROR;
	    }


	    // Send data
		snd_pending = '>';
		do
		{
			process_input(rcv_hnd.signal, cmd.c_str(), snd_pending);
			if ( cmd_state >= WIFI_CMD_STATE_HND )
			{
				if ( cmd_state & WIFI_CMD_STATE_HND )
				{
					unsigned int mytime;

					cmd_state &= ~WIFI_CMD_STATE_HND;
					snd_pending = 0;

					rcv_hnd.tsk_start_read(&received_ch, 1);
					mytime = CURRENT_TASK->time;
					tsk_sleep(55); // data sheet recomendation
					if( snd_hnd.tsk_write(sock->src.as_voidptr, size, WIFI_WRITE_TOT) != RES_OK)
						break;
					CURRENT_TASK->time = mytime;

				} else
				{
					if ( cmd_state >= WIFI_CMD_STATE_OK )
						break; // command completed with OK, ERROR ..
				}
			}

		} while(tsk_resume_wait_signal(rcv_hnd.signal));

//	    wifi_on_blink_transfer(this, GPRS_TRANSFER_INDICATOR);

	    //Check the result
	    if(cmd_state & WIFI_CMD_STATE_OK)
	    {
			TRACE1(" done!");
			sock->src.as_byteptr += size;
			sock->len -= size;
			continue;
		}

	    if (cmd_state & WIFI_CMD_STATE_CMES)
	    {
	    	TRACE_ERROR("\r\nWIFI:%s write ERROR", sock->client.task->name);
	    }

    	break;
	}
	wifi_sleep(120);
	wifi_net_error(NET_ERR_SOCK_WRITE);
	return RES_SIG_ERROR;
}
Example #14
0
RES_CODE esp8266_module::wifi_drv_pwron(bool lowlevel)
{
	RES_CODE res;
	WIFI_DRIVER_DATA * drv_data = drv_info->drv_data;
	bool changed = false;

	if(drv_data->wifi_flags_bad & WIFI_FLAG_SHUTDOWN)
		return wifi_error(NET_ERR_WIFI_SHUTDOWN);
	if( drv_data->wifi_flags_ok & WIFI_FLAG_ON)
		return RES_OK;


	TRACE1_WIFI_DEBUG("\r\nWIFI power");
	drv_data->wifi_flags_ok = WIFI_STATE_OFF;

	do
	{
		res = wifi_echo_off(lowlevel, 0);
		if(lowlevel)
			return res;
		if(res != RES_OK)
		{
			res = wifi_echo_off(lowlevel, 1);
			if(res == RES_OK && !changed)
			{
				//try to switch baudrate
				CSTRING cmd;

				cmd.format("+CIOBAUD=%u", *(const uint32_t*)drv_info->iface_mode_stru[0]);
				if(wifi_send_cmd(cmd.c_str(), 2) & WIFI_CMD_STATE_OK)
					changed = true;
			}
		}
		else
			break;
	} while(changed);


	if(res == RES_OK)
	{
		// configure WiFi module
		wifi_send_cmd("+CWQAP", 3);
		// 1. Enable the module to act as “Station”
		res = wifi_send_cmd("+CWMODE=1", 5);
		if(WIFI_CMD_STATE_OK == res)
		{
		// 2.Enable multiple connections or not (1-multiple/ 0- Single
		//	Note: 	This mode can only be changed after all connections are
		//	 		disconnected. If server is started, reboot is required.
			res = wifi_send_cmd("+CIPMUX=1", 50);
			if(WIFI_CMD_STATE_OK == res)
			{
				//Reset (reset MT with resetting the SIM)
				drv_data->wifi_flags_ok = WIFI_FLAG_ON;
				drv_data->wifi_flags_bad &= ~WIFI_FLAG_ON;
				wifi_on_pwron(this);
				return NET_OK;
			}
		}
	}
	drv_data->wifi_error = NET_ERR_WIFI_ON;
	drv_data->wifi_flags_bad |= WIFI_FLAG_ON;
    return wifi_error(NET_ERR_WIFI);
}
Example #15
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);
}
Example #16
0
void MoveFileDelayAbPath(CSTRING& fileName)
{
	//删不掉的文件更名,延迟删除
	char szTempPath[MAX_PATH], szNum[NUM_LENGTH];
	memset(szTempPath, 0, MAX_PATH);
	memset(szNum, 0, NUM_LENGTH);
	DWORD dwResult=::GetTempPath(MAX_PATH, szTempPath);

	CSTRING TempPath = szTempPath;
	CSTRING temp = fileName.Left(fileName.Find('.'));
	CSTRING postfix = fileName.Right(fileName.GetLength() - fileName.ReverseFind('.'));
	CSTRING tempFile, sysTempFile;

	dwResult=::GetTempPath(MAX_PATH, szTempPath);
	TempPath = szTempPath;
	int n = 0;
	HANDLE hFind;
	WIN32_FIND_DATA ffd; 
	tempFile.Format("%s.%s.tmp", temp.GetBuffer(), itoa(n, szNum, 10));
	hFind = FindFirstFile(tempFile, &ffd);
	while(hFind != INVALID_HANDLE_VALUE)
	{
		n++;
		FindClose(hFind);
		tempFile.Format("%s.%s.tmp", temp.GetBuffer(), itoa(n, szNum, 10));
		hFind = FindFirstFile(tempFile, &ffd);
	}
	tempFile.Format("%s.%s.tmp", temp.GetBuffer(), itoa(n, szNum, 10));
	int ret = temp.ReverseFind('\\');
	CSTRING temp1 = temp;
	if(ret != -1)
	{
		temp1 = temp.Right(temp.GetLength() - ret -1);
	}
	sysTempFile.Format("%s\\%s.tmp", TempPath.GetBuffer(), temp1.GetBuffer());

	if(rename(temp + postfix, tempFile) == 0)
		MoveFileEx(tempFile, sysTempFile, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING); 
}
Example #17
0
WEAK RES_CODE msg_error(CSTRING& msg, int err_code)
{
	MessageBox(msg.c_str());
	return RES_ERROR;
}