Contiki-NG
coap-observe-client.h
1 /*
2  * Copyright (c) 2014, Daniele Alessandrelli.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /*
34  * \file
35  * Extension to Erbium for enabling CoAP observe clients
36  * \author
37  * Daniele Alessandrelli <daniele.alessandrelli@gmail.com>
38  */
39 
40 /**
41  * \addtogroup coap
42  * @{
43  */
44 
45 #ifndef COAP_OBSERVING_CLIENT_H_
46 #define COAP_OBSERVING_CLIENT_H_
47 
48 #include "coap.h"
49 #include "coap-transactions.h"
50 
51 #ifndef COAP_OBSERVE_CLIENT
52 #define COAP_OBSERVE_CLIENT 0
53 #endif
54 
55 #ifdef COAP_CONF_MAX_OBSERVEES
56 #define COAP_MAX_OBSERVEES COAP_CONF_MAX_OBSERVEES
57 #else
58 #define COAP_MAX_OBSERVEES 4
59 #endif /* COAP_CONF_MAX_OBSERVEES */
60 
61 #if COAP_MAX_OPEN_TRANSACTIONS < COAP_MAX_OBSERVEES
62 #warning "COAP_MAX_OPEN_TRANSACTIONS smaller than COAP_MAX_OBSERVEES: " \
63  "this may be a problem"
64 #endif
65 
66 #define IS_RESPONSE_CODE_2_XX(message) (64 < message->code \
67  && message->code < 128)
68 
69 /*----------------------------------------------------------------------------*/
70 typedef enum {
71  OBSERVE_OK,
72  NOTIFICATION_OK,
73  OBSERVE_NOT_SUPPORTED,
74  ERROR_RESPONSE_CODE,
75  NO_REPLY_FROM_SERVER,
76 } coap_notification_flag_t;
77 
78 /*----------------------------------------------------------------------------*/
79 typedef struct coap_observee_s coap_observee_t;
80 
81 typedef void (*notification_callback_t)(coap_observee_t *subject,
82  void *notification,
83  coap_notification_flag_t);
84 
85 struct coap_observee_s {
86  coap_observee_t *next; /* for LIST */
87  coap_endpoint_t endpoint;
88  const char *url;
89  uint8_t token_len;
90  uint8_t token[COAP_TOKEN_LEN];
91  void *data; /* generic pointer for storing user data */
92  notification_callback_t notification_callback;
93  uint32_t last_observe;
94 };
95 
96 /*----------------------------------------------------------------------------*/
97 coap_observee_t *coap_obs_add_observee(const coap_endpoint_t *endpoint,
98  const uint8_t *token, size_t token_len,
99  const char *url,
100  notification_callback_t
101  notification_callback, void *data);
102 
103 void coap_obs_remove_observee(coap_observee_t *o);
104 
105 coap_observee_t *coap_obs_get_observee_by_token(const uint8_t *token,
106  size_t token_len);
107 
108 int coap_obs_remove_observee_by_token(const coap_endpoint_t *endpoint,
109  uint8_t *token, size_t token_len);
110 
111 int coap_obs_remove_observee_by_url(const coap_endpoint_t *endpoint,
112  const char *url);
113 
114 void coap_handle_notification(const coap_endpoint_t *endpoint,
115  coap_message_t *notification);
116 
117 coap_observee_t *coap_obs_request_registration(const coap_endpoint_t *endpoint,
118  char *uri,
119  notification_callback_t
120  notification_callback,
121  void *data);
122 /* TODO: this function may be moved to coap.c */
123 uint8_t coap_generate_token(uint8_t **token_ptr);
124 
125 #endif /* COAP_OBSERVING_CLIENT_H_ */
126 /** @} */
CoAP module for reliable transport
An implementation of the Constrained Application Protocol (RFC 7252).