/* between module initialization for Threaded and NonThreaded stacks.*/ static Boolean_t InitializeModule(void) { /* All we need to do is flag that we are initialized. */ if(!InstanceListInitialized) { InstanceListInitialized = TRUE; BTPS_MemInitialize(InstanceList, 0, sizeof(InstanceList)); } return(TRUE); }
/* non-zero, Instance ID on success or a negative error code. */ static int LLSRegisterService(unsigned int BluetoothStackID, LLS_Event_Callback_t EventCallback, unsigned long CallbackParameter, unsigned int *ServiceID, GATT_Attribute_Handle_Group_t *ServiceHandleRange) { int ret_val; unsigned int InstanceID; LLSServerInstance_t *ServiceInstance; /* Make sure the parameters passed to us are semi-valid. */ if((BluetoothStackID) && (ServiceID)) { /* Verify that no instance is registered to this Bluetooth Stack. */ if(!InstanceRegisteredByStackID(BluetoothStackID)) { /* Acquire a free LLS Instance. */ InstanceID = 0; if((ServiceInstance = AcquireServiceInstance(BluetoothStackID, &InstanceID)) != NULL) { /* Call GATT to register the LLS service. */ ret_val = GATT_Register_Service(BluetoothStackID, LLS_SERVICE_FLAGS, LINK_LOSS_SERVICE_ATTRIBUTE_COUNT, (GATT_Service_Attribute_Entry_t *)Link_Loss_Service, ServiceHandleRange, GATT_ServerEventCallback, InstanceID); if(ret_val > 0) { /* Save the Instance information. */ ServiceInstance->BluetoothStackID = BluetoothStackID; ServiceInstance->ServiceID = (unsigned int)ret_val; ServiceInstance->EventCallback = EventCallback; ServiceInstance->CallbackParameter = CallbackParameter; *ServiceID = (unsigned int)ret_val; /* Intilize the Instance Data for this instance. */ BTPS_MemInitialize(&InstanceData[InstanceID-1], 0,LLS_INSTANCE_DATA_SIZE); ASSIGN_HOST_WORD_TO_LITTLE_ENDIAN_UNALIGNED_WORD(&(InstanceData[InstanceID-1].Alert_Level_Length), LLS_ALERT_LEVEL_LENGTH); /* Return the LLS Instance ID. */ ret_val = (int)InstanceID; } /* UnLock the previously locked Bluetooth Stack. */ BSC_UnLockBluetoothStack(BluetoothStackID); } else ret_val = LLS_ERROR_INSUFFICIENT_RESOURCES; } else ret_val = LLS_ERROR_SERVICE_ALREADY_REGISTERED; } else ret_val = LLS_ERROR_INVALID_PARAMETER; /* Finally return the result to the caller. */ return(ret_val); }
/* FALSE. */ void CleanupTPSModule(Boolean_t ForceCleanup) { /* Check to make sure that this module has been initialized. */ if(InstanceListInitialized) { /* Wait for access to the TPS Context List. */ if((ForceCleanup) || ((!ForceCleanup) && (BSC_AcquireListLock()))) { /* Cleanup the Instance List. */ BTPS_MemInitialize(InstanceList, 0, sizeof(InstanceList)); if(!ForceCleanup) BSC_ReleaseListLock(); } /* Cleanup the module. */ CleanupModule(); } }
/* * NOTE * BD_ADDR is NOT optional and may NOT be set to NULL. */ static IAS_Event_Data_t *FormatEventHeader(unsigned int BufferLength, Byte_t *Buffer, IAS_Event_Type_t EventType, unsigned int InstanceID, unsigned int ConnectionID, GATT_Connection_Type_t ConnectionType, BD_ADDR_t *BD_ADDR) { IAS_Event_Data_t *EventData = NULL; if((BufferLength >= (IAS_EVENT_DATA_SIZE + IAS_EVENT_DATA_BUFFER_SIZE)) && (Buffer) && (BD_ADDR)) { /* Format the header of the event, that is data that is common to */ /* all events. */ BTPS_MemInitialize(Buffer, 0, BufferLength); EventData = (IAS_Event_Data_t *)Buffer; EventData->Event_Data_Type = EventType; EventData->Event_Data.IAS_Alert_Level_Control_Point_Command_Data = (IAS_Alert_Level_Control_Point_Command_Data_t *)(((Byte_t *)EventData) + IAS_EVENT_DATA_SIZE); EventData->Event_Data.IAS_Alert_Level_Control_Point_Command_Data->InstanceID = InstanceID; EventData->Event_Data.IAS_Alert_Level_Control_Point_Command_Data->ConnectionID = ConnectionID; EventData->Event_Data.IAS_Alert_Level_Control_Point_Command_Data->ConnectionType = ConnectionType; EventData->Event_Data.IAS_Alert_Level_Control_Point_Command_Data->RemoteDevice = *BD_ADDR; } /* Finally return the result to the caller. */ return(EventData); }
/* successful or a negative return error code if an error occurs. */ int BTPSAPI TPS_Cleanup_Service(unsigned int BluetoothStackID, unsigned int InstanceID) { int ret_val; TPSServerInstance_t *ServiceInstance; /* Make sure the parameters passed to us are semi-valid. */ if((BluetoothStackID) && (InstanceID)) { /* Acquire the specified TPS Instance. */ if((ServiceInstance = AcquireServiceInstance(BluetoothStackID, &InstanceID)) != NULL) { /* Verify that the service is actually registered. */ if(ServiceInstance->ServiceID) { /* Call GATT to un-register the service. */ GATT_Un_Register_Service(BluetoothStackID, ServiceInstance->ServiceID); /* Mark the instance entry as being free. */ BTPS_MemInitialize(ServiceInstance, 0, TPS_SERVER_INSTANCE_DATA_SIZE); /* Return success to the caller. */ ret_val = 0; } else ret_val = TPS_ERROR_INVALID_PARAMETER; /* UnLock the previously locked Bluetooth Stack. */ BSC_UnLockBluetoothStack(BluetoothStackID); } else ret_val = TPS_ERROR_INVALID_INSTANCE_ID; } else ret_val = TPS_ERROR_INVALID_PARAMETER; /* Finally return the result to the caller. */ return(ret_val); }
/* negative return value to signify an error. */ int BTPSAPI HCITR_COMOpen(HCI_COMMDriverInformation_t *COMMDriverInformation, HCITR_COMDataCallback_t COMDataCallback, unsigned long CallbackParameter) { int ret_val; volatile char dummy = 0; /* First, make sure that the port is not already open and make sure */ /* that valid COMM Driver Information was specified. */ if((!HCITransportOpen) && (COMMDriverInformation) && (COMDataCallback)) { /* Initialize the return value for success. */ ret_val = TRANSPORT_ID; /* Note the COM Callback information. */ _COMDataCallback = COMDataCallback; _COMCallbackParameter = CallbackParameter; /* Try to Open the port for Reading/Writing. */ BTPS_MemInitialize(&UartContext, 0, sizeof(UartContext_t)); UartContext.UartBase = BT_UART_MODULE_BASE; UartContext.ID = 1; UartContext.RxBufferSize = DEFAULT_INPUT_BUFFER_SIZE; UartContext.RxBytesFree = DEFAULT_INPUT_BUFFER_SIZE; UartContext.XOffLimit = XOFF_LIMIT; UartContext.XOnLimit = XON_LIMIT; UartContext.TxBufferSize = DEFAULT_OUTPUT_BUFFER_SIZE; UartContext.TxBytesFree = DEFAULT_OUTPUT_BUFFER_SIZE; UartContext.SuspendState = hssNormal; /* Check to see if this is the first time that the port has been */ /* opened. */ if(!HCITransportOpen) { /* Configure the Bluetooth Slow Clock. */ BT_CONFIG_SLOW_CLOCK(); /* Configure the TXD and RXD pins as UART peripheral pins. */ BT_CONFIG_UART_PINS(); /* configures the RTS and CTS lines. */ BT_CONFIG_RTS_PIN(); BT_CONFIG_CTS_PIN(); /* disable Flow through Local RTS line. */ FLOW_OFF(); /* configure the Device Reset line */ BT_CONFIG_RESET(); /* Set the Baud rate up. */ HAL_CommConfigure(UartContext.UartBase, BLUETOOTH_STARTUP_BAUD_RATE, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); /* Disable Tx Flow, later we will check RTS and see if we */ /* should enable it, but enable our receive flow. */ DISABLE_INTERRUPTS(); UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_FLOW_ENABLED); UartContext.Flags |= UART_CONTEXT_FLAG_FLOW_ENABLED; ENABLE_INTERRUPTS(); /* Bring the Bluetooth Device out of Reset. */ BT_DEVICE_RESET(); BTPS_Delay(10); BT_DEVICE_UNRESET(); /* Bring CTS Line Low to Indicate that we are ready to receive.*/ FLOW_ON(); /* Check to see if we need to enable Tx Flow. */ if(BT_CTS_READ()) { /* CTS is High so we cannot send data at this time. We will */ /* configure the CTS Interrupt to be Negative Edge Active. */ DISABLE_INTERRUPTS(); UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_FLOW_ENABLED); BT_CTS_INT_NEG_EDGE(); ENABLE_INTERRUPTS(); } else { /* CTS is low and ergo we may send data to the controller. */ /* The CTS interrupt will be set to fire on the Positive */ /* Edge. */ DISABLE_INTERRUPTS(); UartContext.Flags |= (UART_CONTEXT_FLAG_TX_FLOW_ENABLED); BT_CTS_INT_POS_EDGE(); ENABLE_INTERRUPTS(); } /* Clear any data that is in the Buffer. */ FlushRxFIFO(UartContext.UartBase); /* Enable Receive interrupt. */ UARTIntEnableReceive(UartContext.UartBase); /* Disable Transmit Interrupt. */ UARTIntDisableTransmit(UartContext.UartBase); DISABLE_INTERRUPTS(); /* Flag that the UART Tx Buffer will need to be primed. */ UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_PRIMED); /* Enable the transmit functionality. */ UartContext.Flags |= UART_CONTEXT_FLAG_TRANSMIT_ENABLED; ENABLE_INTERRUPTS(); /* Check to see if we need to delay after opening the COM Port.*/ if(COMMDriverInformation->InitializationDelay) BTPS_Delay(COMMDriverInformation->InitializationDelay); /* Flag that the HCI Transport is open. */ HCITransportOpen = 1; } } else ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; return(ret_val); }
/* success. */ int BTPSAPI HCITR_COMOpen(HCI_COMMDriverInformation_t *COMMDriverInformation, HCITR_COMDataCallback_t COMDataCallback, unsigned long CallbackParameter) { int ret_val; /* First, make sure that the port is not already open and make sure */ /* that valid COMM Driver Information was specified. */ if((!HCITransportOpen) && (COMMDriverInformation) && (COMDataCallback)) { /* Initialize the return value for success. */ ret_val = TRANSPORT_ID; /* Note the COM Callback information. */ _COMDataCallback = COMDataCallback; _COMCallbackParameter = CallbackParameter; /* Initialize the UART Context Structure. */ BTPS_MemInitialize(&UartContext, 0, sizeof(UartContext_t)); UartContext.Base = HCI_UART_BASE; UartContext.IntBase = HCI_UART_INT; UartContext.ID = 1; UartContext.FlowInfo = UART_CONTEXT_FLAG_FLOW_CONTROL_ENABLED; UartContext.XOnLimit = DEFAULT_XON_LIMIT; UartContext.XOffLimit = DEFAULT_XOFF_LIMIT; UartContext.RxBufferSize = DEFAULT_INPUT_BUFFER_SIZE; UartContext.RxBytesFree = DEFAULT_INPUT_BUFFER_SIZE; UartContext.TxBufferSize = DEFAULT_OUTPUT_BUFFER_SIZE; UartContext.TxBytesFree = DEFAULT_OUTPUT_BUFFER_SIZE; /* Flag that the Rx Thread should not delete itself. */ RxThreadDeleted = FALSE; /* Check to see if this is the first time that the port has been */ /* opened. */ if(!Handle) { /* Configure the UART module and the GPIO pins used by the */ /* UART. */ MAP_SysCtlPeripheralEnable(HCI_UART_GPIO_PERIPH); MAP_SysCtlPeripheralEnable(HCI_UART_RTS_GPIO_PERIPH); MAP_SysCtlPeripheralEnable(HCI_UART_CTS_GPIO_PERIPH); MAP_SysCtlPeripheralEnable(HCI_UART_PERIPH); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_RX); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_TX); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_RTS); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_CTS); MAP_GPIOPinTypeUART(HCI_UART_GPIO_BASE, HCI_UART_PIN_RX | HCI_UART_PIN_TX); MAP_GPIOPinTypeUART(HCI_UART_RTS_GPIO_BASE, HCI_UART_PIN_RTS); MAP_GPIOPinTypeUART(HCI_UART_CTS_GPIO_BASE, HCI_UART_PIN_CTS); UARTFlowControlSet(UartContext.Base, UART_FLOWCONTROL_RX | UART_FLOWCONTROL_TX); /* Create an Event that will be used to signal that data has */ /* arrived. */ RxDataEvent = BTPS_CreateEvent(FALSE); if(RxDataEvent) { /* Create a thread that will process the received data. */ Handle = BTPS_CreateThread(RxThread, 1600, NULL); if(!Handle) { BTPS_CloseEvent(RxDataEvent); ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; } } else ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; } /* If there was no error, then continue to setup the port. */ if(ret_val != HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT) { /* Configure UART Baud Rate and Interrupts. */ MAP_UARTConfigSetExpClk(UartContext.Base, MAP_SysCtlClockGet(), COMMDriverInformation->BaudRate, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); /* SafeRTOS requires RTOS-aware int handlers to be priority */ /* value 5 or greater */ MAP_IntPrioritySet(UartContext.IntBase, 6 << 5); MAP_IntEnable(UartContext.IntBase); MAP_UARTIntEnable(UartContext.Base, UART_INT_RX | UART_INT_RT); UartContext.Flags |= UART_CONTEXT_FLAG_RX_FLOW_ENABLED; /* Clear any data that is in the Buffer. */ FlushRxFIFO(UartContext.Base); /* Bring the Bluetooth Device out of Reset. */ MAP_GPIOPinWrite(HCI_RESET_BASE, HCI_RESET_PIN, HCI_RESET_PIN); /* Check to see if we need to delay after opening the COM Port.*/ if(COMMDriverInformation->InitializationDelay) BTPS_Delay(COMMDriverInformation->InitializationDelay); /* Flag that the HCI Transport is open. */ HCITransportOpen = 1; } } else ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; return(ret_val); }