1 /*
2  * Copyright (C) 2012-2018 NXP Semiconductors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "phNxpNciHal_nciParser.h"
18 
19 #include <dlfcn.h>
20 #include <phNfcTypes.h>
21 #include <string.h>
22 #include "phNxpLog.h"
23 
24 typedef void* tNCI_PARSER_INSTANCE;
25 typedef void* tNCI_PARSER_LIB_HANDLE;
26 
27 typedef struct {
28   tNCI_PARSER_INSTANCE pvInstance;
29   tNCI_PARSER_LIB_HANDLE pvHandle;
30   tNCI_PARSER_FUNCTIONS sEntryFuncs;
31 } sParserContext_t;
32 
33 static sParserContext_t sParserContext;
34 
phNxpNciHal_initParser()35 unsigned char phNxpNciHal_initParser() {
36   sParserContext_t* psContext = &sParserContext;
37 
38   memset(&psContext->sEntryFuncs, 0, sizeof(psContext->sEntryFuncs));
39 
40   psContext->pvInstance = NULL;
41   psContext->sEntryFuncs.createParser = NULL;
42   psContext->sEntryFuncs.initParser = NULL;
43   psContext->sEntryFuncs.deinitParser = NULL;
44   psContext->sEntryFuncs.destroyParser = NULL;
45   psContext->sEntryFuncs.parsePacket = NULL;
46 
47   NXPLOG_NCIHAL_D("%s: enter", __FUNCTION__);
48 
49   psContext->pvHandle = dlopen(NXP_NCI_PARSER_PATH, RTLD_NOW);
50   if (!psContext->pvHandle) {
51     NXPLOG_NCIHAL_E("%s: dlopen failed !!!", __FUNCTION__);
52     return FALSE;
53   }
54 
55   psContext->sEntryFuncs.createParser = (tHAL_API_NATIVE_CREATE_PARSER)dlsym(
56       psContext->pvHandle, "native_createParser");
57   if (psContext->sEntryFuncs.createParser == NULL) {
58     NXPLOG_NCIHAL_E("%s: dlsym native_createParser failed !!!", __FUNCTION__);
59     return FALSE;
60   }
61 
62   psContext->sEntryFuncs.destroyParser = (tHAL_API_NATIVE_DESTROY_PARSER)dlsym(
63       psContext->pvHandle, "native_destroyParser");
64   if (psContext->sEntryFuncs.destroyParser == NULL) {
65     NXPLOG_NCIHAL_E("%s: dlsym native_destroyParser failed !!!", __FUNCTION__);
66     return FALSE;
67   }
68 
69   psContext->sEntryFuncs.initParser = (tHAL_API_NATIVE_INIT_PARSER)dlsym(
70       psContext->pvHandle, "native_initParser");
71   if (psContext->sEntryFuncs.initParser == NULL) {
72     NXPLOG_NCIHAL_E("%s: dlsym native_initParser failed !!!", __FUNCTION__);
73     return FALSE;
74   }
75 
76   psContext->sEntryFuncs.deinitParser = (tHAL_API_NATIVE_DEINIT_PARSER)dlsym(
77       psContext->pvHandle, "native_deinitParser");
78   if (psContext->sEntryFuncs.deinitParser == NULL) {
79     NXPLOG_NCIHAL_E("%s: dlsym native_deinitParser failed !!!", __FUNCTION__);
80     return FALSE;
81   }
82 
83   psContext->sEntryFuncs.parsePacket = (tHAL_API_NATIVE_PARSE_PACKET)dlsym(
84       psContext->pvHandle, "native_parseNciMsg");
85   if (psContext->sEntryFuncs.parsePacket == NULL) {
86     NXPLOG_NCIHAL_E("%s: dlsym native_parseNciMsg failed !!!", __FUNCTION__);
87     return FALSE;
88   }
89 
90   psContext->pvInstance = (*(psContext->sEntryFuncs.createParser))();
91 
92   if (psContext->pvInstance != NULL) {
93     (*(psContext->sEntryFuncs.initParser))(psContext->pvInstance);
94   } else {
95     NXPLOG_NCIHAL_E("Parser Creation Failed !!!");
96     return FALSE;
97   }
98 
99   NXPLOG_NCIHAL_D("%s: exit", __FUNCTION__);
100 
101   return TRUE;
102 }
103 
phNxpNciHal_parsePacket(unsigned char * pNciPkt,unsigned short pktLen)104 void phNxpNciHal_parsePacket(unsigned char* pNciPkt, unsigned short pktLen) {
105   sParserContext_t* psContext = &sParserContext;
106 
107   NXPLOG_NCIHAL_D("%s: enter", __FUNCTION__);
108 
109   if ((pNciPkt == NULL) || (pktLen == 0)) {
110     NXPLOG_NCIHAL_E("Invalid NCI Packet");
111     return;
112   }
113 
114   if (psContext->pvInstance != NULL) {
115     (*(psContext->sEntryFuncs.parsePacket))(psContext->pvInstance, pNciPkt,
116                                             pktLen);
117   } else {
118     NXPLOG_NCIHAL_E("Invalid Handle");
119   }
120   NXPLOG_NCIHAL_D("%s: exit", __FUNCTION__);
121 }
122 
phNxpNciHal_deinitParser()123 void phNxpNciHal_deinitParser() {
124   sParserContext_t* psContext = &sParserContext;
125 
126   NXPLOG_NCIHAL_D("%s: enter", __FUNCTION__);
127 
128   if (psContext->pvInstance != NULL) {
129     (*(psContext->sEntryFuncs.deinitParser))(psContext->pvInstance);
130 
131     (*(psContext->sEntryFuncs.destroyParser))(psContext->pvInstance);
132   } else {
133     NXPLOG_NCIHAL_E("Invalid Handle");
134   }
135 
136   if (psContext->pvHandle != NULL) {
137     dlclose(psContext->pvHandle);
138   }
139 
140   NXPLOG_NCIHAL_D("%s: exit", __FUNCTION__);
141 }
142