/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stdio.h>
#include <plc4c/spi/evaluation_helper.h>
#include "alarm_message_query_type.h"

// Code generated by code-generation. DO NOT EDIT.


// Constant values.
static const uint16_t PLC4C_S7_READ_WRITE_ALARM_MESSAGE_QUERY_TYPE_DATA_LENGTH_const = 0xFFFF;
uint16_t PLC4C_S7_READ_WRITE_ALARM_MESSAGE_QUERY_TYPE_DATA_LENGTH() {
  return PLC4C_S7_READ_WRITE_ALARM_MESSAGE_QUERY_TYPE_DATA_LENGTH_const;
}

// Parse function.
plc4c_return_code plc4c_s7_read_write_alarm_message_query_type_parse(plc4c_spi_read_buffer* readBuffer, plc4c_s7_read_write_alarm_message_query_type** _message) {
  uint16_t startPos = plc4c_spi_read_get_pos(readBuffer);
  uint16_t curPos;
  plc4c_return_code _res = OK;

  // Allocate enough memory to contain this data structure.
  (*_message) = malloc(sizeof(plc4c_s7_read_write_alarm_message_query_type));
  if(*_message == NULL) {
    return NO_MEMORY;
  }

  // Simple Field (functionId)
  uint8_t functionId = 0;
  _res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &functionId);
  if(_res != OK) {
    return _res;
  }
  (*_message)->function_id = functionId;

  // Simple Field (numberOfObjects)
  uint8_t numberOfObjects = 0;
  _res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &numberOfObjects);
  if(_res != OK) {
    return _res;
  }
  (*_message)->number_of_objects = numberOfObjects;

  // Simple Field (returnCode)
  plc4c_s7_read_write_data_transport_error_code* returnCode;
  _res = plc4c_s7_read_write_data_transport_error_code_parse(readBuffer, (void*) &returnCode);
  if(_res != OK) {
    return _res;
  }
  (*_message)->return_code = *returnCode;

  // Simple Field (transportSize)
  plc4c_s7_read_write_data_transport_size* transportSize;
  _res = plc4c_s7_read_write_data_transport_size_parse(readBuffer, (void*) &transportSize);
  if(_res != OK) {
    return _res;
  }
  (*_message)->transport_size = *transportSize;

  // Const Field (DataLength)
  uint16_t DataLength = 0;
  _res = plc4c_spi_read_unsigned_short(readBuffer, 16, (uint16_t*) &DataLength);
  if(_res != OK) {
    return _res;
  }
  if(DataLength != PLC4C_S7_READ_WRITE_ALARM_MESSAGE_QUERY_TYPE_DATA_LENGTH()) {
    return PARSE_ERROR;
    // throw new ParseException("Expected constant value " + PLC4C_S7_READ_WRITE_ALARM_MESSAGE_QUERY_TYPE_DATA_LENGTH + " but got " + DataLength);
  }

  // Array field (messageObjects)
  plc4c_list* messageObjects = NULL;
  plc4c_utils_list_create(&messageObjects);
  if(messageObjects == NULL) {
    return NO_MEMORY;
  }
  {
    // Count array
    uint16_t itemCount = (uint16_t) numberOfObjects;
    for(int curItem = 0; curItem < itemCount; curItem++) {
      bool lastItem = curItem == (itemCount - 1);
      plc4c_s7_read_write_alarm_message_object_query_type* _value = NULL;
      _res = plc4c_s7_read_write_alarm_message_object_query_type_parse(readBuffer, (void*) &_value);
      if(_res != OK) {
        return _res;
      }
      plc4c_utils_list_insert_head_value(messageObjects, _value);
    }
  }
  (*_message)->message_objects = messageObjects;

  return OK;
}

plc4c_return_code plc4c_s7_read_write_alarm_message_query_type_serialize(plc4c_spi_write_buffer* writeBuffer, plc4c_s7_read_write_alarm_message_query_type* _message) {
  plc4c_return_code _res = OK;

  // Simple Field (functionId)
  _res = plc4c_spi_write_unsigned_byte(writeBuffer, 8, _message->function_id);
  if(_res != OK) {
    return _res;
  }

  // Simple Field (numberOfObjects)
  _res = plc4c_spi_write_unsigned_byte(writeBuffer, 8, _message->number_of_objects);
  if(_res != OK) {
    return _res;
  }

  // Simple Field (returnCode)
  _res = plc4c_s7_read_write_data_transport_error_code_serialize(writeBuffer, &_message->return_code);
  if(_res != OK) {
    return _res;
  }

  // Simple Field (transportSize)
  _res = plc4c_s7_read_write_data_transport_size_serialize(writeBuffer, &_message->transport_size);
  if(_res != OK) {
    return _res;
  }

  // Const Field (DataLength)
  plc4c_spi_write_unsigned_short(writeBuffer, 16, PLC4C_S7_READ_WRITE_ALARM_MESSAGE_QUERY_TYPE_DATA_LENGTH());

  // Array field (messageObjects)
  {
    uint8_t itemCount = plc4c_utils_list_size(_message->message_objects);
    for(int curItem = 0; curItem < itemCount; curItem++) {
      bool lastItem = curItem == (itemCount - 1);
      plc4c_s7_read_write_alarm_message_object_query_type* _value = (plc4c_s7_read_write_alarm_message_object_query_type*) plc4c_utils_list_get_value(_message->message_objects, curItem);
      _res = plc4c_s7_read_write_alarm_message_object_query_type_serialize(writeBuffer, (void*) _value);
      if(_res != OK) {
        return _res;
      }
    }
  }

  return OK;
}

uint16_t plc4c_s7_read_write_alarm_message_query_type_length_in_bytes(plc4c_s7_read_write_alarm_message_query_type* _message) {
  return plc4c_s7_read_write_alarm_message_query_type_length_in_bits(_message) / 8;
}

uint16_t plc4c_s7_read_write_alarm_message_query_type_length_in_bits(plc4c_s7_read_write_alarm_message_query_type* _message) {
  uint16_t lengthInBits = 0;

  // Simple field (functionId)
  lengthInBits += 8;

  // Simple field (numberOfObjects)
  lengthInBits += 8;

  // Simple field (returnCode)
  lengthInBits += plc4c_s7_read_write_data_transport_error_code_length_in_bits(&_message->return_code);

  // Simple field (transportSize)
  lengthInBits += plc4c_s7_read_write_data_transport_size_length_in_bits(&_message->transport_size);

  // Const Field (DataLength)
  lengthInBits += 16;

  // Array field
  if(_message->message_objects != NULL) {
    plc4c_list_element* curElement = _message->message_objects->tail;
    while (curElement != NULL) {
      lengthInBits += plc4c_s7_read_write_alarm_message_object_query_type_length_in_bits((plc4c_s7_read_write_alarm_message_object_query_type*) curElement->value);
      curElement = curElement->next;
    }
  }

  return lengthInBits;
}

