blob: 4f26d04c3165d8fbbcaf3b4e86be6d4d34a2d155 [file] [log] [blame]
Brian Silverman43216322015-09-13 02:23:09 -04001require_relative 'load.rb'
Brian Silverman1885bd02014-02-13 12:28:12 -08002
Brian Silverman074392a2014-03-24 16:32:44 -07003# TODO(brians): Special-case Time too and float/double if we can find a good way to do it.
Austin Schuhf907f2e2018-01-02 01:15:27 -08004GenericTypeNames = ['float', 'double', 'char']
5TimeTypeNames = ['::aos::monotonic_clock::time_point']
Brian Silverman074392a2014-03-24 16:32:44 -07006IntegerSizes = [8, 16, 32, 64]
Brian Silverman1885bd02014-02-13 12:28:12 -08007
Brian Silvermancbbc3c42014-02-22 18:33:17 -08008WriteIffChanged.open(ARGV[0]) do |output|
Brian Silverman1885bd02014-02-13 12:28:12 -08009 output.puts <<END
10// This file is generated by #{File.expand_path(__FILE__)}.
11// DO NOT EDIT BY HAND!
12
13#include <sys/types.h>
14#include <stdint.h>
15#include <inttypes.h>
16#include <stdio.h>
17
John Park33858a32018-09-28 23:05:48 -070018#include "aos/byteorder.h"
19#include "aos/time/time.h"
20#include "aos/print_field_helpers.h"
21#include "aos/logging/printf_formats.h"
Brian Silverman1885bd02014-02-13 12:28:12 -080022
23namespace aos {
24
Brian Silvermand4b1ed52014-02-15 15:26:04 -080025bool PrintField(char *output, size_t *output_bytes, const void *input,
Brian Silverman1885bd02014-02-13 12:28:12 -080026 size_t *input_bytes, uint32_t type) {
27 switch (type) {
Brian Silverman074392a2014-03-24 16:32:44 -070028#{GenericTypeNames.collect do |name|
Brian Silverman1885bd02014-02-13 12:28:12 -080029 message_element = Target::MessageElement.new(name, 'value')
30 statement = MessageElementStmt.new(name, 'value')
31 message_element.size = statement.size
32 print_args = []
33 message_element.fetchPrintArgs(print_args)
34 next <<END2
35 case #{message_element.getTypeID()}:
36 {
37 if (*input_bytes < #{statement.size}) return false;
38 *input_bytes -= #{statement.size};
39 #{name} value;
40 to_host(static_cast<const char *>(input), &value);
41 int ret = snprintf(output, *output_bytes,
42 "#{statement.toPrintFormat()}",
43 #{print_args[0]});
44 if (ret < 0) return false;
45 if (static_cast<unsigned int>(ret) >= *output_bytes) return false;
Brian Silverman074392a2014-03-24 16:32:44 -070046 *output_bytes -= ret;
Brian Silverman1885bd02014-02-13 12:28:12 -080047 return true;
48 }
49END2
50end.join('')}
Austin Schuhf907f2e2018-01-02 01:15:27 -080051#{TimeTypeNames.collect do |name|
52 message_element = Target::MessageElement.new(name, 'value')
53 statement = MessageElementStmt.new(name, 'value')
54 message_element.size = statement.size
55 print_args = []
56 message_element.fetchPrintArgs(print_args)
57 next <<END2
58 case #{message_element.getTypeID()}:
59 {
60 if (*input_bytes < #{statement.size}) return false;
61 *input_bytes -= #{statement.size};
62 int32_t upper;
63 uint32_t lower;
64 to_host(static_cast<const char *>(input), &upper);
65 to_host(static_cast<const char *>(input) + 4, &lower);
66 #{name} value(::std::chrono::seconds(upper) + ::std::chrono::nanoseconds(lower));
67 int ret = snprintf(output, *output_bytes,
68 "#{statement.toPrintFormat()}",
69 #{print_args[0]});
70 if (ret < 0) return false;
71 if (static_cast<unsigned int>(ret) >= *output_bytes) return false;
72 *output_bytes -= ret;
73 return true;
74 }
75END2
76end.join('')}
Brian Silverman074392a2014-03-24 16:32:44 -070077#{IntegerSizes.collect do |size|
78 [true, false].collect do |signed|
79 size_bytes = size / 8
80 name = "#{signed ? '' : 'u'}int#{size}_t"
81 message_element = Target::MessageElement.new(name, 'value')
82 message_element.size = size_bytes
83 next <<END2
84 case #{message_element.getTypeID()}:
85 {
86 if (*input_bytes < #{size_bytes}) return false;
87 *input_bytes -= #{size_bytes};
88 #{name} value;
89 to_host(static_cast<const char *>(input), &value);
90 return PrintInteger<#{name}>(output, value, output_bytes);
91 }
92END2
93 end
94end.flatten.join('')}
95#{
96 message_element = Target::MessageElement.new('bool', 'value')
97 message_element.size = 1
98 r = <<END2
99 case #{message_element.getTypeID()}:
100 {
101 if (*input_bytes < 1) return false;
102 if (*output_bytes < 1) return false;
103 *input_bytes -= 1;
104 bool value = static_cast<const char *>(input)[0];
Brian Silverman89f557c2014-03-29 23:43:49 -0700105 *output_bytes -= 1;
Brian Silverman074392a2014-03-24 16:32:44 -0700106 *output = value ? 'T' : 'f';
107 return true;
108 }
109END2
110}
Brian Silverman1885bd02014-02-13 12:28:12 -0800111 default:
112 return false;
113 }
114}
115
116} // namespace aos
117END
118end