-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.cpp
127 lines (115 loc) · 3.63 KB
/
common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//******************************************************************
//
// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "common.h"
#include "ocstack.h"
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#define TAG "sample-common"
const char *getResult(OCStackResult result) {
switch (result) {
case OC_STACK_OK:
return "OC_STACK_OK";
case OC_STACK_RESOURCE_CREATED:
return "OC_STACK_RESOURCE_CREATED";
case OC_STACK_RESOURCE_DELETED:
return "OC_STACK_RESOURCE_DELETED";
case OC_STACK_INVALID_URI:
return "OC_STACK_INVALID_URI";
case OC_STACK_INVALID_QUERY:
return "OC_STACK_INVALID_QUERY";
case OC_STACK_INVALID_IP:
return "OC_STACK_INVALID_IP";
case OC_STACK_INVALID_PORT:
return "OC_STACK_INVALID_PORT";
case OC_STACK_INVALID_CALLBACK:
return "OC_STACK_INVALID_CALLBACK";
case OC_STACK_INVALID_METHOD:
return "OC_STACK_INVALID_METHOD";
case OC_STACK_NO_MEMORY:
return "OC_STACK_NO_MEMORY";
case OC_STACK_COMM_ERROR:
return "OC_STACK_COMM_ERROR";
case OC_STACK_INVALID_PARAM:
return "OC_STACK_INVALID_PARAM";
case OC_STACK_NOTIMPL:
return "OC_STACK_NOTIMPL";
case OC_STACK_NO_RESOURCE:
return "OC_STACK_NO_RESOURCE";
case OC_STACK_RESOURCE_ERROR:
return "OC_STACK_RESOURCE_ERROR";
case OC_STACK_SLOW_RESOURCE:
return "OC_STACK_SLOW_RESOURCE";
case OC_STACK_NO_OBSERVERS:
return "OC_STACK_NO_OBSERVERS";
case OC_STACK_UNAUTHORIZED_REQ:
return "OC_STACK_UNAUTHORIZED_REQ";
#ifdef WITH_PRESENCE
case OC_STACK_PRESENCE_STOPPED:
return "OC_STACK_PRESENCE_STOPPED";
#endif
case OC_STACK_ERROR:
return "OC_STACK_ERROR";
default:
return "UNKNOWN";
}
}
void StripNewLineChar(char* str) {
int i = 0;
if (str)
{
while( str[i])
{
if (str[i] == '\n')
str[i] = '\0';
i++;
}
}
}
time_t _user_set_time = 0;
void getCurrentTime(char * buf) {
time_t now;
time(&now);
now -= _user_set_time;
struct tm* tm_info;
tm_info = localtime(&now);
strftime(buf, 25, "%Y-%m-%dT%H:%M:%S%z", tm_info);
}
void setUserTime(char * buf) {
time_t now;
time(&now);
struct tm _user_time = {0};
int year, month, day, hour, min, sec, timezone = 0;
sscanf(buf, "%04d-%02d-%02dT%02d:%02d:%02d+%04d",
&year, &month, &day, &hour, &min, &sec, &timezone);
_user_time.tm_year = year - 1900;
_user_time.tm_mon = month -1;
_user_time.tm_mday = day;
_user_time.tm_hour = hour;
_user_time.tm_min = min;
_user_time.tm_sec = sec;
//printf("%04d-%02d-%02dT%02d:%02d:%02d+%04d\n",
// year, month, day, hour, min, sec, timezone);
time_t user_time = mktime(&_user_time);
_user_set_time = now - user_time;
}