/////////
// Battery Level
/////////
int BeanSerialTransport::batteryRead( uint8_t* level )
{
  size_t size = 1;
  return call_and_response( MSG_ID_CC_BATT_READ, NULL,
                      (size_t) 0, (uint8_t *)level, &size );

}
void BeanSerialTransport::BTSetLocalName(const char* name) {
    if(name == NULL)
        name = "";

    size_t length = strlen(name);
    if ( length > 20 )
    {
        length = 20;
    }

    BT_RADIOCONFIG_T radioConfig;
    size_t size = sizeof(BT_RADIOCONFIG_T);
    int response = call_and_response(MSG_ID_BT_GET_CONFIG, NULL, 0, (uint8_t *)&radioConfig, &size );

    if ( 0 == response )
    {
        memcpy( (void*)radioConfig.local_name, (void*)name, length );
        radioConfig.local_name_size = length;

        uint16_t msgId = ( m_enableSave ? MSG_ID_BT_SET_CONFIG: MSG_ID_BT_SET_CONFIG_NOSAVE );

        write_message(msgId, (const uint8_t*)&radioConfig, size);
    }

};
int BeanSerialTransport::BTGetScratchChar(uint8_t scratchNum, ScratchData * scratchData){
  int rtnVal;
  size_t lengthSt = sizeof(BT_SCRATCH_T);
  uint8_t buffer[1] = {scratchNum};

  rtnVal = call_and_response(MSG_ID_BT_GET_SCRATCH, buffer,
                        (size_t)1, (uint8_t*)scratchData, &lengthSt);
  // magic: -1 for length byte
  scratchData->length = (uint8_t)lengthSt - 1;

  return rtnVal;
};
//Debug
bool BeanSerialTransport::debugLoopbackVerify(const uint8_t *message,
                                              const size_t size){
  uint8_t res[size];
  size_t  res_size = size;
  if(call_and_response(MSG_ID_DB_LOOPBACK, message, size, res, &res_size) != 0){
    return false;
  }

  for(unsigned int i = 0; i < size; i++){
    if(message[i] != res[i])
      return false;
  }

  return true;
};
void BeanSerialTransport::BTBeaconModeEnable( bool beaconEnable )
{
    BT_RADIOCONFIG_T radioConfig;
    size_t size = sizeof(BT_RADIOCONFIG_T);
    int response = call_and_response(MSG_ID_BT_GET_CONFIG, NULL, 0, (uint8_t *)&radioConfig, &size );

    if ( 0 == response )
    {
        radioConfig.adv_mode = ( beaconEnable ? ADV_IBEACON : ADV_STANDARD);
        uint16_t msgId = ( m_enableSave ? MSG_ID_BT_SET_CONFIG : MSG_ID_BT_SET_CONFIG_NOSAVE );

        write_message(msgId, (const uint8_t*)&radioConfig,
                      size);
    }
}
bool BeanSerialTransport::debugEndToEndLoopbackVerify(const uint8_t *message,
                                                      const size_t size){
  uint8_t res[size];
  size_t  res_size = size;
  // note that we've set the timeout to 250 here and not the default 100ms as
  // this is going to the phone and back.
  if(call_and_response(MSG_ID_DB_E2E_LOOPBACK, message, size, res, &res_size, 250) != 0){
    return false;
  }

  for(unsigned int i = 0; i < size; i++){
    if(message[i] != res[i])
      return false;
  }

  return true;
};
void BeanSerialTransport::BTSetBeaconParams(uint16_t uuid, uint16_t majorid, uint16_t minorid )
{
    BT_RADIOCONFIG_T radioConfig;
    size_t size = sizeof(BT_RADIOCONFIG_T);
    int response = call_and_response(MSG_ID_BT_GET_CONFIG, NULL, 0, (uint8_t *)&radioConfig, &size );

    if ( 0 == response )
    {
        radioConfig.ibeacon_uuid = uuid;
        radioConfig.ibeacon_major = majorid;
        radioConfig.ibeacon_minor = minorid;

        uint16_t msgId = ( m_enableSave ? MSG_ID_BT_SET_CONFIG : MSG_ID_BT_SET_CONFIG_NOSAVE );

        write_message(msgId, (const uint8_t*)&radioConfig,
                      size);
    }

}
void BeanSerialTransport::BTSetAdvertisingInterval( uint16_t interval_ms) {
    if(interval_ms < BEAN_MIN_ADVERTISING_INT_MS)
    {
        interval_ms = BEAN_MIN_ADVERTISING_INT_MS;
    }
    else if(interval_ms > BEAN_MAX_ADVERTISING_INT_MS)
    {
        interval_ms = BEAN_MAX_ADVERTISING_INT_MS;
    }

    BT_RADIOCONFIG_T radioConfig;
    size_t size = sizeof(BT_RADIOCONFIG_T);
    int response = call_and_response(MSG_ID_BT_GET_CONFIG, NULL, 0, (uint8_t *)&radioConfig, &size );

    if ( 0 == response )
    {
        radioConfig.adv_int = (uint16_t)interval_ms;

        uint16_t msgId = ( m_enableSave ? MSG_ID_BT_SET_CONFIG: MSG_ID_BT_SET_CONFIG_NOSAVE );

        write_message(msgId, (const uint8_t*)&radioConfig, size);
    }
}
int  BeanSerialTransport::debugGetDebugCounter(int* counter){
  size_t return_size;
  return call_and_response(MSG_ID_DB_COUNTER, NULL, 0, (uint8_t*)counter, &return_size);
};
int BeanSerialTransport::temperatureRead( int8_t* tempRead )
{
  size_t size = 1;
  return call_and_response( MSG_ID_CC_TEMP_READ, NULL,
                        (size_t) 0, (uint8_t *) tempRead, &size);
}
int BeanSerialTransport::accelRead(ACC_READING_T* reading){
  size_t size = sizeof(ACC_READING_T);
  return call_and_response(MSG_ID_CC_ACCEL_READ, NULL,
                        (size_t) 0, (uint8_t *) reading, &size);
}
int BeanSerialTransport::ledRead(LED_SETTING_T* reading){
  size_t size = sizeof(LED_SETTING_T);
  return call_and_response(MSG_ID_CC_LED_READ_ALL, NULL,
                        0, (uint8_t *) reading, &size);
}
int  BeanSerialTransport::BTGetConfig(BT_RADIOCONFIG_T *config){
  size_t size = sizeof(BT_RADIOCONFIG_T);
  return call_and_response(MSG_ID_BT_GET_CONFIG, NULL,
                        0, (uint8_t *) config, &size);
};
int BeanSerialTransport::accelRangeRead( uint8_t *range)
{
    size_t size = sizeof(uint8_t);
    return call_and_response(MSG_ID_CC_ACCEL_GET_RANGE, NULL,
                             (size_t) 0, (uint8_t *)range, &size);
}
int BeanSerialTransport::BTGetStates(BT_STATES_T * btStates )
{
    size_t length = sizeof(BT_STATES_T);
    return call_and_response(MSG_ID_BT_GET_STATES, NULL,
                             (size_t)0, (uint8_t*)btStates, &length);
}