Skip to content

Commit ca308d7

Browse files
committed
Update Resource Discovery to be cleaner and more maintainable
1 parent 33efe43 commit ca308d7

2 files changed

Lines changed: 134 additions & 51 deletions

File tree

bcmp/resource_discovery.c

Lines changed: 129 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,17 @@ static BmErr bcmp_process_resource_discovery_reply(BcmpProcessData data) {
196196
return err;
197197
}
198198

199-
static BmErr resource_discovery_request_cb(const ResourceInfo *req,
200-
ResourceInfo *rep,
201-
uint8_t port_num) {
202-
uint32_t id = req->resource_id;
203-
BmErr err = add_neighbor_resource(req->topic, req->length, &id, port_num);
199+
/*!
200+
@brief Determine if resource info is valid
204201
205-
if (err == BmOK) {
206-
rep->resource_id = id;
207-
}
202+
@details Compares info struct to the size reported from the data packet.
208203
209-
return err;
210-
}
204+
@param info resource info received
205+
@param size sizeo of data packet
211206
207+
@return true if packet size matches expectations
208+
false otherwise
209+
*/
212210
static bool resource_info_valid(const ResourceInfo *info, uint32_t size) {
213211
// Bounds checking on the message
214212
if (size < sizeof(ResourceInfo) || info->length >= BM_TOPIC_MAX_LEN ||
@@ -219,6 +217,19 @@ static bool resource_info_valid(const ResourceInfo *info, uint32_t size) {
219217
return true;
220218
}
221219

220+
/*!
221+
@brief Handle a resource request message
222+
223+
@details Adds the resource info to the routing table.
224+
225+
@param payload incoming payload from replying device
226+
227+
@return BmOK on success
228+
BmENODATA if full packet is not able to be retrieved from packet
229+
module
230+
BmEBADMSG if the packet information is not valid
231+
BmErr on failure to add resource
232+
*/
222233
static BmErr resource_discovery_reply_cb(uint8_t *payload) {
223234
ResourceInfo *rep = (ResourceInfo *)payload;
224235
if (!rep) {
@@ -240,56 +251,96 @@ static BmErr resource_discovery_reply_cb(uint8_t *payload) {
240251
}
241252

242253
/*!
243-
@brief Process an incoming resource request
254+
@brief Adds to resource table and updates the resource ID to locally assigned
244255
245-
@details Add requested resource to resource table. Also sends out requests to
246-
other ports to propogate the resource of interest onto the network.
256+
@details This is meant to add the resource for routing purposes and then
257+
update the resource's ID to be used with propogated requests on other
258+
ports and as a reply to the original requestor to inform those nodes
259+
of the ID assigned to this node's resource.
247260
248-
@param data
261+
@param info resource information to save and update the resource ID on
262+
@param port_num ingress port number the resource was shared on
249263
250264
@return BmOK on success
251265
BmErr on failure
252266
*/
253-
static BmErr bcmp_process_resource_request(BcmpProcessData data) {
254-
ResourceInfo *req = (ResourceInfo *)data.payload;
255-
if (!resource_info_valid(req, data.size)) {
256-
return BmEBADMSG;
257-
}
267+
static BmErr add_resource_update_id(ResourceInfo *info, uint8_t port_num) {
268+
uint32_t id = info->resource_id;
269+
BmErr err = add_neighbor_resource(info->topic, info->length, &id, port_num);
258270

259-
uint16_t info_size = data.size;
260-
ResourceInfo *rep = (ResourceInfo *)bm_malloc(info_size);
261-
if (!rep) {
262-
return BmENOMEM;
271+
if (err == BmOK) {
272+
info->resource_id = id;
263273
}
264274

265-
// Copy over data to reply and let callback manipulate fields in reply
266-
memcpy(rep, req, info_size);
267-
uint8_t port_num = data.ingress_port;
268-
BmErr err = BmOK;
269-
bm_err_check(err, resource_discovery_request_cb(req, rep, port_num));
275+
return err;
276+
}
270277

271-
// Reply with the information needed from the requestor
278+
/*!
279+
@brief Reply to resource request
280+
281+
@details Prepares reply message to send to requesting device.
282+
add_resource_update_id must be invoked before this function.
283+
284+
@param data data from request with updated resource ID
285+
286+
@return BmOK on success
287+
BmErr on failure
288+
*/
289+
static BmErr resource_reply(BcmpProcessData data) {
290+
uint8_t *rep = data.payload;
291+
uint16_t rep_size = data.size;
272292
uint32_t seq_num = data.header->seq_num;
293+
uint8_t port_num = data.ingress_port;
294+
273295
BcmpTxCtx ctx = {
274-
data.dst, BcmpResourceReplyMessage,
275-
(uint8_t *)rep, info_size,
276-
seq_num, NULL,
277-
port_num,
296+
.dst = data.dst,
297+
.type = BcmpResourceReplyMessage,
298+
.data = rep,
299+
.size = rep_size,
300+
.seq_num = seq_num,
301+
.reply_cb = NULL,
302+
.egress_port = port_num,
278303
};
279-
bm_err_check(err, bcmp_tx_port(ctx));
280-
if (err != BmOK) {
281-
bm_free(rep);
282-
return err;
283-
}
284304

285-
// Propogate resource information down the network
305+
BmErr err;
306+
bm_err_report(err, bcmp_tx_port(ctx));
307+
308+
return err;
309+
}
310+
311+
/*!
312+
@brief Propogate resource request to other nodes on network
313+
314+
@details Will request to send information info to all other online ports on
315+
the network. add_resource_update_id must be invoked before this
316+
function.
317+
318+
@param data data from request with updated resource ID
319+
320+
@return BmOK on success
321+
BmEBADMSG if the packet information is not valid
322+
BmErr on failure
323+
*/
324+
static BmErr resource_propogate(BcmpProcessData data) {
325+
uint8_t *rep = data.payload;
326+
uint16_t rep_size = data.size;
327+
uint8_t ingress_port = data.ingress_port;
328+
286329
uint8_t num_ports = bm_l2_get_port_count();
287-
ctx.type = BcmpResourceRequestMessage;
288-
ctx.seq_num = 0;
289-
ctx.egress_port = 1;
290-
ctx.reply_cb = resource_discovery_reply_cb;
291-
for (ctx.egress_port = 1; ctx.egress_port <= num_ports; ctx.egress_port++) {
292-
if (ctx.egress_port == port_num) {
330+
BcmpTxCtx ctx = {
331+
.dst = data.dst,
332+
.type = BcmpResourceRequestMessage,
333+
.data = rep,
334+
.size = rep_size,
335+
.seq_num = 0,
336+
.reply_cb = resource_discovery_reply_cb,
337+
.egress_port = 1,
338+
};
339+
340+
// Send request to all ports besides ingress port and offline ports
341+
for (; ctx.egress_port <= num_ports; ctx.egress_port++) {
342+
if (ctx.egress_port == ingress_port ||
343+
!bm_l2_get_port_state(ctx.egress_port - 1)) {
293344
continue;
294345
}
295346
BmErr tx_err = bcmp_tx_port(ctx);
@@ -298,11 +349,42 @@ static BmErr bcmp_process_resource_request(BcmpProcessData data) {
298349
ctx.egress_port, tx_err);
299350
};
300351
}
301-
bm_free(rep);
302352

303353
return BmOK;
304354
}
305355

356+
/*!
357+
@brief Process an incoming resource request
358+
359+
@details Add requested resource to resource table. Also sends out requests to
360+
other ports to propogate the resource of interest onto the network.
361+
362+
@param data
363+
364+
@return BmOK on success
365+
366+
BmErr on failure
367+
*/
368+
static BmErr bcmp_process_resource_request(BcmpProcessData data) {
369+
ResourceInfo *req = (ResourceInfo *)data.payload;
370+
if (!resource_info_valid(req, data.size)) {
371+
return BmEBADMSG;
372+
}
373+
374+
// Copy over data to reply and let callback manipulate fields in reply
375+
uint8_t port_num = data.ingress_port;
376+
BmErr err;
377+
bm_err_report(err, add_resource_update_id(req, port_num));
378+
379+
// Reply with the information needed from the requestor
380+
bm_err_check(err, resource_reply(data));
381+
382+
// Propogate resource information down the network
383+
bm_err_check(err, resource_propogate(data));
384+
385+
return err;
386+
}
387+
306388
/*!
307389
@brief Init the bcmp resource discovery module.
308390
*/
@@ -340,6 +422,7 @@ BmErr bcmp_resource_discovery_init(void) {
340422
if (PUB_LIST.lock && SUB_LIST.lock) {
341423
err = BmOK;
342424
}
425+
bm_err_check(err, routing_init());
343426
bm_err_check(err,
344427
packet_add(&resource_request, BcmpResourceTableRequestMessage));
345428
bm_err_check(err, packet_add(&resource_reply, BcmpResourceTableReplyMessage));

middleware/pubsub.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ static void resource_based_routing_tx_cb(BmIpAddr *src, void *arg) {
149149
BmErr on failure
150150
*/
151151
BmErr bm_pubsub_init(void) {
152-
BmErr err = routing_init();
153-
bm_err_check(err,
154-
bm_middleware_add_application(
155-
resource_port, link_local_resource_addr, bm_handle_msg,
156-
resource_based_routing_tx_cb, resource_based_routing_cb));
152+
BmErr err;
153+
bm_err_report(err,
154+
bm_middleware_add_application(
155+
resource_port, link_local_resource_addr, bm_handle_msg,
156+
resource_based_routing_tx_cb, resource_based_routing_cb));
157157
return err;
158158
}
159159

0 commit comments

Comments
 (0)