mirror of
https://github.com/obgm/libcoap.git
synced 2026-10-27 21:22:33 +03:00
106 lines
2.1 KiB
C
106 lines
2.1 KiB
C
/* tiny -- tiny sender
|
|
*
|
|
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <ctype.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 "../coap.h"
|
|
|
|
coap_pdu_t *
|
|
make_pdu( unsigned int value ) {
|
|
coap_pdu_t *pdu;
|
|
unsigned char enc;
|
|
static char buf[20];
|
|
int len;
|
|
|
|
if ( ! ( pdu = coap_new_pdu() ) )
|
|
return NULL;
|
|
|
|
pdu->hdr->type = COAP_MESSAGE_NON;
|
|
pdu->hdr->code = COAP_REQUEST_POST;
|
|
|
|
enc = COAP_PSEUDOFP_ENCODE(value);
|
|
coap_add_data( pdu, 1, &enc);
|
|
|
|
len = snprintf(buf, 20, "%u", COAP_PSEUDOFP_DECODE(enc));
|
|
if ( len > 0 ) {
|
|
coap_add_data( pdu, len, buf );
|
|
}
|
|
|
|
return pdu;
|
|
}
|
|
|
|
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 );
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv) {
|
|
coap_context_t *ctx;
|
|
struct timeval tv;
|
|
coap_pdu_t *pdu;
|
|
struct sockaddr_in6 dst;
|
|
int hops = 4;
|
|
|
|
if ( argc > 1 && strncmp(argv[1], "-h", 2) == 0 ) {
|
|
usage( argv[0] );
|
|
exit( 1 );
|
|
}
|
|
|
|
ctx = coap_new_context();
|
|
if ( !ctx )
|
|
return -1;
|
|
|
|
memset(&dst, 0, sizeof(struct sockaddr_in6 ));
|
|
dst.sin6_family = AF_INET6;
|
|
inet_pton( AF_INET6, argc > 1 ? argv[1] : "::1", &dst.sin6_addr );
|
|
dst.sin6_port = htons( COAP_DEFAULT_PORT );
|
|
|
|
if ( IN6_IS_ADDR_MULTICAST(&dst.sin6_addr) ) {
|
|
/* set socket options for multicast */
|
|
|
|
if ( setsockopt( ctx->sockfd, SOL_SOCKET, IPV6_MULTICAST_HOPS,
|
|
(char *)&hops, sizeof(hops) ) < 0 )
|
|
perror("setsockopt: IPV6_MULTICAST_HOPS");
|
|
|
|
}
|
|
|
|
while ( 1 ) {
|
|
|
|
if (! (pdu = make_pdu( rand() & 0xfff ) ) )
|
|
return -1;
|
|
|
|
coap_send( ctx, &dst, pdu );
|
|
|
|
tv.tv_sec = 5; tv.tv_usec = 0;
|
|
|
|
select( 0, 0, 0, 0, &tv );
|
|
|
|
}
|
|
|
|
coap_free_context( ctx );
|
|
|
|
return 0;
|
|
}
|