<|start_filename|>test/test_block_reader.cpp<|end_filename|>
/*********************************************************************
 *        _       _         _
 *  _ __ | |_  _ | |  __ _ | |__   ___
 * | '__|| __|(_)| | / _` || '_ \ / __|
 * | |   | |_  _ | || (_| || |_) |\__ \
 * |_|    \__|(_)|_| \__,_||_.__/ |___/
 *
 * www.rt-labs.com
 * Copyright 2018 rt-labs AB, Sweden.
 *
 * This software is dual-licensed under GPLv3 and a commercial
 * license. See the file LICENSE.md distributed with this software for
 * full license information.
 ********************************************************************/

#include "utils_for_testing.h"
#include "mocks.h"

#include "pf_block_reader.h"
#include "pf_includes.h"

#include <gtest/gtest.h>

class BlockReaderUnitTest : public PnetUnitTest
{
};

TEST_F (BlockReaderUnitTest, BlockReaderTestGetBits)
{

   /* 11111111 11111111 11111111 11111111 */
   EXPECT_EQ (0x00000000ul, pf_get_bits (0xFFFFFFFF, 0, 0));
   EXPECT_EQ (0x00000001ul, pf_get_bits (0xFFFFFFFF, 0, 1));
   EXPECT_EQ (0x00000003ul, pf_get_bits (0xFFFFFFFF, 0, 2));
   EXPECT_EQ (0x00000007ul, pf_get_bits (0xFFFFFFFF, 0, 3));
   EXPECT_EQ (0x0000000Ful, pf_get_bits (0xFFFFFFFF, 0, 4));
   EXPECT_EQ (0x0000001Ful, pf_get_bits (0xFFFFFFFF, 0, 5));
   EXPECT_EQ (0x0000003Ful, pf_get_bits (0xFFFFFFFF, 0, 6));
   EXPECT_EQ (0x0000007Ful, pf_get_bits (0xFFFFFFFF, 0, 7));
   EXPECT_EQ (0x000000FFul, pf_get_bits (0xFFFFFFFF, 0, 8));
   EXPECT_EQ (0x7FFFFFFFul, pf_get_bits (0xFFFFFFFF, 0, 31));
   EXPECT_EQ (0xFFFFFFFFul, pf_get_bits (0xFFFFFFFF, 0, 32));
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 0, 33)); /* Illegal length */

   EXPECT_EQ (0x00000000ul, pf_get_bits (0xFFFFFFFF, 1, 0));
   EXPECT_EQ (0x00000001ul, pf_get_bits (0xFFFFFFFF, 1, 1));
   EXPECT_EQ (0x00000003ul, pf_get_bits (0xFFFFFFFF, 1, 2));
   EXPECT_EQ (0x00000007ul, pf_get_bits (0xFFFFFFFF, 1, 3));
   EXPECT_EQ (0x000000FFul, pf_get_bits (0xFFFFFFFF, 1, 8));
   EXPECT_EQ (0x7FFFFFFFul, pf_get_bits (0xFFFFFFFF, 1, 31));
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 1, 32)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 1, 33)); /* Illegal length */

   EXPECT_EQ (0x00000000ul, pf_get_bits (0xFFFFFFFF, 31, 0));
   EXPECT_EQ (0x00000001ul, pf_get_bits (0xFFFFFFFF, 31, 1));
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 31, 2));  /* Illegal length and
                                                         position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 31, 32)); /* Illegal length and
                                                         position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 31, 33)); /* Illegal length */

   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 32, 0));  /* Illegal position */
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 32, 1));  /* Illegal position */
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 32, 32)); /* Illegal position */
   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 32, 33)); /* Illegal position and
                                                         illegal length */

   EXPECT_EQ (0ul, pf_get_bits (0xFFFFFFFF, 0xFF, 0xFF)); /* Illegal position
                                                             and illegal length
                                                           */

   /* 00000000 00000000 00000000 00010000 */
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000010, 0, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000010, 1, 3));
   EXPECT_EQ (0x00000004ul, pf_get_bits (0x00000010, 2, 3));
   EXPECT_EQ (0x00000002ul, pf_get_bits (0x00000010, 3, 3));
   EXPECT_EQ (0x00000001ul, pf_get_bits (0x00000010, 4, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000010, 5, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000010, 28, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000010, 29, 3));
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 30, 3)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 31, 3)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 32, 3)); /* Illegal position */
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 33, 3)); /* Illegal position */

   /* 00000000 00000000 00000000 00000001 */
   EXPECT_EQ (0x00000001ul, pf_get_bits (0x00000001, 0, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000001, 1, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000001, 2, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000001, 3, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x00000001, 4, 3));
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 30, 3)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 31, 3)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 32, 3)); /* Illegal position */
   EXPECT_EQ (0ul, pf_get_bits (0x00000010, 33, 3)); /* Illegal position */

   /* 10000000 00000000 00000000 00000000 */
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x80000000, 0, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x80000000, 1, 3));
   EXPECT_EQ (0x00000000ul, pf_get_bits (0x80000000, 28, 3));
   EXPECT_EQ (0x00000004ul, pf_get_bits (0x80000000, 29, 3));
   EXPECT_EQ (0ul, pf_get_bits (0x80000000, 30, 3)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0x80000000, 31, 3)); /* Illegal length and
                                                        position combination*/
   EXPECT_EQ (0ul, pf_get_bits (0x80000000, 32, 3)); /* Illegal position */
   EXPECT_EQ (0ul, pf_get_bits (0x80000000, 33, 3)); /* Illegal position */
}


