Beispiel #1
0
static OSStatus onReceivedData(struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData, size_t inLen, void * inUserContext )
{
  OSStatus err = kUnknownErr;
  const char *    value;
  size_t          valueSize;
  configContext_t *context = (configContext_t *)inUserContext;

  err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL );
  if(err == kNoErr && strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){
    config_log("OTA data %d, %d to: %x", inPos, inLen, context->flashStorageAddress);
#ifdef MICO_FLASH_FOR_UPDATE  
    if(inPos == 0){
      context->flashStorageAddress = UPDATE_START_ADDRESS;
      mico_rtos_lock_mutex(&Context->flashContentInRam_mutex); //We are write the Flash content, no other write is possiable
      context->isFlashLocked = true;
      err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE );
      require_noerr(err, flashErrExit);
      err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS);
      require_noerr(err, flashErrExit);
      err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &context->flashStorageAddress, (uint8_t *)inData, inLen);
      require_noerr(err, flashErrExit);
    }else{
      err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &context->flashStorageAddress, (uint8_t *)inData, inLen);
      require_noerr(err, flashErrExit);
    }
#else
    config_log("OTA storage is not exist");
    return kUnsupportedErr;
#endif
  }
  else if(inHeader->chunkedData == true){
    config_log("ChunkedData: %d, %d:", inPos, inLen);
    for(uint32_t i = 0; i<inLen; i++)
      printf("%c", inData[i]);
    printf("\r\n");
  }
  else{
    return kUnsupportedErr;
  }

  if(err!=kNoErr)  config_log("onReceivedData");
  return err;

#ifdef MICO_FLASH_FOR_UPDATE  
flashErrExit:
  MicoFlashFinalize(MICO_FLASH_FOR_UPDATE);
  mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex);
  return err;
