/*
* This file was automatically generated using Avlos.
* https://github.com/tinymovr/avlos
*
* Any changes to this file will be overwritten when
* content is regenerated.
*/

{%- for include in includes %}
#include {{ include | as_include }}
{%- endfor %}

{% set comma = joiner(", ") %}
uint8_t (*avlos_endpoints[{{ instance | endpoints | length }}])(uint8_t * buffer, uint8_t * buffer_len, Avlos_Command cmd) = { {%- for attr in instance | endpoints %}{{ comma() }}&avlos_{{attr.full_name | replace(".", "_") }}{%- endfor %} };
uint32_t avlos_proto_hash = {{ instance.hash_uint32 }};

uint32_t _avlos_get_proto_hash(void)
{
    return avlos_proto_hash;
}

{%- for attr in instance | endpoints %}

uint8_t avlos_{{attr.full_name | replace(".", "_") }}(uint8_t * buffer, uint8_t * buffer_len, Avlos_Command cmd)
{
    {%- if attr.getter_name %}
    if (AVLOS_CMD_READ == cmd) {
        {{attr.dtype.c_name}} v;
        v = {{ attr.getter_name }}();
        *buffer_len = sizeof(v);
        memcpy(buffer, &v, sizeof(v));
        return AVLOS_RET_READ;
    }
    {%- endif %}
    {%- if attr.setter_name %}
    {% if attr.getter_name %}else {% endif %}if (AVLOS_CMD_WRITE == cmd) {
        {{attr.dtype.c_name}} v;
        memcpy(&v, buffer, sizeof(v));
        {{ attr.setter_name }}(v);
        return AVLOS_RET_WRITE;
    }
    {%- endif %}
    {%- if attr.caller_name %}
        {%- if attr.arguments | length > 0 %}
    uint8_t offset = 0;
        {%- endif %}
    {%- for arg in attr.arguments %}
    {{ arg.dtype.c_name }} {{arg.name}};
    memcpy(&{{arg.name}}, buffer+offset, sizeof({{arg.name}}));
    offset += sizeof({{arg.name}});
    {%- endfor %}

    {%- set comma = joiner(", ") %}
    {%- if attr.dtype.c_name != "void" %}
    {{attr.dtype.c_name}} ret_val = {{ attr.caller_name }}({%- for arg in attr.arguments %}{{ comma() }}{{ arg.name }} {%- endfor %});
    memcpy(buffer, &ret_val, sizeof(ret_val));
    *buffer_len = sizeof(ret_val);
    {%- else %}
    {{ attr.caller_name }}({%- for arg in attr.arguments %}{{ comma() }}{{ arg.name }} {%- endfor %});
    {%- endif %}

    return AVLOS_RET_CALL;
    {%- else %}
    return AVLOS_RET_NOACTION;
    {%- endif %}
}

{%- endfor %}
