diff --git a/include/error_handling.h b/include/error_handling.h deleted file mode 100644 index c011fae831cd8c5da93eb17abbd5bc566ec2b2b6..0000000000000000000000000000000000000000 --- a/include/error_handling.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * This file just provides helpers to handle errors - */ - -#ifndef ERROR_HANDLING_H -#define ERROR_HANDLING_H - -#define SUCCESS 0 -#define E_NOT_FOUND 1 -#define E_INVALID_CPORT_ALLOC 2 -#define E_TABLE_FULL 3 -#define E_EMPTY 4 -#define E_NO_HEAP_MEM 5 -#define E_UNINITIALIZED_CPORT 6 -#define E_SEND_HEADER 7 -#define E_SEND_PAYLOAD 8 -#define E_NOT_RESPONSE 9 -#define E_CLIENT_REQUEST 10 - -#endif diff --git a/include/operations.h b/include/operations.h index 83013ddc0306eae234cac0f38362f4548fa4e739..16dc131b163f3d57ee25c646ca7b726c86788249 100644 --- a/include/operations.h +++ b/include/operations.h @@ -4,19 +4,14 @@ * This API is thread-safe */ -#ifndef OPERATIONS_H -#define OPERATIONS_H +#ifndef _OPERATIONS_H_ +#define _OPERATIONS_H_ #include "greybus_protocol.h" #include <stdbool.h> #include <zephyr/net/socket.h> #include <zephyr/sys/dlist.h> -/* Return codes for the functions defined here */ -#define SUCCESS 0 -#define E_NULL_REQUEST 1 -#define E_ALREADY_SENT 2 - struct gb_controller; typedef struct gb_message *(*gb_controller_read_callback_t)( @@ -131,17 +126,12 @@ static inline bool gb_message_is_success(const struct gb_message *msg) { return gb_hdr_is_success(&msg->header); } -/* - * Deallocate a greybus message. - * - * @param pointer to the message to deallcate - */ -void gb_message_dealloc(struct gb_message *); - /* * Send a greybus message over HDLC * * @param Greybus message + * + * TODO: Move to hdlc */ int gb_message_hdlc_send(const struct gb_message *); @@ -159,6 +149,22 @@ struct gb_connection *gb_create_connection(struct gb_interface *, struct gb_interface *, uint16_t, uint16_t); +/* + * Get greybus connections list. Not sure if this should live in this file + */ +sys_dlist_t *gb_connections_list_get(); + +/* + * Destroy greybus connection + * + * @param interface 1 + * @param interface 2 + * @param interface 1 cport + * @param interface 2 cport + */ +void gb_destroy_connection(struct gb_interface *, struct gb_interface *, + uint16_t, uint16_t); + /* * Allocate a greybus request message * @@ -185,17 +191,34 @@ struct gb_message *gb_message_request_alloc(const void *, size_t, uint8_t, struct gb_message *gb_message_response_alloc(const void *, size_t, uint8_t, uint16_t, uint8_t); +/* + * Deallocate a greybus message. + * + * @param pointer to the message to deallcate + */ +void gb_message_dealloc(struct gb_message *); + +/* + * Allocate a greybus interface + * + * @param read callback + * @param write callback + * @param create connection callback + * @param destroy connection callback + * + * @return allocated greybus interface. NULL in case of error + */ struct gb_interface *gb_interface_alloc(gb_controller_read_callback_t, gb_controller_write_callback_t, gb_controller_create_connection_t, gb_controller_destroy_connection_t, void *); +/* + * Deallocate a greybus interface + * + * @param greybus interface + */ void gb_interface_dealloc(struct gb_interface *); -sys_dlist_t *gb_connections_list_get(); - -void gb_destroy_connection(struct gb_interface *, struct gb_interface *, - uint16_t, uint16_t); - #endif diff --git a/src/operations.c b/src/operations.c index 8463811f34aad9d1e7c683ea3be3ced407aef113..384bc6d8c143b6fb17bba9114a68d2fc75dd8cae 100644 --- a/src/operations.c +++ b/src/operations.c @@ -1,5 +1,4 @@ #include "operations.h" -#include "error_handling.h" #include "greybus_protocol.h" #include "hdlc.h" #include "zephyr/kernel.h" @@ -20,6 +19,43 @@ static atomic_t interface_id_counter = ATOMIC_INIT(INTERFACE_ID_START); static sys_dlist_t gb_connections_list = SYS_DLIST_STATIC_INIT(&gb_connections_list); +static struct gb_connection *gb_connection_get(struct gb_interface *inf_ap, + struct gb_interface *inf_peer) { + struct gb_connection *conn; + + SYS_DLIST_FOR_EACH_CONTAINER(&gb_connections_list, conn, node) { + // While the names are inf_peer and inf_ap, they are just arbitrary. So do + // comparisons in reverse as well + if ((conn->inf_peer == inf_peer && conn->inf_ap == inf_ap) || + (conn->inf_peer == inf_ap && conn->inf_ap == inf_peer)) { + return conn; + } + } + + return NULL; +} + +static struct gb_message * +gb_message_alloc(const void *payload, size_t payload_len, uint8_t message_type, + uint16_t operation_id, uint8_t status) { + struct gb_message *msg; + + msg = k_malloc(sizeof(struct gb_message) + payload_len); + if (msg == NULL) { + LOG_WRN("Failed to allocate Greybus request message"); + return NULL; + } + + msg->header.size = sizeof(struct gb_operation_msg_hdr) + payload_len; + msg->header.id = operation_id; + msg->header.type = message_type; + msg->header.status = status; + msg->payload_size = payload_len; + memcpy(msg->payload, payload, msg->payload_size); + + return msg; +} + static uint16_t new_operation_id() { atomic_val_t temp = atomic_inc(&operation_id_counter); if (temp == UINT16_MAX) { @@ -36,13 +72,7 @@ static uint8_t new_interface_id() { return temp; } -void gb_message_dealloc(struct gb_message *msg) { - if (msg == NULL) { - return; - } - - k_free(msg); -} +void gb_message_dealloc(struct gb_message *msg) { k_free(msg); } int gb_message_hdlc_send(const struct gb_message *msg) { char buffer[HDLC_MAX_BLOCK_SIZE]; @@ -53,7 +83,7 @@ int gb_message_hdlc_send(const struct gb_message *msg) { hdlc_block_send_sync(buffer, msg->header.size, ADDRESS_GREYBUS, 0x03); - return SUCCESS; + return 0; } struct gb_connection *gb_create_connection(struct gb_interface *inf_ap, @@ -93,22 +123,6 @@ struct gb_connection *gb_create_connection(struct gb_interface *inf_ap, return conn; } -static struct gb_connection *gb_connection_get(struct gb_interface *inf_ap, - struct gb_interface *inf_peer) { - struct gb_connection *conn; - - SYS_DLIST_FOR_EACH_CONTAINER(&gb_connections_list, conn, node) { - // While the names are inf_peer and inf_ap, they are just arbitrary. So do - // comparisons in reverse as well - if ((conn->inf_peer == inf_peer && conn->inf_ap == inf_ap) || - (conn->inf_peer == inf_ap && conn->inf_ap == inf_peer)) { - return conn; - } - } - - return NULL; -} - void gb_destroy_connection(struct gb_interface *inf_ap, struct gb_interface *inf_peer, uint16_t ap_cport, uint16_t peer_cport) { @@ -126,27 +140,6 @@ void gb_destroy_connection(struct gb_interface *inf_ap, sys_dlist_t *gb_connections_list_get() { return &gb_connections_list; } -static struct gb_message * -gb_message_alloc(const void *payload, size_t payload_len, uint8_t message_type, - uint16_t operation_id, uint8_t status) { - struct gb_message *msg; - - msg = k_malloc(sizeof(struct gb_message) + payload_len); - if (msg == NULL) { - LOG_WRN("Failed to allocate Greybus request message"); - return NULL; - } - - msg->header.size = sizeof(struct gb_operation_msg_hdr) + payload_len; - msg->header.id = operation_id; - msg->header.type = message_type; - msg->header.status = status; - msg->payload_size = payload_len; - memcpy(msg->payload, payload, msg->payload_size); - - return msg; -} - struct gb_message *gb_message_request_alloc(const void *payload, size_t payload_len, uint8_t request_type, diff --git a/src/svc.c b/src/svc.c index 90c74d961adb45f51f56a8c8424e1cb59ff23eb1..f6dc46c72859588452a88f702c53c7528be32b08 100644 --- a/src/svc.c +++ b/src/svc.c @@ -161,7 +161,7 @@ static int control_send_request(void *payload, size_t payload_len, k_fifo_put(&svc_ctrl_data.pending_read, msg); - return SUCCESS; + return 0; } int svc_send_version() {