1 /*
2  * Copyright 2024 NXP
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 #include <ObserveMode.h>
17 #include <phNfcNciConstants.h>
18 
19 #include <vector>
20 
21 #include "phNxpNciHal_extOperations.h"
22 
23 using namespace std;
24 
25 bool bIsObserveModeEnabled;
26 
27 /*******************************************************************************
28  *
29  * Function         setObserveModeFlag()
30  *
31  * Description      It sets the observe mode flag
32  *
33  * Parameters       bool - true to enable observe mode
34  *                         false to disable observe mode
35  *
36  * Returns          void
37  *
38  ******************************************************************************/
setObserveModeFlag(bool flag)39 void setObserveModeFlag(bool flag) { bIsObserveModeEnabled = flag; }
40 
41 /*******************************************************************************
42  *
43  * Function         isObserveModeEnabled()
44  *
45  * Description      It gets the observe mode flag
46  *
47  * Returns          bool true if the observed mode is enabled
48  *                  otherwise false
49  *
50  ******************************************************************************/
isObserveModeEnabled()51 bool isObserveModeEnabled() { return bIsObserveModeEnabled; }
52 
53 /*******************************************************************************
54  *
55  * Function         handleObserveMode()
56  *
57  * Description      This handles the ObserveMode command and enables the observe
58  *                  Mode flag
59  *
60  * Returns          It returns number of bytes received.
61  *
62  ******************************************************************************/
handleObserveMode(uint16_t data_len,const uint8_t * p_data)63 int handleObserveMode(uint16_t data_len, const uint8_t* p_data) {
64   if (data_len <= 4) {
65     return 0;
66   }
67 
68   uint8_t status = NCI_RSP_FAIL;
69   if (phNxpNciHal_isObserveModeSupported()) {
70     setObserveModeFlag(p_data[NCI_MSG_INDEX_FEATURE_VALUE]);
71     status = NCI_RSP_OK;
72   }
73 
74   phNxpNciHal_vendorSpecificCallback(
75       p_data[NCI_OID_INDEX], p_data[NCI_MSG_INDEX_FOR_FEATURE], {status});
76 
77   return p_data[NCI_MSG_LEN_INDEX];
78 }
79 
80 /*******************************************************************************
81  *
82  * Function         handleGetObserveModeStatus()
83  *
84  * Description      Handles the Get Observe mode command and gives the observe
85  *                  mode status
86  *
87  * Returns          It returns number of bytes received.
88  *
89  ******************************************************************************/
handleGetObserveModeStatus(uint16_t data_len,const uint8_t * p_data)90 int handleGetObserveModeStatus(uint16_t data_len, const uint8_t* p_data) {
91   // 2F 0C 01 04 => ObserveMode Status Command length is 4 Bytes
92   if (data_len < 4) {
93     return 0;
94   }
95   vector<uint8_t> response;
96   response.push_back(0x00);
97   response.push_back(isObserveModeEnabled() ? 0x00 : 0x01);
98   phNxpNciHal_vendorSpecificCallback(p_data[NCI_OID_INDEX],
99                                      p_data[NCI_MSG_INDEX_FOR_FEATURE],
100                                      std::move(response));
101 
102   return p_data[NCI_MSG_LEN_INDEX];
103 }
104