Add plugin periodic function elisp skeleton
[vpp.git] / extras / emacs / plugin-periodic-skel.el
1 ;;; plugin-periodic-skel.el - periodic (process) node 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-periodic
19 "Insert a periodic node 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 '(setq capital-oh-en "ON")
25 "/*
26  * " plugin-name "_periodic.c - skeleton plug-in periodic function 
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
42 #include <vlib/vlib.h>
43 #include <vppinfra/error.h>
44 #include <" plugin-name "/" plugin-name ".h>
45
46 static void 
47 handle_event1 (" plugin-name "_main_t *pm, f64 now, uword event_data)
48 {
49   clib_warning (\"received " PLUGIN-NAME "_EVENT1\");
50 }
51                            
52 static void 
53 handle_event2 (" plugin-name "_main_t *pm, f64 now, uword event_data)
54 {
55   clib_warning (\"received " PLUGIN-NAME "_EVENT2\");
56 }
57                            
58 static void
59 handle_periodic_enable_disable (" plugin-name"_main_t *pm, f64 now, uword event_data)
60 {
61    clib_warning (\"Periodic timeouts now %s\", 
62      event_data ? \"enabled\" : \"disabled\");
63    pm->periodic_timer_enabled = event_data;
64 }
65
66 static void
67 handle_timeout (" plugin-name"_main_t *pm, f64 now)
68 {
69   clib_warning (\"timeout at %.2f\", now);
70 }
71
72 static uword
73 " plugin-name "_periodic_process (vlib_main_t * vm,
74                           vlib_node_runtime_t * rt, vlib_frame_t * f)
75 {
76   " plugin-name "_main_t *pm = &" plugin-name "_main;
77   f64 now;
78   f64 timeout = 10.0;
79   uword *event_data = 0;
80   uword event_type;
81   int i;
82
83   while (1)
84     {
85       if (pm->periodic_timer_enabled)
86         vlib_process_wait_for_event_or_clock (vm, timeout);
87       else
88         vlib_process_wait_for_event (vm);
89
90       now = vlib_time_now (vm);
91
92       event_type = vlib_process_get_events (vm, (uword **) & event_data);
93
94       switch (event_type)
95         {
96           /* Handle " PLUGIN-NAME "_EVENT1 */
97         case " PLUGIN-NAME "_EVENT1:
98           for (i = 0; i < vec_len (event_data); i++)
99             handle_event1 (pm, now, event_data[i]);
100           break;
101
102           /* Handle " PLUGIN-NAME "_EVENT2 */
103         case " PLUGIN-NAME"_EVENT2:
104           for (i = 0; i < vec_len (event_data); i++)
105             handle_event2 (pm, now, event_data[i]);
106           break;
107           /* Handle the periodic timer on/off event */
108         case " PLUGIN-NAME"_EVENT_PERIODIC_ENABLE_DISABLE:
109           for (i = 0; i < vec_len (event_data); i++)
110             handle_periodic_enable_disable (pm, now, event_data[i]);
111           break;
112
113           /* Handle periodic timeouts */
114         case ~0:
115           handle_timeout (pm, now);
116           break;
117         }
118       vec_reset_length (event_data);
119     }
120   return 0;                     /* or not */
121 }
122
123 /* *INDENT-OFF* */
124 VLIB_REGISTER_NODE ("plugin-name"_periodic_node) =
125 {
126   .function = " plugin-name "_periodic_process,
127   .type = VLIB_NODE_TYPE_PROCESS,
128   .name = \"" plugin-name "-periodic-process\",
129 };
130 /* *INDENT-ON* */
131
132 /*
133  * fd.io coding-style-patch-verification: " capital-oh-en "
134  *
135  * Local Variables:
136  * eval: (c-set-style \"gnu\")
137  * End:
138  */
139 ")