blob: 5eb37127fd8b09cd6a9e3156353b27b2e156d9e5 [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.
4GenericTypeNames = ['float', 'double', 'char', '::aos::time::Time']
5IntegerSizes = [8, 16, 32, 64]
Brian Silverman1885bd02014-02-13 12:28:12 -08006
Brian Silvermancbbc3c42014-02-22 18:33:17 -08007WriteIffChanged.open(ARGV[0]) do |output|
Brian Silverman1885bd02014-02-13 12:28:12 -08008 output.puts <<END
9// This file is generated by #{File.expand_path(__FILE__)}.
10// DO NOT EDIT BY HAND!
11
12#include <sys/types.h>
13#include <stdint.h>
14#include <inttypes.h>
15#include <stdio.h>
16
17#include "aos/common/byteorder.h"
Brian Silverman96e6d5a2014-03-24 15:55:40 -070018#include "aos/common/time.h"
Brian Silverman074392a2014-03-24 16:32:44 -070019#include "aos/common/print_field_helpers.h"
Brian Silvermancb5da1f2015-12-05 22:19:58 -050020#include "aos/common/logging/printf_formats.h"
Brian Silverman1885bd02014-02-13 12:28:12 -080021
22namespace aos {
23
Brian Silvermand4b1ed52014-02-15 15:26:04 -080024bool PrintField(char *output, size_t *output_bytes, const void *input,
Brian Silverman1885bd02014-02-13 12:28:12 -080025 size_t *input_bytes, uint32_t type) {
26 switch (type) {
Brian Silverman074392a2014-03-24 16:32:44 -070027#{GenericTypeNames.collect do |name|
Brian Silverman1885bd02014-02-13 12:28:12 -080028 message_element = Target::MessageElement.new(name, 'value')
29 statement = MessageElementStmt.new(name, 'value')
30 message_element.size = statement.size
31 print_args = []
32 message_element.fetchPrintArgs(print_args)
33 next <<END2
34 case #{message_element.getTypeID()}:
35 {
36 if (*input_bytes < #{statement.size}) return false;
37 *input_bytes -= #{statement.size};
38 #{name} value;
39 to_host(static_cast<const char *>(input), &value);
40 int ret = snprintf(output, *output_bytes,
41 "#{statement.toPrintFormat()}",
42 #{print_args[0]});
43 if (ret < 0) return false;
44 if (static_cast<unsigned int>(ret) >= *output_bytes) return false;
Brian Silverman074392a2014-03-24 16:32:44 -070045 *output_bytes -= ret;
Brian Silverman1885bd02014-02-13 12:28:12 -080046 return true;
47 }
48END2
49end.join('')}
Brian Silverman074392a2014-03-24 16:32:44 -070050#{IntegerSizes.collect do |size|
51 [true, false].collect do |signed|
52 size_bytes = size / 8
53 name = "#{signed ? '' : 'u'}int#{size}_t"
54 message_element = Target::MessageElement.new(name, 'value')
55 message_element.size = size_bytes
56 next <<END2
57 case #{message_element.getTypeID()}:
58 {
59 if (*input_bytes < #{size_bytes}) return false;
60 *input_bytes -= #{size_bytes};
61 #{name} value;
62 to_host(static_cast<const char *>(input), &value);
63 return PrintInteger<#{name}>(output, value, output_bytes);
64 }
65END2
66 end
67end.flatten.join('')}
68#{
69 message_element = Target::MessageElement.new('bool', 'value')
70 message_element.size = 1
71 r = <<END2
72 case #{message_element.getTypeID()}:
73 {
74 if (*input_bytes < 1) return false;
75 if (*output_bytes < 1) return false;
76 *input_bytes -= 1;
77 bool value = static_cast<const char *>(input)[0];
Brian Silverman89f557c2014-03-29 23:43:49 -070078 *output_bytes -= 1;
Brian Silverman074392a2014-03-24 16:32:44 -070079 *output = value ? 'T' : 'f';
80 return true;
81 }
82END2
83}
Brian Silverman1885bd02014-02-13 12:28:12 -080084 default:
85 return false;
86 }
87}
88
89} // namespace aos
90END
91end