#endif
}
OSStatus MICORestoreDefault(mico_Context_t *inContext)
{ 
  OSStatus err = kNoErr;
  uint32_t paraStartAddress, paraEndAddress;
 
  paraStartAddress = PARA_START_ADDRESS;
  paraEndAddress = PARA_END_ADDRESS;

  /*wlan configration is not need to change to a default state, use easylink to do that*/
  sprintf(inContext->flashContentInRam.micoSystemConfig.name, DEFAULT_NAME);
  inContext->flashContentInRam.micoSystemConfig.configured = unConfigured;
  inContext->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO;
  inContext->flashContentInRam.micoSystemConfig.rfPowerSaveEnable = false;
  inContext->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable = false;
  inContext->flashContentInRam.micoSystemConfig.bonjourEnable = true;
  inContext->flashContentInRam.micoSystemConfig.configServerEnable = true;
  inContext->flashContentInRam.micoSystemConfig.seed = seedNum;

  /*Application's default configuration*/
  appRestoreDefault_callback(inContext);

  err = MicoFlashInitialize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);
  err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress);
  require_noerr(err, exit);
  err = MicoFlashWrite(MICO_FLASH_FOR_PARA, &paraStartAddress, (void *)inContext, sizeof(flash_content_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);

exit:
  return err;
}
Beispiel #3
0
OSStatus MICORestoreMFG(mico_Context_t *inContext)
{ 
  OSStatus err = kNoErr;
  uint32_t paraStartAddress, paraEndAddress;
 
  paraStartAddress = PARA_START_ADDRESS;
  paraEndAddress = PARA_END_ADDRESS;

  /*wlan configration is not need to change to a default state, use easylink to do that*/
  sprintf(inContext->flashContentInRam.micoSystemConfig.name, DEFAULT_NAME);
  inContext->flashContentInRam.micoSystemConfig.configured = mfgConfigured;

  /*Application's default configuration*/
  appRestoreDefault_callback(inContext);

  err = MicoFlashInitialize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);
  err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress);
  require_noerr(err, exit);
  err = MicoFlashWrite(MICO_FLASH_FOR_PARA, &paraStartAddress, (void *)inContext, sizeof(flash_content_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);

exit:
  return err;
}
Beispiel #4
0
/*************************************************
* Function: HF_WriteDataToFlash
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void MX_WriteDataToFlash(u8 *pu8Data, u16 u16Len)
{
    mico_logic_partition_t  *para_partition_info;
    OSStatus err = kNoErr;
    uint32_t para_offset = PARA_OFFSET;
    para_partition_info = MicoFlashGetInfo(MICO_PARTITION_PARAMETER_2);
    require_action( para_partition_info->partition_owner != MICO_FLASH_NONE, exit, err = kUnsupportedErr );
    err = MicoFlashErase(MICO_PARTITION_PARAMETER_2 ,para_offset, para_partition_info->partition_length-PARA_OFFSET);
    require_noerr(err, exit);
    err = MicoFlashWrite( MICO_PARTITION_PARAMETER_2, &para_offset, pu8Data, u16Len );
    require_noerr(err, exit);
 exit:
  if(err != kNoErr) update_log("Update exit with err = %d", err);
    
}
void mico_set_bootload_ver(void)
{
    char ver[33];
    uint32_t flashaddr = BOOT_END_ADDRESS + 1 - 0x20;
    int i;

    MicoFlashInitialize(MICO_FLASH_FOR_BOOT);
    memset(ver, 0, sizeof(ver));
    MicoFlashRead(MICO_FLASH_FOR_BOOT, &flashaddr, (uint8_t *)ver , 32);
    for(i=0;i<32;i++) {
        if (ver[i] != 0xFF)
            return;
    }
    snprintf(ver, 33, "%s%s", MODEL, Bootloader_REVISION );
    flashaddr = BOOT_END_ADDRESS + 1 - 0x20;
    MicoFlashWrite(MICO_FLASH_FOR_BOOT, &flashaddr, (uint8_t *)ver , 32);
    MicoFlashFinalize(MICO_FLASH_FOR_BOOT);
}
Beispiel #6
0
void mico_set_bootload_ver(void)
{
   char ver[33];
   mico_logic_partition_t *boot_partition = MicoFlashGetInfo( MICO_PARTITION_BOOTLOADER );
   uint32_t flashaddr =  boot_partition->partition_length - 0x20;
   int i;

   memset(ver, 0, sizeof(ver));
   MicoFlashRead( MICO_PARTITION_BOOTLOADER, &flashaddr, (uint8_t *)ver , 32);
   for(i=0;i<32;i++) {
       if (ver[i] != 0xFF)
           return;
   }
   snprintf(ver, 33, "%s %s %d", MODEL, Bootloader_REVISION , STDIO_UART_BAUDRATE);
   flashaddr =  boot_partition->partition_length - 0x20;
   MicoFlashDisableSecurity( MICO_PARTITION_BOOTLOADER, 0x0, boot_partition->partition_length );
   MicoFlashWrite( MICO_PARTITION_BOOTLOADER, &flashaddr, (uint8_t *)ver , 32);
}
Beispiel #7
0
OSStatus HMUpdatePairList(pair_list_in_flash_t *pPairList)
{
  OSStatus err = kNoErr;
  uint32_t exParaStartAddress, exParaEndAddress;
 
  exParaStartAddress = EX_PARA_START_ADDRESS;
  exParaEndAddress = EX_PARA_END_ADDRESS;

  err = MicoFlashInitialize(MICO_FLASH_FOR_EX_PARA);
  require_noerr(err, exit);
  err = MicoFlashErase(MICO_FLASH_FOR_EX_PARA, exParaStartAddress, exParaEndAddress);
  require_noerr(err, exit);
  err = MicoFlashWrite(MICO_FLASH_FOR_EX_PARA, &exParaStartAddress, (uint8_t *)pPairList, sizeof(pair_list_in_flash_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_EX_PARA);
  require_noerr(err, exit);

exit:
  return err;
}
Beispiel #8
0
/*************************************************
* Function: MX_FirmwareUpdate
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
u32 MX_FirmwareUpdate(u8 *pu8FileData, u32 u32Offset, u32 u32DataLen)
{
#if 1
    mico_logic_partition_t  *para_partition_info;
    OSStatus err = kNoErr;
    if (0 == u32Offset)
    {   
        para_partition_info = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
        require_action( para_partition_info->partition_owner != MICO_FLASH_NONE, exit, err = kUnsupportedErr );
        err = MicoFlashErase(MICO_PARTITION_OTA_TEMP , 0, para_partition_info->partition_length );
        require_noerr(err, exit);
    } 
    err = MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &u32Offset, pu8FileData, u32DataLen );
    require_noerr(err, exit);
    return ZC_RET_OK;
exit:
  if(err != kNoErr) update_log("Update exit with err = %d", err);
    return ZC_RET_ERROR;
#endif
}
OSStatus MICOUpdateConfiguration(mico_Context_t *inContext)
{
  OSStatus err = kNoErr;
  uint32_t paraStartAddress, paraEndAddress;
 
  paraStartAddress = PARA_START_ADDRESS;
  paraEndAddress = PARA_END_ADDRESS;

  inContext->flashContentInRam.micoSystemConfig.seed = ++seedNum;
  err = MicoFlashInitialize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);
  err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress);
  require_noerr(err, exit);
  err = MicoFlashWrite(MICO_FLASH_FOR_PARA, &paraStartAddress, (uint8_t *)&inContext->flashContentInRam, sizeof(flash_content_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);

exit:
  return err;
}
Beispiel #10
0
OSStatus HMClearPairList(void)
{ 
  OSStatus err = kNoErr;
  uint32_t exParaStartAddress, exParaEndAddress;
 
  exParaStartAddress = EX_PARA_START_ADDRESS;
  exParaEndAddress = EX_PARA_END_ADDRESS;
  pair_list_in_flash_t *pairList = NULL;
  pairList = calloc(1, sizeof(pair_list_in_flash_t));
  require_action(pairList, exit, err = kNoMemoryErr);

  err = MicoFlashInitialize(MICO_FLASH_FOR_EX_PARA);
  require_noerr(err, exit);
  err = MicoFlashErase(MICO_FLASH_FOR_EX_PARA, exParaStartAddress, exParaEndAddress);
  require_noerr(err, exit);
  err = MicoFlashWrite(MICO_FLASH_FOR_EX_PARA, &exParaStartAddress, (uint8_t *)pairList, sizeof(pair_list_in_flash_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_EX_PARA);
  require_noerr(err, exit);

exit:
  if(pairList) free(pairList);
  return err;
}
Beispiel #11
0
//-------------------------------------------------------------
static s32_t lspiffs_write(u32_t addr, u32_t size, u8_t *src) {
    MicoFlashWrite(MICO_PARTITION_LUA,&addr,src,size);
    return SPIFFS_OK;
  }
Beispiel #12
0
void init_platform_bootloader( void )
{
  OSStatus err = kNoErr;
  
  MicoGpioInitialize( (mico_gpio_t)MICO_SYS_LED, OUTPUT_PUSH_PULL );
  MicoGpioOutputLow( (mico_gpio_t)MICO_SYS_LED );
  MicoGpioInitialize( (mico_gpio_t)MICO_RF_LED, OUTPUT_OPEN_DRAIN_NO_PULL );
  MicoGpioOutputHigh( (mico_gpio_t)MICO_RF_LED );
  
  MicoGpioInitialize((mico_gpio_t)BOOT_SEL, INPUT_PULL_UP);
  MicoGpioInitialize((mico_gpio_t)MFG_SEL, INPUT_PULL_UP);
  
  /* Specific operations used in EMW3165 production */
