/************************************************************************
Function:       novacomm1_parser
Paramerers:     None
Responsibility: Parse the array pointed to by cptr to locate the satellite
                name, satellite azmith, & satellite elevation.
                If found it translates the ASCII azmith and elevation strings
                to integers and loads p_rotor->auto_bearing & el_auto_bearing.

                example data for NOVA's novacomm1 protocol
                if you want to test with a terminal emulator (Putty)
                <NAME> AZ123.4 EL45.7

                Needs space between satellite NAME and AZ.
                Needs space before the EL.
                The decimal points must be there (non numeric).
                NO SPACES between the AZ, EL and the first ascii digit.


Returns:        TRUE (non-zero) if a successful parse
                Zero if parse fails
Global Usage:
                Writes - sat_name[], sat_az[], sat_el[] arrays
                         p_rotor->auto_bearing, el_auto_bearing
Author:         Bill (N9CX)
************************************************************************/

#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/eeprom.h>

#include "uart_getchar.h"
#include "switches.h"
#include "rotor.h"
#include "main.h"
#include "auto_track.h"

char novacomm1_parser(char *cptr, int sz){
    char *ptr_nm = cptr;
    char *ptr_az = cptr;
    char *ptr_el;
    char *ptr_tmp;

    int found_az = FALSE;
    int found_el = FALSE;
    int found_name= FALSE;
    char result=FALSE;
    int tmp_az_bearing, tmp_el_bearing;

          while(ptr_az < cptr+sz ){
              if(*ptr_az == 'A' && *(ptr_az+1) == 'Z'){
                  /*
                  Found the "AZ" azmith indicator
                  The satellite name spans from the start of the array 
                  to the ASCII space before the letters "AZ".
                  Replace the ASCII space with a null to terminate the
                  satellite name substring.
                  */
                  if((ptr_az -1) - cptr < sizeof(sat_name)){
                    ptr_nm = ptr_az - 1; 
                  }
                  else ptr_nm  = cptr + sizeof(sat_name)-1;
                  *ptr_nm = 0;
                  
                  /*********************************************
                  cptr points to the start of the buffer (start
                  of the satellite name substring
                  **********************************************/
                  strcpy(sat_name, cptr);
                  found_name = TRUE;

                  /*
                  The first ASCII digit of the azmith is 2 places forward
                  in the array.
                  */
                  ptr_az +=2;
                  break;
              }
              else{
                  // filter out all EOL symbols
                  if(*ptr_az == '\n' || *ptr_az == '\r') *ptr_az = ' ';
                  ++ptr_az;  
              }
          }
          if(found_name){
              /*
               ptr_az is pointing at the first digit of 
               the azimuth number.
               Find the end of the digits now
              */
              ptr_tmp = ptr_az;
              found_az = TRUE;
              while(isdigit(*ptr_tmp++)){
                  if(ptr_tmp >= cptr+sz-1 || ptr_tmp > ptr_az +3){
                      found_az = FALSE;
                      break;
                  }
              }
          }
          if(found_az){
            /*******************************************************
            Terminate the azmith substrign at the first non numeric
            ASCII character (expected to be the decimal point in the
            azmith). I disregard the fractional  part of the azmith
            since I can not control the rotor to that resolution.
            ********************************************************/
            *ptr_tmp = 0;
            strcpy(sat_az, ptr_az);
             ptr_el = ++ptr_tmp;

             //Find the start of the elevation substrign.
             while(ptr_el < cptr+sz){
                if(*ptr_el == 'E' && *(ptr_el+1) == 'L'){
                      // found the "EL" elevation indicator
                      ptr_el +=2;
                      found_el = TRUE;
                      ptr_tmp = ptr_el;
                      while(isdigit(*ptr_tmp++)){
                         if(ptr_tmp >= cptr+sz-1 || ptr_tmp > ptr_el +3){
                              found_el = FALSE;
                               break;
                         }
                      }
                      // Terminate the substring
                      *ptr_tmp = 0;
                      strcpy(sat_el, ptr_el);
                      break;
                }
                else ++ptr_el;
             }
          }
          
          if(found_az && found_el){
                tmp_az_bearing = atoi(sat_az);
                tmp_el_bearing = atoi(sat_el);
                if(tmp_az_bearing >= 0 && tmp_az_bearing <= 360){
                    if(tmp_el_bearing >= 0 && tmp_el_bearing <= 360){
                        az_rotor.auto_bearing = tmp_az_bearing;
                        el_rotor.auto_bearing = tmp_el_bearing;
                        result = TRUE;
                    }
                }
          }
          return result;
}

