1 /*
2  * SPDX-FileCopyrightText: 2017 Nordic Semiconductor ASA
3  * SPDX-FileCopyrightText: 2016 Vinayak Kariappa Chettimada
4  * SPDX-FileCopyrightText: 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <string.h>
10 #include <errno.h>
11 
12 #include "mesh_types.h"
13 #include "mesh_util.h"
14 #include "mesh_trace.h"
15 
bt_hex(const void * buf,size_t len)16 const char *bt_hex(const void *buf, size_t len)
17 {
18     static const char hex[] = "0123456789abcdef";
19     static char hexbufs[2][129];
20     static uint8_t curbuf;
21     const uint8_t *b = buf;
22     char *str = NULL;
23     int i;
24 
25     str = hexbufs[curbuf++];
26     curbuf %= ARRAY_SIZE(hexbufs);
27 
28     len = MIN(len, (sizeof(hexbufs[0]) - 1) / 2);
29 
30     for (i = 0; i < len; i++) {
31         str[i * 2]     = hex[b[i] >> 4];
32         str[i * 2 + 1] = hex[b[i] & 0xf];
33     }
34 
35     str[i * 2] = '\0';
36 
37     return str;
38 }
39 
mem_rcopy(uint8_t * dst,uint8_t const * src,uint16_t len)40 void mem_rcopy(uint8_t *dst, uint8_t const *src, uint16_t len)
41 {
42     src += len;
43     while (len--) {
44         *dst++ = *--src;
45     }
46 }
47 
48 #ifdef CONFIG_BLE_MESH_BQB_TEST_LOG
49 
50 enum BLE_MESH_BQB_TEST_FLAG_OP {
51     BLE_MESH_BQB_TEST_FLAG_OP_GET = 0,
52     BLE_MESH_BQB_TEST_FLAG_OP_SET,
53 };
54 
bt_mesh_bqb_test_flag(uint8_t op,uint32_t value)55 static uint32_t bt_mesh_bqb_test_flag(uint8_t op, uint32_t value)
56 {
57     static uint32_t bqb_log_flag = 0;
58 
59     switch (op) {
60     case BLE_MESH_BQB_TEST_FLAG_OP_GET:
61         break;
62     case BLE_MESH_BQB_TEST_FLAG_OP_SET:
63         bqb_log_flag = value;
64         break;
65     default:
66         BT_ERR("Unknown BQB test flag opcode 0x%02x", op);
67         break;
68     }
69 
70     return bqb_log_flag;
71 }
72 
bt_mesh_bqb_test_flag_get(void)73 uint32_t bt_mesh_bqb_test_flag_get(void)
74 {
75     return bt_mesh_bqb_test_flag(BLE_MESH_BQB_TEST_FLAG_OP_GET, 0);
76 }
77 
bt_mesh_bqb_test_flag_set(uint32_t flag_mask)78 int bt_mesh_bqb_test_flag_set(uint32_t flag_mask)
79 {
80     if (flag_mask > BLE_MESH_BQB_TEST_LOG_LEVEL_OUTPUT_NONE) {
81         BT_ERR("Invalid BQB test flag mask 0x%08x", flag_mask);
82         return -EINVAL;
83     }
84 
85     return (bt_mesh_bqb_test_flag(BLE_MESH_BQB_TEST_FLAG_OP_SET, flag_mask) == flag_mask) ? 0 : -EINVAL;
86 }
87 
bt_mesh_bqb_test_flag_check(uint32_t flag_mask)88 bool bt_mesh_bqb_test_flag_check(uint32_t flag_mask)
89 {
90     if (flag_mask > BLE_MESH_BQB_TEST_LOG_LEVEL_OUTPUT_NONE) {
91         BT_ERR("Invalid BQB test flag mask 0x%08x", flag_mask);
92         return false;
93     }
94 
95     return ((bt_mesh_bqb_test_flag_get() & flag_mask) == flag_mask);
96 }
97 
98 #endif /* CONFIG_BLE_MESH_BQB_TEST_LOG */
99