pax_global_header00006660000000000000000000000064132763075720014526gustar00rootroot0000000000000052 comment=56899c0504e00b78741ff6f8ac7b4d749bc7eb23 libe131-1.4.0/000077500000000000000000000000001327630757200126705ustar00rootroot00000000000000libe131-1.4.0/.gitignore000066400000000000000000000011121327630757200146530ustar00rootroot00000000000000# Created by https://www.gitignore.io/api/c ### Autotools ### # http://www.gnu.org/software/automake Makefile.in # http://www.gnu.org/software/autoconf /autom4te.cache /autoscan.log /autoscan-*.log /aclocal.m4 /compile /config.h.in /configure /configure.scan /depcomp /install-sh /missing /stamp-h1 ### C ### # Prerequisites *.d # Object files *.o *.ko *.obj *.elf # Precompiled Headers *.gch *.pch # Libraries *.lib *.a *.la *.lo # Shared objects (inc. Windows DLLs) *.dll *.so *.so.* *.dylib # Executables *.exe *.out *.app *.i*86 *.x86_64 *.hex # Debug files *.dSYM/ *.su libe131-1.4.0/LICENSE000066400000000000000000000261151327630757200137020ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2016 Hugo Hromic Licensed 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. libe131-1.4.0/Makefile.am000066400000000000000000000005771327630757200147350ustar00rootroot00000000000000AM_CFLAGS = -Wall -O3 -std=gnu11 -pedantic ACLOCAL_AMFLAGS = -I m4 dist_doc_DATA = LICENSE README.md lib_LTLIBRARIES = libe131.la libe131_la_SOURCES = src/e131.c libe131_la_LDFLAGS = -export-symbols-regex '^[eE]131_[^_]' include_HEADERS = src/e131.h # see: https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html libe131_la_LDFLAGS += -version-info 1:4:0 libe131-1.4.0/README.md000066400000000000000000000355231327630757200141570ustar00rootroot00000000000000# libE131: a lightweight C/C++ library for the E1.31 (sACN) protocol This is a lightweight C/C++ library that provides a simple API for packet, client and server programming to be used for communicating with devices implementing the E1.31 (sACN) protocol. Detailed information about E.131 (sACN) can be found on this [Wiki article][e131], from which most informational content about E1.31 in this document comes from. ACN is a suite of protocols (via Ethernet) that is the industry standard for lighting and control. ESTA, the creator of the standard, ratified a subset of this protocol for "lightweight" devices which is called sACN (E1.31). This lightweight version allows microcontroller based lighting equipment to communicate via ethernet, without all the overhead of the full ACN protocol. The simplest way to think about E1.31 is that it is a way to transport a large number of lighting control channels over a traditional ethernet network connection. E1.31 transports those channels in "Universes", which is a collection of up to 512 channels together. E.131 is ethernet based and is the data sent via UDP. The two accepted transport methods are multicast and unicast, the most common implementation is multicast. When using multicast all the controller needs to do is subscribe to the multicast address for the universe that you want to receive data from. [e131]: http://www.doityourselfchristmas.com/wiki/index.php?title=E1.31_(Streaming-ACN)_Protocol ## Installation To install libE131 in your system, download the [latest release archive][releases] and use the standard autotools approach: $ ./configure --prefix=/usr $ make $ sudo make install The last step requires `root` privileges. You can uninstall the library using: $ sudo make uninstall [releases]: https://github.com/hhromic/libe131/releases/latest ## E1.31 (sACN) Packets E1.31 network packets are based on ACN and are composed of three layers: E1.31 Packet = ACN Root Layer + Framing Layer + Device Management Protocol (DMP) Layer All packet contents are transmitted in **network byte order (big endian)**. If you are accessing fields larger than one byte, you must read/write from/to them using the `ntohs`, `ntohl`, `htons` and `htonl` conversion functions. Fortunately, most of the time you will not need to manipulate those fields directly because libE131 provides convenience functions for the most common fields. See the API documentation section for more information. The following is the data union provided by libE131 for sending/receiving E1.31 packets: ``` typedef union { struct { struct { /* ACN Root Layer: 38 bytes */ uint16_t preamble_size; /* Preamble Size */ uint16_t postamble_size; /* Post-amble Size */ uint8_t acn_pid[12]; /* ACN Packet Identifier */ uint16_t flength; /* Flags (high 4 bits) & Length (low 12 bits) */ uint32_t vector; /* Layer Vector */ uint8_t cid[16]; /* Component Identifier (UUID) */ } __attribute__((packed)) root; struct { /* Framing Layer: 77 bytes */ uint16_t flength; /* Flags (high 4 bits) & Length (low 12 bits) */ uint32_t vector; /* Layer Vector */ uint8_t source_name[64]; /* User Assigned Name of Source (UTF-8) */ uint8_t priority; /* Packet Priority (0-200, default 100) */ uint16_t reserved; /* Reserved (should be always 0) */ uint8_t seq_number; /* Sequence Number (detect duplicates or out of order packets) */ uint8_t options; /* Options Flags (bit 7: preview data, bit 6: stream terminated) */ uint16_t universe; /* DMX Universe Number */ } __attribute__((packed)) frame; struct { /* Device Management Protocol (DMP) Layer: 523 bytes */ uint16_t flength; /* Flags (high 4 bits) / Length (low 12 bits) */ uint8_t vector; /* Layer Vector */ uint8_t type; /* Address Type & Data Type */ uint16_t first_addr; /* First Property Address */ uint16_t addr_inc; /* Address Increment */ uint16_t prop_val_cnt; /* Property Value Count (1 + number of slots) */ uint8_t prop_val[513]; /* Property Values (DMX start code + slots data) */ } __attribute__((packed)) dmp; } __attribute__((packed)); uint8_t raw[638]; /* raw buffer view: 638 bytes */ } e131_packet_t; ``` This union provides two ways to access the data of an E1.31 packet: 1. directly using the `raw` member (typically used for receiving data from the network) 2. structurally using the `root`, `frame` and `dmp` members (typically for processing a packet) You can easily create and initialize a new E1.31 packet to be used for sending using the `e131_pkt_init()` function. See the examples sections to see how this data structure is used with libE131. ### Framing Layer Options The library provides two convenience functions, `e131_get_option()` and `e131_set_option()` to manipulate the options flag in the Framing Layer of an E1.31 packet. The following is a description of the supported option constants: * `E131_OPT_TERMINATED`: the current packet is the last one in the stream. The receiver should stop processing further packets. * `E131_OPT_PREVIEW`: the data in the packet should be only used for preview purposes, e.g. console display, and not to drive live fixtures. See the examples sections to see how Framing Layer options are used with libE131. ### Packet Validation The library provides a convenience function, `e131_pkt_validate()`, to check if an E1.31 packet is valid to be processed by your application. This function returns a validation status from the `e131_error_t` enumeration. The following is a description of each available error constant: * `E131_ERR_NONE`: Success (no validation error detected, you can process the packet). * `E131_ERR_PREAMBLE_SIZE`: Invalid Preamble Size. * `E131_ERR_POSTAMBLE_SIZE`: Invalid Post-amble Size. * `E131_ERR_ACN_PID`: Invalid ACN Packet Identifier. * `E131_ERR_VECTOR_ROOT`: Invalid Root Layer Vector. * `E131_ERR_VECTOR_FRAME`: Invalid Framing Layer Vector. * `E131_ERR_VECTOR_DMP`: Invalid Device Management Protocol (DMP) Layer Vector. * `E131_ERR_TYPE_DMP`: Invalid DMP Address & Data Type. * `E131_ERR_FIRST_ADDR_DMP`: Invalid DMP First Address. * `E131_ERR_ADDR_INC_DMP`: Invalid DMP Address Increment. The above error descriptions are also programatically obtainable using the `e131_strerror()` function. See the API documentation section for more information. See the examples sections to see how the packet validation and error reporting functions are used with libE131. ## Unicast and Multicast Destinations Most E1.31 software and hardware can be set up to communicate via two transport methods: **Unicast** and **Multicast**. ### Unicast Transmission Unicast is a method of sending data across a network where two devices, the control PC and the lighting controller, are directly connected (or thru a network switch or router) and the channel control information meant for that specific controller is only sent to that controller. Unicast is a **point to point protocol** and the lighting channel information is only switched or routed to the device with the matching IP address. You must have unique IP address in each controller. Using Unicast the data packets are sent directly to the device instead of being broadcast across the entire subnet. ### Multicast Transmission Multicast is a method to send data across a network where a sender, typically a PC, broadcasts the data to all devices connected to the network subnet and the information about the channels are sent to all controllers connected to the network and every other device on the network. Multicast is a **point to multipoint broadcast** where the controllers need to listen to and only respond to information they are configured to use. Your PC sequencing software or streaming tool sends multicast packets with an address of `239.255..` where `UHB` is the Universe high byte and `LHB` is the Universe low byte. As an example, the address for universe `1` would be `239.255.0.1`. This is why using multicast addressing can be simpler to configure since this address is always the same for any device using that universe. Depending on the device and the number of universes of data sent it can swamp the device and possibly end up causing a loss of data. Note however that this is not an issue for most networks until you get into the dozens of universes so it's not an issue for most users. The data is received on multicast IP address not on the individual device IP address. ### Unicast vs. Multicast **Unicast:** * Allows more channels of data to controllers and bridges (commonly 12 Universes vs. 7 Universes for Multicast) * Channels can only be sent to one controller per Universe **Multicast:** * Allows less channels of data to controllers and bridges (commonly 7 Universes vs. 12 for Unicast) * Simpler network configuration since controllers don't need data address information * Channels can be mirrored on multiple controllers since the same Universe can be used by multiple controllers. ## E1.31 (sACN) Universes E1.31 transports lighting information in "Universes", which is a collection of up to 512 channels together. You can chose any universe number from **1-63999** and assign it to a block of channels in your sequencing software. While a Universe can contain up to 512 channels, it does not have to, and can be any number between 1-512 channels. ## libE131 API Documentation ### Public Library Constants * *const uint16_t* `E131_DEFAULT_PORT`: default network port for E1.31 UDP data (5568). * *const uint8_t* `E131_DEFAULT_PRIORITY`: default E1.31 packet priority (100). ### Public Library Functions The library provides a number of functions to help you develop clients and servers that communicate using the E1.31 protocol. The following is a descriptive list of all functions provided. See the examples sections to see how the most common API functions are used with libE131. * `int e131_socket(void)`: Create a socket file descriptor suitable for E1.31 communication. On success, a file descriptor for the new socket is returned. On error, -1 is returned, and `errno` is set appropriately. * `int e131_bind(int sockfd, const uint16_t port)`: Bind a socket file descriptor to a port number for E1.31 communication. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `int e131_unicast_dest(e131_addr_t *dest, const char *host, const uint16_t port)`: Initialize a unicast E1.31 destination using a host and port number. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `int e131_multicast_dest(e131_addr_t *dest, const uint16_t universe, const uint16_t port)`: Initialize a multicast E1.31 destination using a universe and port number. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `int e131_dest_str(char *str, const e131_addr_t *dest)`: Describe an E1.31 destination into a string (must be at least 22 bytes). On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `int e131_multicast_join(int sockfd, const uint16_t universe)`: Join a socket file descriptor to an E1.31 multicast group using a universe. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `int e131_pkt_init(e131_packet_t *packet, const uint16_t universe, const uint16_t num_slots)`: Initialize an E1.31 packet using a universe and a number of slots. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `bool e131_get_option(const e131_packet_t *packet, const e131_option_t option)`: Get the state of a framing option in an E1.31 packet. * `int e131_set_option(e131_packet_t *packet, const e131_option_t option, const bool state)`: Set the state of a framing option in an E1.31 packet. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `ssize_t e131_send(int sockfd, const e131_packet_t *packet, const e131_addr_t *dest)`: Send an E1.31 packet to a socket file descriptor using a destination. On success, the number of bytes sent is returned. On error, -1 is returned, and `errno` is set appropriately. * `ssize_t e131_recv(int sockfd, e131_packet_t *packet)`: Receive an E1.31 packet from a socket file descriptor. This function returns the number of bytes received, or -1 if an error occurred. In the event of an error, `errno` is set to indicate the error. * `e131_error_t e131_pkt_validate(const e131_packet_t *packet)`: Validate that an E1.31 packet is well-formed. See the Packet Validation section. * `bool e131_pkt_discard(const e131_packet_t *packet, const uint8_t last_seq_number)`: Check if an E1.31 packet should be discarded (sequence number out of order). This function uses the standard out-of-sequence detection algorithm defined in the E1.31 specification. * `int e131_pkt_dump(FILE *stream, const e131_packet_t *packet)`: Dump an E1.31 packet to a stream (i.e. stdout, stderr). The output is formatted for human readable output. On success, zero is returned. On error, -1 is returned, and `errno` is set appropriately. * `const char *e131_strdest(const e131_addr_t *dest)`: Return a string describing an E1.31 destination. * `const char *e131_strerror(const e131_error_t error)`: Return a string describing an E1.31 error. ## Example: Creating a Client Included in this repository is an example to create a simple E1.31 client (`examples/test_client.c`). Compile it using: gcc -Wall test_client.c -o test_client -le131 ## Example: Creating a Server Included in this repository is an example to create a simple E1.31 server (`examples/test_server.c`). Compile it using: gcc -Wall test_server.c -o test_server -le131 ## Example Projects using libE131 * [E1.31 Xterm256 Console Viewer][e131-viewer] * [E1.31 to AdaLight Bridge][e131-adalight-bridge] * [E1.31 to MQTT Bridge][e131-mqtt-bridge] * [MIDI to E1.31 Synthesizer][midi-e131-synth] Also check out the [Node.js port][e131-node] of libE131. [e131-viewer]: https://github.com/hhromic/e131-viewer [e131-adalight-bridge]: https://github.com/hhromic/e131-adalight-bridge [e131-mqtt-bridge]: https://github.com/hhromic/e131-mqtt-bridge [midi-e131-synth]: https://github.com/hhromic/midi-e131-synth [e131-node]: https://github.com/hhromic/e131-node ## License This software is under the **Apache License 2.0**. Licensed 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. libe131-1.4.0/autogen.sh000077500000000000000000000000511327630757200146650ustar00rootroot00000000000000#!/bin/sh autoreconf --install || exit 1 libe131-1.4.0/configure.ac000066400000000000000000000017171327630757200151640ustar00rootroot00000000000000# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) AC_INIT([libe131], [1.4.0], [hhromic@gmail.com]) AM_INIT_AUTOMAKE([foreign subdir-objects -Wall -Werror]) m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) LT_INIT AC_CONFIG_SRCDIR([src/e131.h]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_MACRO_DIRS([m4]) # Checks for programs. AC_PROG_CXX AC_PROG_AWK AC_PROG_CC AC_PROG_CPP AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET # Checks for libraries. # Checks for header files. AC_CHECK_HEADERS([arpa/inet.h inttypes.h netdb.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_CHECK_HEADER_STDBOOL AC_TYPE_INT8_T AC_TYPE_SIZE_T AC_TYPE_SSIZE_T AC_TYPE_UINT16_T AC_TYPE_UINT32_T AC_TYPE_UINT8_T # Checks for library functions. AC_CHECK_FUNCS([gethostbyname memset socket]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT libe131-1.4.0/examples/000077500000000000000000000000001327630757200145065ustar00rootroot00000000000000libe131-1.4.0/examples/test_client.c000066400000000000000000000021051327630757200171650ustar00rootroot00000000000000#include #include #include #include #include #include int main() { int sockfd; e131_packet_t packet; e131_addr_t dest; // create a socket for E1.31 if ((sockfd = e131_socket()) < 0) err(EXIT_FAILURE, "e131_socket"); // initialize the new E1.31 packet in universe 1 with 24 slots in preview mode e131_pkt_init(&packet, 1, 24); memcpy(&packet.frame.source_name, "E1.31 Test Client", 18); if (e131_set_option(&packet, E131_OPT_PREVIEW, true) < 0) err(EXIT_FAILURE, "e131_set_option"); // set remote system destination as unicast address if (e131_unicast_dest(&dest, "127.0.0.1", E131_DEFAULT_PORT) < 0) err(EXIT_FAILURE, "e131_unicast_dest"); // loop to send cycling levels for each slot uint8_t level = 0; for (;;) { for (size_t pos=0; pos<24; pos++) packet.dmp.prop_val[pos + 1] = level; level++; if (e131_send(sockfd, &packet, &dest) < 0) err(EXIT_FAILURE, "e131_send"); e131_pkt_dump(stderr, &packet); packet.frame.seq_number++; usleep(250000); } } libe131-1.4.0/examples/test_server.c000066400000000000000000000022101327630757200172120ustar00rootroot00000000000000#include #include #include #include #include int main() { int sockfd; e131_packet_t packet; e131_error_t error; uint8_t last_seq = 0x00; // create a socket for E1.31 if ((sockfd = e131_socket()) < 0) err(EXIT_FAILURE, "e131_socket"); // bind the socket to the default E1.31 port and join multicast group for universe 1 if (e131_bind(sockfd, E131_DEFAULT_PORT) < 0) err(EXIT_FAILURE, "e131_bind"); if (e131_multicast_join(sockfd, 1) < 0) err(EXIT_FAILURE, "e131_multicast_join"); // loop to receive E1.31 packets fprintf(stderr, "waiting for E1.31 packets ...\n"); for (;;) { if (e131_recv(sockfd, &packet) < 0) err(EXIT_FAILURE, "e131_recv"); if ((error = e131_pkt_validate(&packet)) != E131_ERR_NONE) { fprintf(stderr, "e131_pkt_validate: %s\n", e131_strerror(error)); continue; } if (e131_pkt_discard(&packet, last_seq)) { fprintf(stderr, "warning: packet out of order received\n"); last_seq = packet.frame.seq_number; continue; } e131_pkt_dump(stderr, &packet); last_seq = packet.frame.seq_number; } } libe131-1.4.0/src/000077500000000000000000000000001327630757200134575ustar00rootroot00000000000000libe131-1.4.0/src/e131.c000066400000000000000000000263121327630757200143000ustar00rootroot00000000000000/** * E1.31 (sACN) library for C/C++ * Hugo Hromic - http://github.com/hhromic * * Some content of this file is based on: * https://github.com/forkineye/E131/blob/master/E131.h * https://github.com/forkineye/E131/blob/master/E131.cpp * * Copyright 2016 Hugo Hromic * * Licensed 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. */ #include #include #include #include #include #include #include "e131.h" /* E1.31 Public Constants */ const uint16_t E131_DEFAULT_PORT = 5568; const uint8_t E131_DEFAULT_PRIORITY = 0x64; /* E1.31 Private Constants */ const uint16_t _E131_PREAMBLE_SIZE = 0x0010; const uint16_t _E131_POSTAMBLE_SIZE = 0x0000; const uint8_t _E131_ACN_PID[] = {0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e, 0x31, 0x37, 0x00, 0x00, 0x00}; const uint32_t _E131_ROOT_VECTOR = 0x00000004; const uint32_t _E131_FRAME_VECTOR = 0x00000002; const uint8_t _E131_DMP_VECTOR = 0x02; const uint8_t _E131_DMP_TYPE = 0xa1; const uint16_t _E131_DMP_FIRST_ADDR = 0x0000; const uint16_t _E131_DMP_ADDR_INC = 0x0001; /* Create a socket file descriptor suitable for E1.31 communication */ int e131_socket(void) { return socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); } /* Bind a socket file descriptor to a port number for E1.31 communication */ int e131_bind(int sockfd, const uint16_t port) { e131_addr_t addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); memset(addr.sin_zero, 0, sizeof addr.sin_zero); return bind(sockfd, (struct sockaddr *)&addr, sizeof addr); } /* Initialize a unicast E1.31 destination using a host and port number */ int e131_unicast_dest(e131_addr_t *dest, const char *host, const uint16_t port) { if (dest == NULL || host == NULL) { errno = EINVAL; return -1; } struct hostent *he = gethostbyname(host); if (he == NULL) { errno = EADDRNOTAVAIL; return -1; } dest->sin_family = AF_INET; dest->sin_addr = *(struct in_addr *)he->h_addr; dest->sin_port = htons(port); memset(dest->sin_zero, 0, sizeof dest->sin_zero); return 0; } /* Initialize a multicast E1.31 destination using a universe and port number */ int e131_multicast_dest(e131_addr_t *dest, const uint16_t universe, const uint16_t port) { if (dest == NULL || universe < 1 || universe > 63999) { errno = EINVAL; return -1; } dest->sin_family = AF_INET; dest->sin_addr.s_addr = htonl(0xefff0000 | universe); dest->sin_port = htons(port); memset(dest->sin_zero, 0, sizeof dest->sin_zero); return 0; } /* Describe an E1.31 destination into a string */ int e131_dest_str(char *str, const e131_addr_t *dest) { if (str == NULL || dest == NULL) { errno = EINVAL; return -1; } sprintf(str, "%s:%d", inet_ntoa(dest->sin_addr), ntohs(dest->sin_port)); return 0; } /* Join a socket file descriptor to an E1.31 multicast group using a universe */ int e131_multicast_join(int sockfd, const uint16_t universe) { if (universe < 1 || universe > 63999) { errno = EINVAL; return -1; } struct ip_mreqn mreq; mreq.imr_multiaddr.s_addr = htonl(0xefff0000 | universe); mreq.imr_address.s_addr = htonl(INADDR_ANY); mreq.imr_ifindex = 0; return setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof mreq); } /* Initialize an E1.31 packet using a universe and a number of slots */ int e131_pkt_init(e131_packet_t *packet, const uint16_t universe, const uint16_t num_slots) { if (packet == NULL || universe < 1 || universe > 63999 || num_slots < 1 || num_slots > 512) { errno = EINVAL; return -1; } // compute packet layer lengths uint16_t prop_val_cnt = num_slots + 1; uint16_t dmp_length = prop_val_cnt + sizeof packet->dmp - sizeof packet->dmp.prop_val; uint16_t frame_length = sizeof packet->frame + dmp_length; uint16_t root_length = sizeof packet->root.flength + sizeof packet->root.vector + sizeof packet->root.cid + frame_length; // clear packet memset(packet, 0, sizeof *packet); // set Root Layer values packet->root.preamble_size = htons(_E131_PREAMBLE_SIZE); packet->root.postamble_size = htons(_E131_POSTAMBLE_SIZE); memcpy(packet->root.acn_pid, _E131_ACN_PID, sizeof packet->root.acn_pid); packet->root.flength = htons(0x7000 | root_length); packet->root.vector = htonl(_E131_ROOT_VECTOR); // set Framing Layer values packet->frame.flength = htons(0x7000 | frame_length); packet->frame.vector = htonl(_E131_FRAME_VECTOR); packet->frame.priority = E131_DEFAULT_PRIORITY; packet->frame.universe = htons(universe); // set Device Management Protocol (DMP) Layer values packet->dmp.flength = htons(0x7000 | dmp_length); packet->dmp.vector = _E131_DMP_VECTOR; packet->dmp.type = _E131_DMP_TYPE; packet->dmp.first_addr = htons(_E131_DMP_FIRST_ADDR); packet->dmp.addr_inc = htons(_E131_DMP_ADDR_INC); packet->dmp.prop_val_cnt = htons(prop_val_cnt); return 0; } /* Get the state of a framing option in an E1.31 packet */ bool e131_get_option(const e131_packet_t *packet, const e131_option_t option) { if (packet != NULL && packet->frame.options & (1 << (option % 8))) return true; return false; } /* Set the state of a framing option in an E1.31 packet */ int e131_set_option(e131_packet_t *packet, const e131_option_t option, const bool state) { if (packet == NULL) { errno = EINVAL; return -1; } packet->frame.options ^= (-state ^ packet->frame.options) & (1 << (option % 8)); return 0; } /* Send an E1.31 packet to a socket file descriptor using a destination */ ssize_t e131_send(int sockfd, const e131_packet_t *packet, const e131_addr_t *dest) { if (packet == NULL || dest == NULL) { errno = EINVAL; return -1; } const size_t packet_length = sizeof packet->raw - sizeof packet->dmp.prop_val + htons(packet->dmp.prop_val_cnt); return sendto(sockfd, packet->raw, packet_length, 0, (const struct sockaddr *)dest, sizeof *dest); } /* Receive an E1.31 packet from a socket file descriptor */ ssize_t e131_recv(int sockfd, e131_packet_t *packet) { if (packet == NULL) { errno = EINVAL; return -1; } return recv(sockfd, packet->raw, sizeof packet->raw, 0); } /* Validate that an E1.31 packet is well-formed */ e131_error_t e131_pkt_validate(const e131_packet_t *packet) { if (packet == NULL) return E131_ERR_NULLPTR; if (ntohs(packet->root.preamble_size) != _E131_PREAMBLE_SIZE) return E131_ERR_PREAMBLE_SIZE; if (ntohs(packet->root.postamble_size) != _E131_POSTAMBLE_SIZE) return E131_ERR_POSTAMBLE_SIZE; if (memcmp(packet->root.acn_pid, _E131_ACN_PID, sizeof packet->root.acn_pid) != 0) return E131_ERR_ACN_PID; if (ntohl(packet->root.vector) != _E131_ROOT_VECTOR) return E131_ERR_VECTOR_ROOT; if (ntohl(packet->frame.vector) != _E131_FRAME_VECTOR) return E131_ERR_VECTOR_FRAME; if (packet->dmp.vector != _E131_DMP_VECTOR) return E131_ERR_VECTOR_DMP; if (packet->dmp.type != _E131_DMP_TYPE) return E131_ERR_TYPE_DMP; if (htons(packet->dmp.first_addr) != _E131_DMP_FIRST_ADDR) return E131_ERR_FIRST_ADDR_DMP; if (htons(packet->dmp.addr_inc) != _E131_DMP_ADDR_INC) return E131_ERR_ADDR_INC_DMP; return E131_ERR_NONE; } /* Check if an E1.31 packet should be discarded (sequence number out of order) */ bool e131_pkt_discard(const e131_packet_t *packet, const uint8_t last_seq_number) { if (packet == NULL) return true; int8_t seq_num_diff = packet->frame.seq_number - last_seq_number; if (seq_num_diff > -20 && seq_num_diff <= 0) return true; return false; } /* Dump an E1.31 packet to a stream (i.e. stdout, stderr) */ int e131_pkt_dump(FILE *stream, const e131_packet_t *packet) { if (stream == NULL || packet == NULL) { errno = EINVAL; return -1; } fprintf(stream, "[Root Layer]\n"); fprintf(stream, " Preamble Size .......... %" PRIu16 "\n", ntohs(packet->root.preamble_size)); fprintf(stream, " Post-amble Size ........ %" PRIu16 "\n", ntohs(packet->root.postamble_size)); fprintf(stream, " ACN Packet Identifier .. %s\n", packet->root.acn_pid); fprintf(stream, " Flags & Length ......... %" PRIu16 "\n", ntohs(packet->root.flength)); fprintf(stream, " Layer Vector ........... %" PRIu32 "\n", ntohl(packet->root.vector)); fprintf(stream, " Component Identifier ... "); for (size_t pos=0, total=sizeof packet->root.cid; posroot.cid[pos]); fprintf(stream, "\n"); fprintf(stream, "[Framing Layer]\n"); fprintf(stream, " Flags & Length ......... %" PRIu16 "\n", ntohs(packet->frame.flength)); fprintf(stream, " Layer Vector ........... %" PRIu32 "\n", ntohl(packet->frame.vector)); fprintf(stream, " Source Name ............ %s\n", packet->frame.source_name); fprintf(stream, " Packet Priority ........ %" PRIu8 "\n", packet->frame.priority); fprintf(stream, " Reserved ............... %" PRIu16 "\n", ntohs(packet->frame.reserved)); fprintf(stream, " Sequence Number ........ %" PRIu8 "\n", packet->frame.seq_number); fprintf(stream, " Options Flags .......... %" PRIu8 "\n", packet->frame.options); fprintf(stream, " DMX Universe Number .... %" PRIu16 "\n", ntohs(packet->frame.universe)); fprintf(stream, "[Device Management Protocol (DMP) Layer]\n"); fprintf(stream, " Flags & Length ......... %" PRIu16 "\n", ntohs(packet->dmp.flength)); fprintf(stream, " Layer Vector ........... %" PRIu8 "\n", packet->dmp.vector); fprintf(stream, " Address & Data Type .... %" PRIu8 "\n", packet->dmp.type); fprintf(stream, " First Address .......... %" PRIu16 "\n", ntohs(packet->dmp.first_addr)); fprintf(stream, " Address Increment ...... %" PRIu16 "\n", ntohs(packet->dmp.addr_inc)); fprintf(stream, " Property Value Count ... %" PRIu16 "\n", ntohs(packet->dmp.prop_val_cnt)); fprintf(stream, "[DMP Property Values]\n "); for (size_t pos=0, total=ntohs(packet->dmp.prop_val_cnt); posdmp.prop_val[pos]); fprintf(stream, "\n"); return 0; } /* Return a string describing an E1.31 error */ const char *e131_strerror(const e131_error_t error) { switch (error) { case E131_ERR_NONE: return "Success"; case E131_ERR_PREAMBLE_SIZE: return "Invalid Preamble Size"; case E131_ERR_POSTAMBLE_SIZE: return "Invalid Post-amble Size"; case E131_ERR_ACN_PID: return "Invalid ACN Packet Identifier"; case E131_ERR_VECTOR_ROOT: return "Invalid Root Layer Vector"; case E131_ERR_VECTOR_FRAME: return "Invalid Framing Layer Vector"; case E131_ERR_VECTOR_DMP: return "Invalid Device Management Protocol (DMP) Layer Vector"; case E131_ERR_TYPE_DMP: return "Invalid DMP Address & Data Type"; case E131_ERR_FIRST_ADDR_DMP: return "Invalid DMP First Address"; case E131_ERR_ADDR_INC_DMP: return "Invalid DMP Address Increment"; default: return "Unknown error"; } } libe131-1.4.0/src/e131.h000066400000000000000000000132651327630757200143100ustar00rootroot00000000000000/** * E1.31 (sACN) library for C/C++ * Hugo Hromic - http://github.com/hhromic * * Some content of this file is based on: * https://github.com/forkineye/E131/blob/master/E131.h * https://github.com/forkineye/E131/blob/master/E131.cpp * * Copyright 2016 Hugo Hromic * * Licensed 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. */ #ifndef _E131_H #define _E131_H #ifdef __cplusplus extern "C" { #endif #include #include #include #include #include /* E1.31 Public Constants */ extern const uint16_t E131_DEFAULT_PORT; extern const uint8_t E131_DEFAULT_PRIORITY; /* E1.31 Socket Address Type */ typedef struct sockaddr_in e131_addr_t; /* E1.31 Packet Type */ /* All packet contents shall be transmitted in network byte order (big endian) */ typedef union { struct { struct { /* ACN Root Layer: 38 bytes */ uint16_t preamble_size; /* Preamble Size */ uint16_t postamble_size; /* Post-amble Size */ uint8_t acn_pid[12]; /* ACN Packet Identifier */ uint16_t flength; /* Flags (high 4 bits) & Length (low 12 bits) */ uint32_t vector; /* Layer Vector */ uint8_t cid[16]; /* Component Identifier (UUID) */ } __attribute__((packed)) root; struct { /* Framing Layer: 77 bytes */ uint16_t flength; /* Flags (high 4 bits) & Length (low 12 bits) */ uint32_t vector; /* Layer Vector */ uint8_t source_name[64]; /* User Assigned Name of Source (UTF-8) */ uint8_t priority; /* Packet Priority (0-200, default 100) */ uint16_t reserved; /* Reserved (should be always 0) */ uint8_t seq_number; /* Sequence Number (detect duplicates or out of order packets) */ uint8_t options; /* Options Flags (bit 7: preview data, bit 6: stream terminated) */ uint16_t universe; /* DMX Universe Number */ } __attribute__((packed)) frame; struct { /* Device Management Protocol (DMP) Layer: 523 bytes */ uint16_t flength; /* Flags (high 4 bits) / Length (low 12 bits) */ uint8_t vector; /* Layer Vector */ uint8_t type; /* Address Type & Data Type */ uint16_t first_addr; /* First Property Address */ uint16_t addr_inc; /* Address Increment */ uint16_t prop_val_cnt; /* Property Value Count (1 + number of slots) */ uint8_t prop_val[513]; /* Property Values (DMX start code + slots data) */ } __attribute__((packed)) dmp; } __attribute__((packed)); uint8_t raw[638]; /* raw buffer view: 638 bytes */ } e131_packet_t; /* E1.31 Framing Options Type */ typedef enum { E131_OPT_TERMINATED = 6, E131_OPT_PREVIEW = 7, } e131_option_t; /* E1.31 Validation Errors Type */ typedef enum { E131_ERR_NONE, E131_ERR_NULLPTR, E131_ERR_PREAMBLE_SIZE, E131_ERR_POSTAMBLE_SIZE, E131_ERR_ACN_PID, E131_ERR_VECTOR_ROOT, E131_ERR_VECTOR_FRAME, E131_ERR_VECTOR_DMP, E131_ERR_TYPE_DMP, E131_ERR_FIRST_ADDR_DMP, E131_ERR_ADDR_INC_DMP, } e131_error_t; /* Create a socket file descriptor suitable for E1.31 communication */ extern int e131_socket(void); /* Bind a socket file descriptor to a port number for E1.31 communication */ extern int e131_bind(int sockfd, const uint16_t port); /* Initialize a unicast E1.31 destination using a host and port number */ extern int e131_unicast_dest(e131_addr_t *dest, const char *host, const uint16_t port); /* Initialize a multicast E1.31 destination using a universe and port number */ extern int e131_multicast_dest(e131_addr_t *dest, const uint16_t universe, const uint16_t port); /* Describe an E1.31 destination into a string (must be at least 22 bytes) */ extern int e131_dest_str(char *str, const e131_addr_t *dest); /* Join a socket file descriptor to an E1.31 multicast group using a universe */ extern int e131_multicast_join(int sockfd, const uint16_t universe); /* Initialize an E1.31 packet using a universe and a number of slots */ extern int e131_pkt_init(e131_packet_t *packet, const uint16_t universe, const uint16_t num_slots); /* Get the state of a framing option in an E1.31 packet */ extern bool e131_get_option(const e131_packet_t *packet, const e131_option_t option); /* Set the state of a framing option in an E1.31 packet */ extern int e131_set_option(e131_packet_t *packet, const e131_option_t option, const bool state); /* Send an E1.31 packet to a socket file descriptor using a destination */ extern ssize_t e131_send(int sockfd, const e131_packet_t *packet, const e131_addr_t *dest); /* Receive an E1.31 packet from a socket file descriptor */ extern ssize_t e131_recv(int sockfd, e131_packet_t *packet); /* Validate that an E1.31 packet is well-formed */ extern e131_error_t e131_pkt_validate(const e131_packet_t *packet); /* Check if an E1.31 packet should be discarded (sequence number out of order) */ extern bool e131_pkt_discard(const e131_packet_t *packet, const uint8_t last_seq_number); /* Dump an E1.31 packet to a stream (i.e. stdout, stderr) */ extern int e131_pkt_dump(FILE *stream, const e131_packet_t *packet); /* Return a string describing an E1.31 error */ extern const char *e131_strerror(const e131_error_t error); #ifdef __cplusplus } #endif #endif