Exemple #1
0
int netstack_socket_recv(struct netstack_socket *sk, 
			 struct netstack_socket_buf *buf,
			 int timeout)
{
	err_t err;
	struct netbuf *nb;
	struct netconn *conn;

	if (!sk || !sk->priv || !buf) {
		return VMM_EINVALID;
	}
	conn = sk->priv;

	if (0 < timeout) {
		netconn_set_recvtimeout(conn, timeout);
	} else {
		netconn_set_recvtimeout(conn, 0);
	}

	buf->data = NULL;
	buf->len = 0;

	err = netconn_recv(conn, &nb);
	if (err == ERR_TIMEOUT) {
		return VMM_ETIMEDOUT;
	} else if (err != ERR_OK) {
		return VMM_EFAIL;
	}

	netbuf_data(nb, &buf->data, &buf->len);
	buf->priv = nb;

	return VMM_OK;
}
Exemple #2
0
static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port)
{
    struct lwip_socket *s = (struct lwip_socket *)server;
    struct lwip_socket *ns = mbed_lwip_arena_alloc();
    if (!ns) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    if (s->conn->pcb.tcp->state != LISTEN) {
        return NSAPI_ERROR_PARAMETER;
    }

    err_t err = netconn_accept(s->conn, &ns->conn);
    if (err != ERR_OK) {
        mbed_lwip_arena_dealloc(ns);
        return mbed_lwip_err_remap(err);
    }

    netconn_set_recvtimeout(ns->conn, 1);
    *(struct lwip_socket **)handle = ns;

    ip_addr_t peer_addr;
    (void) netconn_peer(ns->conn, &peer_addr, port);
    convert_lwip_addr_to_mbed(addr, &peer_addr);

    netconn_set_nonblocking(ns->conn, true);

    return 0;
}
Exemple #3
0
static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
{
    // check if network is connected
    if (lwip_connected == NSAPI_STATUS_DISCONNECTED) {
        return NSAPI_ERROR_NO_CONNECTION;
    }

    // allocate a socket
    struct lwip_socket *s = mbed_lwip_arena_alloc();
    if (!s) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    enum netconn_type lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP;

#if LWIP_IPV6
    // Enable IPv6 (or dual-stack)
    lwip_proto |= NETCONN_TYPE_IPV6;
#endif

    s->conn = netconn_new_with_callback(lwip_proto, mbed_lwip_socket_callback);

    if (!s->conn) {
        mbed_lwip_arena_dealloc(s);
        return NSAPI_ERROR_NO_SOCKET;
    }

    netconn_set_recvtimeout(s->conn, 1);
    *(struct lwip_socket **)handle = s;
    return 0;
}
Exemple #4
0
static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
{
    // check if network is connected
    if (!lwip_get_ip_address()) {
        return NSAPI_ERROR_NO_CONNECTION;
    }

    // allocate a socket
    struct lwip_socket *s = lwip_arena_alloc();
    if (!s) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    s->conn = netconn_new_with_callback(
            proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP,
            lwip_socket_callback);

    if (!s->conn) {
        lwip_arena_dealloc(s);
        return NSAPI_ERROR_NO_SOCKET;
    }

    netconn_set_recvtimeout(s->conn, 1);
    *(struct lwip_socket **)handle = s;
    return 0;
}
Exemple #5
0
void ICACHE_FLASH_ATTR
_setBulbState(int idx, uint16_t state) {
  struct netconn * sendconn;
  struct netbuf * sendbuf;
  char * data;
  err_t ret;

  if (!network_ready) return;

  sendconn = netconn_new( NETCONN_UDP );
  if (sendconn == NULL) {
    os_printf("[setbulb] Couldn't get netconn\r\n");
    return;
  }
  sendbuf = netbuf_new();
  if (sendbuf == NULL) {
    os_printf("[setbulb] couldn't get sendbuff\r\n");
    return;
  }
  data = netbuf_alloc(sendbuf, 38);
  if (data == NULL) {
    os_printf("[setbulb] couldn't alloc data\r\n");
    return;
  }

  getPktSetBulbPower(data, 38, idx, state);

  ret = netconn_bind(sendconn, &myaddr, 34435);
  if (ret != ERR_OK) {
    os_printf("[setbulb] connect error: %d\r\n", ret);
    return;
  }

  netconn_set_recvtimeout( sendconn, 1000);
  ret = netconn_connect(sendconn, getBulbAddr(idx), 56700);
  if (ret != ERR_OK) {
    os_printf("[setbulb] connect error: %d\r\n", ret);
    return;
  }
  ret = netconn_send(sendconn, sendbuf);
  if (ret != ERR_OK) {
    os_printf("[setbulb] send error: %d\r\n", ret);
  } else {
  }

  netbuf_free(sendbuf);
  netbuf_delete(sendbuf);
  netconn_disconnect(sendconn);
  netconn_delete(sendconn);
}
Exemple #6
0
static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port)
{
    struct lwip_socket *s = (struct lwip_socket *)server;
    struct lwip_socket *ns = lwip_arena_alloc();

    err_t err = netconn_accept(s->conn, &ns->conn);
    if (err != ERR_OK) {
        lwip_arena_dealloc(ns);
        return lwip_err_remap(err);
    }

    netconn_set_recvtimeout(ns->conn, 1);
    *(struct lwip_socket **)handle = ns;

    (void) netconn_peer(ns->conn, (ip_addr_t *)addr->bytes, port);
    addr->version = NSAPI_IPv4;

    return 0;
}
Exemple #7
0
static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
{
    // check if network is connected
    if (!lwip_connected) {
        return NSAPI_ERROR_NO_CONNECTION;
    }

    // allocate a socket
    struct lwip_socket *s = mbed_lwip_arena_alloc();
    if (!s) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    u8_t lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP;

#if LWIP_IPV6 && LWIP_IPV4
    const ip_addr_t *ip_addr;
    ip_addr = mbed_lwip_get_ip_addr(true, &lwip_netif);

    if (IP_IS_V6(ip_addr)) {
        // Enable IPv6 (or dual-stack). LWIP dual-stack support is
        // currently incomplete as of 2.0.0rc2 - eg we will only be able
        // to do a UDP sendto to an address matching the type selected
        // here. Matching "get_ip_addr" and DNS logic, use v4 if
        // available.
        lwip_proto |= NETCONN_TYPE_IPV6;
    }
#elif LWIP_IPV6
    lwip_proto |= NETCONN_TYPE_IPV6;
#endif

    s->conn = netconn_new_with_callback(lwip_proto, mbed_lwip_socket_callback);

    if (!s->conn) {
        mbed_lwip_arena_dealloc(s);
        return NSAPI_ERROR_NO_SOCKET;
    }

    netconn_set_recvtimeout(s->conn, 1);
    *(struct lwip_socket **)handle = s;
    return 0;
}
static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
{
    struct lwip_socket *s = lwip_arena_alloc();
    if (!s) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    s->conn = netconn_new_with_callback(
            proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP,
            lwip_socket_callback);

    if (!s->conn) {
        lwip_arena_dealloc(s);
        return NSAPI_ERROR_NO_SOCKET;
    }

    netconn_set_recvtimeout(s->conn, 1);
    *(struct lwip_socket **)handle = s;
    return 0;
}
Exemple #9
0
nsapi_error_t LWIP::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address)
{
#if LWIP_TCP
    struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)server;
    struct mbed_lwip_socket *ns = arena_alloc();
    if (!ns) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    if (s->conn->pcb.tcp->state != LISTEN) {
        return NSAPI_ERROR_PARAMETER;
    }

    err_t err = netconn_accept(s->conn, &ns->conn);
    if (err != ERR_OK) {
        arena_dealloc(ns);
        return err_remap(err);
    }

    netconn_set_recvtimeout(ns->conn, 1);
    *(struct mbed_lwip_socket **)handle = ns;

    ip_addr_t peer_addr;
    nsapi_addr_t addr;
    u16_t port;
    (void) netconn_peer(ns->conn, &peer_addr, &port);
    convert_lwip_addr_to_mbed(&addr, &peer_addr);

    if (address) {
        address->set_addr(addr);
        address->set_port(port);
    }

    netconn_set_nonblocking(ns->conn, true);

    return 0;
