File: lcm-source.c

package info (click to toggle)
lcm 1.3.1%2Brepack1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,784 kB
  • sloc: ansic: 16,184; java: 6,843; cs: 2,266; cpp: 1,594; python: 989; makefile: 348; xml: 252; sh: 62
file content (175 lines) | stat: -rw-r--r-- 4,244 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// file: lcm_source.c
// desc: utility to basically spew garbage on the LC channels to use up 
//       bandwidth.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <winsock2.h>
#endif

#ifndef WIN32
#include <signal.h>
#endif

#include <getopt.h>
#include <time.h>

#include <lcm/lcm.h>

#define DEFAULT_TRANSMIT_INTERVAL_USEC 10000

static int g_quit = 0;

static void
make_msg_channel (char *buf, int maxlen) 
{
    char *cset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i;
    int ind;
    int nchars = strlen(cset);
    if (0 == maxlen) return;

    for (i=0; i<maxlen-1; i++) {
        ind = (int) (nchars * ((double)rand() / (RAND_MAX + 1.0)));
        buf[i] = cset[ind];
    }
    buf[maxlen-1] = 0;
}

static void 
usage()
{
    printf("usage: lcm_source [OPTIONS]\n"
           "\n"
           "periodically transmits LC messages.\n"
           "\n"
           "  -h     prints this help text and exits\n"
           "  -m s   transmits messages on channel s (otherwise random channel)\n"
           "  -p t   wait t microseconds between transmissions (default %d)\n"
           "  -s n   transmit packets of size n bytes.  If n is 0 (default),\n"
           "         then packets are randomly sized between 1 and 65,000\n"
           "         bytes\n"
           "  -v     verbose mode. Prints a summary of each packet\n",
           DEFAULT_TRANSMIT_INTERVAL_USEC);
    exit(1);
}

#ifndef WIN32
void on_signal(int signum)
{
    g_quit = 1;
}
#endif

int main(int argc, char **argv)
{
    int verbose = 0;
    int random_channel = 1;
    char channel[80];
    uint8_t *data = NULL;
    int transmit_interval_usec = DEFAULT_TRANSMIT_INTERVAL_USEC;
    int packet_sz = 0;
//    memset (data, 0, sizeof(data));

    char *optstring = "hm:p:s:v";
    char c;

#ifndef WIN32
    signal(SIGINT, on_signal);
#endif

    while ((c = getopt(argc, argv, optstring)) >= 0)
    {
        switch (c) {
            case 'h':
                usage();
                break;
            case 'm':
                random_channel = 0;
                strncpy (channel, optarg, sizeof(channel));
                break;
            case 'p':
                transmit_interval_usec = atoi(optarg);
                if (transmit_interval_usec < 0) {
                    usage();
                }
                break;
            case 's':
                packet_sz = atoi (optarg);
                if (packet_sz < 0) usage();
                break;
            case 'v':
                verbose = 1;
                break;
            default:
                usage();
                break;
        };
    }

    if (0 == packet_sz) {
        data = (uint8_t*) malloc(65536);
    } else {
        data = (uint8_t*) malloc(packet_sz);
        memset(data, 0, packet_sz);
    }

    lcm_t *lcm = lcm_create(NULL);
    if (! lcm) {
        fprintf(stderr, "couldn't allocate lcm_t\n");
        return 1;
    }

    srand(time(NULL));

    uint32_t seqno = 0;
    while(!g_quit) {
        int sz = 0;

        // pick a random message type if no fixed type was specified...
        if (random_channel) {
            make_msg_channel (channel, 10);
        }

        // pick a random or fixed packet size (depending on cmd-line option)
        if (0 == packet_sz) {
            sz = 1 + (int) (65000 * ((double)rand() / (RAND_MAX + 1.0)));
        } else {
            sz = packet_sz;
        }

        if (sz >= sizeof (seqno)) memcpy (data, &seqno, sizeof (seqno));

        // spew
        lcm_publish (lcm, channel, data, sz);

        if (verbose) {
            printf("packet type: [%s] size: %d\n", channel, sz);
        }

        if (transmit_interval_usec > 0) {
            // sleep...
#ifdef WIN32
            Sleep(transmit_interval_usec / 1000);
#else
            struct timespec ts = {
                .tv_sec = (int) (transmit_interval_usec / 1000000),
                .tv_nsec = (transmit_interval_usec % 1000000) * 1000
            };
            if (0 != nanosleep (&ts, NULL)) {
                perror ("nanosleep");
            }
#endif
        }
        seqno++;
    }
    printf("Transmitted %d messages\n", seqno);
    free(data);

    lcm_destroy (lcm);

    return 0;
}