[514ee46] | 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 |
|
---|
[b01f878] | 29 | /** @addtogroup libnet
|
---|
[514ee46] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Generic communication interfaces for networking.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <generic.h>
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
| 40 | #include <net/device.h>
|
---|
| 41 | #include <adt/measured_strings.h>
|
---|
| 42 | #include <net/packet.h>
|
---|
| 43 |
|
---|
| 44 | /** Notify the module about the device state change.
|
---|
| 45 | *
|
---|
[6b82009] | 46 | * @param[in] sess Service module session.
|
---|
| 47 | * @param[in] message Service specific message.
|
---|
| 48 | * @param[in] device_id Device identifier.
|
---|
| 49 | * @param[in] state New device state.
|
---|
| 50 | * @param[in] target Target module service.
|
---|
| 51 | *
|
---|
| 52 | * @return EOK on success.
|
---|
[514ee46] | 53 | *
|
---|
| 54 | */
|
---|
[6b82009] | 55 | int generic_device_state_msg_remote(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 56 | nic_device_id_t device_id, sysarg_t state, services_t target)
|
---|
[514ee46] | 57 | {
|
---|
[6b82009] | 58 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 59 | async_msg_3(exch, message, (sysarg_t) device_id, state, target);
|
---|
| 60 | async_exchange_end(exch);
|
---|
[514ee46] | 61 |
|
---|
| 62 | return EOK;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Notify a module about the device.
|
---|
| 66 | *
|
---|
[6b82009] | 67 | * @param[in] sess Service module session.
|
---|
| 68 | * @param[in] message Service specific message.
|
---|
| 69 | * @param[in] device_id Device identifier.
|
---|
| 70 | * @param[in] service Device module service.
|
---|
| 71 | *
|
---|
| 72 | * @return EOK on success.
|
---|
| 73 | * @return Other error codes as defined for the specific service
|
---|
| 74 | * message.
|
---|
[514ee46] | 75 | *
|
---|
| 76 | */
|
---|
[6b82009] | 77 | int generic_device_req_remote(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 78 | nic_device_id_t device_id, services_t service)
|
---|
[514ee46] | 79 | {
|
---|
[6b82009] | 80 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[609243f4] | 81 | int rc = async_req_2_0(exch, message, (sysarg_t) device_id,
|
---|
| 82 | (sysarg_t) service);
|
---|
[6b82009] | 83 | async_exchange_end(exch);
|
---|
| 84 |
|
---|
| 85 | return rc;
|
---|
[514ee46] | 86 | }
|
---|
| 87 |
|
---|
| 88 | /** Returns the address.
|
---|
| 89 | *
|
---|
[6b82009] | 90 | * @param[in] sess Service module session.
|
---|
| 91 | * @param[in] message Service specific message.
|
---|
| 92 | * @param[in] device_id Device identifier.
|
---|
| 93 | * @param[out] address Desired address.
|
---|
| 94 | * @param[out] data Address data container.
|
---|
| 95 | *
|
---|
| 96 | * @return EOK on success.
|
---|
| 97 | * @return EBADMEM if the address parameter and/or the data
|
---|
| 98 | * parameter is NULL.
|
---|
| 99 | * @return Other error codes as defined for the specific service
|
---|
| 100 | * message.
|
---|
| 101 | *
|
---|
[514ee46] | 102 | */
|
---|
[6b82009] | 103 | int generic_get_addr_req(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 104 | nic_device_id_t device_id, uint8_t *address, size_t max_len)
|
---|
[514ee46] | 105 | {
|
---|
[609243f4] | 106 | if (!address)
|
---|
[514ee46] | 107 | return EBADMEM;
|
---|
[6b82009] | 108 |
|
---|
[28a3e74] | 109 | /* Request the address */
|
---|
[6b82009] | 110 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[609243f4] | 111 | aid_t aid = async_send_1(exch, message, (sysarg_t) device_id,
|
---|
[6b82009] | 112 | NULL);
|
---|
[609243f4] | 113 | int rc = async_data_read_start(exch, address, max_len);
|
---|
[6b82009] | 114 | async_exchange_end(exch);
|
---|
| 115 |
|
---|
| 116 | sysarg_t result;
|
---|
[609243f4] | 117 | async_wait_for(aid, &result);
|
---|
[6b82009] | 118 |
|
---|
[609243f4] | 119 | if (rc != EOK)
|
---|
| 120 | return rc;
|
---|
[514ee46] | 121 |
|
---|
| 122 | return (int) result;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** Return the device packet dimension for sending.
|
---|
| 126 | *
|
---|
[6b82009] | 127 | * @param[in] sess Service module session.
|
---|
| 128 | * @param[in] message Service specific message.
|
---|
| 129 | * @param[in] device_id Device identifier.
|
---|
| 130 | * @param[out] packet_dimension Packet dimension.
|
---|
| 131 | *
|
---|
| 132 | * @return EOK on success.
|
---|
| 133 | * @return EBADMEM if the packet_dimension parameter is NULL.
|
---|
| 134 | * @return Other error codes as defined for the specific service
|
---|
| 135 | * message.
|
---|
| 136 | *
|
---|
[514ee46] | 137 | */
|
---|
[6b82009] | 138 | int generic_packet_size_req_remote(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 139 | nic_device_id_t device_id, packet_dimension_t *packet_dimension)
|
---|
[514ee46] | 140 | {
|
---|
| 141 | if (!packet_dimension)
|
---|
| 142 | return EBADMEM;
|
---|
| 143 |
|
---|
[96b02eb9] | 144 | sysarg_t addr_len;
|
---|
| 145 | sysarg_t prefix;
|
---|
| 146 | sysarg_t content;
|
---|
| 147 | sysarg_t suffix;
|
---|
[514ee46] | 148 |
|
---|
[6b82009] | 149 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 150 | sysarg_t result = async_req_1_4(exch, message, (sysarg_t) device_id,
|
---|
| 151 | &addr_len, &prefix, &content, &suffix);
|
---|
| 152 | async_exchange_end(exch);
|
---|
[514ee46] | 153 |
|
---|
| 154 | packet_dimension->prefix = (size_t) prefix;
|
---|
| 155 | packet_dimension->content = (size_t) content;
|
---|
| 156 | packet_dimension->suffix = (size_t) suffix;
|
---|
| 157 | packet_dimension->addr_len = (size_t) addr_len;
|
---|
| 158 |
|
---|
| 159 | return (int) result;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /** Pass the packet queue to the module.
|
---|
| 163 | *
|
---|
[6b82009] | 164 | * @param[in] sess Service module session.
|
---|
| 165 | * @param[in] message Service specific message.
|
---|
| 166 | * @param[in] device_id Device identifier.
|
---|
| 167 | * @param[in] packet_id Received packet or the received packet queue
|
---|
| 168 | * identifier.
|
---|
| 169 | * @param[in] target Target module service.
|
---|
| 170 | * @param[in] error Error module service.
|
---|
| 171 | *
|
---|
| 172 | * @return EOK on success.
|
---|
| 173 | *
|
---|
[514ee46] | 174 | */
|
---|
[6b82009] | 175 | int generic_received_msg_remote(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 176 | nic_device_id_t device_id, packet_id_t packet_id, services_t target,
|
---|
[6b82009] | 177 | services_t error)
|
---|
[514ee46] | 178 | {
|
---|
[6b82009] | 179 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 180 |
|
---|
[514ee46] | 181 | if (error) {
|
---|
[6b82009] | 182 | async_msg_4(exch, message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 183 | (sysarg_t) packet_id, (sysarg_t) target, (sysarg_t) error);
|
---|
[514ee46] | 184 | } else {
|
---|
[6b82009] | 185 | async_msg_3(exch, message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 186 | (sysarg_t) packet_id, (sysarg_t) target);
|
---|
[514ee46] | 187 | }
|
---|
| 188 |
|
---|
[6b82009] | 189 | async_exchange_end(exch);
|
---|
| 190 |
|
---|
[514ee46] | 191 | return EOK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /** Send the packet queue.
|
---|
| 195 | *
|
---|
[6b82009] | 196 | * @param[in] sess Service module session.
|
---|
| 197 | * @param[in] message Service specific message.
|
---|
| 198 | * @param[in] device_id Device identifier.
|
---|
| 199 | * @param[in] packet_id Packet or the packet queue identifier.
|
---|
| 200 | * @param[in] sender Sending module service.
|
---|
| 201 | * @param[in] error Error module service.
|
---|
| 202 | *
|
---|
| 203 | * @return EOK on success.
|
---|
[514ee46] | 204 | *
|
---|
| 205 | */
|
---|
[6b82009] | 206 | int generic_send_msg_remote(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 207 | nic_device_id_t device_id, packet_id_t packet_id, services_t sender,
|
---|
[6b82009] | 208 | services_t error)
|
---|
[514ee46] | 209 | {
|
---|
[6b82009] | 210 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 211 |
|
---|
[514ee46] | 212 | if (error) {
|
---|
[6b82009] | 213 | async_msg_4(exch, message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 214 | (sysarg_t) packet_id, (sysarg_t) sender, (sysarg_t) error);
|
---|
[514ee46] | 215 | } else {
|
---|
[6b82009] | 216 | async_msg_3(exch, message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 217 | (sysarg_t) packet_id, (sysarg_t) sender);
|
---|
[514ee46] | 218 | }
|
---|
| 219 |
|
---|
[6b82009] | 220 | async_exchange_end(exch);
|
---|
| 221 |
|
---|
[514ee46] | 222 | return EOK;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /** Translates the given strings.
|
---|
| 226 | *
|
---|
| 227 | * Allocates and returns the needed memory block as the data parameter.
|
---|
| 228 | *
|
---|
[6b82009] | 229 | * @param[in] sess Service module session.
|
---|
| 230 | * @param[in] message Service specific message.
|
---|
| 231 | * @param[in] device_id Device identifier.
|
---|
| 232 | * @param[in] service Module service.
|
---|
| 233 | * @param[in] configuration Key strings.
|
---|
| 234 | * @param[in] count Number of configuration keys.
|
---|
| 235 | * @param[out] translation Translated values.
|
---|
| 236 | * @param[out] data Translation data container.
|
---|
| 237 | *
|
---|
| 238 | * @return EOK on success.
|
---|
| 239 | * @return EINVAL if the configuration parameter is NULL.
|
---|
| 240 | * @return EINVAL if the count parameter is zero.
|
---|
| 241 | * @return EBADMEM if the translation or the data parameters are
|
---|
| 242 | * NULL.
|
---|
| 243 | * @return Other error codes as defined for the specific service
|
---|
| 244 | * message.
|
---|
| 245 | *
|
---|
[514ee46] | 246 | */
|
---|
[6b82009] | 247 | int generic_translate_req(async_sess_t *sess, sysarg_t message,
|
---|
[609243f4] | 248 | nic_device_id_t device_id, services_t service,
|
---|
[6b82009] | 249 | measured_string_t *configuration, size_t count,
|
---|
[61bfc370] | 250 | measured_string_t **translation, uint8_t **data)
|
---|
[514ee46] | 251 | {
|
---|
[6b82009] | 252 | if ((!configuration) || (count == 0))
|
---|
[514ee46] | 253 | return EINVAL;
|
---|
[6b82009] | 254 |
|
---|
| 255 | if ((!translation) || (!data))
|
---|
[514ee46] | 256 | return EBADMEM;
|
---|
[6b82009] | 257 |
|
---|
[28a3e74] | 258 | /* Request the translation */
|
---|
[6b82009] | 259 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 260 | aid_t message_id = async_send_3(exch, message, (sysarg_t) device_id,
|
---|
| 261 | (sysarg_t) count, (sysarg_t) service, NULL);
|
---|
| 262 | measured_strings_send(exch, configuration, count);
|
---|
| 263 | int rc = measured_strings_return(exch, translation, data, count);
|
---|
| 264 | async_exchange_end(exch);
|
---|
| 265 |
|
---|
| 266 | sysarg_t result;
|
---|
[514ee46] | 267 | async_wait_for(message_id, &result);
|
---|
[6b82009] | 268 |
|
---|
[28a3e74] | 269 | /* If not successful */
|
---|
[6b82009] | 270 | if ((rc == EOK) && (result != EOK)) {
|
---|
[28a3e74] | 271 | /* Clear the data */
|
---|
[514ee46] | 272 | free(*translation);
|
---|
| 273 | free(*data);
|
---|
| 274 | }
|
---|
[6b82009] | 275 |
|
---|
[514ee46] | 276 | return (int) result;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /** @}
|
---|
| 280 | */
|
---|