13d8536b4Sopenharmony_ci/*
23d8536b4Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
33d8536b4Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
43d8536b4Sopenharmony_ci *
53d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
63d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met:
73d8536b4Sopenharmony_ci *
83d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
93d8536b4Sopenharmony_ci *    conditions and the following disclaimer.
103d8536b4Sopenharmony_ci *
113d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
123d8536b4Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
133d8536b4Sopenharmony_ci *    provided with the distribution.
143d8536b4Sopenharmony_ci *
153d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
163d8536b4Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
173d8536b4Sopenharmony_ci *    permission.
183d8536b4Sopenharmony_ci *
193d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
203d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
213d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
233d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
243d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
253d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
263d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
273d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
283d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
293d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
303d8536b4Sopenharmony_ci */
313d8536b4Sopenharmony_ci
323d8536b4Sopenharmony_ci#ifndef _MQUEUE_IMPL_H
333d8536b4Sopenharmony_ci#define _MQUEUE_IMPL_H
343d8536b4Sopenharmony_ci
353d8536b4Sopenharmony_ci#define _GNU_SOURCE
363d8536b4Sopenharmony_ci
373d8536b4Sopenharmony_ci#include <mqueue.h>
383d8536b4Sopenharmony_ci#include <errno.h>
393d8536b4Sopenharmony_ci#include <pthread.h>
403d8536b4Sopenharmony_ci#include <unistd.h>
413d8536b4Sopenharmony_ci#include <securec.h>
423d8536b4Sopenharmony_ci#include <fcntl.h>
433d8536b4Sopenharmony_ci#include <limits.h>
443d8536b4Sopenharmony_ci#include <sys/select.h>
453d8536b4Sopenharmony_ci#include "los_queue.h"
463d8536b4Sopenharmony_ci#include "los_memory.h"
473d8536b4Sopenharmony_ci#include "los_task.h"
483d8536b4Sopenharmony_ci#include "los_compiler.h"
493d8536b4Sopenharmony_ci
503d8536b4Sopenharmony_ci#define OS_QUEUE_OPERATE_TYPE(ReadOrWrite, HeadOrTail, PointOrNot)  \
513d8536b4Sopenharmony_ci                (((UINT32)(PointOrNot) << 2) | ((UINT32)(HeadOrTail) << 1) | (ReadOrWrite))
523d8536b4Sopenharmony_ci#define OS_QUEUE_READ_WRITE_GET(type) ((type) & (0x01))
533d8536b4Sopenharmony_ci#define OS_QUEUE_READ_HEAD     (OS_QUEUE_READ | (OS_QUEUE_HEAD << 1))
543d8536b4Sopenharmony_ci#define OS_QUEUE_READ_TAIL     (OS_QUEUE_READ | (OS_QUEUE_TAIL << 1))
553d8536b4Sopenharmony_ci#define OS_QUEUE_WRITE_HEAD    (OS_QUEUE_WRITE | (OS_QUEUE_HEAD << 1))
563d8536b4Sopenharmony_ci#define OS_QUEUE_WRITE_TAIL    (OS_QUEUE_WRITE | (OS_QUEUE_TAIL << 1))
573d8536b4Sopenharmony_ci#define OS_QUEUE_OPERATE_GET(type) ((type) & (0x03))
583d8536b4Sopenharmony_ci#define OS_QUEUE_IS_POINT(type)    ((type) & (0x04))
593d8536b4Sopenharmony_ci#define OS_QUEUE_IS_READ(type)     (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_READ)
603d8536b4Sopenharmony_ci#define OS_QUEUE_IS_WRITE(type)    (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_WRITE)
613d8536b4Sopenharmony_ci#define OS_READWRITE_LEN           2
623d8536b4Sopenharmony_ci
633d8536b4Sopenharmony_ci/**
643d8536b4Sopenharmony_ci * @ingroup mqueue
653d8536b4Sopenharmony_ci * Maximum number of messages in a message queue
663d8536b4Sopenharmony_ci */
673d8536b4Sopenharmony_ci#define MQ_MAX_MSG_NUM    16
683d8536b4Sopenharmony_ci
693d8536b4Sopenharmony_ci/**
703d8536b4Sopenharmony_ci * @ingroup mqueue
713d8536b4Sopenharmony_ci * Maximum size of a single message in a message queue
723d8536b4Sopenharmony_ci */
733d8536b4Sopenharmony_ci#define MQ_MAX_MSG_LEN    64
743d8536b4Sopenharmony_ci
753d8536b4Sopenharmony_ci#define ENOERR 0
763d8536b4Sopenharmony_ci
773d8536b4Sopenharmony_ci/* CONSTANTS */
783d8536b4Sopenharmony_ci
793d8536b4Sopenharmony_ci#define MQ_USE_MAGIC  0x89abcdef
803d8536b4Sopenharmony_ci#define MQ_PRIO_MAX 1
813d8536b4Sopenharmony_ci
823d8536b4Sopenharmony_ci#ifndef FNONBLOCK
833d8536b4Sopenharmony_ci#define FNONBLOCK   O_NONBLOCK
843d8536b4Sopenharmony_ci#endif
853d8536b4Sopenharmony_ci
863d8536b4Sopenharmony_ci#define QUEUE_SPLIT_BIT        16
873d8536b4Sopenharmony_ci
883d8536b4Sopenharmony_ci#define SET_QUEUE_ID(count, queueID)    (((count) << QUEUE_SPLIT_BIT) | (queueID))
893d8536b4Sopenharmony_ci
903d8536b4Sopenharmony_ci/**
913d8536b4Sopenharmony_ci * @ingroup los_queue
923d8536b4Sopenharmony_ci * get the queue index
933d8536b4Sopenharmony_ci */
943d8536b4Sopenharmony_ci#define GET_QUEUE_INDEX(queueID)        ((queueID) & ((1U << QUEUE_SPLIT_BIT) - 1))
953d8536b4Sopenharmony_ci
963d8536b4Sopenharmony_ci/**
973d8536b4Sopenharmony_ci * @ingroup los_queue
983d8536b4Sopenharmony_ci * get the queue count
993d8536b4Sopenharmony_ci */
1003d8536b4Sopenharmony_ci#define GET_QUEUE_COUNT(queueID)        ((queueID) >> QUEUE_SPLIT_BIT)
1013d8536b4Sopenharmony_ci
1023d8536b4Sopenharmony_ci
1033d8536b4Sopenharmony_ci/**
1043d8536b4Sopenharmony_ci * @ingroup los_queue
1053d8536b4Sopenharmony_ci * Obtain the head node in a queue doubly linked list.
1063d8536b4Sopenharmony_ci */
1073d8536b4Sopenharmony_ci#define GET_QUEUE_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosQueueCB, readWriteList[OS_QUEUE_WRITE])
1083d8536b4Sopenharmony_ci
1093d8536b4Sopenharmony_ci#define STATIC static
1103d8536b4Sopenharmony_ci#define INLINE inline
1113d8536b4Sopenharmony_ci
1123d8536b4Sopenharmony_citypedef union send_receive_t {
1133d8536b4Sopenharmony_ci    unsigned oth : 3;
1143d8536b4Sopenharmony_ci    unsigned grp : 6;
1153d8536b4Sopenharmony_ci    unsigned usr : 9;
1163d8536b4Sopenharmony_ci    short data;
1173d8536b4Sopenharmony_ci} mode_s;
1183d8536b4Sopenharmony_ci
1193d8536b4Sopenharmony_ci/* TYPE DEFINITIONS */
1203d8536b4Sopenharmony_cistruct mqarray {
1213d8536b4Sopenharmony_ci    UINT32 mq_id : 31;
1223d8536b4Sopenharmony_ci    UINT32 unlinkflag : 1;
1233d8536b4Sopenharmony_ci    char *mq_name;
1243d8536b4Sopenharmony_ci    UINT32 unlink_ref;
1253d8536b4Sopenharmony_ci    mode_s mode_data; /* mode data of mqueue */
1263d8536b4Sopenharmony_ci    uid_t euid; /* euid of mqueue */
1273d8536b4Sopenharmony_ci    gid_t egid; /* egid of mqueue */
1283d8536b4Sopenharmony_ci    fd_set mq_fdset; /* mqueue sysFd bit map */
1293d8536b4Sopenharmony_ci    LosQueueCB *mqcb;
1303d8536b4Sopenharmony_ci    struct mqpersonal *mq_personal;
1313d8536b4Sopenharmony_ci};
1323d8536b4Sopenharmony_ci
1333d8536b4Sopenharmony_cistruct mqpersonal {
1343d8536b4Sopenharmony_ci    struct mqarray *mq_posixdes;
1353d8536b4Sopenharmony_ci    struct mqpersonal *mq_next;
1363d8536b4Sopenharmony_ci    int mq_flags;
1373d8536b4Sopenharmony_ci    int mq_mode;  /* Mode of mqueue */
1383d8536b4Sopenharmony_ci    UINT32 mq_status;
1393d8536b4Sopenharmony_ci    UINT32 mq_refcount;
1403d8536b4Sopenharmony_ci};
1413d8536b4Sopenharmony_ci
1423d8536b4Sopenharmony_ci#endif
143