2 * Copyright (C) 2010 NXP Semiconductors
\r
3 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
20 * \brief OSAL Implementation for linux
\r
22 * Project: Trusted NFC Linux Light
\r
24 * $Date: 03 aug 2009
\r
25 * $Author: Jérémie Corbier
\r
36 #include <phOsalNfc.h>
\r
39 #define LOG_TAG "NFC_PLUGIN_NXP_HCI"
\r
41 #include <utils/Log.h>
\r
43 phOsalNfc_Exception_t phOsalNfc_Exception;
\r
47 #define MAX_PRINT_BUFSIZE (0x450U)
\r
48 char phOsalNfc_DbgTraceBuffer[MAX_PRINT_BUFSIZE];
\r
52 * \brief Allocates memory.
\r
53 * This function attempts to allocate \a size bytes on the heap and
\r
54 * returns a pointer to the allocated block.
\r
56 * \param size size of the memory block to be allocated on the heap.
\r
58 * \return pointer to allocated memory block or NULL in case of error.
\r
60 void *phOsalNfc_GetMemory(uint32_t size)
\r
62 void *pMem = (void *)malloc(size);
\r
67 * \brief Frees allocated memory block.
\r
68 * This function deallocates memory region pointed to by \a pMem.
\r
70 * \param pMem pointer to memory block to be freed.
\r
72 void phOsalNfc_FreeMemory(void *pMem)
\r
78 void phOsalNfc_DbgString(const char *pString)
\r
85 LOGD("%s", pString);
\r
90 void phOsalNfc_DbgTrace(uint8_t data[], uint32_t size)
\r
102 for(i = 0; i < size; i++)
\r
105 printf("\n\t\t\t");
\r
106 printf("%02X ", data[i]);
\r
108 printf("\n\tBlock size is: %d\n", size);
\r
110 phOsalNfc_DbgTraceBuffer[0] = '\0';
\r
111 for(i = 0; i < size; i++)
\r
115 LOGD("%s", phOsalNfc_DbgTraceBuffer);
\r
116 phOsalNfc_DbgTraceBuffer[0] = '\0';
\r
119 snprintf(buf, 10, "%02X ", data[i]);
\r
120 strncat(phOsalNfc_DbgTraceBuffer, buf, 10);
\r
122 LOGD("%s", phOsalNfc_DbgTraceBuffer);
\r
123 LOGD("Block size is: %d", size);
\r
129 * \brief Raises exception.
\r
130 * This function raises an exception of type \a eExceptionType with
\r
131 * reason \a reason to stack clients.
\r
133 * \param eExceptionType exception type.
\r
134 * \param reason reason for this exception.
\r
136 * \note Clients willing to catch exceptions are to handle the SIGABRT signal.
\r
137 * On Linux, exception type and reason are passed to the signal handler as
\r
138 * a pointer to a phOsalNfc_Exception_t structure.
\r
139 * As sigqueue is not available in Android, exception information are
\r
140 * stored in the phOsalNfc_Exception global.
\r
142 void phOsalNfc_RaiseException(phOsalNfc_ExceptionType_t eExceptionType, uint16_t reason)
\r
144 LOGD("phOsalNfc_RaiseException() called");
\r
146 if(eExceptionType == phOsalNfc_e_UnrecovFirmwareErr)
\r
148 LOGE("HCI Timeout - Exception raised");
\r
149 LOGE("Force restart of NFC Service");
\r
155 * \brief display data bytes.
\r
156 * This function displays data bytes for debug purpose
\r
157 * \param[in] pString pointer to string to be displayed.
\r
158 * \param[in] length number of bytes to be displayed.
\r
159 * \param[in] pBuffer pointer to data bytes to be displayed.
\r
162 void phOsalNfc_PrintData(const char *pString, uint32_t length, uint8_t *pBuffer,
\r
165 char print_buffer[length * 3 + 1];
\r
168 if (pString == NULL) {
\r
171 print_buffer[0] = '\0';
\r
172 for (i = 0; i < length; i++) {
\r
173 snprintf(&print_buffer[i*3], 4, " %02X", pBuffer[i]);
\r
178 if (verbosity >= 2) {
\r
179 uint8_t llc_header = 0;
\r
180 if (!strcmp(pString, "SEND") && length >= 2) {
\r
181 llc_header = pBuffer[1];
\r
182 } else if (!strcmp(pString, "RECV") && length >= 2) {
\r
183 llc_header = pBuffer[0];
\r
186 if ((llc_header & 0xC0) == 0x80) {
\r
188 uint8_t ns = (llc_header & 0x38) >> 3;
\r
189 uint8_t nr = llc_header & 0x07;
\r
190 snprintf(&llc[0], sizeof(llc), "I %d (%d)", ns, nr);
\r
191 } else if ((llc_header & 0xE0) == 0xC0) {
\r
193 uint8_t t = (llc_header & 0x18) >> 3;
\r
194 uint8_t nr = llc_header & 0x07;
\r
197 case 0x00: type = "RR "; break;
\r
198 case 0x01: type = "REJ"; break;
\r
199 case 0x02: type = "RNR"; break;
\r
200 case 0x03: type = "SREJ"; break;
\r
201 default: type = "???"; break;
\r
203 snprintf(&llc[0], sizeof(llc), "S %s (%d)", type, nr);
\r
204 } else if ((llc_header & 0xE0) == 0xE0) {
\r
206 snprintf(&llc[0], sizeof(llc), "U");
\r
207 } else if (length > 1) {
\r
208 snprintf(&llc[0], sizeof(llc), "???");
\r
212 LOGD("> %s:%s\t%s", pString, print_buffer, llc);
\r