diff options
author | Jean-Baptiste Maillet <jean-baptiste.maillet@parrot.com> | 2014-11-17 14:46:55 +0100 |
---|---|---|
committer | Jean-Baptiste Maillet <jean-baptiste.maillet@parrot.com> | 2014-11-17 14:46:55 +0100 |
commit | c24897c0b6b71f4a72a6dd3c87fcb48fb02a1487 (patch) | |
tree | c03946656d86d9583d07ed3b7587410390f49268 /examples/simple_talker | |
parent | f3e10b3f8dc159603ec18f7e608255ca0be63214 (diff) | |
download | Open-AVB-c24897c0b6b71f4a72a6dd3c87fcb48fb02a1487.tar.gz |
simple_talker/listener: move the MRP client part to the common subdir
Run tested OK.
Diffstat (limited to 'examples/simple_talker')
-rw-r--r-- | examples/simple_talker/Makefile | 11 | ||||
-rw-r--r-- | examples/simple_talker/talker_mrp_client.c | 575 | ||||
-rw-r--r-- | examples/simple_talker/talker_mrp_client.h | 82 |
3 files changed, 6 insertions, 662 deletions
diff --git a/examples/simple_talker/Makefile b/examples/simple_talker/Makefile index 6e34b94b..2fddf2c6 100644 --- a/examples/simple_talker/Makefile +++ b/examples/simple_talker/Makefile @@ -2,23 +2,24 @@ OPT=-O2 CFLAGS=$(OPT) -Wall -W -Wno-parentheses CC=gcc -INCFLAGS=-I../../lib/igb +INCFLAGS=-I../../lib/igb -I../../daemons/mrpd -I../common LDLIBS=-ligb -lpci -lz -lrt -lm -pthread LDFLAGS=-L../../lib/igb all: simple_talker -simple_talker: simple_talker.o talker_mrp_client.o +simple_talker: simple_talker.o ../common/talker_mrp_client.o simple_talker.o: simple_talker.c - gcc -c $(INCFLAGS) -I../../daemons/mrpd $(CFLAGS) simple_talker.c + gcc -c $(INCFLAGS) $(CFLAGS) simple_talker.c -talker_mrp_client.o: talker_mrp_client.c - gcc -c $(INCFLAGS) -I../../daemons/mrpd $(CFLAGS) talker_mrp_client.c +../common/talker_mrp_client.o: + make -C ../common/ talker_mrp_client.o %: %.o $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ clean: rm -f `find . -name "*~" -o -name "*.[oa]" -o -name "\#*\#" -o -name TAGS -o -name core -o -name "*.orig"` simple_talker + rm -f ../common/talker_mrp_client.o diff --git a/examples/simple_talker/talker_mrp_client.c b/examples/simple_talker/talker_mrp_client.c deleted file mode 100644 index 981006fa..00000000 --- a/examples/simple_talker/talker_mrp_client.c +++ /dev/null @@ -1,575 +0,0 @@ -/****************************************************************************** - - Copyright (c) 2012, Intel Corporation - Copyright (c) 2014, Parrot SA - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. 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. - - 3. Neither the name of the Intel Corporation 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. - -******************************************************************************/ - -/* - * simple_talker MRP client part - * gcc -Wall -c -I../../daemons/mrpd talker_mrp_client.c - */ - -#include "talker_mrp_client.h" - -/* global variables */ - -int control_socket = -1; - -volatile int halt_tx = 0; -volatile int listeners = 0; -volatile int mrp_okay; -volatile int mrp_error = 0;; - -volatile int domain_a_valid = 0; -int domain_class_a_id; -int domain_class_a_priority; -int domain_class_a_vid; - -volatile int domain_b_valid = 0; -int domain_class_b_id; -int domain_class_b_priority; -int domain_class_b_vid; - -pthread_t monitor_thread; -pthread_attr_t monitor_attr; -unsigned char monitor_stream_id[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - -/* - * private - */ - -int send_mrp_msg(char *notify_data, int notify_len) -{ - struct sockaddr_in addr; - socklen_t addr_len; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(MRPD_PORT_DEFAULT); - inet_aton("127.0.0.1", &addr.sin_addr); - addr_len = sizeof(addr); - if (control_socket != -1) - return (sendto - (control_socket, notify_data, notify_len, 0, - (struct sockaddr *)&addr, addr_len)); - - else - return (0); -} - -int process_mrp_msg(char *buf, int buflen) -{ - - /* - * 1st character indicates application - * [MVS] - MAC, VLAN or STREAM - */ - unsigned int id; - unsigned int priority; - unsigned int vid; - int i, j, k; - unsigned int substate; - unsigned char recovered_streamid[8]; - k = 0; - next_line:if (k >= buflen) - return (0); - switch (buf[k]) { - case 'E': - printf("%s from mrpd\n", buf); - fflush(stdout); - mrp_error = 1; - break; - case 'O': - mrp_okay = 1; - break; - case 'M': - case 'V': - printf("%s unhandled from mrpd\n", buf); - fflush(stdout); - - /* unhandled for now */ - break; - case 'L': - - /* parse a listener attribute - see if it matches our monitor_stream_id */ - i = k; - while (buf[i] != 'D') - i++; - i += 2; /* skip the ':' */ - sscanf(&(buf[i]), "%d", &substate); - while (buf[i] != 'S') - i++; - i += 2; /* skip the ':' */ - for (j = 0; j < 8; j++) { - sscanf(&(buf[i + 2 * j]), "%02x", &id); - recovered_streamid[j] = (unsigned char)id; - } printf - ("FOUND STREAM ID=%02x%02x%02x%02x%02x%02x%02x%02x ", - recovered_streamid[0], recovered_streamid[1], - recovered_streamid[2], recovered_streamid[3], - recovered_streamid[4], recovered_streamid[5], - recovered_streamid[6], recovered_streamid[7]); - switch (substate) { - case 0: - printf("with state ignore\n"); - break; - case 1: - printf("with state askfailed\n"); - break; - case 2: - printf("with state ready\n"); - break; - case 3: - printf("with state readyfail\n"); - break; - default: - printf("with state UNKNOWN (%d)\n", substate); - break; - } - if (substate > MSRP_LISTENER_ASKFAILED) { - if (memcmp - (recovered_streamid, monitor_stream_id, - sizeof(recovered_streamid)) == 0) { - listeners = 1; - printf("added listener\n"); - } - } - fflush(stdout); - - /* try to find a newline ... */ - while ((i < buflen) && (buf[i] != '\n') && (buf[i] != '\0')) - i++; - if (i == buflen) - return (0); - if (buf[i] == '\0') - return (0); - i++; - k = i; - goto next_line; - break; - case 'D': - i = k + 4; - - /* save the domain attribute */ - sscanf(&(buf[i]), "%d", &id); - while (buf[i] != 'P') - i++; - i += 2; /* skip the ':' */ - sscanf(&(buf[i]), "%d", &priority); - while (buf[i] != 'V') - i++; - i += 2; /* skip the ':' */ - sscanf(&(buf[i]), "%x", &vid); - if (id == 6) { - domain_class_a_id = id; - domain_class_a_priority = priority; - domain_class_a_vid = vid; - domain_a_valid = 1; - } else { - domain_class_b_id = id; - domain_class_b_priority = priority; - domain_class_b_vid = vid; - domain_b_valid = 1; - } - while ((i < buflen) && (buf[i] != '\n') && (buf[i] != '\0')) - i++; - if ((i == buflen) || (buf[i] == '\0')) - return (0); - i++; - k = i; - goto next_line; - break; - case 'T': - - /* as simple_talker we don't care about other talkers */ - i = k; - while ((i < buflen) && (buf[i] != '\n') && (buf[i] != '\0')) - i++; - if (i == buflen) - return (0); - if (buf[i] == '\0') - return (0); - i++; - k = i; - goto next_line; - break; - case 'S': - - /* handle the leave/join events */ - switch (buf[k + 4]) { - case 'L': - i = k + 5; - while (buf[i] != 'D') - i++; - i += 2; /* skip the ':' */ - sscanf(&(buf[i]), "%d", &substate); - while (buf[i] != 'S') - i++; - i += 2; /* skip the ':' */ - for (j = 0; j < 8; j++) { - sscanf(&(buf[i + 2 * j]), "%02x", &id); - recovered_streamid[j] = (unsigned char)id; - } printf - ("EVENT on STREAM ID=%02x%02x%02x%02x%02x%02x%02x%02x ", - recovered_streamid[0], recovered_streamid[1], - recovered_streamid[2], recovered_streamid[3], - recovered_streamid[4], recovered_streamid[5], - recovered_streamid[6], recovered_streamid[7]); - switch (substate) { - case 0: - printf("with state ignore\n"); - break; - case 1: - printf("with state askfailed\n"); - break; - case 2: - printf("with state ready\n"); - break; - case 3: - printf("with state readyfail\n"); - break; - default: - printf("with state UNKNOWN (%d)\n", substate); - break; - } - switch (buf[k + 1]) { - case 'L': - printf("got a leave indication\n"); - if (memcmp - (recovered_streamid, monitor_stream_id, - sizeof(recovered_streamid)) == 0) { - listeners = 0; - printf("listener left\n"); - } - break; - case 'J': - case 'N': - printf("got a new/join indication\n"); - if (substate > MSRP_LISTENER_ASKFAILED) { - if (memcmp - (recovered_streamid, - monitor_stream_id, - sizeof(recovered_streamid)) == 0) - listeners = 1; - } - break; - } - - /* only care about listeners ... */ - default: - return (0); - break; - } - break; - case '\0': - break; - } - return (0); -} - -void *mrp_monitor_thread(void *arg) -{ - char *msgbuf; - struct sockaddr_in client_addr; - struct msghdr msg; - struct iovec iov; - int bytes = 0; - struct pollfd fds; - int rc; - if (NULL == arg) - rc = 0; - - else - rc = 1; - msgbuf = (char *)malloc(MAX_MRPD_CMDSZ); - if (NULL == msgbuf) - return NULL; - while (!halt_tx) { - fds.fd = control_socket; - fds.events = POLLIN; - fds.revents = 0; - rc = poll(&fds, 1, 100); - if (rc < 0) { - free(msgbuf); - pthread_exit(NULL); - } - if (rc == 0) - continue; - if ((fds.revents & POLLIN) == 0) { - free(msgbuf); - pthread_exit(NULL); - } - memset(&msg, 0, sizeof(msg)); - memset(&client_addr, 0, sizeof(client_addr)); - memset(msgbuf, 0, MAX_MRPD_CMDSZ); - iov.iov_len = MAX_MRPD_CMDSZ; - iov.iov_base = msgbuf; - msg.msg_name = &client_addr; - msg.msg_namelen = sizeof(client_addr); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - bytes = recvmsg(control_socket, &msg, 0); - if (bytes < 0) - continue; - process_mrp_msg(msgbuf, bytes); - } - free(msgbuf); - pthread_exit(NULL); -} - -/* - * public - */ - -int mrp_connect(void) -{ - struct sockaddr_in addr; - int sock_fd = -1; - sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (sock_fd < 0) - goto out; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(MRPD_PORT_DEFAULT); - inet_aton("127.0.0.1", &addr.sin_addr); - memset(&addr, 0, sizeof(addr)); - control_socket = sock_fd; - return (0); - out: if (sock_fd != -1) - close(sock_fd); - sock_fd = -1; - return (-1); -} - -int mrp_disconnect(void) -{ - char *msgbuf; - int rc; - msgbuf = malloc(64); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 64); - sprintf(msgbuf, "BYE"); - mrp_okay = 0; - rc = send_mrp_msg(msgbuf, 1500); - - /* rc = recv_mrp_okay(); */ - free(msgbuf); - return rc; -} - -int mrp_monitor(void) -{ - pthread_attr_init(&monitor_attr); - pthread_create(&monitor_thread, NULL, mrp_monitor_thread, NULL); - return (0); -} - -int mrp_register_domain(int *class_id, int *priority, u_int16_t * vid) -{ - char *msgbuf; - int rc; - msgbuf = malloc(64); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 64); - sprintf(msgbuf, "S+D:C=%d,P=%d,V=%04x", *class_id, *priority, *vid); - mrp_okay = 0; - rc = send_mrp_msg(msgbuf, 1500); - - /* rc = recv_mrp_okay(); */ - free(msgbuf); - return rc; -} - - -int -mrp_advertise_stream(uint8_t * streamid, - uint8_t * destaddr, - u_int16_t vlan, - int pktsz, int interval, int priority, int latency) -{ - char *msgbuf; - int rc; - msgbuf = malloc(1500); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 1500); - sprintf(msgbuf, "S++:S=%02X%02X%02X%02X%02X%02X%02X%02X" - ",A=%02X%02X%02X%02X%02X%02X" - ",V=%04X" - ",Z=%d" - ",I=%d" - ",P=%d" - ",L=%d", streamid[0], streamid[1], streamid[2], - streamid[3], streamid[4], streamid[5], streamid[6], - streamid[7], destaddr[0], destaddr[1], destaddr[2], - destaddr[3], destaddr[4], destaddr[5], vlan, pktsz, - interval, priority << 5, latency); - mrp_okay = 0; - rc = send_mrp_msg(msgbuf, 1500); - - /* rc = recv_mrp_okay(); */ - free(msgbuf); - return rc; -} - -int -mrp_unadvertise_stream(uint8_t * streamid, - uint8_t * destaddr, - u_int16_t vlan, - int pktsz, int interval, int priority, int latency) -{ - char *msgbuf; - int rc; - msgbuf = malloc(1500); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 1500); - sprintf(msgbuf, "S--:S=%02X%02X%02X%02X%02X%02X%02X%02X" - ",A=%02X%02X%02X%02X%02X%02X" - ",V=%04X" - ",Z=%d" - ",I=%d" - ",P=%d" - ",L=%d", streamid[0], streamid[1], streamid[2], - streamid[3], streamid[4], streamid[5], streamid[6], - streamid[7], destaddr[0], destaddr[1], destaddr[2], - destaddr[3], destaddr[4], destaddr[5], vlan, pktsz, - interval, priority << 5, latency); - mrp_okay = 0; - rc = send_mrp_msg(msgbuf, 1500); - - /* rc = recv_mrp_okay(); */ - free(msgbuf); - return rc; -} - - -int mrp_await_listener(unsigned char *streamid) -{ - char *msgbuf; - memcpy(monitor_stream_id, streamid, sizeof(monitor_stream_id)); - msgbuf = malloc(64); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 64); - sprintf(msgbuf, "S??"); - send_mrp_msg(msgbuf, 64); - free(msgbuf); - - /* either already there ... or need to wait ... */ - while (!halt_tx && (listeners == 0)) - usleep(20000); - return (0); -} - -/* - * actually not used - */ - -int mrp_get_domain(int *class_a_id, int *a_priority, u_int16_t * a_vid, - int *class_b_id, int *b_priority, u_int16_t * b_vid) -{ - char *msgbuf; - - /* we may not get a notification if we are joining late, - * so query for what is already there ... - */ - msgbuf = malloc(64); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 64); - sprintf(msgbuf, "S??"); - send_mrp_msg(msgbuf, 64); - free(msgbuf); - while (!halt_tx && (domain_a_valid == 0) && (domain_b_valid == 0)) - usleep(20000); - *class_a_id = 0; - *a_priority = 0; - *a_vid = 0; - *class_b_id = 0; - *b_priority = 0; - *b_vid = 0; - if (domain_a_valid) { - *class_a_id = domain_class_a_id; - *a_priority = domain_class_a_priority; - *a_vid = domain_class_a_vid; - } - if (domain_b_valid) { - *class_b_id = domain_class_b_id; - *b_priority = domain_class_b_priority; - *b_vid = domain_class_b_vid; - } - return (0); -} - -int mrp_join_vlan() -{ - char *msgbuf; - int rc; - msgbuf = malloc(1500); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 1500); - sprintf(msgbuf, "V++:I=0002"); - rc = send_mrp_msg(msgbuf, 1500); - - free(msgbuf); - return rc; -} - -int mrp_join_listener(uint8_t * streamid) -{ - char *msgbuf; - int rc; - msgbuf = malloc(1500); - if (NULL == msgbuf) - return -1; - memset(msgbuf, 0, 1500); - sprintf(msgbuf, "S+L:S=%02X%02X%02X%02X%02X%02X%02X%02X" - ",D=2", streamid[0], streamid[1], streamid[2], streamid[3], - streamid[4], streamid[5], streamid[6], streamid[7]); - mrp_okay = 0; - rc = send_mrp_msg(msgbuf, 1500); - - /* rc = recv_mrp_okay(); */ - free(msgbuf); - return rc; -} - -// TODO remove -int recv_mrp_okay() -{ - while ((mrp_okay == 0) && (mrp_error == 0)) - usleep(20000); - return 0; -} - diff --git a/examples/simple_talker/talker_mrp_client.h b/examples/simple_talker/talker_mrp_client.h deleted file mode 100644 index 2dee00f3..00000000 --- a/examples/simple_talker/talker_mrp_client.h +++ /dev/null @@ -1,82 +0,0 @@ -/****************************************************************************** - - Copyright (c) 2012, Intel Corporation - Copyright (c) 2014, Parrot SA - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. 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. - - 3. Neither the name of the Intel Corporation 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. - -******************************************************************************/ - -#ifndef _TALKER_MRP_CLIENT_H_ -#define _TALKER_MRP_CLIENT_H_ - -/* - * simple_talker MRP client part - */ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <netinet/in.h> -#include <pthread.h> -#include <poll.h> - -#include "mrpd.h" -#include "mrp.h" -#include "msrp.h" // spurious dep daemons/mrpd/msrp.h:50:#define MSRP_LISTENER_ASKFAILED - -/* global variables */ - -// TODO move these in a talker_context struct + init func - -extern volatile int halt_tx; -extern volatile int listeners; -extern volatile int mrp_error; - -extern volatile int domain_a_valid; -extern int domain_class_a_id; -extern int domain_class_a_priority; -extern int domain_class_a_vid; - -extern volatile int domain_b_valid; -extern int domain_class_b_id; -extern int domain_class_b_priority; -extern int domain_class_b_vid; - -/* functions */ - -int mrp_connect(void); -int mrp_disconnect(void); -int mrp_monitor(void); -int mrp_register_domain(int *class_id, int *priority, u_int16_t * vid); -int mrp_join_vlan(void); -int mrp_advertise_stream(uint8_t * streamid, uint8_t * destaddr, u_int16_t vlan, int pktsz, int interval, int priority, int latency); -int mrp_unadvertise_stream(uint8_t * streamid, uint8_t * destaddr, u_int16_t vlan, int pktsz, int interval, int priority, int latency); -int mrp_await_listener(unsigned char *streamid); - -#endif /* _TALKER_MRP_CLIENT_H_ */ |