forked from ggerganov/ggwave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-ggwave.c
78 lines (57 loc) · 2.2 KB
/
test-ggwave.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
#include "ggwave/ggwave.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHECK(cond) \
if (!(cond)) { \
fprintf(stderr, "[%s:%d] Check failed: %s\n", __FILE__, __LINE__, #cond); \
exit(1); \
}
#define CHECK_T(cond) CHECK(cond)
#define CHECK_F(cond) CHECK(!(cond))
int main() {
//ggwave_setLogFile(NULL); // disable logging
ggwave_setLogFile(stdout);
ggwave_Parameters parameters = ggwave_getDefaultParameters();
parameters.sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I16;
parameters.sampleFormatOut = GGWAVE_SAMPLE_FORMAT_I16;
ggwave_Instance instance = ggwave_init(parameters);
int ret;
const char * payload = "test";
char decoded[16];
int n = ggwave_encode(instance, payload, 4, GGWAVE_PROTOCOL_AUDIBLE_FASTEST, 50, NULL, 1);
char *waveform = malloc(n);
CHECK(waveform != NULL);
int ne = ggwave_encode(instance, payload, 4, GGWAVE_PROTOCOL_AUDIBLE_FASTEST, 50, waveform, 0);
CHECK(ne > 0);
// not enough output buffer size to store the decoded message
ret = ggwave_ndecode(instance, waveform, ne, decoded, 3);
CHECK(ret == -2); // fail
// just enough size to store it
ret = ggwave_ndecode(instance, waveform, ne, decoded, 4);
CHECK(ret == 4); // success
// unsafe method - will write the decoded output to the output buffer regardless of the size
ret = ggwave_decode(instance, waveform, ne, decoded);
CHECK(ret == 4);
// disable Rx protocol
{
ggwave_rxToggleProtocol(GGWAVE_PROTOCOL_AUDIBLE_FASTEST, 0);
ggwave_Instance instanceTmp = ggwave_init(parameters);
ret = ggwave_ndecode(instanceTmp, waveform, ne, decoded, 4);
CHECK(ret == -1); // fail
ggwave_free(instanceTmp);
}
// enable Rx protocol
{
ggwave_rxToggleProtocol(GGWAVE_PROTOCOL_AUDIBLE_FASTEST, 1);
ggwave_Instance instanceTmp = ggwave_init(parameters);
ret = ggwave_ndecode(instanceTmp, waveform, ne, decoded, 4);
CHECK(ret == 4); // success
ggwave_free(instanceTmp);
}
decoded[ret] = 0; // null-terminate the received data
CHECK(strcmp(decoded, payload) == 0);
ggwave_free(instance);
free(waveform);
return 0;
}