OpenBIC
OpenSource Bridge-IC
libutil.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef LIBUTIL_H
18#define LIBUTIL_H
19
20#include <stdint.h>
21#include "ipmb.h"
22#include "hal_i2c.h"
23
24#define SAFE_FREE(p) \
25 if (p) { \
26 free(p); \
27 p = NULL; \
28 }
29
30#define SETBIT(x, y) (x | (1ULL << y))
31#define SETBITS(x, y, z) (x | (y << z))
32#define GETBIT(x, y) ((x & (1ULL << y)) > y)
33#define CLEARBIT(x, y) (x & (~(1ULL << y)))
34#define CLEARBITS(x, y, z) \
35 for (int i = y; i <= z; ++i) { \
36 x = CLEARBIT(x, i); \
37 }
38
39#define SHELL_CHECK_NULL_ARG(arg_ptr) \
40 if (arg_ptr == NULL) { \
41 shell_error(shell, "Parameter \"" #arg_ptr "\" passed in as NULL"); \
42 return; \
43 }
44
45#define SHELL_CHECK_NULL_ARG_WITH_RETURN(arg_ptr, ret_val) \
46 if (arg_ptr == NULL) { \
47 shell_error(shell, "Parameter \"" #arg_ptr "\" passed in as NULL"); \
48 return ret_val; \
49 }
50
51#define CHECK_NULL_ARG(arg_ptr) \
52 if (arg_ptr == NULL) { \
53 LOG_DBG("Parameter \"" #arg_ptr "\" passed in as NULL"); \
54 return; \
55 }
56
57#define CHECK_NULL_ARG_WITH_RETURN(arg_ptr, ret_val) \
58 if (arg_ptr == NULL) { \
59 LOG_DBG("Parameter \"" #arg_ptr "\" passed in as NULL"); \
60 return ret_val; \
61 }
62
63#define CHECK_ARG_WITH_RETURN(cmp, ret_val) \
64 if (cmp) { \
65 LOG_DBG("Parameter \"" #cmp "\" true"); \
66 return ret_val; \
67 }
68
69#define CHECK_MSGQ_INIT(msgq) CHECK_NULL_ARG((msgq)->buffer_start);
70
71#define CHECK_MSGQ_INIT_WITH_RETURN(msgq, ret_val) \
72 CHECK_NULL_ARG_WITH_RETURN((msgq)->buffer_start, ret_val);
73
74#define CHECK_MUTEX_INIT(mutex) CHECK_NULL_ARG((mutex)->wait_q.waitq.head);
75
76#define CHECK_MUTEX_INIT_WITH_RETURN(mutex, ret_val) \
77 CHECK_NULL_ARG_WITH_RETURN((mutex)->wait_q.waitq.head, ret_val);
78
79#define SET_FLAG_WITH_RETURN(flag, set_val, ret_val) \
80 if (flag != true) { \
81 LOG_DBG("Set parameter \"" #flag "\" to true"); \
82 flag = set_val; \
83 return ret_val; \
84 }
85
86typedef uint_least16_t char16_t;
87
92};
93
97};
98
102};
103
104ipmi_msg construct_ipmi_message(uint8_t seq_source, uint8_t netFn, uint8_t command,
105 uint8_t source_inft, uint8_t target_inft, uint16_t data_len,
106 uint8_t *data);
107
108I2C_MSG construct_i2c_message(uint8_t bus_id, uint8_t address, uint8_t tx_len, uint8_t *data,
109 uint8_t rx_len);
110
111void reverse_array(uint8_t arr[], uint8_t size);
112int ascii_to_val(uint8_t ascii_byte);
113uint32_t uint32_t_byte_reverse(uint32_t data);
114void convert_uint32_t_to_uint8_t_pointer(uint32_t data_32, uint8_t *data_8, uint8_t len,
115 uint8_t endian);
116void convert_uint8_t_pointer_to_uint32_t(uint32_t *data_32, const uint8_t *data_8, uint8_t len,
117 uint8_t endian);
118double power(double x, int y);
119int uint8_t_to_dec_ascii_pointer(uint8_t val, uint8_t *result, uint8_t len);
120int find_byte_data_in_buf(const uint8_t *buf, uint8_t byte_data, int start_index, int end_index);
121void clear_bits(uint32_t *value, int start_bit, int end_bit);
122void sort_bubble(int *array, int len);
123size_t strlen16(const char16_t *str);
124char16_t *strcpy16(char16_t *dest, const char16_t *src);
126
127#endif
uint32_t val
Definition: plat_util.c:40
uint8_t seq_source
Definition: ipmb.h:5
uint16_t data_len
Definition: ipmb.h:14
uint8_t data[]
Definition: isl69259.c:2
I2C_MSG construct_i2c_message(uint8_t bus_id, uint8_t address, uint8_t tx_len, uint8_t *data, uint8_t rx_len)
Definition: libutil.c:65
ipmi_msg construct_ipmi_message(uint8_t seq_source, uint8_t netFn, uint8_t command, uint8_t source_inft, uint8_t target_inft, uint16_t data_len, uint8_t *data)
Definition: libutil.c:37
size_t strlen16(const char16_t *str)
Definition: libutil.c:224
void reverse_array(uint8_t arr[], uint8_t size)
Definition: libutil.c:77
uint_least16_t char16_t
Definition: libutil.h:86
char16_t * ch16_strcat_char(char16_t *dest)
Definition: libutil.c:242
int find_byte_data_in_buf(const uint8_t *buf, uint8_t byte_data, int start_index, int end_index)
Definition: libutil.c:185
int ascii_to_val(uint8_t ascii_byte)
Definition: libutil.c:87
int uint8_t_to_dec_ascii_pointer(uint8_t val, uint8_t *result, uint8_t len)
Definition: libutil.c:163
void convert_uint8_t_pointer_to_uint32_t(uint32_t *data_32, const uint8_t *data_8, uint8_t len, uint8_t endian)
Definition: libutil.c:129
uint32_t uint32_t_byte_reverse(uint32_t data)
Definition: libutil.c:97
BYTE_ENDIAN
Definition: libutil.h:99
@ BIG_ENDIAN
Definition: libutil.h:101
@ SMALL_ENDIAN
Definition: libutil.h:100
BIT_SETTING_READING
Definition: libutil.h:88
@ BIT_SET
Definition: libutil.h:90
@ BIT_GET
Definition: libutil.h:91
@ BIT_CLEAR
Definition: libutil.h:89
void clear_bits(uint32_t *value, int start_bit, int end_bit)
Definition: libutil.c:196
double power(double x, int y)
Definition: libutil.c:147
BIT_SETTING_READING_N
Definition: libutil.h:94
@ BIT_CLEAR_N
Definition: libutil.h:96
@ BIT_SET_N
Definition: libutil.h:95
void convert_uint32_t_to_uint8_t_pointer(uint32_t data_32, uint8_t *data_8, uint8_t len, uint8_t endian)
Definition: libutil.c:109
char16_t * strcpy16(char16_t *dest, const char16_t *src)
Definition: libutil.c:233
void sort_bubble(int *array, int len)
Definition: libutil.c:208
uint16_t size
Definition: pldm_oem.h:0
Definition: hal_i2c.h:163
Definition: ipmb.h:165