Пример #1
0
int Agent_cloud_process(void)
{
	EdpPacket* connect_pkg = NULL;
	int ret;
	unsigned char failedcnt = 0;

	cloud_conn_status.conn_status = SOCK_DONE;

	#ifndef   Devid_RestFul
		AGAIN:    
			/* 向Onenet服务器发送EDP连接报文 */
			connect_pkg = PacketConnect1((char*)SRC_DEVID, (char*)CLOUD_API_KEY);
		
			/* dump package data */
			log_info("Packet connect data(len:%d):\n", connect_pkg->_write_pos);
			dump_hex(connect_pkg->_data, connect_pkg->_write_pos);
			log_info("SRC_DEVID:%s CLOUD_API_KEY:%s\r\n",SRC_DEVID,CLOUD_API_KEY);
		
			ret = Socket_TCPClientSendData((char *)connect_pkg->_data, connect_pkg->_write_pos);
			DeleteBuffer(&connect_pkg);
			if (ret < 0) {
				cloud_conn_status.conn_status = CLOUD_CONN_ERROR;
				log_info("Send connect data failed(ret:%d).\n", ret);
		        failedcnt++;
		        /* 发送连接次数超过3次则退出连接 */
		        if (failedcnt > 3) {
		            Socket_TCPClientClose();
		            return -1;
		        }
		        OSTimeDly(50);
				goto AGAIN;
			} else {
				log_notice("Send connect data success.\n");
			}
	#endif


	/* 主进程用于接收云端服务器的数据 */
	while (1)
	{
		ret = Socket_TCPClientRecvData(recv_buf, CLOUD_RECV_BUFFER_SIZE);
		if (ret > 0) {
			recv_buf[ret] = '\0';
			#if   (4 & Devid_Mode) //restful方式
			   log_notice("%s\r\n",recv_buf);
			   RestFul_RecvProcess(recv_buf,ret);
			#else
               recv_data_process(recv_buf, ret);
			#endif
		}
		
		if (ret < 0 || TCPClient_reset_flag == 0xa5 ) {  //关闭TCP链接
			log_err("Recv tcp client data error(%d)\n", ret);
			Socket_TCPClientClose();
			return -1;
		}
	}
}
Пример #2
0
void Connect_RequestType1(int8_t *devid, int8_t *api_key)
{
    EdpPacket *send_pkg;

    send_pkg = PacketConnect1((const int8_t *)devid, (const int8_t *)api_key);
    if(send_pkg == NULL)
    {
        return;
    }
    /* send_pkg = PacketConnect2("433223", "{ \"SYS\" : \"0DEiuApATHgLurKNEl6vY4bLwbQ=\" }"); */
    /* send_pkg = PacketConnect2("433223", "{ \"13982031959\" : \"888888\" }"); */

    /* 向设备云发送连接请求 */
    printf("send connect to server, bytes: %d\n", send_pkg->_write_pos);
    DoSend(0, (const uint8_t *)send_pkg->_data, send_pkg->_write_pos);
    /* 必须释放这个内存,否则造成泄露 */
    DeleteBuffer(&send_pkg);
    mDelay(1000);
}