summaryrefslogtreecommitdiff
path: root/src/examples/dlt-example-multicast-clientmsg-view.c
blob: aa3d5af29f9b64cc70042b857e20b989f5ad0ed6 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 2019 LG Electronics Inc.
 * SPDX-License-Identifier: MPL-2.0
 *
 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 */

/*!
 * \author
 * Guruprasad KN <guruprasad.kn@lge.com>
 * Sachin Sudhakar Shetty <sachin.shetty@lge.com>
 * Sunil Kovila Sampath <sunil.s@lge.com>
 *
 * \Copyright (c) 2019 LG Electronics Inc.
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt-example-multicast-clientmsg-view.c
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>      /* for isprint() */
#include <stdlib.h>     /* for atoi() */
#include <sys/stat.h>   /* for S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
#include <fcntl.h>      /* for open() */
#include <sys/uio.h>    /* for writev() */
#include <errno.h>
#include <string.h>
#include <glob.h>
#include <syslog.h>
#include <linux/limits.h> /* for PATH_MAX */
#include <inttypes.h>

#include "dlt_client.h"
#include "dlt_client_cfg.h"

#define DLT_RECEIVE_TEXTBUFSIZE 10024

#define HELLO_PORT 3491
#define HELLO_GROUP "225.0.0.37"

struct clientinfostruct
{
    int fd;
    struct sockaddr_in addr;
    socklen_t addlen;
    DltReceiver receiver;
};

int dlt_receiver_receive_socket_udp(struct clientinfostruct *clientinfo, DltReceiver *receiver)
{
    if ((receiver == NULL) || (clientinfo == NULL)) {
        printf("NULL receiver or clientinfo in dlt_receiver_receive_socket_udp\n");
        return -1;
    }

    if (receiver->buffer == NULL) {
        printf("NULL receiver->buffer in dlt_receiver_receive_socket_udp\n");
        return -1;
    }

    receiver->buf = (char *)receiver->buffer;
    receiver->lastBytesRcvd = receiver->bytesRcvd;

    /* wait for data from socket */
    unsigned int addrlen = sizeof(clientinfo->addr);

    if ((receiver->bytesRcvd = recvfrom(clientinfo->fd,
                                        receiver->buf + receiver->lastBytesRcvd,
                                        receiver->buffersize - receiver->lastBytesRcvd,
                                        0,
                                        (struct sockaddr *)&(clientinfo->addr), &addrlen))
        <= 0) {
        printf("Error\n");
        perror("recvfrom");
        receiver->bytesRcvd = 0;
        return receiver->bytesRcvd;
    } /* if */

    receiver->totalBytesRcvd += receiver->bytesRcvd;
    receiver->bytesRcvd += receiver->lastBytesRcvd;

    return receiver->bytesRcvd;
}

int dlt_receive_message_callback_udp(DltMessage *message)
{
    static char text[DLT_RECEIVE_TEXTBUFSIZE];

    if ((message == NULL)) {
        printf("NULL message in dlt_receive_message_callback_udp\n");
        return -1;
    }

    /* prepare storage header */
    if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
        dlt_set_storageheader(message->storageheader, message->headerextra.ecu);
    else
        dlt_set_storageheader(message->storageheader, "ECU1");

    dlt_message_header(message, text, DLT_RECEIVE_TEXTBUFSIZE, 0);

    printf("%s ", text);

    dlt_message_payload(message, text, DLT_RECEIVE_TEXTBUFSIZE, DLT_OUTPUT_ASCII, 0);

    printf("[%s]\n", text);

    return 0;
}


int main()
{
    struct clientinfostruct clientinfo;
    struct ip_mreq mreq;

    u_int yes = 1;

    /* create what looks like an ordinary UDP socket */
    if ((clientinfo.fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    /* allow multiple sockets to use the same PORT number */
    if (setsockopt(clientinfo.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
        perror("Reusing ADDR failed");
        exit(1);
    }

    /* set up destination address */
    memset(&clientinfo.addr, 0, sizeof(clientinfo.addr));
    clientinfo.addr.sin_family = AF_INET;
    clientinfo.addr.sin_addr.s_addr = htonl(INADDR_ANY); /* N.B.: differs from sender */
    clientinfo.addr.sin_port = htons(HELLO_PORT);

    /* bind to receive address */
    if (bind(clientinfo.fd, (struct sockaddr *)&clientinfo.addr, sizeof(clientinfo.addr)) < 0) {
        perror("bind");
        exit(1);
    }

    /* use setsockopt() to request that the kernel join a multicast group */
    mreq.imr_multiaddr.s_addr = inet_addr(HELLO_GROUP);
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);

    if (setsockopt(clientinfo.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
        perror("setsockopt");
        exit(1);
    }

    DltMessage msg;

    if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
        return DLT_RETURN_ERROR;

    if (dlt_receiver_init(&(clientinfo.receiver), clientinfo.fd,
                          DLT_RECEIVE_BUFSIZE) != DLT_RETURN_OK)
        return DLT_RETURN_ERROR;

    printf("Waiting for message on ip %s port : %d\n", HELLO_GROUP, HELLO_PORT);

    while (1) {
        /* wait for data from socket */
        dlt_receiver_receive_socket_udp(&clientinfo, &(clientinfo.receiver));

        while (dlt_message_read(&msg, (unsigned char *)(clientinfo.receiver.buf),
                                clientinfo.receiver.bytesRcvd, 0, 0) == DLT_MESSAGE_ERROR_OK) {
            dlt_receive_message_callback_udp(&msg);

            if (dlt_receiver_remove(&(clientinfo.receiver),
                                    msg.headersize + msg.datasize - sizeof(DltStorageHeader))
                == DLT_RETURN_ERROR) {
                /* Return value ignored */
                dlt_message_free(&msg, 0);
                return DLT_RETURN_ERROR;
            }
        }

        if (dlt_receiver_move_to_begin(&(clientinfo.receiver)) == DLT_RETURN_ERROR) {
            /* Return value ignored */
            dlt_message_free(&msg, 0);
            return DLT_RETURN_ERROR;
        }
    }
}