#else
    return NSAPI_ERROR_UNSUPPORTED;
#endif
}
void wanwuyun_task(void * pvParameters)
{
	//创建Queue
	SENSOR_STRUCT *pRxSensorFrame;
	portBASE_TYPE  xStatus; 
	struct ip_addr WanWuYunIPaddr;
	static err_t err,recv_err;
	
	//网络相关结构体
	struct   netbuf *inbuf;
	uint8_t  *buf;
	uint16_t buflen;
	//成功发送 = true,其他状态为 = false	
	bool wan_send_success_flag = false;	
	
	//创建Json的结构体
	cJSON   *DataUpReqJson, *RowJson, *DataUpResJson,*fld;
	char 	*DataOut;
	
	char double_string[]={0};
#if 0	
	cJSON   *SignInReqJson ,*SignInResJson;
	SignInReqJson=cJSON_CreateObject();
	cJSON_AddStringToObject(SignInReqJson, "username", "jackeyjiang");
	cJSON_AddStringToObject(SignInReqJson, "accessid", "AMFJA2V5AMLHBMCXNDI5NZE2NDIXMTMW");
	cJSON_AddStringToObject(SignInReqJson, "appid", "9785739981");
	cJSON_AddStringToObject(SignInReqJson, "dev_id", "stm32_test");
	DataOut = cJSON_Print(DataUpReqJson);
	cJSON_Delete(SignInReqJson);
	vPortFree(DataOut);
	
	DataUpReqJson=cJSON_CreateArray();
	cJSON_AddItemToObject(DataUpReqJson,NULL,fld = cJSON_CreateObject());
	cJSON_AddStringToObject(fld, "seckey", "HgqoOLTlav5jTsefyj3nL5AkRu8UAFRf");
	cJSON_AddItemToObject(fld, "row", RowJson=cJSON_CreateObject());
	cJSON_AddStringToObject(RowJson, "DEV_ID", "stm32_test");
	cJSON_AddNumberToObject(RowJson, "TEMPERATURE", 12.5);
	cJSON_AddNumberToObject(RowJson, "LATITUDE", 12.7);
	cJSON_AddNumberToObject(RowJson, "LONGITUDE", 12.8);
	//转换数据为cJSON数据
	DataOut = cJSON_Print(DataUpReqJson);
	cJSON_Delete(DataUpReqJson);
	printf("%s",DataOut);
	vPortFree(DataOut);	

	//解析返回的Json数据
	SignInResJson = cJSON_Parse(SignInResponse);
	if (!SignInResJson) 
	vPrintString("Error before: [%s]\n",cJSON_GetErrorPtr());
	else {};
	DataUpResJson = cJSON_Parse(DataUpResponse);
	if (!DataUpResJson) vPrintString("Error before: [%s]\n",cJSON_GetErrorPtr());
	else {};	
#endif	
	//创建传感器队列
	pRxSensor_xQueue = xQueueCreate( 2, sizeof(  struct _SENSOR_STRUCT * ) );

	//建立短连接,接收到队列传过来的数据,将其打包成json数据传送到万物云。
	IP4_ADDR( &WanWuYunIPaddr, WANWUYUN_IP_ADDR0, WANWUYUN_IP_ADDR1, WANWUYUN_IP_ADDR2, WANWUYUN_IP_ADDR3 );
	
	while(1)
	{
		/* Create a new connection identifier. */
		STM_EVAL_LEDOff(LED2);
		wanwuyun_clientconn = netconn_new_with_callback(NETCONN_TCP,wanwuyun_callback); //测试
		if (wanwuyun_clientconn!=NULL)
		{  
			/*built a connect to wanwuyun.com server*/
			//netconn_set_nonblocking(wanwuyun_clientconn, 1); //测试
			err = netconn_connect(wanwuyun_clientconn,&WanWuYunIPaddr,WANWUYUN_SERVER_PORT);
			if (err != ERR_OK)  netconn_delete(wanwuyun_clientconn); 
			else if (err == ERR_OK)
			{
				STM_EVAL_LEDOn(LED2);
				vPrintString("netconn_connect wanwuyun_clientconn\r\n");
				/*timeout to wait for new data to be received <Avoid death etc.> */
				netconn_set_sendtimeout(wanwuyun_clientconn,300);
				netconn_set_recvtimeout(wanwuyun_clientconn,700);
				while(!ERR_IS_FATAL(wanwuyun_clientconn->last_err)) 
				{
					STM_EVAL_LEDToggle(LED3);
					xStatus = xQueueReceive(pRxSensor_xQueue,&(pRxSensorFrame),( portTickType ) 1 );
					if(xStatus == pdPASS)
					{	
						//提取结构体中的数据
						DataUpReqJson=cJSON_CreateArray();
						cJSON_AddItemToObject(DataUpReqJson,NULL,fld = cJSON_CreateObject());
						cJSON_AddStringToObject(fld, "seckey", "HgqoOLTlav5jTsefyj3nL5AkRu8UAFRf");
						cJSON_AddItemToObject(fld, "row", RowJson=cJSON_CreateObject());
						cJSON_AddStringToObject(RowJson, "DEV_ID", "stm32_test");
						sprintf(double_string,"%f",pRxSensorFrame->temperature[0]);
						cJSON_AddStringToObject(RowJson, "TEMPERATURE", double_string);
						sprintf(double_string,"%f",pRxSensorFrame->latitude[0]);
						cJSON_AddStringToObject(RowJson, "LATITUDE", double_string);
						sprintf(double_string,"%f",pRxSensorFrame->longitude[0]);
						cJSON_AddStringToObject(RowJson, "LONGITUDE", double_string);
						//转换数据为cJSON数据
						DataOut = cJSON_Print(DataUpReqJson);
						//printf("%d",strlen(DataOut));
						cJSON_Delete(DataUpReqJson);
						vPrintString("%s\r\n",DataOut);
						//vPortFree(DataOut);					
						err=netconn_write(wanwuyun_clientconn,\
							DataOut,strlen(DataOut),\
							NETCONN_COPY);	
						if(err != ERR_OK)  
						{
							vPortFree(DataOut);
							vPrintString("StatuUploadReq erro code is %d\r\n",err);
							break;
						}	
						else
						{
							wan_send_success_flag = true; //表示数据已经发送了
							vPortFree(DataOut);	
						}
					}
					//netconn_recved(wanwuyun_clientconn,100); //测试
					recv_err = netconn_recv(wanwuyun_clientconn, &inbuf);
					if (recv_err == ERR_OK)
					{
						if (netconn_err(wanwuyun_clientconn) == ERR_OK)
						{ 
							netbuf_data(inbuf, (void**)&buf, &buflen);
							DataUpResJson = cJSON_Parse((char *)buf);
							DataOut = cJSON_Print(DataUpResJson);
							vPrintString("%s\r\n",DataOut);
							netbuf_delete(inbuf);
							wan_send_success_flag = false;
							//使用短链接,成功后跳出while(1)
							//break;
						}
						else
						{
							vPrintString("recv_err != ERR_OK \r\n");
						}
					}
					#if 1  //测试当断开连接的时候的状态
					else if((recv_err == ERR_TIMEOUT)&&(wan_send_success_flag == true)) 
					{
						wan_send_success_flag = false;
						vPrintString("recv_err == %d\r\n",recv_err);
						netconn_close(wanwuyun_clientconn);
						netbuf_delete(inbuf);
						netconn_delete(wanwuyun_clientconn);
						//为发送的数据重新入队
						xQueueSendToFront(pRxSensor_xQueue,&(pRxSensorFrame),( portTickType )1);
						break;
					}
					#endif
				}
			}
		}
	}	
}