Blame view

RIOT/tests/pkg_microcoap/coap.c 3.1 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  /*
   * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  #include <coap.h>
  #include <string.h>
  
  #define MAX_RESPONSE_LEN 500
  static uint8_t response[MAX_RESPONSE_LEN] = { 0 };
  
  static int handle_get_well_known_core(coap_rw_buffer_t *scratch,
                                        const coap_packet_t *inpkt,
                                        coap_packet_t *outpkt,
                                        uint8_t id_hi, uint8_t id_lo);
  
  static int handle_get_riot_board(coap_rw_buffer_t *scratch,
                                   const coap_packet_t *inpkt,
                                   coap_packet_t *outpkt,
                                   uint8_t id_hi, uint8_t id_lo);
  
  static const coap_endpoint_path_t path_well_known_core =
          { 2, { ".well-known", "core" } };
  
  static const coap_endpoint_path_t path_riot_board =
          { 2, { "riot", "board" } };
  
  const coap_endpoint_t endpoints[] =
  {
      { COAP_METHOD_GET,	handle_get_well_known_core,
          &path_well_known_core, "ct=40" },
      { COAP_METHOD_GET,	handle_get_riot_board,
          &path_riot_board,	   "ct=0"  },
      /* marks the end of the endpoints array: */
      { (coap_method_t)0, NULL, NULL, NULL }
  };
  
  static int handle_get_well_known_core(coap_rw_buffer_t *scratch,
          const coap_packet_t *inpkt, coap_packet_t *outpkt,
          uint8_t id_hi, uint8_t id_lo)
  {
      char *rsp = (char *)response;
      /* resetting the content of response message */
      memset(response, 0, sizeof(response));
      int len = sizeof(response);
      const coap_endpoint_t *ep = endpoints;
      int i;
  
      len--; // Null-terminated string
  
      while (NULL != ep->handler) {
          if (NULL == ep->core_attr) {
              ep++;
              continue;
          }
  
          if (0 < strlen(rsp)) {
              strncat(rsp, ",", len);
              len--;
          }
  
          strncat(rsp, "<", len);
          len--;
  
          for (i = 0; i < ep->path->count; i++) {
              strncat(rsp, "/", len);
              len--;
  
              strncat(rsp, ep->path->elems[i], len);
              len -= strlen(ep->path->elems[i]);
          }
  
          strncat(rsp, ">;", len);
          len -= 2;
  
          strncat(rsp, ep->core_attr, len);
          len -= strlen(ep->core_attr);
  
          ep++;
      }
  
      return coap_make_response(scratch, outpkt, (const uint8_t *)rsp,
                                strlen(rsp), id_hi, id_lo, &inpkt->tok,
                                COAP_RSPCODE_CONTENT,
                                COAP_CONTENTTYPE_APPLICATION_LINKFORMAT);
  }
  
  static int handle_get_riot_board(coap_rw_buffer_t *scratch,
          const coap_packet_t *inpkt, coap_packet_t *outpkt,
          uint8_t id_hi, uint8_t id_lo)
  {
      const char *riot_name = RIOT_BOARD;
      int len = strlen(RIOT_BOARD);
  
      memcpy(response, riot_name, len);
  
      return coap_make_response(scratch, outpkt, (const uint8_t *)response, len,
                                id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT,
                                COAP_CONTENTTYPE_TEXT_PLAIN);
  }