Blame mesh/util.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2018  Intel Corporation. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This library is free software; you can redistribute it and/or
Packit 34410b
 *  modify it under the terms of the GNU Lesser General Public
Packit 34410b
 *  License as published by the Free Software Foundation; either
Packit 34410b
 *  version 2.1 of the License, or (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This library is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 34410b
 *  Lesser General Public License for more details.
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#define _GNU_SOURCE
Packit 34410b
#include <dirent.h>
Packit 34410b
#include <ftw.h>
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <limits.h>
Packit 34410b
#include <time.h>
Packit 34410b
#include <sys/time.h>
Packit 34410b
Packit 34410b
#include <ell/ell.h>
Packit 34410b
Packit 34410b
#include "mesh/util.h"
Packit 34410b
Packit 34410b
void print_packet(const char *label, const void *data, uint16_t size)
Packit 34410b
{
Packit 34410b
	struct timeval pkt_time;
Packit 34410b
Packit 34410b
	gettimeofday(&pkt_time, NULL);
Packit 34410b
Packit 34410b
	if (size > 0) {
Packit 34410b
		char *str;
Packit 34410b
Packit 34410b
		str = l_util_hexstring(data, size);
Packit 34410b
		l_debug("%05d.%03d %s: %s",
Packit 34410b
				(uint32_t) pkt_time.tv_sec % 100000,
Packit 34410b
				(uint32_t) pkt_time.tv_usec/1000, label, str);
Packit 34410b
		l_free(str);
Packit 34410b
	} else
Packit 34410b
		l_debug("%05d.%03d %s: empty",
Packit 34410b
				(uint32_t) pkt_time.tv_sec % 100000,
Packit 34410b
				(uint32_t) pkt_time.tv_usec/1000, label);
Packit 34410b
}
Packit 34410b
Packit 34410b
uint32_t get_timestamp_secs(void)
Packit 34410b
{
Packit 34410b
	struct timespec ts;
Packit 34410b
Packit 34410b
	clock_gettime(CLOCK_MONOTONIC, &ts);
Packit 34410b
	return ts.tv_sec;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool str2hex(const char *str, uint16_t in_len, uint8_t *out,
Packit 34410b
							uint16_t out_len)
Packit 34410b
{
Packit 34410b
	uint16_t i;
Packit 34410b
Packit 34410b
	if (in_len < out_len * 2)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	for (i = 0; i < out_len; i++) {
Packit 34410b
		if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1)
Packit 34410b
			return false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
size_t hex2str(uint8_t *in, size_t in_len, char *out, size_t out_len)
Packit 34410b
{
Packit 34410b
	static const char hexdigits[] = "0123456789abcdef";
Packit 34410b
	size_t i;
Packit 34410b
Packit 34410b
	if (in_len * 2 > (out_len - 1))
Packit 34410b
		return 0;
Packit 34410b
Packit 34410b
	for (i = 0; i < in_len; i++) {
Packit 34410b
		out[i * 2] = hexdigits[in[i] >> 4];
Packit 34410b
		out[i * 2 + 1] = hexdigits[in[i] & 0xf];
Packit 34410b
	}
Packit 34410b
Packit 34410b
	out[in_len * 2] = '\0';
Packit 34410b
	return i;
Packit 34410b
}
Packit 34410b
Packit 34410b
int create_dir(const char *dir_name)
Packit 34410b
{
Packit 34410b
	struct stat st;
Packit 34410b
	char dir[PATH_MAX + 1], *prev, *next;
Packit 34410b
	int err;
Packit 34410b
Packit 34410b
	err = stat(dir_name, &st);
Packit 34410b
	if (!err && S_ISREG(st.st_mode))
Packit 34410b
		return 0;
Packit 34410b
Packit 34410b
	memset(dir, 0, PATH_MAX + 1);
Packit 34410b
	strcat(dir, "/");
Packit 34410b
Packit 34410b
	prev = strchr(dir_name, '/');
Packit 34410b
Packit 34410b
	while (prev) {
Packit 34410b
		next = strchr(prev + 1, '/');
Packit 34410b
		if (!next)
Packit 34410b
			break;
Packit 34410b
Packit 34410b
		if (next - prev == 1) {
Packit 34410b
			prev = next;
Packit 34410b
			continue;
Packit 34410b
		}
Packit 34410b
Packit 34410b
		strncat(dir, prev + 1, next - prev);
Packit 34410b
		mkdir(dir, 0755);
Packit 34410b
Packit 34410b
		prev = next;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	mkdir(dir_name, 0755);
Packit 34410b
Packit 34410b
	return 0;
Packit 34410b
}