AUTOSAR SOME/IP container: C++98/STL parsing library

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of AUTOSAR SOME/IP container 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_container_t data(&ks);
    

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

data.some_ip_packages() // => get some ip packages

C++98/STL source code to parse AUTOSAR SOME/IP container

some_ip_container.h

#ifndef SOME_IP_CONTAINER_H_
#define SOME_IP_CONTAINER_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.h"
#include <vector>

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

class some_ip_container_t : public kaitai::kstruct {

public:

    some_ip_container_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, some_ip_container_t* p__root = 0);

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

public:
    ~some_ip_container_t();

private:
    std::vector<some_ip_t*>* m_some_ip_packages;
    some_ip_container_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::vector<some_ip_t*>* some_ip_packages() const { return m_some_ip_packages; }
    some_ip_container_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // SOME_IP_CONTAINER_H_

some_ip_container.cpp

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

#include "some_ip_container.h"

some_ip_container_t::some_ip_container_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, some_ip_container_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_some_ip_packages = 0;

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

void some_ip_container_t::_read() {
    m_some_ip_packages = new std::vector<some_ip_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_some_ip_packages->push_back(new some_ip_t(m__io));
            i++;
        }
    }
}

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

void some_ip_container_t::_clean_up() {
    if (m_some_ip_packages) {
        for (std::vector<some_ip_t*>::iterator it = m_some_ip_packages->begin(); it != m_some_ip_packages->end(); ++it) {
            delete *it;
        }
        delete m_some_ip_packages; m_some_ip_packages = 0;
    }
}