HPM APP
HPMicro Application solution
monitor_profile.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __MONITOR_PROFILE_H
9 #define __MONITOR_PROFILE_H
10 
11 #include <stdio.h>
12 #include <stdint.h>
13 #include "monitor_kconfig.h"
14 
15 #define MONITOR_PROFILE_VERSION (2)
16 
17 // START: 0x4850 起始位
18 // MAGIC: 0x4D44 标识位
19 // VER: 0x0001 版本号,可扩展
20 // EXTEND1: 0x00000000 协议扩展预留
21 // CRC: 0x00000000 范围为后续所有的内容
22 // INDEX: 0x0001 序号,用于同步,范围0~65535,当无ACK响应时会有重发机制存在;
23 // CMD: 0x8001/0x0001 最高位:1为上位机, 0为MCU
24 // RESULT: 0x0001 当CMD最高位是1 上位机时,RESULT为收到MCU的结果反馈;
25 // 当CMD最高位是0 MCU时, RESULT为收到上位机的结果反馈;
26 // TIMING: 0x00000000 上报间隔,用于控制主动上报数据的频率;
27 // EXTEND2: 0x00000000 协议扩展预留
28 // LENGH: 0x0001 PAYLOAD长度,可以是0(可以没有payload,只是ack响应);
29 // PAYLOAD: [] 数据内容
30 // END: 0xA55A 结束符
31 
32 #define MONITOR_START_CMD (0x4850)
33 #define MONITOR_MAGIC_CMD (0x4D44)
34 #define MONITOR_END_CMD (0xA55A)
35 
36 #define MONITOR_CMD_HOST_MASK (0x8000)
37 
38 #define MONITOR_PAYLOAD_MAXSIZE (MONITOR_PROFILE_MAXSIZE - sizeof(monitor_profile_t))
39 
40 #define MONITOR_PROFILE_MIN_SIZE (sizeof(monitor_profile_t) - 4) // 4 is payload point
41 
42 #define MONITOR_PAYLOAD_DATA_OFFSET ((MONITOR_PROFILE_MIN_SIZE-2)+(sizeof(monitor_payload_t)-4)+(sizeof(data_payload_t)-4))
43 
44 #define MONITOR_RESULT_ACK_MASK (0x8000)
45 
46 // uint16_t start;
47 // uint16_t magic;
48 // uint16_t version;
49 // uint32_t extend1;
50 // uint32_t crc32;
51 #define MONITOR_PROFILE_CRC_OFFSET (14)
52 // COUNT: 多包个数
53 // TYPE: 数据类型
54 // ADDRESS: 对应内存地址
55 // SIZE: 数据长度
56 // DATA: 数据值
57 // 。。。: 下一包数据,TYPE:SIZE:ADDRESS:DATA
58 
59 typedef struct
60 {
61  uint8_t type;
62  uint8_t rsvd1;
63  uint16_t rsvd2;
64  uint32_t addr_or_ch;
65  uint8_t *data;
66 } __attribute__((packed, aligned(1))) data_payload_t;
67 
68 typedef struct
69 {
70  uint8_t type;
71  uint8_t mode;
72  uint8_t expr;
73  uint8_t rsvd;
74  uint32_t addr_or_ch;
75  uint8_t *data;
76 } __attribute__((packed, aligned(1))) data_trigger_t;
77 
78 typedef struct
79 {
80  uint16_t pkt_count;
81  uint8_t rsvd;
82  uint8_t flag;
83  uint32_t dat_count;
84  uint32_t freq;
85  union {
86  data_payload_t *data_payload;
87  data_trigger_t *data_trigger;
88  };
89 } __attribute__((packed, aligned(1))) monitor_payload_t;
90 
91 typedef struct
92 {
93  uint16_t vid;
94  uint16_t pid;
95  uint64_t monitor_version;
96  uint32_t maxsize;
97  uint32_t memsize;
98  uint16_t max_count;
99  uint16_t threshold;
100 } __attribute__((packed, aligned(1))) monitor_info_t;
101 
102 typedef struct
103 {
104  uint16_t start;
105  uint16_t magic;
106  uint16_t version;
107  uint32_t extend1;
108  uint32_t crc32;
109  uint16_t index;
110  uint16_t cmd;
111  uint16_t result;
112  uint32_t timing;
113  uint32_t extend2;
114  uint32_t length;
115  monitor_payload_t *monitor_payload;
116  uint16_t end;
117 } __attribute__((packed, aligned(1))) monitor_profile_t;
118 
119 typedef enum
120 {
121  CMD_INVALID = 0x0000,
122  CMD_CONNECT = 0x0001,
123  CMD_GETINFO = 0x0002,
124 
125  CMD_GETVALUE = 0x00A0,
126  CMD_SETVALUE = 0x00A1,
127 
128  CMD_NOTIFY = 0x0A00,
129  CMD_STREAM = 0x0B00,
130  CMD_BUFFER = 0x0C00,
131  CMD_TRIGGER = 0x0D00,
133 
134 typedef enum
135 {
136  TYPE_BOOL = 0x0,
137  TYPE_UINT8 = 0x01,
138  TYPE_INT8 = 0x02,
139  TYPE_UINT16 = 0x03,
140  TYPE_INT16 = 0x04,
141  TYPE_UINT32 = 0x05,
142  TYPE_INT32 = 0x06,
143  TYPE_UINT64 = 0x07,
144  TYPE_INT64 = 0x08,
145  TYPE_FLOAT = 0x09,
146  TYPE_DOUBLE = 0x0A,
147  TYPE_STRUCT = 0x0B,
159  TYPE_UNKNOWN = 0xA0,
160 } DATA_TYPE;
161 
162 typedef enum
163 {
164  RESULT_BAD = 0x0001,
165  RESULT_HEAD_ERR = 0x0002,
166  RESULT_VER_ERR = 0x0003,
167  RESULT_CRC_ERR = 0x0004,
169  RESULT_GET_ERR = 0x0006,
170  RESULT_SET_ERR = 0x0007,
174  RESULT_CMD_ERR = 0x000B,
177 
178  RESULT_SUCCESS = 0x00FF,
179  RESULT_ON = 0x02FF,
180  RESULT_OFF = 0x01FF,
181  RESULT_LOSS = 0x0FFF,
183 
184 typedef enum
185 {
191 } TRIGGER_MODE;
192 
193 typedef enum
194 {
197 } TRIGGER_EXPR;
198 
200 
202 
203 #define MONITOR_PAYLOAD_CH_MASK (0xFFFF0000)
204 #define MONITOR_PAYLOAD_CH_DATA(ch) (ch & ~MONITOR_PAYLOAD_CH_MASK)
205 
206 // function
207 uint16_t monitor_get_value(uint8_t type, uint32_t addr, uint8_t *data);
208 
209 uint16_t monitor_set_value(uint8_t type, uint32_t addr, uint8_t *data);
210 
211 bool payload_is_channel_type(uint32_t value);
212 
214 
215 uint32_t monitor_profile_crc32(uint8_t *data);
216 
217 void monitor_profile_set_header(uint8_t *data);
218 
219 void monitor_profile_set_end(uint8_t *data);
220 
221 void monitor_profile_set_crc32(uint8_t *data, uint32_t crc32);
222 
223 uint32_t monitor_profile_get_crc32(uint8_t *data);
224 
225 uint16_t monitor_profile_get_index(uint8_t *data);
226 
227 void monitor_profile_set_index(uint8_t *data, uint16_t index);
228 
229 uint16_t monitor_profile_get_cmd(uint8_t *data);
230 
231 void monitor_profile_set_cmd(uint8_t *data, uint16_t cmd);
232 
233 uint16_t monitor_profile_get_result(uint8_t *data);
234 
235 void monitor_profile_set_result(uint8_t *data, uint16_t result);
236 
237 uint32_t monitor_profile_get_timing(uint8_t *data);
238 
239 void monitor_profile_set_timing(uint8_t *data, uint32_t tick_us);
240 
241 uint32_t monitor_profile_get_length(uint8_t *data);
242 
243 void monitor_profile_set_length(uint8_t *data, uint32_t length);
244 
245 monitor_payload_t *monitor_profile_get_payload(uint8_t *data);
246 
247 monitor_info_t *monitor_profile_get_monitorinfo(uint8_t *data);
248 
249 uint16_t monitor_profile_payload_process(uint16_t cmd, uint8_t *data, uint32_t length);
250 
251 uint16_t monitor_profile_info_process(uint8_t *data);
252 
253 uint16_t monitor_profile_cmd_process(uint16_t cmd, uint16_t parse_result, uint8_t *data, uint32_t length);
254 
255 uint16_t monitor_profile_parse(uint8_t *data, uint32_t length, uint32_t *drop_offset, uint32_t *expect_length);
256 
257 const monitor_var_info_t *monitor_ch_info_find_by_name(const char *name);
258 
260 
262 
264 
265 #endif //__MONITOR_PROFILE_H
uint16_t monitor_profile_parse(uint8_t *data, uint32_t length, uint32_t *drop_offset, uint32_t *expect_length)
Definition: monitor_profile.c:566
void monitor_profile_set_cmd(uint8_t *data, uint16_t cmd)
Definition: monitor_profile.c:333
const uint32_t d7d75a2a6eebafc3d2ec0a0be089e05322057c4c308d648b374b362ea7eff5fa
void monitor_profile_set_index(uint8_t *data, uint16_t index)
Definition: monitor_profile.c:321
uint16_t monitor_profile_info_process(uint8_t *data)
Definition: monitor_profile.c:501
uint16_t monitor_set_value(uint8_t type, uint32_t addr, uint8_t *data)
Definition: monitor_profile.c:137
const monitor_var_info_t * monitor_ch_info_find_by_channel(uint8_t channel)
Definition: monitor_profile.c:686
TRIGGER_EXPR
Definition: monitor_profile.h:194
@ TRIGGER_EXPR_OR
Definition: monitor_profile.h:196
@ TRIGGER_EXPR_AND
Definition: monitor_profile.h:195
const monitor_var_info_t * monitor_ch_info_find_by_name(const char *name)
Definition: monitor_profile.c:674
uint16_t monitor_get_value(uint8_t type, uint32_t addr, uint8_t *data)
Definition: monitor_profile.c:67
void monitor_ch_info_print_registry(void)
Definition: monitor_profile.c:703
void monitor_profile_set_length(uint8_t *data, uint32_t length)
Definition: monitor_profile.c:369
uint32_t monitor_profile_get_timing(uint8_t *data)
Definition: monitor_profile.c:351
uint32_t monitor_profile_crc32(uint8_t *data)
Definition: monitor_profile.c:281
uint32_t monitor_ch_info_get_registry_count(void)
Definition: monitor_profile.c:698
uint32_t monitor_profile_get_length(uint8_t *data)
Definition: monitor_profile.c:363
uint16_t monitor_profile_get_index(uint8_t *data)
Definition: monitor_profile.c:315
void monitor_profile_set_timing(uint8_t *data, uint32_t tick_us)
Definition: monitor_profile.c:357
bool payload_is_channel_type(uint32_t value)
Definition: monitor_profile.c:207
MONITOR_RESULT
Definition: monitor_profile.h:163
@ RESULT_GET_ERR
Definition: monitor_profile.h:169
@ RESULT_PAYLOAD_ERR
Definition: monitor_profile.h:168
@ RESULT_DATA_TYPE_ERR
Definition: monitor_profile.h:172
@ RESULT_VER_ERR
Definition: monitor_profile.h:166
@ RESULT_HEAD_ERR
Definition: monitor_profile.h:165
@ RESULT_OFF
Definition: monitor_profile.h:180
@ RESULT_CONFIG_ERR
Definition: monitor_profile.h:175
@ RESULT_CRC_ERR
Definition: monitor_profile.h:167
@ RESULT_RUNNING_ERR
Definition: monitor_profile.h:176
@ RESULT_LOSS
Definition: monitor_profile.h:181
@ RESULT_CMD_ERR
Definition: monitor_profile.h:174
@ RESULT_SET_ERR
Definition: monitor_profile.h:170
@ RESULT_PRFILE_OVERFLOW
Definition: monitor_profile.h:173
@ RESULT_BAD
Definition: monitor_profile.h:164
@ RESULT_ON
Definition: monitor_profile.h:179
@ RESULT_DATA_LEN_ERR
Definition: monitor_profile.h:171
@ RESULT_SUCCESS
Definition: monitor_profile.h:178
int monitor_type_convert_byte(DATA_TYPE type)
Definition: monitor_profile.c:212
const uint64_t be8443a5d67825271edb6f6bee202d6125a38e6aa8f2acbd54652d113af8793b
void monitor_profile_set_crc32(uint8_t *data, uint32_t crc32)
Definition: monitor_profile.c:303
DATA_TYPE
Definition: monitor_profile.h:135
@ TYPE_DOUBLE
Definition: monitor_profile.h:146
@ TYPE_INT32
Definition: monitor_profile.h:142
@ TYPE_INT64
Definition: monitor_profile.h:144
@ TYPE_INT8
Definition: monitor_profile.h:138
@ TYPE_ARRAY_FLOAT
Definition: monitor_profile.h:157
@ TYPE_FLOAT
Definition: monitor_profile.h:145
@ TYPE_ARRAY_DOUBLE
Definition: monitor_profile.h:158
@ TYPE_INT16
Definition: monitor_profile.h:140
@ TYPE_ARRAY_UINT16
Definition: monitor_profile.h:151
@ TYPE_BOOL
Definition: monitor_profile.h:136
@ TYPE_ARRAY_UINT64
Definition: monitor_profile.h:155
@ TYPE_ARRAY_UINT32
Definition: monitor_profile.h:153
@ TYPE_UNKNOWN
Definition: monitor_profile.h:159
@ TYPE_ARRAY_STRUCT
Definition: monitor_profile.h:148
@ TYPE_ARRAY_INT16
Definition: monitor_profile.h:152
@ TYPE_STRUCT
Definition: monitor_profile.h:147
@ TYPE_UINT8
Definition: monitor_profile.h:137
@ TYPE_UINT32
Definition: monitor_profile.h:141
@ TYPE_UINT16
Definition: monitor_profile.h:139
@ TYPE_ARRAY_UINT8
Definition: monitor_profile.h:149
@ TYPE_UINT64
Definition: monitor_profile.h:143
@ TYPE_ARRAY_INT64
Definition: monitor_profile.h:156
@ TYPE_ARRAY_INT8
Definition: monitor_profile.h:150
@ TYPE_ARRAY_INT32
Definition: monitor_profile.h:154
uint16_t monitor_profile_cmd_process(uint16_t cmd, uint16_t parse_result, uint8_t *data, uint32_t length)
Definition: monitor_profile.c:522
struct __attribute__ aligned
monitor_payload_t * monitor_profile_get_payload(uint8_t *data)
Definition: monitor_profile.c:375
monitor_info_t * monitor_profile_get_monitorinfo(uint8_t *data)
Definition: monitor_profile.c:381
uint16_t monitor_profile_payload_process(uint16_t cmd, uint8_t *data, uint32_t length)
Definition: monitor_profile.c:387
uint32_t monitor_profile_get_crc32(uint8_t *data)
Definition: monitor_profile.c:309
uint16_t monitor_profile_get_cmd(uint8_t *data)
Definition: monitor_profile.c:327
TRIGGER_MODE
Definition: monitor_profile.h:185
@ TRIGGER_MODE_EDGE_BOTH
Definition: monitor_profile.h:186
@ TRIGGER_MODE_LEVEL_HIGH
Definition: monitor_profile.h:190
@ TRIGGER_MODE_EDGE_RISING
Definition: monitor_profile.h:187
@ TRIGGER_MODE_EDGE_FALLING
Definition: monitor_profile.h:188
@ TRIGGER_MODE_LEVEL_LOW
Definition: monitor_profile.h:189
void monitor_profile_set_header(uint8_t *data)
Definition: monitor_profile.c:288
uint16_t monitor_profile_get_result(uint8_t *data)
Definition: monitor_profile.c:339
DEVICE_CMD
Definition: monitor_profile.h:120
@ CMD_CONNECT
Definition: monitor_profile.h:122
@ CMD_INVALID
Definition: monitor_profile.h:121
@ CMD_BUFFER
Definition: monitor_profile.h:130
@ CMD_STREAM
Definition: monitor_profile.h:129
@ CMD_TRIGGER
Definition: monitor_profile.h:131
@ CMD_GETVALUE
Definition: monitor_profile.h:125
@ CMD_GETINFO
Definition: monitor_profile.h:123
@ CMD_SETVALUE
Definition: monitor_profile.h:126
@ CMD_NOTIFY
Definition: monitor_profile.h:128
void monitor_profile_set_result(uint8_t *data, uint16_t result)
Definition: monitor_profile.c:345
void monitor_profile_set_end(uint8_t *data)
Definition: monitor_profile.c:297
Definition: monitor_kconfig.h:186