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 */ |
| 23 | |
| 24 | #define M(T,t) \ |
| 25 | do { \ |
| 26 | vam->result_ready = 0; \ |
| 27 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \ |
| 28 | memset (mp, 0, sizeof (*mp)); \ |
| 29 | mp->_vl_msg_id = ntohs (VL_API_##T); \ |
| 30 | mp->client_index = vam->my_client_index; \ |
| 31 | } while(0); |
| 32 | |
| 33 | #define M2(T,t,n) \ |
| 34 | do { \ |
| 35 | vam->result_ready = 0; \ |
| 36 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)+(n)); \ |
| 37 | memset (mp, 0, sizeof (*mp)); \ |
| 38 | mp->_vl_msg_id = ntohs (VL_API_##T); \ |
| 39 | mp->client_index = vam->my_client_index; \ |
| 40 | } while(0); |
| 41 | |
| 42 | |
| 43 | /* S: send a message */ |
| 44 | #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp)) |
| 45 | |
| 46 | /* W: wait for results, with timeout */ |
| 47 | #define W \ |
| 48 | do { \ |
| 49 | timeout = vat_time_now (vam) + 1.0; \ |
| 50 | \ |
| 51 | while (vat_time_now (vam) < timeout) { \ |
| 52 | if (vam->result_ready == 1) { \ |
| 53 | return (vam->retval); \ |
| 54 | } \ |
| 55 | vat_suspend (vam->vlib_main, 1e-5); \ |
| 56 | } \ |
| 57 | return -99; \ |
| 58 | } while(0); |
| 59 | |
| 60 | /* W2: wait for results, with timeout */ |
| 61 | #define W2(body) \ |
| 62 | do { \ |
| 63 | timeout = vat_time_now (vam) + 1.0; \ |
| 64 | \ |
| 65 | while (vat_time_now (vam) < timeout) { \ |
| 66 | if (vam->result_ready == 1) { \ |
| 67 | (body); \ |
| 68 | return (vam->retval); \ |
| 69 | } \ |
| 70 | vat_suspend (vam->vlib_main, 1e-5); \ |
| 71 | } \ |
| 72 | return -99; \ |
| 73 | } while(0); |
| 74 | |
| 75 | |
| 76 | #endif /* __vat_helper_macros_h__ */ |