[go: up one dir, main page]

Menu

[073328]: / src / mem.c  Maximize  Restore  History

Download this file

1286 lines (1070 with data), 32.5 kB

   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
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
/** \addtogroup rig
* @{
*/
/**
* \file src/mem.c
* \brief Memory and channel interface
* \author Stephane Fillod
* \date 2000-2011
*
* Hamlib interface is a frontend implementing wrapper functions.
*
*/
/*
* Hamlib Interface - mem/channel calls
* Copyright (c) 2000-2011 by Stephane Fillod
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <hamlib/rig.h>
#ifndef DOC_HIDDEN
#define CHECK_RIG_ARG(r) (!(r) || !(r)->caps || !(r)->state.comm_state)
#endif /* !DOC_HIDDEN */
/**
* \brief set the current memory channel number
* \param rig The rig handle
* \param vfo The target VFO
* \param ch The memory channel number
*
* Sets the current memory channel number.
* It is not mandatory for the radio to be in memory mode. Actually
* it depends on rigs. YMMV.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_mem()
*/
int HAMLIB_API rig_set_mem(RIG *rig, vfo_t vfo, int ch)
{
const struct rig_caps *caps;
int retcode;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
caps = rig->caps;
if (caps->set_mem == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_PURE) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo)
return caps->set_mem(rig, vfo, ch);
if (!caps->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->set_mem(rig, vfo, ch);
caps->set_vfo(rig, curr_vfo);
return retcode;
}
/**
* \brief get the current memory channel number
* \param rig The rig handle
* \param vfo The target VFO
* \param ch The location where to store the current memory channel number
*
* Retrieves the current memory channel number.
* It is not mandatory for the radio to be in memory mode. Actually
* it depends on rigs. YMMV.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_mem()
*/
int HAMLIB_API rig_get_mem(RIG *rig, vfo_t vfo, int *ch)
{
const struct rig_caps *caps;
int retcode;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig) || !ch)
return -RIG_EINVAL;
caps = rig->caps;
if (caps->get_mem == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_PURE) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo)
return caps->get_mem(rig, vfo, ch);
if (!caps->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->get_mem(rig, vfo, ch);
caps->set_vfo(rig, curr_vfo);
return retcode;
}
/**
* \brief set the current memory bank
* \param rig The rig handle
* \param vfo The target VFO
* \param bank The memory bank
*
* Sets the current memory bank number.
* It is not mandatory for the radio to be in memory mode. Actually
* it depends on rigs. YMMV.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_mem()
*/
int HAMLIB_API rig_set_bank(RIG *rig, vfo_t vfo, int bank)
{
const struct rig_caps *caps;
int retcode;
vfo_t curr_vfo;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
caps = rig->caps;
if (caps->set_bank == NULL)
return -RIG_ENAVAIL;
if ((caps->targetable_vfo&RIG_TARGETABLE_PURE) ||
vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo)
return caps->set_bank(rig, vfo, bank);
if (!caps->set_vfo)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
retcode = caps->set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
retcode = caps->set_bank(rig, vfo, bank);
caps->set_vfo(rig, curr_vfo);
return retcode;
}
#ifndef DOC_HIDDEN
/*
* call on every ext_levels of a rig
*/
static int generic_retr_extl(RIG *rig, const struct confparams *cfp, rig_ptr_t ptr)
{
channel_t *chan = (channel_t *)ptr;
struct ext_list *p;
unsigned el_size = 0;
if (chan->ext_levels == NULL)
p = chan->ext_levels = malloc(2*sizeof(struct ext_list));
else {
for (p = chan->ext_levels; !RIG_IS_EXT_END(*p); p++)
el_size += sizeof(struct ext_list);
chan->ext_levels = realloc(chan->ext_levels,
el_size+sizeof(struct ext_list));
}
if (!chan->ext_levels) {
rig_debug(RIG_DEBUG_ERR, "%s:%d memory allocation error!\n",
__FUNCTION__, __LINE__);
return -RIG_ENOMEM;
}
p->token = cfp->token;
rig_get_ext_level(rig, RIG_VFO_CURR, p->token, &p->val);
p++;
p->token = 0; /* RIG_EXT_END */
return 1; /* process them all */
}
static const channel_cap_t mem_cap_all = {
.bank_num = 1,
.vfo = 1,
.ant = 1,
.freq = 1,
.mode = 1,
.width = 1,
.tx_freq = 1,
.tx_mode = 1,
.tx_width = 1,
.split = 1,
.tx_vfo = 1,
.rptr_shift = 1,
.rptr_offs = 1,
.tuning_step = 1,
.rit = 1,
.xit = 1,
.funcs = (setting_t)-1,
.levels = (setting_t)-1,
.ctcss_tone = 1,
.ctcss_sql = 1,
.dcs_code = 1,
.dcs_sql = 1,
.scan_group = 1,
.flags = 1,
.channel_desc = 1,
.ext_levels = 1,
};
static int rig_mem_caps_empty(const channel_cap_t *mem_cap)
{
return !(
mem_cap->bank_num ||
mem_cap->vfo ||
mem_cap->ant ||
mem_cap->freq ||
mem_cap->mode ||
mem_cap->width ||
mem_cap->tx_freq ||
mem_cap->tx_mode ||
mem_cap->tx_width ||
mem_cap->split ||
mem_cap->tx_vfo ||
mem_cap->rptr_shift ||
mem_cap->rptr_offs ||
mem_cap->tuning_step ||
mem_cap->rit ||
mem_cap->xit ||
mem_cap->funcs ||
mem_cap->levels ||
mem_cap->ctcss_tone ||
mem_cap->ctcss_sql ||
mem_cap->dcs_code ||
mem_cap->dcs_sql ||
mem_cap->scan_group ||
mem_cap->flags ||
mem_cap->channel_desc ||
mem_cap->ext_levels
);
}
/*
* stores current VFO state into chan by emulating rig_get_channel
*/
static int generic_save_channel(RIG *rig, channel_t *chan)
{
int i, retval;
int chan_num;
vfo_t vfo;
setting_t setting;
const channel_cap_t *mem_cap = NULL;
chan_num = chan->channel_num;
vfo = chan->vfo;
memset(chan, 0, sizeof(channel_t));
chan->channel_num = chan_num;
chan->vfo = vfo;
if (vfo == RIG_VFO_MEM)
{
const chan_t *chan_cap;
chan_cap = rig_lookup_mem_caps(rig, chan_num);
if (chan_cap) mem_cap = &chan_cap->mem_caps;
}
/* If vfo!=RIG_VFO_MEM or incomplete backend, try all properties */
if (mem_cap == NULL || rig_mem_caps_empty(mem_cap))
{
mem_cap = &mem_cap_all;
}
if (mem_cap->freq) {
retval = rig_get_freq(rig, RIG_VFO_CURR, &chan->freq);
/* empty channel ? */
if (retval == -RIG_ENAVAIL || chan->freq == RIG_FREQ_NONE)
return -RIG_ENAVAIL;
}
if (mem_cap->vfo)
rig_get_vfo(rig, &chan->vfo);
if (mem_cap->mode || mem_cap->width)
rig_get_mode(rig, RIG_VFO_CURR, &chan->mode, &chan->width);
chan->split = RIG_SPLIT_OFF;
if (mem_cap->split)
rig_get_split_vfo(rig, RIG_VFO_CURR, &chan->split, &chan->tx_vfo);
if (chan->split != RIG_SPLIT_OFF) {
if (mem_cap->tx_freq)
rig_get_split_freq(rig, RIG_VFO_CURR, &chan->tx_freq);
if (mem_cap->tx_mode || mem_cap->tx_width)
rig_get_split_mode(rig, RIG_VFO_CURR, &chan->tx_mode, &chan->tx_width);
} else {
chan->tx_freq = chan->freq;
chan->tx_mode = chan->mode;
chan->tx_width = chan->width;
}
if (mem_cap->rptr_shift)
rig_get_rptr_shift(rig, RIG_VFO_CURR, &chan->rptr_shift);
if (mem_cap->rptr_offs)
rig_get_rptr_offs(rig, RIG_VFO_CURR, &chan->rptr_offs);
if (mem_cap->ant)
rig_get_ant(rig, RIG_VFO_CURR, &chan->ant);
if (mem_cap->tuning_step)
rig_get_ts(rig, RIG_VFO_CURR, &chan->tuning_step);
if (mem_cap->rit)
rig_get_rit(rig, RIG_VFO_CURR, &chan->rit);
if (mem_cap->xit)
rig_get_xit(rig, RIG_VFO_CURR, &chan->xit);
for (i=0; i<RIG_SETTING_MAX; i++) {
setting = rig_idx2setting(i);
if ((setting & mem_cap->levels) && RIG_LEVEL_SET(setting))
rig_get_level(rig, RIG_VFO_CURR, setting, &chan->levels[i]);
}
for (i=0; i<RIG_SETTING_MAX; i++) {
int fstatus;
setting = rig_idx2setting(i);
if ((setting & mem_cap->funcs) &&
(rig_get_func(rig, RIG_VFO_CURR, setting, &fstatus) == RIG_OK))
chan->funcs |= fstatus ? setting : 0;
}
if (mem_cap->ctcss_tone)
rig_get_ctcss_tone(rig, RIG_VFO_CURR, &chan->ctcss_tone);
if (mem_cap->ctcss_sql)
rig_get_ctcss_sql(rig, RIG_VFO_CURR, &chan->ctcss_sql);
if (mem_cap->dcs_code)
rig_get_dcs_code(rig, RIG_VFO_CURR, &chan->dcs_code);
if (mem_cap->dcs_sql)
rig_get_dcs_sql(rig, RIG_VFO_CURR, &chan->dcs_sql);
/*
* TODO: (missing calls)
* - channel_desc
* - bank_num
* - scan_group
* - flags
*/
rig_ext_level_foreach(rig, generic_retr_extl, (rig_ptr_t)chan);
return RIG_OK;
}
/*
* Restores chan into current VFO state by emulating rig_set_channel
*/
static int generic_restore_channel(RIG *rig, const channel_t *chan)
{
int i;
struct ext_list *p;
setting_t setting;
const channel_cap_t *mem_cap = NULL;
if (chan->vfo == RIG_VFO_MEM)
{
const chan_t *chan_cap;
chan_cap = rig_lookup_mem_caps(rig, chan->channel_num);
if (chan_cap) mem_cap = &chan_cap->mem_caps;
}
/* If vfo!=RIG_VFO_MEM or incomplete backend, try all properties */
if (mem_cap == NULL || rig_mem_caps_empty(mem_cap))
{
mem_cap = &mem_cap_all;
}
rig_set_vfo(rig, chan->vfo);
if (mem_cap->freq)
rig_set_freq(rig, RIG_VFO_CURR, chan->freq);
if (mem_cap->mode || mem_cap->width)
rig_set_mode(rig, RIG_VFO_CURR, chan->mode, chan->width);
rig_set_split_vfo(rig, RIG_VFO_CURR, chan->split, chan->tx_vfo);
if (chan->split != RIG_SPLIT_OFF) {
if (mem_cap->tx_freq)
rig_set_split_freq(rig, RIG_VFO_CURR, chan->tx_freq);
if (mem_cap->tx_mode || mem_cap->tx_width)
rig_set_split_mode(rig, RIG_VFO_CURR, chan->tx_mode, chan->tx_width);
}
if (mem_cap->rptr_shift)
rig_set_rptr_shift(rig, RIG_VFO_CURR, chan->rptr_shift);
if (mem_cap->rptr_offs)
rig_set_rptr_offs(rig, RIG_VFO_CURR, chan->rptr_offs);
for (i=0; i<RIG_SETTING_MAX; i++) {
setting = rig_idx2setting(i);
if (setting & mem_cap->levels)
rig_set_level(rig, RIG_VFO_CURR, setting, chan->levels[i]);
}
if (mem_cap->ant)
rig_set_ant(rig, RIG_VFO_CURR, chan->ant);
if (mem_cap->tuning_step)
rig_set_ts(rig, RIG_VFO_CURR, chan->tuning_step);
if (mem_cap->rit)
rig_set_rit(rig, RIG_VFO_CURR, chan->rit);
if (mem_cap->xit)
rig_set_xit(rig, RIG_VFO_CURR, chan->xit);
for (i=0; i<RIG_SETTING_MAX; i++) {
setting = rig_idx2setting(i);
if (setting & mem_cap->funcs)
rig_set_func(rig, RIG_VFO_CURR, setting,
chan->funcs & rig_idx2setting(i));
}
if (mem_cap->ctcss_tone)
rig_set_ctcss_tone(rig, RIG_VFO_CURR, chan->ctcss_tone);
if (mem_cap->ctcss_sql)
rig_set_ctcss_sql(rig, RIG_VFO_CURR, chan->ctcss_sql);
if (mem_cap->dcs_code)
rig_set_dcs_code(rig, RIG_VFO_CURR, chan->dcs_code);
if (mem_cap->dcs_sql)
rig_set_dcs_sql(rig, RIG_VFO_CURR, chan->dcs_sql);
/*
* TODO: (missing calls)
* - channel_desc
* - bank_num
* - scan_group
* - flags
*/
for (p = chan->ext_levels; p && !RIG_IS_EXT_END(*p); p++)
rig_set_ext_level(rig, RIG_VFO_CURR, p->token, p->val);
return RIG_OK;
}
#endif /* !DOC_HIDDEN */
/**
* \brief set channel data
* \param rig The rig handle
* \param chan The location of data to set for this channel
*
* Sets the data associated with a channel. This channel can either
* be the state of a VFO specified by \a chan->vfo, or a memory channel
* specified with \a chan->vfo = RIG_VFO_MEM and \a chan->channel_num.
* See #channel_t for more information.
*
* The rig_set_channel is supposed to have no impact on the current VFO
* and memory number selected. Depending on backend and rig capabilities,
* the chan struct may not be set completely.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_channel()
*/
int HAMLIB_API rig_set_channel(RIG *rig, const channel_t *chan)
{
struct rig_caps *rc;
int curr_chan_num, get_mem_status = RIG_OK;
vfo_t curr_vfo;
vfo_t vfo; /* requested vfo */
int retcode;
int can_emulate_by_vfo_mem, can_emulate_by_vfo_op;
#ifdef PARANOID_CHANNEL_HANDLING
channel_t curr_chan;
#endif
if (CHECK_RIG_ARG(rig) || !chan)
return -RIG_EINVAL;
/*
* TODO: check validity of chan->channel_num
*/
rc = rig->caps;
if (rc->set_channel)
return rc->set_channel(rig, chan);
/*
* if not available, emulate it
* Optional: get_vfo, set_vfo,
*/
vfo = chan->vfo;
if (vfo == RIG_VFO_CURR)
return generic_restore_channel(rig, chan);
/* any emulation requires set_mem() */
if (vfo == RIG_VFO_MEM && !rc->set_mem)
return -RIG_ENAVAIL;
can_emulate_by_vfo_mem = rc->set_vfo &&
((rig->state.vfo_list & RIG_VFO_MEM) == RIG_VFO_MEM);
can_emulate_by_vfo_op = rc->vfo_op &&
rig_has_vfo_op(rig, RIG_OP_FROM_VFO);
if (!can_emulate_by_vfo_mem && !can_emulate_by_vfo_op)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
/* may be needed if the restore_channel has some side effects */
#ifdef PARANOID_CHANNEL_HANDLING
generic_save_channel(rig, &curr_chan);
#endif
if (vfo == RIG_VFO_MEM)
get_mem_status = rig_get_mem(rig, RIG_VFO_CURR, &curr_chan_num);
if (can_emulate_by_vfo_mem && curr_vfo != vfo) {
retcode = rig_set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
}
if (vfo == RIG_VFO_MEM)
rig_set_mem(rig, RIG_VFO_CURR, chan->channel_num);
retcode = generic_restore_channel(rig, chan);
if (!can_emulate_by_vfo_mem && can_emulate_by_vfo_op) {
retcode = rig_vfo_op(rig, RIG_VFO_CURR, RIG_OP_FROM_VFO);
if (retcode != RIG_OK)
return retcode;
}
/* restore current memory number */
if (vfo == RIG_VFO_MEM && get_mem_status == RIG_OK)
rig_set_mem(rig, RIG_VFO_CURR, curr_chan_num);
if (can_emulate_by_vfo_mem)
rig_set_vfo(rig, curr_vfo);
#ifdef PARANOID_CHANNEL_HANDLING
generic_restore_channel(rig, &curr_chan);
#endif
return retcode;
}
/**
* \brief get channel data
* \param rig The rig handle
* \param chan The location where to store the channel data
*
* Retrieves the data associated with a channel. This channel can either
* be the state of a VFO specified by \a chan->vfo, or a memory channel
* specified with \a chan->vfo = RIG_VFO_MEM and \a chan->channel_num.
* See #channel_t for more information.
*
* Example:
\code
channel_t chan;
int err;
chan->vfo = RIG_VFO_MEM;
chan->channel_num = 10;
err = rig_get_channel(rig, &chan);
if (err != RIG_OK)
error("get_channel failed: %s", rigerror(err));
\endcode
*
* The rig_get_channel is supposed to have no impact on the current VFO
* and memory number selected. Depending on backend and rig capabilities,
* the chan struct may not be filled in completely.
*
* Note: chan->ext_levels is a pointer to a newly mallocated memory.
* This is the responsability of the caller to manage and eventually
* free it.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_channel()
*/
int HAMLIB_API rig_get_channel(RIG *rig, channel_t *chan)
{
struct rig_caps *rc;
int curr_chan_num, get_mem_status = RIG_OK;
vfo_t curr_vfo;
vfo_t vfo; /* requested vfo */
int retcode;
int can_emulate_by_vfo_mem, can_emulate_by_vfo_op;
#ifdef PARANOID_CHANNEL_HANDLING
channel_t curr_chan;
#endif
if (CHECK_RIG_ARG(rig) || !chan)
return -RIG_EINVAL;
/*
* TODO: check validity of chan->channel_num
*/
rc = rig->caps;
if (rc->get_channel)
return rc->get_channel(rig, chan);
/*
* if not available, emulate it
* Optional: get_vfo, set_vfo
* TODO: check return codes
*/
vfo = chan->vfo;
if (vfo == RIG_VFO_CURR)
return generic_save_channel(rig, chan);
/* any emulation requires set_mem() */
if (vfo == RIG_VFO_MEM && !rc->set_mem)
return -RIG_ENAVAIL;
can_emulate_by_vfo_mem = rc->set_vfo &&
((rig->state.vfo_list & RIG_VFO_MEM) == RIG_VFO_MEM);
can_emulate_by_vfo_op = rc->vfo_op &&
rig_has_vfo_op(rig, RIG_OP_TO_VFO);
if (!can_emulate_by_vfo_mem && !can_emulate_by_vfo_op)
return -RIG_ENTARGET;
curr_vfo = rig->state.current_vfo;
/* may be needed if the restore_channel has some side effects */
#ifdef PARANOID_CHANNEL_HANDLING
generic_save_channel(rig, &curr_chan);
#endif
if (vfo == RIG_VFO_MEM)
get_mem_status = rig_get_mem(rig, RIG_VFO_CURR, &curr_chan_num);
if (can_emulate_by_vfo_mem && curr_vfo != vfo) {
retcode = rig_set_vfo(rig, vfo);
if (retcode != RIG_OK)
return retcode;
}
if (vfo == RIG_VFO_MEM)
rig_set_mem(rig, RIG_VFO_CURR, chan->channel_num);
if (!can_emulate_by_vfo_mem && can_emulate_by_vfo_op) {
retcode = rig_vfo_op(rig, RIG_VFO_CURR, RIG_OP_TO_VFO);
if (retcode != RIG_OK)
return retcode;
}
retcode = generic_save_channel(rig, chan);
/* restore current memory number */
if (vfo == RIG_VFO_MEM && get_mem_status == RIG_OK)
rig_set_mem(rig, RIG_VFO_CURR, curr_chan_num);
if (can_emulate_by_vfo_mem)
rig_set_vfo(rig, curr_vfo);
#ifdef PARANOID_CHANNEL_HANDLING
generic_restore_channel(rig, &curr_chan);
#endif
return retcode;
}
#ifndef DOC_HIDDEN
int get_chan_all_cb_generic (RIG *rig, chan_cb_t chan_cb, rig_ptr_t arg)
{
int i,j,retval;
chan_t *chan_list = rig->state.chan_list;
channel_t *chan;
for (i=0; !RIG_IS_CHAN_END(chan_list[i]) && i < CHANLSTSIZ; i++) {
/*
* setting chan to NULL means the application
* has to provide a struct where to store data
* future data for channel channel_num
*/
chan = NULL;
retval = chan_cb(rig, &chan, chan_list[i].start, chan_list, arg);
if (retval != RIG_OK)
return retval;
if (chan == NULL)
return -RIG_ENOMEM;
for (j = chan_list[i].start; j <= chan_list[i].end; j++) {
int chan_next;
chan->vfo = RIG_VFO_MEM;
chan->channel_num = j;
/*
* TODO: if doesn't have rc->get_channel, special generic
*/
retval = rig_get_channel(rig, chan);
if (retval == -RIG_ENAVAIL) {
/*
* empty channel
*
* Should it continue or call chan_cb with special arg?
*/
continue;
}
if (retval != RIG_OK)
return retval;
chan_next = j < chan_list[i].end ? j+1 : j;
chan_cb(rig, &chan, chan_next, chan_list, arg);
}
}
return RIG_OK;
}
int set_chan_all_cb_generic (RIG *rig, chan_cb_t chan_cb, rig_ptr_t arg)
{
int i,j,retval;
chan_t *chan_list = rig->state.chan_list;
channel_t *chan;
for (i=0; !RIG_IS_CHAN_END(chan_list[i]) && i < CHANLSTSIZ; i++) {
for (j = chan_list[i].start; j <= chan_list[i].end; j++) {
chan_cb(rig, &chan, j, chan_list, arg);
chan->vfo = RIG_VFO_MEM;
retval = rig_set_channel(rig, chan);
if (retval != RIG_OK)
return retval;
}
}
return RIG_OK;
}
struct map_all_s {
channel_t *chans;
const struct confparams *cfgps;
value_t *vals;
};
/*
* chan_cb_t to be used for non cb get/set_all
*/
static int map_chan (RIG *rig, channel_t **chan, int channel_num, const chan_t *chan_list, rig_ptr_t arg)
{
struct map_all_s *map_arg = (struct map_all_s*)arg;
/* TODO: check channel_num within start-end of chan_list */
*chan = &map_arg->chans[channel_num];
return RIG_OK;
}
#endif /* DOC_HIDDEN */
/**
* \brief set all channel data, by callback
* \param rig The rig handle
* \param chan_cb Pointer to a callback function to provide channel data
* \param arg Arbitrary argument passed back to \a chan_cb
*
* Write the data associated with a all the memory channels.
* This is the preferred method to support clonable rigs.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_chan_all(), rig_get_chan_all_cb()
*/
int HAMLIB_API rig_set_chan_all_cb (RIG *rig, chan_cb_t chan_cb, rig_ptr_t arg)
{
struct rig_caps *rc;
int retval;
if (CHECK_RIG_ARG(rig) || !chan_cb)
return -RIG_EINVAL;
rc = rig->caps;
if (rc->set_chan_all_cb)
return rc->set_chan_all_cb(rig, chan_cb, arg);
/* if not available, emulate it */
retval = set_chan_all_cb_generic (rig, chan_cb, arg);
return retval;
}
/**
* \brief get all channel data, by callback
* \param rig The rig handle
* \param chan_cb Pointer to a callback function to retrieve channel data
* \param arg Arbitrary argument passed back to \a chan_cb
*
* Retrieves the data associated with a all the memory channels.
* This is the preferred method to support clonable rigs.
*
* \a chan_cb is called first with no data in chan (chan equals NULL).
* This means the application has to provide a struct where to store
* future data for channel channel_num. If channel_num == chan->channel_num,
* the application does not need to provide a new allocated structure.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_chan_all(), rig_set_chan_all_cb()
*/
int HAMLIB_API rig_get_chan_all_cb (RIG *rig, chan_cb_t chan_cb, rig_ptr_t arg)
{
struct rig_caps *rc;
int retval;
if (CHECK_RIG_ARG(rig) || !chan_cb)
return -RIG_EINVAL;
rc = rig->caps;
if (rc->get_chan_all_cb)
return rc->get_chan_all_cb(rig, chan_cb, arg);
/* if not available, emulate it */
retval = get_chan_all_cb_generic (rig, chan_cb, arg);
return retval;
}
/**
* \brief set all channel data
* \param rig The rig handle
* \param chans The location of data to set for all channels
*
* Write the data associated with all the memory channels.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_set_chan_all_cb(), rig_get_chan_all()
*/
int HAMLIB_API rig_set_chan_all (RIG *rig, const channel_t chans[])
{
struct rig_caps *rc;
struct map_all_s map_arg;
int retval;
if (CHECK_RIG_ARG(rig) || !chans)
return -RIG_EINVAL;
rc = rig->caps;
map_arg.chans = (channel_t *) chans;
if (rc->set_chan_all_cb)
return rc->set_chan_all_cb(rig, map_chan, (rig_ptr_t)&map_arg);
/* if not available, emulate it */
retval = set_chan_all_cb_generic (rig, map_chan, (rig_ptr_t)&map_arg);
return retval;
}
/**
* \brief get all channel data
* \param rig The rig handle
* \param chans The location where to store all the channel data
*
* Retrieves the data associated with all the memory channels.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_chan_all_cb(), rig_set_chan_all()
*/
int HAMLIB_API rig_get_chan_all (RIG *rig, channel_t chans[])
{
struct rig_caps *rc;
struct map_all_s map_arg;
int retval;
if (CHECK_RIG_ARG(rig) || !chans)
return -RIG_EINVAL;
rc = rig->caps;
map_arg.chans = chans;
if (rc->get_chan_all_cb)
return rc->get_chan_all_cb(rig, map_chan, (rig_ptr_t)&map_arg);
/*
* if not available, emulate it
*
* TODO: save_current_state, restore_current_state
*/
retval = get_chan_all_cb_generic (rig, map_chan, (rig_ptr_t)&map_arg);
return retval;
}
int HAMLIB_API rig_copy_channel(RIG *rig, channel_t *dest, const channel_t *src)
{
struct ext_list *saved_ext_levels;
int i;
/* TODO: ext_levels[] of different sizes */
for (i=0; !RIG_IS_EXT_END(src->ext_levels[i]) &&
!RIG_IS_EXT_END(dest->ext_levels[i]); i++) {
dest->ext_levels[i] = src->ext_levels[i];
}
saved_ext_levels = dest->ext_levels;
memcpy(dest, src, sizeof(channel_t));
dest->ext_levels = saved_ext_levels;
return RIG_OK;
}
#ifndef DOC_HIDDEN
static int map_parm (RIG *rig, const struct confparams *cfgps, value_t *value,
rig_ptr_t arg)
{
return -RIG_ENIMPL;
}
int get_parm_all_cb_generic (RIG *rig, confval_cb_t parm_cb, rig_ptr_t cfgps,
rig_ptr_t vals)
{
return -RIG_ENIMPL;
}
int set_parm_all_cb_generic (RIG *rig, confval_cb_t parm_cb, rig_ptr_t cfgps,
rig_ptr_t vals)
{
return -RIG_ENIMPL;
}
#endif /* DOC_HIDDEN */
/**
* \brief set all channel and non-channel data by call-back
* \param rig The rig handle
* \param chan_cb The callback for channel data
* \param parm_cb The callback for non-channel(aka parm) data
* \param arg Cookie passed to \a chan_cb and \a parm_cb
*
* Writes the data associated with all the memory channels,
* and rigs memory parameters, by callback.
* This is the preferred method to support clonable rigs.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_mem_all_cb(), rig_set_mem_all()
* \todo finish coding and testing of mem_all functions
*/
int HAMLIB_API rig_set_mem_all_cb (RIG *rig, chan_cb_t chan_cb, confval_cb_t parm_cb, rig_ptr_t arg)
{
struct rig_caps *rc;
int retval;
if (CHECK_RIG_ARG(rig) || !chan_cb)
return -RIG_EINVAL;
rc = rig->caps;
if (rc->set_mem_all_cb)
return rc->set_mem_all_cb(rig, chan_cb, parm_cb, arg);
/* if not available, emulate it */
retval = rig_set_chan_all_cb (rig, chan_cb, arg);
if (retval != RIG_OK)
return retval;
#if 0
retval = rig_set_parm_all_cb (rig, parm_cb, arg);
if (retval != RIG_OK)
return retval;
#else
return -RIG_ENIMPL;
#endif
return retval;
}
/**
* \brief get all channel and non-channel data by call-back
* \param rig The rig handle
* \param chan_cb The callback for channel data
* \param parm_cb The callback for non-channel(aka parm) data
* \param arg Cookie passed to \a chan_cb and \a parm_cb
*
* Retrieves the data associated with all the memory channels,
* and rigs memory parameters, by callback.
* This is the preferred method to support clonable rigs.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_mem_all_cb(), rig_set_mem_all()
*
* \todo get all parm's
* \todo finish coding and testing of mem_all functions
*/
int HAMLIB_API rig_get_mem_all_cb (RIG *rig, chan_cb_t chan_cb, confval_cb_t parm_cb, rig_ptr_t arg)
{
struct rig_caps *rc;
int retval;
if (CHECK_RIG_ARG(rig) || !chan_cb)
return -RIG_EINVAL;
rc = rig->caps;
if (rc->get_mem_all_cb)
return rc->get_mem_all_cb(rig, chan_cb, parm_cb, arg);
/* if not available, emulate it */
retval = rig_get_chan_all_cb (rig, chan_cb, arg);
if (retval != RIG_OK)
return retval;
#if 0
retval = rig_get_parm_cb (rig, parm_cb, arg);
if (retval != RIG_OK)
return retval;
#else
return -RIG_ENIMPL;
#endif
return retval;
}
/**
* \brief set all channel and non-channel data
* \param rig The rig handle
* \param chans Channel data
* \param cfgps ??
* \param vals ??
*
* Writes the data associated with all the memory channels,
* and rigs memory parameters.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_mem_all(), rig_set_mem_all_cb()
*
* \todo set all parm's
* \todo finish coding and testing of mem_all functions
*/
int HAMLIB_API rig_set_mem_all (RIG *rig, const channel_t chans[], const struct confparams cfgps[], const value_t vals[])
{
struct rig_caps *rc;
int retval;
struct map_all_s mem_all_arg;
if (CHECK_RIG_ARG(rig) || !chans || !cfgps || !vals)
return -RIG_EINVAL;
rc = rig->caps;
mem_all_arg.chans = (channel_t *) chans;
mem_all_arg.cfgps = cfgps;
mem_all_arg.vals = (value_t *) vals;
if (rc->set_mem_all_cb)
return rc->set_mem_all_cb(rig, map_chan, map_parm,
(rig_ptr_t)&mem_all_arg);
/* if not available, emulate it */
retval = rig_set_chan_all (rig, chans);
if (retval != RIG_OK)
return retval;
#if 0
retval = rig_set_parm_all (rig, parms);
if (retval != RIG_OK)
return retval;
#else
return -RIG_ENIMPL;
#endif
return retval;
}
/**
* \brief get all channel and non-channel data
* \param rig The rig handle
* \param chans Array of channels where to store the data
* \param cfgps Array of config parameters to retrieve
* \param vals Array of values where to store the data
*
* Retrieves the data associated with all the memory channels,
* and rigs memory parameters.
* This is the preferred method to support clonable rigs.
*
* \return RIG_OK if the operation has been sucessful, otherwise
* a negative value if an error occured (in which case, cause is
* set appropriately).
*
* \sa rig_get_mem_all(), rig_set_mem_all_cb()
* \todo finish coding and testing of mem_all functions
*/
int HAMLIB_API rig_get_mem_all (RIG *rig, channel_t chans[], const struct confparams cfgps[], value_t vals[])
{
struct rig_caps *rc;
int retval;
struct map_all_s mem_all_arg;
if (CHECK_RIG_ARG(rig) || !chans || !cfgps || !vals)
return -RIG_EINVAL;
rc = rig->caps;
mem_all_arg.chans = chans;
mem_all_arg.cfgps = cfgps;
mem_all_arg.vals = vals;
if (rc->get_mem_all_cb)
return rc->get_mem_all_cb(rig, map_chan, map_parm,
(rig_ptr_t)&mem_all_arg);
/*
* if not available, emulate it
*
* TODO: save_current_state, restore_current_state
*/
retval = rig_get_chan_all (rig, chans);
if (retval != RIG_OK)
return retval;
retval = get_parm_all_cb_generic (rig, map_parm, (rig_ptr_t)cfgps,
(rig_ptr_t)vals);
return retval;
}
/**
* \brief lookup the memory type and capabilities
* \param rig The rig handle
* \param ch The memory channel number
*
* Lookup the memory type and capabilities associated with a channel number.
* If \a ch equals RIG_MEM_CAPS_ALL, then a union of all the mem_caps sets
* is returned (pointer to static memory).
*
* \return a pointer to a chan_t structure if the operation has been sucessful,
* otherwise a NULL pointer, most probably because of incorrect channel number
* or buggy backend.
*/
const chan_t * HAMLIB_API rig_lookup_mem_caps(RIG *rig, int ch)
{
chan_t *chan_list;
static chan_t chan_list_all;
int i, j;
if (CHECK_RIG_ARG(rig))
return NULL;
if (ch == RIG_MEM_CAPS_ALL)
{
memset (&chan_list_all, 0, sizeof(chan_list_all));
chan_list = rig->state.chan_list;
chan_list_all.start = chan_list[0].start;
chan_list_all.type = RIG_MTYPE_NONE; /* meaningless */
for (i=0; i<CHANLSTSIZ && !RIG_IS_CHAN_END(chan_list[i]); i++) {
unsigned char *p1, *p2;
p1=(unsigned char*)&chan_list_all.mem_caps;
p2=(unsigned char*)&chan_list[i].mem_caps;
/* It's kind of hackish, we just want to do update set with:
* chan_list_all.mem_caps |= chan_list[i].mem_caps
*/
for (j=0; j<sizeof(channel_cap_t); j++) {
p1[j] |= p2[j];
}
/* til the end, most probably meaningless */
chan_list_all.end = chan_list[i].end;
}
return &chan_list_all;
}
chan_list = rig->state.chan_list;
for (i=0; i<CHANLSTSIZ && !RIG_IS_CHAN_END(chan_list[i]); i++) {
if (ch >= chan_list[i].start && ch <= chan_list[i].end) {
return &chan_list[i];
}
}
return NULL;
}
/**
* \brief get memory channel count
* \param rig The rig handle
*
* Get the total memory channel count, computed from the rig caps
*
* \return the memory count
*/
int HAMLIB_API rig_mem_count(RIG *rig)
{
const chan_t *chan_list;
int i, count;
if (CHECK_RIG_ARG(rig))
return -RIG_EINVAL;
chan_list = rig->state.chan_list;
count = 0;
for (i=0; i<CHANLSTSIZ && !RIG_IS_CHAN_END(chan_list[i]); i++) {
count += chan_list[i].end - chan_list[i].start + 1;
}
return count;
}
/*! @} */