Esempio n. 1
0
   /* instances.                                                        */
static void BTPSAPI GATT_ServerEventCallback(unsigned int BluetoothStackID, GATT_Server_Event_Data_t *GATT_ServerEventData, unsigned long CallbackParameter)
{
   Word_t               AttributeOffset;
   Word_t               ValueLength;
   Byte_t               Event_Buffer[IAS_EVENT_DATA_SIZE + IAS_EVENT_DATA_BUFFER_SIZE];
   unsigned int         TransactionID;
   unsigned int         InstanceID;
   IAS_Event_Data_t    *EventData;
   IASServerInstance_t *ServiceInstance;

    /* Verify that all parameters to this callback are Semi-Valid.      */
   if((BluetoothStackID) && (GATT_ServerEventData) && (CallbackParameter))
   {
      /* The Instance ID is always registered as the callback parameter.*/
      InstanceID = (unsigned int)CallbackParameter;

      /* Acquire the Service Instance for the specified service.        */
      if((ServiceInstance = AcquireServiceInstance(BluetoothStackID, &InstanceID)) != NULL)
      {
         switch(GATT_ServerEventData->Event_Data_Type)
         {
              case etGATT_Server_Write_Request:
               /* Verify that the Event Data is valid.                  */
               if(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data)
               {
                  /* Store the needed members for use later.            */
                  AttributeOffset = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeOffset;
                  TransactionID   = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->TransactionID;
                  ValueLength     = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeValueLength;

                  /* Verify that the parameters look correct.           */
                  if((ValueLength == IAS_ALERT_LEVEL_CONTROL_POINT_VALUE_LENGTH) && (!(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeValueOffset)) && (!(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->DelayWrite)))
                  {
                     /* IAS defines that the Write Without Response     */
                     /* procedure is used to write to this attribute,   */
                     /* therefore we will just accept the write request */
                     /* here to allow GATT to clean up the resources.   */
                     GATT_Write_Response(BluetoothStackID, TransactionID);

                     /* Format and Dispatch the event.                  */
                     EventData = FormatEventHeader(sizeof(Event_Buffer), Event_Buffer, etIAS_Server_Alert_Level_Control_Point_Command, InstanceID, GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->ConnectionID, GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->ConnectionType, &(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->RemoteDevice));
                     if(EventData)
                     {
                        /* Format the rest of the event.                */
                        EventData->Event_Data_Size                                                = IAS_ALERT_LEVEL_CONTROL_POINT_COMMAND_DATA_SIZE;
                        EventData->Event_Data.IAS_Alert_Level_Control_Point_Command_Data->Command = (IAS_Control_Point_Command_t)READ_UNALIGNED_BYTE_LITTLE_ENDIAN(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeValue);

                        __BTPSTRY
                        {
                           (*ServiceInstance->EventCallback)(ServiceInstance->BluetoothStackID, EventData, ServiceInstance->CallbackParameter);
                        }
                        __BTPSEXCEPT(1)
                        {
                           /* Do Nothing.                               */
                        }
                     }
                  }
                  else
                     GATT_Error_Response(BluetoothStackID, TransactionID, AttributeOffset, ATT_PROTOCOL_ERROR_CODE_REQUEST_NOT_SUPPORTED);
               }
Esempio n. 2
0
   /* instances.                                                        */
static void BTPSAPI GATT_ServerEventCallback(unsigned int BluetoothStackID, GATT_Server_Event_Data_t *GATT_ServerEventData, unsigned long CallbackParameter)
{
   Word_t               AttributeOffset;
   Word_t               InstanceTag;
   Word_t               ValueLength;
   Byte_t              *Value;
   unsigned int         TransactionID;
   unsigned int         InstanceID;
   LLSServerInstance_t *ServiceInstance;
   LLS_Event_Data_t    *EventData;
   Byte_t               Event_Buffer[LLS_EVENT_DATA_SIZE + LLS_EVENT_DATA_BUFFER_SIZE];

   /* Verify that all parameters to this callback are Semi-Valid.       */
   if((BluetoothStackID) && (GATT_ServerEventData) && (CallbackParameter))
   {
      /* The Instance ID is always registered as the callback parameter.*/
      InstanceID = (unsigned int)CallbackParameter;
      /* Acquire the Service Instance for the specified service.        */
      if((ServiceInstance = AcquireServiceInstance(BluetoothStackID, &InstanceID)) != NULL)
      {
         switch(GATT_ServerEventData->Event_Data_Type)
         {
         case etGATT_Server_Read_Request:
            /* Verify that the Event Data is valid.                     */
            AttributeOffset = GATT_ServerEventData->Event_Data.GATT_Read_Request_Data->AttributeOffset;
            TransactionID   = GATT_ServerEventData->Event_Data.GATT_Read_Request_Data->TransactionID;

            if(GATT_ServerEventData->Event_Data.GATT_Read_Request_Data)
            {
               /* Verify that they are not trying to write with an      */
               /* offset or using preprared writes.                     */
               if(!(GATT_ServerEventData->Event_Data.GATT_Read_Request_Data->AttributeValueOffset))
               {
                  /* Get the instance tag for the characteristic.       */
                  InstanceTag = (Word_t)(((GATT_Characteristic_Value_16_Entry_t *)Link_Loss_Service[AttributeOffset].Attribute_Value)->Characteristic_Value_Length);
                  ValueLength = READ_UNALIGNED_WORD_LITTLE_ENDIAN(&(((Byte_t *)(&InstanceData[InstanceID-1]))[InstanceTag]));
                  Value       = (Byte_t *)(&(((Byte_t *)(&InstanceData[InstanceID-1]))[InstanceTag + WORD_SIZE]));

                  /* Respond with the data.                             */
                  GATT_Read_Response(BluetoothStackID, TransactionID, (unsigned int)ValueLength, Value);
               }
            }
            else
               GATT_Error_Response(BluetoothStackID, TransactionID, AttributeOffset, ATT_PROTOCOL_ERROR_CODE_ATTRIBUTE_NOT_LONG);
            break;
         case etGATT_Server_Write_Request:
            AttributeOffset = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeOffset;
            TransactionID   = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->TransactionID;
            ValueLength     = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeValueLength;
            Value           = GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->AttributeValue;
            /* Verify the Event data is valid                           */
            if(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data)
            {
               /* Verify that the attribute value is valid.             */
               if((Value) && (ValueLength == LLS_ALERT_LEVEL_LENGTH))
               {
                  /* Format and Dispatch the event.                     */
                  EventData = FormatEventHeader(sizeof(Event_Buffer), Event_Buffer, etLLS_Alert_Level_Update, InstanceID, GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->ConnectionID, NULL, GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->ConnectionType, &(GATT_ServerEventData->Event_Data.GATT_Write_Request_Data->RemoteDevice));
                  if(EventData)
                  {
                     /* Format the rest of the event.                   */
                     EventData->Event_Data_Size = LLS_ALERT_LEVEL_UPDATE_DATA_SIZE;

                     /* Parse out the command.                          */
                     if(!DecodeAlertLevel(InstanceID, ValueLength, Value, &(EventData->Event_Data.LLS_Alert_Level_Update_Data->AlertLevel)))
                     {
                        /* Since the requested Alert Level was formatted*/
                        /* correctly and with the accepted range we will*/
                        /* accept the new Alert Level.                  */
                        GATT_Write_Response(BluetoothStackID, TransactionID);

                        /* Dispatch the event.                          */
                        __BTPSTRY
                        {
                           (*ServiceInstance->EventCallback)(ServiceInstance->BluetoothStackID, EventData, ServiceInstance->CallbackParameter);
                        }
                        __BTPSEXCEPT(1)
                        {
                           /* Do Nothing.                               */
                        }
                     }
                     else
                        GATT_Error_Response(BluetoothStackID, TransactionID, AttributeOffset, ATT_PROTOCOL_ERROR_CODE_UNLIKELY_ERROR);
                  }
                  else
                     GATT_Error_Response(BluetoothStackID, TransactionID, AttributeOffset, ATT_PROTOCOL_ERROR_CODE_UNLIKELY_ERROR);
               }