\hypertarget{irsend_8cpp-example}{}\section{irsend.\+cpp}

\begin{DoxyCodeInclude}
\textcolor{comment}{/*}
\textcolor{comment}{}
\textcolor{comment}{  irsend -  application for sending IR-codes via lirc}
\textcolor{comment}{}
\textcolor{comment}{  Copyright (C) 1998 Christoph Bartelmus (lirc@bartelmus.de)}
\textcolor{comment}{}
\textcolor{comment}{  This program is free software; you can redistribute it and/or modify}
\textcolor{comment}{  it under the terms of the GNU General Public License as published by}
\textcolor{comment}{  the Free Software Foundation; either version 2 of the License, or}
\textcolor{comment}{  (at your option) any later version.}
\textcolor{comment}{}
\textcolor{comment}{  This program is distributed in the hope that it will be useful,}
\textcolor{comment}{  but WITHOUT ANY WARRANTY; without even the implied warranty of}
\textcolor{comment}{  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}
\textcolor{comment}{  GNU General Public License for more details.}
\textcolor{comment}{}
\textcolor{comment}{  You should have received a copy of the GNU General Public License}
\textcolor{comment}{  along with this program; if not, write to the Free Software}
\textcolor{comment}{  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA}
\textcolor{comment}{}
\textcolor{comment}{*/}

