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
90};
91
95};
96
100};
101
102ipmi_msg construct_ipmi_message(uint8_t seq_source, uint8_t netFn, uint8_t command,
103 uint8_t source_inft, uint8_t target_inft, uint16_t data_len,
104 uint8_t *data);
105
106I2C_MSG construct_i2c_message(uint8_t bus_id, uint8_t address, uint8_t tx_len, uint8_t *data,
107 uint8_t rx_len);
108
109void reverse_array(uint8_t arr[], uint8_t size);
110int ascii_to_val(uint8_t ascii_byte);
111uint32_t uint32_t_byte_reverse(uint32_t data);
112void convert_uint32_t_to_uint8_t_pointer(uint32_t data_32, uint8_t *data_8, uint8_t len,
113 uint8_t endian);
114void convert_uint8_t_pointer_to_uint32_t(uint32_t *data_32, const uint8_t *data_8, uint8_t len,
115 uint8_t endian);
116double power(double x, int y);
117int uint8_t_to_dec_ascii_pointer(uint8_t val, uint8_t *result, uint8_t len);
118int find_byte_data_in_buf(const uint8_t *buf, uint8_t byte_data, int start_index, int end_index);
119void clear_bits(uint32_t *value, int start_bit, int end_bit);
120void sort_bubble(int *array, int len);
121
122#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
void reverse_array(uint8_t arr[], uint8_t size)
Definition: libutil.c:77
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:97
@ BIG_ENDIAN
Definition: libutil.h:99
@ SMALL_ENDIAN
Definition: libutil.h:98
BIT_SETTING_READING
Definition: libutil.h:86
@ BIT_SET
Definition: libutil.h:88
@ BIT_GET
Definition: libutil.h:89
@ BIT_CLEAR
Definition: libutil.h:87
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:92
@ BIT_CLEAR_N
Definition: libutil.h:94
@ BIT_SET_N
Definition: libutil.h:93
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
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