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);
131 bool _bt_mesh_util_opcode_get(const uint8_t *buf,
132 uint16_t sz, uint32_t *opcode, int *n)
134 BT_INFO("Mesh: Opcode Get DatLen [%d] Buf0 [0x%x]",
136 if (!n || !opcode || sz < 1) return false;
138 switch (buf[0] & 0xc0) {
154 *opcode = l_get_be16(buf);
162 *opcode = l_get_be16(buf + 1);
163 *opcode |= buf[0] << 16;
167 BT_ERR("Mesh: Bad Packet:\n");
168 _bt_mesh_util_print_byte_array("\t", (void *) buf, sz);
176 uint32_t _bt_mesh_util_get_timestamp_secs(void)
180 clock_gettime(CLOCK_MONOTONIC, &ts);
184 bool _bt_mesh_util_convert_string_to_hex(const char *str,
185 uint16_t in_len, uint8_t *out,
190 if (in_len < out_len * 2)
193 for (i = 0; i < out_len; i++) {
194 if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1)
201 size_t _bt_mesh_util_convert_hex_to_string(uint8_t *in,
202 size_t in_len, char *out, size_t out_len)
204 static const char hexdigits[] = "0123456789abcdef";
207 if (in_len * 2 > (out_len - 1))
210 for (i = 0; i < in_len; i++) {
211 out[i * 2] = hexdigits[in[i] >> 4];
212 out[i * 2 + 1] = hexdigits[in[i] & 0xf];
215 out[in_len * 2] = '\0';
219 void _bt_mesh_util_print_packet(const char *label,
220 const void *data, uint16_t size)
222 struct timeval pkt_time;
224 gettimeofday(&pkt_time, NULL);
229 str = l_util_hexstring(data, size);
230 BT_DBG("%05d.%03d %s: %s",
231 (uint32_t) pkt_time.tv_sec % 100000,
232 (uint32_t) pkt_time.tv_usec/1000, label, str);
235 BT_DBG("%05d.%03d %s: empty",
236 (uint32_t) pkt_time.tv_sec % 100000,
237 (uint32_t) pkt_time.tv_usec/1000, label);
240 bool _bt_mesh_util_create_directory(const char *mesh_dir)
243 BT_ERR("Mesh: Create [%s]", mesh_dir);
245 if (stat(mesh_dir, &st) == 0) {
246 if (!S_ISDIR(st.st_mode)) {
247 BT_ERR("Mesh: [%s] not a directory", mesh_dir);
250 } else if (errno == ENOENT) {
251 BT_ERR("Mesh: Dir not available: [%s]", mesh_dir);
252 if (mkdir(mesh_dir, 0755) != 0) {
253 BT_ERR("Mesh: Dir creation failed: [%s] error [%d]",
258 BT_ERR("Mesh: Cannot open config directory");
265 bool _bt_mesh_util_is_directory_exists(const char *dir_path)
269 if (stat(dir_path, &st) == 0) {
270 if (!S_ISDIR(st.st_mode)) {
271 BT_ERR("Mesh: [%s] not a directory", dir_path);
274 } else if (errno == ENOENT) {