#define NEED_RF_DRIVER_COPY_BASE    ((uint32_t)0x08008000)
#define TEMP_RF_DRIVER_BASE         ((uint32_t)0x08040000)
#define TEMP_RF_DRIVER_END          ((uint32_t)0x0807FFFF)
  
  const uint8_t isDriverNeedCopy = *(uint8_t *)(NEED_RF_DRIVER_COPY_BASE);
  const uint32_t totalLength = ( DRIVER_FLASH_SIZE < 0x40000)?  DRIVER_FLASH_SIZE:0x40000;
  const uint8_t crcResult = *(uint8_t *)(TEMP_RF_DRIVER_END);
  uint8_t targetCrcResult = 0;
  
  uint32_t copyLength;
  uint32_t destStartAddress_tmp = DRIVER_START_ADDRESS;
  uint32_t sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE;
  uint32_t i;
  
  if ( isDriverNeedCopy != 0x0 )
    return;
  
  platform_log( "Bootloader start to copy RF driver..." );
  /* Copy RF driver to SPI flash */
  err = MicoFlashInitialize( (mico_flash_t)MICO_FLASH_FOR_DRIVER );
  require_noerr(err, exit);
  err = MicoFlashInitialize( (mico_flash_t)MICO_INTERNAL_FLASH );
  require_noerr(err, exit);
  err = MicoFlashErase( MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, DRIVER_END_ADDRESS );
  require_noerr(err, exit);
  platform_log( "Time: %d", mico_get_time_no_os() );
  
  for(i = 0; i <= totalLength/SizePerRW; i++){
    if( i == totalLength/SizePerRW ){
      if(totalLength%SizePerRW)
        copyLength = totalLength%SizePerRW;
      else
        break;
    }else{
      copyLength = SizePerRW;
    }
    printf(".");
    err = MicoFlashRead( MICO_INTERNAL_FLASH, &sourceStartAddress_tmp, data , copyLength );
    require_noerr( err, exit );
    err = MicoFlashWrite( MICO_FLASH_FOR_DRIVER, &destStartAddress_tmp, data, copyLength);
    require_noerr(err, exit);
  }
  
  printf("\r\n");
  /* Check CRC-8 check-sum */
  platform_log( "Bootloader start to verify RF driver..." );
  sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE;
  destStartAddress_tmp = DRIVER_START_ADDRESS;
  
  for(i = 0; i <= totalLength/SizePerRW; i++){
    if( i == totalLength/SizePerRW ){
      if(totalLength%SizePerRW)
        copyLength = totalLength%SizePerRW;
      else
        break;
    }else{
      copyLength = SizePerRW;
    }
    printf(".");
    err = MicoFlashRead( MICO_FLASH_FOR_DRIVER, &destStartAddress_tmp, data, copyLength );
    require_noerr( err, exit );
    
    targetCrcResult = CRC8_Table(targetCrcResult, data, copyLength);
  }
  
  printf("\r\n");
  //require_string( crcResult == targetCrcResult, exit, "Check-sum error" ); 
  if( crcResult != targetCrcResult ){
    platform_log("Check-sum error");
    while(1);
  }
  /* Clear RF driver from temperary storage */
  platform_log("Bootloader start to clear RF driver temporary storage...");
  
  err = MicoFlashInitialize( (mico_flash_t)MICO_INTERNAL_FLASH );
  require_noerr(err, exit);  
  
  /* Clear copy tag */
  err = MicoFlashErase(MICO_INTERNAL_FLASH, NEED_RF_DRIVER_COPY_BASE, NEED_RF_DRIVER_COPY_BASE);
  require_noerr(err, exit);
  
