-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommon.c
More file actions
86 lines (70 loc) · 1.53 KB
/
Copy pathcommon.c
File metadata and controls
86 lines (70 loc) · 1.53 KB
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
#define XOPEN_SOURCE 600 // strtoul
#include "err.h"
#include "common.h"
#include <stdlib.h>
#include <errno.h>
int
convert_channel_num(
char *s,
unsigned int *channel_num,
err_t err
) {
unsigned int ui;
if (!convert_string_to_unsigned_int(s, &ui)) {
err_set2(err, "invalid channel number \"%s\"", s);
return 0;
}
if (ui > 15) {
err_set2(err, "invalid channel number \"%s\", should be 0..15", s);
}
*channel_num = ui;
return 1;
}
int
convert_sample_num(
char *s,
unsigned int *sample_num,
err_t err
) {
unsigned int ui;
if (!convert_string_to_unsigned_int(s, &ui)) {
err_set2(err, "invalid sample number \"%s\"", s);
return 0;
}
if (ui > 16383) {
err_set2(err, "invalid sample number \"%s\", should be 0..16383", s);
}
*sample_num = ui;
return 1;
}
int
convert_string_to_unsigned_int(
char *s,
unsigned int *ui
) {
char *endptr;
unsigned int c;
errno = 0;
c = strtoul(s, &endptr, 0);
if (
errno != 0 ||
*s == '\0' ||
*endptr != '\0'
) {
return 0;
}
*ui = c;
return 1;
}
const char *
response_to_string(response_type response) {
switch (response) {
case RESPONSE_ACK: return "ACK";
case RESPONSE_NAK: return "NAK";
case RESPONSE_CANCEL: return "CANCEL";
case RESPONSE_WAIT: return "WAIT";
case RESPONSE_TIMEOUT: return "TIMEOUT";
case RESPONSE_NULL: return "NULL";
}
return "UNKNOWN";
}