Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
develop
Branches (12)
Tags (28)
develop
main
release-4.3.4-patches
release-4.3.4
release-4.3.3
release-4.3.2
release-4.3.1
release-4.3.0
release-4.2.1
release-4.2.0
riot/gnrc_sock_udp
dtls
v4.3.5-rc2
v4.3.5-rc1
v4.3.4a
v4.3.4
v4.3.3
v4.3.2
v4.3.2-rc2
v4.3.2-rc1
v4.3.1
v4.3.1-rc2
v4.3.1-rc1
v4.3.0
v4.3.0-rc4
v4.3.0-rc3
v4.3.0-rc2
v4.3.0-rc1
v4.2.1
v4.2.0
v4.2.0-rc4
v4.2.0-rc3
develop
Branches (12)
Tags (28)
develop
main
release-4.3.4-patches
release-4.3.4
release-4.3.3
release-4.3.2
release-4.3.1
release-4.3.0
release-4.2.1
release-4.2.0
riot/gnrc_sock_udp
dtls
v4.3.5-rc2
v4.3.5-rc1
v4.3.4a
v4.3.4
v4.3.3
v4.3.2
v4.3.2-rc2
v4.3.2-rc1
v4.3.1
v4.3.1-rc2
v4.3.1-rc1
v4.3.0
v4.3.0-rc4
v4.3.0-rc3
v4.3.0-rc2
v4.3.0-rc1
v4.2.1
v4.2.0
v4.2.0-rc4
v4.2.0-rc3
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
develop
Branches (12)
Tags (28)
develop
main
release-4.3.4-patches
release-4.3.4
release-4.3.3
release-4.3.2
release-4.3.1
release-4.3.0
release-4.2.1
release-4.2.0
riot/gnrc_sock_udp
dtls
v4.3.5-rc2
v4.3.5-rc1
v4.3.4a
v4.3.4
v4.3.3
v4.3.2
v4.3.2-rc2
v4.3.2-rc1
v4.3.1
v4.3.1-rc2
v4.3.1-rc1
v4.3.0
v4.3.0-rc4
v4.3.0-rc3
v4.3.0-rc2
v4.3.0-rc1
v4.2.1
v4.2.0
v4.2.0-rc4
v4.2.0-rc3
libcoap
/
examples
/
tiny.c
libcoap
/
examples
/
tiny.c
tiny.c 4.60 KB
Copy Edit Raw Blame History
/* tiny -- tiny sender
*
* Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <coap3/coap.h>
#define Nn 8 /* duplicate definition of N if built on sky motes */
#define ENCODE_HEADER_SIZE 4
#define HIBIT (1 << (Nn - 1))
#define EMASK ((1 << ENCODE_HEADER_SIZE) - 1)
#define MMASK ((1 << Nn) - 1 - EMASK)
#define MAX_VALUE ( (1 << Nn) - (1 << ENCODE_HEADER_SIZE) ) * (1 << ((1 << ENCODE_HEADER_SIZE) - 1))
#define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
/* ls and s must be integer variables */
/* #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls) */
COAP_STATIC_INLINE unsigned char
COAP_PSEUDOFP_ENCODE_8_4_DOWN(unsigned int v, int *ls) {
if (v < HIBIT)
return v;
*ls = coap_fls(v) - Nn;
return ((v >> *ls) & MMASK) + *ls;
}
#define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (s = (((v + ((1<<ENCODE_HEADER_SIZE<<ls)-1)) >> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls))
static int quit = 0;
/* SIGINT handler: set quit to 1 for graceful termination */
static void
handle_sigint(int signum COAP_UNUSED) {
quit = 1;
}
static coap_pdu_t *
make_pdu(coap_session_t *session, unsigned int value) {
coap_pdu_t *pdu;
unsigned char enc;
static unsigned char buf[20];
int len, ls;
if (!(pdu = coap_pdu_init(COAP_MESSAGE_NON, COAP_REQUEST_CODE_POST,
coap_new_message_id(session), COAP_DEFAULT_MTU)))
return NULL;
enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value, &ls);
len = sprintf((char *)buf, "%c%u", enc, COAP_PSEUDOFP_DECODE_8_4(enc));
coap_add_data(pdu, len, buf);
return pdu;
}
static void
usage(const char *program) {
const char *p;
p = strrchr(program, '/');
if (p)
program = ++p;
fprintf(stderr, "%s -- tiny fake sensor\n"
"(c) 2010 Olaf Bergmann <bergmann@tzi.org>\n\n"
"usage: %s [group address]\n"
"\n\nSends some fake sensor values to specified multicast group\n",
program, program);
}
static coap_session_t *
get_session(coap_context_t *ctx, const char *group) {
int s;
struct addrinfo hints;
struct addrinfo *result, *rp;
coap_session_t *session;
int hops = 16;
if (!ctx)
return NULL;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL;
s = getaddrinfo(group, NULL, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return NULL;
}
/* iterate through results until success */
for (rp = result; rp != NULL; rp = rp->ai_next) {
coap_address_t addr;
coap_address_init(&addr);
addr.size = rp->ai_addrlen;
memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen);
session = coap_new_client_session(ctx, NULL, &addr, COAP_PROTO_UDP);
if (!session)
continue;
if (coap_is_mcast(&addr)) {
/* set socket options for multicast */
if (!coap_mcast_set_hops(session, hops))
perror("setsockopt: IPV6_MULTICAST_HOPS");
}
freeaddrinfo(result);
return session;
}
fprintf(stderr, "no session available for group '%s'\n", group);
freeaddrinfo(result);
return NULL;
}
int
main(int argc, char **argv) {
struct timeval tv;
coap_pdu_t *pdu;
coap_session_t *session;
struct sigaction sa;
coap_context_t *ctx;
/* Initialize libcoap library */
coap_startup();
if (argc > 1 && strncmp(argv[1], "-h", 2) == 0) {
usage(argv[0]);
exit(1);
}
ctx = coap_new_context(NULL);
if (!ctx)
return -1;
session = get_session(ctx, argc > 1 ? argv[1] : "::1");
if (!session)
return -1;
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = handle_sigint;
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
/* So we do not exit on a SIGPIPE */
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
while (!quit) {
if (!(pdu = make_pdu(session, rand() & 0xfff)))
break;
coap_send(session, pdu);
tv.tv_sec = 5;
tv.tv_usec = 0;
select(0, 0, 0, 0, &tv);
}
coap_free_context(ctx);
coap_cleanup();
return 0;
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

No description
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hackos/libcoap.git
git@gitee.com:hackos/libcoap.git
hackos
libcoap
libcoap
develop
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /