Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit 0002c0b

Browse files
committed
Merge pull request #452 from mfine/mfine-file-io
Update to new file_io messages
2 parents 42d0084 + 45186c1 commit 0002c0b

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/sbp_fileio.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "sbp.h"
1919
#include "sbp_fileio.h"
20+
#include "sbp_utils.h"
2021
#include "cfs/cfs.h"
2122

2223
static void read_cb(u16 sender_id, u8 len, u8 msg[], void* context);
@@ -31,13 +32,13 @@ void sbp_fileio_setup(void)
3132
{
3233
static sbp_msg_callbacks_node_t read_node;
3334
sbp_register_cbk(
34-
SBP_MSG_FILEIO_READ,
35+
SBP_MSG_FILEIO_READ_REQUEST,
3536
&read_cb,
3637
&read_node
3738
);
3839
static sbp_msg_callbacks_node_t read_dir_node;
3940
sbp_register_cbk(
40-
SBP_MSG_FILEIO_READ_DIR,
41+
SBP_MSG_FILEIO_READ_DIR_REQUEST,
4142
&read_dir_cb,
4243
&read_dir_node
4344
);
@@ -49,57 +50,65 @@ void sbp_fileio_setup(void)
4950
);
5051
static sbp_msg_callbacks_node_t write_node;
5152
sbp_register_cbk(
52-
SBP_MSG_FILEIO_WRITE,
53+
SBP_MSG_FILEIO_WRITE_REQUEST,
5354
&write_cb,
5455
&write_node
5556
);
5657
}
5758

5859
/** File read callback.
59-
* Responds to a SBP_MSG_FILEIO_READ message.
60+
* Responds to a SBP_MSG_FILEIO_READ_REQUEST message.
6061
*
6162
* Reads a certain length (up to 255 bytes) from a given offset. Returns the
62-
* data in a SBP_MSG_FILEIO_READ message where the message length field indicates
63-
* how many bytes were succesfully read.
63+
* data in a SBP_MSG_FILEIO_READ_RESPONSE message where the message length field
64+
* indicates how many bytes were succesfully read.
6465
*/
6566
static void read_cb(u16 sender_id, u8 len, u8 msg[], void* context)
6667
{
67-
(void)sender_id;
6868
(void)context;
6969

70+
if (sender_id != SBP_SENDER_ID) {
71+
puts("Invalid sender!");
72+
return;
73+
}
74+
7075
if ((len < 9) || (msg[len-1] != '\0')) {
7176
puts("Invalid fileio read message!");
7277
return;
7378
}
7479

7580
u32 offset = ((u32)msg[3] << 24) | ((u32)msg[2] << 16) | (msg[1] << 8) | msg[0];
76-
u8 readlen = msg[4];
81+
u8 readlen = MIN(msg[4], SBP_FRAMING_MAX_PAYLOAD_SIZE - len);
7782
u8 buf[256];
7883
memcpy(buf, msg, len);
7984
int f = cfs_open((char*)&msg[5], CFS_READ);
8085
cfs_seek(f, offset, CFS_SEEK_SET);
8186
len += cfs_read(f, buf + len, readlen);
8287
cfs_close(f);
8388

84-
sbp_send_msg(SBP_MSG_FILEIO_READ, len, buf);
89+
sbp_send_msg(SBP_MSG_FILEIO_READ_RESPONSE, len, buf);
8590
}
8691

