1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 /* TODO: replace krhino apis with aos apis. */
6 
7 #include <mqueue.h>
8 #include <k_api.h>
9 
mq_open(const char * name,int oflag,...)10 mqd_t mq_open(const char *name, int oflag, ...)
11 {
12     kbuf_queue_t *mq;
13     kstat_t       ret;
14 
15     ret = krhino_buf_queue_dyn_create(&mq, "buf_queue", DEFAULT_MQUEUE_SIZE, DEFAULT_MAX_MSG_SIZE);
16     if (ret != RHINO_SUCCESS) {
17         return 0;
18     }
19 
20     return mq;
21 }
22 
mq_close(mqd_t mqdes)23 int mq_close(mqd_t mqdes)
24 {
25     return krhino_buf_queue_dyn_del(mqdes);
26 }
27 
mq_receive(mqd_t mqdes,char * msg_ptr,size_t msg_len,unsigned * msg_prio)28 ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
29 {
30     size_t  msg_size;
31     kstat_t ret;
32 
33     *msg_prio = 0;
34     ret = krhino_buf_queue_recv(mqdes, 0, msg_ptr, &msg_size);
35     if (ret != RHINO_SUCCESS) {
36         return 0;
37     }
38 
39     return msg_size;
40 }
41 
mq_send(mqd_t mqdes,const char * msg_ptr,size_t msg_len,unsigned msg_prio)42 int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
43 {
44     kstat_t ret;
45 
46     ret = krhino_buf_queue_send((kbuf_queue_t *)mqdes, (void *)msg_ptr, msg_len);
47     if (ret != RHINO_SUCCESS) {
48         return 0;
49     }
50 
51     return msg_len;
52 }
53 
mq_setattr(mqd_t mqdes,const struct mq_attr * mqstat,struct mq_attr * omqstat)54 int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat, struct mq_attr *omqstat)
55 {
56     return 0;
57 }
58 
mq_getattr(mqd_t mqdes,struct mq_attr * mqstat)59 int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat)
60 {
61     return 0;
62 }
63 
mq_timedreceive(mqd_t mqdes,char * msg_ptr,size_t msg_len,unsigned * msg_prio,const struct timespec * abs_timeout)64 ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
65                         unsigned *msg_prio, const struct timespec *abs_timeout)
66 {
67     size_t  msg_size;
68     kstat_t ret;
69     tick_t  ticks;
70 
71     *msg_prio = 0;
72 
73     ticks = abs_timeout->tv_sec * RHINO_CONFIG_TICKS_PER_SECOND +
74             (abs_timeout->tv_nsec * RHINO_CONFIG_TICKS_PER_SECOND) / 1000000000LL;
75 
76     ret = krhino_buf_queue_recv(mqdes, ticks, msg_ptr, &msg_size);
77     if (ret != RHINO_SUCCESS) {
78         return 0;
79     }
80 
81     return msg_size;
82 }
83 
mq_timedsend(mqd_t mqdes,const char * msg_ptr,size_t msg_len,unsigned msg_prio,const struct timespec * abs_timeout)84 int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
85                  unsigned msg_prio, const struct timespec *abs_timeout)
86 {
87     kstat_t ret;
88 
89     (void)msg_prio;
90 
91     ret = krhino_buf_queue_send((kbuf_queue_t *)mqdes, (void *)msg_ptr, msg_len);
92     if (ret != RHINO_SUCCESS) {
93         return 0;
94     }
95 
96     return msg_len;
97 }
98