Home | History | Annotate | Download | only in libloc_api_50001
      1 /* Copyright (c) 2011, The Linux Foundation. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 #include <linux/stat.h>
     30 #include <fcntl.h>
     31 
     32 #include <linux/types.h>
     33 
     34 #include "log_util.h"
     35 #include "platform_lib_includes.h"
     36 #include "loc_eng_dmn_conn_glue_msg.h"
     37 #include "loc_eng_dmn_conn_handler.h"
     38 
     39 /*===========================================================================
     40 FUNCTION    loc_eng_dmn_conn_glue_msgget
     41 
     42 DESCRIPTION
     43    This function get a message queue
     44 
     45    q_path - name path of the message queue
     46    mode -
     47 
     48 DEPENDENCIES
     49    None
     50 
     51 RETURN VALUE
     52    message queue id
     53 
     54 SIDE EFFECTS
     55    N/A
     56 
     57 ===========================================================================*/
     58 int loc_eng_dmn_conn_glue_msgget(const char * q_path, int mode)
     59 {
     60     int msgqid;
     61     msgqid = loc_eng_dmn_conn_glue_pipeget(q_path, mode);
     62     return msgqid;
     63 }
     64 
     65 /*===========================================================================
     66 FUNCTION    loc_eng_dmn_conn_glue_msgremove
     67 
     68 DESCRIPTION
     69    remove a message queue
     70 
     71    q_path - name path of the message queue
     72    msgqid - message queue id
     73 
     74 DEPENDENCIES
     75    None
     76 
     77 RETURN VALUE
     78    0: success or negative value for failure
     79 
     80 SIDE EFFECTS
     81    N/A
     82 
     83 ===========================================================================*/
     84 int loc_eng_dmn_conn_glue_msgremove(const char * q_path, int msgqid)
     85 {
     86     int result;
     87     result = loc_eng_dmn_conn_glue_piperemove(q_path, msgqid);
     88     return result;
     89 }
     90 
     91 /*===========================================================================
     92 FUNCTION    loc_eng_dmn_conn_glue_msgsnd
     93 
     94 DESCRIPTION
     95    Send a message
     96 
     97    msgqid - message queue id
     98    msgp - pointer to the message to be sent
     99    msgsz - size of the message
    100 
    101 DEPENDENCIES
    102    None
    103 
    104 RETURN VALUE
    105    number of bytes sent out or negative value for failure
    106 
    107 SIDE EFFECTS
    108    N/A
    109 
    110 ===========================================================================*/
    111 int loc_eng_dmn_conn_glue_msgsnd(int msgqid, const void * msgp, size_t msgsz)
    112 {
    113     int result;
    114     struct ctrl_msgbuf *pmsg = (struct ctrl_msgbuf *) msgp;
    115     pmsg->msgsz = msgsz;
    116 
    117     result = loc_eng_dmn_conn_glue_pipewrite(msgqid, msgp, msgsz);
    118     if (result != (int) msgsz) {
    119         LOC_LOGE("%s:%d] pipe broken %d, msgsz = %d\n", __func__, __LINE__, result, (int) msgsz);
    120         return -1;
    121     }
    122 
    123     return result;
    124 }
    125 
    126 /*===========================================================================
    127 FUNCTION    loc_eng_dmn_conn_glue_msgrcv
    128 
    129 DESCRIPTION
    130    receive a message
    131 
    132    msgqid - message queue id
    133    msgp - pointer to the buffer to hold the message
    134    msgsz - size of the buffer
    135 
    136 DEPENDENCIES
    137    None
    138 
    139 RETURN VALUE
    140    number of bytes received or negative value for failure
    141 
    142 SIDE EFFECTS
    143    N/A
    144 
    145 ===========================================================================*/
    146 int loc_eng_dmn_conn_glue_msgrcv(int msgqid, void *msgp, size_t msgbufsz)
    147 {
    148     int result;
    149     struct ctrl_msgbuf *pmsg = (struct ctrl_msgbuf *) msgp;
    150 
    151     result = loc_eng_dmn_conn_glue_piperead(msgqid, &(pmsg->msgsz), sizeof(pmsg->msgsz));
    152     if (result != sizeof(pmsg->msgsz)) {
    153         LOC_LOGE("%s:%d] pipe broken %d\n", __func__, __LINE__, result);
    154         return -1;
    155     }
    156 
    157     if (msgbufsz < pmsg->msgsz) {
    158         LOC_LOGE("%s:%d] msgbuf is too small %d < %d\n", __func__, __LINE__, (int) msgbufsz, (int) pmsg->msgsz);
    159         return -1;
    160     }
    161 
    162     result = loc_eng_dmn_conn_glue_piperead(msgqid, (uint8_t *) msgp + sizeof(pmsg->msgsz), pmsg->msgsz - sizeof(pmsg->msgsz));
    163     if (result != (int) (pmsg->msgsz - sizeof(pmsg->msgsz))) {
    164         LOC_LOGE("%s:%d] pipe broken %d, msgsz = %d\n", __func__, __LINE__, result, (int) pmsg->msgsz);
    165         return -1;
    166     }
    167 
    168     return pmsg->msgsz;
    169 }
    170 
    171 /*===========================================================================
    172 FUNCTION    loc_eng_dmn_conn_glue_msgunblock
    173 
    174 DESCRIPTION
    175    unblock a message queue
    176 
    177    msgqid - message queue id
    178 
    179 DEPENDENCIES
    180    None
    181 
    182 RETURN VALUE
    183    0: success
    184 
    185 SIDE EFFECTS
    186    N/A
    187 
    188 ===========================================================================*/
    189 int loc_eng_dmn_conn_glue_msgunblock(int msgqid)
    190 {
    191     return loc_eng_dmn_conn_glue_pipeunblock(msgqid);
    192 }
    193 
    194 /*===========================================================================
    195 FUNCTION    loc_eng_dmn_conn_glue_msgflush
    196 
    197 DESCRIPTION
    198    flush out the message in a queue
    199 
    200    msgqid - message queue id
    201 
    202 DEPENDENCIES
    203    None
    204 
    205 RETURN VALUE
    206    number of bytes that are flushed out.
    207 
    208 SIDE EFFECTS
    209    N/A
    210 
    211 ===========================================================================*/
    212 int loc_eng_dmn_conn_glue_msgflush(int msgqid)
    213 {
    214     int length;
    215     char buf[128];
    216 
    217     do {
    218         length = loc_eng_dmn_conn_glue_piperead(msgqid, buf, 128);
    219         LOC_LOGD("%s:%d] %s\n", __func__, __LINE__, buf);
    220     } while(length);
    221     return length;
    222 }
    223 
    224