1 /* 2 * Copyright (c) 2018-2020 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef TP_H 8 #define TP_H 9 10 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 14 #include <sys/types.h> 15 #include <zephyr/data/json.h> 16 #include <zephyr/net/net_pkt.h> 17 #include "connection.h" 18 19 #if defined(CONFIG_NET_TEST_PROTOCOL) 20 21 #define TP_SEQ 0 22 #define TP_ACK 1 23 24 #define TP_BOOL 1 25 #define TP_INT 2 26 27 enum tp_type { /* Test protocol message type */ 28 TP_NONE = 0, 29 TP_COMMAND, 30 TP_CONFIG_REQUEST, 31 TP_CONFIG_REPLY, 32 TP_INTROSPECT_REQUEST, 33 TP_INTROSPECT_REPLY, 34 TP_INTROSPECT_MEMORY_REQUEST, 35 TP_INTROSPECT_MEMORY_REPLY, 36 TP_INTROSPECT_PACKETS_REQUEST, 37 TP_INTROSPECT_PACKETS_REPLY, 38 TP_DEBUG_STOP, 39 TP_DEBUG_STEP, 40 TP_DEBUG_CONTINUE, 41 TP_DEBUG_RESPONSE, 42 TP_DEBUG_BREAKPOINT_ADD, 43 TP_DEBUG_BREAKPOINT_DELETE, 44 TP_TRACE_ADD, 45 TP_TRACE_DELETE 46 }; 47 48 extern bool tp_trace; 49 extern enum tp_type tp_state; 50 51 struct tp_msg { 52 const char *msg; 53 }; 54 55 static const struct json_obj_descr tp_msg_dsc[] = { 56 JSON_OBJ_DESCR_PRIM(struct tp_msg, msg, JSON_TOK_STRING) 57 }; 58 59 struct tp { 60 enum tp_type type; 61 const char *msg; 62 const char *status; 63 const char *state; 64 int seq; 65 int ack; 66 const char *rcv; 67 const char *data; 68 const char *op; 69 }; 70 71 #define json_str(_type, _field) \ 72 JSON_OBJ_DESCR_PRIM(struct _type, _field, JSON_TOK_STRING) 73 #define json_num(_type, _field) \ 74 JSON_OBJ_DESCR_PRIM(struct _type, _field, JSON_TOK_NUMBER) 75 76 static const struct json_obj_descr tp_descr[] = { 77 json_str(tp, msg), 78 json_str(tp, status), 79 json_str(tp, state), 80 json_num(tp, seq), 81 json_num(tp, ack), 82 json_str(tp, rcv), 83 json_str(tp, data), 84 json_str(tp, op), 85 }; 86 87 struct tp_entry { 88 const char *key; 89 const char *value; 90 }; 91 92 static const struct json_obj_descr tp_entry_dsc[] = { 93 JSON_OBJ_DESCR_PRIM(struct tp_entry, key, JSON_TOK_STRING), 94 JSON_OBJ_DESCR_PRIM(struct tp_entry, value, JSON_TOK_STRING), 95 }; 96 97 struct tp_new { 98 const char *msg; 99 struct tp_entry data[10]; 100 size_t num_entries; 101 }; 102 103 static const struct json_obj_descr tp_new_dsc[] = { 104 JSON_OBJ_DESCR_PRIM(struct tp_new, msg, JSON_TOK_STRING), 105 JSON_OBJ_DESCR_OBJ_ARRAY(struct tp_new, data, 10, num_entries, 106 tp_entry_dsc, ARRAY_SIZE(tp_entry_dsc)), 107 }; 108 109 enum net_verdict tp_input(struct net_conn *net_conn, 110 struct net_pkt *pkt, 111 union net_ip_header *ip, 112 union net_proto_header *proto, 113 void *user_data); 114 115 char *tp_basename(char *path); 116 const char *tp_hex_to_str(void *data, size_t len); 117 size_t tp_str_to_hex(void *buf, size_t bufsize, const char *s); 118 119 void _tp_output(sa_family_t af, struct net_if *iface, void *data, 120 size_t data_len, const char *file, int line); 121 #define tp_output(_af, _iface, _data, _data_len) \ 122 _tp_output(_af, _iface, _data, _data_len, \ 123 tp_basename(__FILE__), __LINE__) 124 125 void tp_pkt_adj(struct net_pkt *pkt, int req_len); 126 127 enum tp_type tp_msg_to_type(const char *s); 128 129 void *tp_malloc(size_t size, const char *file, int line, const char *func); 130 void tp_free(void *ptr, const char *file, int line, const char *func); 131 void *tp_calloc(size_t nmemb, size_t size, const char *file, int line, 132 const char *func); 133 void tp_mem_stat(void); 134 135 struct net_buf *tp_nbuf_alloc(struct net_buf_pool *pool, size_t len, 136 const char *file, int line, const char *func); 137 struct net_buf *tp_nbuf_clone(struct net_buf *buf, const char *file, int line, 138 const char *func); 139 void tp_nbuf_unref(struct net_buf *nbuf, const char *file, int line, 140 const char *func); 141 void tp_nbuf_stat(void); 142 void tp_pkt_alloc(struct net_pkt *pkt, 143 const char *file, int line); 144 145 struct net_pkt *tp_pkt_clone(struct net_pkt *pkt, const char *file, int line); 146 void tp_pkt_unref(struct net_pkt *pkt, const char *file, int line); 147 void tp_pkt_stat(void); 148 149 uint32_t tp_seq_track(int kind, uint32_t *pvalue, int req, 150 const char *file, int line, const char *func); 151 void tp_seq_stat(void); 152 153 struct tp *json_to_tp(void *data, size_t data_len); 154 enum tp_type json_decode_msg(void *data, size_t data_len); 155 struct tp_new *json_to_tp_new(void *data, size_t data_len); 156 void tp_encode(struct tp *tp, void *data, size_t *data_len); 157 void tp_new_to_json(struct tp_new *tp, void *data, size_t *data_len); 158 void tp_new_find_and_apply(struct tp_new *tp, const char *key, void *value, 159 int type); 160 void tp_out(sa_family_t af, struct net_if *iface, const char *msg, 161 const char *key, const char *value); 162 163 bool tp_tap_input(struct net_pkt *pkt); 164 165 #else /* else of IS_ENABLED(CONFIG_NET_TEST_PROTOCOL) */ 166 167 #define tp_tap_input(_pkt) false 168 #define tp_input(_pkt) false 169 #define tp_out(args...) 170 171 #endif /* end of IS_ENABLED(CONFIG_NET_TEST_PROTOCOL) */ 172 173 #ifdef __cplusplus 174 } 175 #endif 176 177 #endif /* TP_H */ 178