Pebble App Framework
v7.1.1
How It Works
CLI
Device API
Phone API
C Coverage
TS Coverage
GitHub
Theme
Pebble App Framework: Device API
Toggle main menu visibility
Loading...
Searching...
No Matches
wire_read.h
Go to the documentation of this file.
1
17
#pragma once
18
#include <stdbool.h>
19
#include <stdint.h>
20
21
#include "
io/bytes_le.h
"
22
27
38
static
inline
int32_t
wire_int_or
(
bool
is_signed, uint16_t length,
const
uint8_t *value, int32_t fallback)
39
{
40
if
(!value)
41
{
42
return
fallback;
43
}
44
45
switch
(length)
46
{
47
// a byte read signed is -128 to 127 and unsigned is 0 to 255
48
// that is the only thing the signedness decides at this width
49
case
1:
return
is_signed ? (int32_t)(int8_t)value[0] : (int32_t)value[0];
50
51
// read the pair as signed and take the sign back off when it is not wanted
52
// so the unsigned half lands on 0 to 65535 rather than going negative at the top
53
case
2:
return
is_signed ? (int32_t)
read_i16_le
(value)
54
: (int32_t)(uint16_t)
read_i16_le
(value);
55
56
// four bytes fill the whole width either way so the bits are the same
57
// and only the caller's reading of the top one differs
58
case
4:
return
read_i32_le
(value);
59
60
default
:
return
fallback;
61
}
62
}
63
75
static
inline
bool
wire_cstring_terminated
(
const
char
*s, uint16_t length)
76
{
77
return
s && length > 0 && s[length - 1] ==
'\0'
;
78
}
79
95
static
inline
int32_t
wire_digits_or
(
const
char
*text, int32_t fallback)
96
{
97
if
(!text || text[0] ==
'\0'
)
98
{
99
return
fallback;
100
}
101
102
int32_t value = 0;
103
for
(
const
char
*cursor = text; *cursor !=
'\0'
; cursor++)
104
{
105
if
(*cursor <
'0'
|| *cursor >
'9'
)
106
{
107
return
fallback;
108
}
109
110
int32_t digit = *cursor -
'0'
;
111
112
// held at the top before the next digit goes on, since a sum past INT32_MAX is undefined
113
// in C. the walk carries on so a letter further along still turns the text away
114
if
(value > (INT32_MAX - digit) / 10)
115
{
116
value = INT32_MAX;
117
continue
;
118
}
119
120
value = value * 10 + digit;
121
}
122
123
return
value;
124
}
125
bytes_le.h
Little-endian byte readers shared by the wire-decoding stores.
read_i16_le
static int16_t read_i16_le(const uint8_t *p)
Reads a little-endian signed 16-bit value out of a byte pair.
Definition
bytes_le.h:26
wire_cstring_terminated
static bool wire_cstring_terminated(const char *s, uint16_t length)
Whether a string of length bytes carries its terminator inside itself.
Definition
wire_read.h:75
read_i32_le
static int32_t read_i32_le(const uint8_t *p)
Reads a little-endian signed 32-bit value out of four bytes.
Definition
bytes_le.h:37
wire_int_or
static int32_t wire_int_or(bool is_signed, uint16_t length, const uint8_t *value, int32_t fallback)
Reads a little-endian integer of exactly length bytes.
Definition
wire_read.h:38
wire_digits_or
static int32_t wire_digits_or(const char *text, int32_t fallback)
Reads text holding only digits as the whole number it spells, such as a select's "3".
Definition
wire_read.h:95
src
c
core
wire
wire_read.h
Generated by
1.18.0