exit:
  MicoFlashFinalize( MICO_INTERNAL_FLASH );
  MicoFlashFinalize( MICO_FLASH_FOR_DRIVER );
}
Beispiel #13
0
//====================================
static int set_sparams( lua_State* L )
{
  uint8_t change = 0;
  const char* buf;
  uint32_t lua_param_offset = 0x0;
  uint8_t *p_id = &lua_system_param.ID;
  char *p_f = &lua_system_param.init_file[0];
  
  if (!lua_istable(L, 1)) {
    l_message( NULL, "table arg needed" );
    return 0;
  }

  lua_getfield(L, 1, "use_wwdg");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      uint8_t wd = luaL_checkinteger( L, -1 );
      if (wd == 0) lua_system_param.use_wwdg = 0;
      else lua_system_param.use_wwdg = 1;
      l_message( NULL, "updated: use_wwdg" );
      change++;
    } else
    {
      l_message( NULL, "wrong arg type: use_wwdg" );
    }
  }

  lua_getfield(L, 1, "wdg_tmo");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      uint32_t wdtmo = luaL_checkinteger( L, -1 );
      if (wdtmo < 2000 || wdtmo > 3600000) wdtmo = 10000;
      lua_system_param.wdg_tmo = wdtmo;
      l_message( NULL, "updated: wdg_tmo" );
      change++;
    } else
    {
      l_message( NULL, "wrong arg type: wdg_tmo" );
    }
  }

  lua_getfield(L, 1, "stack_size");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      uint16_t stksz = luaL_checkinteger( L, -1 );
      if (stksz < 8192 || stksz > 24576) {
        l_message( NULL, "stack_size: 8192 ~ 24576, not updated" );
      }
      else {
        lua_system_param.stack_size = stksz;
        l_message( NULL, "updated: stack_size" );
        change++;
      }
    } else
    {
      l_message( NULL, "wrong arg type: stack_size" );
    }
  }

  lua_getfield(L, 1, "inbuf_size");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      uint16_t inbsz = luaL_checkinteger( L, -1 );
      if (inbsz < 128 || inbsz > 1024) inbsz = 256;
      lua_system_param.inbuf_size = inbsz;
      l_message( NULL, "updated: inbuf_size" );
      change++;
    } else
    {
      l_message( NULL, "wrong arg type: inbuf_size" );
    }
  }

  lua_getfield(L, 1, "baud_rate");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      uint32_t bdr = luaL_checkinteger( L, -1 );
      lua_system_param.baud_rate = bdr;
      l_message( NULL, "updated: baud_rate" );
      change++;
    } else
    {
      l_message( NULL, "wrong arg type: baud_rate" );
    }
  }

  lua_getfield(L, 1, "parity");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      buf = luaL_checkstring( L, -1 );
      if (strlen(buf) == 1) {
        change++;
        if(strcmp(buf, "n") == 0)
          lua_system_param.parity = NO_PARITY;
        else if(strcmp(buf, "o") == 0)
          lua_system_param.parity = ODD_PARITY;
        else if(strcmp(buf, "e") == 0)
          lua_system_param.parity = EVEN_PARITY;
        else {
          l_message( NULL, "arg parity should be 'n' or 'o' or 'e' " );
          change--;
        }
        l_message( NULL, "updated: parity" );
      }
      else {
        l_message( NULL, "arg parity should be 'n' or 'o' or 'e' " );
      }
    } else
    {
      l_message( NULL, "wrong arg type: parity" );
    }
  }

  lua_getfield(L, 1, "init_file");
  if (!lua_isnil(L, -1)) {  // found?
    if( lua_isstring(L, -1) )   // deal with the string
    {
      buf = luaL_checkstring( L, -1 );
      if (strlen(buf) < 16) {
        sprintf(p_f, buf);
        change++;
        l_message( NULL, "updated: init_file" );
      }
      else {
        l_message( NULL, "file name too long" );
      }
    } else
    {
      l_message( NULL, "wrong arg type: init_file" );
    }
  }

  if (change) {
    lua_system_param.crc = _get_luaparamsCRC();
    MicoFlashErase(MICO_PARTITION_PARAMETER_1, 0, sizeof(lua_system_param_t));
    lua_param_offset = 0;
    MicoFlashWrite(MICO_PARTITION_PARAMETER_1, &lua_param_offset, p_id, sizeof(lua_system_param_t));
    l_message( NULL, "New params saved." );
  }
  else {
    l_message( NULL, "Params to change: use_wwdg,baud_rate,parity,inbuf_size,init_file,stack_size,wdg_tmo" );
  }
  
  return 0;
}
teZcbStatus ePDM_Init(mico_Context_t* mico_context)
{
    OSStatus err;
    user_zigbeePDM_log("Create PDM lock");
    mico_rtos_init_mutex(&sLock);
    mico_logic_partition_t *zigbeePDM_partition_info;
    //mico_logic_partition_t *p1_info;
    uint8_t read_test[100]= {0};
    uint8_t i = 0;
    uint32_t dest_offset = 0;
    uint8_t data_write[6]= {0x06,0x05,0x04,0x03,0x02,0x01};

    mico_rtos_lock_mutex(&sLock);

#if 0
    //init  MICO_PARTITION_ZIGBEEPDM_TEMP
    err = MicoFlashInitialize(MICO_PARTITION_ZIGBEEPDM_TEMP);
    require_noerr(err, exit);

    // Get Info  MICO_PARTITION_ZIGBEEPDM_TEMP
    zigbeePDM_partition_info = MicoFlashGetInfo(MICO_PARTITION_ZIGBEEPDM_TEMP);
    user_zigbeePDM_log("ZigBee PDM Partition info:start_addr:%x ,length:%x",zigbeePDM_partition_info->partition_start_addr,zigbeePDM_partition_info->partition_length);

    //Erase MICO_PARTITION_ZIGBEEPDM_TEMP
    err = MicoFlashErase( MICO_PARTITION_ZIGBEEPDM_TEMP, 0x0, zigbeePDM_partition_info->partition_length);
    require_noerr(err, exit);


    mico_thread_msleep(100);	//sleep


    //MicoFlashWrite(mico_partition_t partition, volatile uint32_t * off_set, uint8_t * inBuffer, uint32_t inBufferLength);

    //Write MICO_PARTITION_ZIGBEEPDM_TEMP
    err = MicoFlashWrite(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, (uint8_t *)data_write, sizeof(data_write));
    require_noerr(err, exit);
#endif

#if 0
    mico_context -> user_config_data = (void*)data_write;
    mico_context -> user_config_data_size = 10;
    err = mico_system_context_update(mico_context);
    require_noerr(err, exit);
    mico_thread_msleep(1000);
#endif

#if 0
    //Read
    dest_offset = 0;
    err = MicoFlashRead(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, read_test, 5);
    require_noerr(err, exit);
#endif

#if 0
    err = MicoFlashErase( MICO_PARTITION_PARAMETER_1, 0x0, 60);
    require_noerr(err, exit);
    mico_thread_msleep(10);

    err = MicoFlashWrite( MICO_PARTITION_PARAMETER_1, &dest_offset, "aaaaa", 5);
    require_noerr(err, exit);


    p1_info = MicoFlashGetInfo(MICO_PARTITION_PARAMETER_1);
    err = MicoFlashRead(MICO_PARTITION_PARAMETER_1, &dest_offset, read_test, 60);
    require_noerr(err, exit);
#endif

#if 0
    //Output
    for(i = 0; i<5; i++)
    {
        printf("0x%x ",read_test[i]);
    }
    printf("\r\n");
#endif
    //MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &context->offset, (uint8_t *)inData, inLen);

    //MicoFlashRead(MICO_PARTITION_OTA_TEMP, &flashaddr, (uint8_t *)md5_recv, 16);

    //err = MicoFlashDisableSecurity( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition_info->partition_length );



    //if (sqlite3_open_v2(pcPDMFile, &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) != SQLITE_OK)
    //{
    //    daemon_log(LOG_ERR, "Error initialising PDM database (%s)", sqlite3_errmsg(pDb));
    //    return E_ZCB_ERROR;
    //}
    //user_zigbeePDM_log("PDM Database opened\n");
    {
        //const char *pcTableDef = "CREATE TABLE IF NOT EXISTS pdm (id INTEGER, size INTEGER, numblocks INTEGER, block INTEGER, blocksize INTEGER, data BLOB, PRIMARY KEY (id,block))";
        //char *pcErr;

        //user_zigbeePDM_log("Execute SQL: '%s'\n", pcTableDef);

        //if (sqlite3_exec(pDb, pcTableDef, NULL, NULL, &pcErr) != SQLITE_OK)
        //{
        //    mico_log("Error creating table (%s)", pcErr);
        //sqlite3_free(pcErr);
        //mico_rtos_unlock_mutex(&sLock);
        //return E_ZCB_ERROR;
        //}
    }
    //user_zigbeePDM_log("PDM Database initialised\n");

    //eSL_AddListener(E_SL_MSG_PDM_AVAILABLE_REQUEST,         PDM_HandleAvailableRequest,     NULL);
    //eSL_AddListener(E_SL_MSG_PDM_LOAD_RECORD_REQUEST,       PDM_HandleLoadRequest,          NULL);
    //eSL_AddListener(E_SL_MSG_PDM_SAVE_RECORD_REQUEST,       PDM_HandleSaveRequest,          NULL);
    //eSL_AddListener(E_SL_MSG_PDM_DELETE_ALL_RECORDS_REQUEST,PDM_HandleDeleteAllRequest,     NULL);


    mico_rtos_unlock_mutex(&sLock);
    return E_ZCB_OK;
exit:
    mico_rtos_unlock_mutex(&sLock);
    return err;
}
Beispiel #15
0
OSStatus update(void)
{
  boot_table_t updateLog;
  uint32_t i, j, size;
  uint32_t updateStartAddress;
  uint32_t destStartAddress_tmp;
  uint32_t paraStartAddress;
  OSStatus err = kNoErr;
 
  MicoFlashInitialize( (mico_flash_t)MICO_FLASH_FOR_UPDATE );
  memset(data, 0xFF, SizePerRW);
  memset(newData, 0xFF, SizePerRW);
  memset(paraSaveInRam, 0xFF, PARA_FLASH_SIZE);
  
  updateStartAddress = UPDATE_START_ADDRESS;
  
  paraStartAddress = PARA_START_ADDRESS;  
  err = MicoFlashRead(MICO_FLASH_FOR_PARA, &paraStartAddress, (uint8_t *)&updateLog, sizeof(boot_table_t));
  require_noerr(err, exit);

  /*Not a correct record*/
  if(updateLogCheck(&updateLog) != Log_NeedUpdate){
    size = UPDATE_FLASH_SIZE/SizePerRW;
    for(i = 0; i <= size; i++){
      if( i==size ){
        err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , UPDATE_FLASH_SIZE%SizePerRW);
        require_noerr(err, exit);
      }
      else{
        err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , SizePerRW);
        require_noerr(err, exit);
      }
      
      for(j=0; j<SizePerRW; j++){
        if(data[j] != 0xFF){
          update_log("Update data need to be erased");
          err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE );
          require_noerr(err, exit);
          err = MicoFlashErase( MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS );
          require_noerr(err, exit);
          err = MicoFlashFinalize( MICO_FLASH_FOR_UPDATE );
          require_noerr(err, exit);
          break;
        }
      }
    }
    goto exit;
  }
  
  update_log("Write OTA data to destination, type:%d, from 0x%08x to 0x%08x, length 0x%x", destFlashType, destStartAddress, destEndAddress, updateLog.length);
  
  destStartAddress_tmp = destStartAddress;
  updateStartAddress = UPDATE_START_ADDRESS;
  
  err = MicoFlashInitialize( destFlashType );
  require_noerr(err, exit);
  err = MicoFlashErase( destFlashType, destStartAddress, destEndAddress );
  require_noerr(err, exit);
  size = (updateLog.length)/SizePerRW;
  for(i = 0; i <= size; i++){
    if( i==size && (updateLog.length)%SizePerRW){
      err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , (updateLog.length)%SizePerRW);
      require_noerr(err, exit);
      err = MicoFlashInitialize( destFlashType );
      require_noerr(err, exit);
      err = MicoFlashWrite(destFlashType, &destStartAddress_tmp, data, (updateLog.length)%SizePerRW);
      require_noerr(err, exit);
      destStartAddress_tmp -= (updateLog.length)%SizePerRW;
      err = MicoFlashRead(destFlashType, &destStartAddress_tmp, newData , (updateLog.length)%SizePerRW);
      require_noerr(err, exit);
      err = memcmp(data, newData, (updateLog.length)%SizePerRW);
      require_noerr_action(err, exit, err = kWriteErr);
    }
    else{
      err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , SizePerRW);
      require_noerr(err, exit);
      err = MicoFlashInitialize( destFlashType );
      require_noerr(err, exit);
      err = MicoFlashWrite(destFlashType, &destStartAddress_tmp, data, SizePerRW);
      require_noerr(err, exit);
      destStartAddress_tmp -= SizePerRW;
      err = MicoFlashRead(destFlashType, &destStartAddress_tmp, newData , SizePerRW);
      require_noerr(err, exit);
      err = memcmp(data, newData, SizePerRW);
      require_noerr_action(err, exit, err = kWriteErr); 
    }
  } 
  update_log("Update start to clear data...");
    
  paraStartAddress = PARA_START_ADDRESS;
  err = MicoFlashRead(MICO_FLASH_FOR_PARA, &paraStartAddress, paraSaveInRam, PARA_FLASH_SIZE);
  require_noerr(err, exit);
  memset(paraSaveInRam, 0xff, sizeof(boot_table_t));
  
  err = MicoFlashErase(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_END_ADDRESS);
  require_noerr(err, exit);

  paraStartAddress = PARA_START_ADDRESS;
  err = MicoFlashWrite(MICO_FLASH_FOR_PARA, &paraStartAddress, paraSaveInRam, PARA_FLASH_SIZE);
  require_noerr(err, exit);
  
  err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS);
  require_noerr(err, exit);
  update_log("Update success");
  
