aboutsummaryrefslogtreecommitdiffstats
path: root/src/qcomm-hidl/AntHidlClient.cpp
blob: 37aa540259f56382e0c2809dfccc44afa84843e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *Copyright (c) 2017, The Linux Foundation. All rights reserved.
 *
 *
 *Redistribution and use in source and binary forms, with or without
 *modification, are permitted provided that the following conditions are
 *met:
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *    * Neither the name of The Linux Foundation nor the names of its
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 *
 *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 *ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 *BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 *IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include <iostream>       // std::cin, std::cout
#include <queue>          // std::queue
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
#include <cstdlib>
#include <thread>

#include <hwbinder/ProcessState.h>

#include <com/qualcomm/qti/ant/1.0/IAntHci.h>
#include <com/qualcomm/qti/ant/1.0/IAntHciCallbacks.h>
#include <com/qualcomm/qti/ant/1.0/types.h>

#include <utils/Log.h>


#include <hidl/Status.h>
#include <hwbinder/ProcessState.h>
#include "ant_types.h"
#include "AntHidlClient.h"

using com::qualcomm::qti::ant::V1_0::IAntHci;
using com::qualcomm::qti::ant::V1_0::IAntHciCallbacks;
using com::qualcomm::qti::ant::V1_0::AntPacket;
using com::qualcomm::qti::ant::V1_0::Status;

using ::android::hardware::hidl_vec;


using android::hardware::ProcessState;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
android::sp<IAntHci> anthci;
typedef std::unique_lock<std::mutex> Lock;



struct ant_hci_t {
public:
   std::condition_variable rx_cond;
   std::condition_variable on_cond;
   std::mutex on_mtx;
   std::mutex rx_mtx;
   ant_power_state_t state;
   bool rx_processing;
};

static struct ant_hci_t ant_hci;

Return<void> initialization_complete(bool is_hci_initialize)
{
   ALOGI("%s: is_hci_initialize: %d", __func__, is_hci_initialize);

   Lock lk(ant_hci.on_mtx);
   if (is_hci_initialize)
   {
      ant_hci.state = ANT_RADIO_ENABLED;
      ALOGI("%s: is_hci_initialize: %d", __func__, is_hci_initialize);
   }
   else
   {
      ALOGE("%s:Ant hci init failed :%d", __func__, is_hci_initialize);
      ant_hci.state = ANT_RADIO_DISABLED;
   }
   ant_hci.on_cond.notify_all();
   ALOGI("%s: exit", __func__);
   return Void();
}


class AntHciCallbacks : public IAntHciCallbacks {
public:
   AntHciCallbacks() {};
   virtual ~AntHciCallbacks() = default;

   Return<void> initializationComplete(Status status)
   {
      ALOGI("%s", __func__);
      if(status == Status::SUCCESS)
      {
         initialization_complete(true);
      } else {
         initialization_complete(false);
      }
      ALOGI("%s: exit", __func__);
      return Void();
   }

   Return<void> antControlReceived(const hidl_vec<uint8_t>& event)
   {
      ALOGI("%s: Start ", __func__);
      Lock lk(ant_hci.rx_mtx);

      // Make sure we don't overwrite a message still processing.
      while(ant_hci.rx_processing)
      {
         ant_hci.rx_cond.wait(lk);
      }
      ant_hci.rx_processing = true;

      memcpy(&aucRxBuffer[0][0], event.data(), event.size());
      iRxBufferLength[0] = event.size();
      ant_hci.rx_cond.notify_all();
      ALOGI("%s:  End", __func__);
      return Void();
   }

   Return<void> antDataReceived(const hidl_vec<uint8_t>& event)
   {
      ALOGI("%s: ", __func__);
      Lock lk(ant_hci.rx_mtx);

      // Make sure we don't overwrite a message still processing.
      while(ant_hci.rx_processing)
      {
         ant_hci.rx_cond.wait(lk);
      }
      ant_hci.rx_processing = true;

      memcpy(&aucRxBuffer[0][0], event.data(), event.size());
      iRxBufferLength[0] = event.size();
      ant_hci.rx_cond.notify_all();
      ALOGI("%s: exit", __func__);
      return Void();
   }
};

bool hci_initialize()
{
   ALOGI("%s", __func__);

   anthci = IAntHci::getService();

   if(anthci != nullptr)
   {
      ant_hci.state = ANT_RADIO_ENABLING;
      ant_hci.rx_processing = false;
      android::sp<IAntHciCallbacks> callbacks = new AntHciCallbacks();
      anthci->initialize(callbacks);
      ALOGI("%s: exit", __func__);
      return true;
   } else {
      return false;
   }
}

void hci_close() {
   ALOGV("%s", __func__);

   if(anthci != nullptr)
   {
      anthci->close();
   }
   ant_rx_clear();
   anthci =nullptr;
   ant_hci.state = ANT_RADIO_DISABLED;
   ALOGI("%s: exit", __func__);
}

ANT_UINT ant_get_status()
{
   return ant_hci.state;
}

ANTStatus ant_tx_write(ANT_U8 *pucTxMessage,ANT_U8 ucMessageLength)
{
   AntPacket data;
   ANT_U8  packet_type;
   ALOGI("%s: start", __func__);
   packet_type = *pucTxMessage;
   ALOGV("%s: proto type  :%d", __func__, packet_type);
   if (anthci != nullptr)
   {
      data.setToExternal(pucTxMessage+1, ucMessageLength);
      if (packet_type == ANT_DATA_TYPE_PACKET)
      {
         anthci->sendAntData(data);
      } else {
         anthci->sendAntControl(data);
      }
   } else {
      ALOGE("%s: antHci is NULL", __func__);
      return -1;
   }
   ALOGI("%s: exit", __func__);
   return ucMessageLength;
}

ANTStatus ant_rx_check()
{
   ALOGI("%s: start", __func__);
   Lock lk(ant_hci.rx_mtx);
   ant_hci.rx_cond.wait(lk);
   ALOGI("%s: exit", __func__);
   if (ant_hci.rx_processing)
   {
      return ANT_STATUS_SUCCESS;
   }
   else
   {
      return ANT_STATUS_NO_VALUE_AVAILABLE;
   }
}

void ant_rx_clear()
{
   ALOGI("%s: start", __func__);
   Lock lk(ant_hci.rx_mtx);
   ant_hci.rx_processing = false;
   ant_hci.rx_cond.notify_all();
   ALOGI("%s: exit", __func__);
}

void ant_interface_init()
{
   ALOGI("%s: start", __func__);
   bool status;
   status = hci_initialize();
   if (status)
   {
      ALOGV("%s waiting for iniialization complete hci state: %d ", __func__, ant_hci.state);
      Lock lk(ant_hci.on_mtx);
      while(ant_hci.state == ANT_RADIO_ENABLING)
      {
         ant_hci.on_cond.wait(lk);
         ALOGV(LOG_TAG,"%s:after on_cond wait  ",__func__);
      }
   } else {
      ALOGE(LOG_TAG,"%s:Failed ",__func__);
      ant_hci.state = ANT_RADIO_DISABLED;
   }
   ALOGI(LOG_TAG,"%s:exit ",__func__);
}