From patchwork Mon Nov 21 20:34:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schmidt X-Patchwork-Id: 9440021 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2C48360469 for ; Mon, 21 Nov 2016 20:34:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E71328897 for ; Mon, 21 Nov 2016 20:34:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1195628898; Mon, 21 Nov 2016 20:34:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C5F428892 for ; Mon, 21 Nov 2016 20:34:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754835AbcKUUek (ORCPT ); Mon, 21 Nov 2016 15:34:40 -0500 Received: from proxima.lasnet.de ([78.47.171.185]:47098 "EHLO proxima.lasnet.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754751AbcKUUek (ORCPT ); Mon, 21 Nov 2016 15:34:40 -0500 Received: from localhost.localdomain (p20030048092FF37D9A8389FFFE20FF3F.dip0.t-ipconnect.de [IPv6:2003:48:92f:f37d:9a83:89ff:fe20:ff3f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: stefan@sostec.de) by proxima.lasnet.de (Postfix) with ESMTPSA id 38358C009F; Mon, 21 Nov 2016 21:34:38 +0100 (CET) From: Stefan Schmidt To: linux-wpan@vger.kernel.org Cc: Alexander Aring , Stefan Schmidt Subject: [PATCH wpan-tools 3/8] examples: af_ieee802154_rx example Date: Mon, 21 Nov 2016 21:34:16 +0100 Message-Id: <1479760461-18947-4-git-send-email-stefan@osg.samsung.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1479760461-18947-1-git-send-email-stefan@osg.samsung.com> References: <1479760461-18947-1-git-send-email-stefan@osg.samsung.com> Sender: linux-wpan-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wpan@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Show case receiving 802.15.4 frames over the socket. Signed-off-by: Stefan Schmidt --- examples/Makefile.am | 3 +- examples/af_ieee802154_rx.c | 111 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 examples/af_ieee802154_rx.c diff --git a/examples/Makefile.am b/examples/Makefile.am index 4ddb3b7..1ebedf9 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,3 +1,4 @@ -bin_PROGRAMS = af_ieee802154_tx +bin_PROGRAMS = af_ieee802154_tx \ + af_ieee802154_rx EXTRA_DIST = README.examples diff --git a/examples/af_ieee802154_rx.c b/examples/af_ieee802154_rx.c new file mode 100644 index 0000000..86ba707 --- /dev/null +++ b/examples/af_ieee802154_rx.c @@ -0,0 +1,111 @@ +/* + * IEEE 802.15.4 socket example + * + * Copyright (C) 2016 Samsung Electronics Co., Ltd. + * + * Author: Stefan Schmidt + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* gcc af_ieee802154_rx.c -o af_ieee802154_rx */ + +#include +#include +#include +#include +#include +#include + +#define IEEE802154_ADDR_LEN 8 +#define MAX_PACKET_LEN 127 +#define EXTENDED 1 + +enum { + IEEE802154_ADDR_NONE = 0x0, + IEEE802154_ADDR_SHORT = 0x2, + IEEE802154_ADDR_LONG = 0x3, +}; + +struct ieee802154_addr_sa { + int addr_type; + uint16_t pan_id; + union { + uint8_t hwaddr[IEEE802154_ADDR_LEN]; + uint16_t short_addr; + }; +}; + +struct sockaddr_ieee802154 { + sa_family_t family; + struct ieee802154_addr_sa addr; +}; + +int main(int argc, char *argv[]) { + int ret, sd; + struct sockaddr_ieee802154 src, dst; + unsigned char buf[MAX_PACKET_LEN + 1]; + socklen_t addrlen; + /* IEEE 802.15.4 extended address to receive frames on, adapt to your setup */ + uint8_t long_addr[IEEE802154_ADDR_LEN] = {0xd6, 0x55, 0x2c, 0xd6, 0xe4, 0x1c, 0xeb, 0x57}; + + /* Create IEEE 802.15.4 address family socket for the SOCK_DGRAM type */ + sd = socket(PF_IEEE802154, SOCK_DGRAM, 0); + if (sd < 0) { + perror("socket"); + return 1; + } + + /* Prepare source socket address struct */ + memset(&src, 0, sizeof(src)); + src.family = AF_IEEE802154; + /* Used PAN ID is 0x23 here, adapt to your setup */ + src.addr.pan_id = 0x0023; + +#if EXTENDED /* IEEE 802.15.4 extended address usage */ + src.addr.addr_type = IEEE802154_ADDR_LONG; + memcpy(&src.addr.hwaddr, &long_addr, IEEE802154_ADDR_LEN); +#else + src.addr.addr_type = IEEE802154_ADDR_SHORT; + src.addr.short_addr = 0x0002; +#endif + + /* Bind socket on this side */ + ret = bind(sd, (struct sockaddr *)&src, sizeof(src)); + if (ret) { + perror("bind"); + close(sd); + return 1; + } + + addrlen = sizeof(dst); + + /* Infinite loop receiving 802.15.4 frames and print out */ + while (1) { + ret = recvfrom(sd, buf, MAX_PACKET_LEN, 0, (struct sockaddr *)&dst, &addrlen); + if (ret < 0) { + perror("recvfrom"); + continue; + } + buf[ret] = '\0'; +#if EXTENDED + printf("Received (from %s): %s\n", dst.addr.hwaddr, buf); +#else + printf("Received (from %x): %s\n", dst.addr.short_addr, buf); +#endif + } + + shutdown(sd, SHUT_RDWR); + close(sd); + return 0; +}