#include "string.h"
#include "ringbuf.h"
#include "at_cmd.h"
#include "delay.h"
#include <stdio.h>
sendmsg callback=NULL;
volatile char *flag_ok=NULL;
struct ringbuf *result_ptr=NULL;
void register_cmd_handler(sendmsg func,void *result_buf, volatile char *flag)
{
  callback=func;
  result_ptr=(struct ringbuf *)result_buf;
  flag_ok=flag;
}
 void SendCmd(char* cmd, uint8_t *result,uint16_t timeout)
{
	//printf("%s",cmd);
	 char *msg_p=NULL;
   if(callback==NULL||result_ptr==NULL||flag_ok==NULL)
     return ;
	 *flag_ok=0;
   ringbuf_clear(result_ptr);//清除之前可能残留的信息
	 printf("cmd:%s\n",cmd);
   callback((uint8_t*)cmd, strlen((const char *)cmd));
  // ringbuf_clear(result_ptr);//清除之前可能残留的信息
     while(1)
   {
      
      if(*flag_ok==1)
      {
         printf("cmd_rsp:%s\r\n",result_ptr->data);
         *flag_ok=0;
         msg_p=strstr(result_ptr->data,result);
         if(msg_p!=NULL){
			   	ringbuf_clear(result_ptr);
                   break;
				 }else{
             ringbuf_clear(result_ptr);						 
				     mDelay(3000);
				     printf("cmd:%s\n",cmd);
				     callback((uint8_t*)cmd, strlen((const char *)cmd));					 
			  }
      }
   
   }
   mDelay(timeout);
}
void SentData(uint8_t* buf, uint8_t *result,uint16_t len)
{
//   char *msg_p=NULL;
   if(callback==NULL||result_ptr==NULL)
     return ;
   ringbuf_clear(result_ptr);
	 printf("%s",buf);
   callback((uint8_t*)buf, len);
	 mDelay(10);
  // ringbuf_clear(result_ptr);
	/* 
      while(1)
   {
      if(*flag_ok==1)
      {
         *flag_ok=0;
         printf("data_rsp:%s\r\n",result_ptr->data);
         msg_p=strstr(result_ptr->data,result);
         ringbuf_clear(result_ptr);
         if(msg_p!=NULL)
          break;
      }
   
   }
	 */
}
