| 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* From message-iterator-const.h */ |
| 26 | |
| 27 | typedef enum bt_message_iterator_status { |
| 28 | BT_MESSAGE_ITERATOR_STATUS_OK = 0, |
| 29 | BT_MESSAGE_ITERATOR_STATUS_END = 1, |
| 30 | BT_MESSAGE_ITERATOR_STATUS_AGAIN = 11, |
| 31 | BT_MESSAGE_ITERATOR_STATUS_ERROR = -1, |
| 32 | BT_MESSAGE_ITERATOR_STATUS_NOMEM = -12, |
| 33 | } bt_message_iterator_status; |
| 34 | |
| 35 | /* From self-message-iterator.h */ |
| 36 | |
| 37 | typedef enum bt_self_message_iterator_status { |
| 38 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK = BT_MESSAGE_ITERATOR_STATUS_OK, |
| 39 | BT_SELF_MESSAGE_ITERATOR_STATUS_END = BT_MESSAGE_ITERATOR_STATUS_END, |
| 40 | BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN = BT_MESSAGE_ITERATOR_STATUS_AGAIN, |
| 41 | BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR = BT_MESSAGE_ITERATOR_STATUS_ERROR, |
| 42 | BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM = BT_MESSAGE_ITERATOR_STATUS_NOMEM, |
| 43 | } bt_self_message_iterator_status; |
| 44 | |
| 45 | extern bt_self_component * |
| 46 | bt_self_message_iterator_borrow_component( |
| 47 | bt_self_message_iterator *message_iterator); |
| 48 | |
| 49 | extern bt_self_port_output * |
| 50 | bt_self_message_iterator_borrow_port( |
| 51 | bt_self_message_iterator *message_iterator); |
| 52 | |
| 53 | extern void bt_self_message_iterator_set_data( |
| 54 | bt_self_message_iterator *message_iterator, |
| 55 | void *user_data); |
| 56 | |
| 57 | extern void *bt_self_message_iterator_get_data( |
| 58 | const bt_self_message_iterator *message_iterator); |
| 59 | |
| 60 | /* From self-component-port-input-message-iterator.h */ |
| 61 | |
| 62 | bt_message_iterator * |
| 63 | bt_self_component_port_input_message_iterator_as_message_iterator( |
| 64 | bt_self_component_port_input_message_iterator *iterator); |
| 65 | |
| 66 | extern bt_self_component_port_input_message_iterator * |
| 67 | bt_self_component_port_input_message_iterator_create( |
| 68 | bt_self_component_port_input *input_port); |
| 69 | |
| 70 | extern bt_component * |
| 71 | bt_self_component_port_input_message_iterator_borrow_component( |
| 72 | bt_self_component_port_input_message_iterator *iterator); |
| 73 | |
| 74 | extern bt_message_iterator_status |
| 75 | bt_self_component_port_input_message_iterator_next( |
| 76 | bt_self_component_port_input_message_iterator *iterator, |
| 77 | bt_message_array_const *msgs, uint64_t *count); |
| 78 | |
| 79 | extern bt_bool |
| 80 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 81 | bt_self_component_port_input_message_iterator *iterator, |
| 82 | int64_t ns_from_origin); |
| 83 | |
| 84 | extern bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 85 | bt_self_component_port_input_message_iterator *iterator); |
| 86 | |
| 87 | extern bt_message_iterator_status |
| 88 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( |
| 89 | bt_self_component_port_input_message_iterator *iterator, |
| 90 | int64_t ns_from_origin); |
| 91 | |
| 92 | extern bt_message_iterator_status |
| 93 | bt_self_component_port_input_message_iterator_seek_beginning( |
| 94 | bt_self_component_port_input_message_iterator *iterator); |
| 95 | |
| 96 | extern void bt_self_component_port_input_message_iterator_get_ref( |
| 97 | const bt_self_component_port_input_message_iterator *self_component_port_input_message_iterator); |
| 98 | |
| 99 | extern void bt_self_component_port_input_message_iterator_put_ref( |
| 100 | const bt_self_component_port_input_message_iterator *self_component_port_input_message_iterator); |
| 101 | |
| 102 | /* From port-output-message-iterator.h */ |
| 103 | |
| 104 | bt_message_iterator * |
| 105 | bt_port_output_message_iterator_as_message_iterator( |
| 106 | bt_port_output_message_iterator *iterator); |
| 107 | |
| 108 | extern bt_port_output_message_iterator * |
| 109 | bt_port_output_message_iterator_create( |
| 110 | bt_graph *graph, |
| 111 | const bt_port_output *output_port); |
| 112 | |
| 113 | extern bt_message_iterator_status |
| 114 | bt_port_output_message_iterator_next( |
| 115 | bt_port_output_message_iterator *iterator, |
| 116 | bt_message_array_const *msgs, uint64_t *count); |
| 117 | |
| 118 | extern bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin( |
| 119 | bt_port_output_message_iterator *iterator, |
| 120 | int64_t ns_from_origin); |
| 121 | |
| 122 | extern bt_bool bt_port_output_message_iterator_can_seek_beginning( |
| 123 | bt_port_output_message_iterator *iterator); |
| 124 | |
| 125 | extern bt_message_iterator_status |
| 126 | bt_port_output_message_iterator_seek_ns_from_origin( |
| 127 | bt_port_output_message_iterator *iterator, |
| 128 | int64_t ns_from_origin); |
| 129 | |
| 130 | extern bt_message_iterator_status |
| 131 | bt_port_output_message_iterator_seek_beginning( |
| 132 | bt_port_output_message_iterator *iterator); |
| 133 | |
| 134 | extern void bt_port_output_message_iterator_get_ref( |
| 135 | const bt_port_output_message_iterator *port_output_message_iterator); |
| 136 | |
| 137 | extern void bt_port_output_message_iterator_put_ref( |
| 138 | const bt_port_output_message_iterator *port_output_message_iterator); |
| 139 | |
| 140 | /* Helper functions for Python */ |
| 141 | %{ |
| 142 | static PyObject *bt_py3_get_user_component_from_user_msg_iter( |
| 143 | bt_self_message_iterator *self_message_iterator) |
| 144 | { |
| 145 | bt_self_component *self_component = bt_self_message_iterator_borrow_component(self_message_iterator); |
| 146 | PyObject *py_comp; |
| 147 | |
| 148 | BT_ASSERT(self_component); |
| 149 | py_comp = bt_self_component_get_data(self_component); |
| 150 | BT_ASSERT(py_comp); |
| 151 | |
| 152 | /* Return new reference */ |
| 153 | Py_INCREF(py_comp); |
| 154 | return py_comp; |
| 155 | } |
| 156 | |
| 157 | static inline |
| 158 | PyObject *create_pylist_from_messages(bt_message_array_const messages, |
| 159 | uint64_t message_count) |
| 160 | { |
| 161 | uint64_t i; |
| 162 | PyObject *py_msg_list = PyList_New(message_count); |
| 163 | BT_ASSERT(py_msg_list); |
| 164 | for (i = 0; i < message_count; i++) { |
| 165 | PyList_SET_ITEM(py_msg_list, i, |
| 166 | SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]), |
| 167 | SWIGTYPE_p_bt_message, 0)); |
| 168 | } |
| 169 | |
| 170 | return py_msg_list; |
| 171 | } |
| 172 | |
| 173 | static |
| 174 | PyObject *bt_py3_get_msg_range_common(bt_message_iterator_status status, |
| 175 | bt_message_array_const messages, uint64_t message_count) |
| 176 | { |
| 177 | PyObject *py_status; |
| 178 | PyObject *py_return_tuple; |
| 179 | PyObject *py_msg_list = Py_None; |
| 180 | |
| 181 | py_status = SWIG_From_long_SS_long(status); |
| 182 | if (status != BT_MESSAGE_ITERATOR_STATUS_OK) { |
| 183 | goto end; |
| 184 | } |
| 185 | |
| 186 | py_msg_list = create_pylist_from_messages(messages, message_count); |
| 187 | |
| 188 | end: |
| 189 | py_return_tuple = PyTuple_New(2); |
| 190 | BT_ASSERT(py_return_tuple); |
| 191 | PyTuple_SET_ITEM(py_return_tuple, 0, py_status); |
| 192 | PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list); |
| 193 | |
| 194 | return py_return_tuple; |
| 195 | } |
| 196 | |
| 197 | static PyObject |
| 198 | *bt_py3_self_component_port_input_get_msg_range( |
| 199 | bt_self_component_port_input_message_iterator *iter) |
| 200 | { |
| 201 | bt_message_array_const messages; |
| 202 | uint64_t message_count = 0; |
| 203 | bt_message_iterator_status status; |
| 204 | |
| 205 | status = bt_self_component_port_input_message_iterator_next(iter, &messages, |
| 206 | &message_count); |
| 207 | |
| 208 | return bt_py3_get_msg_range_common(status, messages, message_count); |
| 209 | } |
| 210 | |
| 211 | static PyObject |
| 212 | *bt_py3_port_output_get_msg_range( |
| 213 | bt_port_output_message_iterator *iter) |
| 214 | { |
| 215 | bt_message_array_const messages; |
| 216 | uint64_t message_count = 0; |
| 217 | bt_message_iterator_status status; |
| 218 | |
| 219 | status = |
| 220 | bt_port_output_message_iterator_next(iter, &messages, |
| 221 | &message_count); |
| 222 | |
| 223 | return bt_py3_get_msg_range_common(status, messages, message_count); |
| 224 | } |
| 225 | %} |
| 226 | |
| 227 | PyObject *bt_py3_get_user_component_from_user_msg_iter( |
| 228 | bt_self_message_iterator *self_message_iterator); |
| 229 | PyObject *bt_py3_self_component_port_input_get_msg_range( |
| 230 | bt_self_component_port_input_message_iterator *iter); |
| 231 | PyObject *bt_py3_port_output_get_msg_range( |
| 232 | bt_port_output_message_iterator *iter); |