exit:
  if(err != kNoErr) update_log("Update exit with err = %d", err);
  MicoFlashFinalize(MICO_FLASH_FOR_UPDATE);
  MicoFlashFinalize(destFlashType);
  return err;
}
Beispiel #16
0
OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader )
{
  OSStatus err = kParamErr;
  ssize_t readResult;
  int selectResult;
  fd_set readSet;
  const char *    value;
  size_t          valueSize;
  size_t    lastChunkLen, chunckheaderLen; 
  char *nextPackagePtr;
#ifdef MICO_FLASH_FOR_UPDATE
  bool writeToFlash = false;
#endif
  
  require( inHeader, exit );
  
  err = kNotReadableErr;
  
  FD_ZERO( &readSet );
  FD_SET( inSock, &readSet );

  /* Chunked data, return after receive one chunk */
  if( inHeader->chunkedData == true ){
    /* Move next chunk to chunked data buffer header point */
    lastChunkLen = inHeader->extraDataPtr - inHeader->chunkedDataBufferPtr + inHeader->contentLength;
    if(inHeader->contentLength) lastChunkLen+=2;  //Last chunck data has a CRLF tail
    memmove( inHeader->chunkedDataBufferPtr, inHeader->chunkedDataBufferPtr + lastChunkLen, inHeader->chunkedDataBufferLen - lastChunkLen  );
    inHeader->extraDataLen -= lastChunkLen;

    while ( findChunkedDataLength( inHeader->chunkedDataBufferPtr, inHeader->extraDataLen, &inHeader->extraDataPtr ,"%llu", &inHeader->contentLength ) == false){
      require_action(inHeader->extraDataLen < inHeader->chunkedDataBufferLen, exit, err=kMalformedErr );

      selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
      require( selectResult >= 1, exit ); 

      readResult = read( inSock, inHeader->extraDataPtr, (size_t)( inHeader->chunkedDataBufferLen - inHeader->extraDataLen ) );

      if( readResult  > 0 ) inHeader->extraDataLen += readResult;
      else { err = kConnectionErr; goto exit; }
    }

    chunckheaderLen = inHeader->extraDataPtr - inHeader->chunkedDataBufferPtr;

    if(inHeader->contentLength == 0){ //This is the last chunk
      while( findCRLF( inHeader->extraDataPtr, inHeader->extraDataLen - chunckheaderLen, &nextPackagePtr ) == false){ //find CRLF
        selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
        require( selectResult >= 1, exit ); 

        readResult = read( inSock,
                          (uint8_t *)( inHeader->extraDataPtr + inHeader->extraDataLen - chunckheaderLen ),
                          256 - inHeader->extraDataLen ); //Assume chunk trailer length is less than 256 (256 is the min chunk buffer, maybe dangerous

        if( readResult  > 0 ) inHeader->extraDataLen += readResult;
        else { err = kConnectionErr; goto exit; }
      }

      err = kNoErr;
      goto exit;
    }
    else{
      /* Extend chunked data buffer */
      if( inHeader->chunkedDataBufferLen < inHeader->contentLength + chunckheaderLen + 2){
        inHeader->chunkedDataBufferLen = inHeader->contentLength + chunckheaderLen + 256;
        inHeader->chunkedDataBufferPtr = realloc(inHeader->chunkedDataBufferPtr, inHeader->chunkedDataBufferLen);
        require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr);
      }

      /* Read chunked data */
      while ( inHeader->extraDataLen < inHeader->contentLength + chunckheaderLen + 2 ){
        selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
        require( selectResult >= 1, exit ); 

        readResult = read( inSock,
                          (uint8_t *)( inHeader->extraDataPtr + inHeader->extraDataLen - chunckheaderLen),
                          ( inHeader->contentLength - (inHeader->extraDataLen - chunckheaderLen) + 2 ));
        
        if( readResult  > 0 ) inHeader->extraDataLen += readResult;
        else { err = kConnectionErr; goto exit; }
      } 
      
      if( *(inHeader->extraDataPtr + inHeader->contentLength) != '\r' ||
         *(inHeader->extraDataPtr + inHeader->contentLength +1 ) != '\n'){
           err = kMalformedErr; 
           goto exit;
         }
    }
  }

  /* We has extra data but total length is not clear, store them to 1500 bytes buffer 
     return when connection is disconnected by remote server */
  if( inHeader->dataEndedbyClose == true){ 
    if(inHeader->contentLength == 0) { //First read body, return using data received by SocketReadHTTPHeader
      inHeader->contentLength = inHeader->extraDataLen;
    }else{
      selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
      require( selectResult >= 1, exit ); 
      
      readResult = read( inSock,
                        (uint8_t*)( inHeader->extraDataPtr ),
                        1500 );
      if( readResult  > 0 ) inHeader->contentLength = readResult;
      else { err = kConnectionErr; goto exit; }
    }
    err = kNoErr;
    goto exit;
  }
  
  /* We has extra data and we has a predefined buffer to store the total extra data
     return when all data has received*/
  while ( inHeader->extraDataLen < inHeader->contentLength )
  {
    selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL );
    require( selectResult >= 1, exit );
    
    
    err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL );
    require_noerr(err, exit);
    if( strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0 ){
#ifdef MICO_FLASH_FOR_UPDATE  
      writeToFlash = true;
      inHeader->otaDataPtr = calloc(OTA_Data_Length_per_read, sizeof(uint8_t)); 
      require_action(inHeader->otaDataPtr, exit, err = kNoMemoryErr);
      if((inHeader->contentLength - inHeader->extraDataLen)<OTA_Data_Length_per_read){
        readResult = read( inSock,
                          (uint8_t*)( inHeader->otaDataPtr ),
                          ( inHeader->contentLength - inHeader->extraDataLen ) );
      }else{
        readResult = read( inSock,
                          (uint8_t*)( inHeader->otaDataPtr ),
                          OTA_Data_Length_per_read);
      }
      
      if( readResult  > 0 ) inHeader->extraDataLen += readResult;
      else { err = kConnectionErr; goto exit; }
      
      err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &flashStorageAddress, (uint8_t *)inHeader->otaDataPtr, readResult);
      require_noerr(err, exit);
      
      free(inHeader->otaDataPtr);
      inHeader->otaDataPtr = 0;
