21 #include "pwrlogger.h"
23 #ifdef FTUE_CHECK_ENABLED
34 static const char *ftue_flag_file =
"/opt/persistent/ftue_complete.flag";
37 #ifdef FTUE_CHECK_ENABLED
39 static bool convert_text_encoding(
char * input, std::string &output,
size_t input_size)
42 iconv_t conv = iconv_open(
"UTF-8",
"UTF-16LE");
43 if((
size_t)-1 == (
size_t)conv)
45 LOG(
"%s: there was an error.\n", __func__);
49 char output_buffer[1024] = {0};
50 char * output_ptr = &output_buffer[0];
51 size_t out_bytes_left =
sizeof(output_buffer);
52 size_t result = iconv(conv, &input, &input_size, &output_ptr, &out_bytes_left);
53 if((
size_t)-1 == result)
55 LOG(
"%s: conversion failed.\n", __func__);
60 output = output_buffer;
66 static bool convert_text_encoding2(
const char *input, std::string &output,
size_t input_length)
72 static const unsigned int MAX_BUFFER_SIZE = 1024;
75 if (0 != (input_length % 2))
77 LOG(
"%s: Input buffer length is not a multiple of 2. This cannot be UTF-16.\n", __func__);
81 size_t output_length = input_length / 2;
82 if (output_length > MAX_BUFFER_SIZE)
85 output_length = MAX_BUFFER_SIZE;
86 input_length = output_length * 2;
89 char *output_buffer =
static_cast<char *
>(malloc(output_length + 1));
90 if (
nullptr == output_buffer)
92 LOG(
"%s: Failed to allocate memory.\n", __func__);
95 output_buffer[output_length] = 0;
97 for (
int i = 0, j = 0; i < input_length; i += 2)
100 if ((0 == input[i + 1]) && (0x20 <= ch) && (0x7e >= ch))
101 output_buffer[j++] = ch;
103 output = output_buffer;
108 static int get_last_station(
const std::string &input)
110 const char *search_pattern = R
"(lastStation":)";
112 std::size_t start = input.find(search_pattern);
113 if (std::string::npos != start)
115 start += strlen(search_pattern);
116 std::size_t stop = input.find(
",", start);
117 if (std::string::npos != stop)
119 std::string station_number_text = input.substr(start, (stop - start));
122 ret = std::stoi(station_number_text);
126 LOG(
"%s: Exception when converting to number.\n", __func__);
131 LOG(
"%s: Could not locate end of string\n", __func__);
134 LOG(
"%s: Search pattern <%s> not found\n", __func__, search_pattern);
138 static bool set_ftue_status_from_ra_store()
140 const char *resident_app_local_storage =
"/opt/persistent/rdkservices/ResidentApp/wpe/local-storage/http_platco.thor.local_50050.localstorage";
142 const char *query = R
"(select * from ItemTable where key = "ftue";)";
143 sqlite3_stmt *prepared_statement = nullptr;
144 static const int FTUE_DONE_STATION_NUMBER = 14;
145 bool retStatus =
false;
147 LOG(
"%s: start.\n", __func__);
148 int ret = sqlite3_open_v2(resident_app_local_storage, &db, SQLITE_OPEN_READWRITE, NULL);
152 LOG(
"%s: Can't open database: %s\n", __func__, sqlite3_errmsg(db));
155 ret = sqlite3_prepare_v2(db, query, -1, &prepared_statement,
nullptr);
156 if (SQLITE_OK != ret)
158 LOG(
"%s: Prepare failed: %s\n", __func__, sqlite3_errmsg(db));
164 ret = sqlite3_step(prepared_statement);
170 const char *second_column =
static_cast<const char *
>(sqlite3_column_blob(prepared_statement, 1));
171 int data_size = sqlite3_column_bytes(prepared_statement, 1);
173 std::string converted_text;
174 if (
true == convert_text_encoding2(second_column, converted_text, data_size))
176 int station = get_last_station(converted_text);
177 LOG(
"%s: station number %d\n", __func__, station);
178 if (FTUE_DONE_STATION_NUMBER == station)
180 LOG(
"%s: detected FTUE complete.\n", __func__);
181 std::ofstream flag(ftue_flag_file);
182 if (
false == flag.is_open())
183 LOG(
"%s: Error creating file %s\n", __func__, ftue_flag_file);
195 LOG(
"Step returned unhandled error %d\n", ret);
197 }
while (SQLITE_ROW == ret);
199 sqlite3_finalize(prepared_statement);
203 LOG(
"%s: exit.\n", __func__);
205 #endif // FTUE_CHECK_ENABLED
207 bool isTVOperatingInFactory()
209 #ifdef FTUE_CHECK_ENABLED
211 if (0 == access(ftue_flag_file, F_OK))
219 static const auto CALLBACK_DELAY_SECONDS = 3;
220 auto deferred_callback = [](gpointer data)
222 bool retFtueStatus = set_ftue_status_from_ra_store();
223 LOG(
"%s: %s\n", __func__, (
true == retFtueStatus ?
"true" :
"false"));
224 return (retFtueStatus)?G_SOURCE_REMOVE:G_SOURCE_CONTINUE;
226 g_timeout_add_seconds(CALLBACK_DELAY_SECONDS, deferred_callback,
nullptr);
228 LOG(
"%s: %s\n", __func__, (
true == ret ?
"true" :
"false"));
231 LOG(
"%s: Warning! This is an unhandled control flow.\n", __func__);