/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup packet
* @{
*/
/** @file
* Packet client.
* The hosting module has to be compiled with both the packet.c and the packet_client.c source files.
* To function correctly, initialization of the packet map by the pm_init() function has to happen at the first place.
* The module should not send the packet messages to the packet server but use the functions provided.
* The packet map should be released by the pm_destroy() function during the module termination.
* @see packet.h
*/
#ifndef __NET_PACKET_CLIENT_H__
#define __NET_PACKET_CLIENT_H__
#include "packet.h"
/** Allocates the specified type right before the actual packet content and returns its pointer.
* The wrapper of the packet_prepend() function.
* @param packet The packet to be used. Input parameter.
* @param type The type to be allocated at the beginning of the packet content. Input parameter.
* @returns The typed pointer to the allocated memory.
* @returns NULL if the packet is not valid.
* @returns NULL if there is not enough memory left.
*/
#define PACKET_PREFIX( packet, type ) ( type * ) packet_prepend(( packet ), sizeof( type ))
/** Allocates the specified type right after the actual packet content and returns its pointer.
* The wrapper of the packet_append() function.
* @param packet The packet to be used. Input parameter.
* @param type The type to be allocated at the end of the packet content. Input parameter.
* @returns The typed pointer to the allocated memory.
* @returns NULL if the packet is not valid.
* @returns NULL if there is not enough memory left.
*/
#define PACKET_SUFFIX( packet, type ) ( type * ) packet_append(( packet ), sizeof( type ))
/** Trims the actual packet content by the specified prefix and suffix types.
* The wrapper of the packet_trim() function.
* @param packet The packet to be trimmed. Input parameter.
* @param prefix The type of the prefix to be removed from the beginning of the packet content. Input parameter.
* @param suffix The type of the suffix to be removed from the end of the packet content. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns EINVAL if there is not enough memory left.
*/
#define PACKET_TRIM( packet, prefix, suffix ) packet_trim(( packet ), sizeof( prefix ), sizeof( suffix ))
/** Allocates the specified space right before the actual packet content and returns its pointer.
* @param packet The packet to be used. Input parameter.
* @param length The space length to be allocated at the beginning of the packet content. Input parameter.
* @returns The pointer to the allocated memory.
* @returns NULL if there is not enough memory left.
*/
void * packet_prefix( packet_t packet, size_t length );
/** Allocates the specified space right after the actual packet content and returns its pointer.
* @param packet The packet to be used. Input parameter.
* @param length The space length to be allocated at the end of the packet content. Input parameter.
* @returns The pointer to the allocated memory.
* @returns NULL if there is not enough memory left.
*/
void * packet_suffix( packet_t packet, size_t length );
/** Trims the actual packet content by the specified prefix and suffix lengths.
* @param packet The packet to be trimmed. Input parameter.
* @param prefix The prefix length to be removed from the beginning of the packet content. Input parameter.
* @param suffix The suffix length to be removed from the end of the packet content. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns ENOMEM if there is not enough memory left.
*/
int packet_trim( packet_t packet, size_t prefix, size_t suffix );
/** Copies the packet content.
* Obtains the new packet from the packet server.
* @param phone The packet server module phone. Input parameter.
* @param owner The owner of the new packet. Input parameter.
* @param packet The packet reference. Input parameter.
* @returns The packet copy.
* @returns NULL if the source packet ids not valid.
* @returns NULL on error.
* \todo other error?
*/
packet_t packet_copy( int phone, services_t owner, const packet_t packet );
/** Copies the specified data to the beginning of the actual packet content.
* Pushes the content end if needed.
* @param packet The packet to be filled. Input parameter.
* @param data The data to be copied. Input parameter.
* @param length The length of the copied data. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns ENOMEM if there is not enough memory left.
*/
int packet_copy_data( packet_t packet, const void * data, size_t length );
/** Returns the packet identifier.
* @param packet The packet. Input parameter.
* @returns The packet identifier.
* @returns Zero (0) if the packet is not valid.
*/
packet_id_t packet_get_id( const packet_t packet );
/** Returns the packet content length.
* @param packet The packet. Input parameter.
* @returns The packet content length in bytes.
* @returns Zero (0) if the packet is not valid.
*/
size_t packet_get_data_length( const packet_t packet );
/** Returns the pointer to the beginning of the packet content.
* @param packet The packet. Input parameter.
* @returns The pointer to the beginning of the packet content.
* @returns NULL if the packet is not valid.
*/
void * packet_get_data( const packet_t packet );
/** Returns the stored packet addresses and their length.
* @param packet The packet. Input parameter.
* @param src The source address. Output parameter.
* @param dest The destination address. Output parameter.
* @returns The addresses length.
* @returns Zero (0) if the addresses are not present.
* @returns EINVAL if the packet is not valid.
* @returns EINVAL if the src and/or the dest parameter is NULL.
*/
int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest );
/** Returns the packet operation mode.
* @param packet The packet. Input parameter.
* @returns The packet operation mode.
* @see packet_mode
*/
packet_mode_t packet_get_mode( const packet_t packet );
/** Sets the packet addresses.
* @param packet The packet. Input parameter.
* @param src The new source address. Output parameter.
* @param dest The new destination address. Output parameter.
* @param addr_len The addresses length.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns ENOMEM if there is not enough memory left.
*/
int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len );
/** Sets the packet operation mode.
* @param packet The packet. Input parameter.
* @param mode The new packet operation mode. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @see packet_mode
*/
int packet_set_mode( packet_t packet, packet_mode_t mode );
/** Sets the packet owner.
* @param packet The packet. Input parameter.
* @param owner The new packet owner. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
*/
int packet_set_owner( packet_t packet, services_t owner );
/** Translates the packet identifier to the packet reference.
* Tries to find mapping first.
* Contacts the packet server to share the packet if the mapping is not present.
* @param phone The packet server module phone. Input parameter.
* @param packet The packet reference. Output parameter.
* @param packet_id The packet identifier. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet parameter is NULL.
* @returns Other error codes as defined for the NET_PACKET_GET_SIZE message.
* \todo errors as packet_return()
*/
int packet_translate( int phone, packet_ref packet, packet_id_t packet_id );
/** Obtains the packet of the given dimensions.
* Contacts the packet server to return the appropriate packet.
* @param phone The packet server module phone. Input parameter.
* @param owner The owner of the new packet. Input parameter.
* @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
* @param max_prefix The maximal prefix length in bytes. Input parameter.
* @param max_content The maximal content length in bytes. Input parameter.
* @param max_suffix The maximal suffix length in bytes. Input parameter.
* @returns The packet reference.
* @returns NULL on error.
* \todo error as NET_PACKET_CREATE_5, packet_return()
*/
packet_t packet_get_5( int phone, services_t owner, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix );
/** Obtains the packet of the given content size.
* Contacts the packet server to return the appropriate packet.
* @param phone The packet server module phone. Input parameter.
* @param owner The owner of the new packet. Input parameter.
* @param content The maximal content length in bytes. Input parameter.
* @returns The packet reference.
* @returns NULL on error.
* \todo error as NET_PACKET_CREATE_1, packet_return()
*/
packet_t packet_get_1( int phone, services_t owner, size_t content );
/** Releases the packet.
* The packet is marked as free for use.
* The module should not use the packet after this point until it is received or obtained again.
* @param phone The packet server module phone. Input parameter.
* @param packet_id The packet identifier. Input parameter.
*/
void packet_release( int phone, packet_id_t packet_id );
#endif
/** @}
*/