MRTKLIB 0.7.14
Modernized GNSS Positioning Library
Loading...
Searching...
No Matches
mrtk_stream.h
Go to the documentation of this file.
1/*------------------------------------------------------------------------------
2 * mrtk_stream.h : stream I/O type definitions and functions
3 *
4 * Copyright (C) 2026 H.SHIONO (MRTKLIB Project)
5 * Copyright (C) 2023-2025 Cabinet Office, Japan
6 * Copyright (C) 2024-2025 Lighthouse Technology & Consulting Co. Ltd.
7 * Copyright (C) 2023-2025 Japan Aerospace Exploration Agency
8 * Copyright (C) 2023-2025 TOSHIBA ELECTRONIC TECHNOLOGIES CORPORATION
9 * Copyright (C) 2015- Mitsubishi Electric Corp.
10 * Copyright (C) 2014 Geospatial Information Authority of Japan
11 * Copyright (C) 2014 T.SUZUKI
12 * Copyright (C) 2007-2023 T.TAKASU
13 *
14 * SPDX-License-Identifier: BSD-2-Clause
15 *----------------------------------------------------------------------------*/
16/**
17 * @file mrtk_stream.h
18 * @brief MRTKLIB Stream Module — stream I/O abstraction layer.
19 *
20 * This header provides the stream_t type and related types (strconv_t,
21 * strsvr_t) for managing various I/O streams (file, serial, TCP, NTRIP,
22 * UDP, FTP/HTTP, memory buffer).
23 *
24 * @note Functions declared here are implemented in mrtk_stream.c.
25 */
26#ifndef MRTK_STREAM_H
27#define MRTK_STREAM_H
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
34#include "mrtklib/mrtk_rcvraw.h"
35#include "mrtklib/mrtk_rtcm.h"
36#include "mrtklib/mrtk_sol.h"
37#include "mrtklib/mrtk_time.h"
38
39/*============================================================================
40 * Stream Type Constants
41 *===========================================================================*/
42
43#define STR_NONE 0 /* stream type: none */
44#define STR_SERIAL 1 /* stream type: serial */
45#define STR_FILE 2 /* stream type: file */
46#define STR_TCPSVR 3 /* stream type: TCP server */
47#define STR_TCPCLI 4 /* stream type: TCP client */
48#define STR_NTRIPSVR 5 /* stream type: NTRIP server */
49#define STR_NTRIPCLI 6 /* stream type: NTRIP client */
50#define STR_FTP 7 /* stream type: ftp */
51#define STR_HTTP 8 /* stream type: http */
52#define STR_NTRIPCAS 9 /* stream type: NTRIP caster */
53#define STR_UDPSVR 10 /* stream type: UDP server */
54#define STR_UDPCLI 11 /* stream type: UDP client */
55#define STR_MEMBUF 12 /* stream type: memory buffer */
56
57#define MAXRCVFMT 12 /* max number of receiver format */
58
59#define STR_MODE_R 0x1 /* stream mode: read */
60#define STR_MODE_W 0x2 /* stream mode: write */
61#define STR_MODE_RW 0x3 /* stream mode: read/write */
62
63#define MSG_DISCONN "$_DISCONNECT\r\n" /* disconnect message */
64
65/*============================================================================
66 * Stream Types
67 *===========================================================================*/
68
69typedef struct stream_tag { /* stream type */
70 int type; /* type (STR_???) */
71 int mode; /* mode (STR_MODE_?) */
72 int state; /* state (-1:error,0:close,1:open) */
73 uint64_t inb; /* input bytes */
74 uint32_t inr; /* input rate (bps) */
75 uint64_t outb; /* output bytes */
76 uint32_t outr; /* output rate (bps) */
77 uint32_t tick_i; /* input tick tick */
78 uint32_t tick_o; /* output tick */
79 uint32_t tact; /* active tick */
80 uint64_t inbt, outbt; /* input/output bytes at tick */
81 rtk_lock_t lock; /* lock flag */
82 void* port; /* type dependent port control struct */
83 char path[MAXSTRPATH]; /* stream path */
84 char msg[MAXSTRMSG]; /* stream message */
86
87typedef struct { /* stream converter type */
88 int itype, otype; /* input and output stream type */
89 int nmsg; /* number of output messages */
90 int msgs[32]; /* output message types */
91 double tint[32]; /* output message intervals (s) */
92 uint32_t tick[32]; /* cycle tick of output message */
93 int ephsat[32]; /* satellites of output ephemeris */
94 int stasel; /* station info selection (0:remote,1:local) */
95 rtcm_t rtcm; /* rtcm input data buffer */
96 raw_t raw; /* raw input data buffer */
97 rtcm_t out; /* rtcm output data buffer */
98} strconv_t;
99
100typedef struct { /* stream server type */
101 int state; /* server state (0:stop,1:running) */
102 int cycle; /* server cycle (ms) */
103 int buffsize; /* input/monitor buffer size (bytes) */
104 int nmeacycle; /* NMEA request cycle (ms) (0:no) */
105 int relayback; /* relay back of output streams (0:no) */
106 int nstr; /* number of streams (1 input + (nstr-1) outputs */
107 int npb; /* data length in peek buffer (bytes) */
108 char cmds_periodic[16][MAXRCVCMD]; /* periodic commands */
109 double nmeapos[3]; /* NMEA request position (ecef) (m) */
110 uint8_t* buff; /* input buffers */
111 uint8_t* pbuf; /* peek buffer */
112 uint32_t tick; /* start tick */
113 stream_t stream[16]; /* input/output streams */
114 stream_t strlog[16]; /* return log streams */
115 strconv_t* conv[16]; /* stream converter */
116 rtk_thread_t thread; /* server thread */
117 rtk_lock_t lock; /* lock flag */
118} strsvr_t;
119
120/*============================================================================
121 * Global Variables
122 *===========================================================================*/
123
124extern const char* formatstrs[]; /* stream format strings */
125
126/*============================================================================
127 * Stream Functions
128 *===========================================================================*/
129
130/**
131 * @brief Initialize stream communication system.
132 */
133void strinitcom(void);
134
135/**
136 * @brief Initialize stream structure.
137 * @param[in,out] stream stream struct
138 */
139void strinit(stream_t* stream);
140
141/**
142 * @brief Lock stream.
143 * @param[in,out] stream stream struct
144 */
145void strlock(stream_t* stream);
146
147/**
148 * @brief Unlock stream.
149 * @param[in,out] stream stream struct
150 */
151void strunlock(stream_t* stream);
152
153/**
154 * @brief Open stream.
155 * @param[in,out] stream stream struct
156 * @param[in] type stream type (STR_???)
157 * @param[in] mode stream mode (STR_MODE_?)
158 * @param[in] path stream path
159 * @return status (0:error,1:ok)
160 */
161int stropen(stream_t* stream, int type, int mode, const char* path);
162
163/**
164 * @brief Close stream.
165 * @param[in,out] stream stream struct
166 */
167void strclose(stream_t* stream);
168
169/**
170 * @brief Read data from stream.
171 * @param[in,out] stream stream struct
172 * @param[out] buff data buffer
173 * @param[in] n max data length (bytes)
174 * @return data length (bytes)
175 */
176int strread(stream_t* stream, uint8_t* buff, int n);
177
178/**
179 * @brief Write data to stream.
180 * @param[in,out] stream stream struct
181 * @param[in] buff data buffer
182 * @param[in] n data length (bytes)
183 * @return bytes written (0: nothing written or error; some stream types
184 * return -1 on error)
185 */
186int strwrite(stream_t* stream, uint8_t* buff, int n);
187
188/**
189 * @brief Sync streams.
190 * @param[in,out] stream1 stream struct 1
191 * @param[in,out] stream2 stream struct 2
192 */
193void strsync(stream_t* stream1, stream_t* stream2);
194
195/**
196 * @brief Get stream status.
197 * @param[in] stream stream struct
198 * @param[out] msg status message (NULL: no output)
199 * @return stream status (-1:error,0:close,1:wait,2:connect,3:active)
200 */
201int strstat(stream_t* stream, char* msg);
202
203/**
204 * @brief Get stream extended status.
205 * @param[in] stream stream struct
206 * @param[out] msg status message (NULL: no output)
207 * @return stream status
208 */
209int strstatx(stream_t* stream, char* msg);
210
211/**
212 * @brief Get stream statistics summary.
213 * @param[in] stream stream struct
214 * @param[out] inb input bytes (NULL: not output)
215 * @param[out] inr input rate (bps) (NULL: not output)
216 * @param[out] outb output bytes (NULL: not output)
217 * @param[out] outr output rate (bps) (NULL: not output)
218 */
219void strsum(stream_t* stream, uint64_t* inb, int* inr, uint64_t* outb, int* outr);
220
221/**
222 * @brief Set stream options.
223 * @param[in] opt stream options (timeout/reconnect/rate/buffsize/fswapmargin)
224 */
225void strsetopt(const int* opt);
226
227/**
228 * @brief Get stream time.
229 * @param[in] stream stream struct
230 * @return stream time
231 */
233
234/**
235 * @brief Send NMEA request to stream.
236 * @param[in,out] stream stream struct
237 * @param[in] sol solution data
238 */
239void strsendnmea(stream_t* stream, const sol_t* sol);
240
241/**
242 * @brief Send receiver command to stream.
243 * @param[in,out] stream stream struct
244 * @param[in] cmd receiver command
245 */
246void strsendcmd(stream_t* stream, const char* cmd);
247
248/**
249 * @brief Set stream timeout.
250 * @param[in,out] stream stream struct
251 * @param[in] toinact inactive timeout (ms) (0:no timeout)
252 * @param[in] tirecon reconnect interval (ms)
253 */
254void strsettimeout(stream_t* stream, int toinact, int tirecon);
255
256/**
257 * @brief Set FTP/HTTP local directory.
258 * @param[in] dir local directory
259 */
260void strsetdir(const char* dir);
261
262/**
263 * @brief Set HTTP/NTRIP proxy address.
264 * @param[in] addr proxy address
265 */
266void strsetproxy(const char* addr);
267
268/**
269 * @brief Set default NTRIP version for new connections.
270 * @param[in] ver NTRIP version (0:auto, 1:v1, 2:v2)
271 *
272 * When set to auto (0), the client tries NTRIP v2 first and falls back to v1
273 * if the caster does not support it. Per-stream override is available via
274 * the ?ver=N query parameter in the stream path.
275 */
276void strsetntripver(int ver);
277
278/*============================================================================
279 * Stream Server Functions
280 *===========================================================================*/
281
282void strsvrinit(strsvr_t* svr, int nout);
283int strsvrstart(strsvr_t* svr, int* opts, int* strs, char** paths, char** logs, strconv_t** conv, char** cmds,
284 char** cmds_priodic, const double* nmeapos);
285void strsvrstop(strsvr_t* svr, char** cmds);
286/**
287 * @brief Get stream server status.
288 * @param[in] svr stream server
289 * @param[out] stat status of each stream [svr->nstr]
290 * @param[out] log_stat status of each stream log [svr->nstr]
291 * @param[out] byte bytes received (stream 0) / sent (others) [svr->nstr]
292 * @param[out] bps bit rate received / sent [svr->nstr]
293 * @param[out] msg status messages
294 */
295void strsvrstat(strsvr_t* svr, int* stat, int* log_stat, uint64_t* byte, int* bps, char* msg);
296strconv_t* strconvnew(int itype, int otype, const char* msgs, int staid, int stasel, const char* opt);
297void strconvfree(strconv_t* conv);
298
299#ifdef __cplusplus
300}
301#endif
302
303#endif /* MRTK_STREAM_H */
MRTKLIB Foundation — Common types, standard includes, and system constants (MAXSAT,...
#define MAXSTRPATH
#define rtk_thread_t
#define MAXRCVCMD
#define MAXSTRMSG
#define rtk_lock_t
MRTKLIB Receiver Raw Data Module — Receiver raw data types and decoder functions for various GNSS rec...
MRTKLIB RTCM Module — RTCM message types and functions.
MRTKLIB Solution Module — Solution data types and I/O functions.
const char * formatstrs[32]
Definition mrtk_stream.c:80
static int toinact
void strinit(stream_t *stream)
Initialize stream structure.
void strsvrinit(strsvr_t *svr, int nout)
int strstat(stream_t *stream, char *msg)
Get stream status.
gtime_t strgettime(stream_t *stream)
Get stream time.
void strsvrstat(strsvr_t *svr, int *stat, int *log_stat, uint64_t *byte, int *bps, char *msg)
Get stream server status.
void strsettimeout(stream_t *stream, int toinact, int tirecon)
Set stream timeout.
int strstatx(stream_t *stream, char *msg)
Get stream extended status.
int strread(stream_t *stream, uint8_t *buff, int n)
Read data from stream.
void strsendnmea(stream_t *stream, const sol_t *sol)
Send NMEA request to stream.
void strsvrstop(strsvr_t *svr, char **cmds)
strconv_t * strconvnew(int itype, int otype, const char *msgs, int staid, int stasel, const char *opt)
void strsetopt(const int *opt)
Set stream options.
void strlock(stream_t *stream)
Lock stream.
void strsetdir(const char *dir)
Set FTP/HTTP local directory.
void strsendcmd(stream_t *stream, const char *cmd)
Send receiver command to stream.
void strinitcom(void)
Initialize stream communication system.
void strconvfree(strconv_t *conv)
struct stream_tag stream_t
int strsvrstart(strsvr_t *svr, int *opts, int *strs, char **paths, char **logs, strconv_t **conv, char **cmds, char **cmds_priodic, const double *nmeapos)
void strclose(stream_t *stream)
Close stream.
int strwrite(stream_t *stream, uint8_t *buff, int n)
Write data to stream.
void strsetntripver(int ver)
Set default NTRIP version for new connections.
void strsetproxy(const char *addr)
Set HTTP/NTRIP proxy address.
int stropen(stream_t *stream, int type, int mode, const char *path)
Open stream.
void strsync(stream_t *stream1, stream_t *stream2)
Sync streams.
void strunlock(stream_t *stream)
Unlock stream.
void strsum(stream_t *stream, uint64_t *inb, int *inr, uint64_t *outb, int *outr)
Get stream statistics summary.
MRTKLIB Time Module — GNSS time types and conversion functions.
GNSS time representation.
Definition mrtk_time.h:49
Receiver raw data control type.
Definition mrtk_rcvraw.h:78
RTCM control struct type.
Definition mrtk_rtcm.h:50
uint32_t tick[32]
Definition mrtk_stream.h:92
int ephsat[32]
Definition mrtk_stream.h:93
int msgs[32]
Definition mrtk_stream.h:90
double tint[32]
Definition mrtk_stream.h:91
rtcm_t rtcm
Definition mrtk_stream.h:95
rtcm_t out
Definition mrtk_stream.h:97
raw_t raw
Definition mrtk_stream.h:96
char msg[MAXSTRMSG]
Definition mrtk_stream.h:84
void * port
Definition mrtk_stream.h:82
uint64_t outb
Definition mrtk_stream.h:75
uint32_t inr
Definition mrtk_stream.h:74
uint64_t inbt
Definition mrtk_stream.h:80
uint32_t outr
Definition mrtk_stream.h:76
uint32_t tick_i
Definition mrtk_stream.h:77
uint64_t inb
Definition mrtk_stream.h:73
rtk_lock_t lock
Definition mrtk_stream.h:81
char path[MAXSTRPATH]
Definition mrtk_stream.h:83
uint32_t tick_o
Definition mrtk_stream.h:78
uint32_t tact
Definition mrtk_stream.h:79
uint64_t outbt
Definition mrtk_stream.h:80
double nmeapos[3]
strconv_t * conv[16]
uint32_t tick
stream_t stream[16]
rtk_thread_t thread
stream_t strlog[16]
uint8_t * pbuf
int relayback
char cmds_periodic[16][MAXRCVCMD]
uint8_t * buff
int nmeacycle
rtk_lock_t lock