| 1 |
/*************************************************************************\ |
| 2 |
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne |
| 3 |
* National Laboratory. |
| 4 |
* Copyright (c) 2002 The Regents of the University of California, as |
| 5 |
* Operator of Los Alamos National Laboratory. |
| 6 |
* SPDX-License-Identifier: EPICS |
| 7 |
* EPICS Base is distributed subject to a Software License Agreement found |
| 8 |
* in file LICENSE that is included with this distribution. |
| 9 |
\*************************************************************************/ |
| 10 |
/* |
| 11 |
* Author W. Eric Norum |
| 12 |
* [email protected] |
| 13 |
* 630 252 4793 |
| 14 |
*/ |
| 15 |
|
| 16 |
/* |
| 17 |
* We want to access information which is |
| 18 |
* normally hidden from application programs. |
| 19 |
*/ |
| 20 |
#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ 1 |
| 21 |
|
| 22 |
#define epicsExportSharedSymbols |
| 23 |
#include <assert.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <rtems.h> |
| 28 |
#include <rtems/error.h> |
| 29 |
#include "epicsMessageQueue.h" |
| 30 |
#include "errlog.h" |
| 31 |
#include <epicsAtomic.h> |
| 32 |
|
| 33 |
#include <errno.h> |
| 34 |
#include <mqueue.h> |
| 35 |
#include <fcntl.h> |
| 36 |
|
| 37 |
static int idCnt; |
| 38 |
|
| 39 |
LIBCOM_API epicsMessageQueueId epicsStdCall |
| 40 |
epicsMessageQueueCreate(unsigned int capacity, unsigned int maximumMessageSize) |
| 41 |
{ |
| 42 |
struct mq_attr the_attr; |
| 43 |
epicsMessageQueueId id = (epicsMessageQueueId)calloc(1, sizeof(*id)); |
| 44 |
if (!id) { |
| 45 |
fprintf (stderr, "Can't allocate message queue: %s\n", strerror(errno)); |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
sprintf(id->name, "MQ_%0d", epicsAtomicIncrIntT(&idCnt)); |
| 49 |
the_attr.mq_maxmsg = capacity; |
| 50 |
the_attr.mq_msgsize = maximumMessageSize; |
| 51 |
id->id = mq_open(id->name, O_RDWR | O_CREAT | O_EXCL, 0644, &the_attr); |
| 52 |
if (id->id <0) { |
| 53 |
fprintf (stderr, "Can't create message queue: %s\n", strerror (errno)); |
| 54 |
free(id); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
return id; |
| 58 |
} |
| 59 |
|
| 60 |
LIBCOM_API void epicsStdCall epicsMessageQueueDestroy( |
| 61 |
epicsMessageQueueId id) |
| 62 |
{ |
| 63 |
int rv; |
| 64 |
rv = mq_close(id->id); |
| 65 |
if( rv ) { |
| 66 |
fprintf(stderr, "epicsMessageQueueDestroy mq_close failed: %s\n", |
| 67 |
strerror(rv)); |
| 68 |
} |
| 69 |
rv = mq_unlink(id->name); |
| 70 |
if( rv ) { |
| 71 |
fprintf(stderr,"epicsMessageQueueDestroy mq_unlink %s failed: %s\n", |
| 72 |
id->name, strerror(rv)); |
| 73 |
} |
| 74 |
free(id); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
LIBCOM_API int epicsStdCall epicsMessageQueueTrySend( |
| 79 |
epicsMessageQueueId id, |
| 80 |
void *message, |
| 81 |
unsigned int messageSize) |
| 82 |
{ |
| 83 |
struct timespec ts; |
| 84 |
clock_gettime(CLOCK_REALTIME, &ts); |
| 85 |
return mq_timedsend(id->id, (char const *)message, messageSize, 0, &ts); |
| 86 |
} |
| 87 |
|
| 88 |
LIBCOM_API int epicsStdCall epicsMessageQueueSendWithTimeout( |
| 89 |
epicsMessageQueueId id, |
| 90 |
void *message, |
| 91 |
unsigned int messageSize, |
| 92 |
double timeout) |
| 93 |
{ |
| 94 |
struct timespec ts; |
| 95 |
unsigned long micros; |
| 96 |
|
| 97 |
// assume timeout in sec |
| 98 |
micros = (unsigned long)(timeout * 1000000.0); |
| 99 |
clock_gettime(CLOCK_REALTIME, &ts); |
| 100 |
ts.tv_sec += micros / 1000000L; |
| 101 |
ts.tv_nsec += (micros % 1000000L) * 1000L; |
| 102 |
|
| 103 |
return mq_timedsend (id->id, (const char *)message, messageSize, 0, &ts); |
| 104 |
} |
| 105 |
|
| 106 |
LIBCOM_API int epicsStdCall epicsMessageQueueTryReceive( |
| 107 |
epicsMessageQueueId id, |
| 108 |
void *message, |
| 109 |
unsigned int size) |
| 110 |
{ |
| 111 |
struct timespec ts; |
| 112 |
clock_gettime(CLOCK_REALTIME, &ts); |
| 113 |
return mq_timedreceive(id->id, (char *)message, size, NULL, &ts); |
| 114 |
} |
| 115 |
|
| 116 |
LIBCOM_API int epicsStdCall epicsMessageQueueReceiveWithTimeout( |
| 117 |
epicsMessageQueueId id, |
| 118 |
void *message, |
| 119 |
unsigned int size, |
| 120 |
double timeout) |
| 121 |
{ |
| 122 |
unsigned long micros; |
| 123 |
struct timespec ts; |
| 124 |
|
| 125 |
micros = (unsigned long)(timeout * 1000000.0); |
| 126 |
clock_gettime(CLOCK_REALTIME, &ts); |
| 127 |
ts.tv_sec += micros / 1000000L; |
| 128 |
ts.tv_nsec += (micros % 1000000L) * 1000L; |
| 129 |
|
| 130 |
return mq_timedreceive(id->id, (char *)message, size, NULL, &ts); |
| 131 |
} |
| 132 |
|
| 133 |
LIBCOM_API int epicsStdCall epicsMessageQueuePending( |
| 134 |
epicsMessageQueueId id) |
| 135 |
{ |
| 136 |
int rv; |
| 137 |
struct mq_attr the_attr; |
| 138 |
|
| 139 |
rv = mq_getattr(id->id, &the_attr); |
| 140 |
if (rv) { |
| 141 |
fprintf(stderr, "Epics Message queue %x (%s) get attr failed: %s\n", |
| 142 |
(unsigned int)id->id, id->name, strerror(rv)); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
return the_attr.mq_curmsgs; |
| 146 |
} |
| 147 |
LIBCOM_API void epicsStdCall epicsMessageQueueShow( |
| 148 |
epicsMessageQueueId id, |
| 149 |
int level) |
| 150 |
{ |
| 151 |
int rv; |
| 152 |
struct mq_attr the_attr; |
| 153 |
|
| 154 |
rv = mq_getattr(id->id, &the_attr); |
| 155 |
if (rv) { |
| 156 |
fprintf(stderr, "Epics Message queue %x (%s) get attr failed: %s\n", |
| 157 |
(unsigned int)id->id, id->name, strerror(rv)); |
| 158 |
} |
| 159 |
|
| 160 |
printf("Message Queue Used:%ld Max Msg:%lu", the_attr.mq_curmsgs, the_attr.mq_maxmsg); |
| 161 |
if (level >= 1) |
| 162 |
printf(" Maximum size:%lu", the_attr.mq_msgsize); |
| 163 |
|
| 164 |
printf("\n"); |
| 165 |
} |