Repository navigation
Expand file tree
/
Copy pathoffchip_statistics.c
More file actions
108 lines (102 loc) · 2.25 KB
/
Copy pathoffchip_statistics.c
File metadata and controls
108 lines (102 loc) · 2.25 KB
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
/*
* Copyright 2022 Morse Micro
* SPDX-License-Identifier: GPL-2.0-or-later OR LicenseRef-MorseMicroCommercial
*/
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "offchip_statistics.h"
#include "command.h"
/*
* Get the offchip data for this tag,
* or NULL if none can be found.
*/
struct statistics_offchip_data *get_stats_offchip(const struct morsectrl *mors, stats_tlv_tag_t tag)
{
struct statistics_offchip_data *res = NULL;
for (int i = 0; i < mors->n_stats; i++)
{
if (mors->stats[i].tag == tag)
{
res = mors->stats + i;
break;
}
}
return res;
}
int64_t get_signed_value_as_int64(const uint8_t *buf, uint32_t size)
{
int64_t n = 0;
switch (size)
{
case 1:
{
int8_t x;
memcpy(&x, buf, size);
n = (int8_t)x;
break;
}
case 2:
{
__le16 x;
memcpy(&x, buf, size);
n = (int16_t)le16toh(x);
break;
}
case 4:
{
__le32 x;
memcpy(&x, buf, size);
n = (int32_t)le32toh(x);
break;
}
case 8:
{
__le64 x;
memcpy(&x, buf, size);
n = (int64_t) le64toh(x);
break;
}
default:
mctrl_err("get_signed_value_as_int64 can't convert %d-byte quantity\n", size);
break;
}
return n;
}
uint64_t get_unsigned_value_as_uint64(const uint8_t *buf, uint32_t size)
{
uint64_t n = 0;
switch (size)
{
case 1:
{
n = buf[0];
break;
}
case 2:
{
__le16 x;
memcpy(&x, buf, size);
n = (uint64_t) le16toh(x);
break;
}
case 4:
{
__le32 x;
memcpy(&x, buf, size);
n = (uint64_t) le32toh(x);
break;
}
case 8:
{
__le64 x;
memcpy(&x, buf, size);
n = (uint64_t) le64toh(x);
break;
}
default:
mctrl_err("get_unsigned_value_as_uint64 can't convert %d-byte quantity\n", size);
break;
}
return n;
}