Newer
Older
/*
* common.c: Common utils of Get/Set/Operate handlers
*
* Copyright (C) 2023 IOPSYS Software Solutions AB. All rights reserved.
*
* Author: Vivek Dutta <vivek.dutta@iopsys.eu>
* Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
* Author: Iryna Antsyferova <iryna.antsyferova@iopsys.eu>
*
* See LICENSE file for license related information.
*/
#include "common.h"
#include "get_helper.h"
static unsigned char gLogLevel = 2;
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Logging utilities
void set_debug_level(unsigned char level)
{
gLogLevel = level;
}
void print_error(const char *format, ...)
{
va_list arglist;
if (gLogLevel < 1)
return;
va_start(arglist, format);
vsyslog(LOG_ERR, format, arglist);
va_end(arglist);
}
void print_warning(const char *format, ...)
{
va_list arglist;
if (gLogLevel < 2)
return;
va_start(arglist, format);
vsyslog(LOG_WARNING, format, arglist);
va_end(arglist);
}
void print_info(const char *format, ...)
{
va_list arglist;
if (gLogLevel < 3)
return;
va_start(arglist, format);
vsyslog(LOG_INFO, format, arglist);
va_end(arglist);
}
void print_debug(const char *format, ...)
{
va_list arglist;
if (gLogLevel < 4)
return;
va_start(arglist, format);
vsyslog(LOG_DEBUG, format, arglist);
va_end(arglist);
}
bool is_str_eq(const char *s1, const char *s2)
{
if (strcmp(s1, s2) == 0)
return true;
return false;
}
bool get_boolean_string(char *value)
{
if (!value)
return false;
if (strncasecmp(value, "true", 4) == 0 ||
value[0] == '1' ||
strncasecmp(value, "on", 2) == 0 ||
strncasecmp(value, "yes", 3) == 0 ||
strncasecmp(value, "enabled", 7) == 0)
return true;
return false;
}
int get_dm_type(char *dm_type)
{
if (dm_type == NULL)
return DMT_STRING;
if (DM_STRCMP(dm_type, DMT_TYPE[DMT_STRING]) == 0)
return DMT_STRING;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_UNINT]) == 0)
return DMT_UNINT;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_INT]) == 0)
return DMT_INT;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_UNLONG]) == 0)
return DMT_UNLONG;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_LONG]) == 0)
return DMT_LONG;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_BOOL]) == 0)
return DMT_BOOL;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_TIME]) == 0)
return DMT_TIME;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_HEXBIN]) == 0)
return DMT_HEXBIN;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_BASE64]) == 0)
return DMT_BASE64;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_COMMAND]) == 0)
return DMT_COMMAND;
else if (DM_STRCMP(dm_type, DMT_TYPE[DMT_EVENT]) == 0)
return DMT_EVENT;
else
return DMT_STRING;
return DMT_STRING;
}
eParamType get_eparam_type(char *dm_type, int *err)
{
*err = 0;
switch (get_dm_type(dm_type))
{
case DMT_STRING: return PARAM_STRING;
case DMT_UNINT: return PARAM_UNSIGNED_INTEGER;
case DMT_INT: return PARAM_INTEGER;
case DMT_UNLONG: return PARAM_UNSIGNED_LONG;
case DMT_LONG: return PARAM_LONG;
case DMT_BOOL: return PARAM_BOOLEAN;
case DMT_HEXBIN: return PARAM_HEXBINARY;
case DMT_BASE64: return PARAM_BASE64;
case DMT_TIME: return PARAM_STRING;
default: break;
}
*err = -1;
return 0;
}
int get_proto_type(const char *proto)
{
int type = BBFDM_BOTH;
if (proto) {
if (is_str_eq("cwmp", proto))
type = BBFDM_CWMP;
else if (is_str_eq("usp", proto))
type = BBFDM_USP;
else
type = BBFDM_BOTH;
}
return type;
}
int get_instance_mode(int instance_mode)
{
if (instance_mode > INSTANCE_MODE_ALIAS)
instance_mode = INSTANCE_MODE_NUMBER;
return instance_mode;
}