summaryrefslogtreecommitdiffstats
path: root/libtiutils/MessageQueue.cpp
blob: 13b1d53c849ef30c6fa235f791e67c9f495b18b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <unistd.h>
#include <utils/Errors.h>



#define LOG_TAG "MessageQueue"
#include <utils/Log.h>

#include "MessageQueue.h"

namespace Ti {
namespace Utils {

/**
   @brief Constructor for the message queue class

   @param none
   @return none
 */
MessageQueue::MessageQueue()
{
    LOG_FUNCTION_NAME;

    int fds[2] = {-1,-1};
    android::status_t stat;

    stat = pipe(fds);

    if ( 0 > stat )
        {
        MSGQ_LOGEB("Error while openning pipe: %s", strerror(stat) );
        this->fd_read = 0;
        this->fd_write = 0;
        mHasMsg = false;
        }
    else
        {
        this->fd_read = fds[0];
        this->fd_write = fds[1];

        mHasMsg = false;
        }

    LOG_FUNCTION_NAME_EXIT;
}

/**
   @brief Destructor for the semaphore class

   @param none
   @return none
 */
MessageQueue::~MessageQueue()
{
    LOG_FUNCTION_NAME;

    if(this->fd_read >= 0)
        {
        close(this->fd_read);
        }

    if(this->fd_write >= 0)
        {
        close(this->fd_write);
        }

    LOG_FUNCTION_NAME_EXIT;
}

/**
   @brief Get a message from the queue

   @param msg Message structure to hold the message to be retrieved
   @return android::NO_ERROR On success
   @return android::BAD_VALUE if the message pointer is NULL
   @return android::NO_INIT If the file read descriptor is not set
   @return android::UNKNOWN_ERROR if the read operation fromthe file read descriptor fails
 */
android::status_t MessageQueue::get(Message* msg)
{
    LOG_FUNCTION_NAME;

    if(!msg)
        {
        MSGQ_LOGEA("msg is NULL");
        LOG_FUNCTION_NAME_EXIT;
        return android::BAD_VALUE;
        }

    if(!this->fd_read)
        {
        MSGQ_LOGEA("read descriptor not initialized for message queue");
        LOG_FUNCTION_NAME_EXIT;
        return android::NO_INIT;
        }

    char* p = (char*) msg;
    size_t read_bytes = 0;

    while( read_bytes  < sizeof(*msg) )
        {
        int err = read(this->fd_read, p, sizeof(*msg) - read_bytes);

        if( err < 0 )
            {
            MSGQ_LOGEB("read() error: %s", strerror(errno));
            return android::UNKNOWN_ERROR;
            }
        else
            {
            read_bytes += err;
            }
        }

    MSGQ_LOGDB("MQ.get(%d,%p,%p,%p,%p)", msg->command, msg->arg1,msg->arg2,msg->arg3,msg->arg4);

    mHasMsg = false;

    LOG_FUNCTION_NAME_EXIT;

    return 0;
}

/**
   @brief Get the input file descriptor of the message queue

   @param none
   @return file read descriptor
 */

int MessageQueue::getInFd()
{
    return this->fd_read;
}

/**
   @brief Constructor for the message queue class

   @param fd file read descriptor
   @return none
 */

void MessageQueue::setInFd(int fd)
{
    LOG_FUNCTION_NAME;

    if ( -1 != this->fd_read )
        {
        close(this->fd_read);
        }

    this->fd_read = fd;

    LOG_FUNCTION_NAME_EXIT;
}

/**
   @brief Queue a message

   @param msg Message structure to hold the message to be retrieved
   @return android::NO_ERROR On success
   @return android::BAD_VALUE if the message pointer is NULL
   @return android::NO_INIT If the file write descriptor is not set
   @return android::UNKNOWN_ERROR if the write operation fromthe file write descriptor fails
 */

android::status_t MessageQueue::put(Message* msg)
{
    LOG_FUNCTION_NAME;

    char* p = (char*) msg;
    size_t bytes = 0;

    if(!msg)
        {
        MSGQ_LOGEA("msg is NULL");
        LOG_FUNCTION_NAME_EXIT;
        return android::BAD_VALUE;
        }

    if(!this->fd_write)
        {
        MSGQ_LOGEA("write descriptor not initialized for message queue");
        LOG_FUNCTION_NAME_EXIT;
        return android::NO_INIT;
        }


    MSGQ_LOGDB("MQ.put(%d,%p,%p,%p,%p)", msg->command, msg->arg1,msg->arg2,msg->arg3,msg->arg4);

    while( bytes  < sizeof(msg) )
        {
        int err = write(this->fd_write, p, sizeof(*msg) - bytes);

        if( err < 0 )
            {
            MSGQ_LOGEB("write() error: %s", strerror(errno));
            LOG_FUNCTION_NAME_EXIT;
            return android::UNKNOWN_ERROR;
            }
        else
            {
            bytes += err;
            }
        }

    MSGQ_LOGDA("MessageQueue::put EXIT");

    LOG_FUNCTION_NAME_EXIT;
    return 0;
}


/**
   @brief Returns if the message queue is empty or not

   @param none
   @return true If the queue is empty
   @return false If the queue has at least one message
 */
bool MessageQueue::isEmpty()
{
    LOG_FUNCTION_NAME;

    struct pollfd pfd;

    pfd.fd = this->fd_read;
    pfd.events = POLLIN;
    pfd.revents = 0;

    if(!this->fd_read)
        {
        MSGQ_LOGEA("read descriptor not initialized for message queue");
        LOG_FUNCTION_NAME_EXIT;
        return android::NO_INIT;
        }


    if( -1 == poll(&pfd,1,0) )
        {
        MSGQ_LOGEB("poll() error: %s", strerror(errno));
        LOG_FUNCTION_NAME_EXIT;
        return false;
        }

    if(pfd.revents & POLLIN)
        {
        mHasMsg = true;
        }
    else
        {
        mHasMsg = false;
        }

    LOG_FUNCTION_NAME_EXIT;
    return !mHasMsg;
}

void MessageQueue::clear()
{
    LOG_FUNCTION_NAME;

    if(!this->fd_read)
        {
        MSGQ_LOGEA("read descriptor not initialized for message queue");
        LOG_FUNCTION_NAME_EXIT;
        return;
        }

    Message msg;
    while(!isEmpty())
        {
        get(&msg);
        }

}


/**
   @brief Force whether the message queue has message or not

   @param hasMsg Whether the queue has a message or not
   @return none
 */
void MessageQueue::setMsg(bool hasMsg)
    {
    mHasMsg = hasMsg;
    }


/**
   @briefWait for message in maximum three different queues with a timeout

   @param queue1 First queue. At least this should be set to a valid queue pointer
   @param queue2 Second queue. Optional.
   @param queue3 Third queue. Optional.
   @param timeout The timeout value (in micro secs) to wait for a message in any of the queues
   @return android::NO_ERROR On success
   @return android::BAD_VALUE If queue1 is NULL
   @return android::NO_INIT If the file read descriptor of any of the provided queues is not set
 */
android::status_t MessageQueue::waitForMsg(MessageQueue *queue1, MessageQueue *queue2, MessageQueue *queue3, int timeout)
    {
    LOG_FUNCTION_NAME;

    int n =1;
    struct pollfd pfd[3];

    if(!queue1)
        {
        MSGQ_LOGEA("queue1 pointer is NULL");
        LOG_FUNCTION_NAME_EXIT;
        return android::BAD_VALUE;
        }

    pfd[0].fd = queue1->getInFd();
    if(!pfd[0].fd)
        {
        MSGQ_LOGEA("read descriptor not initialized for message queue1");
        LOG_FUNCTION_NAME_EXIT;
        return android::NO_INIT;
        }
    pfd[0].events = POLLIN;
    pfd[0].revents = 0;
    if(queue2)
        {
        MSGQ_LOGDA("queue2 not-null");
        pfd[1].fd = queue2->getInFd();
        if(!pfd[1].fd)
            {
            MSGQ_LOGEA("read descriptor not initialized for message queue2");
            LOG_FUNCTION_NAME_EXIT;
            return android::NO_INIT;
            }

        pfd[1].events = POLLIN;
        pfd[1].revents = 0;
        n++;
        }

    if(queue3)
        {
        MSGQ_LOGDA("queue3 not-null");
        pfd[2].fd = queue3->getInFd();
        if(!pfd[2].fd)
            {
            MSGQ_LOGEA("read descriptor not initialized for message queue3");
            LOG_FUNCTION_NAME_EXIT;
            return android::NO_INIT;
            }

        pfd[2].events = POLLIN;
        pfd[2].revents = 0;
        n++;
        }


    int ret = poll(pfd, n, timeout);
    if(ret==0)
        {
        LOG_FUNCTION_NAME_EXIT;
        return ret;
        }

    if(ret<android::NO_ERROR)
        {
        MSGQ_LOGEB("Message queue returned error %d", ret);
        LOG_FUNCTION_NAME_EXIT;
        return ret;
        }

    if (pfd[0].revents & POLLIN)
        {
        queue1->setMsg(true);
        }

    if(queue2)
        {
        if (pfd[1].revents & POLLIN)
            {
            queue2->setMsg(true);
            }
        }

    if(queue3)
        {
        if (pfd[2].revents & POLLIN)
            {
            queue3->setMsg(true);
            }
        }

    LOG_FUNCTION_NAME_EXIT;
    return ret;
    }

} // namespace Utils
} // namespace Ti