forked from Avnu/libavtp
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from boschglobal/app/hello-world
Added a hello-world example for Open1722
- Loading branch information
Showing
4 changed files
with
519 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Hello World! | ||
|
||
The hello-world applications provide an exemplary set of listener and talker based on the IEEE 1722 protocol. | ||
These applications are based on the Genereal Purpose Control (GPC) subtype of the AVTP Control Formats. | ||
|
||
## hello-world-talker | ||
_hello-world-talker_ sends the string "Hello World!" packed in a GPC frame of AVTP Control Format. | ||
The string being sent can be changed using the ```--message``` option. | ||
Options are available to send the IEEE 1722 frame as layer 2 frame or as a UDP segment. | ||
|
||
To run over Ethernet | ||
``` | ||
$ ./hello-world-talker <Ethernet interface name> <Destination MAC address> | ||
``` | ||
|
||
To run over UDP | ||
``` | ||
$ ./hello-world-talker --udp <destination IP>:<Port> | ||
``` | ||
|
||
By running wireshark on an appropriate interface, IEEE 1722 frames can be observed. | ||
|
||
|
||
## hello-world-listener | ||
_hello-world-listener_ receives the message string sent by the talker and prints it out on the console. The listener shall be operated in the same configuration (e.g. layer 2 or UDP) as the talker to ensure compatability. | ||
|
||
To run over Ethernet | ||
``` | ||
$ ./hello-world-listener <Ethernet interface name> | ||
``` | ||
|
||
To run over UDP | ||
``` | ||
$ ./hello-world-talker --udp <--port=<UDP port> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* Copyright (c) 2024, COVESA | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of COVESA nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include <argp.h> | ||
#include <stdlib.h> | ||
#include <linux/if_ether.h> | ||
#include <linux/if_packet.h> | ||
#include <linux/if.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <inttypes.h> | ||
|
||
#include "common/common.h" | ||
#include "avtp/Udp.h" | ||
#include "avtp/acf/Ntscf.h" | ||
#include "avtp/acf/Tscf.h" | ||
#include "avtp/acf/Common.h" | ||
#include "avtp/acf/Gpc.h" | ||
#include "avtp/CommonHeader.h" | ||
|
||
#define MAX_PDU_SIZE 1500 | ||
#define MAX_MSG_SIZE 100 | ||
|
||
static char ifname[IFNAMSIZ]; | ||
static uint8_t macaddr[ETH_ALEN]; | ||
static uint8_t use_udp; | ||
static uint32_t udp_port = 17220; | ||
|
||
static struct argp_option options[] = { | ||
{"port", 'p', "UDP_PORT", 0, "UDP Port to listen on if UDP enabled"}, | ||
{"udp", 'u', 0, 0, "Use UDP"}, | ||
{"dst-mac-address", 0, 0, OPTION_DOC, "Stream destination MAC address (If Ethernet)"}, | ||
{"ifname", 0, 0, OPTION_DOC, "Network interface (If Ethernet)" }, | ||
{ 0 } | ||
}; | ||
|
||
static error_t parser(int key, char *arg, struct argp_state *state) | ||
{ | ||
int res; | ||
|
||
switch (key) { | ||
case 'p': | ||
udp_port = atoi(arg); | ||
break; | ||
case 'u': | ||
use_udp = 1; | ||
break; | ||
|
||
case ARGP_KEY_NO_ARGS: | ||
break; | ||
|
||
case ARGP_KEY_ARG: | ||
|
||
if(state->argc < 2){ | ||
argp_usage(state); | ||
} | ||
|
||
if(!use_udp){ | ||
|
||
strncpy(ifname, arg, sizeof(ifname) - 1); | ||
|
||
if(state->next < state->argc) | ||
{ | ||
res = sscanf(state->argv[state->next], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | ||
&macaddr[0], &macaddr[1], &macaddr[2], | ||
&macaddr[3], &macaddr[4], &macaddr[5]); | ||
if (res != 6) { | ||
fprintf(stderr, "Invalid MAC address\n\n"); | ||
argp_usage(state); | ||
} | ||
state->next += 1; | ||
} | ||
} | ||
|
||
break; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static struct argp argp = { options, parser, 0, 0}; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int sk_fd, res; | ||
uint64_t msg_length, proc_bytes = 0, msg_proc_bytes = 0; | ||
uint64_t udp_seq_num, subtype, flag, acf_type, pdu_length; | ||
uint8_t pdu[MAX_PDU_SIZE]; | ||
uint8_t *cf_pdu, *acf_pdu, *udp_pdu; | ||
uint64_t gpc_code; | ||
char *recd_msg; | ||
|
||
argp_parse(&argp, argc, argv, 0, NULL, NULL); | ||
|
||
if (use_udp) { | ||
sk_fd = create_listener_socket_udp(udp_port); | ||
} else { | ||
sk_fd = create_listener_socket(ifname, macaddr, ETH_P_TSN); | ||
} | ||
|
||
if (sk_fd < 0) | ||
return 1; | ||
|
||
while (1) { | ||
proc_bytes = 0; | ||
|
||
res = recv(sk_fd, pdu, MAX_PDU_SIZE, 0); | ||
if (res < 0 || res > MAX_PDU_SIZE) { | ||
perror("Failed to receive data"); | ||
goto err; | ||
} | ||
|
||
// If UDP is used the packets starts with an encapsulation number | ||
if (use_udp) { | ||
udp_pdu = pdu; | ||
Avtp_Udp_GetField((Avtp_Udp_t *)udp_pdu, AVTP_UDP_FIELD_ENCAPSULATION_SEQ_NO, &udp_seq_num); | ||
cf_pdu = pdu + AVTP_UDP_HEADER_LEN; | ||
proc_bytes += AVTP_UDP_HEADER_LEN; | ||
} else { | ||
cf_pdu = pdu; | ||
} | ||
|
||
// Check if the packet is a control format packet (i.e. NTSCF or TSCF) | ||
res = Avtp_CommonHeader_GetField((Avtp_CommonHeader_t*)cf_pdu, AVTP_COMMON_HEADER_FIELD_SUBTYPE, &subtype); | ||
if (res < 0) { | ||
fprintf(stderr, "Failed to get subtype field: %d\n", res); | ||
goto err; | ||
} | ||
if(subtype == AVTP_SUBTYPE_TSCF){ | ||
proc_bytes += AVTP_TSCF_HEADER_LEN; | ||
Avtp_Tscf_GetField((Avtp_Tscf_t*)cf_pdu, AVTP_TSCF_FIELD_STREAM_DATA_LENGTH, (uint64_t *) &msg_length); | ||
}else{ | ||
proc_bytes += AVTP_NTSCF_HEADER_LEN; | ||
Avtp_Ntscf_GetField((Avtp_Ntscf_t*)cf_pdu, AVTP_NTSCF_FIELD_NTSCF_DATA_LENGTH, (uint64_t *) &msg_length); | ||
} | ||
|
||
// Check if the control packet payload is a ACF GPC. | ||
acf_pdu = &pdu[proc_bytes]; | ||
Avtp_AcfCommon_GetField((Avtp_AcfCommon_t*)acf_pdu, AVTP_ACF_FIELD_ACF_MSG_TYPE, &acf_type); | ||
if (acf_type != AVTP_ACF_TYPE_GPC) { | ||
fprintf(stderr, "ACF type mismatch: expected %u, got %lu\n", | ||
AVTP_ACF_TYPE_GPC, acf_type); | ||
continue; | ||
} | ||
|
||
// Parse the GPC Packet and print contents on the STDOUT | ||
Avtp_Gpc_GetField((Avtp_Gpc_t*)acf_pdu, AVTP_GPC_FIELD_GPC_MSG_ID, &gpc_code); | ||
Avtp_Gpc_GetField((Avtp_Gpc_t*)acf_pdu, AVTP_GPC_FIELD_ACF_MSG_LENGTH, &pdu_length); | ||
if (pdu_length * 4 <= MAX_MSG_SIZE) { | ||
recd_msg = acf_pdu + AVTP_GPC_HEADER_LEN; | ||
printf("%s : GPC Code %ld\n", recd_msg, gpc_code); | ||
} | ||
} | ||
|
||
return 0; | ||
|
||
err: | ||
close(sk_fd); | ||
return 1; | ||
|
||
} |
Oops, something went wrong.