forked from smartuni/riot-saul-coap-external-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinch_coap.c
54 lines (44 loc) · 1.46 KB
/
winch_coap.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
#include "winch.h"
#include "net/gcoap.h"
static ssize_t _winch_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
/* CoAP resources. Must be sorted by path (ASCII order). */
static const coap_resource_t _resources[] = {
{ "/winch", COAP_POST, _winch_handler, NULL },
};
static gcoap_listener_t _listener = {
&_resources[0],
sizeof(_resources) / sizeof(_resources[0]),
NULL,
NULL
};
winch_t *pwinch;
static ssize_t _winch_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
{
int l_ges;
(void)ctx;
//change payload size from 5 to 9- according to expected parameters etc.
if (pdu->payload_len <= 11) {
char req_payl[12] = { 0 };
memcpy(req_payl, (char *)pdu->payload, pdu->payload_len);
l_ges = atoi(req_payl);
printf("Submitted value for winch is %d\n", l_ges);
}
else {
return gcoap_response(pdu, buf, len, COAP_CODE_BAD_REQUEST);
}
size_t resp_len;
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
/* option of text and cbor format*/
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
//coap_opt_add_format(pdu, COAP_FORMAT_CBOR);
resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
//variable initiations?
winch_control(pwinch, l_ges);
//resp_len = gcoap_response(pdu, buf, len, COAP_CODE_404);
return resp_len;
}
void winch_coap_init(winch_t *winch)
{
pwinch = winch;
gcoap_register_listener(&_listener);
}