1 /*
2 * Copyright 2022 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
17 #include "phNxpNciHal_PowerTrackerIface.h"
18
19 #include <dlfcn.h>
20
21 /*******************************************************************************
22 **
23 ** Function phNxpNciHal_isPowerTrackerConfigured()
24 **
25 ** Description This function is to check power tracker feature is
26 ** configured or not by checking poll duration from
27 ** config file.
28 **
29 ** Parameters pollDuration - Output parameter for fetching poll duration
30 ** if it is configured in libnfc-ncp.conf
31 ** Returns true if supported
32 ** false otherwise
33 *******************************************************************************/
phNxpNciHal_isPowerTrackerConfigured(unsigned long * pollDuration)34 static bool phNxpNciHal_isPowerTrackerConfigured(unsigned long* pollDuration) {
35 unsigned long num = 0;
36
37 if ((GetNxpNumValue(NAME_NXP_SYSTEM_POWER_TRACE_POLL_DURATION, &num,
38 sizeof(num)))) {
39 if ((uint8_t)num > 0) {
40 NXPLOG_NCIHAL_D("%s: NxpNci isPowerTrackerSupported true", __func__);
41 if (pollDuration) {
42 // Convert from seconds to Milliseconds
43 *pollDuration = num * 1000;
44 }
45 return true;
46 }
47 }
48 NXPLOG_NCIHAL_D("%s: NxpNci isPowerTrackerSupported false", __func__);
49 return false;
50 }
51
52 /*******************************************************************************
53 **
54 ** Function phNxpNciHal_PowerTrackerInit()
55 **
56 ** Description Initialize power tracker framework.
57 **
58 ** Parameters outHandle - Power Tracker Handle
59 ** Returns NFCSTATUS_SUCCESS if success.
60 ** NFCSTATUS_FAILURE otherwise
61 *******************************************************************************/
62
phNxpNciHal_PowerTrackerInit(PowerTrackerHandle * outHandle)63 NFCSTATUS phNxpNciHal_PowerTrackerInit(PowerTrackerHandle* outHandle) {
64 NFCSTATUS status = NFCSTATUS_SUCCESS;
65
66 if (outHandle == NULL) {
67 return NFCSTATUS_FAILED;
68 }
69 if (!phNxpNciHal_isPowerTrackerConfigured(&outHandle->pollDuration)) {
70 return status;
71 }
72 // Open power_tracker shared library
73 NXPLOG_NCIHAL_D("Opening (/vendor/lib64/power_tracker.so)");
74 outHandle->dlHandle = dlopen("/vendor/lib64/power_tracker.so", RTLD_NOW);
75 if (outHandle->dlHandle == NULL) {
76 NXPLOG_NCIHAL_D("Error : opening (/vendor/lib64/power_tracker.so) %s!!",
77 dlerror());
78 return NFCSTATUS_FAILED;
79 }
80 outHandle->start = (PowerTrackerStartFunc_t)dlsym(
81 outHandle->dlHandle, "phNxpNciHal_startPowerTracker");
82 if (outHandle->start == NULL) {
83 NXPLOG_NCIHAL_D(
84 "Error : Failed to find symbol phNxpNciHal_startPowerTracker %s!!",
85 dlerror());
86 }
87 outHandle->stateChange = (PowerTrackerStateChangeFunc_t)dlsym(
88 outHandle->dlHandle, "phNxpNciHal_onRefreshNfccPowerState");
89 if (outHandle->stateChange == NULL) {
90 NXPLOG_NCIHAL_D(
91 "Error : Failed to find symbol phNxpNciHal_onRefreshNfccPowerState "
92 "%s!!",
93 dlerror());
94 }
95 outHandle->stop = (PowerTrackerStopFunc_t)dlsym(
96 outHandle->dlHandle, "phNxpNciHal_stopPowerTracker");
97 if (outHandle->stop == NULL) {
98 NXPLOG_NCIHAL_D(
99 "Error : Failed to find symbol phNxpNciHal_stopPowerTracker %s !!",
100 dlerror());
101 }
102 NXPLOG_NCIHAL_D("Opened (/vendor/lib64/power_tracker.so)");
103 return status;
104 }
105
106 /*******************************************************************************
107 **
108 ** Function phNxpNciHal_PowerTrackerDeinit()
109 **
110 ** Description Deinitialize power tracker framework.
111 **
112 ** Parameters outHandle - Power Tracker Handle
113 ** Returns NFCSTATUS_SUCCESS if success.
114 ** NFCSTATUS_FAILURE otherwise
115 *******************************************************************************/
116
phNxpNciHal_PowerTrackerDeinit(PowerTrackerHandle * outHandle)117 NFCSTATUS phNxpNciHal_PowerTrackerDeinit(PowerTrackerHandle* outHandle) {
118 NXPLOG_NCIHAL_D("Closing (/vendor/lib64/power_tracker.so)");
119 if (outHandle == NULL) {
120 return NFCSTATUS_SUCCESS;
121 }
122 if (outHandle->dlHandle != NULL) {
123 dlclose(outHandle->dlHandle);
124 outHandle->dlHandle = NULL;
125 }
126 outHandle->start = NULL;
127 outHandle->stateChange = NULL;
128 outHandle->stop = NULL;
129 return NFCSTATUS_SUCCESS;
130 }
131