Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
socket.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file socket.c
20 * @brief Loopback UDP implementation for USHER's local development transport.
21 * @details The fixed port permits the local bridge and test tools to find each other
22 * without configuration. All I/O is non-blocking so local consumers cannot delay the
23 * flight-controller UART event loop.
24 */
25
26#include <arpa/inet.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <netinet/in.h>
30#include <stdio.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/uio.h>
34#include <unistd.h>
35#include <string.h>
36#include "socket.h"
37
38
39#define SOCKET_PORT 32000
40
41static int socket_fd = -1;
42static struct sockaddr_in socket_server;
43
44int socket_init(int is_server)
45{
46 if (socket_fd >= 0) {
47 errno = EALREADY;
48 return -1;
49 }
50 // Initialize socket
51 if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
52 perror("Socket: socket");
53 return -1;
54 }
55 if (fcntl(socket_fd, F_SETFD, FD_CLOEXEC) != 0) {
57 socket_fd = -1;
58 return -1;
59 }
60 int status_flags = fcntl(socket_fd, F_GETFL);
61 if (status_flags < 0 || fcntl(socket_fd, F_SETFL, status_flags | O_NONBLOCK) != 0) {
63 socket_fd = -1;
64 return -1;
65 }
66
67 memset(&socket_server, 0, sizeof(socket_server));
68 socket_server.sin_family = AF_INET;
69 socket_server.sin_port = htons(SOCKET_PORT);
70 socket_server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
71
72 if (is_server) {
73 if (bind(socket_fd, (struct sockaddr *)&socket_server, sizeof(socket_server)) != 0) {
74 int bind_errno = errno;
75 if (bind_errno == EADDRINUSE) {
76 fprintf(stderr,
77 "USHER:\tanother USHER instance or payload listener already owns "
78 "127.0.0.1:%d\n"
79 "USHER:\tstop the existing process before starting another instance\n"
80 "USHER:\tfor the managed MORA service, use: "
81 "sudo -n systemctl stop usher.service\n",
83 } else {
84 fprintf(stderr, "USHER:\tfailed to bind local payload port 127.0.0.1:%d: %s\n",
85 SOCKET_PORT, strerror(bind_errno));
86 }
88 socket_fd = -1;
89 errno = bind_errno;
90 return -1;
91 }
92 }
93 return 0;
94}
95
96void socket_deinit(void)
97{
98 if (socket_fd >= 0) close(socket_fd);
99 socket_fd = -1;
100}
101
103{
104 return socket_fd;
105}
106
107int socket_recv(char *buffer, int len)
108{
109 if (!buffer || len <= 0) { errno = EINVAL; return -1; }
110 if (socket_fd < 0) { errno = ENOTCONN; return -1; }
111 struct iovec vector = {.iov_base = buffer, .iov_len = (size_t)len};
112 struct msghdr message = {
113 .msg_name = &socket_server,
114 .msg_namelen = sizeof(socket_server),
115 .msg_iov = &vector,
116 .msg_iovlen = 1
117 };
118 ssize_t received = recvmsg(socket_fd, &message, 0);
119 if (received >= 0 && (message.msg_flags & MSG_TRUNC) != 0) {
120 errno = EMSGSIZE;
121 return -1;
122 }
123 return (int)received;
124}
125
126void socket_send(char *buffer, int len)
127{
128 if (len < 0 || (!buffer && len)) { errno = EINVAL; return; }
129 if (socket_fd < 0) { errno = ENOTCONN; return; }
130 socklen_t slen = sizeof(socket_server);
131 sendto(socket_fd, buffer, len, 0, (struct sockaddr *)&socket_server, slen);
132}
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int socket_get_fd(void)
Return the active UDP descriptor for polling.
Definition socket.c:102
int socket_recv(char *buffer, int len)
Receive one datagram without blocking.
Definition socket.c:107
#define SOCKET_PORT
Definition socket.c:39
int socket_init(int is_server)
Create the loopback UDP endpoint and, for a server, claim USHER ownership.
Definition socket.c:44
void socket_deinit(void)
Close the owned UDP endpoint; repeated calls are harmless.
Definition socket.c:96
static struct sockaddr_in socket_server
Definition socket.c:42
void socket_send(char *buffer, int len)
Send one loopback datagram without blocking.
Definition socket.c:126
static int socket_fd
Definition socket.c:41
Local non-blocking UDP channel used by USHER's development pipeline.