merge with master
[adaptation/devices/nfc-plugin-65nxp.git] / Linux_x86 / phOsalNfc.c
1 /*\r
2  * Copyright (C) 2010 NXP Semiconductors\r
3  * Copyright (c) 2013 Samsung Electronics Co., Ltd.\r
4  *\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
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\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
16  */\r
17 \r
18 /**\r
19  * \file phOsalNfc.c\r
20  * \brief OSAL Implementation for linux\r
21  *\r
22  * Project: Trusted NFC Linux Light\r
23  *\r
24  * $Date: 03 aug 2009\r
25  * $Author: Jérémie Corbier\r
26  * $Revision: 1.0\r
27  *\r
28  */\r
29 \r
30 #include <stddef.h>\r
31 #include <stdlib.h>\r
32 #include <stdio.h>\r
33 #include <signal.h>\r
34 #include <unistd.h>\r
35 \r
36 #include <phOsalNfc.h>\r
37 \r
38 //#ifdef ANDROID\r
39 #define LOG_TAG "NFC_PLUGIN_NXP_HCI"\r
40 \r
41 #include <utils/Log.h>\r
42 \r
43 phOsalNfc_Exception_t phOsalNfc_Exception;\r
44 //#endif\r
45 \r
46 #ifdef DEBUG\r
47 #define MAX_PRINT_BUFSIZE (0x450U)\r
48 char phOsalNfc_DbgTraceBuffer[MAX_PRINT_BUFSIZE];\r
49 #endif\r
50 \r
51 /*!\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
55  *\r
56  * \param size size of the memory block to be allocated on the heap.\r
57  *\r
58  * \return pointer to allocated memory block or NULL in case of error.\r
59  */\r
60 void *phOsalNfc_GetMemory(uint32_t size)\r
61 {\r
62    void *pMem = (void *)malloc(size);\r
63    return pMem;\r
64 }\r
65 \r
66 /*!\r
67  * \brief Frees allocated memory block.\r
68  *        This function deallocates memory region pointed to by \a pMem.\r
69  *\r
70  * \param pMem pointer to memory block to be freed.\r
71  */\r
72 void phOsalNfc_FreeMemory(void *pMem)\r
73 {\r
74    if(NULL !=  pMem)\r
75       free(pMem);\r
76 }\r
77 \r
78 void phOsalNfc_DbgString(const char *pString)\r
79 {\r
80 #ifdef DEBUG\r
81    if(pString != NULL)\r
82 #ifndef ANDROID\r
83       printf(pString);\r
84 #else\r
85       LOGD("%s", pString);\r
86 #endif\r
87 #endif\r
88 }\r
89 \r
90 void phOsalNfc_DbgTrace(uint8_t data[], uint32_t size)\r
91 {\r
92 #ifdef DEBUG\r
93    uint32_t i;\r
94 #ifdef ANDROID\r
95    char buf[10];\r
96 #endif\r
97 \r
98    if(size == 0)\r
99       return;\r
100 \r
101 #ifndef ANDROID\r
102    for(i = 0; i < size; i++)\r
103    {\r
104       if((i % 10) == 0)\r
105          printf("\n\t\t\t");\r
106       printf("%02X ", data[i]);\r
107    }\r
108    printf("\n\tBlock size is: %d\n", size);\r
109 #else\r
110    phOsalNfc_DbgTraceBuffer[0] = '\0';\r
111    for(i = 0; i < size; i++)\r
112    {\r
113       if((i % 10) == 0)\r
114       {\r
115          LOGD("%s", phOsalNfc_DbgTraceBuffer);\r
116          phOsalNfc_DbgTraceBuffer[0] = '\0';\r
117       }\r
118 \r
119       snprintf(buf, 10, "%02X ", data[i]);\r
120       strncat(phOsalNfc_DbgTraceBuffer, buf, 10);\r
121    }\r
122    LOGD("%s", phOsalNfc_DbgTraceBuffer);\r
123    LOGD("Block size is: %d", size);\r
124 #endif\r
125 #endif\r
126 }\r
127 \r
128 /*!\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
132  *\r
133  * \param eExceptionType exception type.\r
134  * \param reason reason for this exception.\r
135  *\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
141  */\r
142 void phOsalNfc_RaiseException(phOsalNfc_ExceptionType_t eExceptionType, uint16_t reason)\r
143 {\r
144     LOGD("phOsalNfc_RaiseException() called");\r
145 \r
146     if(eExceptionType == phOsalNfc_e_UnrecovFirmwareErr)\r
147     {\r
148         LOGE("HCI Timeout - Exception raised");\r
149         LOGE("Force restart of NFC Service");\r
150         abort();\r
151     }\r
152 }\r
153 \r
154 /*!\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
160  *\r
161  */\r
162 void phOsalNfc_PrintData(const char *pString, uint32_t length, uint8_t *pBuffer,\r
163         int verbosity)\r
164 {\r
165     char print_buffer[length * 3 + 1];\r
166     unsigned int i;\r
167 \r
168     if (pString == NULL) {\r
169         pString = "";\r
170     }\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
174     }\r
175 \r
176     char llc[40] = "";\r
177 \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
184         }\r
185 \r
186         if ((llc_header & 0xC0) == 0x80) {\r
187             // I\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
192             // S\r
193             uint8_t t = (llc_header & 0x18) >> 3;\r
194             uint8_t nr = llc_header & 0x07;\r
195             char *type;\r
196             switch (t) {\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
202             }\r
203             snprintf(&llc[0], sizeof(llc), "S %s (%d)", type, nr);\r
204         } else if ((llc_header & 0xE0) == 0xE0) {\r
205             // U\r
206             snprintf(&llc[0], sizeof(llc), "U");\r
207         } else if (length > 1) {\r
208             snprintf(&llc[0], sizeof(llc), "???");\r
209         }\r
210     }\r
211 \r
212     LOGD("> %s:%s\t%s", pString, print_buffer, llc);\r
213 }\r