/*********************************************************************
 * @fn      SimpleBLECentral_ProcessEvent
 *
 * @brief   Simple BLE Central Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  events not processed
 */
uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )
{
  
  VOID task_id; // OSAL required parameter that isn't used in this function
  
  if ( events & SYS_EVENT_MSG )
  {
    uint8 *pMsg;

    if ( (pMsg = osal_msg_receive( simpleBLETaskId )) != NULL )
    {
      simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

      // Release the OSAL message
      VOID osal_msg_deallocate( pMsg );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  if ( events & START_DEVICE_EVT )
  {
    // Start the Device
    VOID GAPCentralRole_StartDevice( (gapCentralRoleCB_t *) &simpleBLERoleCB );

    // Register with bond manager after starting device
    GAPBondMgr_Register( (gapBondCBs_t *) &simpleBLEBondCB );

    return ( events ^ START_DEVICE_EVT );
  }

  if ( events & START_DISCOVERY_EVT )
  {
    simpleBLECentralStartDiscovery( );
    
    return ( events ^ START_DISCOVERY_EVT );
  }
  
  if ( events & SBP_PERIODIC_EVT )
  {
    // Restart timer
    if ( SBP_PERIODIC_EVT_PERIOD && simpleBLEState == BLE_STATE_CONNECTED )
    {
      osal_start_timerEx( simpleBLETaskId, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
    }

    // Perform periodic application task
    performPeriodicTask();

    return (events ^ SBP_PERIODIC_EVT);
  }
  
  // Discard unknown events
  return 0;
}
/*********************************************************************************************
 * @fn      simpleBLECentralEventCB
 *
 * @brief   Central event callback function.
 *
 * @param   pEvent - pointer to event structure
 *
 * @return  none
 **********************************************************************************************/
static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent ){
  switch ( pEvent->gap.opcode ){
                case GAP_DEVICE_INIT_DONE_EVENT:{  
       			    //LCDPrintText("Central Initialized",0,PRINT_STRING);
                            osal_memcpy(mac_buffer[0],pEvent->initDone.devAddr, MAC_LEN);        		               
                            HalLcdWriteString(bdAddr2Str( mac_buffer[0]), HAL_LCD_LINE_1); 
			    
                            
                            uint32 random_scan_duration =500;//to avoid normal scan be discarded by the timer,so its lasting-time should be short
                            GAP_SetParamValue( TGAP_GEN_DISC_SCAN, random_scan_duration );        //random scan duration
                            //LCDPrintText("discovering",0,PRINT_STRING);
                            
                            uint32 timeout_value = 200;				// timeout_value - in milliseconds.
                            osal_start_timerEx(MasterSlaveSwitchTaskID, PERIOD_DETECH_EVT, timeout_value);
                            

                            
                            uint8 return_status;
                            if(return_status = GAPCentralRole_StartDiscovery(DISCOVERY_MODE, ACTIVE_SCAN, DISCOVERY_WHITE_LIST ))
                                 	LCDPrintText("discovery error:",return_status,PRINT_VALUE); 
			    break;
                }
      	
      
                case GAP_DEVICE_INFO_EVENT:{						//find a new device 				     			
				// filtering device discovery results based on service UUID
                            //LCDPrintText("find new device",0,PRINT_STRING);   
                            if(simpleBLEScanRes >= MAX_SCAN_RES){
                                            GAPCentralRole_CancelDiscovery();
                                            break;
                            }
                            
                            if ( simpleBLEFindSvcUuid(WANTED_SERVICE_UUID, pEvent->deviceInfo.pEvtData, pEvent->deviceInfo.dataLen) ){
         				simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
                                   //GAPCentralRole_CancelDiscovery();     //stop discoverying                                    
                            }			
			    break;
                }
      		           
   	 	case GAP_DEVICE_DISCOVERY_EVENT:{   			//discaovery has completed 
                           osal_stop_timerEx(MasterSlaveSwitchTaskID,PERIOD_DETECH_EVT);
                            //LCDPrintText("disca completed ",0,PRINT_STRING);    
        		    if ( simpleBLEScanRes > 0 ){
	          			// connect to current device in scan result                                                                             
                                        uint8 random_select = random_num%simpleBLEScanRes;
                                        //LCDPrintText("random_select ",random_select,PRINT_STRING); 
					CurrentConnectionInfo.MacType= simpleBLEDevList[random_select].addrType;
	     		 		CurrentConnectionInfo.MacAddr= simpleBLEDevList[random_select].addr; 
          				uint8 return_status;
                                        if(return_status = GAPCentralRole_EstablishLink( LINK_HIGH_DUTY_CYCLE, LINK_WHITE_LIST, CurrentConnectionInfo.MacType, CurrentConnectionInfo.MacAddr)){
                                                  LCDPrintText("Link Error",return_status,PRINT_VALUE); 
                                                  osal_set_event(MasterSlaveSwitchTaskID, MSS_CHANGE_ROLE_EVT);
                                        } 
			    }

                            else{
					//LCDPrintText("no device found",0,PRINT_STRING);                                        
                                       	osal_set_event(MasterSlaveSwitchTaskID, MSS_CHANGE_ROLE_EVT);     //switch to periperal                                        
                            }


			    break;
      		}
      
    		case GAP_LINK_ESTABLISHED_EVENT:{
      	 		    if ( pEvent->gap.hdr.status == SUCCESS ){ 
                                        
					//LCDPrintText("Connected",0,PRINT_STRING);     				
          				CurrentConnectionInfo.Handle= pEvent->linkCmpl.connectionHandle;  
                                        
                                        if(CharHandleSearchFlag == 1)
         				      simpleBLECentralStartDiscovery(); 
                                        else{
                                              ClientWriteValue();                                              
                                              CharSendingFlag =1;
                                              //LCDPrintText("NO NEED TO",0,PRINT_STRING);  
                                        }
      	 		    }
       			
       			    else{				                                        
                                        LCDPrintText("Connect Failed ",pEvent->gap.hdr.status,PRINT_VALUE);
                                            osal_set_event(MasterSlaveSwitchTaskID, MSS_CHANGE_ROLE_EVT);
       			    	}

			    break;
      		}
      

    		case GAP_LINK_TERMINATED_EVENT:{       
                                        osal_set_event(MasterSlaveSwitchTaskID, MSS_CHANGE_ROLE_EVT);
      			    break;
      		}

    		case GAP_LINK_PARAM_UPDATE_EVENT:
      				break;
      
    		default:
      				break;
  	}
}