RDK Documentation (Open Sourced RDK Components)
btrMgr_persistIface.c
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 /**
20  * @file btrMgr_persistIface.c
21  *
22  * @description This file defines bluetooth manager's Persistent storage interfaces
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 /* System Headers */
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 
35 /* Ext lib Headers */
36 #include "cJSON.h"
37 
38 /* Interface lib Headers */
39 #include "btrMgr_logger.h"
40 
41 /* Local Headers */
42 #include "btrMgr_Types.h"
43 #include "btrMgr_persistIface.h"
44 
45 
46 #define BTRMGR_PI_DEVID_LEN 17
47 #define BTRMGR_PI_VOL_LEN 4
48 
49 typedef struct _stBTRMgrPIHdl {
52 
53 /* Static Function Prototypes */
54 static char* readPersistentFile (char* fileContent);
55 static void writeToPersistentFile (char* fileName,cJSON* profileData);
56 
57 /* Local Op Threads Prototypes*/
58 
59 /* Incoming Callbacks Prototypes*/
60 
61 /* Static Function Definition */
62 static char*
63 readPersistentFile (
64  char* fileName
65 ) {
66  FILE *fp = NULL;
67  char *fileContent = NULL;
68  BTRMGRLOG_INFO ("Reading file - %s\n", fileName);
69 
70  if (0 == access(fileName, F_OK)) {
71  fp = fopen(fileName, "r");
72  if (fp == NULL) {
73  BTRMGRLOG_ERROR ("Could not open file - %s\n", fileName);
74  }
75  else {
76  int ch_count = 0;
77  fseek(fp, 0, SEEK_END);
78  ch_count = ftell(fp);
79  fseek(fp, 0, SEEK_SET);
80 
81  if(ch_count > 0) {
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);
86  } //CID:23373 - Checked return
87  }
88  fileContent[ch_count] ='\0';
89  } //CID:23376 - Negative retuns
90  fclose(fp);
91  BTRMGRLOG_DEBUG ("Reading %s success, Content = %s \n", fileName,fileContent);
92  }
93  }
94  else {
95  BTRMGRLOG_WARN ("File %s does not exist!!!", fileName);
96  }
97 
98  return fileContent;
99 }
100 
101 static void
102 writeToPersistentFile (
103  char* fileName,
104  cJSON* profileData
105 ) {
106  FILE *fp = NULL;
107  BTRMGRLOG_INFO("Writing data to file %s\n" ,fileName);
108 
109  fp = fopen(fileName, "w");
110  if (fp == NULL) {
111  BTRMGRLOG_ERROR ("Could not open file to write, - %s\n" ,fileName);
112  }
113  else {
114  char* fileContent = cJSON_Print(profileData);
115  fprintf(fp, "%s", fileContent);
116  fclose(fp);
117  BTRMGRLOG_DEBUG ("Writing data to file - %s, Content - %s\n" ,fileName,fileContent);
118  BTRMGRLOG_DEBUG ("File write Success\n");
119  }
120 }
121 
122 /* Local Op Threads */
123 
124 
125  /* Interfaces */
128  tBTRMgrPIHdl* hBTRMgrPiHdl
129 ) {
130  stBTRMgrPIHdl* piHandle = NULL;
131 
132  if ((piHandle = (stBTRMgrPIHdl*)malloc (sizeof(stBTRMgrPIHdl))) == NULL) {
133  BTRMGRLOG_ERROR ("BTRMgr_PI_Init FAILED\n");
134  return eBTRMgrFailure;
135  }
136 
137  memset(piHandle->piData.limitBeaconDetection, '\0', BTRMGR_NAME_LEN_MAX);
138 
139  *hBTRMgrPiHdl = (tBTRMgrPIHdl) piHandle;
140  return eBTRMgrSuccess;
141 }
142 
143 
146  tBTRMgrPIHdl hBTRMgrPiHdl
147 ) {
148  stBTRMgrPIHdl* pstBtrMgrPiHdl = (stBTRMgrPIHdl*)hBTRMgrPiHdl;
149 
150  if ( NULL != pstBtrMgrPiHdl) {
151  free((void*)pstBtrMgrPiHdl);
152  pstBtrMgrPiHdl = NULL;
153  BTRMGRLOG_INFO ("BTRMgr_PI_DeInit SUCCESS\n");
154  return eBTRMgrSuccess;
155  }
156  else {
157  BTRMGRLOG_WARN ("BTRMgr PI handle is not Inited(NULL)\n");
158  return eBTRMgrFailure;
159  }
160 }
161 
162 /*Creating a persistent get/set api for limiting beacon detection */
164 BTRMgr_PI_GetLEBeaconLimitingStatus (
165  BTRMGR_Beacon_PersistentData_t* persistentData
166 ) {
167  char *persistent_file_content = NULL;
168 
169  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
170  if(persistent_file_content == NULL) {
171  // Seems like file is empty
172  return eBTRMgrFailure;
173  }
174 
175  cJSON *btData = cJSON_Parse(persistent_file_content);
176  free(persistent_file_content);
177  if(btData == NULL) {
178  // Corrupted JSON
179  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
180  return eBTRMgrFailure;
181  }
182 
183  cJSON * limitDetection = cJSON_GetObjectItem(btData,"LimitBeaconDetection");
184  if(limitDetection == NULL) {
185  // Corrupted JSON
186  BTRMGRLOG_ERROR ("Could not able to get limit value for beacon detection from JSON\n");
187  return eBTRMgrFailure;
188  }
189 
190  strcpy(persistentData->limitBeaconDetection ,limitDetection->valuestring);
191  return eBTRMgrSuccess;
192 }
193 
195 BTRMgr_PI_SetLEBeaconLimitingStatus (
196  BTRMGR_Beacon_PersistentData_t* persistentData
197 ) {
198  char *persistent_file_content = NULL;
199 
200  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
201  if(persistent_file_content == NULL) {
202  // Seems like file is empty
203  return eBTRMgrFailure;
204  }
205 
206  cJSON *btData = cJSON_Parse(persistent_file_content);
207  free(persistent_file_content);
208  if(btData == NULL) {
209  // Corrupted JSON
210  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
211  return eBTRMgrFailure;
212  }
213 
214  /*Adding limit for beacon detection*/
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);
220  }
221  else if (strcmp(limitDetection->valuestring, persistentData->limitBeaconDetection)) { // Only if Beacon change write persistent memory
222  strcpy(limitDetection->valuestring, persistentData->limitBeaconDetection);
223  writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
224  }
225 
226  return eBTRMgrSuccess;
227 }
228 
229 #ifdef RDKTV_PERSIST_VOLUME_SKY
230 /*Creating a persistent get/set api for value of volume */
232 BTRMgr_PI_GetVolume (
233  BTRMGR_Volume_PersistentData_t* persistentData
234 ) {
235  char *persistent_file_content = NULL;
236 
237  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
238  if(persistent_file_content == NULL) {
239  // Seems like file is empty
240  return eBTRMgrFailure;
241  }
242 
243  cJSON *btData = cJSON_Parse(persistent_file_content);
244  free(persistent_file_content);
245  if(btData == NULL) {
246  // Corrupted JSON
247  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
248  return eBTRMgrFailure;
249  }
250 
251  cJSON * Volume = cJSON_GetObjectItem(btData,"Volume");
252  if(Volume == NULL) {
253  // Corrupted JSON
254  BTRMGRLOG_ERROR ("Could not able to get value of volume detection from JSON\n");
255  return eBTRMgrFailure;
256  }
257 
258  persistentData->Volume = (unsigned char ) atoi (Volume->valuestring);
259  return eBTRMgrSuccess;
260 }
261 
263 BTRMgr_PI_SetVolume (
264  BTRMGR_Volume_PersistentData_t* persistentData
265 ) {
266  char *persistent_file_content = NULL;
267 
268  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
269  if(persistent_file_content == NULL) {
270  // Seems like file is empty
271  return eBTRMgrFailure;
272  }
273 
274  cJSON *btData = cJSON_Parse(persistent_file_content);
275  free(persistent_file_content);
276  if(btData == NULL) {
277  // Corrupted JSON
278  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
279  return eBTRMgrFailure;
280  }
281 
282  /*Adding value of volume */
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);
291  }
292  else if (strcmp(Volume->valuestring, VolStr)) { // Only if Volume changed write to persistence
293  strcpy(Volume->valuestring, VolStr);
294  writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
295  }
296 
297 
298  return eBTRMgrSuccess;
299 }
300 
301 /*Creating a persistent get/set api for limiting beacon detection */
303 BTRMgr_PI_GetMute (
304  BTRMGR_Mute_PersistentData_t* persistentData
305 ) {
306  char *persistent_file_content = NULL;
307 
308  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
309  if(persistent_file_content == NULL) {
310  // Seems like file is empty
311  return eBTRMgrFailure;
312  }
313 
314  cJSON *btData = cJSON_Parse(persistent_file_content);
315  free(persistent_file_content);
316  if(btData == NULL) {
317  // Corrupted JSON
318  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
319  return eBTRMgrFailure;
320  }
321 
322  cJSON *Mute = cJSON_GetObjectItem(btData,"Mute");
323  if(Mute == NULL) {
324  // Corrupted JSON
325  BTRMGRLOG_ERROR ("Could not able to get limit value for beacon detection from JSON\n");
326  return eBTRMgrFailure;
327  }
328 
329  strcpy(persistentData->Mute ,Mute->valuestring);
330  return eBTRMgrSuccess;
331 }
332 
334 BTRMgr_PI_SetMute (
335  BTRMGR_Mute_PersistentData_t* persistentData
336 ) {
337  char *persistent_file_content = NULL;
338 
339  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
340  if(persistent_file_content == NULL) {
341  // Seems like file is empty
342  return eBTRMgrFailure;
343  }
344 
345  cJSON *btData = cJSON_Parse(persistent_file_content);
346  free(persistent_file_content);
347  if(btData == NULL) {
348  // Corrupted JSON
349  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
350  return eBTRMgrFailure;
351  }
352 
353  /*Adding limit for beacon detection*/
354  BTRMGRLOG_INFO ("Appending object to JSON file\n");
355  cJSON *Mute = cJSON_GetObjectItem(btData,"Mute");
356  if (Mute == NULL) {
357  cJSON_AddStringToObject(btData, "Mute", persistentData->Mute);
358  writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
359  }
360  else if (strcmp(Mute->valuestring, persistentData->Mute)) { // Only if Mute changed write to persistence
361  strcpy(Mute->valuestring, persistentData->Mute);
362  writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH, btData);
363  }
364 
365  return eBTRMgrSuccess;
366 }
367 #endif
368 
371  tBTRMgrPIHdl hBTRMgrPiHdl,
372  BTRMGR_PersistentData_t* persistentData
373 ) {
374  char *persistent_file_content = NULL;
375  int profileCount = 0;
376  int deviceCount = 0;
377  int pcount = 0;
378  int dcount = 0;
379 
380  // Validate Handle
381  stBTRMgrPIHdl* pstBtrMgrPiHdl = (stBTRMgrPIHdl*)hBTRMgrPiHdl;
382 
383  if (pstBtrMgrPiHdl == NULL) {
384  BTRMGRLOG_ERROR ("PI Handle not initialized\n");
385  return eBTRMgrFailure;
386  }
387 
388  // Read file and fill persistent_file_content
389  persistent_file_content = readPersistentFile(BTRMGR_PERSISTENT_DATA_PATH);
390  // Seems like file is empty
391  if (persistent_file_content == NULL) {
392  memset (persistentData->limitBeaconDetection, '\0', BTRMGR_NAME_LEN_MAX);
393  strcpy(persistentData->limitBeaconDetection, "false");
394  return eBTRMgrFailure;
395  }
396 
397  cJSON *btProfileData = cJSON_Parse(persistent_file_content);
398  free(persistent_file_content);
399  if (btProfileData == NULL) {
400  // Corrupted JSON
401  BTRMGRLOG_ERROR ("Could not parse JSON data file - Corrupted JSON\n");
402  return eBTRMgrFailure;
403  }
404 
405  cJSON * adpaterIdptr = cJSON_GetObjectItem(btProfileData,"AdapterId");
406  if (adpaterIdptr == NULL) {
407  // Corrupted JSON
408  BTRMGRLOG_ERROR ("Could not able to get AdapterId from JSON\n");
409  return eBTRMgrFailure;
410  }
411  strcpy(persistentData->adapterId,adpaterIdptr->valuestring);
412 
413  cJSON *limitDetection = cJSON_GetObjectItem(btProfileData,"LimitBeaconDetection");
414  if(limitDetection != NULL) {
415  strcpy(persistentData->limitBeaconDetection, limitDetection->valuestring);
416  }
417  else {
418  memset (persistentData->limitBeaconDetection, '\0', BTRMGR_NAME_LEN_MAX);
419  strcpy(persistentData->limitBeaconDetection, "false");
420  }
421 
422  cJSON *btProfiles = cJSON_GetObjectItem(btProfileData,"Profiles");
423  if (btProfiles != NULL) {
424  // Read Profile details
425  profileCount = cJSON_GetArraySize(btProfiles);
426  BTRMGRLOG_DEBUG ("Successfully Parsed Persistent profile, Profile count = %d\n",profileCount);
427 
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")) {
433 
434  profileId = cJSON_GetObjectItem(profile,"ProfileId")->valuestring;
435  if (profileId) {
436  strcpy(persistentData->profileList[pcount].profileId,profileId);
437 
438  // Get Device Details
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);
443 
444  for(dcount = 0; dcount<deviceCount; dcount++) {
445  char* deviceId = NULL;
446  int isConnect = 0;
447  cJSON *device = cJSON_GetArrayItem(btDevices, dcount);
448  if(cJSON_GetObjectItem(device,"DeviceId")) {
449  deviceId = cJSON_GetObjectItem(device,"DeviceId")->valuestring;
450  if (deviceId) { //CID:23328. 23379 - Reverse_inull and 23385,23443 - Forward null
451  persistentData->profileList[pcount].deviceList[dcount].deviceId = strtoll(deviceId,NULL,10);
452 
453  if(cJSON_GetObjectItem(device,"ConnectionStatus")->valueint)
454  isConnect = cJSON_GetObjectItem(device,"ConnectionStatus")->valueint;
455 
456  persistentData->profileList[pcount].deviceList[dcount].isConnected = isConnect;
457  BTRMGRLOG_DEBUG ("Parsing device details, Device- %s, Status-%d, Profile-%s\n",deviceId,isConnect,profileId);
458  }
459  }
460  }
461  }
462  }
463  }
464  }
465  else {
466  // Corrupted JSON
467  BTRMGRLOG_ERROR ("Could not able to get Profile Lists from JSON\n");
468  return eBTRMgrFailure;
469  }
470 
471  return eBTRMgrSuccess;
472 }
473 
474 
477  tBTRMgrPIHdl hBTRMgrPiHdl,
478  BTRMGR_Profile_t* persistProfile
479 ) { //CID:23369 and 23437 - Pass by Value
480  // Get Current persistent data in order to append
482  int pcount = 0;
483  int isObjectAdded = 0;
484 
485  // Validate Handle
486  stBTRMgrPIHdl* pstBtrMgrPiHdl = (stBTRMgrPIHdl*)hBTRMgrPiHdl;
487 
488  if ((pstBtrMgrPiHdl == NULL) && (persistProfile == NULL)) {
489  BTRMGRLOG_ERROR ("PI Handle and Persist profile not initialized\n");
490  return eBTRMgrFailure;
491  }
492 
493  BTRMgr_PI_GetAllProfiles(hBTRMgrPiHdl,&piData);
494  int profileCount = piData.numOfProfiles;
495  if( profileCount > 0) { // Seems like some profile are already there, So append data
496  BTRMGRLOG_DEBUG ("Profile Count >0 need to append profile \n");
497 
498  for(pcount=0; pcount<profileCount ; pcount++) {
499  if(strcmp(piData.profileList[pcount].profileId,persistProfile->profileId) == 0) { // Profile already exists simply add device
500  BTRMGRLOG_DEBUG ("Profile entry already exists,need to add device alone \n");
501  int deviceCnt = piData.profileList[pcount].numOfDevices;
502  // Check if it is a duplicate entry
503  int dcount = 0;
504  for(dcount = 0; dcount < deviceCnt; dcount++) {
505  if(piData.profileList[pcount].deviceList[dcount].deviceId == persistProfile->deviceId) {
506  // Its a duplicate entry
507  BTRMGRLOG_ERROR ("Adding Failed Duplicate entry found - %lld\n",persistProfile->deviceId);
508  return eBTRMgrFailure;
509  }
510  }
511  // Not a duplicate add device to same profile
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++;
516  isObjectAdded = 1;
517  break;
518  }
519  }
520 
521  if(0 == isObjectAdded) { // Seems like its a new profile add it
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++;
528  isObjectAdded = 1;
529  BTRMGRLOG_DEBUG ("New Profile added - %s \n",persistProfile->profileId);
530  }
531  }
532  else { // Data is empty now, Lets create one and add
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;
540  isObjectAdded = 1;
541  }
542 
543  if(isObjectAdded) {
544  BTRMGRLOG_DEBUG ("Writing changes to file - %s \n",persistProfile->profileId);
545  BTRMgr_PI_SetAllProfiles(hBTRMgrPiHdl,&piData);
546  }
547 
548  return eBTRMgrSuccess;
549 }
550 
553  tBTRMgrPIHdl hBTRMgrPiHdl,
554  BTRMGR_PersistentData_t* persistentData
555 ) {
556  int profileCount = 0;
557  int pcount = 0;
558  int dcount = 0;
559  cJSON *Profiles = NULL;
560  cJSON *devices = NULL;
561  cJSON *Profile = NULL;
562  cJSON *piData = NULL;
563 
564  // Validate Handle
565  stBTRMgrPIHdl* pstBtrMgrPiHdl = (stBTRMgrPIHdl*)hBTRMgrPiHdl;
566  if (pstBtrMgrPiHdl == NULL) {
567  BTRMGRLOG_ERROR ("PI Handle not initialized\n");
568  return eBTRMgrFailure;
569  }
570 
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++) {
576  // Get All Device details first
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);
590  }
591  Profile = cJSON_CreateObject();
592  cJSON_AddStringToObject(Profile,"ProfileId",persistentData->profileList[pcount].profileId);
593  cJSON_AddItemToObject(Profile, "Devices", devices);
594  cJSON_AddItemToArray(Profiles, Profile);
595  }
596  else {
597  BTRMGRLOG_ERROR ("Empty device list could not set\n");
598  //return eBTRMgrFailure;
599  }
600  }
601 
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);
606  }
607  else { // No profiles exists empty file
608  BTRMGRLOG_DEBUG ("Writing empty data\n");
609  }
610 
611  if (persistentData->limitBeaconDetection && *(persistentData->limitBeaconDetection) != '\0') {
612  cJSON_AddStringToObject(piData, "LimitBeaconDetection", persistentData->limitBeaconDetection);
613  }
614 
615  writeToPersistentFile(BTRMGR_PERSISTENT_DATA_PATH,piData);
616  cJSON_Delete(piData);
617 
618  return eBTRMgrSuccess;
619 }
620 
623  tBTRMgrPIHdl hBTRMgrPiHdl,
624  BTRMGR_Profile_t* persistProfile
625 ) {
626  // Get Current persistent data in order to append
628  int pcount = 0;
629  int isObjectRemoved = 0;
630 
631  // Validate Handle
632  stBTRMgrPIHdl* pstBtrMgrPiHdl = (stBTRMgrPIHdl*)hBTRMgrPiHdl;
633  if ((pstBtrMgrPiHdl == NULL) && (persistProfile == NULL)) {
634  BTRMGRLOG_ERROR ("PI Handle and Persist profile not initialized\n");
635  return eBTRMgrFailure;
636  }
637 
638  BTRMGRLOG_DEBUG ("Removing profile - %s\n",persistProfile->profileId);
639  BTRMgr_PI_GetAllProfiles(hBTRMgrPiHdl,&piData);
640  int profileCount = piData.numOfProfiles;
641  if( profileCount > 0) { // Seems like some profile are already
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) { // Profile already exists simply remove device
645  BTRMGRLOG_DEBUG ("Profile match found - %s find device\n",persistProfile->profileId);
646  int deviceCnt = piData.profileList[pcount].numOfDevices;
647  // Check if it is a duplicate entry
648  int dcount = 0;
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--;
656  isObjectRemoved = 1;
657  BTRMGRLOG_DEBUG ("Profile match found && Device Match Found - %s Delete Success %lld\n",
658  persistProfile->profileId, persistProfile->deviceId);
659  break;
660  }
661  }
662 
663  if(isObjectRemoved && piData.profileList[pcount].numOfDevices == 0) { // There is no more device exists so no need to profile
664  memset(piData.profileList[pcount].profileId, 0,BTRMGR_NAME_LEN_MAX );
665  piData.numOfProfiles--;
666  }
667  }
668  else if (!isObjectRemoved && (pcount == (profileCount - 1))) {
669  // Unknown profile not able to delete
670  BTRMGRLOG_ERROR ("Profile Not found, Could not delete %s\n",persistProfile->profileId);
671  return eBTRMgrFailure;
672  }
673  }
674 
675  if(isObjectRemoved && piData.numOfProfiles == 0) { // There is no more profiles exists
676  memset(piData.adapterId, 0,BTRMGR_NAME_LEN_MAX );
677  }
678 
679  if(isObjectRemoved) {
680  BTRMgr_PI_SetAllProfiles(hBTRMgrPiHdl,&piData);
681  }
682  }
683  else {
684  // Profile is empty cant delete
685  BTRMGRLOG_ERROR ("Nothing to delete, Profile is empty \n");
686  return eBTRMgrFailure;
687  }
688 
689  return eBTRMgrSuccess;
690 }
691 
692 
694 BTRMgr_PI_GetProfile (
695  stBTRMgrPersistProfile* persistProfile,
696  char* profileName,
697  char* deviceId
698 ) {
699  return eBTRMgrSuccess;
700 }
701 
702 
703 // Outgoing callbacks Registration Interfaces
704 
705 
706 /* Incoming Callbacks */
707 
708 
709  /* End of File */
btrMgr_Types.h
_BTRMGR_PersistentData_t
Definition: btrMgr_persistIface.h:77
BTRMgr_PI_SetAllProfiles
eBTRMgrRet BTRMgr_PI_SetAllProfiles(tBTRMgrPIHdl hBTRMgrPiHdl, BTRMGR_PersistentData_t *persistentData)
This API sets all bluetooth profiles.
Definition: btrMgr_persistIface.c:552
BTRMgr_PI_Init
eBTRMgrRet BTRMgr_PI_Init(tBTRMgrPIHdl *hBTRMgrPiHdl)
This API initializes bluetooth manager's persistent storage interface.
Definition: btrMgr_persistIface.c:127
_BTRMGR_Profile_t
Definition: btrMgr_persistIface.h:85
eBTRMgrRet
enum _eBTRMgrRet eBTRMgrRet
Represents the bluetooth manager return values.
_BTRMGR_Beacon_PersistentData_t
Definition: btrMgr_persistIface.h:63
BTRMgr_PI_DeInit
eBTRMgrRet BTRMgr_PI_DeInit(tBTRMgrPIHdl hBTRMgrPiHdl)
This API deinitializes bluetooth manager's persistent storage interface.
Definition: btrMgr_persistIface.c:145
btrMgr_persistIface.h
BTRMgr_PI_AddProfile
eBTRMgrRet BTRMgr_PI_AddProfile(tBTRMgrPIHdl hBTRMgrPiHdl, BTRMGR_Profile_t *persistProfile)
This API adds a single bluetooth profile to json file.
Definition: btrMgr_persistIface.c:476
BTRMgr_PI_RemoveProfile
eBTRMgrRet BTRMgr_PI_RemoveProfile(tBTRMgrPIHdl hBTRMgrPiHdl, BTRMGR_Profile_t *persistProfile)
This API removes a single bluetooth profile from json file.
Definition: btrMgr_persistIface.c:622
_stBTRMgrPersistProfile
Definition: btrMgr_persistIface.h:57
BTRMgr_PI_GetAllProfiles
eBTRMgrRet BTRMgr_PI_GetAllProfiles(tBTRMgrPIHdl hBTRMgrPiHdl, BTRMGR_PersistentData_t *persistentData)
This API reads all bluetooth profiles from json file and saves to BTRMGR_PersistentData_t structure.
Definition: btrMgr_persistIface.c:370
_stBTRMgrPIHdl
Definition: btrMgr_persistIface.c:49