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

The main tasks of the Service Discovery Protocol are communicating the availability of functional entities called services in the in-vehicle communication as well as controlling the send behavior of event messages. This allows sending only event messages to receivers requiring them (Publish/Subscribe). The solution described here is also known as SOME/IP-SD (Scalable service-Oriented MiddlewarE over IP - Service Discovery).

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 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_t data(&ks);
    

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

data.flags() // => The SOME/IP-SD Header shall start with an 8 Bit field called flags.

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

some_ip_sd.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 "some_ip_sd_entries.h"
#include "some_ip_sd_options.h"

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

/**
 * The main tasks of the Service Discovery Protocol are communicating the
 * availability of functional entities called services in the in-vehicle
 * communication as well as controlling the send behavior of event messages.
 * This allows sending only event messages to receivers requiring them (Publish/Subscribe).
 * The solution described here is also known as SOME/IP-SD
 * (Scalable service-Oriented MiddlewarE over IP - Service Discovery).
 * \sa https://www.autosar.org/fileadmin/standards/foundation/19-11/AUTOSAR_PRS_SOMEIPServiceDiscoveryProtocol.pdf Source
 */

class some_ip_sd_t : public kaitai::kstruct {

public:
    class sd_flags_t;

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

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

public:
    ~some_ip_sd_t();

    /**
     * \sa AUTOSAR_PRS_SOMEIPServiceDiscoveryProtocol.pdf - Figure 4.3
     */

    class sd_flags_t : public kaitai::kstruct {

    public:

        sd_flags_t(kaitai::kstream* p__io, some_ip_sd_t* p__parent = nullptr, some_ip_sd_t* p__root = nullptr);

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

    public:
        ~sd_flags_t();

    private:
        bool m_reboot;
        bool m_unicast;
        bool m_initial_data;
        uint64_t m_reserved;
        some_ip_sd_t* m__root;
        some_ip_sd_t* m__parent;

    public:
        bool reboot() const { return m_reboot; }
        bool unicast() const { return m_unicast; }
        bool initial_data() const { return m_initial_data; }
        uint64_t reserved() const { return m_reserved; }
        some_ip_sd_t* _root() const { return m__root; }
        some_ip_sd_t* _parent() const { return m__parent; }
    };

private:
    std::unique_ptr<sd_flags_t> m_flags;
    std::string m_reserved;
    uint32_t m_len_entries;
    std::unique_ptr<some_ip_sd_entries_t> m_entries;
    uint32_t m_len_options;
    std::unique_ptr<some_ip_sd_options_t> m_options;
    some_ip_sd_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_entries;
    std::unique_ptr<kaitai::kstream> m__io__raw_entries;
    std::string m__raw_options;
    std::unique_ptr<kaitai::kstream> m__io__raw_options;

public:

    /**
     * The SOME/IP-SD Header shall start with an 8 Bit field called flags.
     */
    sd_flags_t* flags() const { return m_flags.get(); }
    std::string reserved() const { return m_reserved; }
    uint32_t len_entries() const { return m_len_entries; }
    some_ip_sd_entries_t* entries() const { return m_entries.get(); }
    uint32_t len_options() const { return m_len_options; }
    some_ip_sd_options_t* options() const { return m_options.get(); }
    some_ip_sd_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::string _raw_entries() const { return m__raw_entries; }
    kaitai::kstream* _io__raw_entries() const { return m__io__raw_entries.get(); }
    std::string _raw_options() const { return m__raw_options; }
    kaitai::kstream* _io__raw_options() const { return m__io__raw_options.get(); }
};

some_ip_sd.cpp

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

#include "some_ip_sd.h"

some_ip_sd_t::some_ip_sd_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, some_ip_sd_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_flags = nullptr;
    m_entries = nullptr;
    m__io__raw_entries = nullptr;
    m_options = nullptr;
    m__io__raw_options = nullptr;
    _read();
}

void some_ip_sd_t::_read() {
    m_flags = std::unique_ptr<sd_flags_t>(new sd_flags_t(m__io, this, m__root));
    m_reserved = m__io->read_bytes(3);
    m_len_entries = m__io->read_u4be();
    m__raw_entries = m__io->read_bytes(len_entries());
    m__io__raw_entries = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_entries));
    m_entries = std::unique_ptr<some_ip_sd_entries_t>(new some_ip_sd_entries_t(m__io__raw_entries.get()));
    m_len_options = m__io->read_u4be();
    m__raw_options = m__io->read_bytes(len_options());
    m__io__raw_options = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_options));
    m_options = std::unique_ptr<some_ip_sd_options_t>(new some_ip_sd_options_t(m__io__raw_options.get()));
}

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

void some_ip_sd_t::_clean_up() {
}

some_ip_sd_t::sd_flags_t::sd_flags_t(kaitai::kstream* p__io, some_ip_sd_t* p__parent, some_ip_sd_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void some_ip_sd_t::sd_flags_t::_read() {
    m_reboot = m__io->read_bits_int_be(1);
    m_unicast = m__io->read_bits_int_be(1);
    m_initial_data = m__io->read_bits_int_be(1);
    m_reserved = m__io->read_bits_int_be(5);
}

some_ip_sd_t::sd_flags_t::~sd_flags_t() {
    _clean_up();
}

void some_ip_sd_t::sd_flags_t::_clean_up() {
}