فهرست منبع

[new] add rt-thread example

wuhanstudio 4 سال پیش
والد
کامیت
c964f2887f

+ 29 - 0
SConscript

@@ -0,0 +1,29 @@
+Import('RTT_ROOT')
+Import('rtconfig')
+from building import *
+
+cwd = GetCurrentDir()
+
+src = Glob('src/*.cpp')
+
+if GetDepend('EMBEDDEDPROTO_USING_ENCODE_DECODE_EXAMPLE'):
+    src += Glob('examples/encode_decode.cpp')
+    src += Glob('examples/UartReadBuffer.cpp')
+    src += Glob('examples/UartWriteBuffer.cpp')
+
+CPPPATH = [cwd, ]
+CPPPATH += [cwd + '/src']
+
+LOCAL_CPPFLAGS = ''
+if rtconfig.CROSS_TOOL == 'gcc':
+    LOCAL_CPPFLAGS += ' -std=c++14'
+
+group = DefineGroup('EmbeddedProto', src, depend = ['PKG_USING_EMBEDDEDPROTO'], CPPPATH = CPPPATH, LOCAL_CCFLAGS=LOCAL_CPPFLAGS )
+
+Return('group')
+
+
+
+
+
+

+ 107 - 0
examples/UartReadBuffer.cpp

@@ -0,0 +1,107 @@
+/*
+ *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
+ *
+ *  This file is part of Embedded Proto.
+ *
+ *  Embedded Proto is open source software: you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License as published
+ *  by the Free Software Foundation, version 3 of the license.
+ *
+ *  Embedded Proto  is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
+ *
+ *  For commercial and closed source application please visit:
+ *  <https://EmbeddedProto.com/license/>.
+ *
+ *  Embedded AMS B.V.
+ *  Info:
+ *    info at EmbeddedProto dot com
+ *
+ *  Postal adress:
+ *    Johan Huizingalaan 763a
+ *    1066 VH, Amsterdam
+ *    the Netherlands
+ */
+
+#include "UartReadBuffer.h"
+
+UartReadBuffer::UartReadBuffer()
+  : data_{0},
+    write_index_(0),
+    read_index_(0)
+{
+
+}
+
+uint32_t UartReadBuffer::get_size() const
+{
+  return write_index_;
+}
+
+uint32_t UartReadBuffer::get_max_size() const
+{
+  return MAX_SIZE;
+}
+
+bool UartReadBuffer::peek(uint8_t& byte) const
+{
+  bool return_value = write_index_ > read_index_;
+  if(return_value)
+  {
+    byte = data_[read_index_];
+  }
+  return return_value;
+}
+
+void UartReadBuffer::advance()
+{
+  ++read_index_;
+}
+
+void UartReadBuffer::advance(const uint32_t N)
+{
+  read_index_ += N;
+}
+
+bool UartReadBuffer::pop(uint8_t& byte)
+{
+  bool return_value = write_index_ > read_index_;
+  if(return_value) {
+    byte = data_[read_index_];
+    ++read_index_;
+  }
+  return return_value;
+}
+
+uint8_t* UartReadBuffer::get_data_array()
+{
+  return data_;
+}
+
+uint32_t& UartReadBuffer::get_bytes_written()
+{
+  return write_index_;
+}
+
+void UartReadBuffer::clear()
+{
+  read_index_ = 0;
+  write_index_ = 0;
+}
+
+bool UartReadBuffer::push(uint8_t& byte)
+{
+  bool return_value = MAX_SIZE > write_index_;
+  if(return_value)
+  {
+    data_[write_index_] = byte;
+    ++write_index_;
+  }
+  return return_value;
+}
+

+ 89 - 0
examples/UartReadBuffer.h

