Skip to content

Commit aef1c7f

Browse files
author
Zachary Crockett
committed
Merge branch 'master' into compile-server2
2 parents 6d136f1 + 1928ec0 commit aef1c7f

File tree

11 files changed

+922
-5
lines changed

11 files changed

+922
-5
lines changed

src/coap.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CoAPCode::Enum CoAP::code(const unsigned char *message)
3232
case 0x01: return CoAPCode::GET;
3333
case 0x02: return CoAPCode::POST;
3434
case 0x03: return CoAPCode::PUT;
35+
case 0x45: return CoAPCode::CONTENT;
3536
default: return CoAPCode::ERROR;
3637
}
3738
}
@@ -47,3 +48,31 @@ CoAPType::Enum CoAP::type(const unsigned char *message)
4748
case 0x30: return CoAPType::RESET;
4849
}
4950
}
51+
52+
size_t CoAP::option_decode(unsigned char **option)
53+
{
54+
unsigned char nibble = **option & 0x0f;
55+
size_t option_length;
56+
if (13 > nibble)
57+
{
58+
option_length = nibble;
59+
(*option)++;
60+
}
61+
else if (13 == nibble)
62+
{
63+
(*option)++;
64+
option_length = **option + 13;
65+
(*option)++;
66+
}
67+
else if (14 == nibble)
68+
{
69+
option_length = ((*(*option + 1) << 8) | *(*option + 2)) + 269;
70+
(*option) += 3;
71+
}
72+
else
73+
{
74+
// 15 == nibble, reserved value in CoAP spec
75+
option_length = 0;
76+
}
77+
return option_length;
78+
}

src/coap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
License along with this library; if not, see <http://www.gnu.org/licenses/>.
2323
******************************************************************************
2424
*/
25+
26+
#include <string.h>
27+
2528
namespace CoAPMessageType {
2629
enum Enum {
2730
HELLO,
@@ -31,9 +34,11 @@ namespace CoAPMessageType {
3134
UPDATE_BEGIN,
3235
UPDATE_DONE,
3336
CHUNK,
37+
EVENT,
3438
KEY_CHANGE,
3539
SIGNAL_START,
3640
SIGNAL_STOP,
41+
TIME,
3742
EMPTY_ACK,
3843
PING,
3944
ERROR
@@ -46,6 +51,7 @@ namespace CoAPCode {
4651
POST,
4752
PUT,
4853
EMPTY,
54+
CONTENT,
4955
ERROR
5056
};
5157
}
@@ -64,4 +70,5 @@ class CoAP
6470
public:
6571
static CoAPCode::Enum code(const unsigned char *message);
6672
static CoAPType::Enum type(const unsigned char *message);
73+
static size_t option_decode(unsigned char **option);
6774
};

src/events.cpp

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ size_t event(uint8_t buf[], uint16_t message_id, const char *event_name,
3333
*p++ = 0x02; // code 0.02 POST request
3434
*p++ = message_id >> 8;
3535
*p++ = message_id & 0xff;
36-
*p++ = 0xb1;
36+
*p++ = 0xb1; // one-byte Uri-Path option
3737
*p++ = event_type;
3838

3939
size_t name_data_len = strnlen(event_name, 63);
@@ -59,9 +59,74 @@ size_t event(uint8_t buf[], uint16_t message_id, const char *event_name,
5959
return p - buf;
6060
}
6161

62+
// Private, used by two subscription variants below
63+
uint8_t *subscription_prelude(uint8_t buf[], uint16_t message_id,
64+
const char *event_name)
65+
{
66+
uint8_t *p = buf;
67+
*p++ = 0x40; // confirmable, no token
68+
*p++ = 0x01; // code 0.01 GET request
69+
*p++ = message_id >> 8;
70+
*p++ = message_id & 0xff;
71+
*p++ = 0xb1; // one-byte Uri-Path option
72+
*p++ = 'e';
73+
74+
if (NULL != event_name)
75+
{
76+
size_t len = strnlen(event_name, 63);
77+
p += event_name_uri_path(p, event_name, len);
78+
}
79+
80+
return p;
81+
}
82+
83+
size_t subscription(uint8_t buf[], uint16_t message_id,
84+
const char *event_name, const char *device_id)
85+
{
86+
uint8_t *p = subscription_prelude(buf, message_id, event_name);
87+
88+
if (NULL != device_id)
89+
{
90+
size_t len = strnlen(device_id, 63);
91+
92+
*p++ = 0xff;
93+
memcpy(p, device_id, len);
94+
p += len;
95+
}
96+
97+
return p - buf;
98+
}
99+
100+
size_t subscription(uint8_t buf[], uint16_t message_id,
101+
const char *event_name, SubscriptionScope::Enum scope)
102+
{
103+
uint8_t *p = subscription_prelude(buf, message_id, event_name);
104+
105+
switch (scope)
106+
{
107+
case SubscriptionScope::MY_DEVICES:
108+
*p++ = 0x41; // one-byte Uri-Query option
109+
*p++ = 'u';
110+
break;
111+
case SubscriptionScope::FIREHOSE:
112+
default:
113+
// unfiltered firehose is not allowed
114+
if (NULL == event_name || 0 == *event_name)
115+
{
116+
return -1;
117+
}
118+
}
119+
120+
return p - buf;
121+
}
122+
62123
size_t event_name_uri_path(uint8_t buf[], const char *name, size_t name_len)
63124
{
64-
if (name_len < 13)
125+
if (0 == name_len)
126+
{
127+
return 0;
128+
}
129+
else if (name_len < 13)
65130
{
66131
buf[0] = name_len;
67132
memcpy(buf + 1, name, name_len);

src/events.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,30 @@ namespace EventType {
3535
};
3636
}
3737

38+
namespace SubscriptionScope {
39+
enum Enum {
40+
MY_DEVICES,
41+
FIREHOSE
42+
};
43+
}
44+
45+
typedef void (*EventHandler)(const char *event_name, const char *data);
46+
47+
struct FilteringEventHandler
48+
{
49+
char filter[64];
50+
EventHandler handler;
51+
};
52+
3853
size_t event(uint8_t buf[], uint16_t message_id, const char *event_name,
3954
const char *data, int ttl, EventType::Enum event_type);
4055

56+
size_t subscription(uint8_t buf[], uint16_t message_id,
57+
const char *event_name, const char *device_id);
58+
59+
size_t subscription(uint8_t buf[], uint16_t message_id,
60+
const char *event_name, SubscriptionScope::Enum scope);
61+
4162
size_t event_name_uri_path(uint8_t buf[], const char *name, size_t name_len);
4263

4364
#endif // __EVENTS_H

0 commit comments

Comments
 (0)