39 #include "btrMgr_logger.h"
46 #define BTRMGR_PI_DEVID_LEN 17
47 #define BTRMGR_PI_VOL_LEN 4
54 static char* readPersistentFile (
char* fileContent);
55 static void writeToPersistentFile (
char* fileName,cJSON* profileData);
67 char *fileContent = NULL;
68 BTRMGRLOG_INFO (
"Reading file - %s\n", fileName);
70 if (0 == access(fileName, F_OK)) {
71 fp = fopen(fileName,
"r");
73 BTRMGRLOG_ERROR (
"Could not open file - %s\n", fileName);
77 fseek(fp, 0, SEEK_END);
79 fseek(fp, 0, SEEK_SET);
82 fileContent = (
char *) malloc(
sizeof(
char) * (ch_count + 1));
83 if (fileContent != NULL) {
84 if (fread(fileContent, 1, ch_count, fp) != ch_count) {
85 BTRMGRLOG_ERROR (
"fileContent not read - %s\n", fileName);
88 fileContent[ch_count] =
'\0';
91 BTRMGRLOG_DEBUG (
"Reading %s success, Content = %s \n", fileName,fileContent);
95 BTRMGRLOG_WARN (
"File %s does not exist!!!", fileName);
102 writeToPersistentFile (
107 BTRMGRLOG_INFO(
"Writing data to file %s\n" ,fileName);
109 fp = fopen(fileName,
"w");
111 BTRMGRLOG_ERROR (
"Could not open file to write, - %s\n" ,fileName);
114 char* fileContent = cJSON_Print(profileData);
115 fprintf(fp,
"%s", fileContent);
117 BTRMGRLOG_DEBUG (
"Writing data to file - %s, Content - %s\n" ,fileName,fileContent);
118 BTRMGRLOG_DEBUG (
"File write Success\n");
128 tBTRMgrPIHdl* hBTRMgrPiHdl
133 BTRMGRLOG_ERROR (
"BTRMgr_PI_Init FAILED\n");
134 return eBTRMgrFailure;
137 memset(piHandle->piData.limitBeaconDetection,
'\0', BTRMGR_NAME_LEN_MAX);
139 *hBTRMgrPiHdl = (tBTRMgrPIHdl) piHandle;
140 return eBTRMgrSuccess;
146 tBTRMgrPIHdl hBTRMgrPiHdl
150 if ( NULL != pstBtrMgrPiHdl) {
151 free((
void*)pstBtrMgrPiHdl);
152 pstBtrMgrPiHdl = NULL;
153 BTRMGRLOG_INFO (
"BTRMgr_PI_DeInit SUCCESS\n");
154 return eBTRMgrSuccess;
157 BTRMGRLOG_WARN (
"BTRMgr PI handle is not Inited(NULL)\n");
158 return eBTRMgrFailure;
164 BTRMgr_PI_GetLEBeaconLimitingStatus (
167 char *persistent_file_content = NULL;
169 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
170 if(persistent_file_content == NULL) {
172 return eBTRMgrFailure;
175 cJSON *btData = cJSON_Parse(persistent_file_content);
176 free(persistent_file_content);
179 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
180 return eBTRMgrFailure;
183 cJSON * limitDetection = cJSON_GetObjectItem(btData,
"LimitBeaconDetection");
184 if(limitDetection == NULL) {
186 BTRMGRLOG_ERROR (
"Could not able to get limit value for beacon detection from JSON\n");
187 return eBTRMgrFailure;
190 strcpy(persistentData->limitBeaconDetection ,limitDetection->valuestring);
191 return eBTRMgrSuccess;
195 BTRMgr_PI_SetLEBeaconLimitingStatus (
198 char *persistent_file_content = NULL;
200 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
201 if(persistent_file_content == NULL) {
203 return eBTRMgrFailure;
206 cJSON *btData = cJSON_Parse(persistent_file_content);
207 free(persistent_file_content);
210 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
211 return eBTRMgrFailure;
215 BTRMGRLOG_DEBUG (
"Appending object to JSON file\n");
216 cJSON *limitDetection = cJSON_GetObjectItem(btData,
"LimitBeaconDetection");
217 if(limitDetection == NULL) {
218 cJSON_AddStringToObject(btData,
"LimitBeaconDetection", persistentData->limitBeaconDetection);
219 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
221 else if (strcmp(limitDetection->valuestring, persistentData->limitBeaconDetection)) {
222 strcpy(limitDetection->valuestring, persistentData->limitBeaconDetection);
223 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
226 return eBTRMgrSuccess;
229 #ifdef RDKTV_PERSIST_VOLUME_SKY
232 BTRMgr_PI_GetVolume (
233 BTRMGR_Volume_PersistentData_t* persistentData
235 char *persistent_file_content = NULL;
237 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
238 if(persistent_file_content == NULL) {
240 return eBTRMgrFailure;
243 cJSON *btData = cJSON_Parse(persistent_file_content);
244 free(persistent_file_content);
247 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
248 return eBTRMgrFailure;
251 cJSON * Volume = cJSON_GetObjectItem(btData,
"Volume");
254 BTRMGRLOG_ERROR (
"Could not able to get value of volume detection from JSON\n");
255 return eBTRMgrFailure;
258 persistentData->Volume = (
unsigned char ) atoi (Volume->valuestring);
259 return eBTRMgrSuccess;
263 BTRMgr_PI_SetVolume (
264 BTRMGR_Volume_PersistentData_t* persistentData
266 char *persistent_file_content = NULL;
268 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
269 if(persistent_file_content == NULL) {
271 return eBTRMgrFailure;
274 cJSON *btData = cJSON_Parse(persistent_file_content);
275 free(persistent_file_content);
278 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
279 return eBTRMgrFailure;
283 BTRMGRLOG_INFO (
"Appending object to JSON file\n");
284 char VolStr[BTRMGR_PI_VOL_LEN];
285 memset(VolStr,
'\0', BTRMGR_PI_VOL_LEN);
286 sprintf(VolStr,
"%d",persistentData->Volume);
287 cJSON *Volume = cJSON_GetObjectItem(btData,
"Volume");
288 if (Volume == NULL) {
289 cJSON_AddStringToObject(btData,
"Volume", VolStr);
290 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
292 else if (strcmp(Volume->valuestring, VolStr)) {
293 strcpy(Volume->valuestring, VolStr);
294 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
298 return eBTRMgrSuccess;
304 BTRMGR_Mute_PersistentData_t* persistentData
306 char *persistent_file_content = NULL;
308 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
309 if(persistent_file_content == NULL) {
311 return eBTRMgrFailure;
314 cJSON *btData = cJSON_Parse(persistent_file_content);
315 free(persistent_file_content);
318 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
319 return eBTRMgrFailure;
322 cJSON *Mute = cJSON_GetObjectItem(btData,
"Mute");
325 BTRMGRLOG_ERROR (
"Could not able to get limit value for beacon detection from JSON\n");
326 return eBTRMgrFailure;
329 strcpy(persistentData->Mute ,Mute->valuestring);
330 return eBTRMgrSuccess;
335 BTRMGR_Mute_PersistentData_t* persistentData
337 char *persistent_file_content = NULL;
339 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
340 if(persistent_file_content == NULL) {
342 return eBTRMgrFailure;
345 cJSON *btData = cJSON_Parse(persistent_file_content);
346 free(persistent_file_content);
349 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
350 return eBTRMgrFailure;
354 BTRMGRLOG_INFO (
"Appending object to JSON file\n");
355 cJSON *Mute = cJSON_GetObjectItem(btData,
"Mute");
357 cJSON_AddStringToObject(btData,
"Mute", persistentData->Mute);
358 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
360 else if (strcmp(Mute->valuestring, persistentData->Mute)) {
361 strcpy(Mute->valuestring, persistentData->Mute);
362 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
365 return eBTRMgrSuccess;
371 tBTRMgrPIHdl hBTRMgrPiHdl,
374 char *persistent_file_content = NULL;
375 int profileCount = 0;
383 if (pstBtrMgrPiHdl == NULL) {
384 BTRMGRLOG_ERROR (
"PI Handle not initialized\n");
385 return eBTRMgrFailure;
389 persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
391 if (persistent_file_content == NULL) {
392 memset (persistentData->limitBeaconDetection,
'\0', BTRMGR_NAME_LEN_MAX);
393 strcpy(persistentData->limitBeaconDetection,
"false");
394 return eBTRMgrFailure;
397 cJSON *btProfileData = cJSON_Parse(persistent_file_content);
398 free(persistent_file_content);
399 if (btProfileData == NULL) {
401 BTRMGRLOG_ERROR (
"Could not parse JSON data file - Corrupted JSON\n");
402 return eBTRMgrFailure;
405 cJSON * adpaterIdptr = cJSON_GetObjectItem(btProfileData,
"AdapterId");
406 if (adpaterIdptr == NULL) {
408 BTRMGRLOG_ERROR (
"Could not able to get AdapterId from JSON\n");
409 return eBTRMgrFailure;
411 strcpy(persistentData->adapterId,adpaterIdptr->valuestring);
413 cJSON *limitDetection = cJSON_GetObjectItem(btProfileData,
"LimitBeaconDetection");
414 if(limitDetection != NULL) {
415 strcpy(persistentData->limitBeaconDetection, limitDetection->valuestring);
418 memset (persistentData->limitBeaconDetection,
'\0', BTRMGR_NAME_LEN_MAX);
419 strcpy(persistentData->limitBeaconDetection,
"false");
422 cJSON *btProfiles = cJSON_GetObjectItem(btProfileData,
"Profiles");
423 if (btProfiles != NULL) {
425 profileCount = cJSON_GetArraySize(btProfiles);
426 BTRMGRLOG_DEBUG (
"Successfully Parsed Persistent profile, Profile count = %d\n",profileCount);
428 persistentData->numOfProfiles = profileCount;
429 for (pcount = 0; pcount < profileCount; pcount++ ) {
430 char* profileId = NULL;
431 cJSON *profile = cJSON_GetArrayItem(btProfiles, pcount);
432 if(cJSON_GetObjectItem(profile,
"ProfileId")) {
434 profileId = cJSON_GetObjectItem(profile,
"ProfileId")->valuestring;
436 strcpy(persistentData->profileList[pcount].profileId,profileId);
439 cJSON *btDevices = cJSON_GetObjectItem(profile,
"Devices");
440 deviceCount = cJSON_GetArraySize(btDevices);
441 persistentData->profileList[pcount].numOfDevices = deviceCount;
442 BTRMGRLOG_DEBUG (
"Parsing device details, %d devices found for profile %s\n",deviceCount,profileId);
444 for(dcount = 0; dcount<deviceCount; dcount++) {
445 char* deviceId = NULL;
447 cJSON *device = cJSON_GetArrayItem(btDevices, dcount);
448 if(cJSON_GetObjectItem(device,
"DeviceId")) {
449 deviceId = cJSON_GetObjectItem(device,
"DeviceId")->valuestring;
451 persistentData->profileList[pcount].deviceList[dcount].deviceId = strtoll(deviceId,NULL,10);
453 if(cJSON_GetObjectItem(device,
"ConnectionStatus")->valueint)
454 isConnect = cJSON_GetObjectItem(device,
"ConnectionStatus")->valueint;
456 persistentData->profileList[pcount].deviceList[dcount].isConnected = isConnect;
457 BTRMGRLOG_DEBUG (
"Parsing device details, Device- %s, Status-%d, Profile-%s\n",deviceId,isConnect,profileId);
467 BTRMGRLOG_ERROR (
"Could not able to get Profile Lists from JSON\n");
468 return eBTRMgrFailure;
471 return eBTRMgrSuccess;
477 tBTRMgrPIHdl hBTRMgrPiHdl,
483 int isObjectAdded = 0;
488 if ((pstBtrMgrPiHdl == NULL) && (persistProfile == NULL)) {
489 BTRMGRLOG_ERROR (
"PI Handle and Persist profile not initialized\n");
490 return eBTRMgrFailure;
494 int profileCount = piData.numOfProfiles;
495 if( profileCount > 0) {
496 BTRMGRLOG_DEBUG (
"Profile Count >0 need to append profile \n");
498 for(pcount=0; pcount<profileCount ; pcount++) {
499 if(strcmp(piData.profileList[pcount].profileId,persistProfile->profileId) == 0) {
500 BTRMGRLOG_DEBUG (
"Profile entry already exists,need to add device alone \n");
501 int deviceCnt = piData.profileList[pcount].numOfDevices;
504 for(dcount = 0; dcount < deviceCnt; dcount++) {
505 if(piData.profileList[pcount].deviceList[dcount].deviceId == persistProfile->deviceId) {
507 BTRMGRLOG_ERROR (
"Adding Failed Duplicate entry found - %lld\n",persistProfile->deviceId);
508 return eBTRMgrFailure;
512 BTRMGRLOG_DEBUG (
"Not a duplicate device appending new device to deviceLits- %lld \n",persistProfile->deviceId);
513 piData.profileList[pcount].deviceList[dcount].deviceId = persistProfile->deviceId;
514 piData.profileList[pcount].deviceList[dcount].isConnected = persistProfile->isConnect;
515 piData.profileList[pcount].numOfDevices++;
521 if(0 == isObjectAdded) {
522 BTRMGRLOG_DEBUG (
"New Profile found, add to profile list - %s \n",persistProfile->profileId);
523 piData.profileList[pcount].numOfDevices = 1;
524 strcpy(piData.profileList[pcount].profileId,persistProfile->profileId);
525 piData.profileList[pcount].deviceList[0].deviceId = persistProfile->deviceId;
526 piData.profileList[pcount].deviceList[0].isConnected = persistProfile->isConnect;
527 piData.numOfProfiles++;
529 BTRMGRLOG_DEBUG (
"New Profile added - %s \n",persistProfile->profileId);
533 BTRMGRLOG_DEBUG (
"Data is empty creating new entry - %s \n",persistProfile->profileId);
534 strcpy(piData.adapterId,persistProfile->adapterId);
535 piData.numOfProfiles = 1;
536 piData.profileList[0].numOfDevices =1;
537 strcpy(piData.profileList[0].profileId,persistProfile->profileId );
538 piData.profileList[0].deviceList[0].deviceId = persistProfile->deviceId;
539 piData.profileList[0].deviceList[0].isConnected = persistProfile->isConnect;
544 BTRMGRLOG_DEBUG (
"Writing changes to file - %s \n",persistProfile->profileId);
548 return eBTRMgrSuccess;
553 tBTRMgrPIHdl hBTRMgrPiHdl,
556 int profileCount = 0;
559 cJSON *Profiles = NULL;
560 cJSON *devices = NULL;
561 cJSON *Profile = NULL;
562 cJSON *piData = NULL;
566 if (pstBtrMgrPiHdl == NULL) {
567 BTRMGRLOG_ERROR (
"PI Handle not initialized\n");
568 return eBTRMgrFailure;
571 profileCount = persistentData->numOfProfiles;
572 piData = cJSON_CreateObject();
573 Profiles = cJSON_CreateArray();
574 BTRMGRLOG_DEBUG (
"Writing object to JSON\n");
575 for (pcount = 0; pcount <profileCount; pcount++) {
577 int deviceCount = persistentData->profileList[pcount].numOfDevices;
578 if(deviceCount > 0) {
579 BTRMGRLOG_DEBUG (
"Device count > 0 , Count = %d\n",deviceCount);
580 devices = cJSON_CreateArray();
581 for(dcount = 0; dcount<deviceCount; dcount++) {
582 cJSON* device = cJSON_CreateObject();
583 char deviceId[BTRMGR_PI_DEVID_LEN];
584 memset(deviceId,
'\0', BTRMGR_PI_DEVID_LEN);
585 sprintf(deviceId,
"%lld",persistentData->profileList[pcount].deviceList[dcount].deviceId );
586 cJSON_AddStringToObject(device,
"DeviceId",deviceId );
587 cJSON_AddNumberToObject(device,
"ConnectionStatus",persistentData->profileList[pcount].deviceList[dcount].isConnected);
588 cJSON_AddItemToArray(devices, device);
589 BTRMGRLOG_DEBUG (
"Device Added :- %s, Status- %d,\n",deviceId,persistentData->profileList[pcount].deviceList[dcount].isConnected);
591 Profile = cJSON_CreateObject();
592 cJSON_AddStringToObject(Profile,
"ProfileId",persistentData->profileList[pcount].profileId);
593 cJSON_AddItemToObject(Profile,
"Devices", devices);
594 cJSON_AddItemToArray(Profiles, Profile);
597 BTRMGRLOG_ERROR (
"Empty device list could not set\n");
602 if (profileCount != 0) {
603 cJSON_AddStringToObject(piData,
"AdapterId",persistentData->adapterId);
604 cJSON_AddItemToObject(piData,
"Profiles", Profiles);
605 BTRMGRLOG_DEBUG (
"Writing Profile details - %s\n",persistentData->profileList[pcount].profileId);
608 BTRMGRLOG_DEBUG (
"Writing empty data\n");
611 if (persistentData->limitBeaconDetection && *(persistentData->limitBeaconDetection) !=
'\0') {
612 cJSON_AddStringToObject(piData,
"LimitBeaconDetection", persistentData->limitBeaconDetection);
615 writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH,piData);
616 cJSON_Delete(piData);
618 return eBTRMgrSuccess;
623 tBTRMgrPIHdl hBTRMgrPiHdl,
629 int isObjectRemoved = 0;
633 if ((pstBtrMgrPiHdl == NULL) && (persistProfile == NULL)) {
634 BTRMGRLOG_ERROR (
"PI Handle and Persist profile not initialized\n");
635 return eBTRMgrFailure;
638 BTRMGRLOG_DEBUG (
"Removing profile - %s\n",persistProfile->profileId);
640 int profileCount = piData.numOfProfiles;
641 if( profileCount > 0) {
642 BTRMGRLOG_DEBUG (
"Profiles not empty- %s\n",persistProfile->profileId);
643 for(pcount = 0; pcount < profileCount; pcount++) {
644 if(strcmp(piData.profileList[pcount].profileId,persistProfile->profileId) == 0) {
645 BTRMGRLOG_DEBUG (
"Profile match found - %s find device\n",persistProfile->profileId);
646 int deviceCnt = piData.profileList[pcount].numOfDevices;
649 for(dcount = 0; dcount < deviceCnt; dcount++) {
650 if(piData.profileList[pcount].deviceList[dcount].deviceId == persistProfile->deviceId) {
651 BTRMGRLOG_DEBUG (
"Profile match found && Device Match Found - %s Deleting device %lld\n",
652 persistProfile->profileId, persistProfile->deviceId);
653 piData.profileList[pcount].deviceList[dcount].deviceId = 0;
654 piData.profileList[pcount].deviceList[dcount].isConnected = 0;
655 piData.profileList[pcount].numOfDevices--;
657 BTRMGRLOG_DEBUG (
"Profile match found && Device Match Found - %s Delete Success %lld\n",
658 persistProfile->profileId, persistProfile->deviceId);
663 if(isObjectRemoved && piData.profileList[pcount].numOfDevices == 0) {
664 memset(piData.profileList[pcount].profileId, 0,BTRMGR_NAME_LEN_MAX );
665 piData.numOfProfiles--;
668 else if (!isObjectRemoved && (pcount == (profileCount - 1))) {
670 BTRMGRLOG_ERROR (
"Profile Not found, Could not delete %s\n",persistProfile->profileId);
671 return eBTRMgrFailure;
675 if(isObjectRemoved && piData.numOfProfiles == 0) {
676 memset(piData.adapterId, 0,BTRMGR_NAME_LEN_MAX );
679 if(isObjectRemoved) {
685 BTRMGRLOG_ERROR (
"Nothing to delete, Profile is empty \n");
686 return eBTRMgrFailure;
689 return eBTRMgrSuccess;
694 BTRMgr_PI_GetProfile (
699 return eBTRMgrSuccess;