1 /* ****************************************************************
3 * Copyright 2017 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 #include "cacommonutil.h"
24 #include "camanagerleutil.h"
25 #include "caleclient.h"
26 #include "caleutils.h"
28 #define TAG "OIC_CA_MANAGER_UTIL"
30 static const NSString *NSUSERDEFULATS_KEY = @"acList_dictionary";
31 static NSMutableDictionary *g_connectedDeviceSet = NULL;
32 static NSMutableDictionary *g_peripherals = NULL;
34 NSUserDefaults* CAManagerGetSharedPreference()
36 return [NSUserDefaults standardUserDefaults];
39 bool CAManagerUpdatePrefStringSet(NSMutableDictionary *set)
41 OIC_LOG(DEBUG, TAG, "IN-CAManagerUpdatePrefStringSet");
42 VERIFY_NON_NULL_RET(set, TAG, "set", false);
44 NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
45 @synchronized (userDefaults) {
46 NSData *contentData = [NSKeyedArchiver archivedDataWithRootObject:set];
47 [userDefaults setObject:contentData forKey:NSUSERDEFULATS_KEY];
50 return [userDefaults synchronize];
53 NSMutableDictionary* CAManagerGetPrefStringSet()
55 OIC_LOG(DEBUG, TAG, "CAManagerGetPrefStringSet");
57 NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
59 NSMutableDictionary *set = nil;
62 NSData *contentData = [[userDefaults objectForKey:NSUSERDEFULATS_KEY] mutableCopy];
64 set = (NSMutableDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:contentData];
71 bool CAManagerContainsPrefStringSet()
73 OIC_LOG(DEBUG, TAG, "CAManagerContainsPrefStringSet");
75 NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
80 data = [userDefaults dataForKey:NSUSERDEFULATS_KEY];
83 return data != nil ? true : false;
86 bool CAManagerRemovePrefStringSet()
88 OIC_LOG(DEBUG, TAG, "CAManagerRemovePrefStringSet");
90 NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
91 @synchronized (userDefaults) {
92 [userDefaults removeObjectForKey:NSUSERDEFULATS_KEY];
95 return [userDefaults synchronize];
98 CBPeripheral* CAManagerGetValueConnectedDeviceAddress(const char* address)
100 OIC_LOG(DEBUG, TAG, "IN-CAManagerGetValueConnectedDeviceAddress");
102 VERIFY_NON_NULL_RET(address, TAG, "address", nil);
104 NSString *ns_address = [NSString stringWithUTF8String:address];
106 if (![g_connectedDeviceSet objectForKey:ns_address] || ![g_peripherals objectForKey:ns_address])
108 OIC_LOG(DEBUG, TAG, "it's un-registered one");
112 return (CBPeripheral *)g_peripherals[ns_address];
115 bool CAManagerSetValueConnectedDeviceAddress(CBPeripheral *peripheral, const char* address)
117 OIC_LOG(DEBUG, TAG, "IN-CAManagerSetValueConnectedDeviceAddress");
119 VERIFY_NON_NULL_RET(address, TAG, "address", false);
121 NSString *ns_address = [NSString stringWithUTF8String:address];
123 if ([g_connectedDeviceSet objectForKey:ns_address] && [g_peripherals objectForKey:ns_address])
125 OIC_LOG(DEBUG, TAG, "it's already done");
129 NSUUID *peri_identifier =[[NSUUID alloc] initWithUUIDString:
130 [[peripheral identifier] UUIDString]];
132 [g_connectedDeviceSet setObject:peri_identifier forKey:ns_address];
133 [g_peripherals setObject:peripheral forKey:ns_address];
136 //return CAManagerUpdatePrefStringSet(g_connectedDeviceSet);
139 bool CAManagerAddConnectedDeviceAddress(const char* address)
141 OIC_LOG(DEBUG, TAG, "IN-CAManagerAddConnectedDeviceAddress");
143 VERIFY_NON_NULL_RET(address, TAG, "address", false);
145 NSString *ns_address = [NSString stringWithUTF8String:address];
147 if ([g_connectedDeviceSet objectForKey:ns_address] && [g_peripherals objectForKey:ns_address])
149 OIC_LOG(DEBUG, TAG, "it's already done");
153 [g_connectedDeviceSet setObject:[NSNull null] forKey:ns_address];
154 [g_peripherals setObject:[NSNull null] forKey:ns_address];
157 //return CAManagerUpdatePrefStringSet(g_connectedDeviceSet);
160 bool CAManagerIsConnectedDeviceAddress(const char* address)
162 OIC_LOG(DEBUG, TAG, "CAManagerIsConnectedDeviceAddress");
164 VERIFY_NON_NULL_RET(address, TAG, "address", false);
166 NSString *ns_address = [NSString stringWithUTF8String:address];
168 return ([g_connectedDeviceSet objectForKey:ns_address] != nil &&
169 [g_peripherals objectForKey:ns_address] != nil) ? true : false;
172 bool CAManagerGetConnectedDeviceAddress()
174 OIC_LOG(DEBUG, TAG, "CAManagerGetConnectedDeviceAddress");
179 bool CAManagerRemoveConnectedDeviceAddress(const char* address)
181 OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveConnectedDeviceAddress");
183 VERIFY_NON_NULL_RET(address, TAG, "address", false);
185 NSString *ns_address = [NSString stringWithUTF8String:address];
187 if (![g_connectedDeviceSet objectForKey:ns_address])
189 OIC_LOG(ERROR, TAG, "it's already done");
193 if (![g_peripherals objectForKey:ns_address])
195 OIC_LOG(ERROR, TAG, "identifier OR peripheral set is wrong");
199 [g_peripherals removeObjectForKey:ns_address];
200 [g_connectedDeviceSet removeObjectForKey:ns_address];
205 static bool ishexdigit(char var)
209 if(var >= '0') return true;
214 if(var >= 'A') return true;
219 if(var >= 'a') return true;
226 bool CAManagerCheckBTAddress(const char *address)
228 OIC_LOG(DEBUG, TAG, "CAManagerCheckBTAddress");
230 VERIFY_NON_NULL_RET(address, TAG, "address is null", false);
233 * Check if the provided MAC address is valid.
234 * 1. The length of MAC address should always be 17.
235 * 2. Hyphens are expected at positions {3, 6, 9, 12, 15}.
236 * 3. The rest characters should be simple xdigits.
238 int hyphens[5] = {3, 6, 9, 12, 15};
239 if (strlen(address) != 17)
241 return false;//Oops. The lenth doesn't match.
244 for (int i = 0, counter = 0; i < 17; i ++)
246 char var = address[i];
247 if (i == hyphens[counter] - 1)// Check if a hyphen is expected here.
249 // Yep. We need a hyphen here.
252 return false;// Oops. The character is not a hyphen.
256 counter++;// Move on to the next expected hyphen position.
261 // Nope. The character here should be a simple xdigit
262 if (ishexdigit(var) == 0)
264 return false;// Oops. The current character is not a hyphen.
268 return true;// Seen'em all!
271 bool CAManagerCreateSetString()
273 OIC_LOG(DEBUG, TAG, "CAManagerCreateSetString");
275 g_connectedDeviceSet = [[NSMutableDictionary alloc] init];
276 g_peripherals = [[NSMutableDictionary alloc] init];
278 return (g_connectedDeviceSet != nil && g_peripherals != nil) ? true : false;
281 CAResult_t CAManagerTerminateSetString()
283 OIC_LOG(DEBUG, TAG, "CAManagerTerminateSetString");
285 [g_connectedDeviceSet removeAllObjects];
286 [g_connectedDeviceSet release];
288 [g_peripherals removeAllObjects];
289 [g_peripherals release];
291 g_connectedDeviceSet = nil;
294 return (g_connectedDeviceSet == nil && g_peripherals == nil) ? CA_STATUS_OK : CA_STATUS_FAILED;