Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lowpan_context.c Source File

lowpan_context.c

00001 /*
00002  * Copyright (c) 2015-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * \file lowpan_context.c
00020  * \brief API for Add,Remove and update timeouts for lowpan context's
00021  *
00022  */
00023 #include "nsconfig.h"
00024 #include "string.h"
00025 #include "ns_types.h"
00026 #include "ns_trace.h"
00027 #include "nsdynmemLIB.h"
00028 #include "ns_list.h"
00029 #include "6LoWPAN/IPHC_Decode/lowpan_context.h"
00030 #include "common_functions.h"
00031 
00032 #define TRACE_GROUP "lCon"
00033 
00034 lowpan_context_t *lowpan_contex_get_by_id(const lowpan_context_list_t *list, uint8_t id)
00035 {
00036     id &=  LOWPAN_CONTEXT_CID_MASK;
00037     /* Check to see we already have info for this context */
00038     ns_list_foreach(lowpan_context_t, entry, list) {
00039         if (entry->cid == id) {
00040             return entry;
00041         }
00042     }
00043     return NULL;
00044 }
00045 
00046 lowpan_context_t *lowpan_context_get_by_address(const lowpan_context_list_t *list, const uint8_t *ipv6Address)
00047 {
00048     /* Check to see we already have info for this context
00049      * List is already listed that longest prefix are first at list
00050      */
00051     ns_list_foreach(lowpan_context_t, entry, list) {
00052         if (bitsequal(entry->prefix, ipv6Address, entry->length)) {
00053             //Take always longest match prefix
00054             return entry;
00055         }
00056     }
00057     return NULL;
00058 }
00059 
00060 
00061 int_fast8_t lowpan_context_update(lowpan_context_list_t *list, uint8_t cid_flags, uint16_t lifetime, const uint8_t *prefix, uint_fast8_t len, bool stable)
00062 {
00063     uint8_t cid = cid_flags & LOWPAN_CONTEXT_CID_MASK;
00064     lowpan_context_t *ctx = NULL;
00065 
00066     /* Check to see we already have info for this context */
00067 
00068     ctx = lowpan_contex_get_by_id(list, cid);
00069     if (ctx) {
00070         //Remove from the list - it will be reinserted below, sorted by its
00071         //new context length. (Don't need "safe" foreach, as we break
00072         //immediately after the removal).
00073         ns_list_remove(list, ctx);
00074     }
00075 
00076     if (lifetime == 0) {
00077         /* This is a removal request: delete any existing entry, then exit */
00078         if (ctx) {
00079             ns_dyn_mem_free(ctx);
00080         }
00081         return 0;
00082     }
00083 
00084     if (!ctx) {
00085         ctx = ns_dyn_mem_alloc(sizeof(lowpan_context_t));
00086     }
00087 
00088     if (!ctx) {
00089         tr_error("No heap for New 6LoWPAN Context");
00090         return -2;
00091     }
00092 
00093     bool inserted = false;
00094     ns_list_foreach(lowpan_context_t, entry, list) {
00095         if (len >= entry->length) {
00096             ns_list_add_before(list, entry, ctx);
00097             inserted = true;
00098             break;
00099         }
00100     }
00101     if (!inserted) {
00102         ns_list_add_to_end(list, ctx);
00103     }
00104 
00105     ctx->length = len;
00106     ctx->cid = cid;
00107     ctx->expiring = false;
00108     ctx->stable = stable;
00109     ctx->compression = cid_flags & LOWPAN_CONTEXT_C;
00110     ctx->lifetime = (uint32_t) lifetime * 600u; /* minutes -> 100ms ticks */
00111 
00112     // Do our own zero-padding, just in case sender has done something weird
00113     memset(ctx->prefix, 0, sizeof ctx->prefix);
00114     bitcopy(ctx->prefix, prefix, len);
00115 
00116     return 0;
00117 }
00118 
00119 void lowpan_context_list_free(lowpan_context_list_t *list)
00120 {
00121     ns_list_foreach_safe(lowpan_context_t, cur, list) {
00122         ns_list_remove(list, cur);
00123         ns_dyn_mem_free(cur);
00124     }
00125 }
00126 
00127 /* ticks is in 1/10s */
00128 void lowpan_context_timer(lowpan_context_list_t *list, uint_fast16_t ticks)
00129 {
00130     ns_list_foreach_safe(lowpan_context_t, ctx, list) {
00131         if (ctx->lifetime > ticks) {
00132             ctx->lifetime -= ticks;
00133             continue;
00134         }
00135         if (!ctx->expiring) {
00136             /* Main lifetime has run out. Clear compression flag, and retain a
00137              * bit longer (RFC 6775 5.4.3).
00138              */
00139             ctx->compression = false;
00140             ctx->expiring = true;
00141             ctx->lifetime = 2 * 18000u; /* 2 * default Router Lifetime = 2 * 1800s = 1 hour */
00142             tr_debug("Context timed out - compression disabled");
00143         } else {
00144             /* 1-hour expiration timer set above has run out */
00145             ns_list_remove(list, ctx);
00146             ns_dyn_mem_free(ctx);
00147             tr_debug("Delete Expired context");
00148         }
00149     }
00150 }
00151