2 * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <netinet/in.h>
34 #include <cmdline_parse_ipaddr.h>
35 #include <cmdline_parse.h>
37 #include "xbee_atcmd.h"
38 #include "parse_atcmd.h"
40 /* This file is an example of extension of libcmdline. It provides an
41 * example of named objects stored in a list, supporting the
42 * completion on objects name */
45 parse_atcmd(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
48 struct token_atcmd *tk2 = (struct token_atcmd *)tk;
49 struct token_atcmd_data *tkd = &tk2->atcmd_data;
50 struct xbee_atcmd *cmd;
52 if (res && ressize < sizeof(struct object *))
55 /* XXX should be related to an xbee device */
56 cmd = xbee_atcmd_lookup_desc(buf);
60 /* command must match flags */
61 if ((cmd->flags & tkd->atcmd_mask) != tkd->atcmd_flags)
64 /* store the address of object in structure */
66 *(struct xbee_atcmd **)res = cmd;
72 complete_atcmd_start(cmdline_parse_token_hdr_t *tk,
73 __attribute__((unused)) const char *tokstr,
76 struct xbee_atcmd *cmd;
78 cmd = &xbee_atcmd_list[0];
79 *opaque = (void *)cmd;
84 complete_atcmd_iterate(cmdline_parse_token_hdr_t *tk, void **opaque,
85 char *dstbuf, unsigned int size)
87 struct token_atcmd *tk2 = (struct token_atcmd *)tk;
88 struct token_atcmd_data *tkd = &tk2->atcmd_data;
89 struct xbee_atcmd *cmd;
95 if (cmd->name == NULL)
98 /* skip commands that don't match flags */
99 if ((cmd->flags & tkd->atcmd_mask) != tkd->atcmd_flags) {
104 len = snprintf(dstbuf, size, "%s", cmd->desc);
105 if (len < 0 || len >= size)
108 strcpy(dstbuf, cmd->desc);
117 cmdline_help_atcmd(cmdline_parse_token_hdr_t *tk, char *dstbuf,
120 snprintf(dstbuf, size, "ATCMD");
124 struct cmdline_token_ops token_atcmd_ops = {
125 .parse = parse_atcmd,
126 .complete_start = complete_atcmd_start,
127 .complete_iterate = complete_atcmd_iterate,
128 .complete_end = NULL,
129 .help = cmdline_help_atcmd,