-
Notifications
You must be signed in to change notification settings - Fork 122
/
microcoap.ino
99 lines (86 loc) · 2.17 KB
/
microcoap.ino
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
/*
* WARNING - UDP_TX_PACKET_MAX_SIZE is hardcoded by Arduino to 24 bytes
* This limits the size of possible outbound UDP packets
*/
#include <SPI.h>
#include <Ethernet.h>
#include <stdint.h>
#include <EthernetUdp.h>
#include "coap.h"
#define PORT 5683
static uint8_t mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
EthernetClient client;
EthernetUDP udp;
uint8_t packetbuf[256];
static uint8_t scratch_raw[32];
static coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
void setup()
{
int i;
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
while(1);
}
Serial.print("My IP address: ");
for (i=0;i<4;i++)
{
Serial.print(Ethernet.localIP()[i], DEC);
Serial.print(".");
}
Serial.println();
udp.begin(PORT);
coap_setup();
endpoint_setup();
}
void udp_send(const uint8_t *buf, int buflen)
{
udp.beginPacket(udp.remoteIP(), udp.remotePort());
while(buflen--)
udp.write(*buf++);
udp.endPacket();
}
void loop()
{
int sz;
int rc;
coap_packet_t pkt;
int i;
if ((sz = udp.parsePacket()) > 0)
{
udp.read(packetbuf, sizeof(packetbuf));
for (i=0;i<sz;i++)
{
Serial.print(packetbuf[i], HEX);
Serial.print(" ");
}
Serial.println("");
if (0 != (rc = coap_parse(&pkt, packetbuf, sz)))
{
Serial.print("Bad packet rc=");
Serial.println(rc, DEC);
}
else
{
size_t rsplen = sizeof(packetbuf);
coap_packet_t rsppkt;
coap_handle_req(&scratch_buf, &pkt, &rsppkt);
memset(packetbuf, 0, UDP_TX_PACKET_MAX_SIZE);
if (0 != (rc = coap_build(packetbuf, &rsplen, &rsppkt)))
{
Serial.print("coap_build failed rc=");
Serial.println(rc, DEC);
}
else
{
udp_send(packetbuf, rsplen);
}
}
}
}