Renata Frenken / Mbed 2 deprecated Coap

Dependencies:   EthernetInterface Socket mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers endpoints.cpp Source File

endpoints.cpp

00001 #include "mbed.h"
00002 #include <stdbool.h>
00003 #include <string.h>
00004 
00005 #include "coap.h"
00006 
00007 static char light = '0';
00008 static char led0 = '0';
00009 static char led1 = '0';
00010 static char led2 = '0';
00011 static char led3 = '0';
00012 DigitalOut myled(LED1);
00013 DigitalOut myled1(LED2);
00014 DigitalOut myled2(LED3);
00015 DigitalOut myled3(LED4);
00016 
00017 const uint16_t rsplen = 1500;
00018 static char rsp[1500] = "";
00019 void build_rsp(void);
00020 
00021 #include <stdio.h>
00022 void endpoint_setup(void)
00023 {
00024     build_rsp();
00025 }
00026 
00027 static const coap_endpoint_path_t path_well_known_core = {2, {".well-known", "core"}};
00028 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)
00029 {
00030     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);
00031 }
00032 
00033 static const coap_endpoint_path_t path_light = {1, {"light"}};
00034 
00035 static const coap_endpoint_path_t path_led0 = {2, {"led", "0"}};
00036 
00037 static const coap_endpoint_path_t path_led1 = {2, {"led", "1"}};
00038 
00039 static const coap_endpoint_path_t path_led2 = {2, {"led", "2"}};
00040 
00041 static const coap_endpoint_path_t path_led3 = {2, {"led", "3"}};
00042 
00043 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)
00044 {
00045     return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
00046 }
00047 
00048 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)
00049 {
00050     return coap_make_response(scratch, outpkt, (const uint8_t *)&led0, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
00051 }
00052 
00053 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)
00054 {
00055     return coap_make_response(scratch, outpkt, (const uint8_t *)&led1, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
00056 }
00057 
00058 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)
00059 {
00060     return coap_make_response(scratch, outpkt, (const uint8_t *)&led2, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
00061 }
00062 
00063 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)
00064 {
00065     return coap_make_response(scratch, outpkt, (const uint8_t *)&led3, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
00066 }
00067 
00068 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)
00069 {
00070     if (inpkt->payload.len == 0)
00071         return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
00072     if (inpkt->payload.p[0] == '1')
00073     {
00074         light = '1';
00075 #ifdef ARDUINO
00076         digitalWrite(led, HIGH);
00077 #else
00078         myled = 1;
00079         printf("ON\n");
00080 #endif
00081         return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00082     }
00083     else
00084     {
00085         light = '0';
00086 #ifdef ARDUINO
00087         digitalWrite(led, LOW);
00088 #else
00089         myled = 0;
00090         printf("OFF\n");
00091 #endif
00092         return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00093     }
00094 }
00095 
00096 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)
00097 {
00098     if (inpkt->payload.len == 0)
00099         return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
00100     if (inpkt->payload.p[0] == '1')
00101     {
00102         led0 = '1';
00103         myled = 1;
00104         printf("ON\n");
00105         return coap_make_response(scratch, outpkt, (const uint8_t *)&led0, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00106     }
00107     else
00108     {
00109         led0 = '0';
00110         myled = 0;
00111         printf("OFF\n");
00112         return coap_make_response(scratch, outpkt, (const uint8_t *)&led0, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00113     }
00114 }
00115 
00116 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)
00117 {
00118     if (inpkt->payload.len == 0)
00119         return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
00120     if (inpkt->payload.p[0] == '1')
00121     {
00122         led1 = '1';
00123         myled1 = 1;
00124         printf("ON\n");
00125         return coap_make_response(scratch, outpkt, (const uint8_t *)&led1, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00126     }
00127     else
00128     {
00129         led1 = '0';
00130         myled1 = 0;
00131         printf("OFF\n");
00132         return coap_make_response(scratch, outpkt, (const uint8_t *)&led1, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00133     }
00134 }
00135 
00136 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)
00137 {
00138     if (inpkt->payload.len == 0)
00139         return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
00140     if (inpkt->payload.p[0] == '1')
00141     {
00142         led2 = '1';
00143         myled2 = 1;
00144         printf("ON\n");
00145         return coap_make_response(scratch, outpkt, (const uint8_t *)&led2, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00146     }
00147     else
00148     {
00149         led2 = '0';
00150         myled2 = 0;
00151         printf("OFF\n");
00152         return coap_make_response(scratch, outpkt, (const uint8_t *)&led2, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00153     }
00154 }
00155 
00156 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)
00157 {
00158     if (inpkt->payload.len == 0)
00159         return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
00160     if (inpkt->payload.p[0] == '1')
00161     {
00162         led3 = '1';
00163         myled3 = 1;
00164         printf("ON\n");
00165         return coap_make_response(scratch, outpkt, (const uint8_t *)&led3, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00166     }
00167     else
00168     {
00169         led3 = '0';
00170         myled3 = 0;
00171         printf("OFF\n");
00172         return coap_make_response(scratch, outpkt, (const uint8_t *)&led3, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
00173     }
00174 }
00175 
00176 
00177 
00178 const coap_endpoint_t endpoints[] =
00179 {
00180     {COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40"},
00181     {COAP_METHOD_GET, handle_get_light, &path_light, "ct=0"},
00182     {COAP_METHOD_PUT, handle_put_light, &path_light, NULL},
00183     {COAP_METHOD_GET, handle_get_led0, &path_led0, "ct=0"},
00184     {COAP_METHOD_PUT, handle_put_led0, &path_led0, NULL},
00185     {COAP_METHOD_GET, handle_get_led1, &path_led1, "ct=0"},
00186     {COAP_METHOD_PUT, handle_put_led1, &path_led1, NULL},
00187     {COAP_METHOD_GET, handle_get_led2, &path_led2, "ct=0"},
00188     {COAP_METHOD_PUT, handle_put_led2, &path_led2, NULL},
00189     {COAP_METHOD_GET, handle_get_led3, &path_led3, "ct=0"},
00190     {COAP_METHOD_PUT, handle_put_led3, &path_led3, NULL},
00191     {(coap_method_t)0, NULL, NULL, NULL}
00192 };
00193 
00194 void build_rsp(void)
00195 {
00196     uint16_t len = rsplen;
00197     const coap_endpoint_t *ep = endpoints;
00198     int i;
00199 
00200     len--; // Null-terminated string
00201 
00202     while(NULL != ep->handler)
00203     {
00204         if (NULL == ep->core_attr) {
00205             ep++;
00206             continue;
00207         }
00208 
00209         if (0 < strlen(rsp)) {
00210             strncat(rsp, ",", len);
00211             len--;
00212         }
00213 
00214         strncat(rsp, "<", len);
00215         len--;
00216 
00217         for (i = 0; i < ep->path->count; i++) {
00218             strncat(rsp, "/", len);
00219             len--;
00220 
00221             strncat(rsp, ep->path->elems[i], len);
00222             len -= strlen(ep->path->elems[i]);
00223         }
00224 
00225         strncat(rsp, ">;", len);
00226         len -= 2;
00227 
00228         strncat(rsp, ep->core_attr, len);
00229         len -= strlen(ep->core_attr);
00230 
00231         ep++;
00232     }
00233 }
00234