@@ -0,0 +1,89 @@
+/*
+ *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
+ *
+ *  This file is part of Embedded Proto.
+ *
+ *  Embedded Proto is open source software: you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License as published
+ *  by the Free Software Foundation, version 3 of the license.
+ *
+ *  Embedded Proto  is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
+ *
+ *  For commercial and closed source application please visit:
+ *  <https://EmbeddedProto.com/license/>.
+ *
+ *  Embedded AMS B.V.
+ *  Info:
+ *    info at EmbeddedProto dot com
+ *
+ *  Postal adress:
+ *    Johan Huizingalaan 763a
+ *    1066 VH, Amsterdam
+ *    the Netherlands
+ */
+
+#ifndef INC_UARTREADBUFFER_H_
+#define INC_UARTREADBUFFER_H_
+
+#include <cstdint>
+#include <ReadBufferInterface.h>
+
+class UartReadBuffer : public ::EmbeddedProto::ReadBufferInterface
+{
+    //! Store a maximum of MAX_SIZE bytes in the buffer
+    static constexpr uint32_t MAX_SIZE = 50;
+
+  public:
+    UartReadBuffer();
+    ~UartReadBuffer() override = default;
+
+    /** \see ::EmbeddedProto::ReadBufferInterface::get_size() */
+    uint32_t get_size() const override;
+
+    /** \see ::EmbeddedProto::ReadBufferInterface::get_max_size() */
+    uint32_t get_max_size() const override;
+
+    /** \see ::EmbeddedProto::ReadBufferInterface::peak() */
+    bool peek(uint8_t& byte) const override;
+
+    /** \see ::EmbeddedProto::ReadBufferInterface::advance() */
+    void advance() override;
+
+    /** \see ::EmbeddedProto::ReadBufferInterface::advance(const uint32_t N) */
+    void advance(const uint32_t N) override;
+
+    /** \see ::EmbeddedProto::ReadBufferInterface::pop() */
+    bool pop(uint8_t& byte) override;
+
+    //! Return a pointer to the data array
+    uint8_t* get_data_array();
+
+    //! Return a non constant reference to the number of bytes written to the data array.
+    uint32_t& get_bytes_written();
+
+    //! Clear all indices, in effect allowing the data to be overwritten.
+    void clear();
+
+    //! Push new data into the buffer.
+    bool push(uint8_t& byte);
+
+  private:
+
+    //! The array in which the data received over uart is stored.
+    uint8_t data_[MAX_SIZE];
+
+    //! The number of bytes currently received and stored in the data array.
+    uint32_t write_index_;
+
+    //! The number of bytes read from the data array.
+    uint32_t read_index_;
+};
+
+
+#endif /* INC_UARTREADBUFFER_H_ */

+ 80 - 0
examples/UartWriteBuffer.cpp

@@ -0,0 +1,80 @@
+/*
+ *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
+ *
+ *  This file is part of Embedded Proto.
+ *
+ *  Embedded Proto is open source software: you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License as published
+ *  by the Free Software Foundation, version 3 of the license.
+ *
+ *  Embedded Proto  is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
+ *
+ *  For commercial and closed source application please visit:
+ *  <https://EmbeddedProto.com/license/>.
+ *
+ *  Embedded AMS B.V.
+ *  Info:
+ *    info at EmbeddedProto dot com
+ *
+ *  Postal adress:
+ *    Johan Huizingalaan 763a
+ *    1066 VH, Amsterdam
+ *    the Netherlands
+ */
+
+#include "UartWriteBuffer.h"
+
+#include <string.h>
+
+void UartWriteBuffer::clear()
+{
+  write_index_ = 0;
+}
+
+uint32_t UartWriteBuffer::get_size() const
+{
+  return write_index_;
+}
+
+uint32_t UartWriteBuffer::get_max_size() const
+{
+  return MAX_SIZE;
+}
+
+uint32_t UartWriteBuffer::get_available_size() const
+{
+  return MAX_SIZE - write_index_;
+}
+
+bool UartWriteBuffer::push(const uint8_t byte)
+{
+  bool return_value = MAX_SIZE > write_index_;
+  if(return_value)
+  {
+    data_[write_index_] = byte;
+    ++write_index_;
+  }
+  return return_value;
+}
+
+bool UartWriteBuffer::push(const uint8_t* bytes, const uint32_t length)
+{
+  bool return_value = MAX_SIZE > (write_index_ + length);
+  if(return_value)
+  {
+    memcpy(data_ + write_index_, bytes, length);
+    write_index_ += length;
+  }
+  return return_value;
+}
+
+uint8_t* UartWriteBuffer::get_data()
+{
+  return data_;
+}

+ 78 - 0
examples/UartWriteBuffer.h

