Пример #1
0
OSStatus bt_smartbridge_att_cache_disable( void )
{
    uint32_t a;
    linked_list_node_t* node = NULL;
    bt_smartbridge_att_cache_manager_t* manager = att_cache_manager;

    if ( att_cache_manager == NULL )
    {
        return MICO_BT_SUCCESS;
    }

    /* Set status at the beginning to prevent cached attributes being used even when deinitialisation failed */
    att_cache_manager = NULL;

    while ( linked_list_remove_node_from_front( &manager->free_list, &node ) == MICO_BT_SUCCESS )
    {
        bt_smartbridge_att_cache_t* cache = (bt_smartbridge_att_cache_t*)node->data;

        mico_bt_smart_attribute_delete_list( &cache->attribute_list );
    }

    linked_list_deinit( &manager->free_list );

    while ( linked_list_remove_node_from_front( &manager->used_list, &node ) == MICO_BT_SUCCESS )
    {
        bt_smartbridge_att_cache_t* cache = (bt_smartbridge_att_cache_t*)node->data;

        mico_bt_smart_attribute_delete_list( &cache->attribute_list );
    }

    linked_list_deinit( &manager->used_list );
    mico_rtos_deinit_mutex( &manager->mutex );

    /* Deinitialise mutexes for protecting access to cached attributes */
    for ( a = 0; a < manager->count; a++ )
    {
        mico_rtos_deinit_mutex( &manager->pool[a].mutex );
    }

    if( manager->att_cache_services_count != 0 )
    {
        free( manager->att_cache_services );
    }
    
    memset( manager, 0, CALCULATE_ATT_CACHE_MANAGER_SIZE( manager->count ) );
    free( manager );

    return MICO_BT_SUCCESS;
}
Пример #2
0
OSStatus platform_flash_deinit( platform_flash_driver_t *driver)
{
  OSStatus err = kNoErr;

  require_action_quiet( driver != NULL, exit, err = kParamErr);

  driver->initialized = false;
#ifndef NO_MICO_RTOS 
  mico_rtos_deinit_mutex( &driver->flash_mutex );
#endif

  if( driver->peripheral->flash_type == FLASH_TYPE_INTERNAL){
    err = internalFlashFinalize();   
    require_noerr(err, exit); 
  }
#ifdef USE_MICO_SPI_FLASH
  else if( driver->peripheral->flash_type == FLASH_TYPE_SPI ){
    sflash_handle.device_id = 0x0;
  }
#endif
  else
    return kUnsupportedErr;

  
exit:
  
  return err;
}
Пример #3
0
OSStatus platform_uart_deinit( platform_uart_driver_t* driver )
{
  uint8_t          uart_number;
  OSStatus          err = kNoErr;

  platform_mcu_powersave_disable();

  require_action_quiet( ( driver != NULL ), exit, err = kParamErr);

  uart_number = platform_uart_get_port_number( driver->peripheral->port );

  /* Disable USART */
  USART_Cmd( driver->peripheral->port, DISABLE );

  /* Deinitialise USART */
  USART_DeInit( driver->peripheral->port );

  /**************************************************************************
   * De-initialise STM32 DMA and interrupt
   **************************************************************************/

  /* Deinitialise DMA streams */
  DMA_DeInit( driver->peripheral->tx_dma_config.stream );
  DMA_DeInit( driver->peripheral->rx_dma_config.stream );

  /* Disable TC (transfer complete) interrupt at the source */
  DMA_ITConfig( driver->peripheral->tx_dma_config.stream, DMA_INTERRUPT_FLAGS, DISABLE );
  DMA_ITConfig( driver->peripheral->rx_dma_config.stream, DMA_INTERRUPT_FLAGS, DISABLE );

  /* Disable transmit DMA interrupt at Cortex-M3 */
  NVIC_DisableIRQ( driver->peripheral->tx_dma_config.irq_vector );

  /**************************************************************************
   * De-initialise STM32 USART interrupt
   **************************************************************************/

  USART_ITConfig( driver->peripheral->port, USART_IT_RXNE, DISABLE );

  /* Disable UART interrupt vector on Cortex-M3 */
  NVIC_DisableIRQ( driver->peripheral->rx_dma_config.irq_vector );

  /* Disable registers clocks */
  uart_peripheral_clock_functions[uart_number]( uart_peripheral_clocks[uart_number], DISABLE );

  mico_rtos_deinit_semaphore( &driver->rx_complete );
  mico_rtos_deinit_semaphore( &driver->tx_complete );
  mico_rtos_deinit_mutex( &driver->tx_mutex );
  driver->rx_size              = 0;
  driver->tx_size              = 0;
  driver->last_transmit_result = kNoErr;
  driver->last_receive_result  = kNoErr;
  driver->initialized          = false;

exit:
    platform_mcu_powersave_enable();
    return err;
}
Пример #4
0
teZcbStatus ePDM_Destory()
{
#if 0
    mico_rtos_lock_mutex(&sLock);
    //    if (pDb)
    //    {
    //       sqlite3_close(pDb);
    //        DBG_vPrintf(DBG_PDM, "PDM Database closed\n");
    //    }
    mico_rtos_unlock_mutex(&sLock);
    mico_rtos_deinit_mutex(&sLock);
#endif
    return E_ZCB_OK;
}
Пример #5
0
OSStatus MicoI2cFinalize( mico_i2c_device_t* device )
{
  platform_i2c_config_t config;

  if ( device->port >= MICO_I2C_NONE )
    return kUnsupportedErr;
  
  config.address       = device->address;
  config.address_width = device->address_width;
  config.flags         &= ~I2C_DEVICE_USE_DMA ;
  config.speed_mode    = device->speed_mode;

  if( platform_i2c_drivers[device->port].i2c_mutex != NULL){
    mico_rtos_deinit_mutex( &platform_i2c_drivers[device->port].i2c_mutex );
    platform_i2c_drivers[device->port].i2c_mutex = NULL;
  }
    
  return (OSStatus) platform_i2c_deinit( &platform_i2c_peripherals[device->port], &config );
}
Пример #6
0
void fogcloud_main_thread(void *arg)
{
  OSStatus err = kUnknownErr;
  mico_Context_t *inContext = (mico_Context_t *)arg;
  
  MVDResetRequestData_t devResetRequestData;
  
#ifdef ENABLE_FOGCLOUD_AUTO_ACTIVATE
  MVDActivateRequestData_t devDefaultActivateData;
#endif
  
  /* wait for station on */
  while(!inContext->appStatus.isWifiConnected){
    mico_thread_msleep(500);
  }
  
  //--- create msg recv queue, NOTE: just push msg pionter into queue, so msg memory must be freed after used.
  if(NULL == msg_recv_queue_mutex){
    err = mico_rtos_init_mutex(&msg_recv_queue_mutex);
    require_noerr_action(err, exit,
                         fogcloud_log("ERROR: mico_rtos_init_mutex (msg_recv_queue_mutex) failed, err=%d.", err));
  }
  err = mico_rtos_init_queue(&msg_recv_queue, "fog_recv_queue", sizeof(int), FOGCLOUD_MAX_RECV_QUEUE_LENGTH);
  require_noerr_action(err, exit,
                       fogcloud_log("ERROR: mico_rtos_init_queue (msg_recv_queue) failed, err=%d", err));
  
  /* start FogCloud service */
  err = fogCloudStart(inContext);
  require_noerr_action(err, exit, 
                       fogcloud_log("ERROR: MicoFogCloudCloudInterfaceStart failed!") );
  
  /* start configServer for fogcloud (server for activate/authorize/reset/ota cmd from user APP) */
  if(false == inContext->flashContentInRam.appConfig.fogcloudConfig.owner_binding){
    err = MicoStartFogCloudConfigServer( inContext);
    require_noerr_action(err, exit, 
                         fogcloud_log("ERROR: start FogCloud configServer failed!") );
  }
  
 #ifdef ENABLE_FOGCLOUD_AUTO_ACTIVATE
  /* activate when wifi on */
  while(false == inContext->flashContentInRam.appConfig.fogcloudConfig.isActivated){
    // auto activate, using default login_id/dev_pass/user_token
    fogcloud_log("device activate start...");
    memset((void*)&devDefaultActivateData, 0, sizeof(devDefaultActivateData));
    strncpy(devDefaultActivateData.loginId,
            inContext->flashContentInRam.appConfig.fogcloudConfig.loginId,
            MAX_SIZE_LOGIN_ID);
    strncpy(devDefaultActivateData.devPasswd,
            inContext->flashContentInRam.appConfig.fogcloudConfig.devPasswd,
            MAX_SIZE_DEV_PASSWD);
    strncpy(devDefaultActivateData.user_token,
            inContext->micoStatus.mac,   // use MAC as default user_token
            MAX_SIZE_USER_TOKEN);
    err = fogCloudDevActivate(inContext, devDefaultActivateData);
    if(kNoErr == err){
      fogcloud_log("device activate success!");
      break;
    }
    else{
      fogcloud_log("device auto activate failed, err = %d, will retry in 3s ...", err);
    }
    mico_thread_sleep(3);
  }

#endif  // ENABLE_FOGCLOUD_AUTO_ACTIVATE
  
#ifndef DISABLE_FOGCLOUD_OTA_CHECK
  /* OTA check just device activated */
  if( (!inContext->appStatus.noOTACheckOnSystemStart) && 
     (inContext->flashContentInRam.appConfig.fogcloudConfig.isActivated) ){
    // start ota thread
    err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "fogcloud_ota", 
                                  fogcloud_ota_thread, STACK_SIZE_FOGCLOUD_OTA_THREAD, 
                                  inContext);
    if(kNoErr != err){
      fogcloud_log("ERROR: start FogCloud OTA thread failed, err=%d.", err);
    }
  }
  inContext->appStatus.noOTACheckOnSystemStart = false;
