PX4 Firmware
PX4 Autopilot Software http://px4.io
reflect.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014 Andrew Tridgell. 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 reflect.c
36  *
37  * simple data reflector for load testing terminals (especially USB)
38  *
39  * @author Andrew Tridgell
40  */
41 
42 #include <px4_platform_common/px4_config.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <stdbool.h>
48 #include <assert.h>
49 #include <systemlib/err.h>
50 
51 __EXPORT int reflect_main(int argc, char *argv[]);
52 
53 // memory corruption checking
54 #define MAX_BLOCKS 1000
55 static uint32_t nblocks;
56 struct block {
57  uint32_t v[256];
58 };
59 static struct block *blocks[MAX_BLOCKS];
60 
61 #define VALUE(i) ((i*7) ^ 0xDEADBEEF)
62 
63 static void allocate_blocks(void)
64 {
65  while (nblocks < MAX_BLOCKS) {
66  blocks[nblocks] = calloc(1, sizeof(struct block));
67 
68  if (blocks[nblocks] == NULL) {
69  break;
70  }
71 
72  for (uint32_t i = 0; i < sizeof(blocks[nblocks]->v) / sizeof(uint32_t); i++) {
73  blocks[nblocks]->v[i] = VALUE(i);
74  }
75 
76  nblocks++;
77  }
78 
79  printf("Allocated %u blocks\n", nblocks);
80 }
81 
82 static void check_blocks(void)
83 {
84  for (uint32_t n = 0; n < nblocks; n++) {
85  for (uint32_t i = 0; i < sizeof(blocks[nblocks]->v) / sizeof(uint32_t); i++) {
86  assert(blocks[n]->v[i] == VALUE(i));
87  }
88  }
89 }
90 
91 int
92 reflect_main(int argc, char *argv[])
93 {
94  uint32_t total = 0;
95  printf("Starting reflector\n");
96 
98 
99  while (true) {
100  char buf[128];
101  ssize_t n = read(0, buf, sizeof(buf));
102 
103  if (n < 0) {
104  break;
105  }
106 
107  if (n > 0) {
108  write(1, buf, n);
109  }
110 
111  total += n;
112 
113  if (total > 1024000) {
114  check_blocks();
115  total = 0;
116  }
117  }
118 
119  return OK;
120 }
uint32_t v[256]
Definition: reflect.c:57
static void allocate_blocks(void)
Definition: reflect.c:63
static uint32_t nblocks
Definition: reflect.c:55
Definition: I2C.hpp:51
#define MAX_BLOCKS
Definition: reflect.c:54
static void read(bootloader_app_shared_t *pshared)
static struct block * blocks[MAX_BLOCKS]
Definition: reflect.c:59
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Definition: reflect.c:56
__EXPORT int reflect_main(int argc, char *argv[])
Definition: reflect.c:92
static void write(bootloader_app_shared_t *pshared)
static void check_blocks(void)
Definition: reflect.c:82
#define OK
Definition: uavcan_main.cpp:71