Example #1
0
void
rest_set_response_status(RESPONSE* response, status_code_t status)
{
#ifdef WITH_COAP
  coap_set_code(response, status);
#else /*WITH_COAP*/
  http_set_status(response, status);
#endif /*WITH_COAP*/
}
Example #2
0
int main(void)
{
  char alias[] = "temp";
  char cik[] = "a32c85ba9dda45823be416246cf8b433baa068d7";

  char host[] = "coap.exosite.com";
  char port[] = "5683";

  srand(time(NULL));

  // CoAP Message Setup
  #define MSG_BUF_LEN 64
  uint8_t msg_send_buf[MSG_BUF_LEN];
  coap_pdu msg_send = {msg_send_buf, 0, 64};
  uint8_t msg_recv_buf[MSG_BUF_LEN];
  coap_pdu msg_recv = {msg_recv_buf, 0, 64};

  uint16_t message_id_counter = rand();

  // Socket to Exosite
  int remotesock;
  size_t bytes_sent;
  ssize_t bytes_recv;
  int rv;

  struct addrinfo exohints, *servinfo, *q;

  memset(&exohints, 0, sizeof exohints);
  exohints.ai_family = AF_UNSPEC;
  exohints.ai_socktype = SOCK_DGRAM;
  exohints.ai_flags = AI_PASSIVE;

  if ((rv = getaddrinfo(host, port, &exohints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }


  // loop through all the results and make a socket
  for(q = servinfo; q != NULL; q = q->ai_next) {
    if ((remotesock = socket(q->ai_family, q->ai_socktype, q->ai_protocol)) == -1) {
      perror("bad socket");
      continue;
    }

    break;
  }

  if (q == NULL) {
      fprintf(stderr, "Failed to Bind Socket\n");
      return 2;
  }
 
  for (;;) 
  {
    printf("--------------------------------------------------------------------------------\n");

    // Build Message
    coap_init_pdu(&msg_send);
    //memset(msg_send, 0, msg_send_len);
    coap_set_version(&msg_send, COAP_V1);
    coap_set_type(&msg_send, CT_CON);
    coap_set_code(&msg_send, CC_GET); // or POST to write
    coap_set_mid(&msg_send, message_id_counter++);
    coap_set_token(&msg_send, rand(), 2);
    coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)"1a", 2);
    coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)alias, strlen(alias));
    coap_add_option(&msg_send, CON_URI_QUERY, (uint8_t*)cik, strlen(cik));
    // to write, set payload:
    //coap_set_payload(msg_send, &msg_send_len, MSG_BUF_LEN, (uint8_t*)"99", 2);

    // Send Message
    if ((bytes_sent = sendto(remotesock, msg_send.buf, msg_send.len, 0, q->ai_addr, q->ai_addrlen)) == -1){
      fprintf(stderr, "Failed to Send Message\n");
      return 2;
    }

    printf("Sent.\n");
    coap_pretty_print(&msg_send);

    // Wait for Response
    bytes_recv = recvfrom(remotesock, (void *)msg_recv.buf, msg_recv.max, 0, q->ai_addr, &q->ai_addrlen);
    if (bytes_recv < 0) {
      fprintf(stderr, "%s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }

    msg_recv.len = bytes_recv;

    if(coap_validate_pkt(&msg_recv) == CE_NONE)
    {
      printf("Got Valid CoAP Packet\n");
      if(coap_get_mid(&msg_recv) == coap_get_mid(&msg_send) &&
         coap_get_token(&msg_recv) == coap_get_token(&msg_send))
      {
        printf("Is Response to Last Message\n");
        coap_pretty_print(&msg_recv);
      }
    }else{
      printf("Received %zi Bytes, Not Valid CoAP\n", msg_recv.len);
      hex_dump(msg_recv.buf, msg_recv.len);
    }

    sleep(1); // One Second
  }
}
Example #3
0
void ICACHE_FLASH_ATTR coap_send(void *pvParameters)
//int coap_send(void)
{
    char *url = "coap.me";
    int port = 5683;
    
    uint16_t message_id_counter = rand();

    printf("%d,%d",message_id_counter, port);
    
    int sock;
    int bytes_sent;
    int bytes_recv;
    
    struct hostent *host;
    struct sockaddr_in server_addr;
    
    /* 通过函数入口参数url获得host地址*/
    host= (struct hostent *) gethostbyname(url);
    printf("here");
    /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        printf("Socket Error\n");
    }
    printf("here2");
    /* 初始化预连接的服务端地址 */
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
    
    printf("--------------------\r\n");
    
    // Build Message
    coap_init_pdu(&msg_send);
    //memset(msg_send, 0, msg_send_len);
    coap_set_version(&msg_send, COAP_V1);
    coap_set_type(&msg_send, CT_CON);
    coap_set_code(&msg_send, CC_GET); // or POST to write
    coap_set_mid(&msg_send, message_id_counter++);
    // coap_set_token(&msg_send, rand(), 2);
    coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)"hello", strlen("hello"));
    // coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)alias, strlen(alias));
    // coap_add_option(&msg_send, CON_URI_QUERY, (uint8_t*)cik, strlen(cik));
    // to write, set payload:
    //coap_set_payload(msg_send, &msg_send_len, MSG_BUF_LEN, (uint8_t*)"99", 2);
    
    // Send Message
    if ((bytes_sent = sendto(sock, msg_send.buf, msg_send.len, 0, 
                             (struct sockaddr *)&server_addr, sizeof(struct sockaddr))) == -1)
    {
        printf("Failed to Send Message\r\n");
    }
    else{
	printf("bytes %d\n",bytes_sent);	
    }

     socklen_t len = sizeof(server_addr);
    
     bytes_recv = recvfrom(sock, (void *)msg_recv.buf, msg_recv.max, 0, (struct sockaddr *)&server_addr,&len);
   
    if (bytes_recv < 0) {
      printf("game over baby\n");
    }
    else{
	printf("yuhu\n");
    }

    msg_recv.len = bytes_recv;

    if(coap_validate_pkt(&msg_recv) == CE_NONE)
    {
      printf("Got Valid CoAP Packet\n");
      if(coap_get_mid(&msg_recv) == coap_get_mid(&msg_send) &&
         coap_get_token(&msg_recv) == coap_get_token(&msg_send))
      {
        printf("Is Response to Last Message\n");
        coap_pretty_print(&msg_recv);
      }
    }else{
      printf("Received %zi Bytes, Not Valid CoAP\n", msg_recv.len);
    }    
  vTaskDelete(NULL);	
}
Example #4
0
void* CoapClient(void* parg)
{
	socklen_t sin_size;
	struct sockaddr_in addr_remote;
	uint16_t message_id_counter;
	ssize_t bytes_sent;
	ssize_t bytes_recv;
	uint8_t msg_buf[MSG_BUF_LEN];
	int msg_len;
	
	int fifo_fd;
	char fifo[64] = {'\0'};
	
	struct timeval timeout;
	timeout.tv_sec = 3;
	timeout.tv_usec = 0;
	
	coapIP *ipdata = (coapIP *)parg;
	
	//注册信号
	(void)signal(COAP_CLOSE_SIGNAL, Close_Sig);
	(void)signal(FIFO_CREATE_SIGNAL, Fifo_Create_Sig);
	
	// CoAP Message Setup
	uint8_t msg_send_buf[MSG_BUF_LEN];
	coap_pdu msg_send = {msg_send_buf, 0, MSG_BUF_LEN};
	uint8_t msg_recv_buf[MSG_BUF_LEN];
	coap_pdu msg_recv = {msg_recv_buf, 0, MSG_BUF_LEN};
	
	srand(time(NULL));
	message_id_counter = rand();
	
	//创建套接字
	if( (ipdata->coapSocket = socket(AF_INET, SOCK_DGRAM, 0))==-1 )
	{
		zlog_error(zc, "create socket error!");
		exit(-1);
	}
	
	addr_remote.sin_family = AF_INET;
	addr_remote.sin_port = htons(COAP_PORT);
	addr_remote.sin_addr.s_addr = inet_addr( ipdata->ip );
 	memset(addr_remote.sin_zero, 0, 8);
 	
 	zlog_debug(zc, "coap client socket is %d, ip is %s,path is %s", ipdata->coapSocket, ipdata->ip, ipdata->path);
 	
 	//设置接收超时
 	setsockopt(ipdata->coapSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval));
 	
 	GetfifoName( fifo, sizeof(fifo), getpid() );
	
	//等待fifo创建好的信号
	//pause();
	
	//以只读方式打开FIFO,返回文件描述符fd 
	fifo_fd = open(fifo,O_RDONLY);   
	if( fifo_fd == -1 )
	{
		printf("Open fifo error!\n");
		exit(1);
	}
	
	printf("coap client %d open fifo %s\r\n", getpid(), fifo);
 	 	
	while(1) 
	{
		//清空缓存
		memset(msg_send.buf, '\0', MSG_BUF_LEN);
		msg_send.len = 0;
		
		//从管道读数据
		msg_len = read(fifo_fd, msg_buf, MSG_BUF_LEN);
		//printf("fifo read is %s", msg_buf);
		
		// Build Message
		coap_init_pdu(&msg_send);
		coap_set_version(&msg_send, COAP_V1);
		coap_set_type(&msg_send, CT_CON);
		coap_set_code(&msg_send, CC_GET); // or POST to write
		coap_set_mid(&msg_send, message_id_counter++);
		coap_set_token(&msg_send, rand(), 2);
		coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)ipdata->path, strlen(ipdata->path));
	
		// to write, set payload:
		coap_set_payload( &msg_send, (uint8_t *)msg_buf, msg_len );
		
		// Send Message
		if ( (bytes_sent = sendto(ipdata->coapSocket, msg_send.buf, msg_send.len, 0, 
		(struct sockaddr*)&addr_remote, sizeof(struct sockaddr_in))) == -1 )
			zlog_error(zc, "Failed to Send Message!");
		else
		{
			//发送成功
			//printf("Sent.\n");
			//coap_pretty_print(&msg_send);
		
			sin_size = sizeof(struct sockaddr);
			// Wait for Response
			bytes_recv = recvfrom(ipdata->coapSocket, (void *)msg_recv.buf, msg_recv.max, 0, (struct sockaddr*)&addr_remote, &sin_size);
			if (bytes_recv < 0) 
			{
				zlog_error(zc, "Failed to socket recv Message!");
				continue;
			}
/*
			msg_recv.len = bytes_recv;

			if(coap_validate_pkt(&msg_recv) == CE_NONE)
			{
				printf("Got Valid CoAP Packet\n");
				if(coap_get_mid(&msg_recv) == coap_get_mid(&msg_send) && coap_get_token(&msg_recv) == coap_get_token(&msg_send))
				{
					printf("Is Response to Last Message\n");
					coap_pretty_print(&msg_recv);
				}
			}
			else
			{
				printf("Received %zi Bytes, Not Valid CoAP\n", msg_recv.len);
				hex_dump(msg_recv.buf, msg_recv.len);
			}
*/		
		}
	}
}