[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>
|
---|
[79ae36dd] | 39 | #include <async_obsolete.h>
|
---|
[514ee46] | 40 | #include <ipc/services.h>
|
---|
| 41 | #include <net/device.h>
|
---|
| 42 | #include <adt/measured_strings.h>
|
---|
| 43 | #include <net/packet.h>
|
---|
| 44 |
|
---|
| 45 | /** Notify the module about the device state change.
|
---|
| 46 | *
|
---|
| 47 | * @param[in] phone The service module phone.
|
---|
| 48 | * @param[in] message The service specific message.
|
---|
| 49 | * @param[in] device_id The device identifier.
|
---|
| 50 | * @param[in] state The new device state.
|
---|
| 51 | * @param[in] target The target module service.
|
---|
| 52 | * @return EOK on success.
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
| 55 | int
|
---|
| 56 | generic_device_state_msg_remote(int phone, int message, device_id_t device_id,
|
---|
| 57 | int state, services_t target)
|
---|
| 58 | {
|
---|
[79ae36dd] | 59 | async_obsolete_msg_3(phone, (sysarg_t) message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 60 | (sysarg_t) state, target);
|
---|
[514ee46] | 61 |
|
---|
| 62 | return EOK;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Notify a module about the device.
|
---|
| 66 | *
|
---|
| 67 | * @param[in] phone The service module phone.
|
---|
| 68 | * @param[in] message The service specific message.
|
---|
| 69 | * @param[in] device_id The device identifier.
|
---|
| 70 | * @param[in] arg2 The second argument of the message.
|
---|
| 71 | * @param[in] service The device module service.
|
---|
| 72 | * @return EOK on success.
|
---|
| 73 | * @return Other error codes as defined for the specific service
|
---|
| 74 | * message.
|
---|
| 75 | *
|
---|
| 76 | */
|
---|
| 77 | int
|
---|
| 78 | generic_device_req_remote(int phone, int message, device_id_t device_id,
|
---|
| 79 | int arg2, services_t service)
|
---|
| 80 | {
|
---|
[79ae36dd] | 81 | return (int) async_obsolete_req_3_0(phone, (sysarg_t) message,
|
---|
[96b02eb9] | 82 | (sysarg_t) device_id, (sysarg_t) arg2, (sysarg_t) service);
|
---|
[514ee46] | 83 | }
|
---|
| 84 |
|
---|
| 85 | /** Returns the address.
|
---|
| 86 | *
|
---|
| 87 | * @param[in] phone The service module phone.
|
---|
| 88 | * @param[in] message The service specific message.
|
---|
| 89 | * @param[in] device_id The device identifier.
|
---|
| 90 | * @param[out] address The desired address.
|
---|
| 91 | * @param[out] data The address data container.
|
---|
[1bfd3d3] | 92 | * @return EOK on success.
|
---|
| 93 | * @return EBADMEM if the address parameter and/or the data
|
---|
[514ee46] | 94 | * parameter is NULL.
|
---|
[1bfd3d3] | 95 | * @return Other error codes as defined for the specific service
|
---|
[514ee46] | 96 | * message.
|
---|
| 97 | */
|
---|
| 98 | int
|
---|
| 99 | generic_get_addr_req(int phone, int message, device_id_t device_id,
|
---|
[854151c] | 100 | measured_string_t **address, uint8_t **data)
|
---|
[514ee46] | 101 | {
|
---|
| 102 | aid_t message_id;
|
---|
[96b02eb9] | 103 | sysarg_t result;
|
---|
[514ee46] | 104 | int string;
|
---|
| 105 |
|
---|
| 106 | if (!address || !data)
|
---|
| 107 | return EBADMEM;
|
---|
| 108 |
|
---|
[28a3e74] | 109 | /* Request the address */
|
---|
[79ae36dd] | 110 | message_id = async_obsolete_send_1(phone, (sysarg_t) message,
|
---|
[96b02eb9] | 111 | (sysarg_t) device_id, NULL);
|
---|
[61bfc370] | 112 | string = measured_strings_return(phone, address, data, 1);
|
---|
[514ee46] | 113 | async_wait_for(message_id, &result);
|
---|
| 114 |
|
---|
[28a3e74] | 115 | /* If not successful */
|
---|
[514ee46] | 116 | if ((string == EOK) && (result != EOK)) {
|
---|
[28a3e74] | 117 | /* Clear the data */
|
---|
[514ee46] | 118 | free(*address);
|
---|
| 119 | free(*data);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | return (int) result;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** Return the device packet dimension for sending.
|
---|
| 126 | *
|
---|
| 127 | * @param[in] phone The service module phone.
|
---|
| 128 | * @param[in] message The service specific message.
|
---|
| 129 | * @param[in] device_id The device identifier.
|
---|
| 130 | * @param[out] packet_dimension The packet dimension.
|
---|
| 131 | * @return EOK on success.
|
---|
| 132 | * @return EBADMEM if the packet_dimension parameter is NULL.
|
---|
| 133 | * @return Other error codes as defined for the specific service
|
---|
| 134 | * message.
|
---|
| 135 | */
|
---|
| 136 | int
|
---|
| 137 | generic_packet_size_req_remote(int phone, int message, device_id_t device_id,
|
---|
[f772bc55] | 138 | packet_dimension_t *packet_dimension)
|
---|
[514ee46] | 139 | {
|
---|
| 140 | if (!packet_dimension)
|
---|
| 141 | return EBADMEM;
|
---|
| 142 |
|
---|
[96b02eb9] | 143 | sysarg_t addr_len;
|
---|
| 144 | sysarg_t prefix;
|
---|
| 145 | sysarg_t content;
|
---|
| 146 | sysarg_t suffix;
|
---|
[514ee46] | 147 |
|
---|
[79ae36dd] | 148 | sysarg_t result = async_obsolete_req_1_4(phone, (sysarg_t) message,
|
---|
[96b02eb9] | 149 | (sysarg_t) device_id, &addr_len, &prefix, &content, &suffix);
|
---|
[514ee46] | 150 |
|
---|
| 151 | packet_dimension->prefix = (size_t) prefix;
|
---|
| 152 | packet_dimension->content = (size_t) content;
|
---|
| 153 | packet_dimension->suffix = (size_t) suffix;
|
---|
| 154 | packet_dimension->addr_len = (size_t) addr_len;
|
---|
| 155 |
|
---|
| 156 | return (int) result;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /** Pass the packet queue to the module.
|
---|
| 160 | *
|
---|
| 161 | * @param[in] phone The service module phone.
|
---|
| 162 | * @param[in] message The service specific message.
|
---|
| 163 | * @param[in] device_id The device identifier.
|
---|
| 164 | * @param[in] packet_id The received packet or the received packet queue
|
---|
| 165 | * identifier.
|
---|
| 166 | * @param[in] target The target module service.
|
---|
| 167 | * @param[in] error The error module service.
|
---|
| 168 | * @return EOK on success.
|
---|
| 169 | */
|
---|
| 170 | int
|
---|
| 171 | generic_received_msg_remote(int phone, int message, device_id_t device_id,
|
---|
| 172 | packet_id_t packet_id, services_t target, services_t error)
|
---|
| 173 | {
|
---|
| 174 | if (error) {
|
---|
[79ae36dd] | 175 | async_obsolete_msg_4(phone, (sysarg_t) message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 176 | (sysarg_t) packet_id, (sysarg_t) target, (sysarg_t) error);
|
---|
[514ee46] | 177 | } else {
|
---|
[79ae36dd] | 178 | async_obsolete_msg_3(phone, (sysarg_t) message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 179 | (sysarg_t) packet_id, (sysarg_t) target);
|
---|
[514ee46] | 180 | }
|
---|
| 181 |
|
---|
| 182 | return EOK;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /** Send the packet queue.
|
---|
| 186 | *
|
---|
| 187 | * @param[in] phone The service module phone.
|
---|
| 188 | * @param[in] message The service specific message.
|
---|
| 189 | * @param[in] device_id The device identifier.
|
---|
| 190 | * @param[in] packet_id The packet or the packet queue identifier.
|
---|
| 191 | * @param[in] sender The sending module service.
|
---|
| 192 | * @param[in] error The error module service.
|
---|
| 193 | * @return EOK on success.
|
---|
| 194 | *
|
---|
| 195 | */
|
---|
| 196 | int
|
---|
| 197 | generic_send_msg_remote(int phone, int message, device_id_t device_id,
|
---|
| 198 | packet_id_t packet_id, services_t sender, services_t error)
|
---|
| 199 | {
|
---|
| 200 | if (error) {
|
---|
[79ae36dd] | 201 | async_obsolete_msg_4(phone, (sysarg_t) message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 202 | (sysarg_t) packet_id, (sysarg_t) sender, (sysarg_t) error);
|
---|
[514ee46] | 203 | } else {
|
---|
[79ae36dd] | 204 | async_obsolete_msg_3(phone, (sysarg_t) message, (sysarg_t) device_id,
|
---|
[96b02eb9] | 205 | (sysarg_t) packet_id, (sysarg_t) sender);
|
---|
[514ee46] | 206 | }
|
---|
| 207 |
|
---|
| 208 | return EOK;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** Translates the given strings.
|
---|
| 212 | *
|
---|
| 213 | * Allocates and returns the needed memory block as the data parameter.
|
---|
| 214 | *
|
---|
| 215 | * @param[in] phone The service module phone.
|
---|
| 216 | * @param[in] message The service specific message.
|
---|
| 217 | * @param[in] device_id The device identifier.
|
---|
| 218 | * @param[in] service The module service.
|
---|
| 219 | * @param[in] configuration The key strings.
|
---|
| 220 | * @param[in] count The number of configuration keys.
|
---|
| 221 | * @param[out] translation The translated values.
|
---|
| 222 | * @param[out] data The translation data container.
|
---|
[1bfd3d3] | 223 | * @return EOK on success.
|
---|
| 224 | * @return EINVAL if the configuration parameter is NULL.
|
---|
| 225 | * @return EINVAL if the count parameter is zero.
|
---|
| 226 | * @return EBADMEM if the translation or the data parameters are
|
---|
[514ee46] | 227 | * NULL.
|
---|
[1bfd3d3] | 228 | * @return Other error codes as defined for the specific service
|
---|
[514ee46] | 229 | * message.
|
---|
| 230 | */
|
---|
| 231 | int
|
---|
| 232 | generic_translate_req(int phone, int message, device_id_t device_id,
|
---|
[4eca056] | 233 | services_t service, measured_string_t *configuration, size_t count,
|
---|
[61bfc370] | 234 | measured_string_t **translation, uint8_t **data)
|
---|
[514ee46] | 235 | {
|
---|
| 236 | aid_t message_id;
|
---|
[96b02eb9] | 237 | sysarg_t result;
|
---|
[514ee46] | 238 | int string;
|
---|
| 239 |
|
---|
| 240 | if (!configuration || (count == 0))
|
---|
| 241 | return EINVAL;
|
---|
| 242 | if (!translation || !data)
|
---|
| 243 | return EBADMEM;
|
---|
| 244 |
|
---|
[28a3e74] | 245 | /* Request the translation */
|
---|
[79ae36dd] | 246 | message_id = async_obsolete_send_3(phone, (sysarg_t) message,
|
---|
[96b02eb9] | 247 | (sysarg_t) device_id, (sysarg_t) count, (sysarg_t) service, NULL);
|
---|
[514ee46] | 248 | measured_strings_send(phone, configuration, count);
|
---|
| 249 | string = measured_strings_return(phone, translation, data, count);
|
---|
| 250 | async_wait_for(message_id, &result);
|
---|
| 251 |
|
---|
[28a3e74] | 252 | /* If not successful */
|
---|
[514ee46] | 253 | if ((string == EOK) && (result != EOK)) {
|
---|
[28a3e74] | 254 | /* Clear the data */
|
---|
[514ee46] | 255 | free(*translation);
|
---|
| 256 | free(*data);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return (int) result;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /** @}
|
---|
| 263 | */
|
---|