1 /*
2 * Copyright 2024-2025 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 "NciDiscoveryCommandBuilder.h"
22 #include "phNxpNciHal_extOperations.h"
23
24 using namespace std;
25
26 bool bIsObserveModeEnabled;
27
28 /*******************************************************************************
29 *
30 * Function setObserveModeFlag()
31 *
32 * Description It sets the observe mode flag
33 *
34 * Parameters bool - true to enable observe mode
35 * false to disable observe mode
36 *
37 * Returns void
38 *
39 ******************************************************************************/
setObserveModeFlag(bool flag)40 void setObserveModeFlag(bool flag) { bIsObserveModeEnabled = flag; }
41
42 /*******************************************************************************
43 *
44 * Function isObserveModeEnabled()
45 *
46 * Description It gets the observe mode flag
47 *
48 * Returns bool true if the observed mode is enabled
49 * otherwise false
50 *
51 ******************************************************************************/
isObserveModeEnabled()52 bool isObserveModeEnabled() { return bIsObserveModeEnabled; }
53
54 /*******************************************************************************
55 *
56 * Function handleObserveMode()
57 *
58 * Description This handles the ObserveMode command and enables the observe
59 * Mode flag
60 *
61 * Returns It returns number of bytes received.
62 *
63 ******************************************************************************/
handleObserveMode(uint16_t data_len,const uint8_t * p_data)64 int handleObserveMode(uint16_t data_len, const uint8_t* p_data) {
65 if (data_len <= 4) {
66 return 0;
67 }
68
69 uint8_t status = NCI_RSP_FAIL;
70 if (phNxpNciHal_isObserveModeSupported()) {
71 setObserveModeFlag(p_data[NCI_MSG_INDEX_FEATURE_VALUE]);
72 status = NCI_RSP_OK;
73 }
74
75 phNxpNciHal_vendorSpecificCallback(
76 p_data[NCI_OID_INDEX], p_data[NCI_MSG_INDEX_FOR_FEATURE], {status});
77
78 return p_data[NCI_MSG_LEN_INDEX];
79 }
80
81 /*******************************************************************************
82 *
83 * Function handleObserveModeTechCommand()
84 *
85 * Description This handles the ObserveMode command and enables the observe
86 * Mode flag
87 *
88 * Returns It returns number of bytes received.
89 *
90 ******************************************************************************/
handleObserveModeTechCommand(uint16_t data_len,const uint8_t * p_data)91 int handleObserveModeTechCommand(uint16_t data_len, const uint8_t* p_data) {
92 uint8_t status = NCI_RSP_FAIL;
93 if (phNxpNciHal_isObserveModeSupported() &&
94 (p_data[NCI_MSG_INDEX_FEATURE_VALUE] ==
95 OBSERVE_MODE_TECH_COMMAND_SUPPORT_FLAG ||
96 p_data[NCI_MSG_INDEX_FEATURE_VALUE] ==
97 OBSERVE_MODE_TECH_COMMAND_SUPPORT_FLAG_FOR_ALL_TECH ||
98 p_data[NCI_MSG_INDEX_FEATURE_VALUE] ==
99 NCI_ANDROID_PASSIVE_OBSERVE_PARAM_DISABLE)) {
100 bool flag = (p_data[NCI_MSG_INDEX_FEATURE_VALUE] ==
101 OBSERVE_MODE_TECH_COMMAND_SUPPORT_FLAG ||
102 p_data[NCI_MSG_INDEX_FEATURE_VALUE] ==
103 OBSERVE_MODE_TECH_COMMAND_SUPPORT_FLAG_FOR_ALL_TECH)
104 ? true
105 : false;
106 uint8_t rf_deactivate_cmd[] = {0x21, 0x06, 0x01, 0x00};
107
108 // send RF Deactivate command
109 NFCSTATUS rfDeactivateStatus =
110 phNxpNciHal_send_ext_cmd(sizeof(rf_deactivate_cmd), rf_deactivate_cmd);
111 if (rfDeactivateStatus == NFCSTATUS_SUCCESS) {
112 if (flag) {
113 // send Observe Mode Tech command
114 NFCSTATUS nciStatus =
115 phNxpNciHal_send_ext_cmd(data_len, (uint8_t*)p_data);
116 if (nciStatus != NFCSTATUS_SUCCESS) {
117 NXPLOG_NCIHAL_E("%s ObserveMode tech command failed", __func__);
118 }
119 }
120
121 // send Discovery command
122 vector<uint8_t> discoveryCommand =
123 flag ? NciDiscoveryCommandBuilderInstance.reConfigRFDiscCmd()
124 : NciDiscoveryCommandBuilderInstance.getDiscoveryCommand();
125 NFCSTATUS rfDiscoveryStatus = phNxpNciHal_send_ext_cmd(
126 discoveryCommand.size(), &discoveryCommand[0]);
127
128 if (rfDiscoveryStatus == NFCSTATUS_SUCCESS) {
129 setObserveModeFlag(flag);
130 status = NCI_RSP_OK;
131 } else {
132 NXPLOG_NCIHAL_E("%s Rf Disovery command failed", __func__);
133 }
134
135 } else {
136 NXPLOG_NCIHAL_E("%s Rf Deactivate command failed", __func__);
137 }
138 } else {
139 NXPLOG_NCIHAL_E(
140 "%s ObserveMode feature or tech which is requested is not supported",
141 __func__);
142 }
143
144 phNxpNciHal_vendorSpecificCallback(
145 p_data[NCI_OID_INDEX], p_data[NCI_MSG_INDEX_FOR_FEATURE], {status});
146
147 return p_data[NCI_MSG_LEN_INDEX];
148 }
149
150 /*******************************************************************************
151 *
152 * Function handleGetObserveModeStatus()
153 *
154 * Description Handles the Get Observe mode command and gives the observe
155 * mode status
156 *
157 * Returns It returns number of bytes received.
158 *
159 ******************************************************************************/
handleGetObserveModeStatus(uint16_t data_len,const uint8_t * p_data)160 int handleGetObserveModeStatus(uint16_t data_len, const uint8_t* p_data) {
161 // 2F 0C 01 04 => ObserveMode Status Command length is 4 Bytes
162 if (data_len < 4) {
163 return 0;
164 }
165 vector<uint8_t> response;
166 response.push_back(NCI_RSP_OK);
167 response.push_back(isObserveModeEnabled() ? 0x01 : 0x00);
168 phNxpNciHal_vendorSpecificCallback(p_data[NCI_OID_INDEX],
169 p_data[NCI_MSG_INDEX_FOR_FEATURE],
170 std::move(response));
171
172 return p_data[NCI_MSG_LEN_INDEX];
173 }
174