#else
      http_utils_log("OTA flash memory is not existed, !");
      err = kUnsupportedErr;
#endif
    }else{
      readResult = read( inSock,
                        (uint8_t*)( inHeader->extraDataPtr + inHeader->extraDataLen ),
                        ( inHeader->contentLength - inHeader->extraDataLen ) );
      
      if( readResult  > 0 ) inHeader->extraDataLen += readResult;
      else { err = kConnectionErr; goto exit; }
    }
  }  
  err = kNoErr;
  
exit:
  if(err != kNoErr) inHeader->len = 0;
  if(inHeader->otaDataPtr) {
    free(inHeader->otaDataPtr);
    inHeader->otaDataPtr = 0;
  }
#ifdef MICO_FLASH_FOR_UPDATE
  if(writeToFlash == true) MicoFlashFinalize(MICO_FLASH_FOR_UPDATE);
#endif
  return err;
}
Beispiel #17
0
int SocketReadHTTPHeader( int inSock, HTTPHeader_t *inHeader )
{
  int        err =0;
  char *          buf;
  char *          dst;
  char *          lim;
  char *          end;
  size_t          len;
  ssize_t         n;
  const char *    value;
  size_t          valueSize;
  
  buf = inHeader->buf;
  dst = buf + inHeader->len;
  lim = buf + sizeof( inHeader->buf );
  for( ;; )
  {
    if(findHeader( inHeader,  &end ))
      break ;
    n = read( inSock, dst, (size_t)( lim - dst ) );
    if(      n  > 0 ) len = (size_t) n;
    else  { err = kConnectionErr; goto exit; }
    dst += len;
    inHeader->len += len;
  }
  
  inHeader->len = (size_t)( end - buf );
  err = HTTPHeaderParse( inHeader );
  require_noerr( err, exit );
  inHeader->extraDataLen = (size_t)( dst - end );
  if(inHeader->extraDataPtr) {
    free((uint8_t *)inHeader->extraDataPtr);
    inHeader->extraDataPtr = 0;
  }
  
  if(inHeader->otaDataPtr) {
    free((uint8_t *)inHeader->otaDataPtr);
    inHeader->otaDataPtr = 0;
  }
  
  /* For MXCHIP OTA function, store extra data to OTA data temporary */
  err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL );

  if(err == kNoErr && strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){
#ifdef MICO_FLASH_FOR_UPDATE  
    http_utils_log("Receive OTA data!");        
    err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE );
    require_noerr(err, exit);
    err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &flashStorageAddress, (uint8_t *)end, inHeader->extraDataLen);
    require_noerr(err, exit);
