2020-06-15 10:23:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/can/can_conn.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2020-06-15 10:23:25 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "can/can.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CAN
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The array containing all NetLink connections. */
|
|
|
|
|
2023-02-13 18:15:37 +01:00
|
|
|
#if CONFIG_CAN_PREALLOC_CONNS > 0
|
|
|
|
static struct can_conn_s g_can_connections[CONFIG_CAN_PREALLOC_CONNS];
|
2022-01-04 04:53:55 +01:00
|
|
|
#endif
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
/* A list of all free NetLink connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_can_connections;
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
/* A list of all allocated NetLink connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_active_can_connections;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_initialize()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the User Socket connection structures. Called once and only
|
|
|
|
* from the networking layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void can_initialize(void)
|
|
|
|
{
|
2023-02-13 18:15:37 +01:00
|
|
|
#if CONFIG_CAN_PREALLOC_CONNS > 0
|
2020-06-15 10:23:25 +02:00
|
|
|
int i;
|
|
|
|
|
2023-02-13 18:15:37 +01:00
|
|
|
for (i = 0; i < CONFIG_CAN_PREALLOC_CONNS; i++)
|
2020-06-15 10:23:25 +02:00
|
|
|
{
|
|
|
|
/* Mark the connection closed and move it to the free list */
|
|
|
|
|
2022-02-07 04:55:18 +01:00
|
|
|
dq_addlast(&g_can_connections[i].sconn.node, &g_free_can_connections);
|
2020-06-15 10:23:25 +02:00
|
|
|
}
|
2022-01-04 04:53:55 +01:00
|
|
|
#endif
|
2020-06-15 10:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_alloc()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new, uninitialized NetLink connection structure. This is
|
|
|
|
* normally something done by the implementation of the socket() API
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct can_conn_s *can_alloc(void)
|
|
|
|
{
|
|
|
|
FAR struct can_conn_s *conn;
|
2023-02-13 18:15:37 +01:00
|
|
|
#if CONFIG_CAN_ALLOC_CONNS > 0
|
2022-01-04 04:53:55 +01:00
|
|
|
int i;
|
|
|
|
#endif
|
2020-06-15 10:23:25 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a a mutex. */
|
2020-06-15 10:23:25 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2023-02-13 18:15:37 +01:00
|
|
|
#if CONFIG_CAN_ALLOC_CONNS > 0
|
2022-01-04 04:53:55 +01:00
|
|
|
if (dq_peek(&g_free_can_connections) == NULL)
|
|
|
|
{
|
2023-02-13 18:15:37 +01:00
|
|
|
#if CONFIG_CAN_MAX_CONNS > 0
|
2024-04-06 15:07:06 +02:00
|
|
|
if (dq_count(&g_active_can_connections) +
|
|
|
|
CONFIG_CAN_ALLOC_CONNS > CONFIG_CAN_MAX_CONNS)
|
2023-02-13 18:15:37 +01:00
|
|
|
{
|
|
|
|
nxmutex_unlock(&g_free_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
conn = kmm_zalloc(sizeof(*conn) * CONFIG_CAN_ALLOC_CONNS);
|
2022-01-04 04:53:55 +01:00
|
|
|
if (conn != NULL)
|
|
|
|
{
|
2023-02-13 18:15:37 +01:00
|
|
|
for (i = 0; i < CONFIG_CAN_ALLOC_CONNS; i++)
|
2022-01-04 04:53:55 +01:00
|
|
|
{
|
2022-02-07 04:55:18 +01:00
|
|
|
dq_addlast(&conn[i].sconn.node, &g_free_can_connections);
|
2022-01-04 04:53:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
conn = (FAR struct can_conn_s *)dq_remfirst(&g_free_can_connections);
|
|
|
|
if (conn != NULL)
|
|
|
|
{
|
|
|
|
/* FIXME SocketCAN default behavior enables loopback */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_CANPROTO_OPTIONS
|
|
|
|
/* By default the filter is configured to catch all,
|
|
|
|
* this is done in commented filter code below:
|
|
|
|
*
|
|
|
|
* struct can_filter_t catchall_filter;
|
|
|
|
* filter.can_id = 0;
|
|
|
|
* filter.can_mask = 0;
|
|
|
|
* conn->filters[0] = catchall_filter;
|
|
|
|
*
|
|
|
|
* However memset already sets the filter to 0
|
|
|
|
* therefore we only have to set the filter count to 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn->filter_count = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
|
2022-02-07 04:55:18 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_active_can_connections);
|
2020-06-15 10:23:25 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
2020-06-15 10:23:25 +02:00
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_free()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a NetLink connection structure that is no longer in use. This
|
|
|
|
* should be done by the implementation of close().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void can_free(FAR struct can_conn_s *conn)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2020-06-15 10:23:25 +02:00
|
|
|
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
2022-02-07 04:55:18 +01:00
|
|
|
dq_rem(&conn->sconn.node, &g_active_can_connections);
|
2020-06-15 10:23:25 +02:00
|
|
|
|
2023-02-13 18:15:37 +01:00
|
|
|
/* If this is a preallocated or a batch allocated connection store it in
|
|
|
|
* the free connections list. Else free it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_CAN_ALLOC_CONNS == 1
|
|
|
|
if (conn < g_can_connections || conn >= (g_can_connections +
|
|
|
|
CONFIG_CAN_PREALLOC_CONNS))
|
|
|
|
{
|
|
|
|
kmm_free(conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2023-02-28 11:31:21 +01:00
|
|
|
memset(conn, 0, sizeof(*conn));
|
2023-02-13 18:15:37 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_free_can_connections);
|
|
|
|
}
|
2020-06-15 10:23:25 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
2020-06-15 10:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_nextconn()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of allocated NetLink connections
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from NetLink device logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct can_conn_s *can_nextconn(FAR struct can_conn_s *conn)
|
|
|
|
{
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
return (FAR struct can_conn_s *)g_active_can_connections.head;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-07 04:55:18 +01:00
|
|
|
return (FAR struct can_conn_s *)conn->sconn.node.flink;
|
2020-06-15 10:23:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 08:01:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_active()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of NetLink connections that match dev
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device to search for.
|
|
|
|
* conn - The current connection; may be NULL to start the search at the
|
|
|
|
* beginning
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from NetLink device logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct can_conn_s *can_active(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct can_conn_s *conn)
|
|
|
|
{
|
|
|
|
while ((conn = can_nextconn(conn)) != NULL)
|
|
|
|
{
|
|
|
|
if (conn->dev == NULL || conn->dev == dev)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:23:25 +02:00
|
|
|
#endif /* CONFIG_NET_CAN */
|