-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathFSService.cpp
More file actions
380 lines (363 loc) · 14 KB
/
Copy pathFSService.cpp
File metadata and controls
380 lines (363 loc) · 14 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <nrf_log.h>
#include "FSService.h"
#include "components/ble/BleController.h"
#include "components/ble/NotificationManager.h"
#include "components/settings/Settings.h"
#include "systemtask/SystemTask.h"
using namespace Pinetime::Controllers;
constexpr ble_uuid16_t FSService::fsServiceUuid;
constexpr ble_uuid128_t FSService::fsVersionUuid;
constexpr ble_uuid128_t FSService::fsTransferUuid;
int FSServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
auto* fsService = static_cast<FSService*>(arg);
return fsService->OnFSServiceRequested(conn_handle, attr_handle, ctxt);
}
FSService::FSService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::FS& fs)
: systemTask {systemTask},
fs {fs},
characteristicDefinition {{.uuid = &fsVersionUuid.u,
.access_cb = FSServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
.val_handle = &versionCharacteristicHandle},
{
.uuid = &fsTransferUuid.u,
.access_cb = FSServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
.val_handle = &transferCharacteristicHandle,
},
{0}},
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &fsServiceUuid.u,
.characteristics = characteristicDefinition},
{0},
} {
}
void FSService::Init() {
int res = 0;
res = ble_gatts_count_cfg(serviceDefinition);
ASSERT(res == 0);
res = ble_gatts_add_svcs(serviceDefinition);
ASSERT(res == 0);
}
int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
#ifndef PINETIME_IS_RECOVERY
if (systemTask.GetSettings().GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::Disabled) {
Pinetime::Controllers::NotificationManager::Notification notif;
memcpy(notif.message.data(), denyAlert, denyAlertLength);
notif.size = denyAlertLength;
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
systemTask.GetNotificationManager().Push(std::move(notif));
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);
return BLE_ATT_ERR_INSUFFICIENT_AUTHOR;
}
#endif
if (attributeHandle == versionCharacteristicHandle) {
NRF_LOG_INFO("FS_S : handle = %d", versionCharacteristicHandle);
int res = os_mbuf_append(context->om, &fsVersion, sizeof(fsVersion));
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
if (attributeHandle == transferCharacteristicHandle) {
return FSCommandHandler(connectionHandle, context->om);
}
return 0;
}
namespace {
// The path follows its header in the same packet. Both the length field and
// the buffer it is copied into have to be respected, so check against the
// bytes that actually arrived and against the destination size.
bool CopyPath(const char* pathstr, uint16_t pathlen, size_t pathOffset, size_t packetLen, char* out, size_t outSize) {
if (static_cast<size_t>(pathlen) + 1 > outSize) {
return false;
}
if (pathOffset + static_cast<size_t>(pathlen) > packetLen) {
return false;
}
memcpy(out, pathstr, pathlen);
out[pathlen] = '\0';
return true;
}
}
int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
// Every case below reads its header straight out of the received buffer.
// om_len is how much is really there, so header and path lengths are
// measured against it before they are used.
const size_t packetLen = om->om_len;
if (packetLen < 1) {
return 0;
}
auto command = static_cast<commands>(om->om_data[0]);
NRF_LOG_INFO("[FS_S] -> FSCommandHandler Command %d", command);
// Just always make sure we are awake...
systemTask.PushMessage(Pinetime::System::Messages::StartFileTransfer);
vTaskDelay(10);
while (systemTask.IsSleeping()) {
vTaskDelay(100); // 50ms
}
lfs_dir_t dir = {0};
lfs_info info = {0};
lfs_file f = {0};
switch (command) {
case commands::READ: {
NRF_LOG_INFO("[FS_S] -> Read");
if (packetLen < sizeof(ReadHeader)) {
return 0;
}
auto* header = (ReadHeader*) om->om_data;
if (!CopyPath(header->pathstr, header->pathlen, sizeof(ReadHeader), packetLen, filepath, sizeof(filepath))) {
return 0;
}
ReadResponse resp;
os_mbuf* om;
resp.command = commands::READ_DATA;
resp.status = 0x01;
resp.chunkoff = header->chunkoff;
int res = fs.Stat(filepath, &info);
if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) {
resp.status = (int8_t) res;
resp.chunklen = 0;
resp.totallen = 0;
om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse));
} else {
resp.chunklen = std::min<uint32_t>(std::min<uint32_t>(header->chunksize, info.size), maxChunkLen);
resp.totallen = info.size;
fs.FileOpen(&f, filepath, LFS_O_RDONLY);
fs.FileSeek(&f, header->chunkoff);
uint8_t fileData[maxChunkLen] = {0};
resp.chunklen = fs.FileRead(&f, fileData, resp.chunklen);
om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse));
os_mbuf_append(om, fileData, resp.chunklen);
fs.FileClose(&f);
}
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::READ_PACING: {
NRF_LOG_INFO("[FS_S] -> Readpacing");
if (packetLen < sizeof(ReadHeader)) {
return 0;
}
auto* header = (ReadHeader*) om->om_data;
ReadResponse resp;
resp.command = commands::READ_DATA;
resp.status = 0x01;
resp.chunkoff = header->chunkoff;
int res = fs.Stat(filepath, &info);
if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) {
resp.status = (int8_t) res;
resp.chunklen = 0;
resp.totallen = 0;
} else {
resp.chunklen = std::min<uint32_t>(std::min<uint32_t>(header->chunksize, info.size), maxChunkLen);
resp.totallen = info.size;
fs.FileOpen(&f, filepath, LFS_O_RDONLY);
fs.FileSeek(&f, header->chunkoff);
}
os_mbuf* om;
if (resp.chunklen > 0) {
uint8_t fileData[maxChunkLen] = {0};
resp.chunklen = fs.FileRead(&f, fileData, resp.chunklen);
om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse));
os_mbuf_append(om, fileData, resp.chunklen);
} else {
resp.chunklen = 0;
om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse));
}
fs.FileClose(&f);
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::WRITE: {
NRF_LOG_INFO("[FS_S] -> Write");
if (packetLen < sizeof(WriteHeader)) {
return 0;
}
auto* header = (WriteHeader*) om->om_data;
if (!CopyPath(header->pathstr, header->pathlen, sizeof(WriteHeader), packetLen, filepath, sizeof(filepath))) {
return 0; // TODO make this actually return a BLE notif
}
fileSize = header->totalSize;
WriteResponse resp;
resp.command = commands::WRITE_PACING;
resp.offset = header->offset;
resp.modTime = 0;
int res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT);
if (res == 0) {
fs.FileClose(&f);
resp.status = (res == 0) ? 0x01 : (int8_t) res;
}
resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset);
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(WriteResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::WRITE_DATA: {
NRF_LOG_INFO("[FS_S] -> WriteData");
if (packetLen < sizeof(WritePacing)) {
return 0;
}
auto* header = (WritePacing*) om->om_data;
// dataSize is announced by the peer. Only what arrived may be written,
// otherwise the write runs off the end of the packet and into the file.
const uint32_t dataSize = std::min<uint32_t>(header->dataSize, packetLen - sizeof(WritePacing));
WriteResponse resp;
resp.command = commands::WRITE_PACING;
resp.offset = header->offset;
int res = 0;
if (!(res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT))) {
if ((res = fs.FileSeek(&f, header->offset)) >= 0) {
res = fs.FileWrite(&f, header->data, dataSize);
}
fs.FileClose(&f);
}
if (res < 0) {
resp.status = (int8_t) res;
}
resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset);
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(WriteResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::DELETE: {
NRF_LOG_INFO("[FS_S] -> Delete");
if (packetLen < sizeof(DelHeader)) {
return 0;
}
auto* header = (DelHeader*) om->om_data;
char path[maxpathlen];
if (!CopyPath(header->pathstr, header->pathlen, sizeof(DelHeader), packetLen, path, sizeof(path))) {
return 0;
}
DelResponse resp {};
resp.command = commands::DELETE_STATUS;
int res = fs.FileDelete(path);
resp.status = (res == 0) ? 0x01 : (int8_t) res;
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(DelResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::MKDIR: {
NRF_LOG_INFO("[FS_S] -> MKDir");
if (packetLen < sizeof(MKDirHeader)) {
return 0;
}
auto* header = (MKDirHeader*) om->om_data;
char path[maxpathlen];
if (!CopyPath(header->pathstr, header->pathlen, sizeof(MKDirHeader), packetLen, path, sizeof(path))) {
return 0;
}
MKDirResponse resp {};
resp.command = commands::MKDIR_STATUS;
resp.modification_time = 0;
int res = fs.DirCreate(path);
resp.status = (res == 0) ? 0x01 : (int8_t) res;
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MKDirResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::LISTDIR: {
NRF_LOG_INFO("[FS_S] -> ListDir");
if (packetLen < sizeof(ListDirHeader)) {
return 0;
}
ListDirHeader* header = (ListDirHeader*) om->om_data;
char path[maxpathlen];
if (!CopyPath(header->pathstr, header->pathlen, sizeof(ListDirHeader), packetLen, path, sizeof(path))) {
return 0;
}
ListDirResponse resp {};
resp.command = commands::LISTDIR_ENTRY;
resp.status = 0x01;
resp.totalentries = 0;
resp.entry = 0;
resp.modification_time = 0;
int res = fs.DirOpen(path, &dir);
if (res != 0) {
resp.status = (int8_t) res;
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
};
while (fs.DirRead(&dir, &info)) {
resp.totalentries++;
}
fs.DirRewind(&dir);
while (true) {
res = fs.DirRead(&dir, &info);
if (res <= 0) {
break;
}
switch (info.type) {
case LFS_TYPE_REG: {
resp.flags = 0;
resp.file_size = info.size;
break;
}
case LFS_TYPE_DIR: {
resp.flags = 1;
resp.file_size = 0;
break;
}
}
// strcpy(resp.path, info.name);
resp.path_length = strlen(info.name);
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse));
os_mbuf_append(om, info.name, resp.path_length);
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
/*
* Todo Figure out how to know when the previous Notify was TX'd
* For now just delay 100ms to make sure that the data went out...
*/
vTaskDelay(100); // Allow stuff to actually go out over the BLE conn
resp.entry++;
}
assert(fs.DirClose(&dir) == 0);
resp.file_size = 0;
resp.path_length = 0;
resp.flags = 0;
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
case commands::MOVE: {
NRF_LOG_INFO("[FS_S] -> Move");
if (packetLen < sizeof(MoveHeader)) {
return 0;
}
MoveHeader* header = (MoveHeader*) om->om_data;
// Both paths sit behind the header, separated by a terminator. The old
// one used to be terminated in place, which wrote into the received
// packet at an offset the peer chose.
const size_t oldPathLen = header->OldPathLength;
char oldPath[maxpathlen];
char newPath[maxpathlen];
if (!CopyPath(header->pathstr, header->OldPathLength, sizeof(MoveHeader), packetLen, oldPath, sizeof(oldPath))) {
return 0;
}
if (!CopyPath(&header->pathstr[oldPathLen + 1],
header->NewPathLength,
sizeof(MoveHeader) + oldPathLen + 1,
packetLen,
newPath,
sizeof(newPath))) {
return 0;
}
MoveResponse resp {};
resp.command = commands::MOVE_STATUS;
int8_t res = (int8_t) fs.Rename(oldPath, newPath);
resp.status = (res == 0) ? 1 : res;
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MoveResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break;
}
default:
break;
}
NRF_LOG_INFO("[FS_S] -> done ");
systemTask.PushMessage(Pinetime::System::Messages::StopFileTransfer);
return 0;
}
// Loads resp with file data given a valid filepath header and resp