Cloud ACL Modifications
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / cloud / cloudCommon.c
1 //******************************************************************
2 //
3 // Copyright 2016 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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "ocstack.h"
25 #include "logger.h"
26 #include "camutex.h"
27 #include "cathreadpool.h"
28 #include "ocpayload.h"
29 #include "payload_logging.h"
30 #include "aclresource.h"
31 #include "crlresource.h"
32 #include "ocprovisioningmanager.h"
33 #include "casecurityinterface.h"
34 #include "mbedtls/ssl_ciphersuites.h"
35
36 #include "utils.h"
37 #include "cloudAuth.h"
38 #include "cloudCommon.h"
39 #include "cloudWrapper.h"
40 #include "cloudDiscovery.h"
41
42 #ifdef __unix__
43 #include <unistd.h> //for unlink
44 #endif
45
46 #define TAG "cloudCommon"
47
48 #define DEFAULT_HOST            "10.113.68.85"//"127.0.0.1"
49 #define DEFAULT_PORT            OC_MULTICAST_PORT
50 #define DEFAULT_AUTH_PROVIDER   "github"
51 #define DEFAULT_DB_FILE         "./cloud.dat"
52 #define DEFAULT_RESPONSE_WAIT_TIME (10 * 1000000) //10s
53
54 #define GITHUB_AUTH_LINK        "https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F"
55
56 #define CLIENT_ONLY(mode)       if (OC_SERVER == (mode)) { wrongRequest(); sendDataToServer = false; break; }
57
58 static bool fExit = false;
59
60 static ca_thread_pool_t g_threadPoolHandle = NULL;
61 static OCDevAddr endPoint;
62 static char token[1024] = "";
63 static char authProvider[1024] = DEFAULT_AUTH_PROVIDER;
64 static char *fname = DEFAULT_DB_FILE;
65 static uint64_t timeout;
66 static uint16_t g_credId = 0;
67
68 static ca_cond cond;
69 static ca_mutex mutex;
70
71 typedef enum {
72     SIGN_UP       = 1,
73     SIGN_IN       = 2,
74     SIGN_OUT      = 3,
75
76     HOST          = 4,
77     PORT          = 5,
78     DB_FILE       = 6,
79     AUTH_PROVIDER = 7,
80     USE_RSA = 8,
81     SAVE_TRUST_CERT = 9,
82     USE_SECURE_CONN = 10,
83
84     DISCOVERY     = 13,
85     GET           = 14,
86     PUT           = 15,
87     POST          = 16,
88
89     CSR_SIGN      = 19,
90
91     CRL_GET       = 20,
92     CRL_POST      = 21,
93
94     ACL_ID_CREATE = 30,
95     ACL_ID_GET_BY_DEVICE = 31,
96     ACL_ID_DELETE = 32,
97
98     ACL_INDIVIDUAL_GET_INFO = 40,
99     ACL_INDIVIDUAL_UPDATE_ACE = 41,
100     ACL_INDIVIDUAL_UPDATE = 42,
101     ACL_INDIVIDUAL_DELETE = 43,
102     ACL_INDIVIDUAL_DELETE_ACE = 44,
103
104     ACL_GROUP_CREATE = 50,
105     ACL_GROUP_FIND   = 51,
106     ACL_GROUP_JOIN   = 52,
107     ACL_GROUP_OBSERVE= 53,
108     ACL_GROUP_DELETE = 54,
109
110     ACL_GROUP_SHARE_DEVICE  = 60,
111     ACL_GROUP_DELETE_DEVICE  = 61,
112     ACL_GROUP_GET_INFO  = 62,
113
114     ACL_POLICY_CHECK_REQUEST = 70,
115
116     ACL_GROUP_INVITE_USER = 80,
117     ACL_GROUP_GET_INVITE  = 81,
118     ACL_GROUP_DELETE_INVITE  = 82,
119     ACL_GROUP_CANCEL_INVITE  = 83,
120
121     EXIT          = 99
122 }CloudRequest;
123
124 static void printMenu(OCMode mode)
125 {
126     char *title = "Client";
127     if (OC_SERVER == mode)
128     {
129         title = "Server";
130     }
131     printf("************************************************************\n");
132     printf("****************** Cloud %s Requests *******************\n", title);
133     printf("************************************************************\n");
134     printf("** AUTHORIZATION\n");
135     printf("** %d - Sign Up request\n", SIGN_UP);
136     printf("** %d - Sign In request\n", SIGN_IN);
137     printf("** %d - Sign Out request\n", SIGN_OUT);
138
139     printf("** SETTINGS \n");
140     printf("** %d - Change default host\n", HOST);
141     printf("** %d - Change default port\n", PORT);
142     printf("** %d - Change default database filename\n", DB_FILE);
143     printf("** %d - Change default auth provider\n", AUTH_PROVIDER);
144     printf("** %d - Change TLS cipher suite (ECDSA/RSA)\n", USE_RSA);
145     printf("** %d - Save Trust Cert. Chain into Cred of SVR\n", SAVE_TRUST_CERT);
146     printf("** %d - Change Protocol type (CoAP/CoAPs)\n", USE_SECURE_CONN);
147
148     if (OC_CLIENT == mode)
149     {
150         printf("** DISCOVERY\n");
151         printf("** %d - Start Discovery\n", DISCOVERY);
152         printf("** %d - Get Request\n", GET);
153         printf("** %d - Put Request\n", PUT);
154         printf("** %d - Post Request\n", POST);
155     }
156
157     printf("** CERTIFICATE REQUEST\n");
158     printf("** %d - Certificate Request\n", CSR_SIGN);
159
160     printf("** CRL\n");
161     printf("** %d - CRL GET Request\n", CRL_GET);
162     printf("** %d - CRL POST Request\n", CRL_POST);
163
164     printf("** ACL MANAGER\n");
165     printf("** %d - ACL id create Request\n", ACL_ID_CREATE);
166     printf("** %d - ACL id retrieve by device Request\n", ACL_ID_GET_BY_DEVICE);
167     printf("** %d - ACL id delete Request\n", ACL_ID_DELETE);
168
169     printf("** ACL INDIVIDUAL\n");
170     printf("** %d - ACL individual get info Request\n", ACL_INDIVIDUAL_GET_INFO);
171     printf("** %d - ACL individual update ACE Request\n", ACL_INDIVIDUAL_UPDATE_ACE);
172     printf("** %d - ACL individual update Request\n", ACL_INDIVIDUAL_UPDATE);
173     printf("** %d - ACL individual delete Request\n", ACL_INDIVIDUAL_DELETE);
174     printf("** %d - ACL individual delete ACE Request\n", ACL_INDIVIDUAL_DELETE_ACE);
175
176     printf("** ACL GROUP MANAGER\n");
177     printf("** %d - ACL Create Group Request\n", ACL_GROUP_CREATE);
178     printf("** %d - ACL Find Group Request\n", ACL_GROUP_FIND);
179     printf("** %d - ACL Join to invited Group Request\n", ACL_GROUP_JOIN);
180     printf("** %d - ACL Observe Group Request\n", ACL_GROUP_OBSERVE);
181     printf("** %d - ACL Delete Group Request\n", ACL_GROUP_DELETE);
182
183     printf("** ACL INDIVIDUAL GROUP\n");
184     printf("** %d - ACL Share device into Group Request\n", ACL_GROUP_SHARE_DEVICE);
185     printf("** %d - ACL Delete device from Group Request\n", ACL_GROUP_DELETE_DEVICE);
186     printf("** %d - ACL Get Group Info Request\n", ACL_GROUP_GET_INFO);
187
188     printf("** ACL POLICY ENFORCEMENT\n");
189     printf("** %d - ACL Check Permissions Request\n", ACL_POLICY_CHECK_REQUEST);
190
191     printf("** ACL MEMBER INVITATION\n");
192     printf("** %d - ACL Invite user to group Request\n", ACL_GROUP_INVITE_USER);
193     printf("** %d - ACL Retrieve invitation Request\n", ACL_GROUP_GET_INVITE);
194     printf("** %d - ACL Delete invitation Request\n", ACL_GROUP_DELETE_INVITE);
195     printf("** %d - ACL Cancel invitation Request\n", ACL_GROUP_CANCEL_INVITE);
196
197     printf("** EXIT\n");
198     printf("** %d - Exit cloud %s\n", EXIT, title);
199     printf("************************************************************\n");
200 }
201
202 void unlockMenu(void *data)
203 {
204     OICFree(data);
205
206     if (!fExit)
207     {
208         ca_mutex_lock(mutex);
209         ca_cond_signal(cond);
210         ca_mutex_unlock(mutex);
211     }
212 }
213
214 /**
215  * This is default callback to all requests
216  * Used to sync with main menu
217  *
218  * @param[in] ctx          context
219  * @param[in] result       result
220  * @param[in] data         data
221  */
222 static void handleCB(void* ctx, OCStackResult result, void* data)
223 {
224     OC_UNUSED(ctx);
225     OC_UNUSED(data);
226
227     OIC_LOG_V(INFO, TAG, "%s: Received result = %d", __func__, result);
228
229     unlockMenu(NULL);
230 }
231
232 /**
233  * This function prints Acl id and calls default callback function handleCB()
234  *
235  * @param[in] ctx          context
236  * @param[in] result       result
237  * @param[in] aclId        acl id
238  */
239 static void handleAclIdCB(void* ctx, OCStackResult result, void* aclId)
240 {
241     OIC_LOG_V(INFO, TAG, "Received Acl id = %s", (char *)aclId);
242     handleCB(ctx, result, aclId);
243     OICFree(aclId);
244 }
245
246 /**
247  * This function prints group id and calls default callback function handleCB()
248  *
249  * @param[in] ctx          context
250  * @param[in] result       result
251  * @param[in] groupId      group id
252  */
253 static void handleAclCreateGroupCB(void* ctx, OCStackResult result, void* groupId)
254 {
255     OIC_LOG_V(INFO, TAG, "Received gid = %s", (char *)groupId);
256     handleCB(ctx, result, groupId);
257     OICFree(groupId);
258 }
259
260 /**
261  * This function prints group policy and calls default callback function handleCB()
262  *
263  * @param[in] ctx          context
264  * @param[in] result       result
265  * @param[in] gp           group policy
266  */
267 static void handleAclPolicyCheckCB(void* ctx, OCStackResult result, void* gp)
268 {
269     OIC_LOG_V(INFO, TAG, "Received gp = %s", (char *)gp);
270     handleCB(ctx, result, gp);
271     OICFree(gp);
272 }
273
274 /**
275  * This function prints received acl and calls default callback function handleCB()
276  *
277  * @param[in] ctx          context
278  * @param[in] result       result
279  * @param[in] acl          acl
280  */
281 static void handleAclIndividualGetInfoCB(void* ctx, OCStackResult result, void* acl)
282 {
283     printACL((OicSecAcl_t* )acl);
284     handleCB(ctx, result, acl);
285     //can't delete acl here because its ACE's were added to gAcl
286     //TODO: changes in aclresources.c required to fix that
287 }
288
289 /**
290  * This function prints received group id list and calls default callback function handleCB()
291  *
292  * @param[in] ctx          context
293  * @param[in] result       result
294  * @param[in] gidList      group id list
295  */
296 static void handleAclFindMyGroupCB(void* ctx, OCStackResult result, void* gidList)
297 {
298     printStringArray((stringArray_t *)gidList);
299     handleCB(ctx, result, gidList);
300     clearStringArray((stringArray_t *)gidList);
301 }
302
303 /**
304  * This function prints received acl and calls default callback function handleCB()
305  *
306  * @param[in] ctx          context
307  * @param[in] result       result
308  * @param[in] crl          crl
309  */
310 static void handleGetCrlCB(void* ctx, OCStackResult result, void* crl)
311 {
312     printCrl((OicSecCrl_t *)crl);
313     handleCB(ctx, result, crl);
314     DeleteCrl((OicSecCrl_t *)crl);
315 }
316
317 /**
318  * This function prints received invitation response and calls default callback function handleCB()
319  *
320  * @param[in] ctx          context
321  * @param[in] result       result
322  * @param[in] invite       invitation response (it has inviteResponse_t* type)
323  */
324 static void handleAclGetInvitationCB(void* ctx, OCStackResult result, void* invite)
325 {
326     printInviteResponse((inviteResponse_t *)invite);
327     handleCB(ctx, result, invite);
328     clearInviteResponse((inviteResponse_t *)invite);
329     OICFree(invite);
330 }
331
332 static OCStackResult saveTrustCert(void)
333 {
334     OCStackResult res = OC_STACK_ERROR;
335     OIC_LOG(INFO, TAG, "Save Trust Cert. Chain into Cred of SVR");
336
337     ByteArray_t trustCertChainArray = {0, 0};
338     const char *filename = "rootca.crt";
339
340     if (!readFile(filename, (OCByteString *)&trustCertChainArray))
341     {
342         OIC_LOG_V(ERROR, TAG, "Can't read %s file", filename);
343         return OC_STACK_ERROR;
344     }
345     OIC_LOG_BUFFER(DEBUG, TAG, trustCertChainArray.data, trustCertChainArray.len);
346
347     res = OCSaveTrustCertChain(trustCertChainArray.data, trustCertChainArray.len, OIC_ENCODING_PEM,&g_credId);
348
349     if (OC_STACK_OK != res)
350     {
351         OIC_LOG(ERROR, TAG, "OCSaveTrustCertChain API error");
352     }
353     else
354     {
355         OIC_LOG_V(INFO, TAG, "CredId of Saved Trust Cert. Chain into Cred of SVR : %d.\n", g_credId);
356     }
357     OICFree(trustCertChainArray.data);
358
359     return res;
360 }
361
362 static void wrongRequest()
363 {
364     printf(">> Entered Wrong Menu Number. Please Enter Again\n\n");
365 }
366
367 static void userRequests(void *data)
368 {
369     if (NULL == data)
370     {
371         OIC_LOG(ERROR, TAG, "Received NULL data");
372         return;
373     }
374
375     OCMode mode = *(OCMode*)data;
376
377     memset(&endPoint, 0, sizeof(endPoint));
378     strncpy(endPoint.addr, DEFAULT_HOST, sizeof(endPoint.addr));
379     endPoint.port = DEFAULT_PORT;
380
381     mutex = ca_mutex_new();
382     cond = ca_cond_new();
383
384     while (false == fExit)
385     {
386         OCStackResult res = OC_STACK_ERROR;
387         bool sendDataToServer = true;
388         timeout = DEFAULT_RESPONSE_WAIT_TIME;
389         //startup report
390         printf("-----------------------------------------------------------\n");
391         printf("Connecting to: %s:%d\n", endPoint.addr, endPoint.port);
392         printf("via auth provider: %s\n", authProvider);
393         printf("srv file: %s\n", fname);
394         printf("CoAP prefix: %s\n", DEFAULT_PREFIX);
395         printf("-----------------------------------------------------------\n");
396
397         printMenu(mode);
398
399         int request = 0;
400         readInteger(&request, "Menu number", "see above");
401
402         switch (request)
403         {
404         case SIGN_UP:
405             if (0 == strncmp(authProvider, DEFAULT_AUTH_PROVIDER, sizeof(authProvider)))
406             {
407                 printf("Paste to browser %s and get auth code\n", GITHUB_AUTH_LINK);
408             }
409             readString(token, sizeof(token), "auth token", "check link above");
410             res = CloudSignUp(&endPoint, authProvider, token);
411             break;
412         case SIGN_IN:
413             res = CloudSignIn(&endPoint);
414             break;
415         case SIGN_OUT:
416             res = CloudSignOut(&endPoint);
417             break;
418         case HOST:
419             readString(endPoint.addr, sizeof(endPoint.addr), "host ip address", DEFAULT_HOST);
420             sendDataToServer = false;
421             break;
422         case PORT:
423         {
424             char example[8];
425             snprintf(example, sizeof(example), "%d", DEFAULT_PORT);
426             int tmp = 0;
427             readInteger(&tmp, "port number", example);
428             endPoint.port = tmp;
429             sendDataToServer = false;
430         }
431         break;
432         case CRL_GET:
433             res = OCWrapperGetCRL(&endPoint, handleGetCrlCB);
434             break;
435         case CRL_POST:
436             res = OCWrapperPostCRL(&endPoint, handleCB);
437             break;
438         case ACL_GROUP_CREATE:
439             res = OCWrapperAclCreateGroup(&endPoint, handleAclCreateGroupCB);
440             break;
441         case ACL_GROUP_FIND:
442             res = OCWrapperAclFindMyGroup(&endPoint, handleAclFindMyGroupCB);
443             break;
444         case ACL_GROUP_DELETE:
445             res = OCWrapperAclDeleteGroup(&endPoint, handleCB);
446             break;
447         case ACL_GROUP_JOIN:
448             res = OCWrapperAclJoinToInvitedGroup(&endPoint, handleCB);
449             break;
450         case ACL_GROUP_OBSERVE:
451             res = OCWrapperAclObserveGroup(&endPoint, handleCB);
452             break;
453         case ACL_GROUP_SHARE_DEVICE:
454             res = OCWrapperAclShareDeviceIntoGroup(&endPoint, handleCB);
455             break;
456         case ACL_GROUP_DELETE_DEVICE:
457             res = OCWrapperAclDeleteDeviceFromGroup(&endPoint, handleCB);
458             break;
459         case ACL_GROUP_GET_INFO:
460             res = OCWrapperAclGroupGetInfo(&endPoint, handleCB);
461             break;
462         case ACL_GROUP_INVITE_USER:
463             res = OCWrapperAclInviteUser(&endPoint, handleCB);
464             break;
465         case ACL_GROUP_GET_INVITE:
466             res = OCWrapperAclGetInvitation(&endPoint, handleAclGetInvitationCB);
467             break;
468         case ACL_GROUP_DELETE_INVITE:
469             res = OCWrapperAclDeleteInvitation(&endPoint, handleCB);
470             break;
471         case ACL_GROUP_CANCEL_INVITE:
472             res = OCWrapperAclCancelInvitation(&endPoint, handleCB);
473             break;
474         case ACL_POLICY_CHECK_REQUEST:
475             res = OCWrapperAclPolicyCheck(&endPoint, handleAclPolicyCheckCB);
476             break;
477         case ACL_ID_GET_BY_DEVICE:
478             res = OCWrapperAclIdGetByDevice(&endPoint, handleAclIdCB);
479             break;
480         case ACL_ID_CREATE:
481             res = OCWrapperAclIdCreate(&endPoint, handleAclIdCB);
482             break;
483         case ACL_ID_DELETE:
484             res = OCWrapperAclIdDelete(&endPoint, handleCB);
485             break;
486         case ACL_INDIVIDUAL_GET_INFO:
487             res = OCWrapperAclIndividualGetInfo(&endPoint, handleAclIndividualGetInfoCB);
488             break;
489         case ACL_INDIVIDUAL_UPDATE_ACE:
490             res = OCWrapperAclIndividualUpdateAce(&endPoint, handleCB);
491             break;
492         case ACL_INDIVIDUAL_UPDATE:
493             res = OCWrapperAclIndividualUpdate(&endPoint, handleCB);
494             break;
495         case ACL_INDIVIDUAL_DELETE:
496             res = OCWrapperAclIndividualDelete(&endPoint, handleCB);
497             break;
498         case ACL_INDIVIDUAL_DELETE_ACE:
499             res = OCWrapperAclIndividualDeleteAce(&endPoint, handleCB);
500             break;
501         case CSR_SIGN:
502             res = OCWrapperCertificateIssueRequest(&endPoint, handleCB);
503             break;
504         case DISCOVERY:
505             CLIENT_ONLY(mode);
506             res = InitDiscovery();
507             break;
508         case GET:
509             CLIENT_ONLY(mode);
510             res = InitRequest(OC_REST_GET);
511             break;
512         case PUT:
513             CLIENT_ONLY(mode);
514             res= InitRequest(OC_REST_PUT);
515             break;
516         case POST:
517             CLIENT_ONLY(mode);
518             res= InitRequest(OC_REST_POST);
519             break;
520         case USE_RSA:
521         {
522             int tmp = 0;
523             readInteger(&tmp, "Select Cipher Suite", "0 - ECDSA, other - RSA");
524             uint16_t cipher = tmp? MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA:
525                                    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8;
526             if (CA_STATUS_OK != CASelectCipherSuite(cipher, CA_ADAPTER_TCP))
527             {
528                 OIC_LOG(ERROR, TAG, "CASelectCipherSuite returned an error");
529             }
530             sendDataToServer = false;
531         }
532             break;
533         case SAVE_TRUST_CERT:
534             saveTrustCert();
535             sendDataToServer = false;
536             break;
537         case USE_SECURE_CONN:
538         {
539             int tmp = 0;
540             readInteger(&tmp, "CoAP protocol type", "0 - non-secure, 1 - secure");
541             setCoapPrefix(0 == tmp ? false : true);
542             sendDataToServer = false;
543         }
544             break;
545         case EXIT:
546             ca_mutex_free(mutex);
547             ca_cond_free(cond);
548             fExit = true;
549             sendDataToServer = false;
550             break;
551         default:
552             wrongRequest();
553             sendDataToServer = false;
554             break;
555         }
556
557         //if requests were sent then wait response
558         if (sendDataToServer)
559         {
560             if (OC_STACK_OK == res)
561             {
562                 ca_mutex_lock(mutex);
563                 ca_cond_wait_for(cond, mutex, timeout);
564                 ca_mutex_unlock(mutex);
565             }
566             else
567             {
568                 OIC_LOG_V(ERROR, TAG, "Request returned an error %d", res);
569             }
570         }
571     }
572 }
573
574 FILE* server_fopen(const char *path, const char *mode)
575 {
576     OC_UNUSED(path);
577     return fopen(fname, mode);
578 }
579
580 /**
581  * Check file accessibility
582  *
583  * @param[in] name        file path
584  * @return true           if check was successful
585  */
586 static bool checkConfig(const char *name)
587 {
588     FILE* file = fopen(name, "rb");
589
590     if (file)
591     {
592         fclose(file);
593         return true;
594     }
595     return false;
596 }
597
598 static void printUsage(char *name)
599 {
600     printf("Wrong arguments count!\n");
601     printf("Usage : %s <database_filename>\n", name);
602     printf("Examples : 1) %s 2) %s cloud.dat\n", name, name);
603 }
604
605 bool parseCommandLineArguments(int argc, char *argv[])
606 {
607     bool result = true;
608     if (2 == argc)
609     {
610         fname = argv[1];
611
612         if (!checkConfig(fname))
613         {
614             OIC_LOG_V(ERROR, TAG, "Can't open database file %s, exit!", fname);
615             result = false;
616         }
617     }
618     else if (argc > 2)
619     {
620         printUsage(argv[0]);
621         result = false;
622     }
623     return result;
624 }
625
626 OCStackResult initPersistentStorage()
627 {
628     //Initialize Persistent Storage for SVR database
629     static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
630
631     return OCRegisterPersistentStorageHandler(&ps);
632 }
633
634 OCStackResult startRequestsThread(OCMode *mode)
635 {
636     CAResult_t res = ca_thread_pool_init(1, &g_threadPoolHandle);
637     if (CA_STATUS_OK != res)
638     {
639         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
640         return res;
641     }
642
643     res = ca_thread_pool_add_task(g_threadPoolHandle, userRequests, mode);
644     if (CA_STATUS_OK != res)
645     {
646         OIC_LOG(ERROR, TAG, "thread pool add task error.");
647         ca_thread_pool_free(g_threadPoolHandle);
648     }
649     return res;
650 }
651
652 OCStackResult initProcess(OCMode mode)
653 {
654     return OCInit(NULL, 0, mode);
655 }
656
657 void startProcess()
658 {
659     while(false == fExit)
660     {
661         if (OCProcess() != OC_STACK_OK)
662         {
663             OIC_LOG(ERROR, TAG,"OCProcess process error, exit\n");
664             break;
665         }
666     }
667
668     if (OCStop() != OC_STACK_OK)
669     {
670         OIC_LOG(ERROR, TAG, "OCStop process error\n");
671     }
672 }
673
674 void freeThreadResources()
675 {
676     if (g_threadPoolHandle)
677     {
678         ca_thread_pool_free(g_threadPoolHandle);
679     }
680 }