PX4 Firmware
PX4 Autopilot Software http://px4.io
dumpfile.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file dumpfile.c
36  *
37  * @author Anton Babushkin <anton.babushkin@me.com>
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <px4_platform_common/log.h>
42 #include <px4_platform_common/module.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <fcntl.h>
48 #include <termios.h>
49 
50 #include <systemlib/err.h>
51 
52 __EXPORT int dumpfile_main(int argc, char *argv[]);
53 
54 static void print_usage(void)
55 {
56  PRINT_MODULE_DESCRIPTION("Dump file utility. Prints file size and contents in binary mode (don't replace LF with CR LF) to stdout.");
57 
58  PRINT_MODULE_USAGE_NAME_SIMPLE("dumpfile", "command");
59  PRINT_MODULE_USAGE_ARG("<file>", "File to dump", false);
60 
61 }
62 
63 int
64 dumpfile_main(int argc, char *argv[])
65 {
66  if (argc < 2) {
67  print_usage();
68  return 1;
69  }
70 
71  /* open input file */
72  FILE *f;
73  f = fopen(argv[1], "r");
74 
75  if (f == NULL) {
76  PX4_ERR("Failed to open file (%i)", errno);
77  return 1;
78  }
79 
80  /* get file size */
81  fseek(f, 0L, SEEK_END);
82  int size = ftell(f);
83  fseek(f, 0L, SEEK_SET);
84 
85  printf("File size: %d bytes\n", size);
86 
87  /* configure stdout */
88  int out = fileno(stdout);
89 
90  struct termios tc;
91  struct termios tc_old;
92  tcgetattr(out, &tc);
93 
94  /* save old terminal attributes to restore it later on exit */
95  memcpy(&tc_old, &tc, sizeof(tc));
96 
97  /* don't add CR on each LF*/
98  tc.c_oflag &= ~ONLCR;
99 
100  if (tcsetattr(out, TCSANOW, &tc) < 0) {
101  PX4_ERR("failed setting stdout attributes");
102  fclose(f);
103  return 1;
104  }
105 
106  char buf[512];
107  int nread;
108 
109  /* dump file */
110  while ((nread = fread(buf, 1, sizeof(buf), f)) > 0) {
111  if (write(out, buf, nread) <= 0) {
112  PX4_ERR("write failed");
113  break;
114  }
115  }
116 
117  fsync(out);
118  fclose(f);
119 
120  /* restore old terminal attributes */
121  if (tcsetattr(out, TCSANOW, &tc_old) < 0) {
122  PX4_ERR("failed to restore stdout attributes");
123  return 1;
124  }
125 
126  return 0;
127 }
Definition: I2C.hpp:51
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
__EXPORT int dumpfile_main(int argc, char *argv[])
Definition: dumpfile.c:64
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
static void print_usage(void)
Definition: dumpfile.c:54
static void write(bootloader_app_shared_t *pshared)