FreeCoap Trial of CoAP Protocol Open Source Code

1. FreeCoAP

2. Source Code Directory

├── build-aux
├── certs
├── docs
├── lib
│   ├── include
│   └── src
├── proxy
│   ├── common
│   │   ├── include
│   │   └── src
│   └── http_coap
│       ├── include
│       └── src
├── sample
│   ├── reg_client
│   ├── reg_server
│   ├── time_client
│   └── time_server
└── test
    ├── test_coap_client
    ├── test_coap_mem
    ├── test_coap_msg
    ├── test_coap_server
    ├── test_config
    ├── test_cross
    ├── test_data_buf
    ├── test_http_client
    ├── test_http_msg
    ├── test_lock
    ├── test_proxy_http_coap
    ├── test_thread
    ├── test_tls_client
    ├── test_tls_server
    └── test_uri

3. Compile

  • Create installation script

    cd /home/xxx/work/coap/FreeCoAP-master
    autoreconf --install
    
  • Compile

    • Install gnutls in CentOs7

      yum install gnutls*  # 注意: 编译时,源代码中有include <gnutls/x509.h>,因此,还需要安装gnutls-develop
      # 或安装
      yum install gnutls-develop
      # 查看头文件
      ls /usr/include/gnutls/x509.h
      /usr/include/gnutls/x509.h 
      # 创建libgnutls.so的软链接
      cd /lib
      ln -s /usr/lib64/libgnutls.so.28.43.3 libgnutls.so
      
    • Compile libfreecoap.so

      cd /home/xxx/work/coap/FreeCoAP-master
      mkdir bin
      ./configure --prefix=/home/xxx/work/coap/FreeCoAP-master/bin
      make
      make install
      # 查看输出
      ll /home/xxx/work/coap/FreeCoAP-master/bin/lib/
      -rw-r--r--. 1 root root 268930 4月  22 11:52 libfreecoap.a
      -rwxr-xr-x. 1 root root    988 4月  22 11:52 libfreecoap.la
      lrwxrwxrwx. 1 root root     20 4月  22 11:52 libfreecoap.so -> libfreecoap.so.0.0.5
      lrwxrwxrwx. 1 root root     20 4月  22 11:52 libfreecoap.so.0 -> libfreecoap.so.0.0.5
      -rwxr-xr-x. 1 root root 181304 4月  22 11:52 libfreecoap.so.0.0.5
      

      note: The FreeCoAP library built using this process requires GnuTLS and cannot be used without DTLS (编译依赖需要gnutls库,生成的freecoap库没有dtls不能使用)

  • Routine

    • Time server and client based on CoAP/IPv4
      $ cd FreeCoAP/sample/time_server
      $ make dtls=n
      $ ./time_server 0.0.0.0 10000
      (In a different terminal)
      $ cd FreeCoAP/sample/time_client
      $ make dtls=n
      $ ./time_client 127.0.0.1 10000
      
    • Time server and client based on CoAP/DTLS/IPv4
      $ cd FreeCoAP/sample/time_server
      $ make
      $ ./time_server 0.0.0.0 10000
      (In a different terminal)
      $ cd FreeCoAP/sample/time_client
      $ make
      $ ./time_client 127.0.0.1 10000
      

4. Test (time server/client)

  • Throughput rate of server and client

    Environment: CentOS 7 64bit, PC, Intel® Core™ i3-6100 CPU @ 3.70GHz

    • 100,000 consecutive requests, 41254.12 times / s
    • 500,000 consecutive requests, 36922.16 times / s
  • Problems found in the test

    • Comment out in main.c of time_client

      ret = time_client_get_time(&client, buf, sizeof(buf));
      if (ret < 0)
      {
              
              
        ...
      }
      /* sleep(1); */
      

      Communication is interrupted and "Bad message" error is reported. Debug, the source of the error report is in the coap_client_exchange_con function of coap_client.c

      static int coap_client_exchange_con(coap_client_t *client, coap_msg_t *req, coap_msg_t *resp)
      {
              
              
       ssize_t num = 0;
       int ret = 0;
      
      /*  wait for piggy-backed response in ack message
       *  or ack message and separate response message
      */
      coap_log_info("Expecting acknowledgement from host %s and port %s", client->server_host, client->server_port);
      coap_client_start_ack_timer(client);
      while (1)
      {
              
              
          ret = coap_client_listen_ack(client, req);
          ...
          if (coap_msg_get_msg_id(resp) == coap_msg_get_msg_id(req))
          {
              
              
              if (coap_msg_get_type(resp) == COAP_MSG_ACK)
              {
              
              
                  if (coap_msg_is_empty(resp))
                  {
              
              
                      /* received ack message, wait for separate response message */
                      coap_log_info("Received acknowledgement from host %s and port %s", client->server_host, client->server_port);
                      return coap_client_exchange_sep(client, req, resp);
                  }
                  else if (coap_client_match_token(req, resp)) /* 错误发生地: token不等 */
                  {
              
              
                      return coap_client_handle_piggybacked_response(client, resp);
                  }
              }
              else if (coap_msg_get_type(resp) == COAP_MSG_RST)
              {
              
              
                  return -ECONNRESET;
              }
              coap_client_reject(client, resp);
              /* 错误报告地: Bad message */
              return -EBADMSG;
          }
          else if (coap_client_match_token(req, resp))
      

      As mentioned above, the root cause should be caused by not updating the token when the server responds to ack, and it is under debugging...

Guess you like

Origin blog.csdn.net/hylaking/article/details/89473172