Contiki 3.x
er-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 #ifndef COAP_OBSERVING_CLIENT_H_
41 #define COAP_OBSERVING_CLIENT_H_
42 
43 #include "er-coap.h"
44 #include "er-coap-transactions.h"
45 
46 #ifndef COAP_OBSERVE_CLIENT
47 #define COAP_OBSERVE_CLIENT 0
48 #endif
49 
50 #ifdef COAP_CONF_MAX_OBSERVEES
51 #define COAP_MAX_OBSERVEES COAP_CONF_MAX_OBSERVEES
52 #else
53 #define COAP_MAX_OBSERVEES 4
54 #endif /* COAP_CONF_MAX_OBSERVEES */
55 
56 #if COAP_MAX_OPEN_TRANSACTIONS < COAP_MAX_OBSERVEES
57 #warning "COAP_MAX_OPEN_TRANSACTIONS smaller than COAP_MAX_OBSERVEES: " \
58  "this may be a problem"
59 #endif
60 
61 #define IS_RESPONSE_CODE_2_XX(message) (64 < message->code \
62  && message->code < 128)
63 
64 /*----------------------------------------------------------------------------*/
65 typedef enum {
66  OBSERVE_OK,
67  NOTIFICATION_OK,
68  OBSERVE_NOT_SUPPORTED,
69  ERROR_RESPONSE_CODE,
70  NO_REPLY_FROM_SERVER,
71 } coap_notification_flag_t;
72 
73 /*----------------------------------------------------------------------------*/
74 typedef struct coap_observee_s coap_observee_t;
75 
76 typedef void (*notification_callback_t)(coap_observee_t *subject,
77  void *notification,
78  coap_notification_flag_t);
79 
80 struct coap_observee_s {
81  coap_observee_t *next; /* for LIST */
82  uip_ipaddr_t addr;
83  uint16_t port;
84  const char *url;
85  uint8_t token_len;
86  uint8_t token[COAP_TOKEN_LEN];
87  void *data; /* generic pointer for storing user data */
88  notification_callback_t notification_callback;
89  uint32_t last_observe;
90 };
91 
92 /*----------------------------------------------------------------------------*/
93 coap_observee_t *coap_obs_add_observee(uip_ipaddr_t *addr, uint16_t port,
94  const uint8_t *token, size_t token_len,
95  const char *url,
96  notification_callback_t
97  notification_callback, void *data);
98 
99 void coap_obs_remove_observee(coap_observee_t *o);
100 
101 coap_observee_t *coap_obs_get_observee_by_token(const uint8_t *token,
102  size_t token_len);
103 
104 int coap_obs_remove_observee_by_token(uip_ipaddr_t *addr, uint16_t port,
105  uint8_t *token, size_t token_len);
106 
107 int coap_obs_remove_observee_by_url(uip_ipaddr_t *addr, uint16_t port,
108  const char *url);
109 
110 void coap_handle_notification(uip_ipaddr_t *, uint16_t port,
111  coap_packet_t *notification);
112 
113 coap_observee_t *coap_obs_request_registration(uip_ipaddr_t *addr,
114  uint16_t port, char *uri,
115  notification_callback_t
116  notification_callback,
117  void *data);
118 /* TODO: this function may be moved to er-coap.c */
119 uint8_t coap_generate_token(uint8_t **token_ptr);
120 
121 #endif /* COAP_OBSERVING_CLIENT_H_ */
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
CoAP module for reliable transport
An implementation of the Constrained Application Protocol (RFC).