[0074691a] | 1 | /* |
---|
| 2 | ------------------------------------------------------------------------ |
---|
| 3 | $Id$ |
---|
| 4 | ------------------------------------------------------------------------ |
---|
| 5 | |
---|
| 6 | COPYRIGHT (c) 1997 |
---|
| 7 | Objective Design Systems Ltd Pty (ODS) |
---|
| 8 | All rights reserved (R) Objective Design Systems Ltd Pty |
---|
| 9 | |
---|
| 10 | The license and distribution terms for this file may be found in the |
---|
| 11 | file LICENSE in this distribution or at |
---|
| 12 | http://www.OARcorp.com/rtems/license.html. |
---|
| 13 | |
---|
| 14 | ------------------------------------------------------------------------ |
---|
| 15 | |
---|
| 16 | See header file. |
---|
| 17 | |
---|
| 18 | ------------------------------------------------------------------------ |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include <rtems++/rtemsMessageQueue.h> |
---|
| 22 | |
---|
| 23 | /* ---- |
---|
| 24 | rtemsMessageQueue |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | rtemsMessageQueue::rtemsMessageQueue(const char* mqname, |
---|
| 28 | const rtems_unsigned32 count, |
---|
| 29 | const rtems_unsigned32 max_message_size, |
---|
| 30 | const WaitMode wait_mode, |
---|
| 31 | const Scope scope) |
---|
| 32 | : name(0), |
---|
| 33 | owner(true), |
---|
| 34 | id(0) |
---|
| 35 | { |
---|
| 36 | strcpy(name_str, "NOID"); |
---|
| 37 | create(mqname, count, max_message_size, wait_mode, scope); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | rtemsMessageQueue::rtemsMessageQueue(const char *mqname, |
---|
| 41 | const rtems_unsigned32 node) |
---|
| 42 | : name(0), |
---|
| 43 | owner(false), |
---|
| 44 | id(0) |
---|
| 45 | { |
---|
| 46 | strcpy(name_str, "NOID"); |
---|
| 47 | connect(mqname, node); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | rtemsMessageQueue::rtemsMessageQueue(const rtemsMessageQueue& message_queue) |
---|
| 51 | : name(0), |
---|
| 52 | owner(false), |
---|
| 53 | id(0) |
---|
| 54 | { |
---|
| 55 | name = message_queue.name; |
---|
| 56 | strcpy(name_str, message_queue.name_str); |
---|
| 57 | id = message_queue.id; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | rtemsMessageQueue::rtemsMessageQueue() |
---|
| 61 | : name(0), |
---|
| 62 | owner(false), |
---|
| 63 | id(0) |
---|
| 64 | { |
---|
| 65 | strcpy(name_str, "NOID"); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | rtemsMessageQueue::~rtemsMessageQueue() |
---|
| 69 | { |
---|
| 70 | destroy(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void rtemsMessageQueue::make_invalid() |
---|
| 74 | { |
---|
| 75 | strcpy(name_str, "NOID"); |
---|
| 76 | name = 0; |
---|
| 77 | id = 0; |
---|
| 78 | owner = false; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | const rtems_status_code rtemsMessageQueue::create(const char* mqname, |
---|
| 82 | const rtems_unsigned32 count, |
---|
| 83 | const rtems_unsigned32 max_message_size, |
---|
| 84 | const WaitMode wait_mode, |
---|
| 85 | const Scope scope) |
---|
| 86 | { |
---|
| 87 | if (id) |
---|
| 88 | return set_status_code(RTEMS_ILLEGAL_ON_SELF); |
---|
| 89 | |
---|
| 90 | owner = true; |
---|
| 91 | |
---|
| 92 | strcpy(name_str, " "); |
---|
| 93 | for (int c = 0; (c < 4) && (mqname[c] != '\0'); c++) |
---|
| 94 | name_str[c] = mqname[c]; |
---|
| 95 | name = rtems_build_name(name_str[0], |
---|
| 96 | name_str[1], |
---|
| 97 | name_str[2], |
---|
| 98 | name_str[3]); |
---|
| 99 | |
---|
| 100 | set_status_code(rtems_message_queue_create(name, |
---|
| 101 | count, |
---|
| 102 | max_message_size, |
---|
| 103 | scope | wait_mode, |
---|
| 104 | &id)); |
---|
| 105 | |
---|
| 106 | if (unsuccessful()) |
---|
| 107 | { |
---|
| 108 | make_invalid(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | return last_status_code(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | const rtems_status_code rtemsMessageQueue::destroy() |
---|
| 115 | { |
---|
| 116 | if (id && owner) |
---|
| 117 | { |
---|
| 118 | set_status_code(rtems_message_queue_delete(id)); |
---|
| 119 | make_invalid(); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE); |
---|
| 123 | |
---|
| 124 | return last_status_code(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | const rtemsMessageQueue& rtemsMessageQueue::operator=(const rtemsMessageQueue& message_queue) |
---|
| 128 | { |
---|
| 129 | if (!owner) |
---|
| 130 | { |
---|
| 131 | name = message_queue.name; |
---|
| 132 | strcpy(name_str, message_queue.name_str); |
---|
| 133 | id = message_queue.id; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return *this; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | const rtems_status_code rtemsMessageQueue::connect(const char *mqname, |
---|
| 140 | const rtems_unsigned32 node) |
---|
| 141 | { |
---|
| 142 | if (id && owner) |
---|
| 143 | return set_status_code(RTEMS_UNSATISFIED); |
---|
| 144 | |
---|
| 145 | // change state to not owner |
---|
| 146 | owner = false; |
---|
| 147 | |
---|
| 148 | strcpy(name_str, " "); |
---|
| 149 | for (int c = 0; (c < 4) && (mqname[c] != '\0'); c++) |
---|
| 150 | name_str[c] = mqname[c]; |
---|
| 151 | name = rtems_build_name(name_str[0], |
---|
| 152 | name_str[1], |
---|
| 153 | name_str[2], |
---|
| 154 | name_str[3]); |
---|
| 155 | |
---|
| 156 | set_status_code(rtems_message_queue_ident(name, node, &id)); |
---|
| 157 | |
---|
| 158 | if (unsuccessful()) |
---|
| 159 | { |
---|
| 160 | make_invalid(); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return last_status_code(); |
---|
| 164 | } |
---|