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