1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <ocsecurity.h>
26 #include <sys/types.h>
29 #define TAG "sample-common"
31 OCStackResult SetCredentials(const char* filename) {
36 OCStackResult ret = OC_STACK_ERROR;
38 fp = fopen(filename, "rb");
41 if (stat(filename, &st) == 0)
43 data = (uint8_t*)malloc(st.st_size);
46 if (fread(data, 1, st.st_size, fp) == (size_t)st.st_size)
48 // Provide credentials to OC Stack
49 ret = OCSecSetConfigData((OCSecConfigData *)data,
54 OC_LOG_V(FATAL, TAG, "Error in reading file %s", filename);
62 OC_LOG_V(FATAL, TAG, "Unable to open %s file", filename);
70 const char *getResult(OCStackResult result) {
74 case OC_STACK_RESOURCE_CREATED:
75 return "OC_STACK_RESOURCE_CREATED";
76 case OC_STACK_RESOURCE_DELETED:
77 return "OC_STACK_RESOURCE_DELETED";
78 case OC_STACK_INVALID_URI:
79 return "OC_STACK_INVALID_URI";
80 case OC_STACK_INVALID_QUERY:
81 return "OC_STACK_INVALID_QUERY";
82 case OC_STACK_INVALID_IP:
83 return "OC_STACK_INVALID_IP";
84 case OC_STACK_INVALID_PORT:
85 return "OC_STACK_INVALID_PORT";
86 case OC_STACK_INVALID_CALLBACK:
87 return "OC_STACK_INVALID_CALLBACK";
88 case OC_STACK_INVALID_METHOD:
89 return "OC_STACK_INVALID_METHOD";
90 case OC_STACK_NO_MEMORY:
91 return "OC_STACK_NO_MEMORY";
92 case OC_STACK_COMM_ERROR:
93 return "OC_STACK_COMM_ERROR";
94 case OC_STACK_INVALID_PARAM:
95 return "OC_STACK_INVALID_PARAM";
96 case OC_STACK_NOTIMPL:
97 return "OC_STACK_NOTIMPL";
98 case OC_STACK_NO_RESOURCE:
99 return "OC_STACK_NO_RESOURCE";
100 case OC_STACK_RESOURCE_ERROR:
101 return "OC_STACK_RESOURCE_ERROR";
102 case OC_STACK_SLOW_RESOURCE:
103 return "OC_STACK_SLOW_RESOURCE";
104 case OC_STACK_NO_OBSERVERS:
105 return "OC_STACK_NO_OBSERVERS";
107 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
108 return "OC_STACK_VIRTUAL_DO_NOT_HANDLE";
109 case OC_STACK_PRESENCE_STOPPED:
110 return "OC_STACK_PRESENCE_STOPPED";
113 return "OC_STACK_ERROR";
119 void StripNewLineChar(char* str) {