<|start_filename|>src/device/pf_cmdmc.c<|end_filename|>
/*********************************************************************
 *        _       _         _
 *  _ __ | |_  _ | |  __ _ | |__   ___
 * | '__|| __|(_)| | / _` || '_ \ / __|
 * | |   | |_  _ | || (_| || |_) |\__ \
 * |_|    \__|(_)|_| \__,_||_.__/ |___/
 *
 * www.rt-labs.com
 * Copyright 2018 rt-labs AB, Sweden.
 *
 * This software is dual-licensed under GPLv3 and a commercial
 * license. See the file LICENSE.md distributed with this software for
 * full license information.
 ********************************************************************/

#ifdef UNIT_TEST

#endif

#include "pf_includes.h"

int pf_cmdmc_activate_req (pf_ar_t * p_ar)
{
   return 0;
}

int pf_cmdmc_close_req (pf_ar_t * p_ar)
{
   return 0;
}

int pf_cmdmc_cpm_state_ind (pf_ar_t * p_ar, uint16_t ix, bool start)
{
   return 0;
}


<|start_filename|>test/test_cpm.cpp<|end_filename|>
/*********************************************************************
 *        _       _         _
 *  _ __ | |_  _ | |  __ _ | |__   ___
 * | '__|| __|(_)| | / _` || '_ \ / __|
 * | |   | |_  _ | || (_| || |_) |\__ \
 * |_|    \__|(_)|_| \__,_||_.__/ |___/
 *
 * www.rt-labs.com
 * Copyright 2018 rt-labs AB, Sweden.
 *
 * This software is dual-licensed under GPLv3 and a commercial
 * license. See the file LICENSE.md distributed with this software for
 * full license information.
 ********************************************************************/

#include "utils_for_testing.h"
#include "mocks.h"

#include "pf_includes.h"

#include <gtest/gtest.h>

class CpmUnitTest : public PnetUnitTest
{
};

