forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_handler.c
96 lines (86 loc) · 3 KB
/
coap_handler.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
/*
* Copyright (C) 2019 Kaspar Schleiser <kaspar@schleiser.de>
* 2019 Freie Universität Berlin
* 2019 Inria
*
* 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 "log.h"
#include "suit.h"
#include "net/nanocoap.h"
#include "suit/transport/coap.h"
#include "kernel_defines.h"
#ifdef MODULE_RIOTBOOT_SLOT
#include "riotboot/slot.h"
#endif
static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
coap_request_ctx_t *context)
{
(void)context;
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
COAP_FORMAT_TEXT, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD));
}
static ssize_t _version_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
coap_request_ctx_t *context)
{
(void)context;
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
COAP_FORMAT_TEXT, (uint8_t *)"NONE", 4);
}
static ssize_t _trigger_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
coap_request_ctx_t *context)
{
(void)context;
unsigned code;
size_t payload_len = pkt->payload_len;
if (payload_len) {
if (payload_len >= CONFIG_SOCK_URLPATH_MAXLEN) {
code = COAP_CODE_REQUEST_ENTITY_TOO_LARGE;
}
else {
code = COAP_CODE_CREATED;
LOG_INFO("suit: received URL: \"%s\"\n", (char *)pkt->payload);
suit_worker_trigger((char *)pkt->payload, strlen((char *)pkt->payload));
}
}
else {
code = COAP_CODE_REQUEST_ENTITY_INCOMPLETE;
}
return coap_reply_simple(pkt, code, buf, len,
COAP_FORMAT_NONE, NULL, 0);
}
#ifdef MODULE_RIOTBOOT_SLOT
static ssize_t _slot_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
coap_request_ctx_t *context)
{
/* context is passed either as NULL or 0x1 for /active or /inactive */
char c = '0';
if (coap_request_ctx_get_context(context)) {
c += riotboot_slot_other();
}
else {
c += riotboot_slot_current();
}
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
COAP_FORMAT_TEXT, (uint8_t *)&c, 1);
}
#endif
#ifdef MODULE_RIOTBOOT_SLOT
NANOCOAP_RESOURCE(slot_active) {
.path = "/suit/slot/active", .methods = COAP_METHOD_GET, .handler = _slot_handler,
};
NANOCOAP_RESOURCE(slot_inactive) {
.path = "/suit/slot/inactive", .methods = COAP_METHOD_GET, .handler = _slot_handler, .context = (void *)0x1,
};
#endif
NANOCOAP_RESOURCE(trigger) {
.path = "/suit/trigger", .methods = COAP_METHOD_PUT | COAP_METHOD_POST, .handler = _trigger_handler,
};
NANOCOAP_RESOURCE(version) {
.path = "/suit/version", .methods = COAP_METHOD_GET, .handler = _version_handler,
};
NANOCOAP_RESOURCE(board) {
.path = "/riot/board", .methods = COAP_GET, .handler = _riot_board_handler,
};