merge with master
[adaptation/devices/nfc-plugin-65nxp.git] / Linux_x86 / phDal4Nfc_message_glib.c
1 /*
2  * {nfc-plugin-nxp}
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  */
19
20 #include <phDal4Nfc.h>
21 #include <phOsalNfc.h>
22 #include <phOsalNfc_Timer.h>
23 #include <phDal4Nfc_DeferredCall.h>
24 #include <phDal4Nfc_messageQueueLib.h>
25
26 #include <glib.h>
27 #include <stdlib.h>
28 #include <pthread.h>
29
30 static int g_msg_q_id = -1;
31 pthread_mutex_t * _nfc_get_fri_lock ();
32
33 typedef struct phOsalNfc_Message_Wrapper{
34         long mtype;
35         phOsalNfc_Message_t msg;
36 }phOsalNfc_Message_Wrapper_t;
37
38 gboolean DeferredCallback(gpointer data)
39 {
40         phOsalNfc_Message_Wrapper_t wrapper;
41         phOsalNfc_Message_t OsalMsg;
42
43         phDal4Nfc_msgrcv(g_msg_q_id, (void *)&wrapper, sizeof(phOsalNfc_Message_t), 0, 0);
44
45         memcpy(&OsalMsg, &wrapper.msg, sizeof(phOsalNfc_Message_t));
46
47         if(OsalMsg.eMsgType == PH_DAL4NFC_MESSAGE_BASE)
48         {
49                 phDal4Nfc_DeferredCall_Msg_t* defer_msg = NULL;
50                 defer_msg = (phDal4Nfc_DeferredCall_Msg_t *)(OsalMsg.pMsgData);
51
52                 if((defer_msg != NULL) && (defer_msg->pCallback != NULL))
53                 {
54                         pthread_mutex_lock (_nfc_get_fri_lock());
55                         defer_msg->pCallback(defer_msg->pParameter);
56                         pthread_mutex_unlock (_nfc_get_fri_lock());
57             }
58         }
59
60         #ifdef NXP_MESSAGING
61
62         else if(OsalMsg.eMsgType == PH_OSALNFC_TIMER_MSG)
63         {
64                 phOsalNfc_Timer_Msg_t *timer_msg = NULL;
65                 timer_msg = (phOsalNfc_Timer_Msg_t *)(OsalMsg.pMsgData);
66
67                 if (timer_msg != NULL && timer_msg->pCallBck !=NULL)
68                 {
69                         pthread_mutex_lock (_nfc_get_fri_lock());
70                         timer_msg->pCallBck (timer_msg->TimerId, timer_msg->pContext);
71                         pthread_mutex_unlock (_nfc_get_fri_lock());
72                 }
73         }
74
75         #endif
76
77         return FALSE;
78 }
79
80 int InitMessageQueue()
81 {
82         if(g_msg_q_id < 0)
83         {
84                 g_msg_q_id = phDal4Nfc_msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
85         }
86
87         return g_msg_q_id;
88 }
89
90
91 void DeleteMessageQeueu()
92 {
93         phDal4Nfc_msgctl(g_msg_q_id, IPC_RMID, NULL);
94 }
95
96
97
98 void PostMessage(void* msgp, size_t msgsz, int msgflg)
99 {
100         phDal4Nfc_msgsnd(g_msg_q_id, msgp, msgsz, msgflg);
101 /*
102         if(g_idle_add(DeferredCallback, NULL))
103         {
104                 g_main_context_wakeup(g_main_context_default()) ;
105         }
106 */
107
108
109         if(g_idle_add_full(G_PRIORITY_HIGH_IDLE, DeferredCallback, NULL, NULL))
110         {
111                 g_main_context_wakeup(g_main_context_default()) ;
112         }
113
114 }
115
116