-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathctrlm_controller.cpp
More file actions
412 lines (337 loc) · 14.4 KB
/
ctrlm_controller.cpp
File metadata and controls
412 lines (337 loc) · 14.4 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2015 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include "ctrlm.h"
#include "ctrlm_log.h"
#include "ctrlm_network.h"
#include "ctrlm_utils.h"
#include "ctrlm_database.h"
#include <uuid/uuid.h>
using namespace std;
#define OTA_MAX_RETRIES (2)
ctrlm_obj_controller_t::ctrlm_obj_controller_t(ctrlm_controller_id_t controller_id, ctrlm_obj_network_t &network, unsigned long long ieee_address) :
controller_id_(controller_id),
obj_network_(&network),
ieee_address_(std::make_shared<ctrlm_ieee_db_addr_t>(ieee_address, &network, controller_id)),
time_binding_(std::make_shared<ctrlm_uint64_db_attr_t>("Binding Time", 0, &network, controller_id, "time_binding")),
last_activity_time_(std::make_shared<ctrlm_uint64_db_attr_t>("Last Activity Time", 0, &network, controller_id, "last_activity_time")),
last_key_time_(std::make_shared<ctrlm_uint64_db_attr_t>("Last Keypress Time", 0, &network, controller_id, "time_last_key")),
last_key_code_(std::make_shared<ctrlm_uint64_db_attr_t>("Last Keypress Code", CTRLM_KEY_CODE_INVALID, &network, controller_id, "last_key_code")),
irdb_entry_id_name_tv_(std::make_shared<ctrlm_string_db_attr_t>("TV IRDB Code", "0", &network, controller_id, "irdb_entry_id_name_tv")),
irdb_entry_id_name_avr_(std::make_shared<ctrlm_string_db_attr_t>("AVR IRDB Code", "0", &network, controller_id, "irdb_entry_id_name_avr")),
voice_metrics_(std::make_shared<ctrlm_voice_metrics_t>(&network, controller_id)),
ota_failure_cnt_from_last_success_(std::make_shared<ctrlm_uint64_db_attr_t>("OTA Failure Count From Last Success", 0, &network, controller_id, "ota_failure_cnt_last_success"))
{
XLOGD_INFO("constructor - %u", controller_id_);
}
ctrlm_obj_controller_t::ctrlm_obj_controller_t() {
XLOGD_INFO("constructor - default");
}
ctrlm_obj_controller_t::~ctrlm_obj_controller_t() {
XLOGD_INFO("deconstructor - %u", controller_id_);
}
void ctrlm_obj_controller_t::db_load() {
ctrlm_db_attr_read(ieee_address_.get());
ctrlm_db_attr_read(time_binding_.get());
ctrlm_db_attr_read(last_activity_time_.get());
if (last_activity_time_->get_value() == 0) {
XLOGD_INFO("Controller %s <%s> last activity time is empty, initializing with the current time.", controller_type_str_get().c_str(), ieee_address_->to_string().c_str());
last_activity_time_->set_value((uint64_t)time(NULL));
ctrlm_db_attr_write(last_activity_time_);
}
ctrlm_db_attr_read(last_key_time_.get());
ctrlm_db_attr_read(last_key_code_.get());
ctrlm_db_attr_read(irdb_entry_id_name_tv_.get());
ctrlm_db_attr_read(irdb_entry_id_name_avr_.get());
ctrlm_db_attr_read(voice_metrics_.get());
ctrlm_db_attr_read(ota_failure_cnt_from_last_success_.get());
ctrlm_db_ble_read_ota_failure_type_z_count(network_id_get(), controller_id_get(), ota_failure_type_z_cnt_);
XLOGD_INFO("Controller %s <%s> OTA total failures since last successful upgrade = %llu", controller_type_str_get().c_str(), ieee_address_->to_string().c_str(), ota_failure_cnt_from_last_success_->get_value());
XLOGD_INFO("Controller %s <%s> OTA type Z failure count = %d.... is %s", controller_type_str_get().c_str(), ieee_address_->to_string().c_str(), ota_failure_type_z_cnt_get(), is_controller_type_z() ? "TYPE Z" : "NOT TYPE Z");
}
void ctrlm_obj_controller_t::db_store() {
ctrlm_db_attr_write(ieee_address_);
ctrlm_db_attr_write(time_binding_);
ctrlm_db_attr_write(last_key_time_);
ctrlm_db_attr_write(last_key_code_);
ctrlm_db_attr_write(irdb_entry_id_name_tv_);
ctrlm_db_attr_write(irdb_entry_id_name_avr_);
ctrlm_db_attr_write(voice_metrics_);
}
std::string ctrlm_obj_controller_t::controller_type_str_get(void) {
XLOGD_WARN("not implemented.");
return "";
}
bool ctrlm_obj_controller_t::is_stale(time_t stale_time_threshold) const {
XLOGD_WARN("not implemented.");
return false;
}
ctrlm_controller_id_t ctrlm_obj_controller_t::controller_id_get() const {
return(controller_id_);
}
ctrlm_network_id_t ctrlm_obj_controller_t::network_id_get() const {
return(obj_network_->network_id_get());
}
string ctrlm_obj_controller_t::device_id_get() const {
return(obj_network_->device_id_get());
}
string ctrlm_obj_controller_t::service_account_id_get() const {
return(obj_network_->service_account_id_get());
}
string ctrlm_obj_controller_t::partner_id_get() const {
return(obj_network_->partner_id_get());
}
string ctrlm_obj_controller_t::experience_get() const {
return(obj_network_->experience_get());
}
string ctrlm_obj_controller_t::stb_name_get() const {
return(obj_network_->stb_name_get());
}
ctrlm_ieee_db_addr_t ctrlm_obj_controller_t::ieee_address_get(void) const {
return(*ieee_address_);
}
time_t ctrlm_obj_controller_t::time_binding_get() const {
return (time_t)(time_binding_->get_value());
}
time_t ctrlm_obj_controller_t::last_activity_time_get() const {
return (time_t)(last_activity_time_->get_value());
}
time_t ctrlm_obj_controller_t::last_key_time_get() const {
return (time_t)(last_key_time_->get_value());
}
void ctrlm_obj_controller_t::last_key_time_set(time_t val) {
last_key_time_->set_value(val);
}
uint16_t ctrlm_obj_controller_t::last_key_code_get() const {
return (uint16_t)(last_key_code_->get_value());
}
void ctrlm_obj_controller_t::last_key_code_set(uint16_t val) {
last_key_code_->set_value(val);
}
void ctrlm_obj_controller_t::last_key_time_update() {
uint64_t current_time = (uint64_t)time(NULL);
last_key_time_->set_value(current_time);
last_activity_time_->set_value(current_time);
if(this->last_key_time_get() > last_key_time_flush_) {
last_key_time_flush_ = this->last_key_time_get() + LAST_KEY_DATABASE_FLUSH_INTERVAL;
ctrlm_db_attr_write(last_key_time_);
ctrlm_db_attr_write(last_key_code_);
ctrlm_db_attr_write(last_activity_time_);
}
}
void ctrlm_obj_controller_t::process_event_key(ctrlm_key_status_t key_status, uint16_t key_code, bool mask) {
last_key_status_ = key_status;
last_key_code_->set_value((uint64_t)key_code);
last_key_time_update();
XLOGD_TELEMETRY("ind_process_keypress: %s - MAC Address <%s>, code = <%d> (%s key), status = <%s>", controller_type_str_get().c_str(),
ieee_address_get().to_string().c_str(),
mask ? -1 : key_code,
ctrlm_linux_key_code_str(key_code, mask),
ctrlm_key_status_str(key_status));
}
ctrlm_controller_capabilities_t ctrlm_obj_controller_t::get_capabilities() const {
return(ctrlm_controller_capabilities_t()); // return empty capabilities object
}
void ctrlm_obj_controller_t::irdb_entry_id_name_set(ctrlm_irdb_dev_type_t type, const std::string &irdb_entry_id_name) {
switch(type) {
case CTRLM_IRDB_DEV_TYPE_TV:
if (irdb_entry_id_name_tv_->to_string() != irdb_entry_id_name) {
irdb_entry_id_name_tv_->set_value(irdb_entry_id_name);
ctrlm_db_attr_write(irdb_entry_id_name_tv_);
}
XLOGD_INFO("TV IRDB Code <%s>", irdb_entry_id_name_tv_->to_string().c_str());
break;
case CTRLM_IRDB_DEV_TYPE_AVR:
if (irdb_entry_id_name_avr_->to_string() != irdb_entry_id_name) {
irdb_entry_id_name_avr_->set_value(irdb_entry_id_name);
ctrlm_db_attr_write(irdb_entry_id_name_avr_);
}
XLOGD_INFO("AVR IRDB Code <%s>", irdb_entry_id_name_avr_->to_string().c_str());
break;
default:
XLOGD_WARN("Invalid type <%d>", type);
break;
}
}
std::string ctrlm_obj_controller_t::get_irdb_entry_id_name_tv() const {
return irdb_entry_id_name_tv_->to_string();
}
std::string ctrlm_obj_controller_t::get_irdb_entry_id_name_avr() const {
return irdb_entry_id_name_avr_->to_string();
}
void ctrlm_obj_controller_t::ota_failure_cnt_incr() {
ota_failure_cnt_session_++;
ota_failure_cnt_from_last_success_->set_value(ota_failure_cnt_from_last_success_->get_value() + 1);
XLOGD_DEBUG("ota_failure_cnt_session_ = %hu, ota_failure_cnt_from_last_success_ = %llu", ota_failure_cnt_session_, ota_failure_cnt_from_last_success_->get_value());
}
void ctrlm_obj_controller_t::ota_clear_all_failure_counters() {
ota_failure_cnt_session_ = 0;
ota_failure_cnt_from_last_success_->set_value(0);
ota_failure_type_z_cnt_set(0);
XLOGD_DEBUG("all OTA failure counters reset to 0");
}
void ctrlm_obj_controller_t::ota_failure_cnt_session_clear() {
ota_failure_cnt_session_ = 0;
XLOGD_DEBUG("ota_failure_cnt_session_ reset to 0");
}
bool ctrlm_obj_controller_t::retry_ota() const {
bool retry = ota_failure_cnt_session_ < OTA_MAX_RETRIES;
XLOGD_WARN("ota_failure_cnt_session_ = <%d>, retry OTA = <%s>",ota_failure_cnt_session_,retry ? "TRUE":"FALSE");
return retry;
}
void ctrlm_obj_controller_t::ota_failure_type_z_cnt_set(uint8_t ota_failures) {
ota_failure_type_z_cnt_ = (ota_failures >= 4) ? 0 : ota_failures;
}
uint8_t ctrlm_obj_controller_t::ota_failure_type_z_cnt_get(void) const {
return ota_failure_type_z_cnt_;
}
bool ctrlm_obj_controller_t::is_controller_type_z(void) const {
return (ota_failure_type_z_cnt_ >= 2) ? true : false;
}
void ctrlm_obj_controller_t::update_voice_metrics(bool is_short_utterance, uint32_t voice_packets_sent, uint32_t voice_packets_lost) {
voice_metrics_->process_time(false);
if(is_short_utterance) {
voice_metrics_->increment_short_voice_count(voice_packets_sent, voice_packets_lost);
} else {
voice_metrics_->increment_voice_count(voice_packets_sent, voice_packets_lost);
}
voice_metrics_->print(__FUNCTION__, true);
}
void ctrlm_obj_controller_t::set_device_minor_id(int device_minor_id) {
XLOGD_DEBUG("Controller %u set device ID to %d", controller_id_get(), device_minor_id);
device_minor_id_ = device_minor_id;
}
int ctrlm_obj_controller_t::get_device_minor_id() const {
return device_minor_id_;
}
bool ctrlm_obj_controller_t::get_connected() const {
XLOGD_WARN("not implemented.");
return false;
}
std::string ctrlm_obj_controller_t::get_name() const {
XLOGD_WARN("not implemented.");
return "";
}
std::string ctrlm_obj_controller_t::get_manufacturer() const {
XLOGD_WARN("not implemented.");
return "";
}
std::string ctrlm_obj_controller_t::get_model() const {
XLOGD_WARN("not implemented.");
return "";
}
ctrlm_sw_version_t ctrlm_obj_controller_t::get_sw_revision() const {
XLOGD_WARN("not implemented.");
return ctrlm_sw_version_t();
}
ctrlm_hw_version_t ctrlm_obj_controller_t::get_hw_revision() const {
XLOGD_WARN("not implemented.");
return ctrlm_hw_version_t();
}
std::string ctrlm_obj_controller_t::get_fw_revision() const {
XLOGD_WARN("not implemented.");
return "";
}
std::string ctrlm_obj_controller_t::get_serial_number() const {
XLOGD_WARN("not implemented.");
return "";
}
uint8_t ctrlm_obj_controller_t::get_battery_percent() const {
XLOGD_WARN("not implemented.");
return 0;
}
uint16_t ctrlm_obj_controller_t::get_last_wakeup_key() const {
XLOGD_WARN("not implemented.");
return 0;
}
ctrlm_rcu_wakeup_config_t ctrlm_obj_controller_t::get_wakeup_config() const {
XLOGD_WARN("not implemented.");
return CTRLM_RCU_WAKEUP_CONFIG_INVALID;
}
std::vector<uint16_t> ctrlm_obj_controller_t::get_wakeup_custom_list() const {
XLOGD_WARN("not implemented.");
return {};
}
void ctrlm_obj_controller_t::set_upgrade_progress(uint8_t progress) {
upgrade_progress_ = progress;
ctrlm_rcu_upgrade_state_t previous_state = get_upgrade_state();
ctrlm_rcu_upgrade_state_t new_state = CTRLM_RCU_UPGRADE_STATE_INVALID;
if (upgrade_progress_ >= 0 && upgrade_progress_ < 100) { // pending
new_state = CTRLM_RCU_UPGRADE_STATE_PENDING;
set_upgrade_error(""); // clear error messaging if pending
} else if (upgrade_progress_ == 100) {
new_state = CTRLM_RCU_UPGRADE_STATE_SUCCESS;
} else if (upgrade_progress_ == 255) {
// set state to canceled if it was pending
if (previous_state == CTRLM_RCU_UPGRADE_STATE_PENDING) {
new_state = CTRLM_RCU_UPGRADE_STATE_CANCELED;
} else {
new_state = CTRLM_RCU_UPGRADE_STATE_IDLE;
}
set_upgrade_increment(-1);
}
set_upgrade_state(new_state);
}
uint8_t ctrlm_obj_controller_t::get_upgrade_progress() const {
return upgrade_progress_;
}
void ctrlm_obj_controller_t::set_upgrade_state(ctrlm_rcu_upgrade_state_t state) {
upgrade_state_ = state;
}
ctrlm_rcu_upgrade_state_t ctrlm_obj_controller_t::get_upgrade_state() const {
return upgrade_state_;
}
void ctrlm_obj_controller_t::set_upgrade_error(const std::string &error_str) {
if (error_str.empty()) {
upgrade_error_msg_.clear();
return;
}
upgrade_error_msg_ = error_str;
}
std::string ctrlm_obj_controller_t::get_upgrade_error() const {
return upgrade_error_msg_;
}
void ctrlm_obj_controller_t::set_upgrade_session_uuid(bool generate) {
if (!generate) {
upgrade_session_uuid_.clear();
return;
}
uuid_t session_uuid;
char tmp[37];
uuid_generate(session_uuid);
uuid_unparse(session_uuid, tmp);
upgrade_session_uuid_ = std::string(tmp);
}
std::string ctrlm_obj_controller_t::get_upgrade_session_uuid() const {
return upgrade_session_uuid_;
}
void ctrlm_obj_controller_t::set_upgrade_increment(uint8_t increment) {
if (increment == 0) { // cannot be 0 as modulo 0 is undefined behavior
increment = -1;
}
upgrade_increment_ = increment;
}
uint8_t ctrlm_obj_controller_t::get_upgrade_increment() const {
return upgrade_increment_;
}
bool ctrlm_obj_controller_t::is_upgrade_progress_at_increment() const {
return ((upgrade_progress_ % upgrade_increment_ == 0) || (upgrade_progress_ == 100));
}