2 * @file frescan_bwres_messages.c
4 * @brief FRESCAN bandwidth reservation layer: negotiation messages formating
6 * This module contains the data types that define the FRESCAN negotiation
7 * message format and operations to convert them into or from negotiation
14 * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
19 #include "frescan_bwres_messages.h"
20 #include "frescan_config.h"
21 #include "frescan_debug.h"
22 #include "frescan_requests.h"
23 #include "frescan_servers.h"
30 * +-----------------------+
31 * | 'NEG' | ID | CONTRACT |
32 * +-----------------------+
36 * +----------------------------------------+
37 * | 'REPNEG' | ID | ACCEPTED | SC_CONTRACT |
38 * +----------------------------------------+
40 * RENEGOTIATE = NEG but with different type
44 * +--------------------------------+
45 * | 'CANCEL' | ID | CONTRACT_LABEL |
46 * +--------------------------------+
50 * +---------------------------+
51 * | 'REP_CANCEL' | ID | ERROR |
52 * +---------------------------+
54 * ID: the id of the request
59 FRESCAN_MSG_TYPE_NEG = 0,
60 FRESCAN_MSG_TYPE_RENEG = 1,
61 FRESCAN_MSG_TYPE_CANCEL = 2,
62 FRESCAN_MSG_TYPE_REP_NEG = 3,
63 FRESCAN_MSG_TYPE_REP_RENEG = 4,
64 FRESCAN_MSG_TYPE_REP_CANCEL = 5
67 int frescan_neg_message_create(uint8_t *msg,
68 frescan_request_id_t id,
69 const frescan_contract_t *contract)
71 size_t num, bytes_written;
74 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
75 "creating a negotiation request message\n");
77 *tmp = (uint8_t)FRESCAN_MSG_TYPE_NEG;
79 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
80 "type: %d (1 byte)\n", FRESCAN_MSG_TYPE_NEG);
83 memcpy(tmp, &id, num);
85 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
86 "request id: %d (2 bytes)\n", id);
88 num = sizeof(frescan_contract_t);
89 memcpy(tmp, contract, num);
92 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
93 "contract (%d bytes)\n", num);
95 bytes_written = tmp - msg;
97 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
98 "total bytes_written: %d\n", bytes_written);
100 return (int)bytes_written;
103 int frescan_repneg_message_create(uint8_t *msg,
104 frescan_request_id_t id,
106 frescan_server_params_t *params)
108 size_t num, bytes_written;
111 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
112 "creating a negotiation reply message\n");
114 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
115 "type: %d (1 byte)\n", FRESCAN_MSG_TYPE_REP_NEG);
116 *tmp = (uint8_t)FRESCAN_MSG_TYPE_REP_NEG;
119 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
120 "request id: %d (2 bytes)\n", id);
122 memcpy(tmp, &id, num);
125 *tmp = (uint8_t)accepted;
126 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG, "accepted:%u\n", *tmp);
129 num = sizeof(frescan_budget_period_t);
130 memcpy(tmp, ¶ms->values, num);
133 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
134 "budget_period (%d bytes)\n", num);
137 memcpy(tmp, ¶ms->prio, num);
140 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
141 "server prio:%u\n", params->prio);
143 bytes_written = tmp - msg;
145 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
146 "total bytes_written: %d\n", bytes_written);
148 return (int)bytes_written;
151 int frescan_reneg_message_create(uint8_t *msg,
152 frescan_request_id_t id,
153 const frescan_contract_t *contract)
155 size_t num, bytes_written;
158 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
159 "creating a renegotiation request message\n");
161 *tmp = (uint8_t)FRESCAN_MSG_TYPE_RENEG;
163 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
164 "type: %d (1 byte)\n", FRESCAN_MSG_TYPE_RENEG);
167 memcpy(tmp, &id, num);
169 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
170 "request id: %d (2 bytes)\n", id);
172 num = sizeof(frescan_contract_t);
173 memcpy(tmp, contract, num);
176 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
177 "contract (%d bytes)\n", num);
179 // TODO: ADD server id? or look up the label??
181 bytes_written = tmp - msg;
183 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
184 "total bytes_written: %d\n", bytes_written);
186 return (int)bytes_written;
189 int frescan_message_parse(frescan_network_t net,
195 frescan_msg_type_t type;
198 frescan_contract_t contract;
199 frescan_request_id_t request_id;
200 frescan_robj_id_t reply;
201 uint8_t reply_msg[200];
203 frescan_neg_return_info_t *neg_return_info;
205 frescan_send_params_t params;
206 frescan_server_params_t server_params;
209 params.channel = FRESCAN_NEG_CHANNEL;
210 params.flags = FRESCAN_SS | FRESCAN_ASYNC;
211 params.ss = the_networks[net].neg_messages_ss_id;
214 tmp = (uint8_t *)msg;
215 type = (frescan_msg_type_t)*msg;
218 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
219 "parsing message, type:%u, size:%u\n", type, size);
222 case FRESCAN_MSG_TYPE_NEG:
223 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
224 "FRESCAN_MSG_TYPE_NEG\n");
227 memcpy(&request_id, tmp, num);
229 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
230 "request id: %d (2 bytes)\n", request_id);
232 num = sizeof(frescan_contract_t);
233 memcpy(&contract, tmp, num);
236 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
237 "contract (%d bytes)\n", num);
239 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
240 "min_budget:%u max_period:(%u,%u)\n",
241 contract.min_values.budget,
242 contract.min_values.period.tv_sec,
243 contract.min_values.period.tv_nsec);
245 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
246 "max_budget:%u min_period:(%u,%u)\n",
247 contract.max_values.budget,
248 contract.max_values.period.tv_sec,
249 contract.max_values.period.tv_nsec);
251 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
252 "prio:%u\n", contract.prio);
254 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
255 "TODO: sched test and add contract to table\n");
257 // server_params.values.budget = contract.min_values.budget;
258 // server_params.values.period = contract.min_values.period;
259 // server_params.prio = contract.prio;
260 FRESCAN_ACQUIRE_LOCK(&the_networks[net].lock);
262 FRESCAN_RELEASE_LOCK(&the_networks[net].lock);
264 server_params.values.budget = 69;
265 server_params.values.period.tv_sec = 33;
266 server_params.values.period.tv_nsec = 666;
267 server_params.prio = 2;
269 DEBUG(FRESCAN_BWRES_ENABLE_DEBUG,
270 "accepted:%u send results, net:%u to:%u ss:%u\n",
271 accepted, params.net, params.to, params.ss);
273 bytes_to_send = frescan_repneg_message_create
279 DEBUG(FRESCAN_BWRES_ENABLE_DEBUG,
280 "bytes_to_send:%u\n", bytes_to_send);
282 if (bytes_to_send <= 0) {
283 ERROR("creating reply_msg to neg\n");
287 ret = frescan_send(¶ms, reply_msg, bytes_to_send);
289 ERROR("error while sending neg request to master\n");
294 case FRESCAN_MSG_TYPE_REP_NEG:
295 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
296 "FRESCAN_MSG_TYPE_REP_NEG\n");
299 memcpy(&request_id, tmp, num);
301 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
302 "request id: %d (2 bytes)\n", request_id);
304 ret = frescan_request_get_return_info
306 (void *)&neg_return_info);
308 ERROR("error getting final_values_ref\n");
312 accepted = (int)*tmp;
316 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
317 "contract accepted:%d\n", accepted);
318 neg_return_info->error = 0;
320 num = sizeof(frescan_budget_period_t);
321 memcpy(&server_params.values, tmp, num);
324 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
325 "final budget:%u period:(%u,%u)\n",
326 server_params.values.budget,
327 server_params.values.period.tv_sec,
328 server_params.values.period.tv_nsec);
331 memcpy(&server_params.prio, tmp, num);
334 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
335 "server prio:%u\n", server_params.prio);
337 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
338 "create sporadic server with results\n");
340 ret = frescan_servers_create(net,
342 &neg_return_info->id);
344 ERROR("error creating server\n");
348 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
349 "contract not accepted:%d\n", accepted);
350 neg_return_info->error = -1;
353 DEBUG(FRESCAN_NEG_MESSAGES_ENABLE_DEBUG,
354 "signal the request id with the results\n");
356 ret = frescan_request_get_reply(request_id, &reply);
358 ERROR("error getting reply object\n");
362 ret = frescan_replyobject_signal(reply);
364 ERROR("error while sending neg request to master\n");
370 ERROR("type not known %u\n", type);