hamlib 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hamlib",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Node.js wrapper for hamlib radio control library",
5
5
  "main": "index.js",
6
6
  "module": "lib/index.mjs",
Binary file
Binary file
Binary file
package/src/hamlib.cpp CHANGED
@@ -15,6 +15,16 @@
15
15
  #endif
16
16
  #endif
17
17
 
18
+ // 安全宏 - 检查RIG指针有效性,防止空指针解引用和已销毁对象访问
19
+ #define CHECK_RIG_VALID() \
20
+ do { \
21
+ if (!hamlib_instance_ || !hamlib_instance_->my_rig) { \
22
+ result_code_ = -RIG_EINVAL; \
23
+ error_message_ = "RIG is not initialized or has been destroyed"; \
24
+ return; \
25
+ } \
26
+ } while(0)
27
+
18
28
  // Structure to hold rig information for the callback
19
29
  struct RigListData {
20
30
  std::vector<Napi::Object> rigList;
@@ -37,6 +47,8 @@ public:
37
47
  : HamLibAsyncWorker(env, hamlib_instance) {}
38
48
 
39
49
  void Execute() override {
50
+ CHECK_RIG_VALID();
51
+
40
52
  result_code_ = rig_open(hamlib_instance_->my_rig);
41
53
  if (result_code_ != RIG_OK) {
42
54
  error_message_ = rigerror(result_code_);
@@ -73,6 +85,8 @@ public:
73
85
  : HamLibAsyncWorker(env, hamlib_instance), freq_(freq), vfo_(vfo) {}
74
86
 
75
87
  void Execute() override {
88
+ CHECK_RIG_VALID();
89
+
76
90
  result_code_ = rig_set_freq(hamlib_instance_->my_rig, vfo_, freq_);
77
91
  if (result_code_ != RIG_OK) {
78
92
  error_message_ = rigerror(result_code_);
@@ -104,6 +118,8 @@ public:
104
118
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), freq_(0) {}
105
119
 
106
120
  void Execute() override {
121
+ CHECK_RIG_VALID();
122
+
107
123
  result_code_ = rig_get_freq(hamlib_instance_->my_rig, vfo_, &freq_);
108
124
  if (result_code_ != RIG_OK) {
109
125
  error_message_ = rigerror(result_code_);
@@ -135,6 +151,8 @@ public:
135
151
  : HamLibAsyncWorker(env, hamlib_instance), mode_(mode), width_(width), vfo_(vfo) {}
136
152
 
137
153
  void Execute() override {
154
+ CHECK_RIG_VALID();
155
+
138
156
  result_code_ = rig_set_mode(hamlib_instance_->my_rig, vfo_, mode_, width_);
139
157
  if (result_code_ != RIG_OK) {
140
158
  error_message_ = rigerror(result_code_);
@@ -167,6 +185,8 @@ public:
167
185
  : HamLibAsyncWorker(env, hamlib_instance), mode_(0), width_(0) {}
168
186
 
169
187
  void Execute() override {
188
+ CHECK_RIG_VALID();
189
+
170
190
  result_code_ = rig_get_mode(hamlib_instance_->my_rig, RIG_VFO_CURR, &mode_, &width_);
171
191
  if (result_code_ != RIG_OK) {
172
192
  error_message_ = rigerror(result_code_);
@@ -203,6 +223,8 @@ public:
203
223
  : HamLibAsyncWorker(env, hamlib_instance), ptt_(ptt) {}
204
224
 
205
225
  void Execute() override {
226
+ CHECK_RIG_VALID();
227
+
206
228
  result_code_ = rig_set_ptt(hamlib_instance_->my_rig, RIG_VFO_CURR, ptt_);
207
229
  if (result_code_ != RIG_OK) {
208
230
  error_message_ = rigerror(result_code_);
@@ -233,6 +255,8 @@ public:
233
255
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), strength_(0) {}
234
256
 
235
257
  void Execute() override {
258
+ CHECK_RIG_VALID();
259
+
236
260
  result_code_ = rig_get_strength(hamlib_instance_->my_rig, vfo_, &strength_);
237
261
  if (result_code_ != RIG_OK) {
238
262
  error_message_ = rigerror(result_code_);
@@ -264,6 +288,8 @@ public:
264
288
  : HamLibAsyncWorker(env, hamlib_instance), level_type_(level_type), value_(value) {}
265
289
 
266
290
  void Execute() override {
291
+ CHECK_RIG_VALID();
292
+
267
293
  result_code_ = rig_set_level(hamlib_instance_->my_rig, RIG_VFO_CURR, level_type_, value_);
268
294
  if (result_code_ != RIG_OK) {
269
295
  error_message_ = rigerror(result_code_);
@@ -297,6 +323,8 @@ public:
297
323
  }
298
324
 
299
325
  void Execute() override {
326
+ CHECK_RIG_VALID();
327
+
300
328
  result_code_ = rig_get_level(hamlib_instance_->my_rig, RIG_VFO_CURR, level_type_, &value_);
301
329
  if (result_code_ != RIG_OK) {
302
330
  error_message_ = rigerror(result_code_);
@@ -328,6 +356,8 @@ public:
328
356
  : HamLibAsyncWorker(env, hamlib_instance), func_type_(func_type), enable_(enable) {}
329
357
 
330
358
  void Execute() override {
359
+ CHECK_RIG_VALID();
360
+
331
361
  result_code_ = rig_set_func(hamlib_instance_->my_rig, RIG_VFO_CURR, func_type_, enable_);
332
362
  if (result_code_ != RIG_OK) {
333
363
  error_message_ = rigerror(result_code_);
@@ -359,6 +389,8 @@ public:
359
389
  : HamLibAsyncWorker(env, hamlib_instance), func_type_(func_type), state_(0) {}
360
390
 
361
391
  void Execute() override {
392
+ CHECK_RIG_VALID();
393
+
362
394
  result_code_ = rig_get_func(hamlib_instance_->my_rig, RIG_VFO_CURR, func_type_, &state_);
363
395
  if (result_code_ != RIG_OK) {
364
396
  error_message_ = rigerror(result_code_);
@@ -390,6 +422,8 @@ public:
390
422
  : HamLibAsyncWorker(env, hamlib_instance), chan_(chan) {}
391
423
 
392
424
  void Execute() override {
425
+ CHECK_RIG_VALID();
426
+
393
427
  result_code_ = rig_set_channel(hamlib_instance_->my_rig, RIG_VFO_MEM, &chan_);
394
428
  if (result_code_ != RIG_OK) {
395
429
  error_message_ = rigerror(result_code_);
@@ -422,6 +456,8 @@ public:
422
456
  }
423
457
 
424
458
  void Execute() override {
459
+ CHECK_RIG_VALID();
460
+
425
461
  chan_.channel_num = channel_num_;
426
462
  chan_.vfo = RIG_VFO_MEM;
427
463
  result_code_ = rig_get_channel(hamlib_instance_->my_rig, RIG_VFO_MEM, &chan_, read_only_);
@@ -476,6 +512,8 @@ public:
476
512
  : HamLibAsyncWorker(env, hamlib_instance), channel_num_(channel_num) {}
477
513
 
478
514
  void Execute() override {
515
+ CHECK_RIG_VALID();
516
+
479
517
  result_code_ = rig_set_mem(hamlib_instance_->my_rig, RIG_VFO_CURR, channel_num_);
480
518
  if (result_code_ != RIG_OK) {
481
519
  error_message_ = rigerror(result_code_);
@@ -506,6 +544,8 @@ public:
506
544
  : HamLibAsyncWorker(env, hamlib_instance), rit_offset_(rit_offset) {}
507
545
 
508
546
  void Execute() override {
547
+ CHECK_RIG_VALID();
548
+
509
549
  result_code_ = rig_set_rit(hamlib_instance_->my_rig, RIG_VFO_CURR, rit_offset_);
510
550
  if (result_code_ != RIG_OK) {
511
551
  error_message_ = rigerror(result_code_);
@@ -536,6 +576,8 @@ public:
536
576
  : HamLibAsyncWorker(env, hamlib_instance), rit_offset_(0) {}
537
577
 
538
578
  void Execute() override {
579
+ CHECK_RIG_VALID();
580
+
539
581
  result_code_ = rig_get_rit(hamlib_instance_->my_rig, RIG_VFO_CURR, &rit_offset_);
540
582
  if (result_code_ != RIG_OK) {
541
583
  error_message_ = rigerror(result_code_);
@@ -566,6 +608,8 @@ public:
566
608
  : HamLibAsyncWorker(env, hamlib_instance), xit_offset_(xit_offset) {}
567
609
 
568
610
  void Execute() override {
611
+ CHECK_RIG_VALID();
612
+
569
613
  result_code_ = rig_set_xit(hamlib_instance_->my_rig, RIG_VFO_CURR, xit_offset_);
570
614
  if (result_code_ != RIG_OK) {
571
615
  error_message_ = rigerror(result_code_);
@@ -596,6 +640,8 @@ public:
596
640
  : HamLibAsyncWorker(env, hamlib_instance), xit_offset_(0) {}
597
641
 
598
642
  void Execute() override {
643
+ CHECK_RIG_VALID();
644
+
599
645
  result_code_ = rig_get_xit(hamlib_instance_->my_rig, RIG_VFO_CURR, &xit_offset_);
600
646
  if (result_code_ != RIG_OK) {
601
647
  error_message_ = rigerror(result_code_);
@@ -626,6 +672,8 @@ public:
626
672
  : HamLibAsyncWorker(env, hamlib_instance) {}
627
673
 
628
674
  void Execute() override {
675
+ CHECK_RIG_VALID();
676
+
629
677
  int ritCode = rig_set_rit(hamlib_instance_->my_rig, RIG_VFO_CURR, 0);
630
678
  int xitCode = rig_set_xit(hamlib_instance_->my_rig, RIG_VFO_CURR, 0);
631
679
 
@@ -661,6 +709,8 @@ public:
661
709
  : HamLibAsyncWorker(env, hamlib_instance), tx_freq_(tx_freq), vfo_(vfo) {}
662
710
 
663
711
  void Execute() override {
712
+ CHECK_RIG_VALID();
713
+
664
714
  result_code_ = rig_set_split_freq(hamlib_instance_->my_rig, vfo_, tx_freq_);
665
715
  if (result_code_ != RIG_OK) {
666
716
  error_message_ = rigerror(result_code_);
@@ -692,6 +742,8 @@ public:
692
742
  : HamLibAsyncWorker(env, hamlib_instance), tx_freq_(0), vfo_(vfo) {}
693
743
 
694
744
  void Execute() override {
745
+ CHECK_RIG_VALID();
746
+
695
747
  result_code_ = rig_get_split_freq(hamlib_instance_->my_rig, vfo_, &tx_freq_);
696
748
  if (result_code_ != RIG_OK) {
697
749
  error_message_ = rigerror(result_code_);
@@ -723,6 +775,8 @@ public:
723
775
  : HamLibAsyncWorker(env, hamlib_instance), rx_vfo_(rx_vfo), split_(split), tx_vfo_(tx_vfo) {}
724
776
 
725
777
  void Execute() override {
778
+ CHECK_RIG_VALID();
779
+
726
780
  result_code_ = rig_set_split_vfo(hamlib_instance_->my_rig, rx_vfo_, split_, tx_vfo_);
727
781
  if (result_code_ != RIG_OK) {
728
782
  error_message_ = rigerror(result_code_);
@@ -755,6 +809,8 @@ public:
755
809
  : HamLibAsyncWorker(env, hamlib_instance), split_(RIG_SPLIT_OFF), tx_vfo_(RIG_VFO_B), vfo_(vfo) {}
756
810
 
757
811
  void Execute() override {
812
+ CHECK_RIG_VALID();
813
+
758
814
  result_code_ = rig_get_split_vfo(hamlib_instance_->my_rig, vfo_, &split_, &tx_vfo_);
759
815
  if (result_code_ != RIG_OK) {
760
816
  error_message_ = rigerror(result_code_);
@@ -796,6 +852,8 @@ public:
796
852
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo) {}
797
853
 
798
854
  void Execute() override {
855
+ CHECK_RIG_VALID();
856
+
799
857
  result_code_ = rig_set_vfo(hamlib_instance_->my_rig, vfo_);
800
858
  if (result_code_ != RIG_OK) {
801
859
  error_message_ = rigerror(result_code_);
@@ -826,6 +884,8 @@ public:
826
884
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(0) {}
827
885
 
828
886
  void Execute() override {
887
+ CHECK_RIG_VALID();
888
+
829
889
  result_code_ = rig_get_vfo(hamlib_instance_->my_rig, &vfo_);
830
890
  if (result_code_ != RIG_OK) {
831
891
  // 提供更清晰的错误信息
@@ -887,6 +947,14 @@ public:
887
947
  : HamLibAsyncWorker(env, hamlib_instance) {}
888
948
 
889
949
  void Execute() override {
950
+ CHECK_RIG_VALID();
951
+
952
+ // 检查rig是否已经关闭
953
+ if (!hamlib_instance_->rig_is_open) {
954
+ result_code_ = RIG_OK; // 已经关闭,返回成功
955
+ return;
956
+ }
957
+
890
958
  result_code_ = rig_close(hamlib_instance_->my_rig);
891
959
  if (result_code_ != RIG_OK) {
892
960
  error_message_ = rigerror(result_code_);
@@ -916,14 +984,15 @@ public:
916
984
  : HamLibAsyncWorker(env, hamlib_instance) {}
917
985
 
918
986
  void Execute() override {
919
- if (hamlib_instance_->rig_is_open) {
920
- rig_close(hamlib_instance_->my_rig);
921
- }
987
+ CHECK_RIG_VALID();
988
+
989
+ // rig_cleanup会自动调用rig_close,所以我们不需要重复调用
922
990
  result_code_ = rig_cleanup(hamlib_instance_->my_rig);
923
991
  if (result_code_ != RIG_OK) {
924
992
  error_message_ = rigerror(result_code_);
925
993
  } else {
926
994
  hamlib_instance_->rig_is_open = false;
995
+ hamlib_instance_->my_rig = nullptr; // 重要:清空指针防止重复释放
927
996
  }
928
997
  }
929
998
 
@@ -949,6 +1018,8 @@ public:
949
1018
  : HamLibAsyncWorker(env, hamlib_instance), scan_type_(scan_type), channel_(channel) {}
950
1019
 
951
1020
  void Execute() override {
1021
+ CHECK_RIG_VALID();
1022
+
952
1023
  result_code_ = rig_scan(hamlib_instance_->my_rig, RIG_VFO_CURR, scan_type_, channel_);
953
1024
  if (result_code_ != RIG_OK) {
954
1025
  error_message_ = rigerror(result_code_);
@@ -981,6 +1052,8 @@ public:
981
1052
  : HamLibAsyncWorker(env, hamlib_instance) {}
982
1053
 
983
1054
  void Execute() override {
1055
+ CHECK_RIG_VALID();
1056
+
984
1057
  result_code_ = rig_scan(hamlib_instance_->my_rig, RIG_VFO_CURR, RIG_SCAN_STOP, 0);
985
1058
  if (result_code_ != RIG_OK) {
986
1059
  error_message_ = rigerror(result_code_);
@@ -1009,6 +1082,8 @@ public:
1009
1082
  : HamLibAsyncWorker(env, hamlib_instance), vfo_op_(vfo_op) {}
1010
1083
 
1011
1084
  void Execute() override {
1085
+ CHECK_RIG_VALID();
1086
+
1012
1087
  result_code_ = rig_vfo_op(hamlib_instance_->my_rig, RIG_VFO_CURR, vfo_op_);
1013
1088
  if (result_code_ != RIG_OK) {
1014
1089
  error_message_ = rigerror(result_code_);
@@ -1040,6 +1115,8 @@ public:
1040
1115
  : HamLibAsyncWorker(env, hamlib_instance), antenna_(antenna), vfo_(vfo), option_(option) {}
1041
1116
 
1042
1117
  void Execute() override {
1118
+ CHECK_RIG_VALID();
1119
+
1043
1120
  result_code_ = rig_set_ant(hamlib_instance_->my_rig, vfo_, antenna_, option_);
1044
1121
  if (result_code_ != RIG_OK) {
1045
1122
  error_message_ = rigerror(result_code_);
@@ -1073,6 +1150,8 @@ public:
1073
1150
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), antenna_(antenna), antenna_curr_(0), antenna_tx_(0), antenna_rx_(0) {}
1074
1151
 
1075
1152
  void Execute() override {
1153
+ CHECK_RIG_VALID();
1154
+
1076
1155
  option_ = {0};
1077
1156
 
1078
1157
  result_code_ = rig_get_ant(hamlib_instance_->my_rig, vfo_, antenna_, &option_, &antenna_curr_, &antenna_tx_, &antenna_rx_);
@@ -1118,6 +1197,8 @@ public:
1118
1197
  : HamLibAsyncWorker(env, hamlib_instance), power_(power), freq_(freq), mode_(mode), mwpower_(0) {}
1119
1198
 
1120
1199
  void Execute() override {
1200
+ CHECK_RIG_VALID();
1201
+
1121
1202
  result_code_ = rig_power2mW(hamlib_instance_->my_rig, &mwpower_, power_, freq_, mode_);
1122
1203
  if (result_code_ != RIG_OK) {
1123
1204
  error_message_ = rigerror(result_code_);
@@ -1152,6 +1233,8 @@ public:
1152
1233
  : HamLibAsyncWorker(env, hamlib_instance), mwpower_(mwpower), freq_(freq), mode_(mode), power_(0.0) {}
1153
1234
 
1154
1235
  void Execute() override {
1236
+ CHECK_RIG_VALID();
1237
+
1155
1238
  result_code_ = rig_mW2power(hamlib_instance_->my_rig, &power_, mwpower_, freq_, mode_);
1156
1239
  if (result_code_ != RIG_OK) {
1157
1240
  error_message_ = rigerror(result_code_);
@@ -1186,6 +1269,8 @@ public:
1186
1269
  : HamLibAsyncWorker(env, hamlib_instance), tx_mode_(tx_mode), tx_width_(tx_width), vfo_(vfo) {}
1187
1270
 
1188
1271
  void Execute() override {
1272
+ CHECK_RIG_VALID();
1273
+
1189
1274
  result_code_ = rig_set_split_mode(hamlib_instance_->my_rig, vfo_, tx_mode_, tx_width_);
1190
1275
  if (result_code_ != RIG_OK) {
1191
1276
  error_message_ = rigerror(result_code_);
@@ -1219,6 +1304,8 @@ public:
1219
1304
  : HamLibAsyncWorker(env, hamlib_instance), tx_mode_(0), tx_width_(0), vfo_(vfo) {}
1220
1305
 
1221
1306
  void Execute() override {
1307
+ CHECK_RIG_VALID();
1308
+
1222
1309
  result_code_ = rig_get_split_mode(hamlib_instance_->my_rig, vfo_, &tx_mode_, &tx_width_);
1223
1310
  if (result_code_ != RIG_OK) {
1224
1311
  error_message_ = rigerror(result_code_);
@@ -1257,6 +1344,8 @@ public:
1257
1344
  : HamLibAsyncWorker(env, hamlib_instance), param_name_(param_name), param_value_(param_value) {}
1258
1345
 
1259
1346
  void Execute() override {
1347
+ CHECK_RIG_VALID();
1348
+
1260
1349
  // Apply serial configuration parameter
1261
1350
  if (param_name_ == "data_bits") {
1262
1351
  int data_bits = std::stoi(param_value_);
@@ -1414,6 +1503,8 @@ public:
1414
1503
  : HamLibAsyncWorker(env, hamlib_instance), param_name_(param_name) {}
1415
1504
 
1416
1505
  void Execute() override {
1506
+ CHECK_RIG_VALID();
1507
+
1417
1508
  // Get serial configuration parameter
1418
1509
  if (param_name_ == "data_bits") {
1419
1510
  param_value_ = std::to_string(hamlib_instance_->my_rig->state.rigport.parm.serial.data_bits);
@@ -1529,6 +1620,8 @@ public:
1529
1620
  : HamLibAsyncWorker(env, hamlib_instance), ptt_type_(ptt_type) {}
1530
1621
 
1531
1622
  void Execute() override {
1623
+ CHECK_RIG_VALID();
1624
+
1532
1625
  ptt_type_t ptt_type;
1533
1626
  if (ptt_type_ == "RIG") {
1534
1627
  ptt_type = RIG_PTT_RIG;
@@ -1581,6 +1674,8 @@ public:
1581
1674
  : HamLibAsyncWorker(env, hamlib_instance) {}
1582
1675
 
1583
1676
  void Execute() override {
1677
+ CHECK_RIG_VALID();
1678
+
1584
1679
  ptt_type_t ptt_type = hamlib_instance_->my_rig->state.pttport.type.ptt;
1585
1680
 
1586
1681
  switch (ptt_type) {
@@ -1640,6 +1735,8 @@ public:
1640
1735
  : HamLibAsyncWorker(env, hamlib_instance), dcd_type_(dcd_type) {}
1641
1736
 
1642
1737
  void Execute() override {
1738
+ CHECK_RIG_VALID();
1739
+
1643
1740
  dcd_type_t dcd_type;
1644
1741
  if (dcd_type_ == "RIG") {
1645
1742
  dcd_type = RIG_DCD_RIG;
@@ -1694,6 +1791,8 @@ public:
1694
1791
  : HamLibAsyncWorker(env, hamlib_instance) {}
1695
1792
 
1696
1793
  void Execute() override {
1794
+ CHECK_RIG_VALID();
1795
+
1697
1796
  dcd_type_t dcd_type = hamlib_instance_->my_rig->state.dcdport.type.dcd;
1698
1797
 
1699
1798
  switch (dcd_type) {
@@ -1756,6 +1855,8 @@ public:
1756
1855
  : HamLibAsyncWorker(env, hamlib_instance), status_(status) {}
1757
1856
 
1758
1857
  void Execute() override {
1858
+ CHECK_RIG_VALID();
1859
+
1759
1860
  result_code_ = rig_set_powerstat(hamlib_instance_->my_rig, status_);
1760
1861
  if (result_code_ != RIG_OK) {
1761
1862
  error_message_ = rigerror(result_code_);
@@ -1786,6 +1887,8 @@ public:
1786
1887
  : HamLibAsyncWorker(env, hamlib_instance), status_(RIG_POWER_UNKNOWN) {}
1787
1888
 
1788
1889
  void Execute() override {
1890
+ CHECK_RIG_VALID();
1891
+
1789
1892
  result_code_ = rig_get_powerstat(hamlib_instance_->my_rig, &status_);
1790
1893
  if (result_code_ != RIG_OK) {
1791
1894
  error_message_ = rigerror(result_code_);
@@ -1817,6 +1920,8 @@ public:
1817
1920
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), ptt_(RIG_PTT_OFF) {}
1818
1921
 
1819
1922
  void Execute() override {
1923
+ CHECK_RIG_VALID();
1924
+
1820
1925
  result_code_ = rig_get_ptt(hamlib_instance_->my_rig, vfo_, &ptt_);
1821
1926
  if (result_code_ != RIG_OK) {
1822
1927
  error_message_ = rigerror(result_code_);
@@ -1849,6 +1954,8 @@ public:
1849
1954
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), dcd_(RIG_DCD_OFF) {}
1850
1955
 
1851
1956
  void Execute() override {
1957
+ CHECK_RIG_VALID();
1958
+
1852
1959
  result_code_ = rig_get_dcd(hamlib_instance_->my_rig, vfo_, &dcd_);
1853
1960
  if (result_code_ != RIG_OK) {
1854
1961
  error_message_ = rigerror(result_code_);
@@ -1881,6 +1988,8 @@ public:
1881
1988
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), ts_(ts) {}
1882
1989
 
1883
1990
  void Execute() override {
1991
+ CHECK_RIG_VALID();
1992
+
1884
1993
  result_code_ = rig_set_ts(hamlib_instance_->my_rig, vfo_, ts_);
1885
1994
  if (result_code_ != RIG_OK) {
1886
1995
  error_message_ = rigerror(result_code_);
@@ -1912,6 +2021,8 @@ public:
1912
2021
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), ts_(0) {}
1913
2022
 
1914
2023
  void Execute() override {
2024
+ CHECK_RIG_VALID();
2025
+
1915
2026
  result_code_ = rig_get_ts(hamlib_instance_->my_rig, vfo_, &ts_);
1916
2027
  if (result_code_ != RIG_OK) {
1917
2028
  error_message_ = rigerror(result_code_);
@@ -1944,6 +2055,8 @@ public:
1944
2055
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), shift_(shift) {}
1945
2056
 
1946
2057
  void Execute() override {
2058
+ CHECK_RIG_VALID();
2059
+
1947
2060
  result_code_ = rig_set_rptr_shift(hamlib_instance_->my_rig, vfo_, shift_);
1948
2061
  if (result_code_ != RIG_OK) {
1949
2062
  error_message_ = rigerror(result_code_);
@@ -1975,6 +2088,8 @@ public:
1975
2088
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), shift_(RIG_RPT_SHIFT_NONE) {}
1976
2089
 
1977
2090
  void Execute() override {
2091
+ CHECK_RIG_VALID();
2092
+
1978
2093
  result_code_ = rig_get_rptr_shift(hamlib_instance_->my_rig, vfo_, &shift_);
1979
2094
  if (result_code_ != RIG_OK) {
1980
2095
  error_message_ = rigerror(result_code_);
@@ -2003,6 +2118,8 @@ public:
2003
2118
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), offset_(offset) {}
2004
2119
 
2005
2120
  void Execute() override {
2121
+ CHECK_RIG_VALID();
2122
+
2006
2123
  result_code_ = rig_set_rptr_offs(hamlib_instance_->my_rig, vfo_, offset_);
2007
2124
  if (result_code_ != RIG_OK) {
2008
2125
  error_message_ = rigerror(result_code_);
@@ -2034,6 +2151,8 @@ public:
2034
2151
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), offset_(0) {}
2035
2152
 
2036
2153
  void Execute() override {
2154
+ CHECK_RIG_VALID();
2155
+
2037
2156
  result_code_ = rig_get_rptr_offs(hamlib_instance_->my_rig, vfo_, &offset_);
2038
2157
  if (result_code_ != RIG_OK) {
2039
2158
  error_message_ = rigerror(result_code_);
@@ -2157,6 +2276,21 @@ NodeHamLib::NodeHamLib(const Napi::CallbackInfo & info): ObjectWrap(info) {
2157
2276
  rig_is_open = false;
2158
2277
  }
2159
2278
 
2279
+ // 析构函数 - 确保资源正确清理
2280
+ NodeHamLib::~NodeHamLib() {
2281
+ // 如果rig指针存在,执行清理
2282
+ if (my_rig) {
2283
+ // 如果rig是打开状态,先关闭
2284
+ if (rig_is_open) {
2285
+ rig_close(my_rig);
2286
+ rig_is_open = false;
2287
+ }
2288
+ // 清理rig资源
2289
+ rig_cleanup(my_rig);
2290
+ my_rig = nullptr;
2291
+ }
2292
+ }
2293
+
2160
2294
  int NodeHamLib::freq_change_cb(RIG *rig, vfo_t vfo, freq_t freq, void* arg) {
2161
2295
  auto instance = static_cast<NodeHamLib*>(arg);
2162
2296
  printf("Rig changed freq to %0.7f Hz\n", freq);
@@ -3976,6 +4110,8 @@ public:
3976
4110
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), tone_(tone) {}
3977
4111
 
3978
4112
  void Execute() override {
4113
+ CHECK_RIG_VALID();
4114
+
3979
4115
  result_code_ = rig_set_ctcss_tone(hamlib_instance_->my_rig, vfo_, tone_);
3980
4116
  if (result_code_ != RIG_OK) {
3981
4117
  error_message_ = rigerror(result_code_);
@@ -4007,6 +4143,8 @@ public:
4007
4143
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), tone_(0) {}
4008
4144
 
4009
4145
  void Execute() override {
4146
+ CHECK_RIG_VALID();
4147
+
4010
4148
  result_code_ = rig_get_ctcss_tone(hamlib_instance_->my_rig, vfo_, &tone_);
4011
4149
  if (result_code_ != RIG_OK) {
4012
4150
  error_message_ = rigerror(result_code_);
@@ -4038,6 +4176,8 @@ public:
4038
4176
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), code_(code) {}
4039
4177
 
4040
4178
  void Execute() override {
4179
+ CHECK_RIG_VALID();
4180
+
4041
4181
  result_code_ = rig_set_dcs_code(hamlib_instance_->my_rig, vfo_, code_);
4042
4182
  if (result_code_ != RIG_OK) {
4043
4183
  error_message_ = rigerror(result_code_);
@@ -4069,6 +4209,8 @@ public:
4069
4209
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), code_(0) {}
4070
4210
 
4071
4211
  void Execute() override {
4212
+ CHECK_RIG_VALID();
4213
+
4072
4214
  result_code_ = rig_get_dcs_code(hamlib_instance_->my_rig, vfo_, &code_);
4073
4215
  if (result_code_ != RIG_OK) {
4074
4216
  error_message_ = rigerror(result_code_);
@@ -4100,6 +4242,8 @@ public:
4100
4242
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), tone_(tone) {}
4101
4243
 
4102
4244
  void Execute() override {
4245
+ CHECK_RIG_VALID();
4246
+
4103
4247
  result_code_ = rig_set_ctcss_sql(hamlib_instance_->my_rig, vfo_, tone_);
4104
4248
  if (result_code_ != RIG_OK) {
4105
4249
  error_message_ = rigerror(result_code_);
@@ -4131,6 +4275,8 @@ public:
4131
4275
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), tone_(0) {}
4132
4276
 
4133
4277
  void Execute() override {
4278
+ CHECK_RIG_VALID();
4279
+
4134
4280
  result_code_ = rig_get_ctcss_sql(hamlib_instance_->my_rig, vfo_, &tone_);
4135
4281
  if (result_code_ != RIG_OK) {
4136
4282
  error_message_ = rigerror(result_code_);
@@ -4162,6 +4308,8 @@ public:
4162
4308
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), code_(code) {}
4163
4309
 
4164
4310
  void Execute() override {
4311
+ CHECK_RIG_VALID();
4312
+
4165
4313
  result_code_ = rig_set_dcs_sql(hamlib_instance_->my_rig, vfo_, code_);
4166
4314
  if (result_code_ != RIG_OK) {
4167
4315
  error_message_ = rigerror(result_code_);
@@ -4193,6 +4341,8 @@ public:
4193
4341
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), code_(0) {}
4194
4342
 
4195
4343
  void Execute() override {
4344
+ CHECK_RIG_VALID();
4345
+
4196
4346
  result_code_ = rig_get_dcs_sql(hamlib_instance_->my_rig, vfo_, &code_);
4197
4347
  if (result_code_ != RIG_OK) {
4198
4348
  error_message_ = rigerror(result_code_);
@@ -4225,6 +4375,8 @@ public:
4225
4375
  : HamLibAsyncWorker(env, hamlib_instance), parm_(parm), value_(value) {}
4226
4376
 
4227
4377
  void Execute() override {
4378
+ CHECK_RIG_VALID();
4379
+
4228
4380
  result_code_ = rig_set_parm(hamlib_instance_->my_rig, parm_, value_);
4229
4381
  if (result_code_ != RIG_OK) {
4230
4382
  error_message_ = rigerror(result_code_);
@@ -4258,6 +4410,8 @@ public:
4258
4410
  }
4259
4411
 
4260
4412
  void Execute() override {
4413
+ CHECK_RIG_VALID();
4414
+
4261
4415
  result_code_ = rig_get_parm(hamlib_instance_->my_rig, parm_, &value_);
4262
4416
  if (result_code_ != RIG_OK) {
4263
4417
  error_message_ = rigerror(result_code_);
@@ -4290,6 +4444,8 @@ public:
4290
4444
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), digits_(digits) {}
4291
4445
 
4292
4446
  void Execute() override {
4447
+ CHECK_RIG_VALID();
4448
+
4293
4449
  result_code_ = rig_send_dtmf(hamlib_instance_->my_rig, vfo_, digits_.c_str());
4294
4450
  if (result_code_ != RIG_OK) {
4295
4451
  error_message_ = rigerror(result_code_);
@@ -4323,6 +4479,8 @@ public:
4323
4479
  }
4324
4480
 
4325
4481
  void Execute() override {
4482
+ CHECK_RIG_VALID();
4483
+
4326
4484
  length_ = max_length_;
4327
4485
  result_code_ = rig_recv_dtmf(hamlib_instance_->my_rig, vfo_, digits_.data(), &length_);
4328
4486
  if (result_code_ != RIG_OK) {
@@ -4361,6 +4519,8 @@ public:
4361
4519
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), ch_(0) {}
4362
4520
 
4363
4521
  void Execute() override {
4522
+ CHECK_RIG_VALID();
4523
+
4364
4524
  result_code_ = rig_get_mem(hamlib_instance_->my_rig, vfo_, &ch_);
4365
4525
  if (result_code_ != RIG_OK) {
4366
4526
  error_message_ = rigerror(result_code_);
@@ -4392,6 +4552,8 @@ public:
4392
4552
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), bank_(bank) {}
4393
4553
 
4394
4554
  void Execute() override {
4555
+ CHECK_RIG_VALID();
4556
+
4395
4557
  result_code_ = rig_set_bank(hamlib_instance_->my_rig, vfo_, bank_);
4396
4558
  if (result_code_ != RIG_OK) {
4397
4559
  error_message_ = rigerror(result_code_);
@@ -4423,6 +4585,8 @@ public:
4423
4585
  : HamLibAsyncWorker(env, hamlib_instance), count_(0) {}
4424
4586
 
4425
4587
  void Execute() override {
4588
+ CHECK_RIG_VALID();
4589
+
4426
4590
  count_ = rig_mem_count(hamlib_instance_->my_rig);
4427
4591
  if (count_ < 0) {
4428
4592
  result_code_ = count_;
@@ -4457,6 +4621,8 @@ public:
4457
4621
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), msg_(msg) {}
4458
4622
 
4459
4623
  void Execute() override {
4624
+ CHECK_RIG_VALID();
4625
+
4460
4626
  result_code_ = rig_send_morse(hamlib_instance_->my_rig, vfo_, msg_.c_str());
4461
4627
  if (result_code_ != RIG_OK) {
4462
4628
  error_message_ = rigerror(result_code_);
@@ -4488,6 +4654,8 @@ public:
4488
4654
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo) {}
4489
4655
 
4490
4656
  void Execute() override {
4657
+ CHECK_RIG_VALID();
4658
+
4491
4659
  result_code_ = rig_stop_morse(hamlib_instance_->my_rig, vfo_);
4492
4660
  if (result_code_ != RIG_OK) {
4493
4661
  error_message_ = rigerror(result_code_);
@@ -4518,6 +4686,8 @@ public:
4518
4686
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo) {}
4519
4687
 
4520
4688
  void Execute() override {
4689
+ CHECK_RIG_VALID();
4690
+
4521
4691
  result_code_ = rig_wait_morse(hamlib_instance_->my_rig, vfo_);
4522
4692
  if (result_code_ != RIG_OK) {
4523
4693
  error_message_ = rigerror(result_code_);
@@ -4549,6 +4719,8 @@ public:
4549
4719
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), ch_(ch) {}
4550
4720
 
4551
4721
  void Execute() override {
4722
+ CHECK_RIG_VALID();
4723
+
4552
4724
  result_code_ = rig_send_voice_mem(hamlib_instance_->my_rig, vfo_, ch_);
4553
4725
  if (result_code_ != RIG_OK) {
4554
4726
  error_message_ = rigerror(result_code_);
@@ -4580,6 +4752,8 @@ public:
4580
4752
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo) {}
4581
4753
 
4582
4754
  void Execute() override {
4755
+ CHECK_RIG_VALID();
4756
+
4583
4757
  #if HAVE_RIG_STOP_VOICE_MEM
4584
4758
  result_code_ = rig_stop_voice_mem(hamlib_instance_->my_rig, vfo_);
4585
4759
  if (result_code_ != RIG_OK) {
@@ -4620,6 +4794,8 @@ public:
4620
4794
  tx_mode_(tx_mode), tx_width_(tx_width) {}
4621
4795
 
4622
4796
  void Execute() override {
4797
+ CHECK_RIG_VALID();
4798
+
4623
4799
  #if HAVE_RIG_SPLIT_FREQ_MODE
4624
4800
  result_code_ = rig_set_split_freq_mode(hamlib_instance_->my_rig, vfo_, tx_freq_, tx_mode_, tx_width_);
4625
4801
  if (result_code_ != RIG_OK) {
@@ -4660,6 +4836,8 @@ public:
4660
4836
  : HamLibAsyncWorker(env, hamlib_instance), vfo_(vfo), tx_freq_(0), tx_mode_(RIG_MODE_NONE), tx_width_(0) {}
4661
4837
 
4662
4838
  void Execute() override {
4839
+ CHECK_RIG_VALID();
4840
+
4663
4841
  #if HAVE_RIG_SPLIT_FREQ_MODE
4664
4842
  result_code_ = rig_get_split_freq_mode(hamlib_instance_->my_rig, vfo_, &tx_freq_, &tx_mode_, &tx_width_);
4665
4843
  if (result_code_ != RIG_OK) {
@@ -4705,6 +4883,8 @@ public:
4705
4883
  : HamLibAsyncWorker(env, hamlib_instance), reset_(reset) {}
4706
4884
 
4707
4885
  void Execute() override {
4886
+ CHECK_RIG_VALID();
4887
+
4708
4888
  result_code_ = rig_reset(hamlib_instance_->my_rig, reset_);
4709
4889
  if (result_code_ != RIG_OK) {
4710
4890
  error_message_ = rigerror(result_code_);
package/src/hamlib.h CHANGED
@@ -31,6 +31,7 @@ protected:
31
31
  class NodeHamLib : public Napi::ObjectWrap<NodeHamLib> {
32
32
  public:
33
33
  NodeHamLib(const Napi::CallbackInfo&);
34
+ ~NodeHamLib(); // Add destructor declaration
34
35
  Napi::Value Open(const Napi::CallbackInfo&);
35
36
  Napi::Value SetVFO(const Napi::CallbackInfo&);
36
37
  Napi::Value SetFrequency(const Napi::CallbackInfo&);