diff --git a/quad/src/quad_app/callbacks.c b/quad/src/quad_app/callbacks.c index c092e20d699eb2368399f5647f2b6d38c62021a3..62f89be50c3abb9a686e6d0b80b601b432caba97 100644 --- a/quad/src/quad_app/callbacks.c +++ b/quad/src/quad_app/callbacks.c @@ -179,7 +179,7 @@ int cb_setparam(modular_structs_t *structs, metadata_t *meta, u8 *data, u16 leng int cb_getparam(modular_structs_t* structs, metadata_t *meta, u8 *data, u16 length) { // Check if the data length is correct - if (length != 8) {return -1;} + if (length != 4) {return -1;} u16 msg_id = meta->msg_id; // Get the controller ID, parameter ID @@ -306,18 +306,86 @@ int cb_getoutput(modular_structs_t* structs, metadata_t *meta, u8 *data, u16 le /* * Handles a request for the list of nodes in the graph * For N total nodes, returns data in the following format: - * |---------------------------------------------------------------------------| - * | data index || 0 - 2*N | 2 - 3 | 4 - 5 | 6 - 7 | - * |---------------------------------------------------------------------------| - * | parameter || Array of node | dest input ID | src node ID | src output ID | - * |---------------------------------------------------------------------------| - * | bytes || 2 | 2 | 2 | 2 | - * |---------------------------------------------------------------------------| + * The node IDs and type IDs are consecutive shorts + * The node names are null-separated + * |---------------------------------------------------------------| + * | data index || 0 - 2*N-1 | 2*N - 4*N-1 | 4*N - (< 4096) | + * |---------------------------------------------------------------| + * | parameter || Node IDs | Node type IDs | Node names | + * |---------------------------------------------------------------| + * | bytes || 2*N | 2*N | < 4096 | + * |---------------------------------------------------------------| */ int cb_getnodes(modular_structs_t* structs, metadata_t *meta, u8 *data, u16 length) { + const struct computation_graph* graph = structs->parameter_struct.graph; + if (graph->n_nodes >= 150) { + static char* error_msg = "Over 150 nodes. Not responding to cb_getnodes for fear of buffer overflow."; + send_data(&structs->hardware_struct.uart, DEBUG_ID, 0, + error_msg, sizeof(error_msg)); + return -1; + } + + // Number of bytes in node ID being sent. Currently short (16 bits) + size_t id_len = 2; + + char resp_buf[4096]; + int i; + // Currently ID is always index in array. + // computation_graph provides no method of accessing ID, since it is implicit + for (i = 0; i < graph->n_nodes; i++) { + pack_short(i, resp_buf + (id_len * i)); + } + + // Construct type IDs + size_t offset = id_len * graph->n_nodes; + for (i = 0; i < graph->n_nodes; i++) { + int type_id = graph->nodes[i].type->type_id; + pack_short(type_id, resp_buf + offset + (id_len * i)); + } + + // Construct list of node names + offset += id_len * graph->n_nodes; + for (i = 0; i < graph->n_nodes; i++) { + size_t remaining_size = sizeof(resp_buf) - offset; + const char* name = graph->nodes[i].name; + size_t name_len = strlen(name); + if (name_len + 1 <= remaining_size) { + memcpy(resp_buf + offset, name, name_len); + offset += name_len; + // Add null-terminator separator + resp_buf[offset] = 0; + offset += 1; + } + } + + send_data(&structs->hardware_struct.uart, RESPNODES_ID, meta->msg_id, resp_buf, offset); return 0; } +/* + * Handles adding a new node with a particular type and name + * Expects the uart buff to have data in the following format: + * |---------------------------------------------| + * | data index || 0 - 1 | 2 - ? | + * |---------------------------------------------| + * | parameter || type ID | New node name | + * |---------------------------------------------| + * | bytes || 2 | ? | + * |---------------------------------------------| + * + * Returns the new node ID in the following format: + * |-----------------------------| + * | data index || 0 - 1 | + * |-----------------------------| + * | parameter || node ID | + * |------------------------------ + * | bytes || 2 | + * |-----------------------------| + */ int cb_addnode(modular_structs_t* structs, metadata_t *meta, u8 *data, u16 length) { + if (length < 2) {return -1;} + // Size of name + size_t name_len = length - 2; + return 0; } \ No newline at end of file