]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
updated rigctl.c
authorJohn Melton - G0ORX/N6LYT <john.d.melton@googlemail.com>
Wed, 7 Dec 2016 08:14:20 +0000 (08:14 +0000)
committerJohn Melton - G0ORX/N6LYT <john.d.melton@googlemail.com>
Wed, 7 Dec 2016 08:14:20 +0000 (08:14 +0000)
old_protocol.c
radio.h
rigctl.c

index 1a05fd14d5d0c4bbf2cd9dac270fdfc8d860ac46..d0ba48b3795c5aaa70c97e77f936561013eb41ce 100644 (file)
@@ -436,16 +436,8 @@ static void process_ozy_input_buffer(char  *buffer) {
     }
 
 
-    // extract the 63 samples
-    int iq_samples=63;
-    switch(RECEIVERS) {
-      case 1:
-        iq_samples=63;
-        break;
-      case 2:
-        iq_samples=36;
-        break;
-    }
+    int iq_samples = (512 -8) / ((RECEIVERS*6)+2);
+
     for(i=0;i<iq_samples;i++) {
 
       for(r=0;r<RECEIVERS;r++) {
diff --git a/radio.h b/radio.h
index f4d9a2b55885e1fb9c0ed810d7cc007f35d59cc6..c0f1c1c7d2fa7a991fbbb1c2fb28ec6389f92917 100644 (file)
--- a/radio.h
+++ b/radio.h
@@ -52,7 +52,7 @@ extern char property_path[];
 #define APOLLO 2
 
 // soecify how many receivers (only 1 or 2 for now)
-#define RECEIVERS 1
+#define RECEIVERS 2
 
 /*
 #define PA_DISABLED 0
index 6d1408c82faecf96ff1d416a878487447f5ef0d7..6292200492cb57031f7a1edb8dfcf92455c66d2f 100644 (file)
--- a/rigctl.c
+++ b/rigctl.c
-
-/* S-2000 emulation via TCP
- * Copyright (C) 2016 Steve Wilson <wevets@gmail.com>
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation, either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program 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
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
- * PiHPSDR RigCtl by Steve KA6S Oct 16 2016
- * 
- */
-#include <gtk/gtk.h>
-#include <gdk/gdk.h>
-#include <unistd.h>
-#include <semaphore.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "toolbar.h"
-#include "sliders.h"
-#include "rigctl.h"
-#include "radio.h"
-#include "channel.h"
-#include "filter.h"
-#include <string.h>
-#include "mode.h"
-#include "filter.h"
-#include "wdsp_init.h"
-#include "bandstack.h"
-#include "vfo.h"
-#include "sliders.h"
-#include <pthread.h>
-#include <wdsp.h>
-
-// IP stuff below
-#include<sys/socket.h>
-#include<arpa/inet.h> //inet_addr
-
-#undef RIGCTL_DEBUG
-
-// the port client will be connecting to
-#define TELNET_PORT 19090  // This is the HAMLIB port
-// max number of bytes we can get at once
-#define MAXDATASIZE 300
-
-int init_server ();
-
-int rigctlGetFilterLow();
-int rigctlGetFilterHigh();
-int rigctlSetFilterLow(int val);
-int rigctlSetFilterHigh(int val);
-int new_level;
-
-typedef struct com_list {
-     char  cmd_string[80];
-     struct com_list * next_ent; 
- } com_list_t;
-com_list_t  cmd_list;
-char cmd_save[80];
-
-char  cmd_input[MAXDATASIZE] ;
-char * wptr;
-com_list_t * work_input; 
-com_list_t * work_list; 
-com_list_t * follow_list; 
-com_list_t * com_head = NULL;
-com_list_t * cur_ent;
-int cmdlistcnt;
-int retcode = 1;
-char workvar[100];
-
-char command[80];
-char msg[80];
-char msg2[80];
-char * newf;
-
-FILE * TTYFILE;
-FILE * out;
-int  output;
-FILTER * band_filter;
-
-static pthread_t rigctl_thread_id;
-static void * rigctl (void * arg);
-
-// This stuff from the telnet server
-int sockfd, numbytes;
-char buf[MAXDATASIZE];
-struct hostent *he;
-
-// connector’s address information
-struct sockaddr_in their_addr;
-
-int socket_desc , client_sock , c , read_size;
-struct sockaddr_in server , client;
-char client_message[MAXDATASIZE];
-
-int freq_flag;  // Determines if we are in the middle of receiving frequency info
-
-int digl_offset = 0;
-int digl_pol = 0;
-int digu_offset = 0;
-int digu_pol = 0;
-double new_vol = 0;
-
-// Now my stuff
-//
-// Looks up entry INDEX_NUM in the command structure and
-// returns the command string
-//
-
-void send_resp (char * msg) {
-    #ifdef  RIGCTL_DEBUG
-        fprintf(stderr,"RIGCTL: RESP=%s\n",msg);
-    #endif
-    write(client_sock, msg, strlen(msg));
-}
-
-char * cmd_lookup (int index_num) {
-    com_list_t * lcl_p;
-    lcl_p = com_head;
-    if(index_num == 0)  {
-       return(lcl_p->cmd_string);
-    } else {
-      while((lcl_p->next_ent != (com_list_t * ) NULL) && (index_num != 0)) {
-          lcl_p = lcl_p->next_ent;
-          index_num--;
-      }
-    }
-    if(index_num == 0)  {
-      return(lcl_p->cmd_string);
-    } else {
-      return ((char *) NULL);
-    } 
-}
-
-static void * rigctl (void * arg) {
-   int work_int;     
-   int len;
-   init_server();
-   double meter;
-   int save_flag = 0; // Used to concatenate two cmd lines together
-
-  for(;;) {
-    fprintf(stderr,"RIGCTL - New run\n");
-    while((numbytes=recv(client_sock , cmd_input , 1000 , 0)) > 0 ) {
-       
-        if(cmd_input[numbytes-1] != ';') {  // Got here because the command line is short
-           cmd_input[numbytes] = '\0';      // Turn it into a C string
-           strcpy(cmd_save,cmd_input);      // And save a copy of it till next time through
-           save_flag = 1;
-           #ifdef RIGCTL_DEBUG
-           fprintf(stderr,"RIGCTL: CMD_SAVE=%s  LEN=%d\n",cmd_save,numbytes);
-           #endif
-        } else if(save_flag == 1) {
-           save_flag = 0;
-           cmd_input[numbytes] = '\0';      // Turn it into a C string
-           strcat(cmd_save,cmd_input);
-           strcpy(cmd_input,cmd_save);      // Cat them together and replace cmd_input
-           numbytes = strlen(cmd_input);
-           #ifdef RIGCTL_DEBUG
-            fprintf(stderr,"RIGCTL: CMD_INPUT=%s LEN=%d\n",cmd_input,numbytes);
-           #endif
-        } else {
-           #ifdef RIGCTL_DEBUG
-           fprintf(stderr,"RIGCTL: CMD_NORM=%s  NUMBYTES=%d\n",cmd_input,numbytes);
-           #endif
-        }
-
-        cmd_input[numbytes-1] = '\0';  // Turn it into a C string.
-        len = strlen(cmd_input);
-           #ifdef RIGCTL_DEBUG
-           fprintf(stderr,"RIGCTL: CMD_AFTER=%s  LEN=%d\n",cmd_input,len);
-           #endif
-        #ifdef RIGCTL_DEBUG
-        fprintf(stderr,"RIGCTL: RCVD=%s  LENGTH=%d\n",cmd_input,len);
-        #endif
-        // Parse the cmd_input
-        //int space = command.indexOf(' ');
-        //char cmd_char = com_head->cmd_string[0]; // Assume the command is first thing!
-        char cmd_str[3];
-        cmd_str[0] = cmd_input[0];
-        cmd_str[1] = cmd_input[1];
-        cmd_str[2] = '\0';
-        if(strcmp(cmd_str,"AC")==0)       { // Sets or reads the internal antenna tuner status
-                                                   // P1 0:RX-AT Thru, 1: RX-AT IN
-                                                   // P2 0: TX-AT Thru 1: TX-AT In
-                                                   // 
-                                             if(len <= 2) {
-                                               send_resp("AC000;");
-                                             }
-                                          }
-        else if(strcmp(cmd_str,"AG")==0) {  // Set Audio Gain - 
-                                            // AG123; Value of 0-
-                                            // AG1 = Sub receiver - what is the value set
-                                            // Response - AG<0/1>123; Where 123 is 0-260
-                                            if(len>4) { // Set Audio Gain
-                                               volume = (double) atoi(&cmd_input[3])/260; 
-                                               g_idle_add(update_af_gain,NULL);               
-                                            } else { // Read Audio Gain
-                                              sprintf(msg,"AG0%03d;",2.6 * volume);
-                                              send_resp(msg);
-                                              #ifdef RIGCTL_DEBUG
-                                              fprintf(stderr,":%s\n",msg);
-                                              #endif
-                                            }
-                                          }
-        else if(strcmp(cmd_str,"AI")==0)  {  // Allow rebroadcast of set frequency after set - not supported
-                                             if(len <=2) {
-                                                send_resp("AI0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"AL")==0)  {  // Set/Reads the auto Notch level 
-                                             if(len <=2) {
-                                                send_resp("AL000;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"AM")==0)  {  // Sets or reads the Auto Mode
-                                             if(len <=2) {
-                                                send_resp("AM0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"AN")==0)  {  // Selects the antenna connector (ANT1/2)
-                                             if(len <=2) {
-                                                send_resp("AN0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"AR")==0)  {  // Sets or reads the ASC function on/off
-                                             if(len <=2) {
-                                                send_resp("AR0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"AS")==0)  {  // Sets/reads automode function parameters
-                                             // AS<P1><2xP2><11P3><P4>;
-                                             // AS<P1><2xP2><11P3><P4>;
-                                             if(len < 6) {  
-                                                sprintf(msg,"AS%1d%02d%011d%01d;",
-                                                             0, // P1
-                                                             0, // Automode 
-                                                             getFrequency(),
-                                                             rigctlGetMode());
-                                                send_resp(msg);
-                               
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"BC")==0)  {  // Beat Cancellor OFF
-                                             if(len <=2) {
-                                                send_resp("BC0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"BD")==0)  {  // Moves down the frequency band
-                                             // No response 
-                                          }
-        else if(strcmp(cmd_str,"BP")==0)  {  // Reads the manual beat canceller frequency setting
-                                             if(len <=2) {
-                                                send_resp("BP000;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"BS")==0)  {  // Sets or reads Beat Canceller status
-                                             if(len <=2) {
-                                                send_resp("BS0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"BU")==0)  {  // Moves Up the frequency band
-                                             // No response 
-                                          }
-        else if(strcmp(cmd_str,"BY")==0)  {  // Read the busy signal status
-                                             if(len <=2) {
-                                                send_resp("BY00;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"CA")==0)  {  // Sets/reads cw auto tune function status
-                                             if(len <=2) {
-                                                send_resp("CA0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"CG")==0)  {  // Sets/reads the carrier gain status
-                                             // 0-100 legal values
-                                             if(len <=2) {
-                                                send_resp("CG000;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"CH")==0)  {  // Sets the current frequency to call Channel
-                                             // No response
-                                          }
-        else if(strcmp(cmd_str,"CI")==0)  {  // While in VFO mode or memory recall mode, sets freq to the call channel
-                                             // No response
-                                          }
-        else if(strcmp(cmd_str,"CM")==0)  {  // Sets or reads the packet cluster tune f(x) on/off
-                                             // 0-100 legal values
-                                             if(len <=2) {
-                                                send_resp("CM0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"CN")==0)  {  // Sets and reads CTSS function - 01-38 legal values
-                                             if(len <=2) {
-                                                send_resp("CN00;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"CT")==0)  {  // Sets and reads CTSS function status
-                                             if(len <=2) {
-                                                send_resp("CT0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"DC")==0)  {  // Sets/Reads TX band status
-                                             if(len <=2) {
-                                                send_resp("DC00;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"DN")==0)  {  // Emulate Mic down key 
-                                          }
-        else if(strcmp(cmd_str,"DQ")==0)  {  // ETs/and reads the DCS function status
-                                             if(len <=2) {
-                                                send_resp("DQ0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"EX")==0)  {  // Set/reads the extension menu
-                                             if(len <=12) {
-                                                send_resp("EX00000000000000000000000;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"FA")==0) {  // VFO A frequency
-                                            // LEN=7 - set frequency
-                                            // Next data will be rest of freq
-                                            if(len == 13) { //We are receiving freq info
-                                               long long new_freqA = atoll(&cmd_input[2]);
-                                               setFrequency(new_freqA);
-                                               g_idle_add(vfo_update,NULL);
-                                            } else {
-                                               if(len==2) {
-                                                  sprintf(msg,"FA%011d;",getFrequency());
-                                                  send_resp(msg);
-                                               }
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"FB")==0) {  // VFO B frequency
-                                            if(len==13) { //We are receiving freq info
-                                               long long new_freqA = atoll(&cmd_input[2]); 
-                                               setFrequency(new_freqA);
-                                               g_idle_add(vfo_update,NULL);
-                                            } else if(len == 2) {
-                                               sprintf(msg,"FB%011d;",getFrequency());
-                                               send_resp(msg);
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"FC")==0) {  // Set Sub receiver freq
-                                            // LEN=7 - set frequency
-                                            // Next data will be rest of freq
-                                            // Len<7 - frequency?
-                                            if(len>4) { //We are receiving freq info
-                                               long long new_freqA = atoll(&cmd_input[2]);              
-                                               //setFrequency(new_freqA);
-                                            } else {
-                                               sprintf(msg,"FC%011d;",getFrequency());
-/*
-                                               send_resp(msg);
-*/
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"FD")==0)  {  // Read the filter display dot pattern
-                                              send_resp("FD00000000;");
-                                          }
-        else if(strcmp(cmd_str,"FR")==0)  {  // Set/reads the extension menu
-                                             if(len <=2) {
-                                                send_resp("FR0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"FS")==0)  {  // Set/reads fine funct status
-                                             if(len <=2) {
-                                                send_resp("FS0;");
-                                             } 
-                                          }
-        else if(strcmp(cmd_str,"FT")==0)  { // Sel or reads the transmitters VFO, M, ch or Call comm   
-                                             if(len <=2) {
-                                                send_resp("FT0;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"FW")==0)  { // Sets/Reas DSP receive filter width in hz 0-9999hz 
-                                             if(len <=2) {
-                                                send_resp("FW0000;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"GT")==0)  { // Sets/Reas AGC constant status
-                                             if(len <=2) {
-                                                send_resp("GT000;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"ID")==0)  { // Read ID - Default to TS-2000 which is type 019.
-                                            sprintf(msg,"ID019;");
-                                            send_resp(msg);
-                                          }
-        else if(strcmp(cmd_str,"IF")==0) {  // Reads Transceiver status
-                                            // IFFFFFFFFFFFF - Frequency 
-                                            //   OFFS        - 4 chars Offset in powers of 10
-                                            //   RITRIT      - 6 chars RIT/XIT Frequency - Not supported =0
-                                            //   R           - 1 char  RIT Status = 0 is off
-                                            //   X           - 1 char  XIT Status = 0 is off
-                                            //   0           - 1 char  Channel Bank number - not used
-                                            //   00          - 2 char  Channel Bank number - not used
-                                            //   C           - 1 char  Mox Status 0=off, 1=on
-                                            //   M           - 1 char Operating mode - align with MD commands
-                                            //   V           - 1 char VFO Split status - not supported  
-                                            //   0           - 1 char Scan status - not supported
-                                            //   A           - 1 char - same as FT command
-                                            //   0           - 1 char - CTCSS tone - not used
-                                            //   00          - 2 char - More tone control
-                                            //   0           - 1 char - Shift status
-                                            sprintf(msg,"IF%011d%04d%06d%1d%1d%1d%02d%01d%01d%01d%01d%01d%01d%02d%01d;",
-                                                         getFrequency(),
-                                                         0,  // Shift Offset
-                                                         0,  // Rit Freq
-                                                         0,  // Rit Status
-                                                         0,  // XIT Status
-                                                         0,  // Channel Bank num
-                                                         0,  // Channel Bank num
-                                                         0,  // Mox Status
-                                                         rigctlGetMode(),  // Mode Status - same as MD Command
-                                                         0,  // VFO Status
-                                                         0,  // Scan status
-                                                         0,  // FT command 
-                                                         0,  // CTSS Tone
-                                                         0,  // More tone control
-                                                         0); // Shift status
-                                            send_resp(msg);
-                                         }
-        else if(strcmp(cmd_str,"IS")==0)  { // Sets/Reas IF shift funct status
-                                             if(len <=2) {
-                                                send_resp("IS00000;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"KS")==0)  { // Sets/Reads keying freq - 0-060 max
-                                             if(len <=2) {
-                                                send_resp("KS000;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"KY")==0)  { // Convert char to morse code
-                                             if(len <=2) {
-                                                send_resp("KY0;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"LK")==0)  { // Set/read key lock function status
-                                             if(len <=2) {
-                                                send_resp("LK00;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"LM")==0)  { // Set/read DRU 3A unit or elect keyer recording status
-                                             if(len <=2) {
-                                                send_resp("LM0;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"LT")==0)  { // Set/read Alt function
-                                             if(len <=2) {
-                                                send_resp("LT0;");
-                                             } 
-                                          }  
-        else if(strcmp(cmd_str,"MC")==0) {  // Recalls or reads memory channel
-                                             if(len <=2) {
-                                               send_resp("MC000;"); // Link this to band stack at some point
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"MD")==0) {  // Mode - digit selects mode
-                                            // 1 = LSB
-                                            // 2 = USB
-                                            // 3 = CWU
-                                            // 4 = FM
-                                            // 5 = AM
-                                            // 6 = DIGL
-                                            // 7 = CWL
-                                            // 9 = DIGU 
-                                            int new_mode;
-                                            if(len > 2) { // Set Mode
-                                               switch(atoi(&cmd_input[2])) {   
-                                                 case 1:  
-                                                     new_mode = modeLSB;
-                                                     break;
-                                                 case 2:  
-                                                     new_mode = modeUSB;
-                                                     break;
-                                                 case 3:  
-                                                     new_mode = modeCWU;
-                                                     break;
-                                                 case 4:  
-                                                     new_mode = modeFMN;
-                                                     break;
-                                                 case 5:  
-                                                     new_mode = modeAM;
-                                                     break;
-                                                 case 6:  
-                                                     new_mode = modeDIGL;
-                                                     break;
-                                                 case 7:  
-                                                     new_mode = modeCWL;
-                                                     break;
-                                                 case 9:  
-                                                     new_mode = modeDIGU;
-                                                     break;
-                                                 default:
-                                                     break;
-                                                     #ifdef RIGCTL_DEBUG
-                                                     fprintf(stderr,"MD command Unknown\n");
-                                                     #endif
-                                               }
-                                            // Other stuff to switch modes goes here..
-                                            // since new_mode has the interpreted command in 
-                                            // in it now.
-                                            BANDSTACK_ENTRY *entry;
-                                            entry= (BANDSTACK_ENTRY *) 
-                                                  bandstack_entry_get_current();
-                                            entry->mode=new_mode;
-                                            //  BUG - kills the system when there is some
-                                            // GPIO activity and Mode sets occur. Used twittling the
-                                           // frequency along with setting mode between USB/LSB with
-                                           // flrig. Tried doing the g_idle_add trick - but don't know the
-                                           // the magic to get that to compile without warnings 
-                                            setMode(entry->mode);
-                                            // Moved the vfo_update down after filter updated. (John G0ORX)
-                                            //g_idle_add(vfo_update,NULL);
-                                            
-                                            FILTER* band_filters=filters[entry->mode];
-                                            FILTER* band_filter=&band_filters[entry->filter];
-                                            setFilter(band_filter->low,band_filter->high);
-                                            /* Need a way to update VFO info here..*/
-                                            g_idle_add(vfo_update,NULL);
-                                            }  else {     // Read Mode
-                                               int curr_mode;
-                                               switch (mode) {
-                                                  case modeLSB: curr_mode = 1; 
-                                                          break;   
-                                                  case modeUSB: curr_mode = 2;
-                                                          break;
-                                                  case modeCWL: curr_mode = 7; // CWL
-                                                          break;
-                                                  case modeCWU: curr_mode = 3; // CWU
-                                                          break;
-                                                  case modeFMN: curr_mode = 4; // FMN
-                                                          break;
-                                                  case modeAM: curr_mode = 5; // AM
-                                                          break;
-                                                  case modeDIGU: curr_mode = 9; // DIGU
-                                                          break;
-                                                  case modeDIGL: curr_mode = 6; // DIGL
-                                                          break;
-                                                  default: 
-                                                          #ifdef RIGCTL_DEBUG
-                                                            fprintf(stderr,
-                                                            "RIGCTL: MD command doesn't support %d\n",
-                                                            mode);
-                                                          #endif
-                                                          break;
-                                               }
-                                               sprintf(msg,"MD%1d;",curr_mode);
-                                               send_resp(msg);
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"MF")==0) {  // Set/read menu A/B
-                                             if(len <=2) {
-                                               send_resp("MF0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"MG")==0) {  // Mike Gain - 3 digit value 
-                                            if(len <=2) {
-                                               work_int = (int) ((mic_gain +10.0)* 100.0/60.0);
-                                               sprintf(msg,"MG%03d",work_int);
-                                               send_resp(msg);
-                                            } else {
-                                               int tval = atoi(&cmd_input[2]);                
-                                               new_vol = (double) (tval * 60/100) - 10; 
-                                               //set_mic_gain(new_vol); 
-                                               double *p_mic_gain=malloc(sizeof(double));
-                                               *p_mic_gain=new_vol;
-                                               g_idle_add(update_mic_gain,(void *)p_mic_gain);
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"ML")==0) {  // Set/read the monitor function level
-                                             if(len <=2) {
-                                               send_resp("ML000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"MO")==0) {  // Set Monitor on/off
-                                             if(len <=2) {
-                                               send_resp("MO0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"MR")==0) {  // Read Memory Channel data
-                                             sprintf(msg,"MR%1d%02d%02d%011d%1d%1d%1d%02d%02d%03d%1d%1d%09d%02d%1d%08d;",
-                                                      0, // P1 - Rx Freq - 1 Tx Freq
-                                                      0, // P2 Bankd and channel number -- see MC comment
-                                                      0, // P3 - see MC comment 
-                                                      getFrequency(), // P4 - frequency
-                                                      rigctlGetMode(), // P5 - Mode
-                                                      0, // P6 - Locked status
-                                                      0, // P7 - O-off, 1-tone, 2-CTCSS, 3 =DCS
-                                                      0, // P8 - Tone Numbe3r - see page 35
-                                                      0, // P9 - CTCSS tone number - see CN command
-                                                      0, // P10 - DCS code - see QC command 
-                                                      0, // P11 - Reverse status
-                                                      0, // P12 - Shift status - see OS command
-                                                      0, // P13 - Offset freq - see OS command
-                                                      0, // P14 - Step Size - see ST command
-                                                      0, // P15 - Memory Group Number (0-9)
-                                                      0);  // P16 - Memory Name - 8 char max
-                                               send_resp(msg);
-                                         }
-        else if(strcmp(cmd_str,"MU")==0) {  // Set/Read Memory Group Data
-                                             if(len <=2) {
-                                               send_resp("MU0000000000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"MW")==0) {  // Store Data to Memory Channel
-                                         }
-        else if(strcmp(cmd_str,"NB")==0) {  // Set/Read Noise Blanker func status (on/off)
-                                             if(len <=2) {
-                                               send_resp("NB0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"NL")==0) {  // Set/Read Noise Reduction  Level
-                                             if(len <=2) {
-                                               send_resp("NL000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"NR")==0) {  // Set/Read Noise Reduction function status
-                                             if(len <=2) {
-                                               send_resp("NR0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"NT")==0) {  // Set/Read autonotch function
-                                             if(len <=2) {
-                                               send_resp("NT0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"OF")==0) {  // Set/Read Offset freq (9 digits - hz)
-                                             if(len <=2) {
-                                               send_resp("OF000000000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"OI")==0) {  // Read Memory Channel Data
-                                             if(len <=2) {
-                                               sprintf(msg,"OI%011d%04d%06d%1d%1d%1d%02d%1d%1d%1d%1d%1d%1d%02d%1d;",
-                                                  getFrequency(),
-                                                  0, // P2 - Freq Step size
-                                                  0, // P3 - Rit/Xit Freq 
-                                                  0, // P4 - RIT off/Rit On
-                                                  0, // P5 - XIT off/on
-                                                  0, // P6 - Channel
-                                                  0, // P7 - Bank
-                                                  0, // P8 - 0RX, 1TX
-                                                  rigctlGetMode(),  // P10 - MD
-                                                  0, // P11 - SC command
-                                                  0, // P12 Split op - SP command
-                                                  0, // P13 Off, 1, 2, 
-                                                  0, // P14 Tone freq - See TN command
-                                                  0);
-                                               send_resp(msg);
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"OS")==0) {  // Set/Read Offset freq (9 digits - hz)
-                                             if(len <=2) {
-                                               send_resp("OS0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PA")==0) {  // Set/Read Preamp function status
-                                             if(len <=2) {
-                                               send_resp("PA00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PB")==0) {  // Set/Read DRU-3A Playback status
-                                             if(len <=2) {
-                                               send_resp("PB0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PC")==0) {  // Set/Read Drive Power output
-                                            if(len<=2) {
-                                              sprintf(msg,"PC%03d;",(int) drive);
-                                              send_resp(msg); 
-                                            } else {
-                                               // Power Control - 3 digit number- 0 to 100
-                                               //Scales to 0.00-1.00
-                                                
-                                               double drive_val = 
-                                                   (double)(atoi(&cmd_input[2])); 
-                                               // setDrive(drive_val);
-                                               double *p_drive=malloc(sizeof(double));
-                                               *p_drive=drive_val;
-                                               g_idle_add(update_drive,(gpointer)p_drive);
-                                               //set_drive(drive_val);
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"PI")==0) {  // STore the programmable memory channel
-                                         }
-        else if(strcmp(cmd_str,"PK")==0) {  // Reads the packet cluster data
-                                               send_resp("PK000000000000000000000000000000000000000000000000;"); 
-                                         }
-        else if(strcmp(cmd_str,"PL")==0) {  // Set/Read Speech processor level
-                                            // P1 000 - min-100 max
-                                            // P2 000 - min - 100 max
-                                             if(len <=2) {
-                                               send_resp("PL000000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PM")==0) {  // Recalls the Programmable memory
-                                             if(len <=2) {
-                                               send_resp("PM0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PR")==0) {  // Sets/reads the speech processor function on/off
-                                            // 0 - off, 1=on
-                                             if(len <=2) {
-                                               send_resp("PR0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PS")==0) {  // Sets/reads Power on/off state
-                                            // 0 - off, 1=on
-                                             if(len <=2) {
-                                               send_resp("PS1;"); // Lets pretend we're powered up ;-) 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"PS")==0) {  // Sets/reads DCS code
-                                            // Codes numbered from 000 to 103.
-                                             if(len <=2) {
-                                               send_resp("QC000;"); // Lets pretend we're powered up ;-) 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"QI")==0) {  // Store the settings in quick memory
-                                         }
-        else if(strcmp(cmd_str,"QR")==0) {  // Send the Quick memory channel data
-                                            // P1 - Quick mem off, 1 quick mem on
-                                            // P2 - Quick mem channel number
-                                             if(len <=2) {
-                                               send_resp("QR00;"); // Lets pretend we're powered up ;-) 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RA")==0) {  // Sets/reads Attenuator function status
-                                            // 00-off, 1-99 -on - Main and sub receivers reported
-                                             if(len <=2) {
-                                               send_resp("RA0000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RC")==0) {  // Clears the RIT offset freq
-                                         }
-        else if(strcmp(cmd_str,"RD")==0) {  // Move the RIT offset freq down, slow down the scan speed in scan mode
-                                             if(len <=2) {
-                                               send_resp("RD0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RG")==0) {  // RF Gain - 3 digit number
-                                            // RG123; 0-255 value
-                                            // Scale from -20 - 120
-                                            if(len>4) { // Set Audio Gain
-                                               int base_value = atoi(&cmd_input[2]);
-                                               double new_gain = ((((double) base_value)/255) * 140) - 20; 
-                                               //set_agc_gain(new_gain);               
-                                               double *p_gain=malloc(sizeof(double));
-                                               *p_gain=new_gain;
-                                               g_idle_add(update_agc_gain,(gpointer)p_gain);
-                                            } else { // Read Audio Gain
-                                              sprintf(msg,"RG%03d;",((256 * (int) agc_gain)/140)+36);
-                                              send_resp(msg);
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"RL")==0) {  // Set/read Noise reduction level
-                                             if(len <=2) {
-                                               send_resp("RL00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RM")==0) {  // Set/read Meter function
-                                            // P1- 0, unsel, 1 SWR, 2 Comp, 3 ALC
-                                            // P2 - 4 dig - Meter value in dots - 000-0030
-                                             if(len <=2) {
-                                               send_resp("RM00000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RT")==0) {  // Set/read the RIT function status
-                                             if(len <=2) {
-                                               send_resp("RT0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RU")==0) {  // Set/move RIT frequency up
-                                             if(len <=2) {
-                                               send_resp("RU0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"RX")==0) {  // Unkey Xmitter
-                                            setMox(0);
-                                            // 0-main, 1=sub
-                                             if(len <=2) {
-                                               send_resp("RX0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SA")==0) {  // Set/reads satellite mode status
-                                            // 0-main, 1=sub
-                                             if(len <=2) {
-                                               send_resp("SA000000000000000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SB")==0) {  // Set/read the SUB TF-W status
-                                             if(len <=2) {
-                                               send_resp("SB0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SC")==0) {  // Set/read the Scan function status
-                                             if(len <=2) {
-                                               send_resp("SC0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SD")==0) {  // Set/read CW break-in time delay
-                                            // 0000-1000 ms (in steps of 50 ms) 0000 is full break in
-                                             if(len <=2) {
-                                               send_resp("SD0000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SH")==0) {  // Set/read the DSP filter settings
-                                             if(len <=2) {
-                                               send_resp("SH00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SI")==0) {  // Enters the satellite memory name
-                                         }
-        else if(strcmp(cmd_str,"SL")==0) {  // Set/read the DSP filter settings - this appears twice? See SH
-                                             if(len <=2) {
-                                               send_resp("SL00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SM")==0) {  // Read SMETER
-                                            // SMx;  x=0 RX1, x=1 RX2 
-                                            // meter is in dbm - value will be 0<260
-                                            // Translate to 00-30 for main, 0-15 fo sub
-                                            // Resp= SMxAABB; 
-                                            // 
-                                            double level;
-                                            level = GetRXAMeter(CHANNEL_RX0, smeter); 
-                                            level = level + (double) get_attenuation();
-                                            new_level = (int) level;            
-                                            #ifdef RIGCTL_DEBUG
-                                            fprintf(stderr,"RIGCTL: SM level=%.21f new_level=%d\n",
-                                                        level,new_level);
-                                            #endif
-                                            sprintf(msg,"SM%1c%04d;",
-                                                cmd_input[2],new_level);
-                                            send_resp(msg);
-                                         }   
-        else if(strcmp(cmd_str,"SQ")==0) {  // Set/read the squelch level
-                                            // P1 - 0- main, 1=sub
-                                            // P2 - 0-255
-                                             if(len <=2) {
-                                               send_resp("SQ0000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SR")==0) {  // Resets the transceiver
-                                         }
-        else if(strcmp(cmd_str,"SS")==0) {  // Set/read Scan pause freq
-                                             if(len <=2) {
-                                               send_resp("SS0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"ST")==0) {  // Set/read the multi/ch control freq steps
-                                             if(len <=2) {
-                                               send_resp("ST00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SU")==0) {  // Set/read the scan pause freq
-                                             if(len <=2) {
-                                               send_resp("SU00000000000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"SV")==0) {  // Execute the memory transfer function
-                                         }
-        else if(strcmp(cmd_str,"TC")==0) {  // Set/read the internal TNC mode
-                                             if(len <=2) {
-                                               send_resp("TC00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"TD")==0) {  // Sends the DTMF memory channel
-                                         }
-        else if(strcmp(cmd_str,"TI")==0) {  // Reads the TNC LED status
-                                             if(len <=2) {
-                                               send_resp("TI00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"TN")==0) {  // Set/Read sub tone freq
-                                             if(len <=2) {
-                                               send_resp("TN00;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"TO")==0) {  // Set/Read tone function
-                                             if(len <=2) {
-                                               send_resp("TO0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"TS")==0) {  // Set/Read TF Set function status
-                                             if(len <=2) {
-                                               send_resp("TS0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"TX")==0) {  // Key Xmitter - P1 - transmit on main/sub freq
-                                            setMox(1);
-                                             if(len <=2) {
-                                               send_resp("TS0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"TY")==0) {  // Set/Read uP firmware type
-                                             if(len <=2) {
-                                               send_resp("TY000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"UL")==0) {  // Detects the PLL unlock status - this should never occur - xmit only
-                                             if(len <=2) {
-                                               send_resp("UL0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"UP")==0) {  // Emulates the mic up key
-                                         }
-        else if(strcmp(cmd_str,"VD")==0) {  // Sets/Reads VOX dleay time - 0000-3000ms in steps of 150
-                                             if(len <=2) {
-                                               send_resp("VD0000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"VG")==0) {  // Sets/Reads VOX gain 000-009
-                                             if(len <=2) {
-                                               send_resp("VG000;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"VG")==0) {  // Emulates the voide 1/2 key
-                                         }
-        else if(strcmp(cmd_str,"VX")==0) {  // Sets/reads vox f(x) state
-                                             if(len <=2) {
-                                               send_resp("VX0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"XT")==0) {  // Sets/reads the XIT f(x) state
-                                             if(len <=2) {
-                                               send_resp("XT0;"); 
-                                             }
-                                         }
-        else if(strcmp(cmd_str,"XX")==0) {  // 
-                                            // Format RL01234: First dig 0=neg, 1=pos number
-                                            //                 1-4- digital offset in hertz.
-                                            if(len > 4) { // It is set instead of a read
-                                               digl_pol = (cmd_input[2]=='0') ? -1 : 1;
-                                               digl_offset = atoi(&cmd_input[3]); 
-                                               #ifdef RIGCTL_DEBUG
-                                               fprintf(stderr,"RIGCTL:RL set %d %d\n",digl_pol,digl_offset); 
-                                               #endif
-                                            } else {
-                                               if(digl_pol==1) { // Nah - its a read
-                                                 sprintf(msg,"RL1%04d;");
-                                               } else {
-                                                 sprintf(msg,"RL0%04d;");
-                                               }         
-                                               send_resp(msg);
-                                               #ifdef RIGCTL_DEBUG
-                                               fprintf(stderr,":%s\n",msg);
-                                               #endif
-                                            }
-                                         }
-        else if(strcmp(cmd_str,"XY")==0) {  // set/read RTTY DIGL offset frequency - Not available - just store values
-                                            // Format RL01234: First dig 0=neg, 1=pos number
-                                            //                 1-4- digital offset in hertz.
-                                            if(len > 4) { // It is set instead of a read
-                                               digl_pol = (cmd_input[2]=='0') ? -1 : 1;
-                                               digl_offset = atoi(&cmd_input[3]); 
-                                               #ifdef RIGCTL_DEBUG
-                                               fprintf(stderr,"RIGCTL:RL set %d %d\n",digl_pol,digl_offset); 
-                                               #endif
-                                            } else {
-                                               if(digl_pol==1) { // Nah - its a read
-                                                 sprintf(msg,"RL1%04d;");
-                                               } else {
-                                                 sprintf(msg,"RL0%04d;");
-                                               }         
-                                               send_resp(msg);
-                                               #ifdef RIGCTL_DEBUG
-                                               fprintf(stderr,":%s\n",msg);
-                                               #endif
-                                            }
-                                         }
-        else                             {
-                                            fprintf(stderr,"RIGCTL: UNKNOWN\n");
-                                         }
-   } 
-    // Got here because pipe closed 
-   if(numbytes == 0) {
-         fprintf(stderr,"RIGCTL: Client disconnected\n");
-         close(client_sock);
-         sleep(1);
-         client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
-   }
- }
-}
-
-char * rigctlGetFilter()
-{
-   
-    char * m = mode_string[mode];
-
-    if (strcmp(m,"CWU") == 0){
-       return (char *) (getFilterHigh() + getFilterLow());
-    }
-    else
-    if (strcmp(m,"CWL") == 0){
-       return (char *) (getFilterHigh() + getFilterLow());
-    }
-    else
-    return (char *) (getFilterHigh() - getFilterLow());
-}
-
-
-void launch_rigctl () {
-   int err;
-   fprintf(stderr, "LAUNCHING RIGCTL!!\n");
-   // This routine encapsulates the pthread call
-   err = pthread_create (&rigctl_thread_id,NULL,rigctl,NULL);
-   if(err != 0) {
-       fprintf(stderr, "pthread_create failed on rigctl launch\n");
-   }  
-}
-
-//
-// Telnet Server Code below:
-//
-    // max number of bytes we can get at once
-    #define MAXDATASIZE 300
-
-int init_server () {
-
-    //Create socket
-    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
-    if (socket_desc == -1)
-    {
-        fprintf(stderr,"RIGCTL: Could not create socket");
-    }
-    fprintf(stderr, "RIGCTL: Socket created\n");
-     
-    //Prepare the sockaddr_in structure
-    server.sin_family = AF_INET;
-    server.sin_addr.s_addr = INADDR_ANY;
-    server.sin_port = htons( TELNET_PORT );
-     
-    //Bind
-    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
-    {
-        //print the error message
-        fprintf(stderr,"RIGCLT: bind failed. Error\n");
-        return 1;
-    }
-    fprintf(stderr,"RIGCTL: bind done\n");
-     
-    //Listen
-    listen(socket_desc , 3);
-     
-    //Accept and incoming connection
-    fprintf(stderr,"RIGCTL: Waiting for incoming connections...\n");
-    c = sizeof(struct sockaddr_in);
-     
-    //accept connection from an incoming client
-    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
-    if (client_sock < 0)
-    {
-        fprintf(stderr,"RIGCTL: Accept failed\n");
-        return 1;
-    }
-    fprintf(stderr,"RIGCTL: Connection accepted\n");
-}
-
-int rigctlGetFilterLow() {
-   int lookup;
-   int rval;
-
-   BANDSTACK_ENTRY *entry;
-   entry = (BANDSTACK_ENTRY *) bandstack_entry_get_current();
-
-   FILTER* band_filters=filters[entry->mode];
-   FILTER* band_filter=&band_filters[entry->filter];
-   rval = 0;
-   for(lookup = 0; lookup<=9; lookup++) {
-      if(band_filter[lookup].low == -150) {
-          rval = lookup;
-          break;
-      }      
-   }
-   return rval;
-}
-
-int rigctlGetFilterHigh() {
-   int lookup;
-   int rval;
-
-   BANDSTACK_ENTRY *entry;
-   entry = (BANDSTACK_ENTRY *) bandstack_entry_get_current();
-
-   FILTER* band_filters=filters[entry->mode];
-   FILTER* band_filter=&band_filters[entry->filter];
-   rval = 0;
-   for(lookup = 0; lookup<=9; lookup++) {
-      if(band_filter[lookup].high == -150) {
-          rval = lookup;
-          break;
-      }      
-   }
-   return rval;
-}
-int rigctlGetMode()  {
-        BANDSTACK_ENTRY *entry;
-        entry= (BANDSTACK_ENTRY *) 
-        bandstack_entry_get_current();
-        switch(entry->mode) {
-           case modeLSB: return(1); // LSB
-           case modeUSB: return(2); // USB
-           case modeCWL: return(7); // CWL
-           case modeCWU: return(3); // CWU
-           case modeFMN: return(4); // FMN
-           case modeAM:  return(5); // AM
-           case modeDIGU: return(9); // DIGU
-           case modeDIGL: return(6); // DIGL
-           default: return(0);
-        }
-}
-
-int rigctlSetFilterLow(int val){
-};
-int rigctlSetFilterHigh(int val){
-};
+\r
+/* S-2000 emulation via TCP\r
+ * Copyright (C) 2016 Steve Wilson <wevets@gmail.com>\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License as\r
+ * published by the Free Software Foundation, either version 2 of the\r
+ * License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+\r
+/*\r
+ * PiHPSDR RigCtl by Steve KA6S Oct 16 2016\r
+ * \r
+ */\r
+#include <gtk/gtk.h>\r
+#include <gdk/gdk.h>\r
+#include <unistd.h>\r
+#include <semaphore.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include "toolbar.h"\r
+#include "sliders.h"\r
+#include "rigctl.h"\r
+#include "radio.h"\r
+#include "channel.h"\r
+#include "filter.h"\r
+#include <string.h>\r
+#include "mode.h"\r
+#include "filter.h"\r
+#include "wdsp_init.h"\r
+#include "bandstack.h"\r
+#include "vfo.h"\r
+#include "sliders.h"\r
+#include <pthread.h>\r
+#include <wdsp.h>\r
+\r
+// IP stuff below\r
+#include<sys/socket.h>\r
+#include<arpa/inet.h> //inet_addr\r
+\r
+#undef RIGCTL_DEBUG\r
+\r
+// the port client will be connecting to\r
+#define TELNET_PORT 19090  // This is the HAMLIB port\r
+// max number of bytes we can get at once\r
+#define MAXDATASIZE 300\r
+\r
+int init_server ();\r
+\r
+int rigctlGetFilterLow();\r
+int rigctlGetFilterHigh();\r
+int rigctlSetFilterLow(int val);\r
+int rigctlSetFilterHigh(int val);\r
+int new_level;\r
+\r
+typedef struct com_list {\r
+     char  cmd_string[80];\r
+     struct com_list * next_ent; \r
+ } com_list_t;\r
+com_list_t  cmd_list;\r
+char cmd_save[80];\r
+\r
+char  cmd_input[MAXDATASIZE] ;\r
+char * wptr;\r
+com_list_t * work_input; \r
+com_list_t * work_list; \r
+com_list_t * follow_list; \r
+com_list_t * com_head = NULL;\r
+com_list_t * cur_ent;\r
+int cmdlistcnt;\r
+int retcode = 1;\r
+char workvar[100];\r
+\r
+char command[80];\r
+char msg[80];\r
+char msg2[80];\r
+char * newf;\r
+\r
+FILE * TTYFILE;\r
+FILE * out;\r
+int  output;\r
+FILTER * band_filter;\r
+\r
+static pthread_t rigctl_thread_id;\r
+static void * rigctl (void * arg);\r
+\r
+// This stuff from the telnet server\r
+int sockfd, numbytes;\r
+char buf[MAXDATASIZE];\r
+struct hostent *he;\r
+\r
+// connector’s address information\r
+struct sockaddr_in their_addr;\r
+\r
+int socket_desc , client_sock , c , read_size;\r
+struct sockaddr_in server , client;\r
+char client_message[MAXDATASIZE];\r
+\r
+int freq_flag;  // Determines if we are in the middle of receiving frequency info\r
+\r
+int digl_offset = 0;\r
+int digl_pol = 0;\r
+int digu_offset = 0;\r
+int digu_pol = 0;\r
+double new_vol = 0;\r
+\r
+// Now my stuff\r
+//\r
+// Looks up entry INDEX_NUM in the command structure and\r
+// returns the command string\r
+//\r
+\r
+void send_resp (char * msg) {\r
+    #ifdef  RIGCTL_DEBUG\r
+        fprintf(stderr,"RIGCTL: RESP=%s\n",msg);\r
+    #endif\r
+    write(client_sock, msg, strlen(msg));\r
+}\r
+\r
+char * cmd_lookup (int index_num) {\r
+    com_list_t * lcl_p;\r
+    lcl_p = com_head;\r
+    if(index_num == 0)  {\r
+       return(lcl_p->cmd_string);\r
+    } else {\r
+      while((lcl_p->next_ent != (com_list_t * ) NULL) && (index_num != 0)) {\r
+          lcl_p = lcl_p->next_ent;\r
+          index_num--;\r
+      }\r
+    }\r
+    if(index_num == 0)  {\r
+      return(lcl_p->cmd_string);\r
+    } else {\r
+      return ((char *) NULL);\r
+    } \r
+}\r
+\r
+static void * rigctl (void * arg) {\r
+   int work_int;     \r
+   int len;\r
+   init_server();\r
+   double meter;\r
+   int save_flag = 0; // Used to concatenate two cmd lines together\r
+\r
+  for(;;) {\r
+    fprintf(stderr,"RIGCTL - New run\n");\r
+    while((numbytes=recv(client_sock , cmd_input , 1000 , 0)) > 0 ) {\r
+       \r
+        if(cmd_input[numbytes-1] != ';') {  // Got here because the command line is short\r
+           cmd_input[numbytes] = '\0';      // Turn it into a C string\r
+           strcpy(cmd_save,cmd_input);      // And save a copy of it till next time through\r
+           save_flag = 1;\r
+           #ifdef RIGCTL_DEBUG\r
+           fprintf(stderr,"RIGCTL: CMD_SAVE=%s  LEN=%d\n",cmd_save,numbytes);\r
+           #endif\r
+        } else if(save_flag == 1) {\r
+           save_flag = 0;\r
+           cmd_input[numbytes] = '\0';      // Turn it into a C string\r
+           strcat(cmd_save,cmd_input);\r
+           strcpy(cmd_input,cmd_save);      // Cat them together and replace cmd_input\r
+           numbytes = strlen(cmd_input);\r
+           #ifdef RIGCTL_DEBUG\r
+            fprintf(stderr,"RIGCTL: CMD_INPUT=%s LEN=%d\n",cmd_input,numbytes);\r
+           #endif\r
+        } else {\r
+           #ifdef RIGCTL_DEBUG\r
+           fprintf(stderr,"RIGCTL: CMD_NORM=%s  NUMBYTES=%d\n",cmd_input,numbytes);\r
+           #endif\r
+        }\r
+\r
+        cmd_input[numbytes-1] = '\0';  // Turn it into a C string.\r
+        len = strlen(cmd_input);\r
+           #ifdef RIGCTL_DEBUG\r
+           fprintf(stderr,"RIGCTL: CMD_AFTER=%s  LEN=%d\n",cmd_input,len);\r
+           #endif\r
+        #ifdef RIGCTL_DEBUG\r
+        fprintf(stderr,"RIGCTL: RCVD=%s  LENGTH=%d\n",cmd_input,len);\r
+        #endif\r
+        // Parse the cmd_input\r
+        //int space = command.indexOf(' ');\r
+        //char cmd_char = com_head->cmd_string[0]; // Assume the command is first thing!\r
+        char cmd_str[3];\r
+        cmd_str[0] = cmd_input[0];\r
+        cmd_str[1] = cmd_input[1];\r
+        cmd_str[2] = '\0';\r
+        if(strcmp(cmd_str,"AC")==0)       { // Sets or reads the internal antenna tuner status\r
+                                                   // P1 0:RX-AT Thru, 1: RX-AT IN\r
+                                                   // P2 0: TX-AT Thru 1: TX-AT In\r
+                                                   // \r
+                                             if(len <= 2) {\r
+                                               send_resp("AC000;");\r
+                                             }\r
+                                          }\r
+        else if(strcmp(cmd_str,"AG")==0) {  // Set Audio Gain - \r
+                                            // AG123; Value of 0-\r
+                                            // AG1 = Sub receiver - what is the value set\r
+                                            // Response - AG<0/1>123; Where 123 is 0-260\r
+                                            if(len>4) { // Set Audio Gain\r
+                                               volume = (double) atoi(&cmd_input[3])/260; \r
+                                               g_idle_add(update_af_gain,NULL);               \r
+                                            } else { // Read Audio Gain\r
+                                              sprintf(msg,"AG0%03d;",2.6 * volume);\r
+                                              send_resp(msg);\r
+                                              #ifdef RIGCTL_DEBUG\r
+                                              fprintf(stderr,":%s\n",msg);\r
+                                              #endif\r
+                                            }\r
+                                          }\r
+        else if(strcmp(cmd_str,"AI")==0)  {  // Allow rebroadcast of set frequency after set - not supported\r
+                                             if(len <=2) {\r
+                                                send_resp("AI0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"AL")==0)  {  // Set/Reads the auto Notch level \r
+                                             if(len <=2) {\r
+                                                send_resp("AL000;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"AM")==0)  {  // Sets or reads the Auto Mode\r
+                                             if(len <=2) {\r
+                                                send_resp("AM0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"AN")==0)  {  // Selects the antenna connector (ANT1/2)\r
+                                             if(len <=2) {\r
+                                                send_resp("AN0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"AR")==0)  {  // Sets or reads the ASC function on/off\r
+                                             if(len <=2) {\r
+                                                send_resp("AR0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"AS")==0)  {  // Sets/reads automode function parameters\r
+                                             // AS<P1><2xP2><11P3><P4>;\r
+                                             // AS<P1><2xP2><11P3><P4>;\r
+                                             if(len < 6) {  \r
+                                                sprintf(msg,"AS%1d%02d%011d%01d;",\r
+                                                             0, // P1\r
+                                                             0, // Automode \r
+                                                             getFrequency(),\r
+                                                             rigctlGetMode());\r
+                                                send_resp(msg);\r
+                               \r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"BC")==0)  {  // Beat Cancellor OFF\r
+                                             if(len <=2) {\r
+                                                send_resp("BC0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"BD")==0)  {  // Moves down the frequency band\r
+                                             // No response \r
+                                          }\r
+        else if(strcmp(cmd_str,"BP")==0)  {  // Reads the manual beat canceller frequency setting\r
+                                             if(len <=2) {\r
+                                                send_resp("BP000;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"BS")==0)  {  // Sets or reads Beat Canceller status\r
+                                             if(len <=2) {\r
+                                                send_resp("BS0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"BU")==0)  {  // Moves Up the frequency band\r
+                                             // No response \r
+                                          }\r
+        else if(strcmp(cmd_str,"BY")==0)  {  // Read the busy signal status\r
+                                             if(len <=2) {\r
+                                                send_resp("BY00;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"CA")==0)  {  // Sets/reads cw auto tune function status\r
+                                             if(len <=2) {\r
+                                                send_resp("CA0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"CG")==0)  {  // Sets/reads the carrier gain status\r
+                                             // 0-100 legal values\r
+                                             if(len <=2) {\r
+                                                send_resp("CG000;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"CH")==0)  {  // Sets the current frequency to call Channel\r
+                                             // No response\r
+                                          }\r
+        else if(strcmp(cmd_str,"CI")==0)  {  // While in VFO mode or memory recall mode, sets freq to the call channel\r
+                                             // No response\r
+                                          }\r
+        else if(strcmp(cmd_str,"CM")==0)  {  // Sets or reads the packet cluster tune f(x) on/off\r
+                                             // 0-100 legal values\r
+                                             if(len <=2) {\r
+                                                send_resp("CM0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"CN")==0)  {  // Sets and reads CTSS function - 01-38 legal values\r
+                                             if(len <=2) {\r
+                                                send_resp("CN00;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"CT")==0)  {  // Sets and reads CTSS function status\r
+                                             if(len <=2) {\r
+                                                send_resp("CT0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"DC")==0)  {  // Sets/Reads TX band status\r
+                                             if(len <=2) {\r
+                                                send_resp("DC00;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"DN")==0)  {  // Emulate Mic down key \r
+                                          }\r
+        else if(strcmp(cmd_str,"DQ")==0)  {  // ETs/and reads the DCS function status\r
+                                             if(len <=2) {\r
+                                                send_resp("DQ0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"EX")==0)  {  // Set/reads the extension menu\r
+                                             if(len <=12) {\r
+                                                send_resp("EX00000000000000000000000;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"FA")==0) {  // VFO A frequency\r
+                                            // LEN=7 - set frequency\r
+                                            // Next data will be rest of freq\r
+                                            if(len == 13) { //We are receiving freq info\r
+                                               long long new_freqA = atoll(&cmd_input[2]);\r
+                                               setFrequency(new_freqA);\r
+                                               g_idle_add(vfo_update,NULL);\r
+                                            } else {\r
+                                               if(len==2) {\r
+                                                  sprintf(msg,"FA%011d;",getFrequency());\r
+                                                  send_resp(msg);\r
+                                               }\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"FB")==0) {  // VFO B frequency\r
+                                            if(len==13) { //We are receiving freq info\r
+                                               long long new_freqA = atoll(&cmd_input[2]); \r
+                                               setFrequency(new_freqA);\r
+                                               g_idle_add(vfo_update,NULL);\r
+                                            } else if(len == 2) {\r
+                                               sprintf(msg,"FB%011d;",getFrequency());\r
+                                               send_resp(msg);\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"FC")==0) {  // Set Sub receiver freq\r
+                                            // LEN=7 - set frequency\r
+                                            // Next data will be rest of freq\r
+                                            // Len<7 - frequency?\r
+                                            if(len>4) { //We are receiving freq info\r
+                                               long long new_freqA = atoll(&cmd_input[2]);              \r
+                                               //setFrequency(new_freqA);\r
+                                            } else {\r
+                                               sprintf(msg,"FC%011d;",getFrequency());\r
+/*\r
+                                               send_resp(msg);\r
+*/\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"FD")==0)  {  // Read the filter display dot pattern\r
+                                              send_resp("FD00000000;");\r
+                                          }\r
+        else if(strcmp(cmd_str,"FR")==0)  {  // Set/reads the extension menu\r
+                                             if(len <=2) {\r
+                                                send_resp("FR0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"FS")==0)  {  // Set/reads fine funct status\r
+                                             if(len <=2) {\r
+                                                send_resp("FS0;");\r
+                                             } \r
+                                          }\r
+        else if(strcmp(cmd_str,"FT")==0)  { // Sel or reads the transmitters VFO, M, ch or Call comm   \r
+                                             if(len <=2) {\r
+                                                send_resp("FT0;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"FW")==0)  { // Sets/Reas DSP receive filter width in hz 0-9999hz \r
+                                             if(len <=2) {\r
+                                                send_resp("FW0000;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"GT")==0)  { // Sets/Reas AGC constant status\r
+                                             if(len <=2) {\r
+                                                send_resp("GT000;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"ID")==0)  { // Read ID - Default to TS-2000 which is type 019.\r
+                                            sprintf(msg,"ID019;");\r
+                                            send_resp(msg);\r
+                                          }\r
+        else if(strcmp(cmd_str,"IF")==0) {  // Reads Transceiver status\r
+                                            // IFFFFFFFFFFFF - Frequency \r
+                                            //   OFFS        - 4 chars Offset in powers of 10\r
+                                            //   RITRIT      - 6 chars RIT/XIT Frequency - Not supported =0\r
+                                            //   R           - 1 char  RIT Status = 0 is off\r
+                                            //   X           - 1 char  XIT Status = 0 is off\r
+                                            //   0           - 1 char  Channel Bank number - not used\r
+                                            //   00          - 2 char  Channel Bank number - not used\r
+                                            //   C           - 1 char  Mox Status 0=off, 1=on\r
+                                            //   M           - 1 char Operating mode - align with MD commands\r
+                                            //   V           - 1 char VFO Split status - not supported  \r
+                                            //   0           - 1 char Scan status - not supported\r
+                                            //   A           - 1 char - same as FT command\r
+                                            //   0           - 1 char - CTCSS tone - not used\r
+                                            //   00          - 2 char - More tone control\r
+                                            //   0           - 1 char - Shift status\r
+                                            sprintf(msg,"IF%011d%04d%06d%1d%1d%1d%02d%01d%01d%01d%01d%01d%01d%02d%01d;",\r
+                                                         getFrequency(),\r
+                                                         0,  // Shift Offset\r
+                                                         0,  // Rit Freq\r
+                                                         0,  // Rit Status\r
+                                                         0,  // XIT Status\r
+                                                         0,  // Channel Bank num\r
+                                                         0,  // Channel Bank num\r
+                                                         0,  // Mox Status\r
+                                                         rigctlGetMode(),  // Mode Status - same as MD Command\r
+                                                         0,  // VFO Status\r
+                                                         0,  // Scan status\r
+                                                         0,  // FT command \r
+                                                         0,  // CTSS Tone\r
+                                                         0,  // More tone control\r
+                                                         0); // Shift status\r
+                                            send_resp(msg);\r
+                                         }\r
+        else if(strcmp(cmd_str,"IS")==0)  { // Sets/Reas IF shift funct status\r
+                                             if(len <=2) {\r
+                                                send_resp("IS00000;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"KS")==0)  { // Sets/Reads keying freq - 0-060 max\r
+                                             if(len <=2) {\r
+                                                send_resp("KS000;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"KY")==0)  { // Convert char to morse code\r
+                                             if(len <=2) {\r
+                                                send_resp("KY0;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"LK")==0)  { // Set/read key lock function status\r
+                                             if(len <=2) {\r
+                                                send_resp("LK00;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"LM")==0)  { // Set/read DRU 3A unit or elect keyer recording status\r
+                                             if(len <=2) {\r
+                                                send_resp("LM0;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"LT")==0)  { // Set/read Alt function\r
+                                             if(len <=2) {\r
+                                                send_resp("LT0;");\r
+                                             } \r
+                                          }  \r
+        else if(strcmp(cmd_str,"MC")==0) {  // Recalls or reads memory channel\r
+                                             if(len <=2) {\r
+                                               send_resp("MC000;"); // Link this to band stack at some point\r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"MD")==0) {  // Mode - digit selects mode\r
+                                            // 1 = LSB\r
+                                            // 2 = USB\r
+                                            // 3 = CWU\r
+                                            // 4 = FM\r
+                                            // 5 = AM\r
+                                            // 6 = DIGL\r
+                                            // 7 = CWL\r
+                                            // 9 = DIGU \r
+                                            int new_mode;\r
+                                            if(len > 2) { // Set Mode\r
+                                               switch(atoi(&cmd_input[2])) {   \r
+                                                 case 1:  \r
+                                                     new_mode = modeLSB;\r
+                                                     break;\r
+                                                 case 2:  \r
+                                                     new_mode = modeUSB;\r
+                                                     break;\r
+                                                 case 3:  \r
+                                                     new_mode = modeCWU;\r
+                                                     break;\r
+                                                 case 4:  \r
+                                                     new_mode = modeFMN;\r
+                                                     break;\r
+                                                 case 5:  \r
+                                                     new_mode = modeAM;\r
+                                                     break;\r
+                                                 case 6:  \r
+                                                     new_mode = modeDIGL;\r
+                                                     break;\r
+                                                 case 7:  \r
+                                                     new_mode = modeCWL;\r
+                                                     break;\r
+                                                 case 9:  \r
+                                                     new_mode = modeDIGU;\r
+                                                     break;\r
+                                                 default:\r
+                                                     break;\r
+                                                     #ifdef RIGCTL_DEBUG\r
+                                                     fprintf(stderr,"MD command Unknown\n");\r
+                                                     #endif\r
+                                               }\r
+                                            // Other stuff to switch modes goes here..\r
+                                            // since new_mode has the interpreted command in \r
+                                            // in it now.\r
+                                            BANDSTACK_ENTRY *entry;\r
+                                            entry= (BANDSTACK_ENTRY *) \r
+                                                  bandstack_entry_get_current();\r
+                                            entry->mode=new_mode;\r
+                                            //  BUG - kills the system when there is some\r
+                                            // GPIO activity and Mode sets occur. Used twittling the\r
+                                           // frequency along with setting mode between USB/LSB with\r
+                                           // flrig. Tried doing the g_idle_add trick - but don't know the\r
+                                           // the magic to get that to compile without warnings \r
+                                            setMode(entry->mode);\r
+                                            // Moved the vfo_update down after filter updated. (John G0ORX)\r
+                                            //g_idle_add(vfo_update,NULL);\r
+                                            \r
+                                            FILTER* band_filters=filters[entry->mode];\r
+                                            FILTER* band_filter=&band_filters[entry->filter];\r
+                                            setFilter(band_filter->low,band_filter->high);\r
+                                            /* Need a way to update VFO info here..*/\r
+                                            g_idle_add(vfo_update,NULL);\r
+                                            }  else {     // Read Mode\r
+                                               int curr_mode;\r
+                                               switch (mode) {\r
+                                                  case modeLSB: curr_mode = 1; \r
+                                                          break;   \r
+                                                  case modeUSB: curr_mode = 2;\r
+                                                          break;\r
+                                                  case modeCWL: curr_mode = 7; // CWL\r
+                                                          break;\r
+                                                  case modeCWU: curr_mode = 3; // CWU\r
+                                                          break;\r
+                                                  case modeFMN: curr_mode = 4; // FMN\r
+                                                          break;\r
+                                                  case modeAM: curr_mode = 5; // AM\r
+                                                          break;\r
+                                                  case modeDIGU: curr_mode = 9; // DIGU\r
+                                                          break;\r
+                                                  case modeDIGL: curr_mode = 6; // DIGL\r
+                                                          break;\r
+                                                  default: \r
+                                                          #ifdef RIGCTL_DEBUG\r
+                                                            fprintf(stderr,\r
+                                                            "RIGCTL: MD command doesn't support %d\n",\r
+                                                            mode);\r
+                                                          #endif\r
+                                                          break;\r
+                                               }\r
+                                               sprintf(msg,"MD%1d;",curr_mode);\r
+                                               send_resp(msg);\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"MF")==0) {  // Set/read menu A/B\r
+                                             if(len <=2) {\r
+                                               send_resp("MF0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"MG")==0) {  // Mike Gain - 3 digit value \r
+                                            if(len <=2) {\r
+                                               work_int = (int) ((mic_gain +10.0)* 100.0/60.0);\r
+                                               sprintf(msg,"MG%03d",work_int);\r
+                                               send_resp(msg);\r
+                                            } else {\r
+                                               int tval = atoi(&cmd_input[2]);                \r
+                                               new_vol = (double) (tval * 60/100) - 10; \r
+                                               //set_mic_gain(new_vol); \r
+                                               double *p_mic_gain=malloc(sizeof(double));\r
+                                               *p_mic_gain=new_vol;\r
+                                               g_idle_add(update_mic_gain,(void *)p_mic_gain);\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"ML")==0) {  // Set/read the monitor function level\r
+                                             if(len <=2) {\r
+                                               send_resp("ML000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"MO")==0) {  // Set Monitor on/off\r
+                                             if(len <=2) {\r
+                                               send_resp("MO0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"MR")==0) {  // Read Memory Channel data\r
+                                             sprintf(msg,"MR%1d%02d%02d%011d%1d%1d%1d%02d%02d%03d%1d%1d%09d%02d%1d%08d;",\r
+                                                      0, // P1 - Rx Freq - 1 Tx Freq\r
+                                                      0, // P2 Bankd and channel number -- see MC comment\r
+                                                      0, // P3 - see MC comment \r
+                                                      getFrequency(), // P4 - frequency\r
+                                                      rigctlGetMode(), // P5 - Mode\r
+                                                      0, // P6 - Locked status\r
+                                                      0, // P7 - O-off, 1-tone, 2-CTCSS, 3 =DCS\r
+                                                      0, // P8 - Tone Numbe3r - see page 35\r
+                                                      0, // P9 - CTCSS tone number - see CN command\r
+                                                      0, // P10 - DCS code - see QC command \r
+                                                      0, // P11 - Reverse status\r
+                                                      0, // P12 - Shift status - see OS command\r
+                                                      0, // P13 - Offset freq - see OS command\r
+                                                      0, // P14 - Step Size - see ST command\r
+                                                      0, // P15 - Memory Group Number (0-9)\r
+                                                      0);  // P16 - Memory Name - 8 char max\r
+                                               send_resp(msg);\r
+                                         }\r
+        else if(strcmp(cmd_str,"MU")==0) {  // Set/Read Memory Group Data\r
+                                             if(len <=2) {\r
+                                               send_resp("MU0000000000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"MW")==0) {  // Store Data to Memory Channel\r
+                                         }\r
+        else if(strcmp(cmd_str,"NB")==0) {  // Set/Read Noise Blanker func status (on/off)\r
+                                             if(len <=2) {\r
+                                               send_resp("NB0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"NL")==0) {  // Set/Read Noise Reduction  Level\r
+                                             if(len <=2) {\r
+                                               send_resp("NL000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"NR")==0) {  // Set/Read Noise Reduction function status\r
+                                             if(len <=2) {\r
+                                               send_resp("NR0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"NT")==0) {  // Set/Read autonotch function\r
+                                             if(len <=2) {\r
+                                               send_resp("NT0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"OF")==0) {  // Set/Read Offset freq (9 digits - hz)\r
+                                             if(len <=2) {\r
+                                               send_resp("OF000000000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"OI")==0) {  // Read Memory Channel Data\r
+                                             if(len <=2) {\r
+                                               sprintf(msg,"OI%011d%04d%06d%1d%1d%1d%02d%1d%1d%1d%1d%1d%1d%02d%1d;",\r
+                                                  getFrequency(),\r
+                                                  0, // P2 - Freq Step size\r
+                                                  0, // P3 - Rit/Xit Freq \r
+                                                  0, // P4 - RIT off/Rit On\r
+                                                  0, // P5 - XIT off/on\r
+                                                  0, // P6 - Channel\r
+                                                  0, // P7 - Bank\r
+                                                  0, // P8 - 0RX, 1TX\r
+                                                  rigctlGetMode(),  // P10 - MD\r
+                                                  0, // P11 - SC command\r
+                                                  0, // P12 Split op - SP command\r
+                                                  0, // P13 Off, 1, 2, \r
+                                                  0, // P14 Tone freq - See TN command\r
+                                                  0);\r
+                                               send_resp(msg);\r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"OS")==0) {  // Set/Read Offset freq (9 digits - hz)\r
+                                             if(len <=2) {\r
+                                               send_resp("OS0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PA")==0) {  // Set/Read Preamp function status\r
+                                             if(len <=2) {\r
+                                               send_resp("PA00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PB")==0) {  // Set/Read DRU-3A Playback status\r
+                                             if(len <=2) {\r
+                                               send_resp("PB0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PC")==0) {  // Set/Read Drive Power output\r
+                                            if(len<=2) {\r
+                                              sprintf(msg,"PC%03d;",(int) drive);\r
+                                              send_resp(msg); \r
+                                            } else {\r
+                                               // Power Control - 3 digit number- 0 to 100\r
+                                               //Scales to 0.00-1.00\r
+                                                \r
+                                               double drive_val = \r
+                                                   (double)(atoi(&cmd_input[2])); \r
+                                               // setDrive(drive_val);\r
+                                               double *p_drive=malloc(sizeof(double));\r
+                                               *p_drive=drive_val;\r
+                                               g_idle_add(update_drive,(gpointer)p_drive);\r
+                                               //set_drive(drive_val);\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PI")==0) {  // STore the programmable memory channel\r
+                                         }\r
+        else if(strcmp(cmd_str,"PK")==0) {  // Reads the packet cluster data\r
+                                               send_resp("PK000000000000000000000000000000000000000000000000;"); \r
+                                         }\r
+        else if(strcmp(cmd_str,"PL")==0) {  // Set/Read Speech processor level\r
+                                            // P1 000 - min-100 max\r
+                                            // P2 000 - min - 100 max\r
+                                             if(len <=2) {\r
+                                               send_resp("PL000000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PM")==0) {  // Recalls the Programmable memory\r
+                                             if(len <=2) {\r
+                                               send_resp("PM0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PR")==0) {  // Sets/reads the speech processor function on/off\r
+                                            // 0 - off, 1=on\r
+                                             if(len <=2) {\r
+                                               send_resp("PR0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PS")==0) {  // Sets/reads Power on/off state\r
+                                            // 0 - off, 1=on\r
+                                             if(len <=2) {\r
+                                               send_resp("PS1;"); // Lets pretend we're powered up ;-) \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"PS")==0) {  // Sets/reads DCS code\r
+                                            // Codes numbered from 000 to 103.\r
+                                             if(len <=2) {\r
+                                               send_resp("QC000;"); // Lets pretend we're powered up ;-) \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"QI")==0) {  // Store the settings in quick memory\r
+                                         }\r
+        else if(strcmp(cmd_str,"QR")==0) {  // Send the Quick memory channel data\r
+                                            // P1 - Quick mem off, 1 quick mem on\r
+                                            // P2 - Quick mem channel number\r
+                                             if(len <=2) {\r
+                                               send_resp("QR00;"); // Lets pretend we're powered up ;-) \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RA")==0) {  // Sets/reads Attenuator function status\r
+                                            // 00-off, 1-99 -on - Main and sub receivers reported\r
+                                             if(len <=2) {\r
+                                               send_resp("RA0000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RC")==0) {  // Clears the RIT offset freq\r
+                                         }\r
+        else if(strcmp(cmd_str,"RD")==0) {  // Move the RIT offset freq down, slow down the scan speed in scan mode\r
+                                             if(len <=2) {\r
+                                               send_resp("RD0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RG")==0) {  // RF Gain - 3 digit number\r
+                                            // RG123; 0-255 value\r
+                                            // Scale from -20 - 120\r
+                                            if(len>4) { // Set Audio Gain\r
+                                               int base_value = atoi(&cmd_input[2]);\r
+                                               double new_gain = ((((double) base_value)/255) * 140) - 20; \r
+                                               //set_agc_gain(new_gain);               \r
+                                               double *p_gain=malloc(sizeof(double));\r
+                                               *p_gain=new_gain;\r
+                                               g_idle_add(update_agc_gain,(gpointer)p_gain);\r
+                                            } else { // Read Audio Gain\r
+                                              sprintf(msg,"RG%03d;",((256 * (int) agc_gain)/140)+36);\r
+                                              send_resp(msg);\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RL")==0) {  // Set/read Noise reduction level\r
+                                             if(len <=2) {\r
+                                               send_resp("RL00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RM")==0) {  // Set/read Meter function\r
+                                            // P1- 0, unsel, 1 SWR, 2 Comp, 3 ALC\r
+                                            // P2 - 4 dig - Meter value in dots - 000-0030\r
+                                             if(len <=2) {\r
+                                               send_resp("RM00000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RT")==0) {  // Set/read the RIT function status\r
+                                             if(len <=2) {\r
+                                               send_resp("RT0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RU")==0) {  // Set/move RIT frequency up\r
+                                             if(len <=2) {\r
+                                               send_resp("RU0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"RX")==0) {  // Unkey Xmitter\r
+                                            setMox(0);\r
+                                            // 0-main, 1=sub\r
+                                             if(len <=2) {\r
+                                               send_resp("RX0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SA")==0) {  // Set/reads satellite mode status\r
+                                            // 0-main, 1=sub\r
+                                             if(len <=2) {\r
+                                               send_resp("SA000000000000000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SB")==0) {  // Set/read the SUB TF-W status\r
+                                             if(len <=2) {\r
+                                               send_resp("SB0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SC")==0) {  // Set/read the Scan function status\r
+                                             if(len <=2) {\r
+                                               send_resp("SC0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SD")==0) {  // Set/read CW break-in time delay\r
+                                            // 0000-1000 ms (in steps of 50 ms) 0000 is full break in\r
+                                             if(len <=2) {\r
+                                               send_resp("SD0000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SH")==0) {  // Set/read the DSP filter settings\r
+                                             if(len <=2) {\r
+                                               send_resp("SH00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SI")==0) {  // Enters the satellite memory name\r
+                                         }\r
+        else if(strcmp(cmd_str,"SL")==0) {  // Set/read the DSP filter settings - this appears twice? See SH\r
+                                             if(len <=2) {\r
+                                               send_resp("SL00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SM")==0) {  // Read SMETER\r
+                                            // SMx;  x=0 RX1, x=1 RX2 \r
+                                            // meter is in dbm - value will be 0<260\r
+                                            // Translate to 00-30 for main, 0-15 fo sub\r
+                                            // Resp= SMxAABB; \r
+                                            // \r
+                                            double level;\r
+                                            level = GetRXAMeter(CHANNEL_RX0, smeter); \r
+                                            level = level + (double) get_attenuation();\r
+                                            level = level + 90;\r
+                                            new_level = (int) (level)/2;            \r
+                                            if(new_level < 0) { new_level = 0; }\r
+                                            if(new_level > 30) { new_level = 30;}\r
+                                            #ifdef RIGCTL_DEBUG\r
+                                            fprintf(stderr,"RIGCTL: SM level=%.21f new_level=%d\n",\r
+                                                        level,new_level);\r
+                                            #endif\r
+                                            sprintf(msg,"SM%1c%04d;",\r
+                                                cmd_input[2],new_level);\r
+                                            send_resp(msg);\r
+                                         }   \r
+        else if(strcmp(cmd_str,"SQ")==0) {  // Set/read the squelch level\r
+                                            // P1 - 0- main, 1=sub\r
+                                            // P2 - 0-255\r
+                                             if(len <=2) {\r
+                                               send_resp("SQ0000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SR")==0) {  // Resets the transceiver\r
+                                         }\r
+        else if(strcmp(cmd_str,"SS")==0) {  // Set/read Scan pause freq\r
+                                             if(len <=2) {\r
+                                               send_resp("SS0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"ST")==0) {  // Set/read the multi/ch control freq steps\r
+                                             if(len <=2) {\r
+                                               send_resp("ST00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SU")==0) {  // Set/read the scan pause freq\r
+                                             if(len <=2) {\r
+                                               send_resp("SU00000000000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"SV")==0) {  // Execute the memory transfer function\r
+                                         }\r
+        else if(strcmp(cmd_str,"TC")==0) {  // Set/read the internal TNC mode\r
+                                             if(len <=2) {\r
+                                               send_resp("TC00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"TD")==0) {  // Sends the DTMF memory channel\r
+                                         }\r
+        else if(strcmp(cmd_str,"TI")==0) {  // Reads the TNC LED status\r
+                                             if(len <=2) {\r
+                                               send_resp("TI00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"TN")==0) {  // Set/Read sub tone freq\r
+                                             if(len <=2) {\r
+                                               send_resp("TN00;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"TO")==0) {  // Set/Read tone function\r
+                                             if(len <=2) {\r
+                                               send_resp("TO0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"TS")==0) {  // Set/Read TF Set function status\r
+                                             if(len <=2) {\r
+                                               send_resp("TS0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"TX")==0) {  // Key Xmitter - P1 - transmit on main/sub freq\r
+                                            setMox(1);\r
+                                             if(len <=2) {\r
+                                               send_resp("TS0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"TY")==0) {  // Set/Read uP firmware type\r
+                                             if(len <=2) {\r
+                                               send_resp("TY000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"UL")==0) {  // Detects the PLL unlock status - this should never occur - xmit only\r
+                                             if(len <=2) {\r
+                                               send_resp("UL0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"UP")==0) {  // Emulates the mic up key\r
+                                         }\r
+        else if(strcmp(cmd_str,"VD")==0) {  // Sets/Reads VOX dleay time - 0000-3000ms in steps of 150\r
+                                             if(len <=2) {\r
+                                               send_resp("VD0000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"VG")==0) {  // Sets/Reads VOX gain 000-009\r
+                                             if(len <=2) {\r
+                                               send_resp("VG000;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"VG")==0) {  // Emulates the voide 1/2 key\r
+                                         }\r
+        else if(strcmp(cmd_str,"VX")==0) {  // Sets/reads vox f(x) state\r
+                                             if(len <=2) {\r
+                                               send_resp("VX0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"XT")==0) {  // Sets/reads the XIT f(x) state\r
+                                             if(len <=2) {\r
+                                               send_resp("XT0;"); \r
+                                             }\r
+                                         }\r
+        else if(strcmp(cmd_str,"XX")==0) {  // \r
+                                            // Format RL01234: First dig 0=neg, 1=pos number\r
+                                            //                 1-4- digital offset in hertz.\r
+                                            if(len > 4) { // It is set instead of a read\r
+                                               digl_pol = (cmd_input[2]=='0') ? -1 : 1;\r
+                                               digl_offset = atoi(&cmd_input[3]); \r
+                                               #ifdef RIGCTL_DEBUG\r
+                                               fprintf(stderr,"RIGCTL:RL set %d %d\n",digl_pol,digl_offset); \r
+                                               #endif\r
+                                            } else {\r
+                                               if(digl_pol==1) { // Nah - its a read\r
+                                                 sprintf(msg,"RL1%04d;");\r
+                                               } else {\r
+                                                 sprintf(msg,"RL0%04d;");\r
+                                               }         \r
+                                               send_resp(msg);\r
+                                               #ifdef RIGCTL_DEBUG\r
+                                               fprintf(stderr,":%s\n",msg);\r
+                                               #endif\r
+                                            }\r
+                                         }\r
+        else if(strcmp(cmd_str,"XY")==0) {  // set/read RTTY DIGL offset frequency - Not available - just store values\r
+                                            // Format RL01234: First dig 0=neg, 1=pos number\r
+                                            //                 1-4- digital offset in hertz.\r
+                                            if(len > 4) { // It is set instead of a read\r
+                                               digl_pol = (cmd_input[2]=='0') ? -1 : 1;\r
+                                               digl_offset = atoi(&cmd_input[3]); \r
+                                               #ifdef RIGCTL_DEBUG\r
+                                               fprintf(stderr,"RIGCTL:RL set %d %d\n",digl_pol,digl_offset); \r
+                                               #endif\r
+                                            } else {\r
+                                               if(digl_pol==1) { // Nah - its a read\r
+                                                 sprintf(msg,"RL1%04d;");\r
+                                               } else {\r
+                                                 sprintf(msg,"RL0%04d;");\r
+                                               }         \r
+                                               send_resp(msg);\r
+                                               #ifdef RIGCTL_DEBUG\r
+                                               fprintf(stderr,":%s\n",msg);\r
+                                               #endif\r
+                                            }\r
+                                         }\r
+        else                             {\r
+                                            fprintf(stderr,"RIGCTL: UNKNOWN\n");\r
+                                         }\r
+   } \r
+    // Got here because pipe closed \r
+   if(numbytes == 0) {\r
+         fprintf(stderr,"RIGCTL: Client disconnected\n");\r
+         close(client_sock);\r
+         sleep(1);\r
+         client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);\r
+   }\r
+ }\r
+}\r
+\r
+char * rigctlGetFilter()\r
+{\r
+   \r
+    char * m = mode_string[mode];\r
+\r
+    if (strcmp(m,"CWU") == 0){\r
+       return (char *) (getFilterHigh() + getFilterLow());\r
+    }\r
+    else\r
+    if (strcmp(m,"CWL") == 0){\r
+       return (char *) (getFilterHigh() + getFilterLow());\r
+    }\r
+    else\r
+    return (char *) (getFilterHigh() - getFilterLow());\r
+}\r
+\r
+\r
+void launch_rigctl () {\r
+   int err;\r
+   fprintf(stderr, "LAUNCHING RIGCTL!!\n");\r
+   // This routine encapsulates the pthread call\r
+   err = pthread_create (&rigctl_thread_id,NULL,rigctl,NULL);\r
+   if(err != 0) {\r
+       fprintf(stderr, "pthread_create failed on rigctl launch\n");\r
+   }  \r
+}\r
+\r
+//\r
+// Telnet Server Code below:\r
+//\r
+    // max number of bytes we can get at once\r
+    #define MAXDATASIZE 300\r
+\r
+int init_server () {\r
+\r
+    //Create socket\r
+    socket_desc = socket(AF_INET , SOCK_STREAM , 0);\r
+    if (socket_desc == -1)\r
+    {\r
+        fprintf(stderr,"RIGCTL: Could not create socket");\r
+    }\r
+    fprintf(stderr, "RIGCTL: Socket created\n");\r
+     \r
+    //Prepare the sockaddr_in structure\r
+    server.sin_family = AF_INET;\r
+    server.sin_addr.s_addr = INADDR_ANY;\r
+    server.sin_port = htons( TELNET_PORT );\r
+     \r
+    //Bind\r
+    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)\r
+    {\r
+        //print the error message\r
+        fprintf(stderr,"RIGCLT: bind failed. Error\n");\r
+        return 1;\r
+    }\r
+    fprintf(stderr,"RIGCTL: bind done\n");\r
+     \r
+    //Listen\r
+    listen(socket_desc , 3);\r
+     \r
+    //Accept and incoming connection\r
+    fprintf(stderr,"RIGCTL: Waiting for incoming connections...\n");\r
+    c = sizeof(struct sockaddr_in);\r
+     \r
+    //accept connection from an incoming client\r
+    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);\r
+    if (client_sock < 0)\r
+    {\r
+        fprintf(stderr,"RIGCTL: Accept failed\n");\r
+        return 1;\r
+    }\r
+    fprintf(stderr,"RIGCTL: Connection accepted\n");\r
+}\r
+\r
+int rigctlGetFilterLow() {\r
+   int lookup;\r
+   int rval;\r
+\r
+   BANDSTACK_ENTRY *entry;\r
+   entry = (BANDSTACK_ENTRY *) bandstack_entry_get_current();\r
+\r
+   FILTER* band_filters=filters[entry->mode];\r
+   FILTER* band_filter=&band_filters[entry->filter];\r
+   rval = 0;\r
+   for(lookup = 0; lookup<=9; lookup++) {\r
+      if(band_filter[lookup].low == -150) {\r
+          rval = lookup;\r
+          break;\r
+      }      \r
+   }\r
+   return rval;\r
+}\r
+\r
+int rigctlGetFilterHigh() {\r
+   int lookup;\r
+   int rval;\r
+\r
+   BANDSTACK_ENTRY *entry;\r
+   entry = (BANDSTACK_ENTRY *) bandstack_entry_get_current();\r
+\r
+   FILTER* band_filters=filters[entry->mode];\r
+   FILTER* band_filter=&band_filters[entry->filter];\r
+   rval = 0;\r
+   for(lookup = 0; lookup<=9; lookup++) {\r
+      if(band_filter[lookup].high == -150) {\r
+          rval = lookup;\r
+          break;\r
+      }      \r
+   }\r
+   return rval;\r
+}\r
+int rigctlGetMode()  {\r
+        BANDSTACK_ENTRY *entry;\r
+        entry= (BANDSTACK_ENTRY *) \r
+        bandstack_entry_get_current();\r
+        switch(entry->mode) {\r
+           case modeLSB: return(1); // LSB\r
+           case modeUSB: return(2); // USB\r
+           case modeCWL: return(7); // CWL\r
+           case modeCWU: return(3); // CWU\r
+           case modeFMN: return(4); // FMN\r
+           case modeAM:  return(5); // AM\r
+           case modeDIGU: return(9); // DIGU\r
+           case modeDIGL: return(6); // DIGL\r
+           default: return(0);\r
+        }\r
+}\r
+\r
+int rigctlSetFilterLow(int val){\r
+};\r
+int rigctlSetFilterHigh(int val){\r
+};\r