/
ttops.h
122 lines (108 loc) · 3.61 KB
/
ttops.h
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef __TTOPS_H__
#define __TTOPS_H__
#include "version.h"
#include "bbatt.h"
struct tt_handles { uint16_t ppcp, passcode, magic, cmd_status, length, transfer, check; };
struct tt_files { uint32_t hostname, preference, manifest, activity_start, gps_status, quickgps; };
struct ble_dev_info {
uint16_t handle;
const char *name;
char buf[BT_ATT_DEFAULT_LE_MTU-2];
int len;
};
typedef struct ttdev {
int fd;
int protocol_version;
struct tt_handles *h;
struct ble_dev_info *info; // stuff from UUID=180a (Device Information)
struct version_tuple oldest_tested_firmware, newest_tested_firmware;
const char **tested_models;
struct tt_files *files;
} TTDEV;
#include "util.h"
TTDEV *tt_device_init(int protocol_version, int fd);
bool tt_device_done(TTDEV *d);
struct ble_dev_info *tt_check_device_version(TTDEV *d, bool warning);
int tt_authorize(TTDEV *d, uint32_t code);
int tt_read_file(TTDEV *d, uint32_t fileno, int debug, uint8_t **buf);
int tt_write_file(TTDEV *d, uint32_t fileno, int debug, const uint8_t *buf, uint32_t length, uint32_t write_delay);
int tt_delete_file(TTDEV *d, uint32_t fileno);
int tt_list_sub_files(TTDEV *d, uint32_t fileno, uint16_t **outlist);
int tt_reboot(TTDEV *d);
static inline int
EXPECT_BYTES(TTDEV *d, uint8_t *buf)
{
uint16_t handle;
int length = att_read_not(d->fd, &handle, buf);
if (length < 0)
return length;
else if (handle != d->h->transfer) {
fprintf(stderr, "Expected 0x%04x <- BYTES, but got:\n 0x%04x <- ", d->h->transfer, handle);
hexlify(stderr, buf, length, true);
return -1;
}
return (int)length;
}
static inline int
EXPECT_LENGTH(TTDEV *d)
{
union { uint8_t buf[BT_ATT_DEFAULT_LE_MTU]; uint32_t out; } r;
uint16_t handle;
int length = att_read_not(d->fd, &handle, r.buf);
if (length < 0)
return length;
else if ((handle != d->h->length) || (length != 4)) {
fprintf(stderr, "Expected 0x%04x <- (uint32_t)LENGTH, but got:\n 0x%04x <- ", d->h->length, handle);
hexlify(stderr, r.buf, length, true);
return -1;
}
return btohl(r.out);
}
static inline int
EXPECT_ANY_uint32(TTDEV *d, uint16_t handle, uint32_t *val)
{
union { uint8_t buf[BT_ATT_DEFAULT_LE_MTU]; uint32_t out; } r;
uint16_t h;
int length = att_read_not(d->fd, &h, r.buf);
if (length < 0)
return length;
else if ((h != handle) || (length != 4)) {
fprintf(stderr, "Expected 0x%04x <- (uint32_t), but got:\n 0x%04x <- ", handle, h);
hexlify(stderr, r.buf, length, true);
return -1;
}
if (val)
*val = btohl(r.out);
return 0;
}
static inline int
EXPECT_uint32(TTDEV *d, uint16_t handle, uint32_t val)
{
union { uint8_t buf[BT_ATT_DEFAULT_LE_MTU]; uint32_t out; } r;
uint16_t h;
int length = att_read_not(d->fd, &h, r.buf);
if (length < 0)
return length;
else if ((h != handle) || (length != 4) || (btohl(r.out)!=val)) {
fprintf(stderr, "Expected 0x%04x <- (uint32_t)0x%08x, but got:\n 0x%04x <- ", handle, val, h);
hexlify(stderr, r.buf, length, true);
return -1;
}
return 0;
}
static inline int
EXPECT_uint8(TTDEV *d, uint16_t handle, uint8_t val)
{
uint8_t buf[BT_ATT_DEFAULT_LE_MTU];
uint16_t h;
int length = att_read_not(d->fd, &h, buf);
if (length < 0)
return length;
else if ((h != handle) || (length != 1) || (*buf!=val)) {
fprintf(stderr, "Expected 0x%04x <- (uint8_t)0x%02x, but got:\n 0x%02x <- ", handle, val, h);
hexlify(stderr, buf, length, true);
return -1;
}
return 0;
}
#endif /* #ifndef __TTOPS_H__ */