diff options
author | Inga Stotland <inga.stotland@intel.com> | 2018-07-14 12:58:58 -0700 |
---|---|---|
committer | Brian Gix <brian.gix@intel.com> | 2018-08-20 12:48:23 -0700 |
commit | e594aefd3a5c42b2e0644a6f822266b3abb9026f (patch) | |
tree | 3d570b10cdd9ac703970eabeb4b4b3fb91ce28ed /mesh/util.c | |
parent | 6fbc4c83e13470d669e16ea05cc1def297e71665 (diff) | |
download | bluez-e594aefd3a5c42b2e0644a6f822266b3abb9026f.tar.gz |
mesh: Source files for mesh access layer and utilities
This adds initial implementation of BT Mesh access layer
functionality plus utilities.
Diffstat (limited to 'mesh/util.c')
-rw-r--r-- | mesh/util.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/mesh/util.c b/mesh/util.c new file mode 100644 index 000000000..5d771431c --- /dev/null +++ b/mesh/util.c @@ -0,0 +1,71 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <time.h> + +#include "mesh/util.h" + +uint32_t get_timestamp_secs(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec; +} + +bool str2hex(const char *str, uint16_t in_len, uint8_t *out, + uint16_t out_len) +{ + uint16_t i; + + if (in_len < out_len * 2) + return false; + + for (i = 0; i < out_len; i++) { + if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1) + return false; + } + + return true; +} + +size_t hex2str(uint8_t *in, size_t in_len, char *out, size_t out_len) +{ + static const char hexdigits[] = "0123456789abcdef"; + size_t i; + + if (in_len * 2 > (out_len - 1)) + return 0; + + for (i = 0; i < in_len; i++) { + out[i * 2] = hexdigits[in[i] >> 4]; + out[i * 2 + 1] = hexdigits[in[i] & 0xf]; + } + + out[in_len * 2] = '\0'; + return i; +} |