Bluetooth demo can now create and maintain logical channels - need to determine why...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / BluetoothStack.c
index 7989063..11b6a61 100644 (file)
@@ -54,13 +54,13 @@ void Bluetooth_Stack_USBTask(void)
        Bluetooth_ACLTask();\r
 }\r
 \r
-Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource)\r
+Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchByRemoteChannel)\r
 {\r
        for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)\r
        {\r
                Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];\r
        \r
-               uint16_t CurrentChannelNumber = (SearchBySource) ? ChannelData->RemoteNumber : ChannelData->LocalNumber;\r
+               uint16_t CurrentChannelNumber = (SearchByRemoteChannel) ? ChannelData->RemoteNumber : ChannelData->LocalNumber;\r
        \r
                if (CurrentChannelNumber == ChannelNumber)\r
                  return ChannelData;\r
@@ -71,21 +71,28 @@ Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool Searc
 \r
 Bluetooth_Channel_t* Bluetooth_InitChannelData(uint16_t RemoteChannelNumber, uint16_t PSM)\r
 {\r
-       for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)\r
+       Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(RemoteChannelNumber, false);\r
+\r
+       if (ChannelData == NULL)\r
        {\r
-               Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];\r
-       \r
-               if (ChannelData->State == Channel_Closed)\r
+               for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)\r
                {\r
-                       ChannelData->RemoteNumber = RemoteChannelNumber;\r
-                       ChannelData->LocalNumber  = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);\r
-                       ChannelData->PSM          = PSM;\r
-                       ChannelData->LocalMTU     = MAXIMUM_CHANNEL_MTU;\r
-                       ChannelData->State        = Channel_Config_WaitConfig;\r
-                       \r
-                       return ChannelData;\r
-               }               \r
+                       if (Bluetooth_Connection.Channels[i].State == Channel_Closed)\r
+                       {\r
+                               ChannelData = &Bluetooth_Connection.Channels[i];                                \r
+                               ChannelData->LocalNumber = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);\r
+                               break;\r
+                       }\r
+               }\r
        }\r
 \r
-       return NULL;\r
+       if (ChannelData != NULL)\r
+       {\r
+               ChannelData->RemoteNumber = RemoteChannelNumber;\r
+               ChannelData->PSM          = PSM;\r
+               ChannelData->LocalMTU     = MAXIMUM_CHANNEL_MTU;\r
+               ChannelData->State        = Channel_Config_WaitConfig;\r
+       }\r
+\r
+       return ChannelData;\r
 }\r