#else
    http_utils_log("OTA flash memory is not existed!");
    err = kUnsupportedErr;
#endif
    goto exit;
  }

  /* For chunked extra data without content length */
  if(inHeader->chunkedData == true){
    inHeader->chunkedDataBufferLen = (inHeader->extraDataLen > 256)? inHeader->extraDataLen:256;
    inHeader->chunkedDataBufferPtr = calloc(inHeader->chunkedDataBufferLen, sizeof(uint8_t)); //Make extra data buffer larger than chunk length
    require_action(inHeader->chunkedDataBufferPtr, exit, err = kNoMemoryErr);
    memcpy((uint8_t *)inHeader->chunkedDataBufferPtr, end, inHeader->extraDataLen);
    inHeader->extraDataPtr = inHeader->chunkedDataBufferPtr;
    return kNoErr;
  }

  /* Extra data with content length */
  if (inHeader->contentLength != 0){ //Content length >0, create a memory buffer (Content length) and store extra data
    size_t copyDataLen = (inHeader->contentLength >= inHeader->extraDataLen)? inHeader->contentLength:inHeader->extraDataLen;
    inHeader->extraDataPtr = calloc(copyDataLen , sizeof(uint8_t));
    require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr);
    memcpy((uint8_t *)inHeader->extraDataPtr, end, copyDataLen);
    err = kNoErr;
  } /* Extra data without content length, data is ended by conntection close */
  else if(inHeader->extraDataLen != 0){ //Content length =0, but extra data length >0, create a memory buffer (1500)and store extra data
    inHeader->dataEndedbyClose = true;
    inHeader->extraDataPtr = calloc(1500, sizeof(uint8_t));
    require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr);
    memcpy((uint8_t *)inHeader->extraDataPtr, end, inHeader->extraDataLen);
    err = kNoErr;
  }
  else
    return kNoErr;
  
