aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJoshua Clayton <stillcompiling@gmail.com>2015-11-18 17:30:38 -0500
committerMark Brown <broonie@kernel.org>2015-11-23 09:54:01 -0500
commit5c437a401b824399b6fa7342c66b675ebb7af43e (patch)
tree7fa7f5ae400f9aee7c238ae139af4968b007c1e0 /tools
parent5eca4d843f9f0c3140a8657ba2f8217ee6c08c11 (diff)
spi: spidev_test: transfer_escaped_string function
Move the input_tx code into its own small function. This cleans up some variables from main() that are used only here. While we are at it, check malloc calls instead of assuming they succeed. Signed-off-by: Joshua Clayton <stillcompiling@gmail.com> Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/spi/spidev_test.c36
1 files changed, 23 insertions, 13 deletions
diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index 135b3f592b83..f9d2957e538b 100644
--- a/tools/spi/spidev_test.c
+++ b/tools/spi/spidev_test.c
@@ -249,13 +249,30 @@ static void parse_opts(int argc, char *argv[])
249 } 249 }
250} 250}
251 251
252static void transfer_escaped_string(int fd, char *str)
253{
254 size_t size = strlen(str + 1);
255 uint8_t *tx;
256 uint8_t *rx;
257
258 tx = malloc(size);
259 if (!tx)
260 pabort("can't allocate tx buffer");
261
262 rx = malloc(size);
263 if (!rx)
264 pabort("can't allocate rx buffer");
265
266 size = unescape((char *)tx, str, size);
267 transfer(fd, tx, rx, size);
268 free(rx);
269 free(tx);
270}
271
252int main(int argc, char *argv[]) 272int main(int argc, char *argv[])
253{ 273{
254 int ret = 0; 274 int ret = 0;
255 int fd; 275 int fd;
256 uint8_t *tx;
257 uint8_t *rx;
258 int size;
259 276
260 parse_opts(argc, argv); 277 parse_opts(argc, argv);
261 278
@@ -300,17 +317,10 @@ int main(int argc, char *argv[])
300 printf("bits per word: %d\n", bits); 317 printf("bits per word: %d\n", bits);
301 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); 318 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
302 319
303 if (input_tx) { 320 if (input_tx)
304 size = strlen(input_tx+1); 321 transfer_escaped_string(fd, input_tx);
305 tx = malloc(size); 322 else
306 rx = malloc(size);
307 size = unescape((char *)tx, input_tx, size);
308 transfer(fd, tx, rx, size);
309 free(rx);
310 free(tx);
311 } else {
312 transfer(fd, default_tx, default_rx, sizeof(default_tx)); 323 transfer(fd, default_tx, default_rx, sizeof(default_tx));
313 }
314 324
315 close(fd); 325 close(fd);
316 326