xref: /openbmc/hiomapd/common.c (revision f1e547c7)
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 #define _GNU_SOURCE
4 #include <stdarg.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <syslog.h>
11 #include <time.h>
12 
13 #include "common.h"
14 
15 void (*mbox_vlog)(int p, const char *fmt, va_list args);
16 
17 enum verbose verbosity;
18 
19 void mbox_log_console(int p, const char *fmt, va_list args)
20 {
21 	struct timespec time;
22 	FILE *s = (p < LOG_WARNING) ? stdout : stderr;
23 
24 	clock_gettime(CLOCK_REALTIME, &time);
25 
26 	fprintf(s, "[%s %ld.%.9ld] ", PREFIX, time.tv_sec, time.tv_nsec);
27 
28 	vfprintf(s, fmt, args);
29 }
30 
31 __attribute__((format(printf, 2, 3)))
32 void mbox_log(int p, const char *fmt, ...)
33 {
34 	static bool warned = false;
35 	va_list args;
36 
37 	if (!mbox_vlog) {
38 		if (!warned) {
39 			fprintf(stderr, "Logging backend not configured, "
40 					"log output disabled\n");
41 			warned = true;
42 		}
43 
44 		return;
45 	}
46 
47 	va_start(args, fmt);
48 	mbox_vlog(p, fmt, args);
49 	va_end(args);
50 }
51 
52 uint16_t get_u16(uint8_t *ptr)
53 {
54 	return le16toh(*(uint16_t *)ptr);
55 }
56 
57 void put_u16(uint8_t *ptr, uint16_t val)
58 {
59 	val = htole16(val);
60 	memcpy(ptr, &val, sizeof(val));
61 }
62 
63 uint32_t get_u32(uint8_t *ptr)
64 {
65 	return le32toh(*(uint32_t *)ptr);
66 }
67 
68 void put_u32(uint8_t *ptr, uint32_t val)
69 {
70 	val = htole32(val);
71 	memcpy(ptr, &val, sizeof(val));
72 }
73 
74