CoAP server ported from Arduino microcoap
Dependencies: EthernetInterface Socket mbed-rtos mbed
endpoints.cpp@0:64df7fc091da, 2017-03-23 (annotated)
- Committer:
- rfrenken
- Date:
- Thu Mar 23 16:29:41 2017 +0000
- Revision:
- 0:64df7fc091da
Coap Server ported from Arduino microcoap
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rfrenken | 0:64df7fc091da | 1 | #include "mbed.h" |
rfrenken | 0:64df7fc091da | 2 | #include <stdbool.h> |
rfrenken | 0:64df7fc091da | 3 | #include <string.h> |
rfrenken | 0:64df7fc091da | 4 | |
rfrenken | 0:64df7fc091da | 5 | #include "coap.h" |
rfrenken | 0:64df7fc091da | 6 | |
rfrenken | 0:64df7fc091da | 7 | static char light = '0'; |
rfrenken | 0:64df7fc091da | 8 | static char led0 = '0'; |
rfrenken | 0:64df7fc091da | 9 | static char led1 = '0'; |
rfrenken | 0:64df7fc091da | 10 | static char led2 = '0'; |
rfrenken | 0:64df7fc091da | 11 | static char led3 = '0'; |
rfrenken | 0:64df7fc091da | 12 | DigitalOut myled(LED1); |
rfrenken | 0:64df7fc091da | 13 | DigitalOut myled1(LED2); |
rfrenken | 0:64df7fc091da | 14 | DigitalOut myled2(LED3); |
rfrenken | 0:64df7fc091da | 15 | DigitalOut myled3(LED4); |
rfrenken | 0:64df7fc091da | 16 | |
rfrenken | 0:64df7fc091da | 17 | const uint16_t rsplen = 1500; |
rfrenken | 0:64df7fc091da | 18 | static char rsp[1500] = ""; |
rfrenken | 0:64df7fc091da | 19 | void build_rsp(void); |
rfrenken | 0:64df7fc091da | 20 | |
rfrenken | 0:64df7fc091da | 21 | #include <stdio.h> |
rfrenken | 0:64df7fc091da | 22 | void endpoint_setup(void) |
rfrenken | 0:64df7fc091da | 23 | { |
rfrenken | 0:64df7fc091da | 24 | build_rsp(); |
rfrenken | 0:64df7fc091da | 25 | } |
rfrenken | 0:64df7fc091da | 26 | |
rfrenken | 0:64df7fc091da | 27 | static const coap_endpoint_path_t path_well_known_core = {2, {".well-known", "core"}}; |
rfrenken | 0:64df7fc091da | 28 | 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) |
rfrenken | 0:64df7fc091da | 29 | { |
rfrenken | 0:64df7fc091da | 30 | 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); |
rfrenken | 0:64df7fc091da | 31 | } |
rfrenken | 0:64df7fc091da | 32 | |
rfrenken | 0:64df7fc091da | 33 | static const coap_endpoint_path_t path_light = {1, {"light"}}; |
rfrenken | 0:64df7fc091da | 34 | |
rfrenken | 0:64df7fc091da | 35 | static const coap_endpoint_path_t path_led0 = {2, {"led", "0"}}; |
rfrenken | 0:64df7fc091da | 36 | |
rfrenken | 0:64df7fc091da | 37 | static const coap_endpoint_path_t path_led1 = {2, {"led", "1"}}; |
rfrenken | 0:64df7fc091da | 38 | |
rfrenken | 0:64df7fc091da | 39 | static const coap_endpoint_path_t path_led2 = {2, {"led", "2"}}; |
rfrenken | 0:64df7fc091da | 40 | |
rfrenken | 0:64df7fc091da | 41 | static const coap_endpoint_path_t path_led3 = {2, {"led", "3"}}; |
rfrenken | 0:64df7fc091da | 42 | |
rfrenken | 0:64df7fc091da | 43 | static int handle_get_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 44 | { |
rfrenken | 0:64df7fc091da | 45 | return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 46 | } |
rfrenken | 0:64df7fc091da | 47 | |
rfrenken | 0:64df7fc091da | 48 | static int handle_get_led0(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 49 | { |
rfrenken | 0:64df7fc091da | 50 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led0, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 51 | } |
rfrenken | 0:64df7fc091da | 52 | |
rfrenken | 0:64df7fc091da | 53 | static int handle_get_led1(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 54 | { |
rfrenken | 0:64df7fc091da | 55 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led1, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 56 | } |
rfrenken | 0:64df7fc091da | 57 | |
rfrenken | 0:64df7fc091da | 58 | static int handle_get_led2(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 59 | { |
rfrenken | 0:64df7fc091da | 60 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led2, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 61 | } |
rfrenken | 0:64df7fc091da | 62 | |
rfrenken | 0:64df7fc091da | 63 | static int handle_get_led3(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 64 | { |
rfrenken | 0:64df7fc091da | 65 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led3, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 66 | } |
rfrenken | 0:64df7fc091da | 67 | |
rfrenken | 0:64df7fc091da | 68 | static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 69 | { |
rfrenken | 0:64df7fc091da | 70 | if (inpkt->payload.len == 0) |
rfrenken | 0:64df7fc091da | 71 | return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 72 | if (inpkt->payload.p[0] == '1') |
rfrenken | 0:64df7fc091da | 73 | { |
rfrenken | 0:64df7fc091da | 74 | light = '1'; |
rfrenken | 0:64df7fc091da | 75 | #ifdef ARDUINO |
rfrenken | 0:64df7fc091da | 76 | digitalWrite(led, HIGH); |
rfrenken | 0:64df7fc091da | 77 | #else |
rfrenken | 0:64df7fc091da | 78 | myled = 1; |
rfrenken | 0:64df7fc091da | 79 | printf("ON\n"); |
rfrenken | 0:64df7fc091da | 80 | #endif |
rfrenken | 0:64df7fc091da | 81 | return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 82 | } |
rfrenken | 0:64df7fc091da | 83 | else |
rfrenken | 0:64df7fc091da | 84 | { |
rfrenken | 0:64df7fc091da | 85 | light = '0'; |
rfrenken | 0:64df7fc091da | 86 | #ifdef ARDUINO |
rfrenken | 0:64df7fc091da | 87 | digitalWrite(led, LOW); |
rfrenken | 0:64df7fc091da | 88 | #else |
rfrenken | 0:64df7fc091da | 89 | myled = 0; |
rfrenken | 0:64df7fc091da | 90 | printf("OFF\n"); |
rfrenken | 0:64df7fc091da | 91 | #endif |
rfrenken | 0:64df7fc091da | 92 | return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 93 | } |
rfrenken | 0:64df7fc091da | 94 | } |
rfrenken | 0:64df7fc091da | 95 | |
rfrenken | 0:64df7fc091da | 96 | static int handle_put_led0(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 97 | { |
rfrenken | 0:64df7fc091da | 98 | if (inpkt->payload.len == 0) |
rfrenken | 0:64df7fc091da | 99 | return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 100 | if (inpkt->payload.p[0] == '1') |
rfrenken | 0:64df7fc091da | 101 | { |
rfrenken | 0:64df7fc091da | 102 | led0 = '1'; |
rfrenken | 0:64df7fc091da | 103 | myled = 1; |
rfrenken | 0:64df7fc091da | 104 | printf("ON\n"); |
rfrenken | 0:64df7fc091da | 105 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led0, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 106 | } |
rfrenken | 0:64df7fc091da | 107 | else |
rfrenken | 0:64df7fc091da | 108 | { |
rfrenken | 0:64df7fc091da | 109 | led0 = '0'; |
rfrenken | 0:64df7fc091da | 110 | myled = 0; |
rfrenken | 0:64df7fc091da | 111 | printf("OFF\n"); |
rfrenken | 0:64df7fc091da | 112 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led0, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 113 | } |
rfrenken | 0:64df7fc091da | 114 | } |
rfrenken | 0:64df7fc091da | 115 | |
rfrenken | 0:64df7fc091da | 116 | static int handle_put_led1(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 117 | { |
rfrenken | 0:64df7fc091da | 118 | if (inpkt->payload.len == 0) |
rfrenken | 0:64df7fc091da | 119 | return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 120 | if (inpkt->payload.p[0] == '1') |
rfrenken | 0:64df7fc091da | 121 | { |
rfrenken | 0:64df7fc091da | 122 | led1 = '1'; |
rfrenken | 0:64df7fc091da | 123 | myled1 = 1; |
rfrenken | 0:64df7fc091da | 124 | printf("ON\n"); |
rfrenken | 0:64df7fc091da | 125 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led1, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 126 | } |
rfrenken | 0:64df7fc091da | 127 | else |
rfrenken | 0:64df7fc091da | 128 | { |
rfrenken | 0:64df7fc091da | 129 | led1 = '0'; |
rfrenken | 0:64df7fc091da | 130 | myled1 = 0; |
rfrenken | 0:64df7fc091da | 131 | printf("OFF\n"); |
rfrenken | 0:64df7fc091da | 132 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led1, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 133 | } |
rfrenken | 0:64df7fc091da | 134 | } |
rfrenken | 0:64df7fc091da | 135 | |
rfrenken | 0:64df7fc091da | 136 | static int handle_put_led2(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 137 | { |
rfrenken | 0:64df7fc091da | 138 | if (inpkt->payload.len == 0) |
rfrenken | 0:64df7fc091da | 139 | return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 140 | if (inpkt->payload.p[0] == '1') |
rfrenken | 0:64df7fc091da | 141 | { |
rfrenken | 0:64df7fc091da | 142 | led2 = '1'; |
rfrenken | 0:64df7fc091da | 143 | myled2 = 1; |
rfrenken | 0:64df7fc091da | 144 | printf("ON\n"); |
rfrenken | 0:64df7fc091da | 145 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led2, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 146 | } |
rfrenken | 0:64df7fc091da | 147 | else |
rfrenken | 0:64df7fc091da | 148 | { |
rfrenken | 0:64df7fc091da | 149 | led2 = '0'; |
rfrenken | 0:64df7fc091da | 150 | myled2 = 0; |
rfrenken | 0:64df7fc091da | 151 | printf("OFF\n"); |
rfrenken | 0:64df7fc091da | 152 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led2, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 153 | } |
rfrenken | 0:64df7fc091da | 154 | } |
rfrenken | 0:64df7fc091da | 155 | |
rfrenken | 0:64df7fc091da | 156 | static int handle_put_led3(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) |
rfrenken | 0:64df7fc091da | 157 | { |
rfrenken | 0:64df7fc091da | 158 | if (inpkt->payload.len == 0) |
rfrenken | 0:64df7fc091da | 159 | return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 160 | if (inpkt->payload.p[0] == '1') |
rfrenken | 0:64df7fc091da | 161 | { |
rfrenken | 0:64df7fc091da | 162 | led3 = '1'; |
rfrenken | 0:64df7fc091da | 163 | myled3 = 1; |
rfrenken | 0:64df7fc091da | 164 | printf("ON\n"); |
rfrenken | 0:64df7fc091da | 165 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led3, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 166 | } |
rfrenken | 0:64df7fc091da | 167 | else |
rfrenken | 0:64df7fc091da | 168 | { |
rfrenken | 0:64df7fc091da | 169 | led3 = '0'; |
rfrenken | 0:64df7fc091da | 170 | myled3 = 0; |
rfrenken | 0:64df7fc091da | 171 | printf("OFF\n"); |
rfrenken | 0:64df7fc091da | 172 | return coap_make_response(scratch, outpkt, (const uint8_t *)&led3, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); |
rfrenken | 0:64df7fc091da | 173 | } |
rfrenken | 0:64df7fc091da | 174 | } |
rfrenken | 0:64df7fc091da | 175 | |
rfrenken | 0:64df7fc091da | 176 | |
rfrenken | 0:64df7fc091da | 177 | |
rfrenken | 0:64df7fc091da | 178 | const coap_endpoint_t endpoints[] = |
rfrenken | 0:64df7fc091da | 179 | { |
rfrenken | 0:64df7fc091da | 180 | {COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40"}, |
rfrenken | 0:64df7fc091da | 181 | {COAP_METHOD_GET, handle_get_light, &path_light, "ct=0"}, |
rfrenken | 0:64df7fc091da | 182 | {COAP_METHOD_PUT, handle_put_light, &path_light, NULL}, |
rfrenken | 0:64df7fc091da | 183 | {COAP_METHOD_GET, handle_get_led0, &path_led0, "ct=0"}, |
rfrenken | 0:64df7fc091da | 184 | {COAP_METHOD_PUT, handle_put_led0, &path_led0, NULL}, |
rfrenken | 0:64df7fc091da | 185 | {COAP_METHOD_GET, handle_get_led1, &path_led1, "ct=0"}, |
rfrenken | 0:64df7fc091da | 186 | {COAP_METHOD_PUT, handle_put_led1, &path_led1, NULL}, |
rfrenken | 0:64df7fc091da | 187 | {COAP_METHOD_GET, handle_get_led2, &path_led2, "ct=0"}, |
rfrenken | 0:64df7fc091da | 188 | {COAP_METHOD_PUT, handle_put_led2, &path_led2, NULL}, |
rfrenken | 0:64df7fc091da | 189 | {COAP_METHOD_GET, handle_get_led3, &path_led3, "ct=0"}, |
rfrenken | 0:64df7fc091da | 190 | {COAP_METHOD_PUT, handle_put_led3, &path_led3, NULL}, |
rfrenken | 0:64df7fc091da | 191 | {(coap_method_t)0, NULL, NULL, NULL} |
rfrenken | 0:64df7fc091da | 192 | }; |
rfrenken | 0:64df7fc091da | 193 | |
rfrenken | 0:64df7fc091da | 194 | void build_rsp(void) |
rfrenken | 0:64df7fc091da | 195 | { |
rfrenken | 0:64df7fc091da | 196 | uint16_t len = rsplen; |
rfrenken | 0:64df7fc091da | 197 | const coap_endpoint_t *ep = endpoints; |
rfrenken | 0:64df7fc091da | 198 | int i; |
rfrenken | 0:64df7fc091da | 199 | |
rfrenken | 0:64df7fc091da | 200 | len--; // Null-terminated string |
rfrenken | 0:64df7fc091da | 201 | |
rfrenken | 0:64df7fc091da | 202 | while(NULL != ep->handler) |
rfrenken | 0:64df7fc091da | 203 | { |
rfrenken | 0:64df7fc091da | 204 | if (NULL == ep->core_attr) { |
rfrenken | 0:64df7fc091da | 205 | ep++; |
rfrenken | 0:64df7fc091da | 206 | continue; |
rfrenken | 0:64df7fc091da | 207 | } |
rfrenken | 0:64df7fc091da | 208 | |
rfrenken | 0:64df7fc091da | 209 | if (0 < strlen(rsp)) { |
rfrenken | 0:64df7fc091da | 210 | strncat(rsp, ",", len); |
rfrenken | 0:64df7fc091da | 211 | len--; |
rfrenken | 0:64df7fc091da | 212 | } |
rfrenken | 0:64df7fc091da | 213 | |
rfrenken | 0:64df7fc091da | 214 | strncat(rsp, "<", len); |
rfrenken | 0:64df7fc091da | 215 | len--; |
rfrenken | 0:64df7fc091da | 216 | |
rfrenken | 0:64df7fc091da | 217 | for (i = 0; i < ep->path->count; i++) { |
rfrenken | 0:64df7fc091da | 218 | strncat(rsp, "/", len); |
rfrenken | 0:64df7fc091da | 219 | len--; |
rfrenken | 0:64df7fc091da | 220 | |
rfrenken | 0:64df7fc091da | 221 | strncat(rsp, ep->path->elems[i], len); |
rfrenken | 0:64df7fc091da | 222 | len -= strlen(ep->path->elems[i]); |
rfrenken | 0:64df7fc091da | 223 | } |
rfrenken | 0:64df7fc091da | 224 | |
rfrenken | 0:64df7fc091da | 225 | strncat(rsp, ">;", len); |
rfrenken | 0:64df7fc091da | 226 | len -= 2; |
rfrenken | 0:64df7fc091da | 227 | |
rfrenken | 0:64df7fc091da | 228 | strncat(rsp, ep->core_attr, len); |
rfrenken | 0:64df7fc091da | 229 | len -= strlen(ep->core_attr); |
rfrenken | 0:64df7fc091da | 230 | |
rfrenken | 0:64df7fc091da | 231 | ep++; |
rfrenken | 0:64df7fc091da | 232 | } |
rfrenken | 0:64df7fc091da | 233 | } |
rfrenken | 0:64df7fc091da | 234 |