1 /* Copyright 2020 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <string.h>
7 #include <time.h>
8 
9 #include "cras_util.h"
10 #include "packet_status_logger.h"
11 
packet_status_logger_init(struct packet_status_logger * logger)12 void packet_status_logger_init(struct packet_status_logger *logger)
13 {
14 	memset(logger->data, 0, PACKET_STATUS_LEN_BYTES);
15 	logger->size = PACKET_STATUS_LEN_BYTES * 8;
16 	logger->wp = 0;
17 	logger->num_wraps = 0;
18 	clock_gettime(CLOCK_MONOTONIC_RAW, &logger->ts);
19 }
20 
packet_status_logger_update(struct packet_status_logger * logger,bool val)21 void packet_status_logger_update(struct packet_status_logger *logger, bool val)
22 {
23 	if (val) {
24 		logger->data[logger->wp / 8] |= 1UL << (logger->wp % 8);
25 	} else {
26 		logger->data[logger->wp / 8] &= ~(1UL << (logger->wp % 8));
27 	}
28 	logger->wp++;
29 	if (logger->wp >= logger->size) {
30 		logger->wp %= logger->size;
31 		logger->num_wraps += 1;
32 	}
33 	if (logger->wp == 0 || (logger->num_wraps == 0 && logger->wp == 1))
34 		clock_gettime(CLOCK_MONOTONIC_RAW, &logger->ts);
35 }
36