17
17
18
18
#include "sbp.h"
19
19
#include "sbp_fileio.h"
20
+ #include "sbp_utils.h"
20
21
#include "cfs/cfs.h"
21
22
22
23
static void read_cb (u16 sender_id , u8 len , u8 msg [], void * context );
@@ -31,13 +32,13 @@ void sbp_fileio_setup(void)
31
32
{
32
33
static sbp_msg_callbacks_node_t read_node ;
33
34
sbp_register_cbk (
34
- SBP_MSG_FILEIO_READ ,
35
+ SBP_MSG_FILEIO_READ_REQUEST ,
35
36
& read_cb ,
36
37
& read_node
37
38
);
38
39
static sbp_msg_callbacks_node_t read_dir_node ;
39
40
sbp_register_cbk (
40
- SBP_MSG_FILEIO_READ_DIR ,
41
+ SBP_MSG_FILEIO_READ_DIR_REQUEST ,
41
42
& read_dir_cb ,
42
43
& read_dir_node
43
44
);
@@ -49,57 +50,65 @@ void sbp_fileio_setup(void)
49
50
);
50
51
static sbp_msg_callbacks_node_t write_node ;
51
52
sbp_register_cbk (
52
- SBP_MSG_FILEIO_WRITE ,
53
+ SBP_MSG_FILEIO_WRITE_REQUEST ,
53
54
& write_cb ,
54
55
& write_node
55
56
);
56
57
}
57
58
58
59
/** File read callback.
59
- * Responds to a SBP_MSG_FILEIO_READ message.
60
+ * Responds to a SBP_MSG_FILEIO_READ_REQUEST message.
60
61
*
61
62
* 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.
64
65
*/
65
66
static void read_cb (u16 sender_id , u8 len , u8 msg [], void * context )
66
67
{
67
- (void )sender_id ;
68
68
(void )context ;
69
69
70
+ if (sender_id != SBP_SENDER_ID ) {
71
+ puts ("Invalid sender!" );
72
+ return ;
73
+ }
74
+
70
75
if ((len < 9 ) || (msg [len - 1 ] != '\0' )) {
71
76
puts ("Invalid fileio read message!" );
72
77
return ;
73
78
}
74
79
75
80
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 ) ;
77
82
u8 buf [256 ];
78
83
memcpy (buf , msg , len );
79
84
int f = cfs_open ((char * )& msg [5 ], CFS_READ );
80
85
cfs_seek (f , offset , CFS_SEEK_SET );
81
86
len += cfs_read (f , buf + len , readlen );
82
87
cfs_close (f );
83
88
84
- sbp_send_msg (SBP_MSG_FILEIO_READ , len , buf );
89
+ sbp_send_msg (SBP_MSG_FILEIO_READ_RESPONSE , len , buf );
85
90
}
86
91
87
92
/** Directory listing callback.
88
- * Responds to a SBP_MSG_FILEIO_READ_DIR message.
93
+ * Responds to a SBP_MSG_FILEIO_READ_DIR_REQUEST message.
89
94
*
90
95
* The offset parameter can be used to skip the first n elements of the file
91
96
* list.
92
97
*
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
94
99
* listings as a NULL delimited list. The listing is chunked over multiple SBP
95
100
* packets and the end of the list is identified by an entry containing just
96
101
* the character 0xFF.
97
102
*/
98
103
static void read_dir_cb (u16 sender_id , u8 len , u8 msg [], void * context )
99
104
{
100
- (void )sender_id ;
101
105
(void )context ;
102
106
107
+ if (sender_id != SBP_SENDER_ID ) {
108
+ puts ("Invalid sender!" );
109
+ return ;
110
+ }
111
+
103
112
if ((len < 5 ) || (msg [len - 1 ] != '\0' )) {
104
113
puts ("Invalid fileio read dir message!" );
105
114
return ;
@@ -114,27 +123,31 @@ static void read_dir_cb(u16 sender_id, u8 len, u8 msg[], void* context)
114
123
while (offset && (cfs_readdir (& dir , & dirent ) == 0 ))
115
124
offset -- ;
116
125
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 );
119
128
len += strlen (dirent .name ) + 1 ;
120
129
}
121
130
122
- if (len < 255 )
131
+ if (len < SBP_FRAMING_MAX_PAYLOAD_SIZE )
123
132
buf [len ++ ] = 0xff ;
124
133
125
134
cfs_closedir (& dir );
126
135
127
- sbp_send_msg (SBP_MSG_FILEIO_READ_DIR , len , buf );
136
+ sbp_send_msg (SBP_MSG_FILEIO_READ_DIR_RESPONSE , len , buf );
128
137
}
129
138
130
139
/* Remove file callback.
131
140
* Responds to a SBP_MSG_FILEIO_REMOVE message.
132
141
*/
133
142
static void remove_cb (u16 sender_id , u8 len , u8 msg [], void * context )
134
143
{
135
- (void )sender_id ;
136
144
(void )context ;
137
145
146
+ if (sender_id != SBP_SENDER_ID ) {
147
+ puts ("Invalid sender!" );
148
+ return ;
149
+ }
150
+
138
151
if ((len < 2 ) || (msg [len - 1 ] != '\0' )) {
139
152
puts ("Invalid fileio remove message!" );
140
153
return ;
@@ -144,16 +157,21 @@ static void remove_cb(u16 sender_id, u8 len, u8 msg[], void* context)
144
157
}
145
158
146
159
/* Write to file callback.
147
- * Responds to a SBP_MSG_FILEIO_WRITE message.
160
+ * Responds to a SBP_MSG_FILEIO_WRITE_REQUEST message.
148
161
*
149
162
* 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.
151
165
*/
152
166
static void write_cb (u16 sender_id , u8 len , u8 msg [], void * context )
153
167
{
154
- (void )sender_id ;
155
168
(void )context ;
156
169
170
+ if (sender_id != SBP_SENDER_ID ) {
171
+ puts ("Invalid sender!" );
172
+ return ;
173
+ }
174
+
157
175
if (len < 6 ) {
158
176
puts ("Invalid fileio write message!" );
159
177
return ;
@@ -166,5 +184,5 @@ static void write_cb(u16 sender_id, u8 len, u8 msg[], void* context)
166
184
cfs_write (f , msg + headerlen , len - headerlen );
167
185
cfs_close (f );
168
186
169
- sbp_send_msg (SBP_MSG_FILEIO_WRITE , headerlen , msg );
187
+ sbp_send_msg (SBP_MSG_FILEIO_WRITE_RESPONSE , headerlen , msg );
170
188
}
0 commit comments