/* The following function is the main user interface thread. It */ /* opens the Bluetooth Stack and then drives the main user interface.*/ int main(void) { /* Configure the hardware for its intended use. */ HAL_ConfigureHardware(1); /* Enable interrupts and call the main application thread. */ MainThread(); /* MainThread should run continously, if it exits an error occured. */ while(1) { ToggleLED(NULL); BTPS_Delay(100); } } /* The following function is the main user interface thread. It */ /* opens the Bluetooth Stack and then drives the main user interface.*/ static void MainThread(void) { int Result; BTPS_Initialization_t BTPS_Initialization; HCI_DriverInformation_t HCI_DriverInformation; HCI_HCILLConfiguration_t HCILLConfig; HCI_Driver_Reconfigure_Data_t DriverReconfigureData; /* Configure the UART Parameters. */ HCI_DRIVER_SET_COMM_INFORMATION(&HCI_DriverInformation, 1, VENDOR_BAUD_RATE, cpHCILL_RTS_CTS); HCI_DriverInformation.DriverInformation.COMMDriverInformation.InitializationDelay = 100; /* Set up the application callbacks. */ BTPS_Initialization.GetTickCountCallback = GetTickCallback; BTPS_Initialization.MessageOutputCallback = DisplayCallback; /* Initialize the application. */ if((Result = InitializeApplication(&HCI_DriverInformation, &BTPS_Initialization)) > 0) { /* Register a sleep mode callback if we are using HCILL Mode. */ if((HCI_DriverInformation.DriverInformation.COMMDriverInformation.Protocol == cpHCILL) || (HCI_DriverInformation.DriverInformation.COMMDriverInformation.Protocol == cpHCILL_RTS_CTS)) { HCILLConfig.SleepCallbackFunction = HCI_Sleep_Callback; HCILLConfig.SleepCallbackParameter = 0; DriverReconfigureData.ReconfigureCommand = HCI_COMM_DRIVER_RECONFIGURE_DATA_COMMAND_CHANGE_HCILL_PARAMETERS; DriverReconfigureData.ReconfigureData = (void *)&HCILLConfig; /* Register the sleep mode callback. Note that if this */ /* function returns greater than 0 then sleep is currently */ /* enabled. */ Result = HCI_Reconfigure_Driver((unsigned int)Result, FALSE, &DriverReconfigureData); if(Result > 0) { /* Flag that sleep mode is enabled. */ Display(("Sleep is allowed.\r\n")); } } /* We need to execute Add a function to process the command line */ /* to the BTPS Scheduler. */ if(BTPS_AddFunctionToScheduler(ReadFunction, NULL, 10)) { /* Add the idle function (which determines if LPM3 may be */ /* entered) to the scheduler. */ if(BTPS_AddFunctionToScheduler( Writefunction, NULL, 10)) { /* Loop forever and execute the scheduler. */ while(1) BTPS_ExecuteScheduler(); } } } } static void ReadFunction(void *UserParameter) { //xxx Fill in to put processor into low power mode ProcessCommandLine1(); } static void Writefunction(void *UserParameter) { //xxx Fill in to put processor into low power mode ProcessCommandLine2(); } void ProcessCommandLine1() { //Display(("Read data\r\n")); ReadDataCommand(NULL); } void ProcessCommandLine2() { //Display(("Write data\r\n")); SendDataCommand(NULL); } /* The following function is responsible for reading data sent by a */ /* remote device to which a connection exists. This function will */ /* return zero on successful execution and a negative value on */ /* errors. */ static int ReadDataCommand(ParameterList_t *TempParam) { int LEConnectionIndex; Boolean_t Done; // char BT_address[12] = "d78206cba936"; // char BT_address[12] = "f43538fe9c18"; char BT_address[12] = "cc78ab7945c2"; BD_ADDR_t BD_ADDR; unsigned int Temp; DeviceInfo_t *DeviceInfo; /* Next, make sure that a valid device address exists. */ // if((TempParam) && (TempParam->NumberofParameters > 0) && (TempParam->Params[0].strParam) && (BTPS_StringLength(TempParam->Params[0].strParam) >= (sizeof(BD_ADDR_t)*2))) //{ /* Convert the parameter to a Bluetooth Device Address. */ // StrToBD_ADDR(TempParam->Params[0].strParam, &BD_ADDR); StrToBD_ADDR(BT_address, &BD_ADDR); /* Find the LE Connection Index for this connection. */ if((LEConnectionIndex = FindLEIndexByAddress(BD_ADDR)) >= 0) { /* Get the device info for the connection device. */ if((DeviceInfo = SearchDeviceInfoEntryByBD_ADDR(&DeviceInfoList, LEContextInfo[LEConnectionIndex].ConnectionBD_ADDR)) != NULL) { /* Determine the number of bytes we are going to read. */ Temp = LEContextInfo[LEConnectionIndex].SPPLEBufferInfo.ReceiveBuffer.BufferSize - LEContextInfo[LEConnectionIndex].SPPLEBufferInfo.ReceiveBuffer.BytesFree; Display(("Read: %u.\r\n", Temp)); /* Loop and read all of the data. */ Done = FALSE; while(!Done) { /* Read the data. */ Temp = SPPLEReadData(&(LEContextInfo[LEConnectionIndex]), DeviceInfo, SPPLE_DATA_BUFFER_LENGTH, SPPLEBuffer); if(Temp > 0) { /* Display the data. */ SPPLEBuffer[Temp] = '\0'; Display(((char *)SPPLEBuffer)); } else Done = TRUE; } Display(("\r\n")); } else Display(("No Device Info.\r\n")); } else { /* No matching ConnectionBD_ADDR. */ Display(("No connection with BD_ADDR %s exists.\r\n", TempParam->Params[0].strParam)); } // } //else //{ /* Invalid parameters specified so flag an error to the user. */ // Display(("Usage: LEREAD [BD_ADDR].\r\n")); //} return(0); } /* The following function is responsible for sending a number of */ /* characters to a remote device to which a connection exists. The */ /* function receives a parameter that indicates the number of byte to*/ /* be transferred. This function will return zero on successful */ /* execution and a negative value on errors. */ static int SendDataCommand(ParameterList_t *TempParam) { int LEConnectionIndex; BD_ADDR_t BD_ADDR; DeviceInfo_t *DeviceInfo; char BT_address[12]="cc78ab7945c2"; /* Make sure that all of the parameters required for this function */ /* appear to be at least semi-valid. */ // if((TempParam) && (TempParam->NumberofParameters >= 2) && (TempParam->Params[1].intParam > 0) && (TempParam->Params[0].strParam) && (BTPS_StringLength(TempParam->Params[0].strParam) >= (sizeof(BD_ADDR_t)*2))) //{ /* Convert the parameter to a Bluetooth Device Address. */ StrToBD_ADDR(BT_address, &BD_ADDR); /* Find the LE Connection Index for this connection. */ if((LEConnectionIndex = FindLEIndexByAddress(BD_ADDR)) >= 0) { /* Check to see if we are sending to another port. */ if(!(LEContextInfo[LEConnectionIndex].SPPLEBufferInfo.SendInfo.BytesToSend)) { /* Get the device info for the connection device. */ if((DeviceInfo = SearchDeviceInfoEntryByBD_ADDR(&DeviceInfoList, LEContextInfo[LEConnectionIndex].ConnectionBD_ADDR)) != NULL) { /* Get the count of the number of bytes to send. */ LEContextInfo[LEConnectionIndex].SPPLEBufferInfo.SendInfo.BytesToSend = (Word_t)DataStrLen;//(DWord_t)TempParam->Params[1].intParam; LEContextInfo[LEConnectionIndex].SPPLEBufferInfo.SendInfo.BytesSent = 0; /* Kick start the send process. */ SPPLESendProcess(&(LEContextInfo[LEConnectionIndex]), DeviceInfo); } else Display(("No Device Info.\r\n")); } else Display(("Send Currently in progress.\r\n")); } else { /* No matching ConnectionBD_ADDR. */ Display(("No connection with BD_ADDR %s exists.\r\n", TempParam->Params[0].strParam)); } /* } else DisplayUsage("LESEND [BD_ADDR] [Number of Bytes to send]\r\n");*/ return(0); }