Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / secure / common.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 "common.h"
21
22 #include "ocsecurity.h"
23 #include "logger.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28
29 #define TAG "sample-common"
30
31 OCStackResult SetCredentials(const char* filename) {
32
33     FILE *fp = NULL;
34     uint8_t *data = NULL;
35     struct stat st;
36     OCStackResult ret = OC_STACK_ERROR;
37
38     fp = fopen(filename, "rb");
39     if (fp)
40     {
41         if (stat(filename, &st) == 0)
42         {
43             data = (uint8_t*)malloc(st.st_size);
44             if (data)
45             {
46                 if (fread(data, 1, st.st_size, fp) == (size_t)st.st_size)
47                 {
48                     // Provide credentials to OC Stack
49                     ret = OCSecSetConfigData((OCSecConfigData *)data,
50                             st.st_size);
51                 }
52                 else
53                 {
54                     OC_LOG_V(FATAL, TAG, "Error in reading file %s", filename);
55                 }
56             }
57         }
58         fclose(fp);
59     }
60     else
61     {
62         OC_LOG_V(FATAL, TAG, "Unable to open %s file", filename);
63     }
64
65     free(data);
66
67     return ret;
68 }
69
70 const char *getResult(OCStackResult result) {
71     switch (result) {
72     case OC_STACK_OK:
73         return "OC_STACK_OK";
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";
106     #ifdef WITH_PRESENCE
107     case OC_STACK_PRESENCE_STOPPED:
108         return "OC_STACK_PRESENCE_STOPPED";
109     #endif
110     case OC_STACK_ERROR:
111         return "OC_STACK_ERROR";
112     default:
113         return "UNKNOWN";
114     }
115 }
116
117 void StripNewLineChar(char* str) {
118     int i = 0;
119     if (str)
120     {
121         while( str[i])
122         {
123             if (str[i] == '\n')
124                 str[i] = '\0';
125             i++;
126         }
127     }
128 }
129