| 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 | static |
| 26 | bt_self_component_port_input_message_iterator_create_from_message_iterator_status |
| 27 | bt_bt2_self_component_port_input_message_iterator_create_from_message_iterator( |
| 28 | bt_self_message_iterator *self_msg_iter, |
| 29 | bt_self_component_port_input *input_port, |
| 30 | bt_self_component_port_input_message_iterator **message_iterator) |
| 31 | { |
| 32 | bt_self_component_port_input_message_iterator_create_from_message_iterator_status |
| 33 | status; |
| 34 | |
| 35 | status = bt_self_component_port_input_message_iterator_create_from_message_iterator( |
| 36 | self_msg_iter, input_port, message_iterator); |
| 37 | |
| 38 | if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) { |
| 39 | *message_iterator = NULL; |
| 40 | } |
| 41 | |
| 42 | return status; |
| 43 | } |
| 44 | |
| 45 | static |
| 46 | bt_self_component_port_input_message_iterator_create_from_sink_component_status |
| 47 | bt_bt2_self_component_port_input_message_iterator_create_from_sink_component( |
| 48 | bt_self_component_sink *self_comp, |
| 49 | bt_self_component_port_input *input_port, |
| 50 | bt_self_component_port_input_message_iterator **message_iterator) |
| 51 | { |
| 52 | bt_self_component_port_input_message_iterator_create_from_sink_component_status |
| 53 | status; |
| 54 | |
| 55 | status = bt_self_component_port_input_message_iterator_create_from_sink_component( |
| 56 | self_comp, input_port, message_iterator); |
| 57 | |
| 58 | if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { |
| 59 | *message_iterator = NULL; |
| 60 | } |
| 61 | |
| 62 | return status; |
| 63 | } |
| 64 | |
| 65 | static PyObject *bt_bt2_get_user_component_from_user_msg_iter( |
| 66 | bt_self_message_iterator *self_message_iterator) |
| 67 | { |
| 68 | bt_self_component *self_component = bt_self_message_iterator_borrow_component(self_message_iterator); |
| 69 | PyObject *py_comp; |
| 70 | |
| 71 | BT_ASSERT_DBG(self_component); |
| 72 | py_comp = bt_self_component_get_data(self_component); |
| 73 | BT_ASSERT_DBG(py_comp); |
| 74 | |
| 75 | /* Return new reference */ |
| 76 | Py_INCREF(py_comp); |
| 77 | return py_comp; |
| 78 | } |
| 79 | |
| 80 | static inline |
| 81 | PyObject *create_pylist_from_messages(bt_message_array_const messages, |
| 82 | uint64_t message_count) |
| 83 | { |
| 84 | uint64_t i; |
| 85 | PyObject *py_msg_list = PyList_New(message_count); |
| 86 | |
| 87 | BT_ASSERT(py_msg_list); |
| 88 | |
| 89 | for (i = 0; i < message_count; i++) { |
| 90 | PyList_SET_ITEM(py_msg_list, i, |
| 91 | SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]), |
| 92 | SWIGTYPE_p_bt_message, 0)); |
| 93 | } |
| 94 | |
| 95 | return py_msg_list; |
| 96 | } |
| 97 | |
| 98 | static |
| 99 | PyObject *get_msg_range_common(bt_message_iterator_next_status status, |
| 100 | bt_message_array_const messages, uint64_t message_count) |
| 101 | { |
| 102 | PyObject *py_status; |
| 103 | PyObject *py_return_tuple; |
| 104 | PyObject *py_msg_list; |
| 105 | |
| 106 | py_return_tuple = PyTuple_New(2); |
| 107 | BT_ASSERT(py_return_tuple); |
| 108 | |
| 109 | /* Set tuple[0], status. */ |
| 110 | py_status = SWIG_From_long_SS_long(status); |
| 111 | PyTuple_SET_ITEM(py_return_tuple, 0, py_status); |
| 112 | |
| 113 | /* Set tuple[1], message list on success, None otherwise. */ |
| 114 | if (status == __BT_FUNC_STATUS_OK) { |
| 115 | py_msg_list = create_pylist_from_messages(messages, message_count); |
| 116 | } else { |
| 117 | py_msg_list = Py_None; |
| 118 | Py_INCREF(py_msg_list); |
| 119 | } |
| 120 | |
| 121 | PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list); |
| 122 | |
| 123 | return py_return_tuple; |
| 124 | } |
| 125 | |
| 126 | static PyObject *bt_bt2_self_component_port_input_get_msg_range( |
| 127 | bt_self_component_port_input_message_iterator *iter) |
| 128 | { |
| 129 | bt_message_array_const messages; |
| 130 | uint64_t message_count = 0; |
| 131 | bt_message_iterator_next_status status; |
| 132 | |
| 133 | status = bt_self_component_port_input_message_iterator_next(iter, |
| 134 | &messages, &message_count); |
| 135 | return get_msg_range_common(status, messages, message_count); |
| 136 | } |