blob: c0497fd5b4ad3b63a62b9813dc189878861e31b7 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Cyril Bur314929b2016-10-14 15:55:16 +11003#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
Ratan Gupta90b92fe2017-05-05 17:34:00 +053015void (*mbox_vlog)(int p, const char *fmt, va_list args);
16
17enum verbose verbosity;
18
Cyril Bur314929b2016-10-14 15:55:16 +110019void 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)))
32void mbox_log(int p, const char *fmt, ...)
33{
Andrew Jeffery44ac0782018-02-26 13:03:40 +103034 static bool warned = false;
Cyril Bur314929b2016-10-14 15:55:16 +110035 va_list args;
36
Andrew Jeffery44ac0782018-02-26 13:03:40 +103037 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
Cyril Bur314929b2016-10-14 15:55:16 +110047 va_start(args, fmt);
48 mbox_vlog(p, fmt, args);
49 va_end(args);
50}
51
52uint16_t get_u16(uint8_t *ptr)
53{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103054 return le16toh(*(uint16_t *)ptr);
Cyril Bur314929b2016-10-14 15:55:16 +110055}
56
57void put_u16(uint8_t *ptr, uint16_t val)
58{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103059 val = htole16(val);
Cyril Bur314929b2016-10-14 15:55:16 +110060 memcpy(ptr, &val, sizeof(val));
61}
62
63uint32_t get_u32(uint8_t *ptr)
64{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103065 return le32toh(*(uint32_t *)ptr);
Cyril Bur314929b2016-10-14 15:55:16 +110066}
67
68void put_u32(uint8_t *ptr, uint32_t val)
69{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103070 val = htole32(val);
Cyril Bur314929b2016-10-14 15:55:16 +110071 memcpy(ptr, &val, sizeof(val));
72}
73