File: debug-util.c

package info (click to toggle)
freeipmi 1.6.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,452 kB
  • sloc: ansic: 318,860; sh: 4,648; makefile: 2,090; perl: 1,078
file content (155 lines) | stat: -rw-r--r-- 4,454 bytes parent folder | download | duplicates (6)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (C) 2003-2015 FreeIPMI Core Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#if STDC_HEADERS
#include <string.h>
#include <stdarg.h>
#endif /* STDC_HEADERS */
#include <assert.h>

#include <freeipmi/freeipmi.h>

#include "debug-util.h"

#include "freeipmi-portability.h"

int
debug_hdr_str (uint8_t packet_type,
               uint8_t packet_direction,
               unsigned int packet_flags,
               const char *str,
               char *hdrbuf,
               unsigned int hdrbuf_len)
{
  char *fmt_inband =
    "=====================================================\n"
    "%s%s %s\n"
    "=====================================================";
  char *fmt_outofband =
    "=====================================================\n"
    "%s %s%s %s\n"
    "=====================================================";
  char *str_direction;
  char *str_prefix;
  int len;

  assert (packet_type == DEBUG_UTIL_TYPE_NONE
          || packet_type == DEBUG_UTIL_TYPE_INBAND
          || packet_type == DEBUG_UTIL_TYPE_IPMI_1_5
          || packet_type == DEBUG_UTIL_TYPE_IPMI_2_0);
  assert (packet_direction == DEBUG_UTIL_DIRECTION_NONE
          || packet_direction == DEBUG_UTIL_DIRECTION_REQUEST
          || packet_direction == DEBUG_UTIL_DIRECTION_RESPONSE);
  assert (str);
  assert (hdrbuf);
  assert (hdrbuf_len);

  if (packet_direction == DEBUG_UTIL_DIRECTION_REQUEST)
    str_direction = "Request";
  else if (packet_direction == DEBUG_UTIL_DIRECTION_RESPONSE)
    str_direction = "Response";
  else
    str_direction = "";

  if (packet_flags & DEBUG_UTIL_FLAGS_GROUP_EXTENSION)
    str_prefix = "Group Extension - ";
  else if (packet_flags & DEBUG_UTIL_FLAGS_OEM_GROUP)
    str_prefix = "OEM Group - ";
  else if (packet_flags & DEBUG_UTIL_FLAGS_OEM)
    str_prefix = "OEM - ";
  else
    str_prefix = "";

  if (packet_type == DEBUG_UTIL_TYPE_NONE
      || packet_type == DEBUG_UTIL_TYPE_INBAND)
    len = snprintf (hdrbuf,
                    hdrbuf_len,
                    fmt_inband,
                    str_prefix,
                    str,
                    str_direction);
  else
    {
      char *str_version;

      if (packet_type == DEBUG_UTIL_TYPE_IPMI_1_5)
        str_version = "IPMI 1.5";
      else
        str_version = "IPMI 2.0";

      len = snprintf (hdrbuf,
                      hdrbuf_len,
                      fmt_outofband,
                      str_version,
                      str_prefix,
                      str,
                      str_direction);
    }

  if (len < 0 || len >= hdrbuf_len)
    return (-1);

  return (0);
}

int
debug_hdr_cmd (uint8_t packet_type,
               uint8_t packet_direction,
               uint8_t net_fn,
               uint8_t cmd,
               uint8_t group_extension,
               char *hdrbuf,
               unsigned int hdrbuf_len)
{
  const char *str_cmd;
  unsigned int packet_flags = 0;

  if (IPMI_NET_FN_GROUP_EXTENSION (net_fn))
    {
      if (group_extension == IPMI_NET_FN_GROUP_EXTENSION_IDENTIFICATION_DCMI)
        str_cmd = ipmi_cmd_dcmi_str (cmd);
      else
        str_cmd = "Unknown";
      packet_flags = DEBUG_UTIL_FLAGS_GROUP_EXTENSION;
    }
  else if (IPMI_NET_FN_OEM_GROUP (net_fn))
    {
      str_cmd = "Unknown";
      packet_flags = DEBUG_UTIL_FLAGS_OEM_GROUP;
    }
  else if (IPMI_NET_FN_CONTROLLER_SPECIFIC_OEM_GROUP (net_fn))
    {
      str_cmd = "Unknown";
      packet_flags = DEBUG_UTIL_FLAGS_OEM;
    }
  else
    str_cmd = ipmi_cmd_str (net_fn, cmd);

  return (debug_hdr_str (packet_type,
                         packet_direction,
                         packet_flags,
                         str_cmd,
                         hdrbuf,
                         hdrbuf_len));
}