xbee_atcmd: add atcmd callback on response
[protos/xbee.git] / parse_atcmd.c
1 /*
2  * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <inttypes.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <netinet/in.h>
33
34 #include <cmdline_parse_ipaddr.h>
35 #include <cmdline_parse.h>
36
37 #include "xbee_atcmd.h"
38 #include "parse_atcmd.h"
39
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 */
43
44 static int
45 parse_atcmd(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
46             unsigned ressize)
47 {
48         struct token_atcmd *tk2 = (struct token_atcmd *)tk;
49         struct token_atcmd_data *tkd = &tk2->atcmd_data;
50         struct xbee_atcmd *cmd;
51
52         if (res && ressize < sizeof(struct object *))
53                 return -1;
54
55         /* XXX should be related to an xbee device */
56         cmd = xbee_atcmd_lookup_desc(buf);
57         if (cmd == NULL)
58                 return -1;
59
60         /* command must match flags */
61         if ((cmd->flags & tkd->atcmd_mask) != tkd->atcmd_flags)
62                 return -1;
63
64         /* store the address of object in structure */
65         if (res)
66                 *(struct xbee_atcmd **)res = cmd;
67
68         return 0;
69 }
70
71 static int
72 complete_atcmd_start(cmdline_parse_token_hdr_t *tk,
73                      __attribute__((unused)) const char *tokstr,
74                      void **opaque)
75 {
76         struct xbee_atcmd *cmd;
77
78         cmd = &xbee_atcmd_list[0];
79         *opaque = (void *)cmd;
80         return 0;
81 }
82
83 static int
84 complete_atcmd_iterate(cmdline_parse_token_hdr_t *tk, void **opaque,
85                        char *dstbuf, unsigned int size)
86 {
87         struct token_atcmd *tk2 = (struct token_atcmd *)tk;
88         struct token_atcmd_data *tkd = &tk2->atcmd_data;
89         struct xbee_atcmd *cmd;
90         int len;
91
92         cmd = *opaque;
93
94         while (1) {
95                 if (cmd->name == NULL)
96                         return -1;
97
98                 /* skip commands that don't match flags */
99                 if ((cmd->flags & tkd->atcmd_mask) != tkd->atcmd_flags) {
100                         cmd++;
101                         continue;
102                 }
103
104                 len = snprintf(dstbuf, size, "%s", cmd->desc);
105                 if (len < 0 || len >= size)
106                         return -1;
107
108                 strcpy(dstbuf, cmd->desc);
109                 break;
110         }
111         cmd++;
112         *opaque = cmd;
113         return 0;
114 }
115
116 static int
117 cmdline_help_atcmd(cmdline_parse_token_hdr_t *tk, char *dstbuf,
118                    unsigned int size)
119 {
120         snprintf(dstbuf, size, "ATCMD");
121         return 0;
122 }
123
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,
130 };