@@ -0,0 +1,78 @@
+/*
+ *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
+ *
+ *  This file is part of Embedded Proto.
+ *
+ *  Embedded Proto is open source software: you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License as published
+ *  by the Free Software Foundation, version 3 of the license.
+ *
+ *  Embedded Proto  is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
+ *
+ *  For commercial and closed source application please visit:
+ *  <https://EmbeddedProto.com/license/>.
+ *
+ *  Embedded AMS B.V.
+ *  Info:
+ *    info at EmbeddedProto dot com
+ *
+ *  Postal adress:
+ *    Johan Huizingalaan 763a
+ *    1066 VH, Amsterdam
+ *    the Netherlands
+ */
+
+#ifndef INC_UARTWRITEBUFFER_H_
+#define INC_UARTWRITEBUFFER_H_
+
+#include <cstdint>
+#include <WriteBufferInterface.h>
+
+class UartWriteBuffer : public ::EmbeddedProto::WriteBufferInterface
+{
+    //! Store a maximum of MAX_SIZE bytes in the buffer
+    static constexpr uint32_t MAX_SIZE = 50;
+
+  public:
+    UartWriteBuffer() = default;
+    ~UartWriteBuffer() override = default;
+
+    //! \see ::EmbeddedProto::WriteBufferInterface::clear()
+    virtual void clear() override;
+
+    //! \see ::EmbeddedProto::WriteBufferInterface::get_size()
+    virtual uint32_t get_size() const override;
+
+    //! \see ::EmbeddedProto::WriteBufferInterface::get_max_size()
+    virtual uint32_t get_max_size() const override;
+
+    //! \see ::EmbeddedProto::WriteBufferInterface::get_available_size()
+    virtual uint32_t get_available_size() const override;
+
+    //! \see ::EmbeddedProto::WriteBufferInterface::push()
+    virtual bool push(const uint8_t byte) override;
+
+    //! \see ::EmbeddedProto::WriteBufferInterface::push()
+    virtual bool push(const uint8_t* bytes, const uint32_t length) override;
+
+    //! Return a pointer to the data array.
+    uint8_t* get_data();
+
+  private:
+
+    //! The array in which the serialized data is stored.
+    uint8_t data_[MAX_SIZE];
+
+    //! The number of bytes currently serialized in the array.
+    uint32_t write_index_;
+
+};
+
+
+#endif /* INC_UARTWRITEBUFFER_H_ */

+ 180 - 0
examples/amessage.h

@@ -0,0 +1,180 @@
+/*
+ *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
+ *
+ *  This file is part of Embedded Proto.
+ *
+ *  Embedded Proto is open source software: you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License as published
+ *  by the Free Software Foundation, version 3 of the license.
+ *
+ *  Embedded Proto  is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
+ *
+ *  For commercial and closed source application please visit:
+ *  <https://EmbeddedProto.com/license/>.
+ *
+ *  Embedded AMS B.V.
+ *  Info:
+ *    info at EmbeddedProto dot com
+ *
+ *  Postal address:
+ *    Johan Huizingalaan 763a
+ *    1066 VH, Amsterdam
+ *    the Netherlands
+ */
+
+// This file is generated. Please do not edit!
+#ifndef _AMESSAGE_H_
+#define _AMESSAGE_H_
+
+#include <cstdint>
+#include <MessageInterface.h>
+#include <WireFormatter.h>
+#include <Fields.h>
+#include <MessageSizeCalculator.h>
+#include <ReadBufferSection.h>
+#include <RepeatedFieldFixedSize.h>
+#include <FieldStringBytes.h>
+#include <Errors.h>
+
+// Include external proto definitions
+
+
+class AMessage final: public ::EmbeddedProto::MessageInterface
+{
+  public:
+    AMessage() :
+        a_(0),
+        b_(0)
+    {
+
+    };
+    ~AMessage() override = default;
+
+    enum class id
+    {
+      NOT_SET = 0,
+      A = 1,
+      B = 2
+    };
+
+    AMessage& operator=(const AMessage& rhs)
+    {
+      set_a(rhs.get_a());
+      set_b(rhs.get_b());
+      return *this;
+    }
+
+    inline void clear_a() { a_.clear(); }
+    inline void set_a(const EmbeddedProto::int32& value) { a_ = value; }
+    inline void set_a(const EmbeddedProto::int32&& value) { a_ = value; }
+    inline EmbeddedProto::int32& mutable_a() { return a_; }
+    inline const EmbeddedProto::int32& get_a() const { return a_; }
+    inline EmbeddedProto::int32::FIELD_TYPE a() const { return a_.get(); }
+
+    inline void clear_b() { b_.clear(); }
+    inline void set_b(const EmbeddedProto::int32& value) { b_ = value; }
+    inline void set_b(const EmbeddedProto::int32&& value) { b_ = value; }
+    inline EmbeddedProto::int32& mutable_b() { return b_; }
+    inline const EmbeddedProto::int32& get_b() const { return b_; }
+    inline EmbeddedProto::int32::FIELD_TYPE b() const { return b_.get(); }
+
+
+    ::EmbeddedProto::Error serialize(::EmbeddedProto::WriteBufferInterface& buffer) const final
+    {
+      ::EmbeddedProto::Error return_value = ::EmbeddedProto::Error::NO_ERRORS;
+
+      if((0 != a_.get()) && (::EmbeddedProto::Error::NO_ERRORS == return_value))
+      {
+        return_value = a_.serialize_with_id(static_cast<uint32_t>(id::A), buffer);
+      }
+
+      if((0 != b_.get()) && (::EmbeddedProto::Error::NO_ERRORS == return_value))
+      {
+        return_value = b_.serialize_with_id(static_cast<uint32_t>(id::B), buffer);
+      }
+
+      return return_value;
+    };
+
+    ::EmbeddedProto::Error deserialize(::EmbeddedProto::ReadBufferInterface& buffer) final
+    {
+      ::EmbeddedProto::Error return_value = ::EmbeddedProto::Error::NO_ERRORS;
+      ::EmbeddedProto::WireFormatter::WireType wire_type;
+      uint32_t id_number = 0;
+
+      ::EmbeddedProto::Error tag_value = ::EmbeddedProto::WireFormatter::DeserializeTag(buffer, wire_type, id_number);
+      while((::EmbeddedProto::Error::NO_ERRORS == return_value) && (::EmbeddedProto::Error::NO_ERRORS == tag_value))
+      {
+        switch(id_number)
+        {
+          case static_cast<uint32_t>(id::A):
+          {
+            if(::EmbeddedProto::WireFormatter::WireType::VARINT == wire_type)
+            {
+              return_value = a_.deserialize(buffer);
+            }
+            else
+            {
+              // Wire type does not match field.
+              return_value = ::EmbeddedProto::Error::INVALID_WIRETYPE;
+            }
+            break;
+          }
+
+          case static_cast<uint32_t>(id::B):
+          {
+            if(::EmbeddedProto::WireFormatter::WireType::VARINT == wire_type)
+            {
+              return_value = b_.deserialize(buffer);
+            }
+            else
+            {
+              // Wire type does not match field.
+              return_value = ::EmbeddedProto::Error::INVALID_WIRETYPE;
+            }
+            break;
+          }
+
+          default:
+            break;
+        }
+
+        if(::EmbeddedProto::Error::NO_ERRORS == return_value)
+        {
+            // Read the next tag.
+            tag_value = ::EmbeddedProto::WireFormatter::DeserializeTag(buffer, wire_type, id_number);
+        }
+      }
+
+      // When an error was detect while reading the tag but no other errors where found, set it in the return value.
+      if((::EmbeddedProto::Error::NO_ERRORS == return_value)
+         && (::EmbeddedProto::Error::NO_ERRORS != tag_value)
+         && (::EmbeddedProto::Error::END_OF_BUFFER != tag_value)) // The end of the buffer is not an array in this case.
+      {
+        return_value = tag_value;
+      }
+
+      return return_value;
+    };
+
+    void clear() final
+    {
+      clear_a();
+      clear_b();
+
+    }
+
+    private:
+
+      EmbeddedProto::int32 a_;
+      EmbeddedProto::int32 b_;
+
+};
+
+#endif // _AMESSAGE_H_

