ctp_rust/
ffi.rs

1//! FFI绑定模块
2//!
3//! 直接与CTP C++库的底层接口绑定
4
5use std::os::raw::{c_char, c_int, c_void};
6
7// Debug日志配置结构体
8#[repr(C)]
9pub struct CtpLogConfig {
10    pub enable_debug: c_int,           // 0=关闭, 1=开启
11    pub log_file_path: *const c_char,  // 日志文件路径,NULL=控制台输出
12    pub max_file_size_mb: c_int,       // 最大文件大小(MB)- 预留功能
13    pub max_backup_files: c_int,       // 最大备份文件数 - 预留功能
14}
15
16// SPI回调结构体
17#[repr(C)]
18pub struct MdSpiCallbacks {
19    pub user_data: *mut c_void,
20    pub on_front_connected: Option<extern "C" fn(*mut c_void)>,
21    pub on_front_disconnected: Option<extern "C" fn(*mut c_void, c_int)>,
22    pub on_heart_beat_warning: Option<extern "C" fn(*mut c_void, c_int)>,
23    pub on_rsp_user_login:
24        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
25    pub on_rsp_user_logout:
26        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
27    pub on_rsp_error: Option<extern "C" fn(*mut c_void, *mut c_void, c_int, c_int)>,
28    pub on_rsp_sub_market_data:
29        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
30    pub on_rsp_unsub_market_data:
31        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
32    pub on_rtn_depth_market_data: Option<extern "C" fn(*mut c_void, *mut c_void)>,
33    pub on_rtn_for_quote_rsp: Option<extern "C" fn(*mut c_void, *mut c_void)>,
34}
35
36// 交易SPI回调结构体
37#[repr(C)]
38pub struct TraderSpiCallbacks {
39    pub user_data: *mut c_void,
40    pub on_front_connected: Option<extern "C" fn(*mut c_void)>,
41    pub on_front_disconnected: Option<extern "C" fn(*mut c_void, c_int)>,
42    pub on_heart_beat_warning: Option<extern "C" fn(*mut c_void, c_int)>,
43    pub on_rsp_authenticate:
44        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
45    pub on_rsp_user_login:
46        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
47    pub on_rsp_user_logout:
48        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
49    pub on_rsp_error: Option<extern "C" fn(*mut c_void, *mut c_void, c_int, c_int)>,
50    pub on_rsp_order_insert:
51        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
52    pub on_rsp_order_action:
53        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
54    pub on_rtn_order: Option<extern "C" fn(*mut c_void, *mut c_void)>,
55    pub on_rtn_trade: Option<extern "C" fn(*mut c_void, *mut c_void)>,
56    pub on_rsp_qry_trading_account:
57        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
58    pub on_rsp_qry_investor_position:
59        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
60
61    // 第一阶段新增回调
62    pub on_err_rtn_order_insert: Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void)>,
63    pub on_err_rtn_order_action: Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void)>,
64    pub on_rsp_qry_order:
65        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
66    pub on_rsp_qry_trade:
67        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
68    pub on_rsp_qry_instrument:
69        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
70
71    // 第二阶段新增回调
72    pub on_rsp_qry_instrument_margin_rate:
73        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
74    pub on_rsp_qry_instrument_commission_rate:
75        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
76    pub on_rsp_qry_exchange:
77        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
78    pub on_rsp_qry_product:
79        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
80    pub on_rsp_settlement_info_confirm:
81        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
82    pub on_rsp_parked_order_insert:
83        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
84    pub on_rsp_parked_order_action:
85        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
86
87    // 第三阶段新增回调
88    pub on_rsp_exec_order_insert:
89        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
90    pub on_rsp_exec_order_action:
91        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
92    pub on_rsp_for_quote_insert:
93        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
94    pub on_rsp_quote_insert:
95        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
96    pub on_rsp_quote_action:
97        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
98    pub on_rsp_batch_order_action:
99        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
100    pub on_rsp_remove_parked_order:
101        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
102    pub on_rsp_remove_parked_order_action:
103        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
104    pub on_rsp_qry_max_order_volume:
105        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
106    pub on_rsp_qry_depth_market_data:
107        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
108    pub on_rsp_qry_settlement_info:
109        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
110    pub on_rsp_qry_transfer_bank:
111        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
112    pub on_rsp_qry_investor_position_detail:
113        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
114    pub on_rsp_qry_notice:
115        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
116}
117
118// SPI桥接函数
119#[link(name = "ctp_wrapper")]
120extern "C" {
121    pub fn CreateMdSpiBridge(callbacks: *const MdSpiCallbacks) -> *mut c_void;
122    pub fn DestroyMdSpiBridge(spi_bridge: *mut c_void);
123    pub fn CreateTraderSpiBridge(callbacks: *const TraderSpiCallbacks) -> *mut c_void;
124    pub fn DestroyTraderSpiBridge(spi_bridge: *mut c_void);
125    
126    // Debug日志接口
127    pub fn CTP_InitializeDebugLogging(config: *const CtpLogConfig);
128    pub fn CTP_CleanupDebugLogging();
129}
130
131// CTP行情API的FFI声明
132pub mod md_api {
133    use super::*;
134
135    // 创建行情API实例
136    //
137    // # 参数
138    // * `flow_path` - 存储流文件的目录,默认为当前目录
139    // * `is_using_udp` - 是否使用UDP协议接收多播数据
140    // * `is_multicast` - 是否使用组播方式
141    //
142    // # 返回值
143    // 行情API实例指针
144    #[link(name = "ctp_wrapper")]
145    extern "C" {
146        pub fn CThostFtdcMdApi_CreateFtdcMdApi(
147            flow_path: *const c_char,
148            is_using_udp: bool,
149            is_multicast: bool,
150            is_production_mode: bool,
151        ) -> *mut c_void;
152    }
153
154    // 获取API版本号
155    //
156    // # 返回值
157    // 版本号字符串
158    #[link(name = "ctp_wrapper")]
159    extern "C" {
160        pub fn CThostFtdcMdApi_GetApiVersion() -> *const c_char;
161    }
162
163    // 释放API实例
164    //
165    // # 参数
166    // * `api` - API实例指针
167    #[link(name = "ctp_wrapper")]
168    extern "C" {
169        pub fn CThostFtdcMdApi_Release(api: *mut c_void);
170    }
171
172    // 初始化API
173    //
174    // # 参数
175    // * `api` - API实例指针
176    #[link(name = "ctp_wrapper")]
177    extern "C" {
178        pub fn CThostFtdcMdApi_Init(api: *mut c_void);
179    }
180
181    // 等待API线程结束运行
182    //
183    // # 参数
184    // * `api` - API实例指针
185    //
186    // # 返回值
187    // 线程退出代码
188    #[link(name = "ctp_wrapper")]
189    extern "C" {
190        pub fn CThostFtdcMdApi_Join(api: *mut c_void) -> c_int;
191    }
192
193    // 获取当前交易日
194    //
195    // # 参数
196    // * `api` - API实例指针
197    //
198    // # 返回值
199    // 交易日字符串(YYYYMMDD格式)
200    #[link(name = "ctp_wrapper")]
201    extern "C" {
202        pub fn CThostFtdcMdApi_GetTradingDay(api: *mut c_void) -> *const c_char;
203    }
204
205    // 注册前置机网络地址
206    //
207    // # 参数
208    // * `api` - API实例指针
209    // * `front_address` - 前置机网络地址,格式为:"protocol://ipaddress:port",
210    //                     如:"tcp://127.0.0.1:17001"
211    #[link(name = "ctp_wrapper")]
212    extern "C" {
213        pub fn CThostFtdcMdApi_RegisterFront(api: *mut c_void, front_address: *const c_char);
214    }
215
216    // 注册名字服务器网络地址
217    //
218    // # 参数
219    // * `api` - API实例指针
220    // * `ns_address` - 名字服务器网络地址,格式同前置机地址
221    #[link(name = "ctp_wrapper")]
222    extern "C" {
223        pub fn CThostFtdcMdApi_RegisterNameServer(api: *mut c_void, ns_address: *const c_char);
224    }
225
226    // 注册名字服务器用户信息
227    //
228    // # 参数
229    // * `api` - API实例指针
230    // * `user_info` - 用户信息
231    #[link(name = "ctp_wrapper")]
232    extern "C" {
233        pub fn CThostFtdcMdApi_RegisterFensUserInfo(api: *mut c_void, user_info: *const c_void);
234    }
235
236    // 注册回调接口
237    //
238    // # 参数
239    // * `api` - API实例指针
240    // * `spi` - 派生自CThostFtdcMdSpi的实例指针
241    #[link(name = "ctp_wrapper")]
242    extern "C" {
243        pub fn CThostFtdcMdApi_RegisterSpi(api: *mut c_void, spi: *mut c_void);
244    }
245
246    // 用户登录请求
247    //
248    // # 参数
249    // * `api` - API实例指针
250    // * `req` - 登录请求
251    // * `request_id` - 请求ID
252    //
253    // # 返回值
254    // 0表示成功,非0表示失败
255    #[link(name = "ctp_wrapper")]
256    extern "C" {
257        pub fn CThostFtdcMdApi_ReqUserLogin(
258            api: *mut c_void,
259            req: *const c_void,
260            request_id: c_int,
261        ) -> c_int;
262    }
263
264    // 登出请求
265    //
266    // # 参数
267    // * `api` - API实例指针
268    // * `req` - 登出请求
269    // * `request_id` - 请求ID
270    //
271    // # 返回值
272    // 0表示成功,非0表示失败
273    #[link(name = "ctp_wrapper")]
274    extern "C" {
275        pub fn CThostFtdcMdApi_ReqUserLogout(
276            api: *mut c_void,
277            req: *const c_void,
278            request_id: c_int,
279        ) -> c_int;
280    }
281
282    // 订阅行情
283    //
284    // # 参数
285    // * `api` - API实例指针
286    // * `instrument_ids` - 合约ID数组
287    // * `count` - 合约个数
288    //
289    // # 返回值
290    // 0表示成功,非0表示失败
291    #[link(name = "ctp_wrapper")]
292    extern "C" {
293        pub fn CThostFtdcMdApi_SubscribeMarketData(
294            api: *mut c_void,
295            instrument_ids: *const *const c_char,
296            count: c_int,
297        ) -> c_int;
298    }
299
300    // 退订行情
301    //
302    // # 参数
303    // * `api` - API实例指针
304    // * `instrument_ids` - 合约ID数组
305    // * `count` - 合约个数
306    //
307    // # 返回值
308    // 0表示成功,非0表示失败
309    #[link(name = "ctp_wrapper")]
310    extern "C" {
311        pub fn CThostFtdcMdApi_UnSubscribeMarketData(
312            api: *mut c_void,
313            instrument_ids: *const *const c_char,
314            count: c_int,
315        ) -> c_int;
316    }
317
318    // 订阅询价
319    //
320    // # 参数
321    // * `api` - API实例指针
322    // * `instrument_ids` - 合约ID数组
323    // * `count` - 合约个数
324    //
325    // # 返回值
326    // 0表示成功,非0表示失败
327    #[link(name = "ctp_wrapper")]
328    extern "C" {
329        pub fn CThostFtdcMdApi_SubscribeForQuoteRsp(
330            api: *mut c_void,
331            instrument_ids: *const *const c_char,
332            count: c_int,
333        ) -> c_int;
334    }
335
336    // 退订询价
337    //
338    // # 参数
339    // * `api` - API实例指针
340    // * `instrument_ids` - 合约ID数组
341    // * `count` - 合约个数
342    //
343    // # 返回值
344    // 0表示成功,非0表示失败
345    #[link(name = "ctp_wrapper")]
346    extern "C" {
347        pub fn CThostFtdcMdApi_UnSubscribeForQuoteRsp(
348            api: *mut c_void,
349            instrument_ids: *const *const c_char,
350            count: c_int,
351        ) -> c_int;
352    }
353}
354
355// CTP交易API的FFI声明
356pub mod trader_api {
357    use super::*;
358
359    // 创建交易API实例
360    //
361    // # 参数
362    // * `flow_path` - 存储流文件的目录,默认为当前目录
363    //
364    // # 返回值
365    // 交易API实例指针
366    #[link(name = "ctp_wrapper")]
367    extern "C" {
368        pub fn CThostFtdcTraderApi_CreateFtdcTraderApi(
369            flow_path: *const c_char,
370            is_production_mode: bool,
371        ) -> *mut c_void;
372    }
373
374    // 获取API版本号
375    //
376    // # 返回值
377    // 版本号字符串
378    #[link(name = "ctp_wrapper")]
379    extern "C" {
380        pub fn CThostFtdcTraderApi_GetApiVersion() -> *const c_char;
381    }
382
383    // 释放API实例
384    //
385    // # 参数
386    // * `api` - API实例指针
387    #[link(name = "ctp_wrapper")]
388    extern "C" {
389        pub fn CThostFtdcTraderApi_Release(api: *mut c_void);
390    }
391
392    // 初始化API
393    //
394    // # 参数
395    // * `api` - API实例指针
396    #[link(name = "ctp_wrapper")]
397    extern "C" {
398        pub fn CThostFtdcTraderApi_Init(api: *mut c_void);
399    }
400
401    // 等待API线程结束运行
402    //
403    // # 参数
404    // * `api` - API实例指针
405    //
406    // # 返回值
407    // 线程退出代码
408    #[link(name = "ctp_wrapper")]
409    extern "C" {
410        pub fn CThostFtdcTraderApi_Join(api: *mut c_void) -> c_int;
411    }
412
413    // 获取当前交易日
414    //
415    // # 参数
416    // * `api` - API实例指针
417    //
418    // # 返回值
419    // 交易日字符串(YYYYMMDD格式)
420    #[link(name = "ctp_wrapper")]
421    extern "C" {
422        pub fn CThostFtdcTraderApi_GetTradingDay(api: *mut c_void) -> *const c_char;
423    }
424
425    // 注册前置机网络地址
426    //
427    // # 参数
428    // * `api` - API实例指针
429    // * `front_address` - 前置机网络地址
430    #[link(name = "ctp_wrapper")]
431    extern "C" {
432        pub fn CThostFtdcTraderApi_RegisterFront(api: *mut c_void, front_address: *const c_char);
433    }
434
435    // 注册名字服务器网络地址
436    //
437    // # 参数
438    // * `api` - API实例指针
439    // * `ns_address` - 名字服务器网络地址
440    #[link(name = "ctp_wrapper")]
441    extern "C" {
442        pub fn CThostFtdcTraderApi_RegisterNameServer(api: *mut c_void, ns_address: *const c_char);
443    }
444
445    // 注册名字服务器用户信息
446    //
447    // # 参数
448    // * `api` - API实例指针
449    // * `user_info` - 用户信息
450    #[link(name = "ctp_wrapper")]
451    extern "C" {
452        pub fn CThostFtdcTraderApi_RegisterFensUserInfo(api: *mut c_void, user_info: *const c_void);
453    }
454
455    // 注册回调接口
456    //
457    // # 参数
458    // * `api` - API实例指针
459    // * `spi` - 派生自CThostFtdcTraderSpi的实例指针
460    #[link(name = "ctp_wrapper")]
461    extern "C" {
462        pub fn CThostFtdcTraderApi_RegisterSpi(api: *mut c_void, spi: *mut c_void);
463    }
464
465    // 获取已连接的前置信息
466    //
467    // # 参数
468    // * `api` - API实例指针
469    // * `front_info` - 前置信息结构体指针
470    #[link(name = "ctp_wrapper")]
471    extern "C" {
472        pub fn CThostFtdcTraderApi_GetFrontInfo(api: *mut c_void, front_info: *mut c_void);
473    }
474
475    // 注册微信用户系统信息
476    //
477    // # 参数
478    // * `api` - API实例指针
479    // * `user_system_info` - 微信用户系统信息
480    //
481    // # 返回值
482    // 0表示成功,非0表示失败
483    #[link(name = "ctp_wrapper")]
484    extern "C" {
485        pub fn CThostFtdcTraderApi_RegisterWechatUserSystemInfo(
486            api: *mut c_void,
487            user_system_info: *mut c_void,
488        ) -> c_int;
489    }
490
491    // 上报微信用户系统信息
492    //
493    // # 参数
494    // * `api` - API实例指针
495    // * `user_system_info` - 微信用户系统信息
496    //
497    // # 返回值
498    // 0表示成功,非0表示失败
499    #[link(name = "ctp_wrapper")]
500    extern "C" {
501        pub fn CThostFtdcTraderApi_SubmitWechatUserSystemInfo(
502            api: *mut c_void,
503            user_system_info: *mut c_void,
504        ) -> c_int;
505    }
506
507    // 客户端认证请求
508    //
509    // # 参数
510    // * `api` - API实例指针
511    // * `req` - 认证请求
512    // * `request_id` - 请求ID
513    //
514    // # 返回值
515    // 0表示成功,非0表示失败
516    #[link(name = "ctp_wrapper")]
517    extern "C" {
518        pub fn CThostFtdcTraderApi_ReqAuthenticate(
519            api: *mut c_void,
520            req: *const c_void,
521            request_id: c_int,
522        ) -> c_int;
523    }
524
525    // 注册用户终端信息,用于中继服务器多连接模式
526    //
527    // # 参数
528    // * `api` - API实例指针
529    // * `user_info` - 用户终端信息
530    //
531    // # 返回值
532    // 0表示成功,非0表示失败
533    #[link(name = "ctp_wrapper")]
534    extern "C" {
535        pub fn CThostFtdcTraderApi_RegisterUserSystemInfo(
536            api: *mut c_void,
537            user_info: *const c_void,
538        ) -> c_int;
539    }
540
541    // 上报用户终端信息,用于中继服务器操作员登录模式
542    //
543    // # 参数
544    // * `api` - API实例指针
545    // * `user_info` - 用户终端信息
546    //
547    // # 返回值
548    // 0表示成功,非0表示失败
549    #[link(name = "ctp_wrapper")]
550    extern "C" {
551        pub fn CThostFtdcTraderApi_SubmitUserSystemInfo(
552            api: *mut c_void,
553            user_info: *const c_void,
554        ) -> c_int;
555    }
556
557    // 用户登录请求
558    //
559    // # 参数
560    // * `api` - API实例指针
561    // * `req` - 登录请求
562    // * `request_id` - 请求ID
563    //
564    // # 返回值
565    // 0表示成功,非0表示失败
566    #[link(name = "ctp_wrapper")]
567    extern "C" {
568        pub fn CThostFtdcTraderApi_ReqUserLogin(
569            api: *mut c_void,
570            req: *const c_void,
571            request_id: c_int,
572        ) -> c_int;
573    }
574
575    // 登出请求
576    //
577    // # 参数
578    // * `api` - API实例指针
579    // * `req` - 登出请求
580    // * `request_id` - 请求ID
581    //
582    // # 返回值
583    // 0表示成功,非0表示失败
584    #[link(name = "ctp_wrapper")]
585    extern "C" {
586        pub fn CThostFtdcTraderApi_ReqUserLogout(
587            api: *mut c_void,
588            req: *const c_void,
589            request_id: c_int,
590        ) -> c_int;
591    }
592
593    // 请求查询资金账户
594    //
595    // # 参数
596    // * `api` - API实例指针
597    // * `req` - 查询资金账户请求字段
598    // * `request_id` - 请求ID
599    //
600    // # 返回值
601    // 0表示成功,非0表示失败
602    #[link(name = "ctp_wrapper")]
603    extern "C" {
604        pub fn CThostFtdcTraderApi_ReqQryTradingAccount(
605            api: *mut c_void,
606            req: *const c_void,
607            request_id: c_int,
608        ) -> c_int;
609    }
610
611    // 请求查询投资者持仓
612    //
613    // # 参数
614    // * `api` - API实例指针
615    // * `req` - 查询投资者持仓请求字段
616    // * `request_id` - 请求ID
617    //
618    // # 返回值
619    // 0表示成功,非0表示失败
620    #[link(name = "ctp_wrapper")]
621    extern "C" {
622        pub fn CThostFtdcTraderApi_ReqQryInvestorPosition(
623            api: *mut c_void,
624            req: *const c_void,
625            request_id: c_int,
626        ) -> c_int;
627    }
628
629    // 第一阶段新增API方法
630
631    // 报单录入请求
632    //
633    // # 参数
634    // * `api` - API实例指针
635    // * `req` - 报单录入请求字段
636    // * `request_id` - 请求ID
637    //
638    // # 返回值
639    // 0表示成功,非0表示失败
640    #[link(name = "ctp_wrapper")]
641    extern "C" {
642        pub fn CThostFtdcTraderApi_ReqOrderInsert(
643            api: *mut c_void,
644            req: *const c_void,
645            request_id: c_int,
646        ) -> c_int;
647    }
648
649    // 报单操作请求
650    //
651    // # 参数
652    // * `api` - API实例指针
653    // * `req` - 报单操作请求字段
654    // * `request_id` - 请求ID
655    //
656    // # 返回值
657    // 0表示成功,非0表示失败
658    #[link(name = "ctp_wrapper")]
659    extern "C" {
660        pub fn CThostFtdcTraderApi_ReqOrderAction(
661            api: *mut c_void,
662            req: *const c_void,
663            request_id: c_int,
664        ) -> c_int;
665    }
666
667    // 请求查询报单
668    //
669    // # 参数
670    // * `api` - API实例指针
671    // * `req` - 查询报单请求字段
672    // * `request_id` - 请求ID
673    //
674    // # 返回值
675    // 0表示成功,非0表示失败
676    #[link(name = "ctp_wrapper")]
677    extern "C" {
678        pub fn CThostFtdcTraderApi_ReqQryOrder(
679            api: *mut c_void,
680            req: *const c_void,
681            request_id: c_int,
682        ) -> c_int;
683    }
684
685    // 请求查询成交
686    //
687    // # 参数
688    // * `api` - API实例指针
689    // * `req` - 查询成交请求字段
690    // * `request_id` - 请求ID
691    //
692    // # 返回值
693    // 0表示成功,非0表示失败
694    #[link(name = "ctp_wrapper")]
695    extern "C" {
696        pub fn CThostFtdcTraderApi_ReqQryTrade(
697            api: *mut c_void,
698            req: *const c_void,
699            request_id: c_int,
700        ) -> c_int;
701    }
702
703    // 请求查询合约
704    //
705    // # 参数
706    // * `api` - API实例指针
707    // * `req` - 查询合约请求字段
708    // * `request_id` - 请求ID
709    //
710    // # 返回值
711    // 0表示成功,非0表示失败
712    #[link(name = "ctp_wrapper")]
713    extern "C" {
714        pub fn CThostFtdcTraderApi_ReqQryInstrument(
715            api: *mut c_void,
716            req: *const c_void,
717            request_id: c_int,
718        ) -> c_int;
719    }
720
721    // 第二阶段新增API方法
722
723    // 请求查询合约保证金率
724    //
725    // # 参数
726    // * `api` - API实例指针
727    // * `req` - 查询合约保证金率请求字段
728    // * `request_id` - 请求ID
729    //
730    // # 返回值
731    // 0表示成功,非0表示失败
732    #[link(name = "ctp_wrapper")]
733    extern "C" {
734        pub fn CThostFtdcTraderApi_ReqQryInstrumentMarginRate(
735            api: *mut c_void,
736            req: *const c_void,
737            request_id: c_int,
738        ) -> c_int;
739    }
740
741    // 请求查询合约手续费率
742    //
743    // # 参数
744    // * `api` - API实例指针
745    // * `req` - 查询合约手续费率请求字段
746    // * `request_id` - 请求ID
747    //
748    // # 返回值
749    // 0表示成功,非0表示失败
750    #[link(name = "ctp_wrapper")]
751    extern "C" {
752        pub fn CThostFtdcTraderApi_ReqQryInstrumentCommissionRate(
753            api: *mut c_void,
754            req: *const c_void,
755            request_id: c_int,
756        ) -> c_int;
757    }
758
759    // 请求查询交易所
760    //
761    // # 参数
762    // * `api` - API实例指针
763    // * `req` - 查询交易所请求字段
764    // * `request_id` - 请求ID
765    //
766    // # 返回值
767    // 0表示成功,非0表示失败
768    #[link(name = "ctp_wrapper")]
769    extern "C" {
770        pub fn CThostFtdcTraderApi_ReqQryExchange(
771            api: *mut c_void,
772            req: *const c_void,
773            request_id: c_int,
774        ) -> c_int;
775    }
776
777    // 请求查询产品
778    //
779    // # 参数
780    // * `api` - API实例指针
781    // * `req` - 查询产品请求字段
782    // * `request_id` - 请求ID
783    //
784    // # 返回值
785    // 0表示成功,非0表示失败
786    #[link(name = "ctp_wrapper")]
787    extern "C" {
788        pub fn CThostFtdcTraderApi_ReqQryProduct(
789            api: *mut c_void,
790            req: *const c_void,
791            request_id: c_int,
792        ) -> c_int;
793    }
794
795    // 投资者结算结果确认
796    //
797    // # 参数
798    // * `api` - API实例指针
799    // * `req` - 投资者结算结果确认信息
800    // * `request_id` - 请求ID
801    //
802    // # 返回值
803    // 0表示成功,非0表示失败
804    #[link(name = "ctp_wrapper")]
805    extern "C" {
806        pub fn CThostFtdcTraderApi_ReqSettlementInfoConfirm(
807            api: *mut c_void,
808            req: *const c_void,
809            request_id: c_int,
810        ) -> c_int;
811    }
812
813    // 预埋单录入请求
814    //
815    // # 参数
816    // * `api` - API实例指针
817    // * `req` - 预埋单字段
818    // * `request_id` - 请求ID
819    //
820    // # 返回值
821    // 0表示成功,非0表示失败
822    #[link(name = "ctp_wrapper")]
823    extern "C" {
824        pub fn CThostFtdcTraderApi_ReqParkedOrderInsert(
825            api: *mut c_void,
826            req: *const c_void,
827            request_id: c_int,
828        ) -> c_int;
829    }
830
831    // 预埋撤单录入请求
832    //
833    // # 参数
834    // * `api` - API实例指针
835    // * `req` - 预埋撤单字段
836    // * `request_id` - 请求ID
837    //
838    // # 返回值
839    // 0表示成功,非0表示失败
840    #[link(name = "ctp_wrapper")]
841    extern "C" {
842        pub fn CThostFtdcTraderApi_ReqParkedOrderAction(
843            api: *mut c_void,
844            req: *const c_void,
845            request_id: c_int,
846        ) -> c_int;
847    }
848
849    // 第三阶段新增API方法
850
851    // 执行宣告录入请求
852    //
853    // # 参数
854    // * `api` - API实例指针
855    // * `req` - 执行宣告录入字段
856    // * `request_id` - 请求ID
857    //
858    // # 返回值
859    // 0表示成功,非0表示失败
860    #[link(name = "ctp_wrapper")]
861    extern "C" {
862        pub fn CThostFtdcTraderApi_ReqExecOrderInsert(
863            api: *mut c_void,
864            req: *const c_void,
865            request_id: c_int,
866        ) -> c_int;
867    }
868
869    // 执行宣告操作请求
870    //
871    // # 参数
872    // * `api` - API实例指针
873    // * `req` - 执行宣告操作字段
874    // * `request_id` - 请求ID
875    //
876    // # 返回值
877    // 0表示成功,非0表示失败
878    #[link(name = "ctp_wrapper")]
879    extern "C" {
880        pub fn CThostFtdcTraderApi_ReqExecOrderAction(
881            api: *mut c_void,
882            req: *const c_void,
883            request_id: c_int,
884        ) -> c_int;
885    }
886
887    // 询价录入请求
888    //
889    // # 参数
890    // * `api` - API实例指针
891    // * `req` - 询价录入字段
892    // * `request_id` - 请求ID
893    //
894    // # 返回值
895    // 0表示成功,非0表示失败
896    #[link(name = "ctp_wrapper")]
897    extern "C" {
898        pub fn CThostFtdcTraderApi_ReqForQuoteInsert(
899            api: *mut c_void,
900            req: *const c_void,
901            request_id: c_int,
902        ) -> c_int;
903    }
904
905    // 报价录入请求
906    //
907    // # 参数
908    // * `api` - API实例指针
909    // * `req` - 报价录入字段
910    // * `request_id` - 请求ID
911    //
912    // # 返回值
913    // 0表示成功,非0表示失败
914    #[link(name = "ctp_wrapper")]
915    extern "C" {
916        pub fn CThostFtdcTraderApi_ReqQuoteInsert(
917            api: *mut c_void,
918            req: *const c_void,
919            request_id: c_int,
920        ) -> c_int;
921    }
922
923    // 报价操作请求
924    //
925    // # 参数
926    // * `api` - API实例指针
927    // * `req` - 报价操作字段
928    // * `request_id` - 请求ID
929    //
930    // # 返回值
931    // 0表示成功,非0表示失败
932    #[link(name = "ctp_wrapper")]
933    extern "C" {
934        pub fn CThostFtdcTraderApi_ReqQuoteAction(
935            api: *mut c_void,
936            req: *const c_void,
937            request_id: c_int,
938        ) -> c_int;
939    }
940
941    // 批量报单操作请求
942    //
943    // # 参数
944    // * `api` - API实例指针
945    // * `req` - 批量报单操作字段
946    // * `request_id` - 请求ID
947    //
948    // # 返回值
949    // 0表示成功,非0表示失败
950    #[link(name = "ctp_wrapper")]
951    extern "C" {
952        pub fn CThostFtdcTraderApi_ReqBatchOrderAction(
953            api: *mut c_void,
954            req: *const c_void,
955            request_id: c_int,
956        ) -> c_int;
957    }
958
959    // 删除预埋单请求
960    //
961    // # 参数
962    // * `api` - API实例指针
963    // * `req` - 删除预埋单字段
964    // * `request_id` - 请求ID
965    //
966    // # 返回值
967    // 0表示成功,非0表示失败
968    #[link(name = "ctp_wrapper")]
969    extern "C" {
970        pub fn CThostFtdcTraderApi_ReqRemoveParkedOrder(
971            api: *mut c_void,
972            req: *const c_void,
973            request_id: c_int,
974        ) -> c_int;
975    }
976
977    // 删除预埋撤单请求
978    //
979    // # 参数
980    // * `api` - API实例指针
981    // * `req` - 删除预埋撤单字段
982    // * `request_id` - 请求ID
983    //
984    // # 返回值
985    // 0表示成功,非0表示失败
986    #[link(name = "ctp_wrapper")]
987    extern "C" {
988        pub fn CThostFtdcTraderApi_ReqRemoveParkedOrderAction(
989            api: *mut c_void,
990            req: *const c_void,
991            request_id: c_int,
992        ) -> c_int;
993    }
994
995    // 查询最大报单数量请求
996    //
997    // # 参数
998    // * `api` - API实例指针
999    // * `req` - 查询最大报单数量字段
1000    // * `request_id` - 请求ID
1001    //
1002    // # 返回值
1003    // 0表示成功,非0表示失败
1004    #[link(name = "ctp_wrapper")]
1005    extern "C" {
1006        pub fn CThostFtdcTraderApi_ReqQryMaxOrderVolume(
1007            api: *mut c_void,
1008            req: *const c_void,
1009            request_id: c_int,
1010        ) -> c_int;
1011    }
1012
1013    // 请求查询行情
1014    //
1015    // # 参数
1016    // * `api` - API实例指针
1017    // * `req` - 查询行情字段
1018    // * `request_id` - 请求ID
1019    //
1020    // # 返回值
1021    // 0表示成功,非0表示失败
1022    #[link(name = "ctp_wrapper")]
1023    extern "C" {
1024        pub fn CThostFtdcTraderApi_ReqQryDepthMarketData(
1025            api: *mut c_void,
1026            req: *const c_void,
1027            request_id: c_int,
1028        ) -> c_int;
1029    }
1030
1031    // 请求查询投资者结算结果
1032    //
1033    // # 参数
1034    // * `api` - API实例指针
1035    // * `req` - 查询投资者结算结果字段
1036    // * `request_id` - 请求ID
1037    //
1038    // # 返回值
1039    // 0表示成功,非0表示失败
1040    #[link(name = "ctp_wrapper")]
1041    extern "C" {
1042        pub fn CThostFtdcTraderApi_ReqQrySettlementInfo(
1043            api: *mut c_void,
1044            req: *const c_void,
1045            request_id: c_int,
1046        ) -> c_int;
1047    }
1048
1049    // 请求查询转帐银行
1050    //
1051    // # 参数
1052    // * `api` - API实例指针
1053    // * `req` - 查询转帐银行字段
1054    // * `request_id` - 请求ID
1055    //
1056    // # 返回值
1057    // 0表示成功,非0表示失败
1058    #[link(name = "ctp_wrapper")]
1059    extern "C" {
1060        pub fn CThostFtdcTraderApi_ReqQryTransferBank(
1061            api: *mut c_void,
1062            req: *const c_void,
1063            request_id: c_int,
1064        ) -> c_int;
1065    }
1066
1067    // 请求查询投资者持仓明细
1068    //
1069    // # 参数
1070    // * `api` - API实例指针
1071    // * `req` - 查询投资者持仓明细字段
1072    // * `request_id` - 请求ID
1073    //
1074    // # 返回值
1075    // 0表示成功,非0表示失败
1076    #[link(name = "ctp_wrapper")]
1077    extern "C" {
1078        pub fn CThostFtdcTraderApi_ReqQryInvestorPositionDetail(
1079            api: *mut c_void,
1080            req: *const c_void,
1081            request_id: c_int,
1082        ) -> c_int;
1083    }
1084
1085    // 请求查询客户通知
1086    //
1087    // # 参数
1088    // * `api` - API实例指针
1089    // * `req` - 查询客户通知字段
1090    // * `request_id` - 请求ID
1091    //
1092    // # 返回值
1093    // 0表示成功,非0表示失败
1094    #[link(name = "ctp_wrapper")]
1095    extern "C" {
1096        pub fn CThostFtdcTraderApi_ReqQryNotice(
1097            api: *mut c_void,
1098            req: *const c_void,
1099            request_id: c_int,
1100        ) -> c_int;
1101    }
1102}
1103
1104// SPI回调函数类型定义
1105pub mod callbacks {
1106    use super::*;
1107
1108    // 连接建立回调
1109    pub type OnFrontConnectedCallback = extern "C" fn();
1110
1111    // 连接断开回调
1112    pub type OnFrontDisconnectedCallback = extern "C" fn(reason: c_int);
1113
1114    // 心跳超时警告回调
1115    pub type OnHeartBeatWarningCallback = extern "C" fn(time_lapse: c_int);
1116
1117    // 登录响应回调
1118    pub type OnRspUserLoginCallback = extern "C" fn(
1119        user_login: *const c_void,
1120        rsp_info: *const c_void,
1121        request_id: c_int,
1122        is_last: bool,
1123    );
1124
1125    // 登出响应回调
1126    pub type OnRspUserLogoutCallback = extern "C" fn(
1127        user_logout: *const c_void,
1128        rsp_info: *const c_void,
1129        request_id: c_int,
1130        is_last: bool,
1131    );
1132
1133    // 深度行情通知回调
1134    pub type OnRtnDepthMarketDataCallback = extern "C" fn(depth_market_data: *const c_void);
1135
1136    // 订阅行情应答回调
1137    pub type OnRspSubMarketDataCallback = extern "C" fn(
1138        specific_instrument: *const c_void,
1139        rsp_info: *const c_void,
1140        request_id: c_int,
1141        is_last: bool,
1142    );
1143
1144    // 取消订阅行情应答回调
1145    pub type OnRspUnSubMarketDataCallback = extern "C" fn(
1146        specific_instrument: *const c_void,
1147        rsp_info: *const c_void,
1148        request_id: c_int,
1149        is_last: bool,
1150    );
1151
1152    // 认证响应回调
1153    pub type OnRspAuthenticateCallback = extern "C" fn(
1154        rsp_authenticate: *const c_void,
1155        rsp_info: *const c_void,
1156        request_id: c_int,
1157        is_last: bool,
1158    );
1159
1160    // 错误响应回调
1161    pub type OnRspErrorCallback =
1162        extern "C" fn(rsp_info: *const c_void, request_id: c_int, is_last: bool);
1163}
1164
1165// 确保FFI绑定在编译时正确链接
1166#[cfg(test)]
1167mod tests {
1168    use std::ptr;
1169
1170    #[test]
1171    fn test_ffi_declarations_compile() {
1172        // 这个测试确保FFI声明可以正确编译
1173        // 实际的FFI调用需要真实的CTP库
1174        let _api_ptr: *mut std::os::raw::c_void = ptr::null_mut();
1175        assert!(true, "FFI声明编译成功");
1176    }
1177}