forked from 1248/microcoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.c
113 lines (95 loc) · 3.01 KB
/
endpoints.c
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
104
105
106
107
108
109
110
111
112
#include <stdbool.h>
#include <string.h>
#include "coap.h"
static char light = '0';
const uint16_t rsplen = 1500;
static char rsp[1500] = "";
void build_rsp(void);
#ifdef ARDUINO
#include "Arduino.h"
static int led = 6;
void endpoint_setup(void)
{
pinMode(led, OUTPUT);
build_rsp();
}
#else
#include <stdio.h>
void endpoint_setup(void)
{
build_rsp();
}
#endif
static const coap_endpoint_path_t path_well_known_core = {2, {".well-known", "core"}};
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)
{
return coap_make_response(scratch, outpkt, (const uint8_t *)rsp, strlen(rsp), id_hi, id_lo, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT);
}
static const coap_endpoint_path_t path_light = {1, {"light"}};
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)
{
return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}
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)
{
if (inpkt->payload.len == 0)
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
if (inpkt->payload.p[0] == '1')
{
light = '1';
#ifdef ARDUINO
digitalWrite(led, HIGH);
#else
printf("ON\n");
#endif
return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
else
{
light = '0';
#ifdef ARDUINO
digitalWrite(led, LOW);
#else
printf("OFF\n");
#endif
return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
}
const coap_endpoint_t endpoints[] =
{
{COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40"},
{COAP_METHOD_GET, handle_get_light, &path_light, "ct=0"},
{COAP_METHOD_PUT, handle_put_light, &path_light, NULL},
{(coap_method_t)0, NULL, NULL, NULL}
};
void build_rsp(void)
{
uint16_t len = rsplen;
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++;
}
}