+ 6 - 0
examples/amessage.proto

@@ -0,0 +1,6 @@
+syntax = "proto3";
+
+message AMessage {
+  int32 a=1; 
+  int32 b=2;
+}

+ 46 - 0
examples/encode_decode.cpp

@@ -0,0 +1,46 @@
+#include <rtthread.h>
+
+#include "amessage.h"
+#include "UartReadBuffer.h"
+#include "UartWriteBuffer.h"
+
+AMessage encode_msg;
+AMessage decode_msg;
+
+UartReadBuffer  read_buffer;
+UartWriteBuffer write_buffer;
+
+int encode_decode(int argc, char const *argv[])
+{
+	encode_msg.set_a(20);
+	encode_msg.set_b(10);
+
+	rt_kprintf("---- Encoding ---\n");
+	rt_kprintf("a=%ld, b=%ld\n", encode_msg.a(), encode_msg.b());
+
+	auto serialization_status = encode_msg.serialize(write_buffer);
+	if(::EmbeddedProto::Error::NO_ERRORS == serialization_status)
+	{
+		// The number of bytes in the message.
+		uint8_t n_bytes = write_buffer.get_size();
+		// The actual encoded data
+		uint8_t* buffer = write_buffer.get_data();
+
+		rt_kprintf("Encoding %d serialized bytes\n", n_bytes);
+
+		for(uint8_t i = 0; (i < n_bytes); ++i)
+		{
+			read_buffer.push(buffer[i]);
+		}
+
+		rt_kprintf("---- Decoding ---\n");
+		auto deserialize_status = decode_msg.deserialize(read_buffer);
+		if(::EmbeddedProto::Error::NO_ERRORS == deserialize_status) 
+		{
+			rt_kprintf("a=%ld, b=%ld\n", decode_msg.a(), decode_msg.b());
+		}
+	}
+
+	return 0;
+}
+MSH_CMD_EXPORT(encode_decode, Embedded Proto encode decode example)

+ 1 - 1
src/FieldStringBytes.h

@@ -37,7 +37,7 @@
 #include <cstdint>
 #include <string.h>
 #include <type_traits>
-
+#include <algorithm>
 
 namespace EmbeddedProto
 {