Skip to content

Commit d5f72b7

Browse files
author
Ian Gabaraev
committed
Add relay
1 parent dd663cb commit d5f72b7

24 files changed

Lines changed: 1017 additions & 1066 deletions

cf renamed to .clang-format

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# Generated from CLion C/C++ Code Style settings
12
---
23
Language: Cpp
3-
BasedOnStyle: Google
4+
BasedOnStyle: LLVM
45
AccessModifierOffset: -4
56
AlignConsecutiveAssignments: false
67
AlignConsecutiveDeclarations: false
78
AlignOperands: true
89
AlignTrailingComments: false
10+
AlwaysBreakTemplateDeclarations: Yes
911
BraceWrapping:
1012
AfterCaseLabel: false
1113
AfterClass: false
@@ -29,7 +31,6 @@ BreakConstructorInitializersBeforeComma: false
2931
ColumnLimit: 120
3032
ConstructorInitializerAllOnOneLineOrOnePerLine: false
3133
ContinuationIndentWidth: 8
32-
IncludeBlocks: Preserve
3334
IncludeCategories:
3435
- Regex: '^<.*'
3536
Priority: 1
@@ -38,18 +39,17 @@ IncludeCategories:
3839
- Regex: '.*'
3940
Priority: 3
4041
IncludeIsMainRegex: '([-_](test|unittest))?$'
42+
IndentCaseLabels: true
4143
IndentWidth: 4
4244
InsertNewlineAtEOF: true
4345
MacroBlockBegin: ''
4446
MacroBlockEnd: ''
4547
MaxEmptyLinesToKeep: 2
4648
NamespaceIndentation: All
47-
PointerAlignment: Right
4849
SpaceAfterCStyleCast: true
4950
SpaceAfterTemplateKeyword: false
5051
SpaceBeforeRangeBasedForLoopColon: false
5152
SpaceInEmptyParentheses: false
52-
SpacesBeforeTrailingComments: 1
5353
SpacesInAngles: false
5454
SpacesInConditionalStatement: false
5555
SpacesInCStyleCastParentheses: false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ dkms.conf
6262
*.crt
6363
*.csr
6464
*.pem
65+
66+
# Log
67+
*.log

main.c

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <stdlib.h>
55
#include <string.h>
66

7-
#include "MQTTClient.h"
87
#include "src/buffer/synchronous_single_buffer.h"
98
#include "src/capture/capture.h"
109
#include "src/config/config.h"
@@ -16,61 +15,56 @@ AppConfig app_config;
1615
AvailableDevice available_devices;
1716

1817
int input_device_id(const AvailableDevice *devices) {
19-
int c;
20-
printf("\u25b6 Please enter device #: \n");
21-
while ((c = getchar()) != EOF && !isspace(c)) {
22-
const int n = c - '0';
23-
if (in_array(devices->device_ids, devices->device_count, n)) {
24-
return n;
18+
int c;
19+
printf("\u25b6 Please enter device #: \n");
20+
while ((c = getchar()) != EOF && !isspace(c)) {
21+
const int n = c - '0';
22+
if (in_array(devices->device_ids, devices->device_count, n)) {
23+
return n;
24+
}
2525
}
26-
}
27-
printf("Not found. Using default # 0\n");
28-
return 0;
26+
printf("Not found. Using default # 0\n");
27+
return 0;
2928
}
3029

3130
SynchronousSingleBuffer buffer;
31+
int16_t storage[SYNCHRONOUS_SINGULAR_BUFFER_SIZE] = {0};
3232

3333
int main(const int argc, char *argv[]) {
34-
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
35-
help();
36-
return 0;
37-
}
38-
if (argc <= 1) {
39-
printf("Args missing");
40-
return 0;
41-
}
42-
const char *mqtt_topic = argv[2];
43-
const char *certs_path = argv[3];
44-
const char *aws_endpoint = argv[4];
45-
// MQTTConfig mqtt_config;
46-
// init_config(mqtt_topic, certs_path, aws_endpoint, &mqtt_config);
47-
// MQTTClient client;
48-
// init_client(&client, &mqtt_config);
49-
// send_message(&client, &mqtt_config, "Device connected");
34+
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
35+
help();
36+
return 0;
37+
}
38+
if (argc <= 1) {
39+
printf("Args missing");
40+
return 0;
41+
}
42+
const char *mqtt_topic = argv[2];
43+
const char *certs_path = argv[3];
44+
const char *aws_endpoint = argv[4];
45+
MQTTConfig mqtt_config;
46+
init_config(mqtt_topic, certs_path, aws_endpoint, &mqtt_config);
47+
init_client(&mqtt_config);
48+
send_message(&mqtt_config, "Device connected");
5049

51-
suppress_alsa_errors();
52-
int16_t *storage = calloc(SYNCHRONOUS_SINGULAR_BUFFER_SIZE, sizeof(int16_t));
53-
buffer.storage = storage;
54-
init_buffer(&buffer, storage);
50+
suppress_alsa_errors();
51+
buffer.storage = storage;
52+
init_buffer(&buffer, storage);
5553