#endif  // DISABLE_FOGCLOUD_OTA_CHECK
  
  while(1){
    // device info reset
    if(device_need_delete){
      fogcloud_log("delete device from cloud ...");
      memset((void*)&devResetRequestData, 0, sizeof(devResetRequestData));
      strncpy(devResetRequestData.loginId,
              inContext->flashContentInRam.appConfig.fogcloudConfig.loginId,
              MAX_SIZE_LOGIN_ID);
      strncpy(devResetRequestData.devPasswd,
              inContext->flashContentInRam.appConfig.fogcloudConfig.devPasswd,
              MAX_SIZE_DEV_PASSWD);
      strncpy(devResetRequestData.user_token,
              inContext->flashContentInRam.appConfig.fogcloudConfig.userToken,
                MAX_SIZE_USER_TOKEN);
      err = fogCloudResetCloudDevInfo(inContext, devResetRequestData);
      if(kNoErr == err){
        device_need_delete = false;
        fogcloud_log("delete device success, system need reboot...");
        mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
        MicoFogCloudRestoreDefault(inContext);
        MICOUpdateConfiguration(inContext);
        mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
        // system restart
        inContext->micoStatus.sys_state = eState_Software_Reset;
        if(inContext->micoStatus.sys_state_change_sem){
          mico_rtos_set_semaphore(&inContext->micoStatus.sys_state_change_sem);
        }
      }
      else{
        fogcloud_log("delete device failed, err = %d.", err);
      }
    }
    
    mico_thread_sleep(1);
    if(inContext->appStatus.fogcloudStatus.isOTAInProgress){
      continue;  // ota is in progress, the oled && system led will be holding
    }
    
    if(inContext->appStatus.fogcloudStatus.isCloudConnected){
      set_RF_LED_cloud_connected(inContext);  // toggle LED
    }
    else{
      set_RF_LED_cloud_disconnected(inContext);  // stop LED blink
    }
  }
  
