forked from dgiannakop/Arduino-CoAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coap.h
204 lines (187 loc) · 6.11 KB
/
coap.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/********************************************************************************
** The Arduino-CoAP is free software: you can redistribute it and/or modify **
** it under the terms of the GNU Lesser General Public License as **
** published by the Free Software Foundation, either version 3 of the **
** License, or (at your option) any later version. **
** **
** The Arduino-CoAP is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU Lesser General Public License for more details. **
** **
** You should have received a copy of the GNU Lesser General Public **
** License along with the Arduino-CoAP. **
** If not, see <http://www.gnu.org/licenses/>. **
*******************************************************************************/
#ifndef COAP_H
#define COAP_H
#include <Arduino.h>
#include "coap_conf.h"
//#include "vector.h"
#include "packet.h"
#include "CoapSensor.h"
#include "CoapTransport.h"
#include "resource.h"
#include "observer.h"
typedef CoapPacket coap_packet_t;
typedef CoapResource resource_t;
/**
* The Coap Server Class.
*
* @param transport The CoapTransport to use for communication.
*/
class Coap {
public:
void init(CoapTransport* transport);
/**
* Handles all requests.
*/
void handler(void);
/**
* Checks all registered resources for changes.
*/
void coap_check(void);
/**
* Adds a new Sensor to Coap.
* @param a pointer to the new Sensor.
*/
void add_resource(CoapSensor * sensor);
/**
*
* @param name
* @param methods
* @param fast_resource
* @param notify_time
* @param content_type
*/
void update_resource(String name, uint8_t methods, bool fast_resource, int notify_time, uint8_t content_type);
/**
* Removes a resource from the CoAP server.
* @param name the name of the resource to remove.
*/
void remove_resource(String);
resource_t resource(uint8_t resource_id);
/**
* Generates the body of the response to a new .well-known/core request message.
* @param method
* @param input_data
* @param input_data_len
* @param output_data
* @param output_len
* @param queries
* @return
*/
coap_status_t resource_discovery(uint8_t method, uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_len, queries_t queries);
/**
* Handles new incoming messages from XBEE.
* @param
* @param
* @param
*/
void receiver(uint8_t*, uint16_t, uint8_t);
/**
* Sends the given packet to the destination.
* @param packet the message to send.
* @param destination the destination of the message.
*/
void coap_send(coap_packet_t* packet, uint16_t destination);
/**
* Get the next available MID for a new CoAP message.
* @return the next MID.
*/
uint16_t coap_new_mid();
/**
*
* @param uri_path
* @return
*/
CoapResource* find_resource(String uri_path);
/**
*
* @param req
* @param resp
* @param data
* @param data_len
* @return
*/
int coap_blockwise_response(coap_packet_t *req, coap_packet_t *resp, uint8_t **data, size_t *data_len);
/**
*
* @param id
* @param mid
* @param buf
* @param size
* @param tries
*/
void coap_register_con_msg(uint16_t id, uint16_t mid, uint8_t *buf, uint8_t size, uint8_t tries);
/**
*
* @param mid
* @param flag
* @return
*/
uint8_t coap_unregister_con_msg(uint16_t mid, uint8_t flag);
/**
*
*/
void coap_retransmit_loop();
//coap_status_t call_resource( uint8_t method, uint8_t resource_id, uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_data_len, queries_t queries );
//void coap_resource_discovery( size_t *payload_len );
#ifdef ENABLE_OBSERVE
uint8_t coap_add_observer(coap_packet_t *msg, uint16_t *id, CoapResource* resource_id);
void coap_remove_observer(uint16_t mid);
void coap_notify_from_timer();
void coap_notify_from_interrupt(uint8_t resource_id);
void coap_notify(void);
//uint16_t observe_counter();
void increase_observe_counter();
bool coap_has_observers();
#endif
/**
*
* @param msg
* @param len
*/
String make_string(char* charArray, size_t charLen);
/**
*
* @param msg
* @param len
*/
void debug_msg(uint8_t* msg, uint8_t len);
private:
CoapTransport *transport_;
bool broadcasting;
unsigned long timestamp;
unsigned long last_broadcast;
char hereiam[8];
// Message ID
uint16_t mid_;
uint8_t packetbuf[256];
// new vector type resources
// active requests vector
//active_requests_t active_requests_;
//char *largeBuf_;
// Internal buffer for any reason
uint8_t helperBuf_[CONF_HELPER_BUF_LEN];
// Internal buffer for send
uint8_t sendBuf_[CONF_MAX_MSG_LEN];
// retransmit variables
uint16_t retransmit_id_[CONF_MAX_RETRANSMIT_SLOTS];
uint16_t retransmit_mid_[CONF_MAX_RETRANSMIT_SLOTS];
uint8_t retransmit_register_[CONF_MAX_RETRANSMIT_SLOTS];
uint8_t retransmit_timeout_and_tries_[CONF_MAX_RETRANSMIT_SLOTS];
uint8_t retransmit_size_[CONF_MAX_RETRANSMIT_SLOTS];
uint8_t retransmit_packet_[CONF_MAX_RETRANSMIT_SLOTS][CONF_MAX_MSG_LEN];
unsigned long retransmit_timestamp_[CONF_MAX_RETRANSMIT_SLOTS];
unsigned long timeout_;
resource_t resources_[CONF_MAX_RESOURCES];
int rcount;
#ifdef ENABLE_OBSERVE
observer_t observers[CONF_MAX_OBSERVERS];
#endif
// observe variables
uint16_t observe_counter_;
uint8_t output_data[CONF_LARGE_BUF_LEN];
};
#endif