1 /*
2  * Copyright (c) 2008 Allied Telesis Labs.
3  * Copyright (c) 2012 Red Hat, Inc.
4  *
5  * All rights reserved.
6  *
7  * Author: Angus Salkeld (ahsalkeld@gmail.com)
8  *
9  * This software licensed under BSD license, the text of which follows:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * - Redistributions of source code must retain the above copyright notice,
15  *   this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright notice,
17  *   this list of conditions and the following disclaimer in the documentation
18  *   and/or other materials provided with the distribution.
19  * - Neither the name of the MontaVista Software, Inc. nor the names of its
20  *   contributors may be used to endorse or promote products derived from this
21  *   software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef COROTYPES_H_DEFINED
37 #define COROTYPES_H_DEFINED
38 
39 #include <stdint.h>
40 #include <errno.h>
41 #include <time.h>
42 #include <sys/time.h>
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /**
49  * @brief cs_time_t
50  */
51 typedef int64_t cs_time_t;
52 
53 #define CS_FALSE 0
54 #define CS_TRUE !CS_FALSE
55 #define CS_MAX_NAME_LENGTH 256
56 #define CS_TIME_END    ((cs_time_t)0x7FFFFFFFFFFFFFFFULL)
57 #define CS_MAX(x, y) (((x) > (y)) ? (x) : (y))
58 
59 /**
60  * @brief The cs_name_t struct
61  */
62 typedef struct {
63    uint16_t length;
64    uint8_t value[CS_MAX_NAME_LENGTH];
65 } cs_name_t;
66 
67 
68 /**
69  * @brief The cs_version_t struct
70  */
71 typedef struct {
72    char releaseCode;
73    unsigned char majorVersion;
74    unsigned char minorVersion;
75 } cs_version_t;
76 
77 /**
78  * @brief The cs_dispatch_flags_t enum
79  */
80 typedef enum {
81 	CS_DISPATCH_ONE = 1,
82 	CS_DISPATCH_ALL = 2,
83 	CS_DISPATCH_BLOCKING = 3,
84 	CS_DISPATCH_ONE_NONBLOCKING = 4
85 } cs_dispatch_flags_t;
86 
87 #define CS_TRACK_CURRENT 0x01
88 #define CS_TRACK_CHANGES 0x02
89 #define CS_TRACK_CHANGES_ONLY 0x04
90 
91 /**
92  * @brief The cs_error_t enum
93  */
94 typedef enum {
95    CS_OK = 1,
96    CS_ERR_LIBRARY = 2,
97    CS_ERR_VERSION = 3,
98    CS_ERR_INIT = 4,
99    CS_ERR_TIMEOUT = 5,
100    CS_ERR_TRY_AGAIN = 6,
101    CS_ERR_INVALID_PARAM = 7,
102    CS_ERR_NO_MEMORY = 8,
103    CS_ERR_BAD_HANDLE = 9,
104    CS_ERR_BUSY = 10,
105    CS_ERR_ACCESS = 11,
106    CS_ERR_NOT_EXIST = 12,
107    CS_ERR_NAME_TOO_LONG = 13,
108    CS_ERR_EXIST = 14,
109    CS_ERR_NO_SPACE = 15,
110    CS_ERR_INTERRUPT = 16,
111    CS_ERR_NAME_NOT_FOUND = 17,
112    CS_ERR_NO_RESOURCES = 18,
113    CS_ERR_NOT_SUPPORTED = 19,
114    CS_ERR_BAD_OPERATION = 20,
115    CS_ERR_FAILED_OPERATION = 21,
116    CS_ERR_MESSAGE_ERROR = 22,
117    CS_ERR_QUEUE_FULL = 23,
118    CS_ERR_QUEUE_NOT_AVAILABLE = 24,
119    CS_ERR_BAD_FLAGS = 25,
120    CS_ERR_TOO_BIG = 26,
121    CS_ERR_NO_SECTIONS = 27,
122    CS_ERR_CONTEXT_NOT_FOUND = 28,
123    CS_ERR_TOO_MANY_GROUPS = 30,
124    CS_ERR_SECURITY = 100
125 } cs_error_t;
126 
127 #define CS_IPC_TIMEOUT_MS -1
128 
129 #define CS_TIME_MS_IN_SEC   1000ULL
130 #define CS_TIME_US_IN_SEC   1000000ULL
131 #define CS_TIME_NS_IN_SEC   1000000000ULL
132 #define CS_TIME_US_IN_MSEC  1000ULL
133 #define CS_TIME_NS_IN_MSEC  1000000ULL
134 #define CS_TIME_NS_IN_USEC  1000ULL
135 
136 /**
137  * @brief cs_timestamp_get
138  * @return
139  */
cs_timestamp_get(void)140 static inline uint64_t cs_timestamp_get(void)
141 {
142 	uint64_t result;
143 
144 #if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
145 	struct timespec ts;
146 
147 	clock_gettime (CLOCK_MONOTONIC, &ts);
148 	result = (ts.tv_sec * CS_TIME_NS_IN_SEC) + (uint64_t)ts.tv_nsec;
149 #else
150 	struct timeval time_from_epoch;
151 
152 	gettimeofday (&time_from_epoch, 0);
153 	result = ((time_from_epoch.tv_sec * CS_TIME_NS_IN_SEC) +
154 		(time_from_epoch.tv_usec * CS_TIME_NS_IN_USEC));
155 #endif
156 
157 	return result;
158 }
159 /**
160  * @brief qb_to_cs_error
161  * @param result
162  * @return
163  */
164 cs_error_t qb_to_cs_error (int result);
165 
166 /**
167  * @brief cs_strerror
168  * @param err
169  * @return
170  */
171 const char * cs_strerror(cs_error_t err);
172 
173 /**
174  * @brief hdb_error_to_cs
175  * @param res
176  * @return
177  */
178 cs_error_t hdb_error_to_cs (int res);
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif /* COROTYPES_H_DEFINED */
185 
186