exit:
  fogcloud_log("fogcloud_main_thread exit err=%d.", err);
  if(NULL != msg_recv_queue_mutex){
    mico_rtos_deinit_mutex(&msg_recv_queue_mutex);
  }
  if(NULL != msg_recv_queue){
    mico_rtos_deinit_queue(&msg_recv_queue);
  }
  mico_rtos_delete_thread(NULL);
  return;
}
/****************************************************************************
* Function	: eZCB_RemoveNode
* Description	: Remove Node
* Input Para	:
* Output Para	:
* Return Value:
****************************************************************************/
teZcbStatus eZCB_RemoveNode(tsZCB_Node *psZCBNode)
{
    teZcbStatus eStatus = E_ZCB_ERROR;
    tsZCB_Node *psZCBCurrentNode = &sZCB_Network.sNodes;
    int iNodeFreeable = 0;

    /* lock the list mutex and node mutex in the same order as everywhere else to avoid deadlock */

    mico_rtos_unlock_mutex(&psZCBNode->sLock);

    mico_rtos_lock_mutex(&sZCB_Network.sLock);

    mico_rtos_lock_mutex(&psZCBNode->sLock);

    if (psZCBNode == &sZCB_Network.sNodes)
    {
        eStatus = E_ZCB_OK;
        iNodeFreeable = 0;
    }
    else
    {
        while (psZCBCurrentNode)
        {
            if (psZCBCurrentNode->psNext == psZCBNode)
            {
                user_ZBNetwork_log("Found node to remove\n");
                DBG_PrintNode(psZCBNode);

                psZCBCurrentNode->psNext = psZCBCurrentNode->psNext->psNext;
                eStatus = E_ZCB_OK;
                iNodeFreeable = 1;
                break;
            }
            psZCBCurrentNode = psZCBCurrentNode->psNext;
        }
    }

    if (eStatus == E_ZCB_OK)
    {
        int i, j;
        for (i = 0; i < psZCBNode->u32NumEndpoints; i++)
        {
            user_ZBNetwork_log("Free endpoint %d\n", psZCBNode->pasEndpoints[i].u8Endpoint);
            if (psZCBNode->pasEndpoints[i].pasClusters)
            {
                for (j = 0; j < psZCBNode->pasEndpoints[i].u32NumClusters; j++)
                {
                    user_ZBNetwork_log("Free cluster 0x%04X\n", psZCBNode->pasEndpoints[i].pasClusters[j].u16ClusterID);
                    free(psZCBNode->pasEndpoints[i].pasClusters[j].pau16Attributes);
                    free(psZCBNode->pasEndpoints[i].pasClusters[j].pau8Commands);
                }
                free(psZCBNode->pasEndpoints[i].pasClusters);

            }
        }
        free(psZCBNode->pasEndpoints);

        free(psZCBNode->pau16Groups);

        /* Unlock the node first so that it may be free'd */
        mico_rtos_unlock_mutex(&psZCBNode->sLock);
        mico_rtos_deinit_mutex(&psZCBNode->sLock);
        if (iNodeFreeable)
        {
            free(psZCBNode);
        }
    }
    mico_rtos_unlock_mutex(&sZCB_Network.sLock);
    return eStatus;
}