2013-06-22 05:11:53 +02:00
|
|
|
/**
|
2013-08-08 13:39:00 +02:00
|
|
|
* serial number arithmetics (corresponding RFC1982) for version field in ABRO
|
2013-06-22 05:11:53 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013 INRIA.
|
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-06-22 05:11:53 +02:00
|
|
|
*
|
|
|
|
* @ingroup sixlowpan
|
|
|
|
* @{
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-08-08 13:39:00 +02:00
|
|
|
* @brief serial number arithmetics (corresponding RFC1982) for version field in ABRO
|
2015-02-08 18:51:25 +01:00
|
|
|
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
|
2013-06-22 05:11:53 +02:00
|
|
|
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-06-18 18:25:34 +02:00
|
|
|
#include "serialnumber.h"
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
int serial_add8(uint8_t s, uint8_t n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (n > 127) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return -1;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t sum = s + n;
|
|
|
|
return (uint8_t)(sum % 256);
|
2011-06-18 18:25:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
int serial_add16(uint16_t s, uint16_t n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (n > 32767) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return -1;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t sum = s + n;
|
|
|
|
return (uint16_t)(sum % 65536);
|
2011-06-18 18:25:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
int serial_add32(uint32_t s, uint32_t n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (n > 2147483647) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return -1;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t sum = s + n;
|
|
|
|
return (uint32_t)(sum % 4294967296);
|
2011-06-18 18:25:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
serial_comp_res_t serial_comp8(uint8_t s1, uint8_t s2)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (s1 == s2) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return EQUAL;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 < 128) || (s1 > s2 && s1 - s2 > 128)) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return LESS;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 > 128) || (s1 > s2 && s1 - s2 < 128)) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return GREATER;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-06-18 18:25:34 +02:00
|
|
|
return UNDEF;
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
serial_comp_res_t serial_comp16(uint16_t s1, uint16_t s2)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (s1 == s2) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return EQUAL;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 < 32768) || (s1 > s2 && s1 - s2 > 32768)) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return LESS;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 > 32768) || (s1 > s2 && s1 - s2 < 32768)) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return GREATER;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-06-18 18:25:34 +02:00
|
|
|
return UNDEF;
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
serial_comp_res_t serial_comp32(uint32_t s1, uint32_t s2)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (s1 == s2) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return EQUAL;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 < 2147483648) || (s1 > s2 && s1 - s2 > 2147483648)) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return LESS;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 > 2147483648) || (s1 > s2 && s1 - s2 < 2147483648)) {
|
2011-06-18 18:25:34 +02:00
|
|
|
return GREATER;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-06-18 18:25:34 +02:00
|
|
|
return UNDEF;
|
|
|
|
}
|