TEST_F (CpmUnitTest, CpmCheckCycle)
{
   EXPECT_EQ (-1, pf_cpm_check_cycle (1, 0xFFFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (1, 0));
   EXPECT_EQ (-1, pf_cpm_check_cycle (1, 1));
   EXPECT_EQ (0, pf_cpm_check_cycle (1, 2));
   EXPECT_EQ (0, pf_cpm_check_cycle (1, 3));

   /* Previous counter value was 0x4000 */
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x0000));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x0001));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x2FFE));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x2FFF));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x3000));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x4000, 0x3001));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x4000, 0x3002));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x4000, 0x3FFD));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x4000, 0x3FFE));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x4000, 0x3FFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x4000, 0x4000));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x4001));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0x4002));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x4000, 0xFFFF));

   /* Previous counter value was 0xFFFF (Forbidden zone ends at 0xFFFF) */
   EXPECT_EQ (0, pf_cpm_check_cycle (0xFFFF, 0xEFFE));
   EXPECT_EQ (0, pf_cpm_check_cycle (0xFFFF, 0xEFFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0xFFFF, 0xF000));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0xFFFF, 0xF001));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0xFFFF, 0xFFFE));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0xFFFF, 0xFFFF));
   EXPECT_EQ (0, pf_cpm_check_cycle (0xFFFF, 0x0000));
   EXPECT_EQ (0, pf_cpm_check_cycle (0xFFFF, 0x0001));
   EXPECT_EQ (0, pf_cpm_check_cycle (0xFFFF, 0x0002));

   /* Previous counter value was 0x0000 (Forbidden zone ends at 0x0000) */
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0000, 0xEFFE));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0000, 0xEFFF));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0000, 0xF000));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0000, 0xF001));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0000, 0xFFFE));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0000, 0xFFFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0000, 0x0000));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0000, 0x0001));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0000, 0x0002));

   /* Previous counter value was 0x0FFE (Forbidden zone starts at 0xFFFF) */
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFE, 0xFFFE));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFE, 0xFFFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFE, 0x0000));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFE, 0x0001));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFE, 0x0FFE));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFE, 0x0FFF));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFE, 0x1000));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFE, 0x1001));

   /* Previous counter value was 0x0FFF (Forbidden zone starts at zero) */
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFF, 0xFFFE));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFF, 0xFFFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFF, 0x0000));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFF, 0x0001));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFF, 0x0FFE));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0FFF, 0x0FFF));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFF, 0x1000));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0FFF, 0x1001));

   /* Previous counter value was 0x0010 (Forbidden zone overlaps zero) */
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0010, 0xF00F));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0010, 0xF010));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0xF011));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0xFFFE));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0xFFFF));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0x0000));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0x0001));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0x000E));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0x000F));
   EXPECT_EQ (-1, pf_cpm_check_cycle (0x0010, 0x0010));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0010, 0x0011));
   EXPECT_EQ (0, pf_cpm_check_cycle (0x0010, 0x0012));
}


<|start_filename|>src/device/pf_cmrpc_helpers.h<|end_filename|>
/*********************************************************************
 *        _       _         _
 *  _ __ | |_  _ | |  __ _ | |__   ___
 * | '__|| __|(_)| | / _` || '_ \ / __|
 * | |   | |_  _ | || (_| || |_) |\__ \
 * |_|    \__|(_)|_| \__,_||_.__/ |___/
 *
 * www.rt-labs.com
 * Copyright 2018 rt-labs AB, Sweden.
 *
 * This software is dual-licensed under GPLv3 and a commercial
 * license. See the file LICENSE.md distributed with this software for
 * full license information.
 ********************************************************************/

#ifndef PF_CMRPC_HELPERS_H
#define PF_CMRPC_HELPERS_H

#ifdef __cplusplus
extern "C" {
#endif

/************ Internal functions, made available for unit testing ************/

void pf_generate_uuid (
   uint32_t timestamp,
   uint32_t session_number,
   pnet_ethaddr_t mac_address,
   pf_uuid_t * p_uuid);

#ifdef __cplusplus
}
#endif

#endif /* PF_CMRPC_HELPERS_H */
