|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Intel Corporation. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <logging/log.h> |
| 8 | +LOG_MODULE_REGISTER(net_pkt_sock_sample, LOG_LEVEL_DBG); |
| 9 | + |
| 10 | +#include <zephyr.h> |
| 11 | +#include <errno.h> |
| 12 | +#include <stdio.h> |
| 13 | + |
| 14 | +#include <net/socket.h> |
| 15 | +#include <net/ethernet.h> |
| 16 | + |
| 17 | +#define STACK_SIZE 1024 |
| 18 | +#define THREAD_PRIORITY K_PRIO_COOP(8) |
| 19 | +#define RECV_BUFFER_SIZE 1280 |
| 20 | +#define RAW_WAIT K_SECONDS(5) |
| 21 | + |
| 22 | +static struct packet_data packet; |
| 23 | + |
| 24 | +static struct k_sem quit_lock; |
| 25 | + |
| 26 | +struct packet_data { |
| 27 | + int sock; |
| 28 | + char recv_buffer[RECV_BUFFER_SIZE]; |
| 29 | + struct k_delayed_work send; |
| 30 | +}; |
| 31 | + |
| 32 | +static void process_packet(void); |
| 33 | +static void send_packet(void); |
| 34 | + |
| 35 | +K_THREAD_DEFINE(packet_thread_id, STACK_SIZE, |
| 36 | + process_packet, NULL, NULL, NULL, |
| 37 | + THREAD_PRIORITY, 0, K_FOREVER); |
| 38 | + |
| 39 | +/* Generated by http://www.lipsum.com/ |
| 40 | + * 2 paragraphs, 179 words, 1160 bytes of Lorem Ipsum |
| 41 | + */ |
| 42 | +const char lorem_ipsum[] = |
| 43 | + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque " |
| 44 | + "sodales lorem lorem, sed congue enim vehicula a. Sed finibus diam sed " |
| 45 | + "odio ultrices pharetra. Nullam dictum arcu ultricies turpis congue, " |
| 46 | + "vel venenatis turpis venenatis. Nam tempus arcu eros, ac congue libero " |
| 47 | + "tristique congue. Proin velit lectus, euismod sit amet quam in, " |
| 48 | + "maximus condimentum urna. Cras vel erat luctus, mattis orci ut, varius " |
| 49 | + "urna. Nam eu lobortis velit." |
| 50 | + "\n" |
| 51 | + "Nullam sit amet diam vel odio sodales cursus vehicula eu arcu. Proin " |
| 52 | + "fringilla, enim nec consectetur mollis, lorem orci interdum nisi, " |
| 53 | + "vitae suscipit nisi mauris eu mi. Proin diam enim, mollis ac rhoncus " |
| 54 | + "vitae, placerat et eros. Suspendisse convallis, ipsum nec rhoncus " |
| 55 | + "aliquam, ex augue ultrices nisl, id aliquet mi diam quis ante. " |
| 56 | + "Pellentesque venenatis ornare ultrices. Quisque et porttitor lectus. " |
| 57 | + "Ut venenatis nunc et urna imperdiet porttitor non laoreet massa. Donec " |
| 58 | + "eleifend eros in mi sagittis egestas. Sed et mi nunc. Nunc vulputate, " |
| 59 | + "mauris non ullamcorper viverra, lorem nulla vulputate diam, et congue " |
| 60 | + "dui velit non erat. Duis interdum leo et ipsum tempor consequat. In " |
| 61 | + "faucibus enim quis purus vulputate nullam." |
| 62 | + "\n"; |
| 63 | + |
| 64 | +static void quit(void) |
| 65 | +{ |
| 66 | + k_sem_give(&quit_lock); |
| 67 | +} |
| 68 | + |
| 69 | +static int start_packet_socket(void) |
| 70 | +{ |
| 71 | + struct sockaddr_ll dst; |
| 72 | + int ret; |
| 73 | + |
| 74 | + packet.sock = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL); |
| 75 | + if (packet.sock < 0) { |
| 76 | + LOG_ERR("Failed to create RAW socket : %d", errno); |
| 77 | + return -errno; |
| 78 | + } |
| 79 | + |
| 80 | + dst.sll_ifindex = 0; |
| 81 | + dst.sll_family = AF_PACKET; |
| 82 | + |
| 83 | + ret = bind(packet.sock, (const struct sockaddr *)&dst, |
| 84 | + sizeof(struct sockaddr_ll)); |
| 85 | + if (ret < 0) { |
| 86 | + LOG_ERR("Failed to bind packet socket : %d", errno); |
| 87 | + return -errno; |
| 88 | + } |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +static void wait_send(struct k_work *work) |
| 94 | +{ |
| 95 | + /* Send a new packet at this point */ |
| 96 | + send_packet(); |
| 97 | +} |
| 98 | + |
| 99 | +static void send_packet(void) |
| 100 | +{ |
| 101 | + struct sockaddr_ll dst; |
| 102 | + u8_t send = 100; |
| 103 | + int ret; |
| 104 | + |
| 105 | + dst.sll_ifindex = 0; |
| 106 | + |
| 107 | + /* Sending dummy data */ |
| 108 | + ret = sendto(packet.sock, lorem_ipsum, send, 0, |
| 109 | + (const struct sockaddr *)&dst, |
| 110 | + sizeof(struct sockaddr_ll)); |
| 111 | + if (ret < 0) { |
| 112 | + LOG_ERR("Failed to send, errno %d", errno); |
| 113 | + } else { |
| 114 | + LOG_DBG("Sent %d bytes", send); |
| 115 | + } |
| 116 | + |
| 117 | + k_delayed_work_submit(&packet.send, RAW_WAIT); |
| 118 | +} |
| 119 | + |
| 120 | +static int process_packet_socket(void) |
| 121 | +{ |
| 122 | + int ret = 0; |
| 123 | + int received; |
| 124 | + |
| 125 | + LOG_INF("Waiting for packets ..."); |
| 126 | + |
| 127 | + send_packet(); |
| 128 | + |
| 129 | + do { |
| 130 | + received = recv(packet.sock, packet.recv_buffer, |
| 131 | + sizeof(packet.recv_buffer), 0); |
| 132 | + |
| 133 | + if (received < 0) { |
| 134 | + LOG_ERR("RAW : recv error %d", errno); |
| 135 | + ret = -errno; |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + LOG_DBG("Received %d bytes", received); |
| 140 | + |
| 141 | + } while (true); |
| 142 | + |
| 143 | + return ret; |
| 144 | +} |
| 145 | + |
| 146 | +static void process_packet(void) |
| 147 | +{ |
| 148 | + int ret; |
| 149 | + |
| 150 | + ret = start_packet_socket(); |
| 151 | + if (ret < 0) { |
| 152 | + quit(); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + while (ret == 0) { |
| 157 | + ret = process_packet_socket(); |
| 158 | + if (ret < 0) { |
| 159 | + quit(); |
| 160 | + return; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +void main(void) |
| 166 | +{ |
| 167 | + k_sem_init(&quit_lock, 0, UINT_MAX); |
| 168 | + k_delayed_work_init(&packet.send, wait_send); |
| 169 | + |
| 170 | + LOG_INF("Packet socket sample is running"); |
| 171 | + |
| 172 | + k_thread_start(packet_thread_id); |
| 173 | + |
| 174 | + k_sem_take(&quit_lock, K_FOREVER); |
| 175 | + |
| 176 | + LOG_INF("Stopping..."); |
| 177 | + |
| 178 | + k_thread_abort(packet_thread_id); |
| 179 | + |
| 180 | + k_delayed_work_cancel(&packet.send); |
| 181 | + |
| 182 | + if (packet.sock > 0) { |
| 183 | + (void)close(packet.sock); |
| 184 | + } |
| 185 | +} |
0 commit comments