46 #include "coap-observe-client.h" 54 #if COAP_OBSERVE_CLIENT 58 #define LOG_MODULE "coap" 59 #define LOG_LEVEL LOG_LEVEL_COAP 61 MEMB(obs_subjects_memb, coap_observee_t, COAP_MAX_OBSERVEES);
62 LIST(obs_subjects_list);
66 get_token(coap_message_t *coap_pkt,
const uint8_t **token)
68 *token = coap_pkt->token;
70 return coap_pkt->token_len;
74 set_token(coap_message_t *coap_pkt,
const uint8_t *token,
size_t token_len)
76 coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
77 memcpy(coap_pkt->token, token, coap_pkt->token_len);
79 return coap_pkt->token_len;
83 coap_obs_add_observee(
const coap_endpoint_t *endpoint,
84 const uint8_t *token,
size_t token_len,
const char *url,
85 notification_callback_t notification_callback,
91 coap_obs_remove_observee_by_url(endpoint, url);
96 o->token_len = token_len;
97 memcpy(o->token, token, token_len);
99 o->notification_callback = notification_callback;
101 LOG_DBG(
"Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
110 coap_obs_remove_observee(coap_observee_t *o)
112 LOG_DBG(
"Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
119 coap_get_obs_subject_by_token(
const uint8_t *token,
size_t token_len)
121 coap_observee_t *obs = NULL;
123 for(obs = (coap_observee_t *)
list_head(obs_subjects_list); obs;
125 LOG_DBG(
"Looking for token 0x%02X%02X\n", token[0], token[1]);
126 if(obs->token_len == token_len
127 && memcmp(obs->token, token, token_len) == 0) {
136 coap_obs_remove_observee_by_token(
const coap_endpoint_t *endpoint,
137 uint8_t *token,
size_t token_len)
140 coap_observee_t *obs = NULL;
142 for(obs = (coap_observee_t *)
list_head(obs_subjects_list); obs;
144 LOG_DBG(
"Remove check Token 0x%02X%02X\n", token[0], token[1]);
146 && obs->token_len == token_len
147 && memcmp(obs->token, token, token_len) == 0) {
148 coap_obs_remove_observee(obs);
156 coap_obs_remove_observee_by_url(
const coap_endpoint_t *endpoint,
160 coap_observee_t *obs = NULL;
162 for(obs = (coap_observee_t *)
list_head(obs_subjects_list); obs;
164 LOG_DBG(
"Remove check URL %s\n", url);
166 && (obs->url == url || memcmp(obs->url, url, strlen(obs->url)) == 0)) {
167 coap_obs_remove_observee(obs);
175 simple_reply(coap_message_type_t type,
const coap_endpoint_t *endpoint,
176 coap_message_t *notification)
178 static coap_message_t response[1];
181 coap_init_message(response, type, NO_ERROR, notification->mid);
186 static coap_notification_flag_t
187 classify_notification(coap_message_t *response,
int first)
190 LOG_DBG(
"no response\n");
191 return NO_REPLY_FROM_SERVER;
193 LOG_DBG(
"server replied\n");
194 if(!IS_RESPONSE_CODE_2_XX(response)) {
195 LOG_DBG(
"error response code\n");
196 return ERROR_RESPONSE_CODE;
198 if(!coap_is_option(response, COAP_OPTION_OBSERVE)) {
199 LOG_DBG(
"server does not support observe\n");
200 return OBSERVE_NOT_SUPPORTED;
205 return NOTIFICATION_OK;
209 coap_handle_notification(
const coap_endpoint_t *endpoint,
210 coap_message_t *notification)
212 const uint8_t *token;
214 coap_observee_t *obs;
215 coap_notification_flag_t flag;
218 LOG_DBG(
"coap_handle_notification()\n");
219 token_len = get_token(notification, &token);
220 LOG_DBG(
"Getting token\n");
222 LOG_DBG(
"Error while handling coap observe notification: " 223 "no token in message\n");
226 LOG_DBG(
"Getting observee info\n");
227 obs = coap_get_obs_subject_by_token(token, token_len);
229 LOG_DBG(
"Error while handling coap observe notification: " 230 "no matching token found\n");
231 simple_reply(COAP_TYPE_RST, endpoint, notification);
234 if(notification->type == COAP_TYPE_CON) {
235 simple_reply(COAP_TYPE_ACK, endpoint, notification);
237 if(obs->notification_callback != NULL) {
238 flag = classify_notification(notification, 0);
241 if(flag == NOTIFICATION_OK) {
242 coap_get_header_observe(notification, &observe);
243 if(observe == obs->last_observe) {
244 LOG_DBG(
"Discarding duplicate\n");
247 obs->last_observe = observe;
249 obs->notification_callback(obs, notification, flag);
254 handle_obs_registration_response(
void *data, coap_message_t *response)
256 coap_observee_t *obs;
257 notification_callback_t notification_callback;
258 coap_notification_flag_t flag;
260 LOG_DBG(
"handle_obs_registration_response()\n");
261 obs = (coap_observee_t *)data;
262 notification_callback = obs->notification_callback;
263 flag = classify_notification(response, 1);
264 if(notification_callback) {
265 notification_callback(obs, response, flag);
267 if(flag != OBSERVE_OK) {
268 coap_obs_remove_observee(obs);
273 coap_generate_token(uint8_t **token_ptr)
275 static uint8_t token = 0;
279 *token_ptr = (uint8_t *)&token;
280 return sizeof(token);
284 coap_obs_request_registration(
const coap_endpoint_t *endpoint,
char *uri,
285 notification_callback_t notification_callback,
288 coap_message_t request[1];
289 coap_transaction_t *t;
292 coap_observee_t *obs;
295 coap_init_message(request, COAP_TYPE_CON, COAP_GET, coap_get_mid());
296 coap_set_header_uri_path(request, uri);
297 coap_set_header_observe(request, 0);
298 token_len = coap_generate_token(&token);
299 set_token(request, token, token_len);
300 t = coap_new_transaction(request->mid, endpoint);
302 obs = coap_obs_add_observee(endpoint, (uint8_t *)token, token_len, uri,
303 notification_callback, data);
305 t->callback = handle_obs_registration_response;
306 t->callback_data = obs;
307 t->message_len = coap_serialize_message(request, t->message);
308 coap_send_transaction(t);
310 LOG_DBG(
"Could not allocate obs_subject resource buffer\n");
311 coap_clear_transaction(t);
314 LOG_DBG(
"Could not allocate transaction buffer\n");
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
Send a message to the specified CoAP endpoint.
Memory block allocation routines.
void list_add(list_t list, void *item)
Add an item at the end of a list.
#define LIST(name)
Declare a linked list.
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
void coap_endpoint_copy(coap_endpoint_t *dest, const coap_endpoint_t *src)
Copy a CoAP endpoint from one memory area to another.
uint8_t * coap_databuf(void)
Returns a common data buffer that can be used when generating CoAP messages for transmission.
An implementation of the Constrained Application Protocol (RFC 7252).
Default definitions of C compiler quirk work-arounds.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
int coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2)
Compare two CoAP endpoints.
#define MEMB(name, structure, num)
Declare a memory block.