1 /* 2 * 3 * Copyright (C) 2013-2018 NXP Semiconductors 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #ifndef _PHNXPNCIHAL_UTILS_H_ 20 #define _PHNXPNCIHAL_UTILS_H_ 21 22 #include <assert.h> 23 #include <errno.h> 24 #include <phNfcStatus.h> 25 #include <pthread.h> 26 #include <semaphore.h> 27 28 /********************* Definitions and structures *****************************/ 29 30 /* List structures */ 31 struct listNode { 32 void* pData; 33 struct listNode* pNext; 34 }; 35 36 struct listHead { 37 struct listNode* pFirst; 38 pthread_mutex_t mutex; 39 }; 40 41 /* Semaphore handling structure */ 42 typedef struct phNxpNciHal_Sem { 43 /* Semaphore used to wait for callback */ 44 sem_t sem; 45 46 /* Used to store the status sent by the callback */ 47 NFCSTATUS status; 48 49 /* Used to provide a local context to the callback */ 50 void* pContext; 51 52 } phNxpNciHal_Sem_t; 53 54 /* Semaphore helper macros */ 55 #define SEM_WAIT(cb_data) \ 56 ((sem_wait(&((cb_data).sem)) == 0) \ 57 ? 0 \ 58 : (errno == EINTR) ? sem_wait(&((cb_data).sem)) : -1) 59 60 #define SEM_POST(p_cb_data) sem_post(&((p_cb_data)->sem)) 61 62 /* Semaphore and mutex monitor */ 63 typedef struct phNxpNciHal_Monitor { 64 /* Mutex protecting native library against reentrance */ 65 pthread_mutex_t reentrance_mutex; 66 67 /* Mutex protecting native library against concurrency */ 68 pthread_mutex_t concurrency_mutex; 69 70 /* List used to track pending semaphores waiting for callback */ 71 struct listHead sem_list; 72 73 } phNxpNciHal_Monitor_t; 74 75 /************************ Exposed functions ***********************************/ 76 /* List functions */ 77 int listInit(struct listHead* pList); 78 int listDestroy(struct listHead* pList); 79 int listAdd(struct listHead* pList, void* pData); 80 int listRemove(struct listHead* pList, void* pData); 81 int listGetAndRemoveNext(struct listHead* pList, void** ppData); 82 void listDump(struct listHead* pList); 83 84 /* NXP NCI HAL utility functions */ 85 phNxpNciHal_Monitor_t* phNxpNciHal_init_monitor(void); 86 void phNxpNciHal_cleanup_monitor(void); 87 phNxpNciHal_Monitor_t* phNxpNciHal_get_monitor(void); 88 NFCSTATUS phNxpNciHal_init_cb_data(phNxpNciHal_Sem_t* pCallbackData, 89 void* pContext); 90 void phNxpNciHal_cleanup_cb_data(phNxpNciHal_Sem_t* pCallbackData); 91 void phNxpNciHal_releaseall_cb_data(void); 92 void phNxpNciHal_print_packet(const char* pString, const uint8_t* p_data, 93 uint16_t len); 94 void phNxpNciHal_emergency_recovery(uint8_t status); 95 96 /* Lock unlock helper macros */ 97 /* Lock unlock helper macros */ 98 #define REENTRANCE_LOCK() \ 99 if (phNxpNciHal_get_monitor()) \ 100 pthread_mutex_lock(&phNxpNciHal_get_monitor()->reentrance_mutex) 101 #define REENTRANCE_UNLOCK() \ 102 if (phNxpNciHal_get_monitor()) \ 103 pthread_mutex_unlock(&phNxpNciHal_get_monitor()->reentrance_mutex) 104 #define CONCURRENCY_LOCK() \ 105 if (phNxpNciHal_get_monitor()) \ 106 pthread_mutex_lock(&phNxpNciHal_get_monitor()->concurrency_mutex) 107 #define CONCURRENCY_UNLOCK() \ 108 if (phNxpNciHal_get_monitor()) \ 109 pthread_mutex_unlock(&phNxpNciHal_get_monitor()->concurrency_mutex) 110 111 #endif /* _PHNXPNCIHAL_UTILS_H_ */ 112