4 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 * @author: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing prov_err_strs and
18 * limitations under the License.
32 #include "bt-service-common.h"
33 #include "bt-service-util.h"
35 #include "bt-service-mesh-util.h"
38 /* Multiply used Zero array */
39 static const uint8_t zero[16] = { 0, };
41 static bool __mesh_util_crypto_aes_cmac_one(
42 const uint8_t key[16], const void *msg,
43 size_t msg_len, uint8_t res[16])
48 checksum = l_checksum_new_cmac_aes(key, 16);
52 result = l_checksum_update(checksum, msg, msg_len);
55 ssize_t len = l_checksum_get_digest(checksum, res, 16);
56 result = !!(len == 16);
59 l_checksum_free(checksum);
64 bool _bt_mesh_util_crypto_s1(const void *info,
65 size_t len, uint8_t salt[16])
67 return __mesh_util_crypto_aes_cmac_one(zero,
71 bool _bt_mesh_util_crypto_create_virtual_address(
72 const uint8_t virtual_label[16], uint16_t *addr)
76 if (!_bt_mesh_util_crypto_s1("vtad", 4, tmp))
79 if (!addr || !__mesh_util_crypto_aes_cmac_one(tmp,
80 virtual_label, 16, tmp))
83 *addr = (l_get_be16(tmp + 14) & 0x3fff) | 0x8000;
87 void _bt_mesh_util_print_byte_array(const char *prefix,
88 const void *ptr, int len)
90 const uint8_t *data = ptr;
94 line = g_malloc(strlen(prefix) + (16 * 3) + 2);
95 snprintf(line, strlen(prefix), "%s ", prefix);
96 bytes = line + strlen(prefix) + 1;
98 for (i = 0; i < len; ++i) {
99 snprintf(bytes, 4, "%2.2x ", data[i]);
103 BT_INFO("\r%s\n", line);
104 bytes = line + strlen(prefix) + 1;
109 BT_INFO("\r%s\n", line);
114 uint16_t _bt_mesh_util_opcode_set(uint32_t opcode,
117 if (opcode <= 0x7e) {
120 } else if (opcode >= 0x8000 && opcode <= 0xbfff) {
121 l_put_be16(opcode, buf);
123 } else if (opcode >= 0xc00000 && opcode <= 0xffffff) {
124 buf[0] = (opcode >> 16) & 0xff;
125 l_put_be16(opcode, buf + 1);
132 bool _bt_mesh_util_opcode_get(const uint8_t *buf,
133 uint16_t sz, uint32_t *opcode, int *n)
135 BT_INFO("Mesh: Opcode Get DatLen [%d] Buf0 [0x%x]",
137 if (!n || !opcode || sz < 1) return false;
139 switch (buf[0] & 0xc0) {
155 *opcode = l_get_be16(buf);
163 *opcode = l_get_be16(buf + 1);
164 *opcode |= buf[0] << 16;
168 BT_ERR("Mesh: Bad Packet:\n");
169 _bt_mesh_util_print_byte_array("\t", (void *) buf, sz);
177 uint32_t _bt_mesh_util_get_timestamp_secs(void)
181 clock_gettime(CLOCK_MONOTONIC, &ts);
185 bool _bt_mesh_util_convert_string_to_hex(const char *str,
186 uint16_t in_len, uint8_t *out,
191 if (in_len < out_len * 2)
194 for (i = 0; i < out_len; i++) {
195 if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1)
202 size_t _bt_mesh_util_convert_hex_to_string(uint8_t *in,
203 size_t in_len, char *out, size_t out_len)
205 static const char hexdigits[] = "0123456789abcdef";
208 if (in_len * 2 > (out_len - 1))
211 for (i = 0; i < in_len; i++) {
212 out[i * 2] = hexdigits[in[i] >> 4];
213 out[i * 2 + 1] = hexdigits[in[i] & 0xf];
216 out[in_len * 2] = '\0';
220 void _bt_mesh_util_print_packet(const char *label,
221 const void *data, uint16_t size)
223 struct timeval pkt_time;
225 gettimeofday(&pkt_time, NULL);
230 str = l_util_hexstring(data, size);
231 BT_DBG("%05d.%03d %s: %s",
232 (uint32_t) pkt_time.tv_sec % 100000,
233 (uint32_t) pkt_time.tv_usec/1000, label, str);
236 BT_DBG("%05d.%03d %s: empty",
237 (uint32_t) pkt_time.tv_sec % 100000,
238 (uint32_t) pkt_time.tv_usec/1000, label);
241 bool _bt_mesh_util_create_directory(const char *mesh_dir)
244 BT_ERR("Mesh: Create [%s]", mesh_dir);
246 if (stat(mesh_dir, &st) == 0) {
247 if (!S_ISDIR(st.st_mode)) {
248 BT_ERR("Mesh: [%s] not a directory", mesh_dir);
251 } else if (errno == ENOENT) {
252 BT_ERR("Mesh: Dir not available: [%s]", mesh_dir);
253 if (mkdir(mesh_dir, 0755) != 0) {
254 BT_ERR("Mesh: Dir creation failed: [%s] error [%d]",
259 BT_ERR("Mesh: Cannot open config directory");
266 bool _bt_mesh_util_is_directory_exists(const char *dir_path)
270 if (stat(dir_path, &st) == 0) {
271 if (!S_ISDIR(st.st_mode)) {
272 BT_ERR("Mesh: [%s] not a directory", dir_path);
275 } else if (errno == ENOENT) {