56-
load_ultrasonic_devices(&available_devices);
57-
describe_available_ultrasonic_devices(&available_devices);
54+
load_ultrasonic_devices(&available_devices);
55+
describe_available_ultrasonic_devices(&available_devices);
5856

59-
const int device_id = input_device_id(&available_devices);
60-
(void)getchar();
61-
AudioDevice audio_device = available_devices.devices[device_id];
57+
const int device_id = input_device_id(&available_devices);
58+
(void) getchar();
59+
AudioDevice audio_device = available_devices.devices[device_id];
6260

63-
pthread_t r_thread;
64-
ReaderContext reader_context = {&buffer, SYNCHRONOUS_SINGULAR_BUFFER_SIZE,
65-
audio_device.default_sample_rate_hz};
66-
pthread_create(&r_thread, NULL, (void *(*)(void *))reader_thread,
67-
&reader_context);
61+
pthread_t r_thread;
62+
ReaderContext reader_context = {&buffer, SYNCHRONOUS_SINGULAR_BUFFER_SIZE, audio_device.default_sample_rate_hz};
63+
pthread_create(&r_thread, NULL, (void *(*) (void *) ) reader_thread, &reader_context);
6864

69-
start_stream(SYNCHRONOUS_SINGULAR_BUFFER_SIZE, &audio_device, &buffer);
70-
pthread_join(r_thread, NULL);
65+
start_stream(SYNCHRONOUS_SINGULAR_BUFFER_SIZE, &audio_device, &buffer);
66+
pthread_join(r_thread, NULL);
7167

72-
free(storage);
73-
// MQTTClient_disconnect(client, mqtt_config.timeout_ms);
74-
// MQTTClient_destroy(&client);
75-
return 0;
68+
cleanup();
69+
return 0;
7670
}

reformat.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#/bin/bash
2+
clang-format -i $(find . -name "*.c" -o -name "*.h")

src/buffer/ring_buffer.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,37 @@
22

33
#define RING_BUFFER_SIZE 4096
44

5-
static size_t next_pos(const RingBuffer *rb, size_t pos) {
6-
return (pos + 1) % rb->size;
7-
}
5+
static size_t next_pos(const RingBuffer *rb, size_t pos) { return (pos + 1) % rb->size; }
86

9-
bool ring_buffer_is_empty(const RingBuffer *rb) {
10-
return rb->write_index == rb->read_index;
11-
}
7+
bool ring_buffer_is_empty(const RingBuffer *rb) { return rb->write_index == rb->read_index; }
128

13-
bool ring_buffer_is_full(const RingBuffer *rb) {
14-
return next_pos(rb, rb->write_index) == rb->read_index;
15-
}
9+
bool ring_buffer_is_full(const RingBuffer *rb) { return next_pos(rb, rb->write_index) == rb->read_index; }
1610

1711
void ring_buffer_init(RingBuffer *rb, float *storage, size_t size) {
18-
rb->data = storage;
19-
rb->size = size;
12+
rb->data = storage;
13+
rb->size = size;
2014

21-
rb->write_count = 0;
22-
rb->read_index = 0;
23-
rb->write_index = 0;
15+
rb->write_count = 0;
16+
rb->read_index = 0;
17+
rb->write_index = 0;
2418
}
2519

2620
bool ring_buffer_push(RingBuffer *rb, float sample) {
27-
if (ring_buffer_is_full(rb)) {
28-
return false;
29-
}
30-
rb->data[rb->write_index] = sample;
31-
rb->write_count++;
32-
rb->write_index = next_pos(rb, rb->write_index);
33-
34-
return true;
21+
if (ring_buffer_is_full(rb)) {
22+
return false;
23+
}
24+
rb->data[rb->write_index] = sample;
25+
rb->write_count++;
26+
rb->write_index = next_pos(rb, rb->write_index);
27+
28+
return true;
3529
}
3630

3731
bool ring_buffer_pop(RingBuffer *rb, float *out) {
38-
if (ring_buffer_is_empty(rb)) {
39-
return false;
40-
}
41-
*out = rb->data[rb->read_index];
42-
rb->read_index = next_pos(rb, rb->read_index);
43-
return true;
32+
if (ring_buffer_is_empty(rb)) {
33+
return false;
34+
}
35+
*out = rb->data[rb->read_index];
36+
rb->read_index = next_pos(rb, rb->read_index);
37+
return true;
4438
}

src/buffer/ring_buffer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <stddef.h>
44

55
typedef struct {
6-
float *data;
7-
size_t size;
8-
size_t read_index;
9-
size_t write_index;
10-
size_t write_count;
6+
float *data;
7+
size_t size;
8+
size_t read_index;
9+
size_t write_index;
10+
size_t write_count;
1111
} RingBuffer;
1212

1313
void ring_buffer_init(RingBuffer *rb, float *storage, size_t size);

src/buffer/synchronous_single_buffer.c

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,59 @@
44
#include <stdlib.h>
55

