replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / bt_le_manager / ios / camanagerleutil.m
1 /* ****************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  *
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
21 #include "cacommon.h"
22 #include "logger.h"
23 #include "cacommonutil.h"
24 #include "camanagerleutil.h"
25 #include "caleclient.h"
26 #include "caleutils.h"
27
28 #define TAG "OIC_CA_MANAGER_UTIL"
29
30 static const NSString *NSUSERDEFULATS_KEY = @"acList_dictionary";
31 static NSMutableDictionary *g_connectedDeviceSet = NULL;
32 static NSMutableDictionary *g_peripherals = NULL;
33
34 NSUserDefaults* CAManagerGetSharedPreference()
35 {
36     return [NSUserDefaults standardUserDefaults];
37 }
38
39 bool CAManagerUpdatePrefStringSet(NSMutableDictionary *set)
40 {
41     OIC_LOG(DEBUG, TAG, "IN-CAManagerUpdatePrefStringSet");
42     VERIFY_NON_NULL_RET(set, TAG, "set", false);
43
44     NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
45     @synchronized (userDefaults) {
46         NSData *contentData = [NSKeyedArchiver archivedDataWithRootObject:set];
47         [userDefaults setObject:contentData forKey:NSUSERDEFULATS_KEY];
48     }
49
50     return [userDefaults synchronize];
51 }
52
53 NSMutableDictionary* CAManagerGetPrefStringSet()
54 {
55     OIC_LOG(DEBUG, TAG, "CAManagerGetPrefStringSet");
56
57     NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
58
59     NSMutableDictionary *set = nil;
60
61     if (userDefaults) {
62         NSData *contentData = [[userDefaults objectForKey:NSUSERDEFULATS_KEY] mutableCopy];
63         if (contentData) {
64             set = (NSMutableDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:contentData];
65         }
66     }
67
68     return [set copy];
69 }
70
71 bool CAManagerContainsPrefStringSet()
72 {
73     OIC_LOG(DEBUG, TAG, "CAManagerContainsPrefStringSet");
74
75     NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
76
77     NSData *data = nil;
78
79     if (userDefaults) {
80         data = [userDefaults dataForKey:NSUSERDEFULATS_KEY];
81     }
82
83     return data != nil ? true : false;
84 }
85
86 bool CAManagerRemovePrefStringSet()
87 {
88     OIC_LOG(DEBUG, TAG, "CAManagerRemovePrefStringSet");
89
90     NSUserDefaults *userDefaults = CAManagerGetSharedPreference();
91     @synchronized (userDefaults) {
92         [userDefaults removeObjectForKey:NSUSERDEFULATS_KEY];
93     }
94
95     return [userDefaults synchronize];
96 }
97
98 CBPeripheral* CAManagerGetValueConnectedDeviceAddress(const char* address)
99 {
100     OIC_LOG(DEBUG, TAG, "IN-CAManagerGetValueConnectedDeviceAddress");
101
102     VERIFY_NON_NULL_RET(address, TAG, "address", nil);
103
104     NSString *ns_address = [NSString stringWithUTF8String:address];
105
106     if (![g_connectedDeviceSet objectForKey:ns_address] || ![g_peripherals objectForKey:ns_address])
107     {
108         OIC_LOG(DEBUG, TAG, "it's un-registered one");
109         return false;
110     }
111
112     return (CBPeripheral *)g_peripherals[ns_address];
113 }
114
115 bool CAManagerSetValueConnectedDeviceAddress(CBPeripheral *peripheral, const char* address)
116 {
117     OIC_LOG(DEBUG, TAG, "IN-CAManagerSetValueConnectedDeviceAddress");
118
119     VERIFY_NON_NULL_RET(address, TAG, "address", false);
120
121     NSString *ns_address = [NSString stringWithUTF8String:address];
122
123     if ([g_connectedDeviceSet objectForKey:ns_address] && [g_peripherals objectForKey:ns_address])
124     {
125         OIC_LOG(DEBUG, TAG, "it's already done");
126         return true;
127     }
128
129     NSUUID *peri_identifier =[[NSUUID alloc] initWithUUIDString:
130                               [[peripheral identifier] UUIDString]];
131
132     [g_connectedDeviceSet setObject:peri_identifier forKey:ns_address];
133     [g_peripherals setObject:peripheral forKey:ns_address];
134
135     return true;
136     //return CAManagerUpdatePrefStringSet(g_connectedDeviceSet);
137 }
138
139 bool CAManagerAddConnectedDeviceAddress(const char* address)
140 {
141     OIC_LOG(DEBUG, TAG, "IN-CAManagerAddConnectedDeviceAddress");
142
143     VERIFY_NON_NULL_RET(address, TAG, "address", false);
144
145     NSString *ns_address = [NSString stringWithUTF8String:address];
146
147     if ([g_connectedDeviceSet objectForKey:ns_address] && [g_peripherals objectForKey:ns_address])
148     {
149         OIC_LOG(DEBUG, TAG, "it's already done");
150         return true;
151     }
152
153     [g_connectedDeviceSet setObject:[NSNull null] forKey:ns_address];
154     [g_peripherals setObject:[NSNull null] forKey:ns_address];
155
156     return true;
157     //return CAManagerUpdatePrefStringSet(g_connectedDeviceSet);
158 }
159
160 bool CAManagerIsConnectedDeviceAddress(const char* address)
161 {
162     OIC_LOG(DEBUG, TAG, "CAManagerIsConnectedDeviceAddress");
163
164     VERIFY_NON_NULL_RET(address, TAG, "address", false);
165
166     NSString *ns_address = [NSString stringWithUTF8String:address];
167
168     return ([g_connectedDeviceSet objectForKey:ns_address] != nil &&
169             [g_peripherals objectForKey:ns_address] != nil) ? true : false;
170 }
171
172 bool CAManagerGetConnectedDeviceAddress()
173 {
174     OIC_LOG(DEBUG, TAG, "CAManagerGetConnectedDeviceAddress");
175
176     return false;
177 }
178
179 bool CAManagerRemoveConnectedDeviceAddress(const char* address)
180 {
181     OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveConnectedDeviceAddress");
182
183     VERIFY_NON_NULL_RET(address, TAG, "address", false);
184
185     NSString *ns_address = [NSString stringWithUTF8String:address];
186
187     if (![g_connectedDeviceSet objectForKey:ns_address])
188     {
189         OIC_LOG(ERROR, TAG, "it's already done");
190         return true;
191     }
192
193     if (![g_peripherals objectForKey:ns_address])
194     {
195         OIC_LOG(ERROR, TAG, "identifier OR peripheral set is wrong");
196         return false;
197     }
198
199     [g_peripherals removeObjectForKey:ns_address];
200     [g_connectedDeviceSet removeObjectForKey:ns_address];
201
202     return true;
203 }
204
205 static bool ishexdigit(char var)
206 {
207     if(var <= '9')
208     {
209         if(var >= '0') return true;
210         else           return false;
211     }
212     else if(var <= 'F')
213     {
214         if(var >= 'A') return true;
215         else           return false;
216     }
217     else if(var <= 'f')
218     {
219         if(var >= 'a') return true;
220         else           return false;
221     }
222
223     return false;
224 }
225
226 bool CAManagerCheckBTAddress(const char *address)
227 {
228     OIC_LOG(DEBUG, TAG, "CAManagerCheckBTAddress");
229
230     VERIFY_NON_NULL_RET(address, TAG, "address is null", false);
231
232     /*
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.
237     */
238     int hyphens[5] = {3, 6, 9, 12, 15};
239     if (strlen(address) != 17)
240     {
241         return false;//Oops. The lenth doesn't match.
242     }
243
244     for (int i = 0, counter = 0; i < 17; i ++)
245     {
246         char var = address[i];
247         if (i == hyphens[counter] - 1)// Check if a hyphen is expected here.
248         {
249             // Yep. We need a hyphen here.
250             if (var != ':')
251             {
252                 return false;// Oops. The character is not a hyphen.
253             }
254             else
255             {
256                 counter++;// Move on to the next expected hyphen position.
257             }
258         }
259         else
260         {
261             // Nope. The character here should be a simple xdigit
262             if (ishexdigit(var) == 0)
263             {
264                 return false;// Oops. The current character is not a hyphen.
265             }
266         }
267     }
268     return true;// Seen'em all!
269 }
270
271 bool CAManagerCreateSetString()
272 {
273     OIC_LOG(DEBUG, TAG, "CAManagerCreateSetString");
274
275     g_connectedDeviceSet = [[NSMutableDictionary alloc] init];
276     g_peripherals = [[NSMutableDictionary alloc] init];
277
278     return (g_connectedDeviceSet != nil && g_peripherals != nil) ? true : false;
279 }
280
281 CAResult_t CAManagerTerminateSetString()
282 {
283     OIC_LOG(DEBUG, TAG, "CAManagerTerminateSetString");
284
285     [g_connectedDeviceSet removeAllObjects];
286     [g_connectedDeviceSet release];
287
288     [g_peripherals removeAllObjects];
289     [g_peripherals release];
290
291     g_connectedDeviceSet = nil;
292     g_peripherals = nil;
293
294     return (g_connectedDeviceSet == nil && g_peripherals == nil) ? CA_STATUS_OK : CA_STATUS_FAILED;
295 }