Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4307 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3901 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3901 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
 
3912 mejdrech 29
/** @addtogroup packet
3901 mejdrech 30
 *  @{
31
 */
32
 
33
/** @file
3912 mejdrech 34
 *  Packet client implementation.
3901 mejdrech 35
 */
36
 
37
#include <async.h>
38
#include <errno.h>
4192 mejdrech 39
#include <mem.h>
3901 mejdrech 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
 
3912 mejdrech 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
 */
3901 mejdrech 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
 
3912 mejdrech 75
void * packet_prefix( packet_t packet, size_t length ){
3901 mejdrech 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
 
3912 mejdrech 81
void * packet_suffix( packet_t packet, size_t length ){
3901 mejdrech 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
 
3912 mejdrech 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;
3901 mejdrech 90
    packet->data_start += prefix;
3912 mejdrech 91
    packet->data_end -= suffix;
3901 mejdrech 92
    return EOK;
93
}
94
 
3912 mejdrech 95
packet_id_t packet_get_id( const packet_t packet ){
3901 mejdrech 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 ){
3912 mejdrech 100
    if( !( packet_is_valid( packet ) && src && dest )) return EINVAL;
101
    if( ! packet->addr_len ) return 0;
3901 mejdrech 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
 
3912 mejdrech 120
    if( ! packet_is_valid( packet )) return EINVAL;
4243 mejdrech 121
    if( packet->addr_len < addr_len ) return ENOMEM;
3901 mejdrech 122
    padding = packet->addr_len - addr_len;
123
    if( src ){
124
        memcpy(( void * ) packet + packet->src_addr, src, addr_len );
4243 mejdrech 125
        if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding );
3901 mejdrech 126
    }else{
4192 mejdrech 127
        bzero(( void * ) packet + packet->src_addr + addr_len, packet->addr_len );
3901 mejdrech 128
    }
129
    if( dest ){
130
        memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
4243 mejdrech 131
        if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding );
3901 mejdrech 132
    }else{
4192 mejdrech 133
        bzero(( void * ) packet + packet->dest_addr + addr_len, packet->addr_len );
3901 mejdrech 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;
3912 mejdrech 155
    ipcarg_t    result;
3901 mejdrech 156
 
157
    message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer );
158
    * packet = ( packet_t ) as_get_mappable_page( size );
3912 mejdrech 159
    if( ERROR_OCCURRED( ipc_share_in_start_0_0( phone, * packet, size ))
160
    || ERROR_OCCURRED( pm_add( * packet ))){
3901 mejdrech 161
        munmap( * packet, size );
162
        async_wait_for( message, NULL );
163
        return ERROR_CODE;
164
    }
3912 mejdrech 165
    async_wait_for( message, & result );
166
    return result;
3901 mejdrech 167
}
168
 
3990 mejdrech 169
packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){
3901 mejdrech 170
    ERROR_DECLARE;
171
 
172
    packet_id_t packet_id;
173
    unsigned int size;
174
    packet_t packet;
175
 
4243 mejdrech 176
    if( ERROR_OCCURRED( async_req_4_2( phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, ( ipcarg_t * ) & packet_id, & size ))
3912 mejdrech 177
    || ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){
3901 mejdrech 178
        return NULL;
179
    }
180
    return packet;
181
}
182
 
3990 mejdrech 183
packet_t packet_get_1( int phone, size_t content ){
3901 mejdrech 184
    ERROR_DECLARE;
185
 
186
    packet_id_t packet_id;
187
    unsigned int    size;
188
    packet_t    packet;
189
 
4243 mejdrech 190
    if( ERROR_OCCURRED( async_req_1_2( phone, NET_PACKET_CREATE_1, content, ( ipcarg_t * ) & packet_id, & size ))
3912 mejdrech 191
    || ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){
3901 mejdrech 192
        return NULL;
193
    }
194
    return packet;
195
}
196
 
4192 mejdrech 197
void pq_release( int phone, packet_id_t packet_id ){
3901 mejdrech 198
    async_msg_1( phone, NET_PACKET_RELEASE, packet_id );
199
}
200
 
201
/** @}
202
 */