66
bool init_buffer(SynchronousSingleBuffer *buffer, int16_t *storage) {
7-
if (storage == NULL) {
8-
fprintf(stderr, "Buffer storage is NULL\n");
9-
return false;
10-
}
11-
buffer->write_count = 0;
12-
buffer->reading_at = 0;
13-
buffer->writing_at = 0;
14-
buffer->skipped_samples_count = 0;
15-
buffer->storage = storage;
16-
buffer->producer_online = true;
17-
18-
return true;
7+
if (storage == NULL) {
8+
fprintf(stderr, "Buffer storage is NULL\n");
9+
return false;
10+
}
11+
buffer->write_count = 0;
12+
buffer->reading_at = 0;
13+
buffer->writing_at = 0;
14+
buffer->skipped_samples_count = 0;
15+
buffer->storage = storage;
16+
buffer->producer_online = true;
17+
18+
return true;
1919
};
2020

21-
bool buffer_empty(const SynchronousSingleBuffer *buffer) {
22-
return buffer->write_count == 0;
23-
}
21+
bool buffer_empty(const SynchronousSingleBuffer *buffer) { return buffer->write_count == 0; }
2422

2523
bool rewind_buffer(SynchronousSingleBuffer *buffer) {
26-
buffer->reading_at = 0;
27-
buffer->writing_at = 0;
24+
buffer->reading_at = 0;
25+
buffer->writing_at = 0;
2826

29-
return true;
27+
return true;
3028
}
3129

3230
bool buffer_full(const SynchronousSingleBuffer *buffer) {
33-
return buffer->writing_at == SYNCHRONOUS_SINGULAR_BUFFER_SIZE;
31+
return buffer->writing_at == SYNCHRONOUS_SINGULAR_BUFFER_SIZE;
3432
};
3533

3634
bool buffer_blocked(SynchronousSingleBuffer *buffer) {
37-
return buffer_full(buffer) && buffer->reading_at != buffer->writing_at;
35+
return buffer_full(buffer) && buffer->reading_at != buffer->writing_at;
3836
};
3937

4038
bool buffer_overwrite(SynchronousSingleBuffer *buffer) {
41-
return buffer->write_count > 0 &&
42-
!(buffer->write_count % SYNCHRONOUS_SINGULAR_BUFFER_SIZE);
39+
return buffer->write_count > 0 && !(buffer->write_count % SYNCHRONOUS_SINGULAR_BUFFER_SIZE);
4340
};
4441

4542
bool write_to_buffer(SynchronousSingleBuffer *buffer, const int16_t value) {
46-
if (buffer_full(buffer) && buffer_blocked(buffer)) {
47-
buffer->skipped_samples_count++;
48-
return false;
49-
}
50-
if (buffer_full(buffer) && !buffer_blocked(buffer)) {
51-
rewind_buffer(buffer);
52-
}
53-
buffer->storage[buffer->writing_at] = value;
54-
buffer->write_count++;
55-
buffer->writing_at++;
56-
57-
return true;
43+
if (buffer_full(buffer) && buffer_blocked(buffer)) {
44+
buffer->skipped_samples_count++;
45+
return false;
46+
}
47+
if (buffer_full(buffer) && !buffer_blocked(buffer)) {
48+
rewind_buffer(buffer);
49+
}
50+
buffer->storage[buffer->writing_at] = value;
51+
buffer->write_count++;
52+
buffer->writing_at++;
53+
54+
return true;
5855
};
5956

6057
int16_t *read_from_buffer(SynchronousSingleBuffer *buffer) {
61-
int16_t *value = &buffer->storage[buffer->reading_at];
62-
buffer->reading_at++;
58+
int16_t *value = &buffer->storage[buffer->reading_at];
59+
buffer->reading_at++;
6360

64-
return value;
61+
return value;
6562
};

src/buffer/synchronous_single_buffer.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
#define SYNCHRONOUS_SINGULAR_BUFFER_SIZE 128000
66

77
typedef struct {
8-
uint64_t write_count;
9-
uint32_t writing_at;
10-
uint32_t reading_at;
11-
int16_t *storage;
12-
bool producer_online;
13-
uint32_t skipped_samples_count;
8+
uint64_t write_count;
9+
uint32_t writing_at;
10+
uint32_t reading_at;
11+
int16_t *storage;
12+
bool producer_online;
13+
uint32_t skipped_samples_count;
1414
} SynchronousSingleBuffer;
1515

16+
void destroy_buffer(SynchronousSingleBuffer *buffer);
17+
1618
bool init_buffer(SynchronousSingleBuffer *buffer, int16_t *storage);
1719
bool buffer_blocked(SynchronousSingleBuffer *buffer);
18-
void destroy_buffer(SynchronousSingleBuffer *buffer);
1920
bool buffer_empty(const SynchronousSingleBuffer *buffer);
2021
bool buffer_full(const SynchronousSingleBuffer *buffer);
2122
bool write_to_buffer(SynchronousSingleBuffer *buffer, int16_t value);
22-
int16_t *read_from_buffer(SynchronousSingleBuffer *buffer);
2323
bool buffer_overwrite(SynchronousSingleBuffer *buffer);
2424
bool rewind_buffer(SynchronousSingleBuffer *buffer);
25+
26+
int16_t *read_from_buffer(SynchronousSingleBuffer *buffer);

0 commit comments

Comments
 (0)