Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit 0113ba37 authored by Ayush Singh's avatar Ayush Singh
Browse files

Use a single array in apbridge


- The 2nd array was kind of unnecessary

Signed-off-by: default avatarAyush Singh <ayushdevel1325@gmail.com>
parent 14c92049
Branches
1 merge request!4WIP: Make cc1352 greybus node
Pipeline #9268 passed with stage
in 1 minute and 19 seconds
......@@ -7,90 +7,53 @@
#include "apbridge.h"
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include "hdlc.h"
#include "greybus_interfaces.h"
LOG_MODULE_DECLARE(cc1352_greybus, CONFIG_BEAGLEPLAY_GREYBUS_LOG_LEVEL);
struct ap_to_node_item {
uint16_t node_cport;
struct gb_interface *node_intf;
};
struct node_to_ap_item {
struct node_ap_item {
uint8_t node_id;
uint16_t node_cport;
uint16_t ap_cport;
struct gb_interface *node_intf;
};
static struct ap_to_node_item ap_to_node[AP_MAX_NODES] = {0};
static struct node_to_ap_item node_to_ap[AP_MAX_NODES] = {0};
static size_t node_to_ap_count = 0;
static struct node_ap_item node_ap_map[AP_MAX_NODES] = {0};
static int ap_to_node_add(uint16_t ap_cport, uint16_t node_cport, struct gb_interface *node_intf)
static int node_ap_add(uint16_t ap_cport, uint16_t node_cport, struct gb_interface *node_intf)
{
if (ap_cport >= AP_MAX_NODES) {
return -E2BIG;
}
if (ap_to_node[ap_cport].node_intf) {
if (node_ap_map[ap_cport].node_intf) {
return -EALREADY;
}
ap_to_node[ap_cport].node_cport = node_cport;
ap_to_node[ap_cport].node_intf = node_intf;
node_ap_map[ap_cport].node_id = node_intf->id;
node_ap_map[ap_cport].node_cport = node_cport;
node_ap_map[ap_cport].node_intf = node_intf;
return 0;
}
static int ap_to_node_remove(uint16_t ap_cport)
static int node_ap_remove(uint16_t ap_cport)
{
if (ap_cport >= AP_MAX_NODES) {
return -E2BIG;
}
ap_to_node[ap_cport].node_cport = 0;
ap_to_node[ap_cport].node_intf = NULL;
return 0;
}
static int node_to_ap_add(uint8_t node_id, uint16_t node_cport, uint16_t ap_cport)
{
if (node_to_ap_count >= AP_MAX_NODES) {
return -E2BIG;
}
node_to_ap[node_to_ap_count].node_id = node_id;
node_to_ap[node_to_ap_count].node_cport = node_cport;
node_to_ap[node_to_ap_count].ap_cport = ap_cport;
node_to_ap_count++;
node_ap_map[ap_cport].node_id = 0;
node_ap_map[ap_cport].node_cport = 0;
node_ap_map[ap_cport].node_intf = NULL;
return 0;
}
static int node_to_ap_remove(uint16_t ap_cport)
{
for (size_t i = 0; i < node_to_ap_count; i++) {
if (node_to_ap[i].ap_cport == ap_cport) {
if (i != node_to_ap_count - 1) {
memcpy(&node_to_ap[i], &node_to_ap[node_to_ap_count - 1],
sizeof(struct node_to_ap_item));
}
node_to_ap_count--;
return 0;
}
}
return -EINVAL;
}
static int node_to_ap_cport(uint8_t node_id, uint16_t node_cport)
{
for (size_t i = 0; i < node_to_ap_count; i++) {
if (node_to_ap[i].node_id == node_id && node_to_ap[i].node_cport == node_cport) {
return node_to_ap[i].ap_cport;
for (size_t i = 0; i < AP_MAX_NODES; i++) {
if (node_ap_map[i].node_id == node_id && node_ap_map[i].node_cport == node_cport) {
return i;
}
}
......@@ -104,11 +67,10 @@ void apbridge_init(void)
void apbridge_deinit(void)
{
for (size_t i = 0; i < AP_MAX_NODES; ++i) {
ap_to_node[i].node_cport = 0;
ap_to_node[i].node_intf = NULL;
node_ap_map[i].node_id = 0;
node_ap_map[i].node_cport = 0;
node_ap_map[i].node_intf = NULL;
}
node_to_ap_count = 0;
}
int connection_create(uint8_t intf1_id, uint16_t intf1_cport, uint8_t intf2_id,
......@@ -144,19 +106,12 @@ int connection_create(uint8_t intf1_id, uint16_t intf1_cport, uint8_t intf2_id,
return ret;
}
ret = ap_to_node_add(ap_cport, node_cport, intf);
ret = node_ap_add(ap_cport, node_cport, intf);
if (ret < 0) {
LOG_ERR("Failed to add AP to node");
return ret;
}
ret = node_to_ap_add(node_id, node_cport, ap_cport);
if (ret < 0) {
LOG_ERR("Failed to add node to AP");
ap_to_node_remove(ap_cport);
return ret;
}
return 0;
}
......@@ -188,8 +143,7 @@ int connection_destroy(uint8_t intf1_id, uint16_t intf1_cport, uint8_t intf2_id,
intf->destroy_connection(intf, node_cport);
ap_to_node_remove(ap_cport);
node_to_ap_remove(ap_cport);
node_ap_remove(ap_cport);
return 0;
}
......@@ -200,8 +154,8 @@ int connection_send(uint8_t intf_id, uint16_t intf_cport, struct gb_message *msg
int ret;
if (intf_id == AP_INF_ID) {
intf = ap_to_node[intf_cport].node_intf;
ret = intf->write(intf, msg, ap_to_node[intf_cport].node_cport);
intf = node_ap_map[intf_cport].node_intf;
ret = intf->write(intf, msg, node_ap_map[intf_cport].node_cport);
} else {
ret = node_to_ap_cport(intf_id, intf_cport);
if (ret < 0) {
......
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