AUTOSAR SOME/IP Service Discovery Entries: C++11/STL parsing library

The entries are used to synchronize the state of services instances and the Publish/-Subscribe handling.

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of AUTOSAR SOME/IP Service Discovery Entries using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++11/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.bin", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    some_ip_sd_entries_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.entries() // => get entries

C++11/STL source code to parse AUTOSAR SOME/IP Service Discovery Entries

some_ip_sd_entries.h

#pragma once

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <memory>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * The entries are used to synchronize the state of services instances and the
 * Publish/-Subscribe handling.
 * \sa https://www.autosar.org/fileadmin/standards/foundation/19-11/AUTOSAR_PRS_SOMEIPServiceDiscoveryProtocol.pdf
 * - section 4.1.2.3  Entry Format
 */

class some_ip_sd_entries_t : public kaitai::kstruct {

public:
    class sd_entry_t;

    some_ip_sd_entries_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, some_ip_sd_entries_t* p__root = nullptr);

private:
    void _read();
    void _clean_up();

public:
    ~some_ip_sd_entries_t();

    class sd_entry_t : public kaitai::kstruct {

    public:
        class sd_entry_header_t;
        class sd_service_entry_t;
        class sd_eventgroup_entry_t;

        enum entry_types_t {
            ENTRY_TYPES_FIND = 0,
            ENTRY_TYPES_OFFER = 1,
            ENTRY_TYPES_SUBSCRIBE = 6,
            ENTRY_TYPES_SUBSCRIBE_ACK = 7
        };

        sd_entry_t(kaitai::kstream* p__io, some_ip_sd_entries_t* p__parent = nullptr, some_ip_sd_entries_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~sd_entry_t();

        class sd_entry_header_t : public kaitai::kstruct {

        public:

            sd_entry_header_t(kaitai::kstream* p__io, some_ip_sd_entries_t::sd_entry_t* p__parent = nullptr, some_ip_sd_entries_t* p__root = nullptr);

        private:
            void _read();
            void _clean_up();

        public:
            ~sd_entry_header_t();

        private:
            entry_types_t m_type;
            uint8_t m_index_first_options;
            uint8_t m_index_second_options;
            uint64_t m_number_first_options;
            uint64_t m_number_second_options;
            uint16_t m_service_id;
            uint16_t m_instance_id;
            uint8_t m_major_version;
            uint64_t m_ttl;
            some_ip_sd_entries_t* m__root;
            some_ip_sd_entries_t::sd_entry_t* m__parent;

        public:
            entry_types_t type() const { return m_type; }
            uint8_t index_first_options() const { return m_index_first_options; }
            uint8_t index_second_options() const { return m_index_second_options; }
            uint64_t number_first_options() const { return m_number_first_options; }
            uint64_t number_second_options() const { return m_number_second_options; }
            uint16_t service_id() const { return m_service_id; }
            uint16_t instance_id() const { return m_instance_id; }
            uint8_t major_version() const { return m_major_version; }
            uint64_t ttl() const { return m_ttl; }
            some_ip_sd_entries_t* _root() const { return m__root; }
            some_ip_sd_entries_t::sd_entry_t* _parent() const { return m__parent; }
        };

        class sd_service_entry_t : public kaitai::kstruct {

        public:

            sd_service_entry_t(kaitai::kstream* p__io, some_ip_sd_entries_t::sd_entry_t* p__parent = nullptr, some_ip_sd_entries_t* p__root = nullptr);

        private:
            void _read();
            void _clean_up();

        public:
            ~sd_service_entry_t();

        private:
            uint32_t m_minor_version;
            some_ip_sd_entries_t* m__root;
            some_ip_sd_entries_t::sd_entry_t* m__parent;

        public:
            uint32_t minor_version() const { return m_minor_version; }
            some_ip_sd_entries_t* _root() const { return m__root; }
            some_ip_sd_entries_t::sd_entry_t* _parent() const { return m__parent; }
        };

        class sd_eventgroup_entry_t : public kaitai::kstruct {

        public:

            sd_eventgroup_entry_t(kaitai::kstream* p__io, some_ip_sd_entries_t::sd_entry_t* p__parent = nullptr, some_ip_sd_entries_t* p__root = nullptr);

        private:
            void _read();
            void _clean_up();

        public:
            ~sd_eventgroup_entry_t();

        private:
            uint8_t m_reserved;
            bool m_initial_data_requested;
            uint64_t m_reserved2;
            uint64_t m_counter;
            uint16_t m_event_group_id;
            some_ip_sd_entries_t* m__root;
            some_ip_sd_entries_t::sd_entry_t* m__parent;

        public:
            uint8_t reserved() const { return m_reserved; }
            bool initial_data_requested() const { return m_initial_data_requested; }
            uint64_t reserved2() const { return m_reserved2; }
            uint64_t counter() const { return m_counter; }
            uint16_t event_group_id() const { return m_event_group_id; }
            some_ip_sd_entries_t* _root() const { return m__root; }
            some_ip_sd_entries_t::sd_entry_t* _parent() const { return m__parent; }
        };

    private:
        std::unique_ptr<sd_entry_header_t> m_header;
        std::unique_ptr<kaitai::kstruct> m_content;
        bool n_content;

    public:
        bool _is_null_content() { content(); return n_content; };

    private:
        some_ip_sd_entries_t* m__root;
        some_ip_sd_entries_t* m__parent;

    public:
        sd_entry_header_t* header() const { return m_header.get(); }
        kaitai::kstruct* content() const { return m_content.get(); }
        some_ip_sd_entries_t* _root() const { return m__root; }
        some_ip_sd_entries_t* _parent() const { return m__parent; }
    };

private:
    std::unique_ptr<std::vector<std::unique_ptr<sd_entry_t>>> m_entries;
    some_ip_sd_entries_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::vector<std::unique_ptr<sd_entry_t>>* entries() const { return m_entries.get(); }
    some_ip_sd_entries_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

some_ip_sd_entries.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "some_ip_sd_entries.h"

some_ip_sd_entries_t::some_ip_sd_entries_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, some_ip_sd_entries_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_entries = nullptr;
    _read();
}

