Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Verified Commit 4e5ffd8c authored by Ayush Singh's avatar Ayush Singh
Browse files

Refactor SVC


Signed-off-by: default avatarAyush Singh <ayushsingh1325@gmail.com>
parent 0edf57c4
Branches
1 merge request!2HDLC MR
......@@ -221,4 +221,13 @@ struct gb_interface *gb_interface_alloc(gb_controller_read_callback_t,
*/
void gb_interface_dealloc(struct gb_interface *);
/*
* Get interface associated with interface id.
*
* @param interface id
*
* @param greybus interface
*/
struct gb_interface *find_interface_by_id(uint8_t);
#endif
......@@ -6,13 +6,16 @@
#define SVC_INF_ID 0
/*
* Create CONTROL_TYPE_PING greybus message and queue it for sending.
*
* Note: This does not immediately send the request.
*
* @return 0 if successful, else error.
* Initialize SVC Interface. Should be called before sending any greybus
* request.
*/
int svc_send_ping();
struct gb_interface *svc_init();
/*
* Check if SVC is ready. This mostly means if SVC Hello was successfuly
* executed.
*/
bool svc_is_ready();
/*
* Create SVC_TYPE_VERSION greybus message and queue it for sending.
......@@ -22,18 +25,19 @@ int svc_send_ping();
int svc_send_version();
/*
* Initialize SVC Interface. Should be called before sending any greybus
* request.
* Send the SVC module inserted event.
*
* @param interface id of the new module
*
* @return 0 if successfuly, negative in case of error
*/
struct gb_interface *svc_init();
int svc_send_module_inserted(uint8_t);
/*
* Check if SVC is ready. This mostly means if SVC Hello was successfuly
* executed.
* Get the SVC interface
*
* @return pointer to svc interface
*/
bool svc_is_ready();
int svc_send_module_inserted(uint8_t);
struct gb_interface *svc_interface();
#endif
#include "operations.h"
#include "ap.h"
#include "greybus_protocol.h"
#include "hdlc.h"
#include "node.h"
#include "svc.h"
#include "zephyr/kernel.h"
#include <errno.h>
#include <limits.h>
......@@ -180,3 +183,16 @@ gb_interface_alloc(gb_controller_read_callback_t read_cb,
}
void gb_interface_dealloc(struct gb_interface *intf) { k_free(intf); }
struct gb_interface *find_interface_by_id(uint8_t intf_id) {
switch (intf_id) {
case SVC_INF_ID:
return svc_interface();
case AP_INF_ID:
return ap_intf();
default:
return node_find_by_id(intf_id);
}
}
......@@ -164,18 +164,7 @@ static int control_send_request(void *payload, size_t payload_len,
return 0;
}
int svc_send_version() {
struct gb_svc_version_request req = {.major = GB_SVC_VERSION_MAJOR,
.minor = GB_SVC_VERSION_MINOR};
return control_send_request(&req, sizeof(struct gb_svc_version_request),
GB_SVC_TYPE_PROTOCOL_VERSION_REQUEST);
}
int svc_send_ping() {
return control_send_request(NULL, 0, GB_SVC_TYPE_PING_REQUEST);
}
int svc_send_hello() {
static int svc_send_hello() {
struct gb_svc_hello_request req = {.endo_id = ENDO_ID,
.interface_id = AP_INF_ID};
return control_send_request(&req, sizeof(struct gb_svc_hello_request),
......@@ -187,7 +176,7 @@ static void svc_response_helper(struct gb_message *msg, const void *payload,
struct gb_message *resp = gb_message_response_alloc(
payload, payload_len, msg->header.type, msg->header.id, status);
if (resp == NULL) {
LOG_DBG("Failed to allocate response for %X", msg->header.type);
LOG_ERR("Failed to allocate response for %X", msg->header.type);
return;
}
k_fifo_put(&svc_ctrl_data.pending_read, resp);
......@@ -277,25 +266,14 @@ static void svc_dme_peer_set_handler(struct gb_message *msg) {
GB_SVC_OP_SUCCESS);
}
static struct gb_interface *get_interface(uint8_t intf_id) {
switch (intf_id) {
case SVC_INF_ID:
return &intf;
case AP_INF_ID:
return ap_intf();
default:
return node_find_by_id(intf_id);
}
}
static void svc_connection_create_handler(struct gb_message *msg) {
struct gb_svc_conn_create_request *req =
(struct gb_svc_conn_create_request *)msg->payload;
struct gb_interface *intf_1, *intf_2;
struct gb_connection *conn;
intf_1 = get_interface(req->intf1_id);
intf_2 = get_interface(req->intf2_id);
intf_1 = find_interface_by_id(req->intf1_id);
intf_2 = find_interface_by_id(req->intf2_id);
conn = gb_create_connection(intf_1, intf_2, req->cport1_id, req->cport2_id);
if (conn == NULL) {
......@@ -310,8 +288,8 @@ static void svc_connection_destroy_handler(struct gb_message *msg) {
(struct gb_svc_conn_destroy_request *)msg->payload;
struct gb_interface *intf_1, *intf_2;
intf_1 = get_interface(req->intf1_id);
intf_2 = get_interface(req->intf2_id);
intf_1 = find_interface_by_id(req->intf1_id);
intf_2 = find_interface_by_id(req->intf2_id);
gb_destroy_connection(intf_1, intf_2, req->cport1_id, req->cport2_id);
}
......@@ -373,9 +351,6 @@ static void gb_handle_msg(struct gb_message *msg) {
case GB_SVC_TYPE_PROTOCOL_VERSION_RESPONSE:
svc_version_response_handler(msg);
break;
case GB_SVC_TYPE_PING_RESPONSE:
LOG_DBG("Received Pong");
break;
case GB_SVC_TYPE_HELLO_RESPONSE:
svc_hello_response_handler(msg);
break;
......@@ -402,6 +377,22 @@ static int svc_inf_write(struct gb_controller *ctrl, struct gb_message *msg,
return 0;
}
int svc_send_module_inserted(uint8_t primary_intf_id) {
struct gb_svc_module_inserted_request req = {
.primary_intf_id = primary_intf_id, .intf_count = 1, .flags = 0};
return control_send_request(&req,
sizeof(struct gb_svc_module_inserted_request),
GB_SVC_TYPE_MODULE_INSERTED_REQUEST);
}
int svc_send_version() {
struct gb_svc_version_request req = {.major = GB_SVC_VERSION_MAJOR,
.minor = GB_SVC_VERSION_MINOR};
return control_send_request(&req, sizeof(struct gb_svc_version_request),
GB_SVC_TYPE_PROTOCOL_VERSION_REQUEST);
}
struct gb_interface *svc_init() {
k_fifo_init(&svc_ctrl_data.pending_read);
return &intf;
......@@ -409,11 +400,10 @@ struct gb_interface *svc_init() {
bool svc_is_ready() { return atomic_test_bit(svc_is_read_flag, 0); }
int svc_send_module_inserted(uint8_t primary_intf_id) {
struct gb_svc_module_inserted_request req = {
.primary_intf_id = primary_intf_id, .intf_count = 1, .flags = 0};
struct gb_interface *svc_interface() {
if (svc_is_ready()) {
return &intf;
}
return control_send_request(&req,
sizeof(struct gb_svc_module_inserted_request),
GB_SVC_TYPE_MODULE_INSERTED_REQUEST);
return NULL;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment