Commit 2365a97a authored by Wenxi XU's avatar Wenxi XU
Browse files

创建了新的可变接口的通讯协议,通过组合protocol和interface实现通用

parent 3f25ec44
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
#ifndef ARES_COMM_H__
#define ARES_COMM_H__

#include <ares/protocol/ares_protocol.h>
#include <ares/interface/usb/usb_bulk.h>
#include <zephyr/kernel.h>
#include <zephyr/net_buf.h>

int ares_bind_interface(struct AresInterface *interface, struct AresProtocol *protocol)
{
	// Notice that we initialize interface before protocol
	if (interface == NULL || protocol == NULL) {
		return -EINVAL;
	}
	interface->protocol = protocol;
	protocol->interface = interface;
	int ret = interface->api->init(interface);
	if (ret) {
		return ret;
	}
	ret = protocol->api->init(protocol);
	if (ret) {
		return ret;
	}
	return 0;
}

#endif
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
add_subdirectory_ifdef(CONFIG_USB_BULK_INTERFACE usb)
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
rsource "usb/Kconfig"


config USB_BULK_INTERFACE
	bool "USB_BULK_INTERFACE"
	help
	  Enable USB_BULK_INTERFACE
	select ARES_COMM_LIB
	select USB_DEVICE_STACK_NEXT
 No newline at end of file
+74 −0
Original line number Diff line number Diff line
#ifndef _ARES_INTERFACE_H_
#define _ARES_INTERFACE_H_

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <zephyr/net_buf.h>

#ifdef __cplusplus
extern "C" {
#endif

struct AresProtocol;
struct AresInterface;

/**
 * @brief API that an interface must implement.
 *
 * This structure defines the set of functions that a specific interface
 * (e.g., a UART or USB driver) must provide. It is primarily used by the
 * protocol layer to send data.
 */
struct AresInterfaceAPI {
	int (*send)(struct AresInterface *interface, struct net_buf *buf);
	int (*send_with_lock)(struct AresInterface *interface, struct net_buf *buf,
			      struct k_mutex *mutex);

	int (*connect)(struct AresInterface *interface);
	int (*disconnect)(struct AresInterface *interface);
	bool (*is_connected)(struct AresInterface *interface);

	struct net_buf *(*alloc_buf)(struct AresInterface *interface);
	struct net_buf *(*alloc_buf_with_data)(struct AresInterface *interface, void *data,
					       size_t size);

	int (*init)(struct AresInterface *interface);
};

/**
 * @brief Represents a specific communication interface instance (e.g., a UART port).
 *
 * This structure holds the state of the interface, including the loaded protocol
 * and any driver-specific data.
 */
struct AresInterface {
	const char *name;
	const struct AresInterfaceAPI *api;
	struct AresProtocol *protocol; // The protocol loaded onto this interface

	void *priv_data;    // Interface-specific private data (e.g., UART device ptr)
};

/**
 * @brief Loads a protocol onto a communication interface.
 *
 * @param interface Pointer to the interface instance.
 * @param protocol Pointer to the protocol definition to load.
 * @return 0 on success, negative error code on failure.
 */
static inline int ares_interface_load_protocol(struct AresInterface *interface,
					       struct AresProtocol *protocol)
{
	if (!interface || !protocol) {
		return -EINVAL;
	}
	interface->protocol = protocol;
	return 0;
}

#ifdef __cplusplus
}
#endif

#endif
+10 −0
Original line number Diff line number Diff line
# Copyright (c) 2024 ttwards <12411711@mail.sustech.edu.cn>
# SPDX-License-Identifier: Apache-2.0

zephyr_library()

set(USB_BULK_INTERFACE_SOURCES
	usb_bulk.c
)

zephyr_library_sources(${USB_BULK_INTERFACE_SOURCES})
 No newline at end of file
Loading