Ensure all M() and M2() second parameters are the message pointer.
[vpp.git] / build-root / emacs-lisp / plugin-test-skel.el
1 ;;; plugin-test-skel.el - vpp-api-test plug-in skeleton
2 ;;;
3 ;;; Copyright (c) 2016 Cisco and/or its affiliates.
4 ;;; Licensed under the Apache License, Version 2.0 (the "License");
5 ;;; you may not use this file except in compliance with the License.
6 ;;; You may obtain a copy of the License at:
7 ;;;
8 ;;;     http://www.apache.org/licenses/LICENSE-2.0
9 ;;;
10 ;;; Unless required by applicable law or agreed to in writing, software
11 ;;; distributed under the License is distributed on an "AS IS" BASIS,
12 ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ;;; See the License for the specific language governing permissions and
14 ;;; limitations under the License.
15
16 (require 'skeleton)
17
18 (define-skeleton skel-plugin-test
19 "Insert a plug-in vpp-api-test skeleton "
20 nil
21 '(if (not (boundp 'plugin-name))
22      (setq plugin-name (read-string "Plugin name: ")))
23 '(setq PLUGIN-NAME (upcase plugin-name))
24 "
25 /*
26  * " plugin-name ".c - skeleton vpp-api-test plug-in 
27  *
28  * Copyright (c) <current-year> <your-organization>
29  * Licensed under the Apache License, Version 2.0 (the \"License\");
30  * you may not use this file except in compliance with the License.
31  * You may obtain a copy of the License at:
32  *
33  *     http://www.apache.org/licenses/LICENSE-2.0
34  *
35  * Unless required by applicable law or agreed to in writing, software
36  * distributed under the License is distributed on an \"AS IS\" BASIS,
37  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38  * See the License for the specific language governing permissions and
39  * limitations under the License.
40  */
41 #include <vat/vat.h>
42 #include <vlibapi/api.h>
43 #include <vlibmemory/api.h>
44 #include <vlibsocket/api.h>
45 #include <vppinfra/error.h>
46
47 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
48
49 /* Declare message IDs */
50 #include <" plugin-name "/" plugin-name "_msg_enum.h>
51
52 /* define message structures */
53 #define vl_typedefs
54 #include <" plugin-name "/" plugin-name "_all_api_h.h> 
55 #undef vl_typedefs
56
57 /* declare message handlers for each api */
58
59 #define vl_endianfun             /* define message structures */
60 #include <" plugin-name "/" plugin-name "_all_api_h.h> 
61 #undef vl_endianfun
62
63 /* instantiate all the print functions we know about */
64 #define vl_print(handle, ...)
65 #define vl_printfun
66 #include <" plugin-name "/" plugin-name "_all_api_h.h> 
67 #undef vl_printfun
68
69 /* Get the API version number. */
70 #define vl_api_version(n,v) static u32 api_version=(v);
71 #include <" plugin-name "/" plugin-name "_all_api_h.h>
72 #undef vl_api_version
73
74
75 typedef struct {
76     /* API message ID base */
77     u16 msg_id_base;
78     vat_main_t *vat_main;
79 } " plugin-name "_test_main_t;
80
81 " plugin-name "_test_main_t " plugin-name "_test_main;
82
83 #define foreach_standard_reply_retval_handler   \\
84 _(" plugin-name "_enable_disable_reply)
85
86 #define _(n)                                            \\
87     static void vl_api_##n##_t_handler                  \\
88     (vl_api_##n##_t * mp)                               \\
89     {                                                   \\
90         vat_main_t * vam = " plugin-name "_test_main.vat_main;   \\
91         i32 retval = ntohl(mp->retval);                 \\
92         if (vam->async_mode) {                          \\
93             vam->async_errors += (retval < 0);          \\
94         } else {                                        \\
95             vam->retval = retval;                       \\
96             vam->result_ready = 1;                      \\
97         }                                               \\
98     }
99 foreach_standard_reply_retval_handler;
100 #undef _
101
102 /* 
103  * Table of message reply handlers, must include boilerplate handlers
104  * we just generated
105  */
106 #define foreach_vpe_api_reply_msg                                       \\
107 _(" PLUGIN-NAME "_ENABLE_DISABLE_REPLY, " plugin-name "_enable_disable_reply)
108
109
110 /* M: construct, but don't yet send a message */
111
112 #define M(T,t)                                                  \\
113 do {                                                            \\
114     vam->result_ready = 0;                                      \\
115     mp = vl_msg_api_alloc(sizeof(*mp));                         \\
116     memset (mp, 0, sizeof (*mp));                               \\
117     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \\
118     mp->client_index = vam->my_client_index;                    \\
119 } while(0);
120
121 #define M2(T,t,n)                                               \\
122 do {                                                            \\
123     vam->result_ready = 0;                                      \\
124     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \\
125     memset (mp, 0, sizeof (*mp));                               \\
126     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \\
127     mp->client_index = vam->my_client_index;                    \\
128 } while(0);
129
130 /* S: send a message */
131 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
132
133 /* W: wait for results, with timeout */
134 #define W                                       \\
135 do {                                            \\
136     timeout = vat_time_now (vam) + 1.0;         \\
137                                                 \\
138     while (vat_time_now (vam) < timeout) {      \\
139         if (vam->result_ready == 1) {           \\
140             return (vam->retval);               \\
141         }                                       \\
142     }                                           \\
143     return -99;                                 \\
144 } while(0);
145
146 static int api_" plugin-name "_enable_disable (vat_main_t * vam)
147 {
148     " plugin-name "_test_main_t * sm = &" plugin-name "_test_main;
149     unformat_input_t * i = vam->input;
150     f64 timeout;
151     int enable_disable = 1;
152     u32 sw_if_index = ~0;
153     vl_api_" plugin-name "_enable_disable_t * mp;
154
155     /* Parse args required to build the message */
156     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
157         if (unformat (i, \"%U\", unformat_sw_if_index, vam, &sw_if_index))
158             ;
159         else if (unformat (i, \"sw_if_index %d\", &sw_if_index))
160             ;
161         else if (unformat (i, \"disable\"))
162             enable_disable = 0;
163         else
164             break;
165     }
166     
167     if (sw_if_index == ~0) {
168         errmsg (\"missing interface name / explicit sw_if_index number \\n\");
169         return -99;
170     }
171     
172     /* Construct the API message */
173     M(" PLUGIN-NAME "_ENABLE_DISABLE, mp);
174     mp->sw_if_index = ntohl (sw_if_index);
175     mp->enable_disable = enable_disable;
176
177     /* send it... */
178     S;
179
180     /* Wait for a reply... */
181     W;
182 }
183
184 /* 
185  * List of messages that the api test plugin sends,
186  * and that the data plane plugin processes
187  */
188 #define foreach_vpe_api_msg \\
189 _(" plugin-name "_enable_disable, \"<intfc> [disable]\")
190
191 void vat_api_hookup (vat_main_t *vam)
192 {
193     " plugin-name "_test_main_t * sm = &" plugin-name "_test_main;
194     /* Hook up handlers for replies from the data plane plug-in */
195 #define _(N,n)                                                  \\
196     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \\
197                            #n,                                  \\
198                            vl_api_##n##_t_handler,              \\
199                            vl_noop_handler,                     \\
200                            vl_api_##n##_t_endian,               \\
201                            vl_api_##n##_t_print,                \\
202                            sizeof(vl_api_##n##_t), 1); 
203     foreach_vpe_api_reply_msg;
204 #undef _
205
206     /* API messages we can send */
207 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
208     foreach_vpe_api_msg;
209 #undef _    
210     
211     /* Help strings */
212 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
213     foreach_vpe_api_msg;
214 #undef _
215 }
216
217 clib_error_t * vat_plugin_register (vat_main_t *vam)
218 {
219   " plugin-name "_test_main_t * sm = &" plugin-name "_test_main;
220   u8 * name;
221
222   sm->vat_main = vam;
223
224   /* Ask the vpp engine for the first assigned message-id */
225   name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
226   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
227
228   if (sm->msg_id_base != (u16) ~0)
229     vat_api_hookup (vam);
230   
231   vec_free(name);
232   
233   return 0;
234 }
235 ")