void some_ip_sd_entries_t::_read() {
    m_entries = std::unique_ptr<std::vector<std::unique_ptr<sd_entry_t>>>(new std::vector<std::unique_ptr<sd_entry_t>>());
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_entries->push_back(std::move(std::unique_ptr<sd_entry_t>(new sd_entry_t(m__io, this, m__root))));
            i++;
        }
    }
}

some_ip_sd_entries_t::~some_ip_sd_entries_t() {
    _clean_up();
}

void some_ip_sd_entries_t::_clean_up() {
}

some_ip_sd_entries_t::sd_entry_t::sd_entry_t(kaitai::kstream* p__io, some_ip_sd_entries_t* p__parent, some_ip_sd_entries_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_header = nullptr;
    _read();
}

void some_ip_sd_entries_t::sd_entry_t::_read() {
    m_header = std::unique_ptr<sd_entry_header_t>(new sd_entry_header_t(m__io, this, m__root));
    n_content = true;
    switch (header()->type()) {
    case some_ip_sd_entries_t::sd_entry_t::ENTRY_TYPES_FIND: {
        n_content = false;
        m_content = std::unique_ptr<sd_service_entry_t>(new sd_service_entry_t(m__io, this, m__root));
        break;
    }
    case some_ip_sd_entries_t::sd_entry_t::ENTRY_TYPES_OFFER: {
        n_content = false;
        m_content = std::unique_ptr<sd_service_entry_t>(new sd_service_entry_t(m__io, this, m__root));
        break;
    }
    case some_ip_sd_entries_t::sd_entry_t::ENTRY_TYPES_SUBSCRIBE: {
        n_content = false;
        m_content = std::unique_ptr<sd_eventgroup_entry_t>(new sd_eventgroup_entry_t(m__io, this, m__root));
        break;
    }
    case some_ip_sd_entries_t::sd_entry_t::ENTRY_TYPES_SUBSCRIBE_ACK: {
        n_content = false;
        m_content = std::unique_ptr<sd_eventgroup_entry_t>(new sd_eventgroup_entry_t(m__io, this, m__root));
        break;
    }
    }
}

some_ip_sd_entries_t::sd_entry_t::~sd_entry_t() {
    _clean_up();
}

void some_ip_sd_entries_t::sd_entry_t::_clean_up() {
    if (!n_content) {
    }
}

some_ip_sd_entries_t::sd_entry_t::sd_entry_header_t::sd_entry_header_t(kaitai::kstream* p__io, some_ip_sd_entries_t::sd_entry_t* p__parent, some_ip_sd_entries_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void some_ip_sd_entries_t::sd_entry_t::sd_entry_header_t::_read() {
    m_type = static_cast<some_ip_sd_entries_t::sd_entry_t::entry_types_t>(m__io->read_u1());
    m_index_first_options = m__io->read_u1();
    m_index_second_options = m__io->read_u1();
    m_number_first_options = m__io->read_bits_int_be(4);
    m_number_second_options = m__io->read_bits_int_be(4);
    m__io->align_to_byte();
    m_service_id = m__io->read_u2be();
    m_instance_id = m__io->read_u2be();
    m_major_version = m__io->read_u1();
    m_ttl = m__io->read_bits_int_be(24);
}

some_ip_sd_entries_t::sd_entry_t::sd_entry_header_t::~sd_entry_header_t() {
    _clean_up();
}

void some_ip_sd_entries_t::sd_entry_t::sd_entry_header_t::_clean_up() {
}

some_ip_sd_entries_t::sd_entry_t::sd_service_entry_t::sd_service_entry_t(kaitai::kstream* p__io, some_ip_sd_entries_t::sd_entry_t* p__parent, some_ip_sd_entries_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void some_ip_sd_entries_t::sd_entry_t::sd_service_entry_t::_read() {
    m_minor_version = m__io->read_u4be();
}

some_ip_sd_entries_t::sd_entry_t::sd_service_entry_t::~sd_service_entry_t() {
    _clean_up();
}

void some_ip_sd_entries_t::sd_entry_t::sd_service_entry_t::_clean_up() {
}

some_ip_sd_entries_t::sd_entry_t::sd_eventgroup_entry_t::sd_eventgroup_entry_t(kaitai::kstream* p__io, some_ip_sd_entries_t::sd_entry_t* p__parent, some_ip_sd_entries_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void some_ip_sd_entries_t::sd_entry_t::sd_eventgroup_entry_t::_read() {
    m_reserved = m__io->read_u1();
    m_initial_data_requested = m__io->read_bits_int_be(1);
    m_reserved2 = m__io->read_bits_int_be(3);
    m_counter = m__io->read_bits_int_be(4);
    m__io->align_to_byte();
    m_event_group_id = m__io->read_u2be();
}

some_ip_sd_entries_t::sd_entry_t::sd_eventgroup_entry_t::~sd_eventgroup_entry_t() {
    _clean_up();
}

void some_ip_sd_entries_t::sd_entry_t::sd_eventgroup_entry_t::_clean_up() {
}