\textcolor{preprocessor}{#ifdef HAVE\_CONFIG\_H}
\textcolor{preprocessor}{#  include "config.h"}
\textcolor{preprocessor}{#endif}

\textcolor{preprocessor}{#include <getopt.h>}
\textcolor{preprocessor}{#include <errno.h>}
\textcolor{preprocessor}{#include <limits.h>}

\textcolor{preprocessor}{#include "\hyperlink{lirc__log_8h}{lirc\_log.h}"}
\textcolor{preprocessor}{#include "\hyperlink{lirc__client_8h}{lirc\_client.h}"}

\textcolor{keyword}{static} \textcolor{keyword}{const} \hyperlink{lirc__log_8h_abffd9aa4ab9bcfacdbb9d7728037114b}{logchannel\_t} logchannel = LOG\_APP;

\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char}* \textcolor{keyword}{const} help =
        \textcolor{stringliteral}{"\(\backslash\)nSynopsis:\(\backslash\)n"}
        \textcolor{stringliteral}{"    irsend [options] SEND\_ONCE remote code [code...]\(\backslash\)n"}
        \textcolor{stringliteral}{"    irsend [options] SEND\_START remote code\(\backslash\)n"}
        \textcolor{stringliteral}{"    irsend [options] SEND\_STOP remote code\(\backslash\)n"}
        \textcolor{stringliteral}{"    irsend [options] LIST remote\(\backslash\)n"}
        \textcolor{stringliteral}{"    irsend [options] SET\_TRANSMITTERS remote num [num...]\(\backslash\)n"}
        \textcolor{stringliteral}{"    irsend [options] SIMULATE \(\backslash\)"scancode repeat keysym remote\(\backslash\)"\(\backslash\)n"}
        \textcolor{stringliteral}{"Options:\(\backslash\)n"}
        \textcolor{stringliteral}{"    -h --help\(\backslash\)t\(\backslash\)t\(\backslash\)tdisplay usage summary\(\backslash\)n"}
        \textcolor{stringliteral}{"    -v --version\(\backslash\)t\(\backslash\)tdisplay version\(\backslash\)n"}
        \textcolor{stringliteral}{"    -d --device=device\(\backslash\)t\(\backslash\)tuse given lircd socket ["} \hyperlink{lirc__config_8h_a902b299db59c35938b222a983c56c439}{LIRCD} \textcolor{stringliteral}{"]\(\backslash\)n"}
        \textcolor{stringliteral}{"    -a --address=host[:port]\(\backslash\)tconnect to lircd at this address\(\backslash\)n"}
        \textcolor{stringliteral}{"    -# --count=n\(\backslash\)t\(\backslash\)tsend command n times\(\backslash\)n"};

\textcolor{keyword}{const} \textcolor{keywordtype}{char}* prog;

\textcolor{keywordtype}{int} send\_packet(\hyperlink{structlirc__cmd__ctx}{lirc\_cmd\_ctx}* ctx, \textcolor{keywordtype}{int} fd)
\{
        \textcolor{keywordtype}{int} r;

        \textcolor{keywordflow}{do} \{
                r = \hyperlink{group__lirc__client_ga888d275e1b0cf4988e44fb15f24d2a99}{lirc\_command\_run}(ctx, fd);
                \textcolor{keywordflow}{if} (r != 0 && r != EAGAIN)
                        fprintf(stderr,
                                \textcolor{stringliteral}{"Error running command: %s\(\backslash\)n"}, strerror(r));
        \} \textcolor{keywordflow}{while} (r == EAGAIN);
        \textcolor{keywordflow}{return} r == 0 ? 0 : -1;
\}

\textcolor{keywordtype}{void} reformat\_simarg(\textcolor{keywordtype}{char}* code, \textcolor{keywordtype}{char} buffer[])
\{
        \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} scancode;
        \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} repeat;
        \textcolor{keywordtype}{char} keysym[32];
        \textcolor{keywordtype}{char} remote[64];
        \textcolor{keywordtype}{char} trash[32];
        \textcolor{keywordtype}{int} r;

        r = sscanf(code, \textcolor{stringliteral}{"%x %x %32s %64s %32s"},
                   &scancode, &repeat, keysym, remote, trash);
        \textcolor{keywordflow}{if} (r != 4) \{
                fprintf(stderr, \textcolor{stringliteral}{"Bad simulate argument: %s\(\backslash\)n"}, code);
                exit(EXIT\_FAILURE);
        \}
        snprintf(buffer, \hyperlink{lirc__config_8h_aebdc7d8ca8e25ed8efc90bb88ef7ef5b}{PACKET\_SIZE}, \textcolor{stringliteral}{"%016x %02x %s %s"},
                 scancode, repeat, keysym, remote);
\}





\textcolor{keywordtype}{int} main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char}** argv)
\{
        \textcolor{keywordtype}{char}* directive;
        \textcolor{keywordtype}{char}* remote;
        \textcolor{keywordtype}{char}* code;
        \textcolor{keyword}{const} \textcolor{keywordtype}{char}* lircd = NULL;
        \textcolor{keywordtype}{char}* address = NULL;
        \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{short} port = \hyperlink{lirc__config_8h_a15a6d5db557b7e21b12d35323a84486d}{LIRC\_INET\_PORT};
        \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{long} count = 1;
        \textcolor{keywordtype}{int} fd;
        \textcolor{keywordtype}{char} buffer[\hyperlink{lirc__config_8h_aebdc7d8ca8e25ed8efc90bb88ef7ef5b}{PACKET\_SIZE} + 1];
        \textcolor{keywordtype}{int} r;
        \hyperlink{structlirc__cmd__ctx}{lirc\_cmd\_ctx} ctx;

        prog = \textcolor{stringliteral}{"irsend"};

        \textcolor{keywordflow}{while} (1) \{
                \textcolor{keywordtype}{int} c;
                \textcolor{keyword}{static} \textcolor{keyword}{struct }option long\_options[] = \{
                        \{ \textcolor{stringliteral}{"help"},    no\_argument,       NULL, \textcolor{charliteral}{'h'} \},
                        \{ \textcolor{stringliteral}{"version"}, no\_argument,       NULL, \textcolor{charliteral}{'v'} \},
                        \{ \textcolor{stringliteral}{"device"},  required\_argument, NULL, \textcolor{charliteral}{'d'} \},
                        \{ \textcolor{stringliteral}{"address"}, required\_argument, NULL, \textcolor{charliteral}{'a'} \},
                        \{ \textcolor{stringliteral}{"count"},   required\_argument, NULL, \textcolor{charliteral}{'#'} \},
                        \{ 0,         0,                 0,    0   \}
                \};
                c = getopt\_long(argc, argv, \textcolor{stringliteral}{"hvd:a:#:"}, long\_options, NULL);
                \textcolor{keywordflow}{if} (c == -1)
                        \textcolor{keywordflow}{break};
                \textcolor{keywordflow}{switch} (c) \{
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'h'}:
                        fputs(help, stdout);
                        \textcolor{keywordflow}{return} EXIT\_SUCCESS;
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'v'}:
                        printf(\textcolor{stringliteral}{"%s %s\(\backslash\)n"}, prog, VERSION);
                        \textcolor{keywordflow}{return} EXIT\_SUCCESS;
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'d'}:
                        lircd = optarg;
                        \textcolor{keywordflow}{break};
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'a'}:
                \{
                        \textcolor{keywordtype}{char}* p;
                        \textcolor{keywordtype}{char}* end;
                        \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{long} val;

                        address = strdup(optarg);
                        \textcolor{keywordflow}{if} (!address) \{
                                fprintf(stderr, \textcolor{stringliteral}{"%s: out of memory\(\backslash\)n"}, prog);
                                \textcolor{keywordflow}{return} EXIT\_FAILURE;
                        \}
                        p = strchr(address, \textcolor{charliteral}{':'});
                        \textcolor{keywordflow}{if} (p != NULL) \{
                                val = strtoul(p + 1, &end, 10);
                                \textcolor{keywordflow}{if} (!(*(p + 1)) || *end || val < 1 || val > USHRT\_MAX) \{
                                        fprintf(stderr, \textcolor{stringliteral}{"%s: invalid port number: %s\(\backslash\)n"}, prog, p + 1);
                                        \textcolor{keywordflow}{return} EXIT\_FAILURE;
                                \}
                                port = (\textcolor{keywordtype}{unsigned} short)val;
                                *p = 0;
                        \}
                        \textcolor{keywordflow}{break};
                \}
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'#'}:
                \{
                        \textcolor{keywordtype}{char}* end;

                        count = strtoul(optarg, &end, 10);
                        \textcolor{keywordflow}{if} (!*optarg || *end) \{
                                fprintf(stderr, \textcolor{stringliteral}{"%s: invalid count value: %s\(\backslash\)n"}, prog, optarg);
                                \textcolor{keywordflow}{return} EXIT\_FAILURE;
                        \}
                        \textcolor{keywordflow}{break};
                \}
                \textcolor{keywordflow}{default}:
                        \textcolor{keywordflow}{return} EXIT\_FAILURE;
                \}
        \}
        \textcolor{keywordflow}{if} (optind + 2 > argc) \{
                fprintf(stderr, \textcolor{stringliteral}{"%s: not enough arguments\(\backslash\)n"}, prog);
                \textcolor{keywordflow}{return} EXIT\_FAILURE;
        \}
        \textcolor{keywordflow}{if} (lircd == NULL)
                lircd = \hyperlink{lirc__config_8h_a902b299db59c35938b222a983c56c439}{LIRCD};
        \textcolor{keywordflow}{if} (address == NULL)
                fd = \hyperlink{group__lirc__client_ga13cf4c784df60143d2b171645f4e84c1}{lirc\_get\_local\_socket}(lircd ? lircd : NULL, 0);
        \textcolor{keywordflow}{else}
                fd = \hyperlink{group__lirc__client_ga81b8a02fdab2ef6d21930bbefabedf30}{lirc\_get\_remote\_socket}(address, port, 0);
        \textcolor{keywordflow}{if} (fd < 0) \{
                lircd = lircd ? lircd : getenv(\textcolor{stringliteral}{"LIRC\_SOCKET\_PATH"});
                lircd = lircd ? lircd : \hyperlink{lirc__config_8h_a902b299db59c35938b222a983c56c439}{LIRCD};
                \hyperlink{lirc__log_8c_abf031435b06bf0e8a87af6b2ad58f499}{perrorf}(\textcolor{stringliteral}{"Cannot open socket %s"}, lircd);
                exit(EXIT\_FAILURE);
        \}
        \textcolor{keywordflow}{if} (address)
                free(address);
        address = NULL;

        directive = argv[optind++];

        \textcolor{keywordflow}{if} (strcasecmp(directive, \textcolor{stringliteral}{"set\_transmitters"}) == 0) \{
                code = argv[optind++];
                \textcolor{keywordflow}{if} (strlen(directive) + strlen(code) + 2 < \hyperlink{lirc__config_8h_aebdc7d8ca8e25ed8efc90bb88ef7ef5b}{PACKET\_SIZE}) \{
                        sprintf(buffer, \textcolor{stringliteral}{"%s %s"}, directive, code);
                \} \textcolor{keywordflow}{else} \{
                        fprintf(stderr, \textcolor{stringliteral}{"%s: input too long\(\backslash\)n"}, prog);
                        exit(EXIT\_FAILURE);
                \}
                \textcolor{keywordflow}{while} (optind < argc) \{
                        code = argv[optind++];
                        \textcolor{keywordflow}{if} (strlen(buffer) + strlen(code) + 2 < \hyperlink{lirc__config_8h_aebdc7d8ca8e25ed8efc90bb88ef7ef5b}{PACKET\_SIZE}) \{
                                sprintf(buffer + strlen(buffer), \textcolor{stringliteral}{" %s"}, code);
                        \} \textcolor{keywordflow}{else} \{
                                fprintf(stderr, \textcolor{stringliteral}{"%s: input too long\(\backslash\)n"}, prog);
                                exit(EXIT\_FAILURE);
                        \}
                \}
                strcat(buffer, \textcolor{stringliteral}{"\(\backslash\)n"});
                \hyperlink{group__lirc__client_ga3787c58d32391a5b7c40337fdbee95fc}{lirc\_command\_init}(&ctx, buffer);
                \textcolor{keywordflow}{if} (send\_packet(&ctx, fd) == -1)
                        exit(EXIT\_FAILURE);
        \}
        \textcolor{keywordflow}{if} (strcasecmp(directive, \textcolor{stringliteral}{"simulate"}) == 0) \{
                code = argv[optind++];
                \textcolor{keywordflow}{if} (optind != argc) \{
                        fprintf(stderr, \textcolor{stringliteral}{"%s: invalid argument count\(\backslash\)n"}, prog);
                        exit(EXIT\_FAILURE);
                \}
                reformat\_simarg(code, buffer);
                r = \hyperlink{group__lirc__client_ga3787c58d32391a5b7c40337fdbee95fc}{lirc\_command\_init}(&ctx, \textcolor{stringliteral}{"%s %s\(\backslash\)n"}, directive, buffer);
                \textcolor{keywordflow}{if} (r != 0) \{
                        fprintf(stderr, \textcolor{stringliteral}{"%s: %s\(\backslash\)n"}, prog, strerror(r));
                        exit(EXIT\_FAILURE);
                \}
                \textcolor{keywordflow}{if} (send\_packet(&ctx, fd) == -1)
                        exit(EXIT\_FAILURE);
        \} \textcolor{keywordflow}{else} \{
                remote = argv[optind++];

                \textcolor{keywordflow}{if} (optind == argc) \{
                        fprintf(stderr, \textcolor{stringliteral}{"%s: not enough arguments\(\backslash\)n"}, prog);
                        exit(EXIT\_FAILURE);
                \}
                \textcolor{keywordflow}{while} (optind < argc) \{
                        code = argv[optind++];
                        \textcolor{keywordflow}{if} (strcasecmp(directive, \textcolor{stringliteral}{"SEND\_ONCE"}) == 0 && count > 1)
                                r = \hyperlink{group__lirc__client_ga3787c58d32391a5b7c40337fdbee95fc}{lirc\_command\_init}(&ctx, \textcolor{stringliteral}{"%s %s %s %lu\(\backslash\)n"},
                                                      directive, remote, code, count);
                        \textcolor{keywordflow}{else}
                                r = \hyperlink{group__lirc__client_ga3787c58d32391a5b7c40337fdbee95fc}{lirc\_command\_init}(&ctx, \textcolor{stringliteral}{"%s %s %s\(\backslash\)n"},
                                                      directive, remote, code);
                        \textcolor{keywordflow}{if} (r != 0) \{
                                fprintf(stderr, \textcolor{stringliteral}{"%s: input too long\(\backslash\)n"}, prog);
                                exit(EXIT\_FAILURE);
                        \}
                        \hyperlink{group__lirc__client_gace4d40f00918d8bdf9a4f6df0fb0155d}{lirc\_command\_reply\_to\_stdout}(&ctx);
                        \textcolor{keywordflow}{if} (send\_packet(&ctx, fd) == -1)
                                exit(EXIT\_FAILURE);
                \}
        \}
        close(fd);
        \textcolor{keywordflow}{return} EXIT\_SUCCESS;
\}
\end{DoxyCodeInclude}
 