/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    AliroSvc_app.c
  * @author  MCD Application Team
  * @brief   AliroSvc_app application definition.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "app_common.h"
#include "log_module.h"
#include "app_ble.h"
#include "ll_sys_if.h"
#include "dbg_trace.h"
#include "alirosvc_app.h"
#include "alirosvc.h"
#include "stm32_rtos.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "app_aliro.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

typedef enum
{
  /* USER CODE BEGIN Service1_APP_SendInformation_t */

  /* USER CODE END Service1_APP_SendInformation_t */
  ALIROSVC_APP_SENDINFORMATION_LAST
} ALIROSVC_APP_SendInformation_t;

typedef struct
{
  /* USER CODE BEGIN Service1_APP_Context_t */

  /* USER CODE END Service1_APP_Context_t */
  uint16_t              ConnectionHandle;
} ALIROSVC_APP_Context_t;

/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* External variables --------------------------------------------------------*/
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/* Private macros ------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
static ALIROSVC_APP_Context_t ALIROSVC_APP_Context;

uint8_t a_ALIROSVC_UpdateCharData[247];

/* USER CODE BEGIN PV */

// uint8_t DataBuf[] = { 0x00, 0x8F, 0x02, 0x01, 0x00 };
// uint8_t DataBuf[] = { 0x00, 0x8F, 0x02, 0x01, 0x00, 0x01, 0x00 };

uint8_t a_AdvData2[31] = {0};

//mobile app
      // uint8_t a_AdvData2[] = {
      //   0x02, 0x01, 0x06, 0x1B, 0x16, 0xF2, 0xFF, 0x00, 
      //   0xEB, 0x8F, 0x96, 0x33, 0xDD, 0x85, 0x6D, 0x2A, 
      //   0x15, 0x43, 0x81, 0x66, 0x10, 0x0F, 0xE0, 0x00, 
      //   0xA2, 0x0D, 0xB8, 0x1B, 0xF9, 0x86, 0xFA };

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE BEGIN PFP */

void ALIROSVC_APP_Init_cb(void);
void ALIROSVC_D_AC_PV_WRITE_EVT_cb(uint8_t *ptr, uint32_t size);

/* USER CODE END PFP */

/* Functions Definition ------------------------------------------------------*/
void ALIROSVC_Notification(ALIROSVC_NotificationEvt_t *p_Notification)
{
  /* USER CODE BEGIN Service1_Notification_1 */

  /* USER CODE END Service1_Notification_1 */
  switch(p_Notification->EvtOpcode)
  {
    /* USER CODE BEGIN Service1_Notification_Service1_EvtOpcode */

    /* USER CODE END Service1_Notification_Service1_EvtOpcode */

    case ALIROSVC_R_SPSM_READ_EVT:
      /* USER CODE BEGIN Service1Char1_READ_EVT */

      LOG_INFO_APP("ALIROSVC_R_SPSM_READ_EVT\r\n");
      
      /* USER CODE END Service1Char1_READ_EVT */
      break;

    case ALIROSVC_D_AC_PV_WRITE_EVT:
      /* USER CODE BEGIN Service1Char2_WRITE_EVT */
      
      ALIROSVC_D_AC_PV_WRITE_EVT_cb(p_Notification->DataTransfered.p_Payload, p_Notification->DataTransfered.Length);

      
      /* USER CODE END Service1Char2_WRITE_EVT */
      break;

    default:
      /* USER CODE BEGIN Service1_Notification_default */

      LOG_INFO_APP("ALIROSVC_Notification EvtOpcode:(%d) \r\n", p_Notification->EvtOpcode);
      
      /* USER CODE END Service1_Notification_default */
      break;
  }
  /* USER CODE BEGIN Service1_Notification_2 */

  /* USER CODE END Service1_Notification_2 */
  return;
}

