AUTOSAR SOME/IP Service Discovery: C++98/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++98/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++98/STL source code to parse AUTOSAR SOME/IP Service Discovery

some_ip_sd.h

#ifndef SOME_IP_SD_H_
#define SOME_IP_SD_H_

// 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 "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 = 0, some_ip_sd_t* p__root = 0);

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 = 0, some_ip_sd_t* p__root = 0);

    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:
    sd_flags_t* m_flags;
    std::string m_reserved;
    uint32_t m_len_entries;
    some_ip_sd_entries_t* m_entries;
    uint32_t m_len_options;
    some_ip_sd_options_t* m_options;
    some_ip_sd_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_entries;
    kaitai::kstream* m__io__raw_entries;
    std::string m__raw_options;
    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; }
    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; }
    uint32_t len_options() const { return m_len_options; }
    some_ip_sd_options_t* options() const { return m_options; }
    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; }
    std::string _raw_options() const { return m__raw_options; }
    kaitai::kstream* _io__raw_options() const { return m__io__raw_options; }
};

#endif  // SOME_IP_SD_H_

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 = 0;
    m_entries = 0;
    m__io__raw_entries = 0;
    m_options = 0;
    m__io__raw_options = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void some_ip_sd_t::_read() {
    m_flags = 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 = new kaitai::kstream(m__raw_entries);
    m_entries = new some_ip_sd_entries_t(m__io__raw_entries);
    m_len_options = m__io->read_u4be();
    m__raw_options = m__io->read_bytes(len_options());
    m__io__raw_options = new kaitai::kstream(m__raw_options);
    m_options = new some_ip_sd_options_t(m__io__raw_options);
}

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

void some_ip_sd_t::_clean_up() {
    if (m_flags) {
        delete m_flags; m_flags = 0;
    }
    if (m__io__raw_entries) {
        delete m__io__raw_entries; m__io__raw_entries = 0;
    }
    if (m_entries) {
        delete m_entries; m_entries = 0;
    }
    if (m__io__raw_options) {
        delete m__io__raw_options; m__io__raw_options = 0;
    }
    if (m_options) {
        delete m_options; m_options = 0;
    }
}

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;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

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() {
}