1 /*
2  * Copyright (C) 2017-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 "NCIParser.h"
18 #include "NCILxDebugDecoder.h"
19 #include "phOsal_Posix.h"
20 
21 #include <stdlib.h>
22 #include <cstring>
23 
24 using namespace std;
25 
26 NCI_Parser* NCI_Parser::mpNciParser = nullptr;
27 
NCI_Parser()28 NCI_Parser::NCI_Parser() {
29   mTaskRunning = false;
30   if (mpNciPropDecoder == nullptr) mpNciPropDecoder = new NCI_Decoder_Prop();
31   // mpNciStandardDecoder = new NCI_Decoder_Standard();
32 }
33 
~NCI_Parser()34 NCI_Parser::~NCI_Parser() {
35   mpNciParser = nullptr;
36   delete mpNciPropDecoder;
37   // delete mpNciStandardDecoder;
38 }
39 
getInstance()40 NCI_Parser* NCI_Parser::getInstance() {
41   if (mpNciParser == nullptr) {
42     mpNciParser = new NCI_Parser();
43     return (mpNciParser);
44   } else
45     return (mpNciParser);
46 }
47 
resetInstance()48 void NCI_Parser::resetInstance() {
49   if (mpNciParser != nullptr) delete mpNciParser;
50 }
51 
parsingTask(void * pvParams)52 void* parsingTask(__attribute__((unused)) void* pvParams) {
53   while (1) {
54     if (!NCI_Parser::getInstance()->mTaskRunning) {
55       phOsal_LogDebug(
56           (const unsigned char*)"<<<<<<<<<<Stopping Parser Task>>>>>>>>>>");
57       break;
58     }
59     phOsal_LogDebug(
60         (const unsigned char*)"<<<<<<<<<<Running Parser Task>>>>>>>>>>");
61     NCI_Parser::getInstance()->decodeNciPacket(phOsalAdapt_GetData());
62   }
63   return nullptr;
64 }
65 
initParser()66 void NCI_Parser::initParser() {
67   void* pvParserContext = nullptr;
68   ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
69 
70   dwAdaptStatus = phOsalAdapt_InitOsal(pvParserContext);
71   if (dwAdaptStatus == ADAPTSTATUS_SUCCESS) {
72     mTaskRunning = true;
73     phOsalAdapt_StartTask((void*)parsingTask, this);
74   }
75 }
76 
deinitParser()77 void NCI_Parser::deinitParser() {
78   void* pvTaskHandle = nullptr;
79   ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
80 
81   mTaskRunning = false;
82   dwAdaptStatus = phOsalAdapt_StopTask(pvTaskHandle);
83   if (dwAdaptStatus == ADAPTSTATUS_SUCCESS) {
84     phOsalAdapt_DeinitOsal();
85   }
86 }
87 
parseNciPacket(unsigned char * pMsg,unsigned short len)88 void NCI_Parser::parseNciPacket(unsigned char* pMsg, unsigned short len) {
89   sQueueData_t* psQueueData = nullptr;
90 
91   if (pMsg != nullptr) {
92     psQueueData = (sQueueData_t*)malloc(sizeof(sQueueData_t));
93     memset(psQueueData, 0, sizeof(sQueueData_t));
94     memcpy(psQueueData->buffer, pMsg, len);
95     psQueueData->len = len;
96   }
97 
98   phOsalAdapt_SendData(psQueueData);
99 }
100 
decodeNciPacket(psQueueData_t nciPacket)101 void NCI_Parser::decodeNciPacket(psQueueData_t nciPacket) {
102   if (mpNciPropDecoder != nullptr) {
103     mpNciPropDecoder->getLxDebugDecoder().processLxDbgNciPkt(nciPacket->buffer,
104                                                              nciPacket->len);
105     free(nciPacket);
106   }
107 }
108