Rev 3901 | Rev 3990 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3901 | Rev 3912 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
Line 24... | Line 24... | ||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup net |
29 | /** @addtogroup packet |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
- | 34 | * Packet client implementation. |
|
34 | */ |
35 | */ |
35 | 36 | ||
36 | #include <async.h> |
37 | #include <async.h> |
37 | #include <errno.h> |
38 | #include <errno.h> |
38 | #include <unistd.h> |
39 | #include <unistd.h> |
Line 48... | Line 49... | ||
48 | 49 | ||
49 | #include "packet.h" |
50 | #include "packet.h" |
50 | #include "packet_header.h" |
51 | #include "packet_header.h" |
51 | #include "packet_client.h" |
52 | #include "packet_client.h" |
52 | 53 | ||
- | 54 | /** Obtains the packet from the packet server as the shared memory block. |
|
- | 55 | * Creates the local packet mapping as well. |
|
- | 56 | * @param phone The packet server module phone. Input parameter. |
|
- | 57 | * @param packet The packet reference pointer to store the received packet reference. Output parameter. |
|
- | 58 | * @param packet_id The packet identifier. Input parameter. |
|
- | 59 | * @param size The packet total size in bytes. Input parameter. |
|
- | 60 | * @returns EOK on success. |
|
- | 61 | * \todo ipc_share_in_start() error? |
|
- | 62 | * @returns Other error codes as defined for the pm_add() function. |
|
- | 63 | */ |
|
53 | int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ); |
64 | int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ); |
54 | 65 | ||
55 | packet_t packet_copy( int phone, services_t owner, const packet_t packet ){ |
66 | packet_t packet_copy( int phone, services_t owner, const packet_t packet ){ |
56 | packet_t new; |
67 | packet_t new; |
57 | 68 | ||
Line 72... | Line 83... | ||
72 | packet->data_end = packet->data_start + length; |
83 | packet->data_end = packet->data_start + length; |
73 | } |
84 | } |
74 | return EOK; |
85 | return EOK; |
75 | } |
86 | } |
76 | 87 | ||
77 | void * packet_prepend( packet_t packet, size_t length ){ |
88 | void * packet_prefix( packet_t packet, size_t length ){ |
78 | if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL; |
89 | if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL; |
79 | packet->data_start -= length; |
90 | packet->data_start -= length; |
80 | return ( void * ) packet + packet->data_start; |
91 | return ( void * ) packet + packet->data_start; |
81 | } |
92 | } |
82 | 93 | ||
83 | void * packet_append( packet_t packet, size_t length ){ |
94 | void * packet_suffix( packet_t packet, size_t length ){ |
84 | if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL; |
95 | if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL; |
85 | packet->data_end += length; |
96 | packet->data_end += length; |
86 | return ( void * ) packet + packet->data_end - length; |
97 | return ( void * ) packet + packet->data_end - length; |
87 | } |
98 | } |
88 | 99 | ||
89 | int packet_trim( packet_t packet, size_t prefix, size_t sufix ){ |
100 | int packet_trim( packet_t packet, size_t prefix, size_t suffix ){ |
- | 101 | if( ! packet_is_valid( packet )) return EINVAL; |
|
90 | if(( ! packet_is_valid( packet )) || ( prefix + sufix > packet->data_end - packet->data_start )) return EINVAL; |
102 | if( prefix + suffix > packet->data_end - packet->data_start ) return ENOMEM; |
91 | packet->data_start += prefix; |
103 | packet->data_start += prefix; |
92 | packet->data_end -= sufix; |
104 | packet->data_end -= suffix; |
93 | return EOK; |
105 | return EOK; |
94 | } |
106 | } |
95 | 107 | ||
96 | packet_id_t packet_get_id( packet_t packet ){ |
108 | packet_id_t packet_get_id( const packet_t packet ){ |
97 | return packet_is_valid( packet ) ? packet->packet_id : 0; |
109 | return packet_is_valid( packet ) ? packet->packet_id : 0; |
98 | } |
110 | } |
99 | 111 | ||
100 | int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){ |
112 | int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){ |
101 | if( !( packet_is_valid( packet ) && src && dest )) return 0; |
113 | if( !( packet_is_valid( packet ) && src && dest )) return EINVAL; |
- | 114 | if( ! packet->addr_len ) return 0; |
|
102 | * src = ( void * ) packet + packet->src_addr; |
115 | * src = ( void * ) packet + packet->src_addr; |
103 | * dest = ( void * ) packet + packet->dest_addr; |
116 | * dest = ( void * ) packet + packet->dest_addr; |
104 | return packet->addr_len; |
117 | return packet->addr_len; |
105 | } |
118 | } |
106 | 119 | ||
Line 114... | Line 127... | ||
114 | return ( void * ) packet + packet->data_start; |
127 | return ( void * ) packet + packet->data_start; |
115 | } |
128 | } |
116 | 129 | ||
117 | packet_mode_t packet_get_mode( const packet_t packet ){ |
130 | packet_mode_t packet_get_mode( const packet_t packet ){ |
118 | if( packet_is_valid( packet )) return packet->mode; |
131 | if( packet_is_valid( packet )) return packet->mode; |
119 | return PM_ONEWAY; |
132 | return PM_ONE_WAY; |
120 | } |
133 | } |
121 | 134 | ||
122 | int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){ |
135 | int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){ |
123 | size_t padding; |
136 | size_t padding; |
124 | 137 | ||
125 | if( !( packet_is_valid( packet ) && ( packet->addr_len >= addr_len ))) return EINVAL; |
138 | if( ! packet_is_valid( packet )) return EINVAL; |
- | 139 | if( packet->addr_len >= addr_len ) return ENOMEM; |
|
126 | padding = packet->addr_len - addr_len; |
140 | padding = packet->addr_len - addr_len; |
127 | if( src ){ |
141 | if( src ){ |
128 | memcpy(( void * ) packet + packet->src_addr, src, addr_len ); |
142 | memcpy(( void * ) packet + packet->src_addr, src, addr_len ); |
129 | memset(( void * ) packet + packet->src_addr + addr_len, 0, padding ); |
143 | memset(( void * ) packet + packet->src_addr + addr_len, 0, padding ); |
130 | }else{ |
144 | }else{ |
Line 166... | Line 180... | ||
166 | int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ){ |
180 | int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ){ |
167 | ERROR_DECLARE; |
181 | ERROR_DECLARE; |
168 | 182 | ||
169 | aid_t message; |
183 | aid_t message; |
170 | ipc_call_t answer; |
184 | ipc_call_t answer; |
- | 185 | ipcarg_t result; |
|
171 | 186 | ||
172 | message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer ); |
187 | message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer ); |
173 | * packet = ( packet_t ) as_get_mappable_page( size ); |
188 | * packet = ( packet_t ) as_get_mappable_page( size ); |
174 | if( ERROR_OCCURED( ipc_share_in_start_0_0( phone, * packet, size )) |
189 | if( ERROR_OCCURRED( ipc_share_in_start_0_0( phone, * packet, size )) |
175 | || ERROR_OCCURED( pm_add( * packet ))){ |
190 | || ERROR_OCCURRED( pm_add( * packet ))){ |
176 | munmap( * packet, size ); |
191 | munmap( * packet, size ); |
177 | async_wait_for( message, NULL ); |
192 | async_wait_for( message, NULL ); |
178 | return ERROR_CODE; |
193 | return ERROR_CODE; |
179 | } |
194 | } |
180 | async_wait_for( message, NULL ); |
195 | async_wait_for( message, & result ); |
181 | return EOK; |
196 | return result; |
182 | } |
197 | } |
183 | 198 | ||
184 | 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_sufix ){ |
199 | 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 ){ |
185 | ERROR_DECLARE; |
200 | ERROR_DECLARE; |
186 | 201 | ||
187 | packet_id_t packet_id; |
202 | packet_id_t packet_id; |
188 | unsigned int size; |
203 | unsigned int size; |
189 | packet_t packet; |
204 | packet_t packet; |
190 | 205 | ||
191 | if( ERROR_OCCURED( async_req_5_2( phone, NET_PACKET_CREATE_5, owner, max_content, addr_len, max_prefix, max_sufix, & packet_id, & size )) |
206 | if( ERROR_OCCURRED( async_req_5_2( phone, NET_PACKET_CREATE_5, owner, max_content, addr_len, max_prefix, max_suffix, & packet_id, & size )) |
192 | || ERROR_OCCURED( packet_return( phone, & packet, packet_id, size ))){ |
207 | || ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){ |
193 | return NULL; |
208 | return NULL; |
194 | } |
209 | } |
195 | return packet; |
210 | return packet; |
196 | } |
211 | } |
197 | 212 | ||
Line 200... | Line 215... | ||
200 | 215 | ||
201 | packet_id_t packet_id; |
216 | packet_id_t packet_id; |
202 | unsigned int size; |
217 | unsigned int size; |
203 | packet_t packet; |
218 | packet_t packet; |
204 | 219 | ||
205 | if( ERROR_OCCURED( async_req_2_2( phone, NET_PACKET_CREATE_1, owner, content, & packet_id, & size )) |
220 | if( ERROR_OCCURRED( async_req_2_2( phone, NET_PACKET_CREATE_1, owner, content, & packet_id, & size )) |
206 | || ERROR_OCCURED( packet_return( phone, & packet, packet_id, size ))){ |
221 | || ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){ |
207 | return NULL; |
222 | return NULL; |
208 | } |
223 | } |
209 | return packet; |
224 | return packet; |
210 | } |
225 | } |
211 | 226 |