-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcommline.h
217 lines (184 loc) · 6.95 KB
/
commline.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (C) 2017 Rahul Jadhav <[email protected]>
*
* This file is subject to the terms and conditions of the GNU
* General Public License v2. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup commline
* @{
*
* @file
* @brief Exported interfaces for commline
*
* @author Rahul Jadhav <[email protected]>
*
* @}
*/
#ifndef _COMMLINE_H_
#define _COMMLINE_H_
#include <sys/time.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SUCCESS 0
#define FAILURE -1
#define CL_SUCCESS 0
#define CL_FAILURE -1
#define USE_UNIX_SOCKETS
#define COMMLINE_MAX_BUF 2048
#define CL_CREATEQ (1 << 0) //Used by airline
#define CL_ATTACHQ (1 << 1) //Used by stackline
int cl_init(const long my_mtype, const uint8_t flags);
int cl_bind(const long my_mtype);
void cl_cleanup(void);
//msg_buf_t::flags defined
#define MBUF_IS_ACK (1 << 0) //Mbuf is an ACK
#define MBUF_IS_CMD (1 << 1) //Mbuf is a cmd
#define MBUF_DO_NOT_RESPOND (1 << 2) //Cmd does not need a response
//#define MBUF_OUTPUT_JSON (1<<2)
#pragma pack(push, 1)
typedef struct _msg_buf_ {
#ifdef USE_UNIX_SOCKETS
int mtype;
#else
long mtype;
#endif
uint16_t src_id;
uint16_t dst_id;
uint8_t flags;
union {
struct {
//Note that you can have one or both or none of the indicators.
//Value is 0 if not present. NS3 provides LQI only. Castalia provides RSSI.
uint8_t lqi; //Link Quality Indicator
int8_t rssi; //Rcvd Signal Strength Indicator
} sig;
struct {
uint8_t retries;
uint8_t status;
} ack;
} info;
uint16_t len, max_len; // length of the buf only
uint8_t buf[1];
} msg_buf_t;
#pragma pack(pop)
#define DEFINE_MBUF_SZ(MBUF, SZ) \
uint8_t MBUF##_buf[sizeof(msg_buf_t) + SZ]; \
msg_buf_t *MBUF = (msg_buf_t *)MBUF##_buf; \
memset(MBUF##_buf, 0, sizeof(MBUF##_buf)); \
MBUF->max_len = SZ;
#define DEFINE_MBUF(MBUF) DEFINE_MBUF_SZ(MBUF, COMMLINE_MAX_BUF)
#define MAX_CMD_RSP_SZ 4096
#define CL_FLAG_NOWAIT (1 << 1)
int cl_recvfrom_q(const long mtype, msg_buf_t *mbuf, uint16_t len, uint16_t flags);
int cl_sendto_q(const long mtype, msg_buf_t *mbuf, uint16_t len);
int cl_get_descriptor(const long mtype);
enum {
STACKLINE = 1,
AIRLINE,
FORKER,
MONITOR,
MAX_CL_LINE
};
//In case of Openthread stackline, the packet already contains the machdr
//formed. Thus Airline needs to be informed by setting
//mbuf->dst_id=DSTID_MACHDR_PRESENT.
#define CL_DSTID_MACHDR_PRESENT 0xfffe
#define CL_MGR_ID 0xffff
#define MTYPE(LINE, ID) (((LINE) << 16) | (ID))
#define GET_LINE(MT) (MT >> 16)
#define ms2hh (3600000)
#define ms2mm (60000)
#define ms2ss (1000)
#ifndef ERROR
#if BASIC_TS_PRN
#define PRN(STR, ...) \
{ \
struct timeval tv; \
gettimeofday(&tv, NULL); \
printf("%s %5ld:%-4ld[%s:%d] ", STR, \
tv.tv_sec % 100000, tv.tv_usec / 1000, \
__func__, __LINE__); \
printf(__VA_ARGS__); \
}
#else
#define GET_HH_MM_SS_MS(S, E) \
ms = (((E)->tv_sec - (S)->tv_sec) * 1000) + (((E)->tv_usec - (S)->tv_usec) / 1000); \
hh = ms / ms2hh; \
ms = ms % ms2hh; \
mm = ms / ms2mm; \
ms = ms % ms2mm; \
ss = ms / ms2ss; \
ms = ms % ms2ss;
#define PRN(STR, ...) \
{ \
uint32_t hh, mm, ss, ms; \
static struct timeval begin_tv; \
struct timeval tv; \
\
gettimeofday(&tv, NULL); \
if (!begin_tv.tv_sec) \
begin_tv = tv; \
GET_HH_MM_SS_MS(&begin_tv, &tv); \
printf("%s %02u:%02u:%02u.%03u [%s:%d] ", STR, \
hh, mm, ss, ms, __func__, __LINE__); \
printf(__VA_ARGS__); \
}
#endif
#define ERROR(...) \
PRN("ERROR", __VA_ARGS__); \
fflush(NULL)
#define INFO(...) \
PRN("INFO ", __VA_ARGS__); \
fflush(NULL)
#define WARN(...) PRN("WARN ", __VA_ARGS__)
#endif //ERROR
/* MAC DataConfirmation status */
enum {
WF_STATUS_ACK_OK,
WF_STATUS_NO_ACK,
WF_STATUS_ERR,
WF_STATUS_FATAL,
};
#define memcpy_s(DST, DSTLEN, SRC, SRCLEN) \
memcpy(DST, SRC, DSTLEN < SRCLEN ? DSTLEN : SRCLEN);
#define IN_RANGE(VAL, MIN_N, MAX_N) ((VAL) >= (MIN_N) && (VAL) < (MAX_N))
#define HANDLE_CMD(MBUF, CMD) \
else if (!strncasecmp((char *)(MBUF)->buf, #CMD, sizeof(#CMD) - 1)) \
{ \
int aux_len = 0; \
char *colon_ptr = strchr((char *)(MBUF)->buf, ':'); \
if (colon_ptr) { \
*colon_ptr++ = 0; \
aux_len = strlen(colon_ptr); \
memmove((MBUF)->buf, colon_ptr, aux_len); \
} \
(MBUF)->buf[aux_len] = 0; \
(MBUF)->len = CMD(mbuf->src_id, (char *)(MBUF)->buf, COMMLINE_MAX_BUF); \
}
#define PRINT_HEX(BUF, LEN, ...) \
{ \
int i; \
printf(__VA_ARGS__); \
for (i = 0; i < LEN; i++) { \
if (i && !(i % 16)) \
printf("\n"); \
else if (i && !(i % 8)) \
printf("\t"); \
printf("%02x ", (uint8_t)BUF[i]); \
} \
printf("\n"); \
}
#define CLOSE(FD) \
if (FD >= 0) { \
close(FD); \
FD = -1; \
}
// Stackline Helpers
#include "cl_stackline_helpers.h"
#ifdef __cplusplus
}
#endif
#endif //_COMMLINE_H_