exit:
  return err;
}
Beispiel #18
0
/**
  * @brief  Receive a file using the ymodem protocol.
  * @param  buf: Address of the first byte.
  * @retval The size of the file.
  */
int32_t Ymodem_Receive (uint8_t *buf, mico_flash_t flash, uint32_t flashdestination, int32_t maxRecvSize)
{
  uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr, *buf_ptr;
  int32_t i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0;
  uint32_t  ramsource;
  MicoFlashInitialize(flash);

  for (session_done = 0, errors = 0, session_begin = 0; ;)
  {
    for (packets_received = 0, file_done = 0, buf_ptr = buf; ;)
    {
      switch (Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT))
      {
        case 0:
          errors = 0;
          switch (packet_length)
          {
            /* Abort by sender */
            case - 1:
              Send_Byte(ACK);
              MicoFlashFinalize(flash);
              return 0;
            /* End of transmission */
            case 0:
              Send_Byte(ACK);
              file_done = 1;
              break;
            /* Normal packet */
            default:
              if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff))
              {
                Send_Byte(NAK);
              }
              else
              {
                if (packets_received == 0)
                {
                  /* Filename packet */
                  if (packet_data[PACKET_HEADER] != 0)
                  {
                    /* Filename packet has valid data */
                    for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);)
                    {
                      FileName[i++] = *file_ptr++;
                    }
                    FileName[i++] = '\0';
                    for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);)
                    {
                      file_size[i++] = *file_ptr++;
                    }
                    file_size[i++] = '\0';
                    Str2Int(file_size, &size);

                    /* Test the size of the image to be sent */
                    /* Image size is greater than Flash size */
                    if (size > (maxRecvSize + 1))
                    {
                      /* End session */
                      Send_Byte(CA);
                      Send_Byte(CA);
                      MicoFlashFinalize(flash);
                      return -1;
                    }
                    /* erase user application area */
                    MicoFlashErase(flash, flashdestination, flashdestination + maxRecvSize - 1);
                    Send_Byte(ACK);
                    Send_Byte(CRC16);
                  }
                  /* Filename packet is empty, end session */
                  else
                  {
                    Send_Byte(ACK);
                    file_done = 1;
                    session_done = 1;
                    break;
                  }
                }
                /* Data packet */
                else
                {
                  memcpy(buf_ptr, packet_data + PACKET_HEADER, packet_length);
                  ramsource = (uint32_t)buf;

                  /* Write received data in Flash */
                  if (MicoFlashWrite(flash, &flashdestination, (uint8_t*) ramsource, (uint32_t) packet_length)  == 0)
                  {
                    Send_Byte(ACK);
                  }
                  else /* An error occurred while writing to Flash memory */
                  {
                    /* End session */
                    Send_Byte(CA);
                    Send_Byte(CA);
                    MicoFlashFinalize(flash);
                    return -2;
                  }
                }
                packets_received ++;
                session_begin = 1;
              }
          }
          break;
        case 1:
          Send_Byte(CA);
          Send_Byte(CA);
          MicoFlashFinalize(flash);
          return -3;
        default:
          if (session_begin > 0)
          {
            errors ++;
          }
          if (errors > MAX_ERRORS)
          {
            Send_Byte(CA);
            Send_Byte(CA);
            MicoFlashFinalize(flash);
            return 0;
          }
          Send_Byte(CRC16);
          break;
      }
      if (file_done != 0)
      {
        break;
      }
    }
    if (session_done != 0)
    {
      break;
    }
  }
  MicoFlashFinalize(flash);
  return (int32_t)size;
}