void ALIROSVC_APP_EvtRx(ALIROSVC_APP_ConnHandleNotEvt_t *p_Notification)
{
  /* USER CODE BEGIN Service1_APP_EvtRx_1 */

  /* USER CODE END Service1_APP_EvtRx_1 */

  switch(p_Notification->EvtOpcode)
  {
    /* USER CODE BEGIN Service1_APP_EvtRx_Service1_EvtOpcode */

    /* USER CODE END Service1_APP_EvtRx_Service1_EvtOpcode */
    case ALIROSVC_CONN_HANDLE_EVT :
      /* USER CODE BEGIN Service1_APP_CONN_HANDLE_EVT */

      /* USER CODE END Service1_APP_CONN_HANDLE_EVT */
      break;

    case ALIROSVC_DISCON_HANDLE_EVT :
      /* USER CODE BEGIN Service1_APP_DISCON_HANDLE_EVT */

      /* USER CODE END Service1_APP_DISCON_HANDLE_EVT */
      break;

    default:
      /* USER CODE BEGIN Service1_APP_EvtRx_default */

      /* USER CODE END Service1_APP_EvtRx_default */
      break;
  }

  /* USER CODE BEGIN Service1_APP_EvtRx_2 */

  /* USER CODE END Service1_APP_EvtRx_2 */

  return;
}

void ALIROSVC_APP_Init(void)
{
  UNUSED(ALIROSVC_APP_Context);
  ALIROSVC_Init();

  /* USER CODE BEGIN Service1_APP_Init */

  ALIROSVC_APP_Init_cb();
  
  /* USER CODE END Service1_APP_Init */
  return;
}

/* USER CODE BEGIN FD */

/* USER CODE END FD */

/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/

/* USER CODE BEGIN FD_LOCAL_FUNCTIONS */

void ALIROSVC_APP_Init_cb(void)
{
  ALIROSVC_Data_t DataStruct;
  AliroSvcParamGetInitBuf(&DataStruct.p_Payload);
  AliroSvcParamGetInitLen(&DataStruct.Length);
  
  ALIROSVC_UpdateValue(ALIROSVC_R_SPSM, &DataStruct);
}


void ALIROSVC_D_AC_PV_WRITE_EVT_cb(uint8_t *ptr, uint32_t size)
{
  LOG_INFO_APP("#ALIROSVC_D_AC_PV_WRITE_EVT\r\n");
  LOG_INFO_APP("\t- Write received %d bytes: ", size);
  for(uint32_t i=0;i<size;i++)
  {
    LOG_INFO_APP("%.2X ", ptr[i]);
  }
  LOG_INFO_APP("\r\n");
  
  if (size >= 2)
  {
    uint16_t selectedProtocolVersion_16 = (uint16_t)ptr[1] | (uint16_t)ptr[0]<<8;
    if(0==AliroSvcVerifyProtVer(selectedProtocolVersion_16))
    {
      //protocol ok
      LOG_INFO_APP("ALIRO Protocol version OK\r\n");
    }
    else
    {
      //protocol verification failed
      APP_BLE_Procedure_Gap_General(PROC_GAP_GEN_CONN_TERMINATE);
    }
  }
  else
  {
    LOG_INFO_APP("ERROR: Wrong size\r\n");
  }
}


void Update_a_AdvData(uint8_t **ptr, uint8_t *size)
{
  *size = sizeof(a_AdvData2);
  fillAdvBuffer((uint8_t*) a_AdvData2, size);

  *ptr = a_AdvData2;
}

void Adv_Request_cb(APP_BLE_ConnStatus_t NewStatus)
{ 
  APP_BLE_Procedure_Gap_Peripheral(PROC_GAP_PERIPH_ADVERTISE_START_FAST);
}

void Adv_Stop_cb(APP_BLE_ConnStatus_t NewStatus)
{ 
  APP_BLE_Procedure_Gap_Peripheral(PROC_GAP_PERIPH_ADVERTISE_STOP);
}

/* USER CODE END FD_LOCAL_FUNCTIONS */
