ctx.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
26 #include <inttypes.h>
27 #include <stdbool.h>
28 
29 #include "net/ipv6/addr.h"
30 #include "timex.h"
31 #include "modules.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define GNRC_SIXLOWPAN_CTX_SIZE (16)
45 #define GNRC_SIXLOWPAN_CTX_FLAGS_CID_MASK (0x0f)
46 #define GNRC_SIXLOWPAN_CTX_FLAGS_COMP (0x10)
54 typedef struct {
56  uint8_t prefix_len;
65  uint8_t flags_id;
73  uint16_t ltime;
75 
85 
95 
112  uint8_t prefix_len, uint16_t ltime,
113  bool comp);
114 
120 static inline void gnrc_sixlowpan_ctx_remove(uint8_t id)
121 {
122  if (IS_USED(MODULE_GNRC_SIXLOWPAN_CTX)) {
124  }
125 }
126 
136 static inline bool gnrc_sixlowpan_ctx_match(const gnrc_sixlowpan_ctx_t *ctx,
137  const ipv6_addr_t *prefix, uint8_t prefix_len)
138 {
139  return (ctx != NULL) &&
140  (ctx->prefix_len == prefix_len) &&
141  (ipv6_addr_match_prefix(&ctx->prefix, prefix) >= prefix_len);
142 }
143 
155 static inline bool gnrc_sixlowpan_ctx_update_6ctx(const ipv6_addr_t *prefix, uint8_t prefix_len,
156  uint32_t valid)
157 {
158  if (!IS_USED(MODULE_GNRC_SIXLOWPAN_CTX)) {
159  return false;
160  }
161 
163  uint8_t cid = 0;
164 
165  if (!gnrc_sixlowpan_ctx_match(ctx, prefix, prefix_len)) {
166  /* While the context is a prefix match, the defined prefix within the
167  * context does not match => use new context */
168  ctx = NULL;
169  }
170  else {
172  }
173  /* find first free context ID */
174  if (ctx == NULL) {
175  while (((ctx = gnrc_sixlowpan_ctx_lookup_id(cid)) != NULL) &&
176  !gnrc_sixlowpan_ctx_match(ctx, prefix, prefix_len)) {
177  cid++;
178  }
179  }
180  if (cid < GNRC_SIXLOWPAN_CTX_SIZE) {
181  return gnrc_sixlowpan_ctx_update(cid, (ipv6_addr_t *)prefix, prefix_len,
182  valid / (60 * MS_PER_SEC),
183  true);
184  }
185 
186  return false;
187 }
188 
189 #ifdef TEST_SUITES
194 #endif
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
gnrc_sixlowpan_ctx_t * gnrc_sixlowpan_ctx_update(uint8_t id, const ipv6_addr_t *prefix, uint8_t prefix_len, uint16_t ltime, bool comp)
Updates (or adds if currently not registered) a context.
static bool gnrc_sixlowpan_ctx_update_6ctx(const ipv6_addr_t *prefix, uint8_t prefix_len, uint32_t valid)
Create or update a compression context.
Definition: ctx.h:155
static bool gnrc_sixlowpan_ctx_match(const gnrc_sixlowpan_ctx_t *ctx, const ipv6_addr_t *prefix, uint8_t prefix_len)
Check if a prefix matches a compression context.
Definition: ctx.h:136
static void gnrc_sixlowpan_ctx_remove(uint8_t id)
Removes context.
Definition: ctx.h:120
void gnrc_sixlowpan_ctx_reset(void)
Resets the whole context buffer.
#define GNRC_SIXLOWPAN_CTX_FLAGS_CID_MASK
mask for the Context ID.
Definition: ctx.h:45
#define GNRC_SIXLOWPAN_CTX_SIZE
maximum number of entries in context buffer
Definition: ctx.h:37
gnrc_sixlowpan_ctx_t * gnrc_sixlowpan_ctx_lookup_id(uint8_t id)
Gets context by ID.
gnrc_sixlowpan_ctx_t * gnrc_sixlowpan_ctx_lookup_addr(const ipv6_addr_t *addr)
Gets a context matching the given IPv6 address best with its prefix.
uint8_t ipv6_addr_match_prefix(const ipv6_addr_t *a, const ipv6_addr_t *b)
Checks up to which bit-count two IPv6 addresses match in their prefix.
#define MS_PER_SEC
The number of milliseconds per second.
Definition: time_units.h:71
Adds include for missing inttype definitions.
Definitions for IPv6 addresses.
Common macros and compiler attributes/pragmas configuration.
#define IS_USED(module)
Checks whether a module is being used or not.
Definition: modules.h:67
Entry in the 6LoWPAN context buffer.
Definition: ctx.h:54
uint8_t prefix_len
Length of gnrc_sixlowpan_ctx_t::prefix in bit.
Definition: ctx.h:56
uint16_t ltime
Lifetime in minutes this context is valid.
Definition: ctx.h:73
ipv6_addr_t prefix
The prefix associated to this context.
Definition: ctx.h:55
uint8_t flags_id
4-bit flags, 4-bit Context ID.
Definition: ctx.h:65
Utility library for comparing and computing timestamps.
Data type to represent an IPv6 address.
Definition: addr.h:64