-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.c
148 lines (125 loc) · 4.16 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "util.h"
#include "error.h"
#include "snapcast.h"
#include "syscallwrappers.h"
#include <time.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/epoll.h>
#define STRBUFELEMENTS 4
static char strbuffer[STRBUFELEMENTS][INET6_ADDRSTRLEN + 1];
static int str_bufferoffset = 0;
int max(const int a, const int b) {
if (a > b)
return a;
return b;
}
/* print a human-readable representation of an in6_addr struct to string buffer which can then be printed to wherever
** ** */
const char *print_ip(const struct in6_addr *addr) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
return inet_ntop(AF_INET6, &(addr->s6_addr), strbuffer[str_bufferoffset], INET6_ADDRSTRLEN);
}
/** print a timespec
*/
const char *print_timespec(const struct timespec *t) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "%lu.%09lu", t->tv_sec, t->tv_nsec);
return strbuffer[str_bufferoffset];
}
void log_error(const char *format, ...) {
struct timespec ctime;
obtainsystime(&ctime);
fprintf(stderr, "%s: ", print_timespec(&ctime));
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
void log_debug(const char *format, ...) {
if (!snapctx.debug)
return;
struct timespec ctime;
obtainsystime(&ctime);
fprintf(stderr, "%s: ", print_timespec(&ctime));
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
void log_verbose(const char *format, ...) {
if (!snapctx.verbose)
return;
struct timespec ctime;
obtainsystime(&ctime);
fprintf(stderr, "%s: ", print_timespec(&ctime));
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
void add_fd(int efd, int fd, uint32_t events) {
log_debug("watching fd %d using epoll\n", fd);
struct epoll_event event = {};
event.data.fd = fd;
event.events = events;
if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &event) == -1)
exit_errno("error on add_fd %d on efd %d, epoll_ctl (ADD)", fd, efd);
}
void del_fd(int efd, int fd) {
if (epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL) == -1)
exit_errno("error on del_fd %d on efd %d, epoll_ctl(DEL)", fd, efd);
}
const char *print_mac(const uint8_t mac[6]) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
snprintf(strbuffer[str_bufferoffset], 18, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return strbuffer[str_bufferoffset];
}
const char *print_codec(int codec) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
if (codec == PCM)
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "pcm");
else if (codec == OPUS)
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "opus");
else
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "unknown");
return strbuffer[str_bufferoffset];
}
const char *print_stream_protocol(int protocol) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
if (protocol == PIPE)
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "pipe");
else
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "unknown");
return strbuffer[str_bufferoffset];
}
const char *print_inputpipe_status(int status) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
if (status == IDLE)
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "idle");
else if (status == PLAYING)
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "playing");
else if (status == THROTTLE)
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "throttle");
else
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "unknown");
return strbuffer[str_bufferoffset];
}
const char *print_chunk(pcmChunk *chunk) {
str_bufferoffset = (str_bufferoffset + 1) % STRBUFELEMENTS;
snprintf(strbuffer[str_bufferoffset], INET6_ADDRSTRLEN, "codec: %i play_at: %u.%09u", chunk->codec, chunk->play_at_tv_sec,
chunk->play_at_tv_nsec);
return strbuffer[str_bufferoffset];
}
void print_packet(unsigned char *buf, int size) {
if (!snapctx.debug)
return;
log_debug("Packet: [");
for (int i = 0; i < size; i++) {
if (i % 4 == 0)
log_debug(" ");
log_debug("%02hhX", buf[i]);
}
log_debug("]\n");
}