Subversion Repositories HelenOS

Rev

Rev 3912 | Rev 4243 | 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 <unistd.h>
  40. //#include <stdio.h>
  41. #include <string.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. /** Obtains the packet from the packet server as the shared memory block.
  54.  *  Creates the local packet mapping as well.
  55.  *  @param phone The packet server module phone. Input parameter.
  56.  *  @param packet The packet reference pointer to store the received packet reference. Output parameter.
  57.  *  @param packet_id The packet identifier. Input parameter.
  58.  *  @param size The packet total size in bytes. Input parameter.
  59.  *  @returns EOK on success.
  60.  *  \todo ipc_share_in_start() error?
  61.  *  @returns Other error codes as defined for the pm_add() function.
  62.  */
  63. int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size );
  64.  
  65. int packet_copy_data( packet_t packet, const void * data, size_t length ){
  66.     if( ! packet_is_valid( packet )) return EINVAL;
  67.     if( packet->data_start + length >= packet->length ) return ENOMEM;
  68.     memcpy(( void * ) packet + packet->data_start, data, length );
  69.     if( packet->data_start + length > packet->data_end ){
  70.         packet->data_end = packet->data_start + length;
  71.     }
  72.     return EOK;
  73. }
  74.  
  75. void * packet_prefix( packet_t packet, size_t length ){
  76.     if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL;
  77.     packet->data_start -= length;
  78.     return ( void * ) packet + packet->data_start;
  79. }
  80.  
  81. void * packet_suffix( packet_t packet, size_t length ){
  82.     if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
  83.     packet->data_end += length;
  84.     return ( void * ) packet + packet->data_end - length;
  85. }
  86.  
  87. int packet_trim( packet_t packet, size_t prefix, size_t suffix ){
  88.     if( ! packet_is_valid( packet )) return EINVAL;
  89.     if( prefix + suffix > packet->data_end - packet->data_start ) return ENOMEM;
  90.     packet->data_start += prefix;
  91.     packet->data_end -= suffix;
  92.     return EOK;
  93. }
  94.  
  95. packet_id_t packet_get_id( const packet_t packet ){
  96.     return packet_is_valid( packet ) ? packet->packet_id : 0;
  97. }
  98.  
  99. int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
  100.     if( !( packet_is_valid( packet ) && src && dest )) return EINVAL;
  101.     if( ! packet->addr_len ) return 0;
  102.     * src = ( void * ) packet + packet->src_addr;
  103.     * dest = ( void * ) packet + packet->dest_addr;
  104.     return packet->addr_len;
  105. }
  106.  
  107. size_t packet_get_data_length( const packet_t packet ){
  108.     if( ! packet_is_valid( packet )) return 0;
  109.     return packet->data_end - packet->data_start;
  110. }
  111.  
  112. void * packet_get_data( const packet_t packet ){
  113.     if( ! packet_is_valid( packet )) return NULL;
  114.     return ( void * ) packet + packet->data_start;
  115. }
  116.  
  117. int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
  118.     size_t  padding;
  119.  
  120.     if( ! packet_is_valid( packet )) return EINVAL;
  121.     if( packet->addr_len >= addr_len ) return ENOMEM;
  122.     padding = packet->addr_len - addr_len;
  123.     if( src ){
  124.         memcpy(( void * ) packet + packet->src_addr, src, addr_len );
  125.         memset(( void * ) packet + packet->src_addr + addr_len, 0, padding );
  126.     }else{
  127.         memset(( void * ) packet + packet->src_addr + addr_len, 0, packet->addr_len );
  128.     }
  129.     if( dest ){
  130.         memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
  131.         memset(( void * ) packet + packet->dest_addr + addr_len, 0, padding );
  132.     }else{
  133.         memset(( void * ) packet + packet->dest_addr + addr_len, 0, packet->addr_len );
  134.     }
  135.     return EOK;
  136. }
  137.  
  138. int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
  139.     ERROR_DECLARE;
  140.  
  141.     unsigned int        size;
  142.  
  143.     if( ! packet ) return EINVAL;
  144.     * packet = pm_find( packet_id );
  145.     if( * packet ) return EOK;
  146.     ERROR_PROPAGATE( async_req_1_1( phone, NET_PACKET_GET_SIZE, packet_id, & size ));
  147.     return packet_return( phone, packet, packet_id, size );
  148. }
  149.  
  150. int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ){
  151.     ERROR_DECLARE;
  152.  
  153.     aid_t       message;
  154.     ipc_call_t  answer;
  155.     ipcarg_t    result;
  156.  
  157.     message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer );
  158.     * packet = ( packet_t ) as_get_mappable_page( size );
  159.     if( ERROR_OCCURRED( ipc_share_in_start_0_0( phone, * packet, size ))
  160.     || ERROR_OCCURRED( pm_add( * packet ))){
  161.         munmap( * packet, size );
  162.         async_wait_for( message, NULL );
  163.         return ERROR_CODE;
  164.     }
  165.     async_wait_for( message, & result );
  166.     return result;
  167. }
  168.  
  169. packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){
  170.     ERROR_DECLARE;
  171.  
  172.     packet_id_t packet_id;
  173.     unsigned int size;
  174.     packet_t packet;
  175.  
  176.     if( ERROR_OCCURRED( async_req_4_2( phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, & packet_id, & size ))
  177.     || ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){
  178.         return NULL;
  179.     }
  180.     return packet;
  181. }
  182.  
  183. packet_t packet_get_1( int phone, size_t content ){
  184.     ERROR_DECLARE;
  185.  
  186.     packet_id_t packet_id;
  187.     unsigned int    size;
  188.     packet_t    packet;
  189.  
  190.     if( ERROR_OCCURRED( async_req_1_2( phone, NET_PACKET_CREATE_1, content, & packet_id, & size ))
  191.     || ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){
  192.         return NULL;
  193.     }
  194.     return packet;
  195. }
  196.  
  197. void packet_release( int phone, packet_id_t packet_id ){
  198.     async_msg_1( phone, NET_PACKET_RELEASE, packet_id );
  199. }
  200.  
  201. /** @}
  202.  */
  203.