Subversion Repositories HelenOS

Rev

Rev 4243 | Rev 4505 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup packet
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Packet client implementation.
  35.  */
  36.  
  37. #include <async.h>
  38. #include <errno.h>
  39. #include <mem.h>
  40. #include <unistd.h>
  41. //#include <stdio.h>
  42.  
  43. #include <ipc/ipc.h>
  44. #include <sys/mman.h>
  45.  
  46. #include "../../err.h"
  47. #include "../../messages.h"
  48.  
  49. #include "packet.h"
  50. #include "packet_header.h"
  51. #include "packet_client.h"
  52.  
  53. int packet_copy_data( packet_t packet, const void * data, size_t length ){
  54.     if( ! packet_is_valid( packet )) return EINVAL;
  55.     if( packet->data_start + length >= packet->length ) return ENOMEM;
  56.     memcpy(( void * ) packet + packet->data_start, data, length );
  57.     if( packet->data_start + length > packet->data_end ){
  58.         packet->data_end = packet->data_start + length;
  59.     }
  60.     return EOK;
  61. }
  62.  
  63. void * packet_prefix( packet_t packet, size_t length ){
  64.     if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL;
  65.     packet->data_start -= length;
  66.     return ( void * ) packet + packet->data_start;
  67. }
  68.  
  69. void * packet_suffix( packet_t packet, size_t length ){
  70.     if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
  71.     packet->data_end += length;
  72.     return ( void * ) packet + packet->data_end - length;
  73. }
  74.  
  75. int packet_trim( packet_t packet, size_t prefix, size_t suffix ){
  76.     if( ! packet_is_valid( packet )) return EINVAL;
  77.     if( prefix + suffix > packet->data_end - packet->data_start ) return ENOMEM;
  78.     packet->data_start += prefix;
  79.     packet->data_end -= suffix;
  80.     return EOK;
  81. }
  82.  
  83. packet_id_t packet_get_id( const packet_t packet ){
  84.     return packet_is_valid( packet ) ? packet->packet_id : 0;
  85. }
  86.  
  87. int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
  88.     if( !( packet_is_valid( packet ) && src && dest )) return EINVAL;
  89.     if( ! packet->addr_len ) return 0;
  90.     * src = ( void * ) packet + packet->src_addr;
  91.     * dest = ( void * ) packet + packet->dest_addr;
  92.     return packet->addr_len;
  93. }
  94.  
  95. size_t packet_get_data_length( const packet_t packet ){
  96.     if( ! packet_is_valid( packet )) return 0;
  97.     return packet->data_end - packet->data_start;
  98. }
  99.  
  100. void * packet_get_data( const packet_t packet ){
  101.     if( ! packet_is_valid( packet )) return NULL;
  102.     return ( void * ) packet + packet->data_start;
  103. }
  104.  
  105. int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
  106.     size_t  padding;
  107.  
  108.     if( ! packet_is_valid( packet )) return EINVAL;
  109.     if( packet->addr_len < addr_len ) return ENOMEM;
  110.     padding = packet->addr_len - addr_len;
  111.     if( src ){
  112.         memcpy(( void * ) packet + packet->src_addr, src, addr_len );
  113.         if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding );
  114.     }else{
  115.         bzero(( void * ) packet + packet->src_addr + addr_len, packet->addr_len );
  116.     }
  117.     if( dest ){
  118.         memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
  119.         if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding );
  120.     }else{
  121.         bzero(( void * ) packet + packet->dest_addr + addr_len, packet->addr_len );
  122.     }
  123.     return EOK;
  124. }
  125.  
  126. /** @}
  127.  */
  128.