2022-03-16 10:53:01 +01:00
|
|
|
/* AVB support
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2022 Wim Taymans
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <spa/utils/json.h>
|
|
|
|
|
#include <spa/debug/mem.h>
|
|
|
|
|
|
|
|
|
|
#include <pipewire/pipewire.h>
|
|
|
|
|
|
|
|
|
|
#include "aecp.h"
|
2022-03-17 09:03:52 +01:00
|
|
|
#include "aecp-aem.h"
|
2022-03-16 10:53:01 +01:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
2022-03-22 19:40:23 +01:00
|
|
|
static const uint8_t mac[6] = AVB_BROADCAST_MAC;
|
|
|
|
|
|
2022-03-17 19:13:43 +01:00
|
|
|
struct msg_info {
|
|
|
|
|
uint16_t type;
|
|
|
|
|
const char *name;
|
|
|
|
|
int (*handle) (struct aecp *aecp, const void *p, int len);
|
2022-03-16 10:53:01 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-17 19:13:43 +01:00
|
|
|
static int reply_not_implemented(struct aecp *aecp, const void *p, int len)
|
2022-03-16 17:34:27 +01:00
|
|
|
{
|
|
|
|
|
struct server *server = aecp->server;
|
|
|
|
|
uint8_t buf[len];
|
2022-04-07 15:14:45 +02:00
|
|
|
struct avb_ethernet_header *h = (void*)buf;
|
|
|
|
|
struct avb_packet_aecp_header *reply = SPA_PTROFF(h, sizeof(*h), void);
|
2022-03-16 17:34:27 +01:00
|
|
|
|
2022-04-07 15:14:45 +02:00
|
|
|
memcpy(h, p, len);
|
2022-03-25 10:28:18 +01:00
|
|
|
AVB_PACKET_AECP_SET_STATUS(reply, AVB_AECP_STATUS_NOT_IMPLEMENTED);
|
2022-03-17 09:03:52 +01:00
|
|
|
|
2022-04-07 15:14:45 +02:00
|
|
|
return avb_server_send_packet(server, h->src, AVB_TSN_ETH, buf, len);
|
2022-03-17 09:03:52 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:13:43 +01:00
|
|
|
static const struct msg_info msg_info[] = {
|
2022-03-25 10:28:18 +01:00
|
|
|
{ AVB_AECP_MESSAGE_TYPE_AEM_COMMAND, "aem-command", avb_aecp_aem_handle_command, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_AEM_RESPONSE, "aem-response", avb_aecp_aem_handle_response, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_ADDRESS_ACCESS_COMMAND, "address-access-command", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_ADDRESS_ACCESS_RESPONSE, "address-access-response", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_AVC_COMMAND, "avc-command", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_AVC_RESPONSE, "avc-response", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_VENDOR_UNIQUE_COMMAND, "vendor-unique-command", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_VENDOR_UNIQUE_RESPONSE, "vendor-unique-response", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_EXTENDED_COMMAND, "extended-command", NULL, },
|
|
|
|
|
{ AVB_AECP_MESSAGE_TYPE_EXTENDED_RESPONSE, "extended-response", NULL, },
|
2022-03-17 09:03:52 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-17 19:13:43 +01:00
|
|
|
static inline const struct msg_info *find_msg_info(uint16_t type, const char *name)
|
2022-03-17 09:03:52 +01:00
|
|
|
{
|
|
|
|
|
uint32_t i;
|
2022-03-17 19:13:43 +01:00
|
|
|
for (i = 0; i < SPA_N_ELEMENTS(msg_info); i++) {
|
|
|
|
|
if ((name == NULL && type == msg_info[i].type) ||
|
|
|
|
|
(name != NULL && spa_streq(name, msg_info[i].name)))
|
|
|
|
|
return &msg_info[i];
|
2022-03-17 09:03:52 +01:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:13:43 +01:00
|
|
|
static int aecp_message(void *data, uint64_t now, const void *message, int len)
|
2022-03-16 10:53:01 +01:00
|
|
|
{
|
|
|
|
|
struct aecp *aecp = data;
|
2022-03-22 19:40:23 +01:00
|
|
|
struct server *server = aecp->server;
|
2022-04-07 15:14:45 +02:00
|
|
|
const struct avb_ethernet_header *h = message;
|
|
|
|
|
const struct avb_packet_aecp_header *p = SPA_PTROFF(h, sizeof(*h), void);
|
2022-03-17 19:13:43 +01:00
|
|
|
const struct msg_info *info;
|
2022-03-16 17:34:27 +01:00
|
|
|
int message_type;
|
2022-03-16 10:53:01 +01:00
|
|
|
|
2022-04-07 15:14:45 +02:00
|
|
|
if (ntohs(h->type) != AVB_TSN_ETH)
|
2022-03-22 19:40:23 +01:00
|
|
|
return 0;
|
2022-04-07 15:14:45 +02:00
|
|
|
if (memcmp(h->dest, mac, 6) != 0 &&
|
|
|
|
|
memcmp(h->dest, server->mac_addr, 6) != 0)
|
2022-03-22 19:40:23 +01:00
|
|
|
return 0;
|
2022-03-25 10:28:18 +01:00
|
|
|
if (AVB_PACKET_GET_SUBTYPE(&p->hdr) != AVB_SUBTYPE_AECP)
|
2022-03-16 10:53:01 +01:00
|
|
|
return 0;
|
|
|
|
|
|
2022-03-25 10:28:18 +01:00
|
|
|
message_type = AVB_PACKET_AECP_GET_MESSAGE_TYPE(p);
|
2022-03-17 19:13:43 +01:00
|
|
|
|
|
|
|
|
info = find_msg_info(message_type, NULL);
|
|
|
|
|
if (info == NULL)
|
2022-04-07 15:14:45 +02:00
|
|
|
return reply_not_implemented(aecp, message, len);
|
2022-03-17 19:13:43 +01:00
|
|
|
|
2022-03-18 15:18:11 +01:00
|
|
|
pw_log_debug("got AECP message %s", info->name);
|
2022-03-17 19:13:43 +01:00
|
|
|
|
|
|
|
|
if (info->handle == NULL)
|
2022-04-07 15:14:45 +02:00
|
|
|
return reply_not_implemented(aecp, message, len);
|
2022-03-17 19:13:43 +01:00
|
|
|
|
2022-04-07 15:14:45 +02:00
|
|
|
return info->handle(aecp, message, len);
|
2022-03-16 10:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void aecp_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct aecp *aecp = data;
|
|
|
|
|
spa_hook_remove(&aecp->server_listener);
|
|
|
|
|
free(aecp);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 15:18:11 +01:00
|
|
|
static int do_help(struct aecp *aecp, const char *args, FILE *out)
|
2022-03-16 10:53:01 +01:00
|
|
|
{
|
2022-03-18 15:18:11 +01:00
|
|
|
fprintf(out, "{ \"type\": \"help\","
|
|
|
|
|
"\"text\": \""
|
|
|
|
|
"/adp/help: this help \\n"
|
|
|
|
|
"\" }");
|
2022-03-16 10:53:01 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 15:18:11 +01:00
|
|
|
static int aecp_command(void *data, uint64_t now, const char *command, const char *args, FILE *out)
|
2022-03-16 10:53:01 +01:00
|
|
|
{
|
|
|
|
|
struct aecp *aecp = data;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
if (!spa_strstartswith(command, "/aecp/"))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
command += strlen("/aecp/");
|
|
|
|
|
|
|
|
|
|
if (spa_streq(command, "help"))
|
2022-03-18 15:18:11 +01:00
|
|
|
res = do_help(aecp, args, out);
|
2022-03-16 10:53:01 +01:00
|
|
|
else
|
|
|
|
|
res = -ENOTSUP;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct server_events server_events = {
|
2022-03-25 10:28:18 +01:00
|
|
|
AVB_VERSION_SERVER_EVENTS,
|
2022-03-16 10:53:01 +01:00
|
|
|
.destroy = aecp_destroy,
|
|
|
|
|
.message = aecp_message,
|
|
|
|
|
.command = aecp_command
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-25 10:28:18 +01:00
|
|
|
struct avb_aecp *avb_aecp_register(struct server *server)
|
2022-03-16 10:53:01 +01:00
|
|
|
{
|
|
|
|
|
struct aecp *aecp;
|
|
|
|
|
|
|
|
|
|
aecp = calloc(1, sizeof(*aecp));
|
|
|
|
|
if (aecp == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
aecp->server = server;
|
|
|
|
|
|
|
|
|
|
avdecc_server_add_listener(server, &aecp->server_listener, &server_events, aecp);
|
|
|
|
|
|
2022-03-25 10:28:18 +01:00
|
|
|
return (struct avb_aecp*)aecp;
|
2022-03-16 10:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-25 10:28:18 +01:00
|
|
|
void avb_aecp_unregister(struct avb_aecp *aecp)
|
2022-03-16 10:53:01 +01:00
|
|
|
{
|
|
|
|
|
aecp_destroy(aecp);
|
|
|
|
|
}
|