1 /** ----------------------------------------------------------------------
2  *
3  * Copyright (C) 2016 ST Microelectronics S.A.
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 __HALCORE_H_
20 #define __HALCORE_H_
21 
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27 
28 /* events sent from the callback */
29 #define HAL_EVENT_DSWRITE 1  /* write raw HAL data downstream   */
30 #define HAL_EVENT_DATAIND 2  /* new NCI frame received from CLF  */
31 #define HAL_EVENT_LINKLOST 3 /* connection/link lost             */
32 #define HAL_EVENT_ERROR 4    /* protocol got into an error state */
33 #define HAL_EVENT_JUNKRECEIVED                                        \
34   5 /* protocol signals that junk has been received. resyncronization */
35 
36 #define HAL_EVENT_TIMER_TIMEOUT 6
37 
38 /* flags to be passed to HalCreate */
39 
40 #define HAL_WRAPPER_TIMEOUT_EVT 7
41 
42 #define HAL_FLAG_NO_DEBUG 0 /* disable debug output */
43 #define HAL_FLAG_DEBUG 1    /* enable debug output */
44 
45 bool halTraceMask;
46 pthread_mutex_t debugOutputSem;
47 
48 #ifdef ANDROID
49 #include <android/log.h>
50 #define LOG(s)                                               \
51   if (!halTraceMask) {                                       \
52   } else {                                                   \
53     pthread_mutex_lock(&debugOutputSem);                     \
54     __android_log_print(ANDROID_LOG_DEBUG, "NfcHalCore", s); \
55     pthread_mutex_unlock(&debugOutputSem);                   \
56   }
57 #define LOGE(s)                                              \
58   {                                                          \
59     pthread_mutex_lock(&debugOutputSem);                     \
60     __android_log_print(ANDROID_LOG_ERROR, "NfcHalCore", s); \
61     pthread_mutex_unlock(&debugOutputSem);                   \
62   }
63 #define LOGV(s, ...)                                                      \
64   if (!halTraceMask) {                                                    \
65   } else {                                                                \
66     pthread_mutex_lock(&debugOutputSem);                                  \
67     __android_log_print(ANDROID_LOG_DEBUG, "NfcHalCore", s, __VA_ARGS__); \
68     pthread_mutex_unlock(&debugOutputSem);                                \
69   }
70 #define LOGVE(s, ...)                                                     \
71   {                                                                       \
72     pthread_mutex_lock(&debugOutputSem);                                  \
73     __android_log_print(ANDROID_LOG_ERROR, "NfcHalCore", s, __VA_ARGS__); \
74     pthread_mutex_unlock(&debugOutputSem);                                \
75   }
76 #else
77 #define LOG printf
78 #define LOGE printf
79 #define LOGV printf
80 #define LOGVE printf
81 #endif
82 
83 /* callback function to communicate from HAL Core with the outside world */
84 typedef void (*HAL_CALLBACK)(void* context, uint32_t event, const void* data,
85                              size_t length);
86 
87 /* flags to be passed to HALCreate */
88 
89 typedef void* HALHANDLE;
90 
91 HALHANDLE HalCreate(void* context, HAL_CALLBACK callback, uint32_t flags);
92 
93 void HalDestroy(HALHANDLE hHAL);
94 
95 /* send an NCI frame from the HOST to the CLF */
96 bool HalSendDownstream(HALHANDLE hHAL, const uint8_t* data, size_t size);
97 
98 // HAL WRAPPER
99 bool HalSendDownstreamTimer(HALHANDLE hHAL, const uint8_t* data, size_t size,
100                             uint8_t duration);
101 bool HalSendDownstreamStopTimer(HALHANDLE hHAL);
102 
103 /* send a complete HDLC frame from the CLF to the HOST */
104 bool HalSendUpstream(HALHANDLE hHAL, const uint8_t* data, size_t size);
105 
106 #endif
107