8792
/** Directory listing callback.
88-
* Responds to a SBP_MSG_FILEIO_READ_DIR message.
93+
* Responds to a SBP_MSG_FILEIO_READ_DIR_REQUEST message.
8994
*
9095
* The offset parameter can be used to skip the first n elements of the file
9196
* list.
9297
*
93-
* Returns a SBP_MSG_FILEIO_READ_DIR message containing the directory
98+
* Returns a SBP_MSG_FILEIO_READ_DIR_RESPONSE message containing the directory
9499
* listings as a NULL delimited list. The listing is chunked over multiple SBP
95100
* packets and the end of the list is identified by an entry containing just
96101
* the character 0xFF.
97102
*/
98103
static void read_dir_cb(u16 sender_id, u8 len, u8 msg[], void* context)
99104
{
100-
(void)sender_id;
101105
(void)context;
102106

107+
if (sender_id != SBP_SENDER_ID) {
108+
puts("Invalid sender!");
109+
return;
110+
}
111+
103112
if ((len < 5) || (msg[len-1] != '\0')) {
104113
puts("Invalid fileio read dir message!");
105114
return;
@@ -114,27 +123,31 @@ static void read_dir_cb(u16 sender_id, u8 len, u8 msg[], void* context)
114123
while (offset && (cfs_readdir(&dir, &dirent) == 0))
115124
offset--;
116125

117-
while ((cfs_readdir(&dir, &dirent) == 0) && (len < 255)) {
118-
strncpy((char*)buf + len, dirent.name, 255 - len);
126+
while ((cfs_readdir(&dir, &dirent) == 0) && (len < SBP_FRAMING_MAX_PAYLOAD_SIZE)) {
127+
strncpy((char*)buf + len, dirent.name, SBP_FRAMING_MAX_PAYLOAD_SIZE - len);
119128
len += strlen(dirent.name) + 1;
120129
}
121130

122-
if (len < 255)
131+
if (len < SBP_FRAMING_MAX_PAYLOAD_SIZE)
123132
buf[len++] = 0xff;
124133

125134
cfs_closedir(&dir);
126135

127-
sbp_send_msg(SBP_MSG_FILEIO_READ_DIR, len, buf);
136+
sbp_send_msg(SBP_MSG_FILEIO_READ_DIR_RESPONSE, len, buf);
128137
}
129138

130139
/* Remove file callback.
131140
* Responds to a SBP_MSG_FILEIO_REMOVE message.
132141
*/
133142
static void remove_cb(u16 sender_id, u8 len, u8 msg[], void* context)
134143
{
135-
(void)sender_id;
136144
(void)context;
137145

146+
if (sender_id != SBP_SENDER_ID) {
147+
puts("Invalid sender!");
148+
return;
149+
}
150+
138151
if ((len < 2) || (msg[len-1] != '\0')) {
139152
puts("Invalid fileio remove message!");
140153
return;
@@ -144,16 +157,21 @@ static void remove_cb(u16 sender_id, u8 len, u8 msg[], void* context)
144157
}
145158

146159
/* Write to file callback.
147-
* Responds to a SBP_MSG_FILEIO_WRITE message.
160+
* Responds to a SBP_MSG_FILEIO_WRITE_REQUEST message.
148161
*
149162
* Writes a certain length (up to 255 bytes) at a given offset. Returns a copy
150-
* of the original SBP_MSG_FILEIO_WRITE message to check integrity of the write.
163+
* of the original SBP_MSG_FILEIO_WRITE_RESPONSE message to check integrity of
164+
* the write.
151165
*/
152166
static void write_cb(u16 sender_id, u8 len, u8 msg[], void* context)
153167
{
154-
(void)sender_id;
155168
(void)context;
156169

170+
if (sender_id != SBP_SENDER_ID) {
171+
puts("Invalid sender!");
172+
return;
173+
}
174+
157175
if (len < 6) {
158176
puts("Invalid fileio write message!");
159177
return;
@@ -166,5 +184,5 @@ static void write_cb(u16 sender_id, u8 len, u8 msg[], void* context)
166184
cfs_write(f, msg + headerlen, len - headerlen);
167185
cfs_close(f);
168186

169-
sbp_send_msg(SBP_MSG_FILEIO_WRITE, headerlen, msg);
187+
sbp_send_msg(SBP_MSG_FILEIO_WRITE_RESPONSE, headerlen, msg);
170188
}

0 commit comments

Comments
 (0)