Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * vat_helper_macros.h - collect api client helper macros in one place |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | #ifndef __vat_helper_macros_h__ |
| 20 | #define __vat_helper_macros_h__ |
| 21 | |
| 22 | /* M: construct, but don't yet send a message */ |
Jon Loeliger | 614e97d | 2017-01-31 13:47:08 -0600 | [diff] [blame] | 23 | #define M(T, mp) \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 24 | do { \ |
| 25 | vam->result_ready = 0; \ |
| 26 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \ |
| 27 | memset (mp, 0, sizeof (*mp)); \ |
| 28 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
| 29 | mp->client_index = vam->my_client_index; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 30 | } while(0); |
| 31 | |
Jon Loeliger | 614e97d | 2017-01-31 13:47:08 -0600 | [diff] [blame] | 32 | #define M2(T, mp, n) \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 33 | do { \ |
| 34 | vam->result_ready = 0; \ |
| 35 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)+(n)); \ |
| 36 | memset (mp, 0, sizeof (*mp)); \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 37 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 38 | mp->client_index = vam->my_client_index; \ |
| 39 | } while(0); |
| 40 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 41 | /* S: send a message */ |
Jon Loeliger | 7bc770c | 2017-01-31 14:03:33 -0600 | [diff] [blame] | 42 | #define S(mp) (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp)) |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 43 | |
| 44 | /* W: wait for results, with timeout */ |
Jon Loeliger | 56c7b01 | 2017-02-01 12:31:41 -0600 | [diff] [blame] | 45 | #define W(ret) \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 46 | do { \ |
Jon Loeliger | 1f9191f | 2017-01-31 15:27:19 -0600 | [diff] [blame] | 47 | f64 timeout = vat_time_now (vam) + 1.0; \ |
Filip Tehlar | 6fa5568 | 2017-02-07 13:29:47 +0100 | [diff] [blame] | 48 | ret = -99; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 49 | \ |
| 50 | while (vat_time_now (vam) < timeout) { \ |
| 51 | if (vam->result_ready == 1) { \ |
Jon Loeliger | 56c7b01 | 2017-02-01 12:31:41 -0600 | [diff] [blame] | 52 | ret = vam->retval; \ |
Filip Tehlar | 6fa5568 | 2017-02-07 13:29:47 +0100 | [diff] [blame] | 53 | break; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 54 | } \ |
| 55 | vat_suspend (vam->vlib_main, 1e-5); \ |
| 56 | } \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 57 | } while(0); |
| 58 | |
| 59 | /* W2: wait for results, with timeout */ |
Jon Loeliger | 56c7b01 | 2017-02-01 12:31:41 -0600 | [diff] [blame] | 60 | #define W2(ret, body) \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 61 | do { \ |
Jon Loeliger | 1f9191f | 2017-01-31 15:27:19 -0600 | [diff] [blame] | 62 | f64 timeout = vat_time_now (vam) + 1.0; \ |
Filip Tehlar | 6fa5568 | 2017-02-07 13:29:47 +0100 | [diff] [blame] | 63 | ret = -99; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 64 | \ |
| 65 | while (vat_time_now (vam) < timeout) { \ |
| 66 | if (vam->result_ready == 1) { \ |
| 67 | (body); \ |
Jon Loeliger | 56c7b01 | 2017-02-01 12:31:41 -0600 | [diff] [blame] | 68 | ret = vam->retval; \ |
Filip Tehlar | 6fa5568 | 2017-02-07 13:29:47 +0100 | [diff] [blame] | 69 | break; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 70 | } \ |
| 71 | vat_suspend (vam->vlib_main, 1e-5); \ |
| 72 | } \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 73 | } while(0); |
| 74 | |
| 75 | |
| 76 | #endif /* __vat_helper_macros_h__ */ |