14 #include <sys/select.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
29 static unsigned char buf[20];
37 pdu->
hdr->
id = htons(
id++);
54 p = strrchr( program,
'/' );
58 fprintf( stderr,
"%s -- tiny fake sensor\n"
59 "(c) 2010 Olaf Bergmann <bergmann@tzi.org>\n\n"
60 "usage: %s [group address]\n"
61 "\n\nSends some fake sensor values to specified multicast group\n",
69 struct addrinfo hints;
70 struct addrinfo *result, *rp;
72 memset(&hints, 0,
sizeof(
struct addrinfo));
73 hints.ai_family = AF_UNSPEC;
74 hints.ai_socktype = SOCK_DGRAM;
75 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL;
77 s = getaddrinfo(node, port, &hints, &result);
79 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(s));
84 for (rp = result; rp != NULL; rp = rp->ai_next) {
92 fprintf(stderr,
"no context available for interface '%s'\n", node);
104 struct sockaddr_in6 dst;
107 if ( argc > 1 && strncmp(argv[1],
"-h", 2) == 0 ) {
116 id = rand() & INT_MAX;
118 memset(&dst, 0,
sizeof(
struct sockaddr_in6 ));
119 dst.sin6_family = AF_INET6;
120 inet_pton( AF_INET6, argc > 1 ? argv[1] :
"::1", &dst.sin6_addr );
123 if ( IN6_IS_ADDR_MULTICAST(&dst.sin6_addr) ) {
126 if ( setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
127 (
char *)&hops,
sizeof(hops) ) < 0 )
128 perror(
"setsockopt: IPV6_MULTICAST_HOPS");
134 if (! (pdu =
make_pdu( rand() & 0xfff ) ) )
137 coap_send( ctx, (
struct sockaddr *)&dst,
sizeof(dst), pdu );
140 tv.tv_sec = 5; tv.tv_usec = 0;
142 select( 0, 0, 0, 0, &tv );
#define COAP_REQUEST_POST
void usage(const char *program)
coap_pdu_t * make_pdu(unsigned int value)
coap_context_t * coap_new_context(const coap_address_t *listen_addr)
Creates a new coap_context_t object that will hold the CoAP stack status.
coap_context_t * get_context(const char *node, const char *port)
int main(int argc, char **argv)
Header structure for CoAP PDUs.
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
#define COAP_PSEUDOFP_DECODE_8_4(r)
#define COAP_DEFAULT_PORT
void coap_delete_pdu(coap_pdu_t *pdu)
void coap_free_context(coap_context_t *context)
coap_pdu_t * coap_new_pdu()
Creates a new CoAP PDU.
The CoAP stack's global state is stored in a coap_context_t object.
#define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v, ls)