OSStatus MicoUartSend( mico_uart_t uart, const void* data, uint32_t size ) { if(uart_mapping[uart].uart == FUART){ #ifndef NO_MICO_RTOS mico_rtos_lock_mutex(&uart_interfaces[uart].tx_mutex); #endif FuartSend( (uint8_t *)data, size); #ifndef NO_MICO_RTOS mico_rtos_unlock_mutex(&uart_interfaces[uart].tx_mutex); #endif return kNoErr; }else if(uart_mapping[uart].uart == BUART){ #ifndef NO_MICO_RTOS mico_rtos_lock_mutex(&uart_interfaces[uart].tx_mutex); #endif BuartSend( (uint8_t *)data, size); }else { return kUnsupportedErr; } #ifndef NO_MICO_RTOS mico_rtos_get_semaphore( &uart_interfaces[ uart ].tx_complete, MICO_NEVER_TIMEOUT ); mico_rtos_unlock_mutex(&uart_interfaces[uart].tx_mutex); #else while(uart_interfaces[ uart ].tx_complete == false); uart_interfaces[ uart ].tx_complete = false; #endif return kNoErr; }
OSStatus platform_uart_transmit_bytes( platform_uart_driver_t* driver, const uint8_t* data_out, uint32_t size ) { if(driver->peripheral->uart == FUART){ #ifndef NO_MICO_RTOS mico_rtos_lock_mutex(&driver->tx_mutex); #endif FuartSend( (uint8_t *)data_out, size); #ifndef NO_MICO_RTOS mico_rtos_unlock_mutex(&driver->tx_mutex); #endif return kNoErr; }else if(driver->peripheral->uart == BUART){ #ifndef NO_MICO_RTOS mico_rtos_lock_mutex(&driver->tx_mutex); #endif BuartSend( (uint8_t *)data_out, size); }else { return kUnsupportedErr; } #ifndef NO_MICO_RTOS mico_rtos_get_semaphore( &driver->tx_complete, MICO_NEVER_TIMEOUT ); mico_rtos_unlock_mutex( &driver->tx_mutex ); #else while( driver->tx_complete == false ); driver->tx_complete = false; #endif return kNoErr; }
OSStatus bt_smartbridge_att_cache_generate( const mico_bt_smart_device_t* remote_device, uint16_t connection_handle, bt_smartbridge_att_cache_t** cache ) { bt_smartbridge_att_cache_t* new_cache = NULL; OSStatus result; if ( cache == NULL ) { return MICO_BT_BADARG; } if ( att_cache_manager == NULL ) { return MICO_BT_ATT_CACHE_UNINITIALISED; } /* Cached attributes not found. Get a free instance and discover services */ result = smartbridge_att_cache_get_free_cache( &new_cache ); if ( result != MICO_BT_SUCCESS ) { return result; } /* Copy remote device to cache */ mico_rtos_lock_mutex( &new_cache->mutex ); memcpy( &new_cache->remote_device, remote_device, sizeof( new_cache->remote_device ) ); new_cache->connection_handle = connection_handle; new_cache->is_discovering = MICO_TRUE; mico_rtos_unlock_mutex( &new_cache->mutex ); /* Rediscover services */ result = smartbridge_att_cache_discover_all( new_cache, new_cache->connection_handle ); mico_rtos_lock_mutex( &new_cache->mutex ); new_cache->is_discovering = MICO_FALSE; mico_rtos_unlock_mutex( &new_cache->mutex ); if ( result == MICO_BT_SUCCESS ) { result = smartbridge_att_cache_insert_to_used_list( new_cache ); if ( result == MICO_BT_SUCCESS ) { *cache = new_cache; } } else { smartbridge_att_cache_return_to_free_list( new_cache ); } return result; }
void KvVarProtect(bool ProtectOn) { if(KvMutexInited) { if(ProtectOn) { mico_rtos_lock_mutex(&KvMutex); } else { mico_rtos_lock_mutex(&KvMutex); } } }
OSStatus bt_smartbridge_att_cache_release( bt_smartbridge_att_cache_t* cache ) { OSStatus result; if ( att_cache_manager == NULL ) { return MICO_BT_ATT_CACHE_UNINITIALISED; } /* Lock protection */ result = mico_rtos_lock_mutex( &att_cache_manager->mutex ); if ( result != MICO_BT_SUCCESS ) { return result; } result = linked_list_remove_node( &att_cache_manager->used_list, &cache->node ); if ( result == MICO_BT_SUCCESS ) { /* Delete list and set data to NULL */ mico_bt_smart_attribute_delete_list( &cache->attribute_list ); } smartbridge_att_cache_return_to_free_list( cache ); /* Unlock protection */ mico_rtos_unlock_mutex( &att_cache_manager->mutex ); return result; }
OSStatus bt_smartbridge_att_cache_find( const mico_bt_smart_device_t* remote_device, bt_smartbridge_att_cache_t** cache ) { OSStatus result; linked_list_node_t* node_found; if ( remote_device == NULL || cache == NULL ) { return MICO_BT_BADARG; } if ( att_cache_manager == NULL ) { return MICO_BT_ATT_CACHE_UNINITIALISED; } /* Lock protection */ result = mico_rtos_lock_mutex( &att_cache_manager->mutex ); if ( result != MICO_BT_SUCCESS ) { return result; } result = linked_list_find_node( &att_cache_manager->used_list, smartbridge_att_cache_find_by_device_callback, (void*)remote_device, &node_found ); if ( result == MICO_BT_SUCCESS ) { *cache = (bt_smartbridge_att_cache_t*)node_found->data; } /* Unlock protection */ mico_rtos_unlock_mutex( &att_cache_manager->mutex ); return result; }
OSStatus MicoFlashDisableSecurity( mico_partition_t partition, uint32_t off_set, uint32_t size ) { OSStatus err = kNoErr; uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + off_set; uint32_t end_addr = mico_partitions[ partition ].partition_start_addr + off_set + size - 1; require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr ); require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr ); require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr ); require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr ); require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length, exit, err = kParamErr ); if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false ) { err = MicoFlashInitialize( partition ); require_noerr_quiet( err, exit ); } mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); err = platform_flash_disable_protect( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], start_addr, end_addr); mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); exit: return err; }
OSStatus platform_flash_read( platform_flash_driver_t *driver, volatile uint32_t* FlashAddress, uint8_t* Data ,uint32_t DataLength ) { OSStatus err = kNoErr; require_action_quiet( driver != NULL, exit, err = kParamErr); require_action_quiet( driver->initialized != false, exit, err = kNotInitializedErr); require_action( (*FlashAddress >= driver->peripheral->flash_start_addr) && (*FlashAddress + DataLength) <= (driver->peripheral->flash_start_addr + driver->peripheral->flash_length), exit, err = kParamErr); #ifndef NO_MICO_RTOS mico_rtos_lock_mutex( &driver->flash_mutex ); #endif if( driver->peripheral->flash_type == FLASH_TYPE_INTERNAL ){ memcpy(Data, (void *)(*FlashAddress), DataLength); *FlashAddress += DataLength; } #ifdef USE_MICO_SPI_FLASH else if( driver->peripheral->flash_type == FLASH_TYPE_SPI ){ err = sflash_read( &sflash_handle, *FlashAddress, Data, DataLength ); require_noerr(err, exit_with_mutex); *FlashAddress += DataLength; } #endif else{ err = kTypeErr; goto exit_with_mutex; } exit_with_mutex: #ifndef NO_MICO_RTOS mico_rtos_unlock_mutex( &driver->flash_mutex ); #endif exit: return err; }
OSStatus MVDCloudInterfaceResetCloudDevInfo(mico_Context_t* const inContext, MVDResetRequestData_t devResetRequestData) { OSStatus err = kUnknownErr; // login_id/dev_passwd ok ? if((0 != strncmp(inContext->flashContentInRam.appConfig.virtualDevConfig.loginId, devResetRequestData.loginId, strlen(inContext->flashContentInRam.appConfig.virtualDevConfig.loginId))) || (0 != strncmp(inContext->flashContentInRam.appConfig.virtualDevConfig.devPasswd, devResetRequestData.devPasswd, strlen(inContext->flashContentInRam.appConfig.virtualDevConfig.devPasswd)))) { // devPass err cloud_if_log("ERROR: MVDCloudInterfaceResetCloudDevInfo: loginId/devPasswd mismatch!"); return kMismatchErr; } cloud_if_log("MVDCloudInterfaceResetCloudDevInfo: loginId/devPasswd ok!"); err = EasyCloudDeviceReset(&easyCloudContext); require_noerr_action( err, exit, cloud_if_log("ERROR: EasyCloudDeviceReset failed! err=%d", err) ); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); inContext->flashContentInRam.appConfig.virtualDevConfig.isActivated = false; // need to reActivate sprintf(inContext->flashContentInRam.appConfig.virtualDevConfig.deviceId, DEFAULT_DEVICE_ID); sprintf(inContext->flashContentInRam.appConfig.virtualDevConfig.masterDeviceKey, DEFAULT_DEVICE_KEY); sprintf(inContext->flashContentInRam.appConfig.virtualDevConfig.loginId, DEFAULT_LOGIN_ID); sprintf(inContext->flashContentInRam.appConfig.virtualDevConfig.devPasswd, DEFAULT_DEV_PASSWD); inContext->appStatus.virtualDevStatus.isCloudConnected = false; MICOUpdateConfiguration(inContext); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); exit: return err; }
SysComMsg *SysComCreateMsg(MsgType_t msgtype, u16 sizeofmsg, u16 transid, SthreadId sender, SthreadId receiver) { SysComMsg* msg = NULL; void* payload = NULL; mico_rtos_lock_mutex(&SysComMutex); msg = (SysComMsg*)MemMalloc(SIZEOF_STRUCT_SYSCOM); if(msg == NULL) { SysCom_ERR("SysComCreateMsg: msg malloc failed"); } else { SysCom_DBG("SysComCreateMsg: malloc msg(addr: 0x%08X) successfully", (addP_t)msg); payload = MemMalloc(sizeofmsg); if(payload == NULL) { SysCom_ERR("SysComCreateMsg: malloc payload failed"); SysComDestroyMsg(msg); SysCom_DBG("SysComCreateMsg: msg destroyed"); msg = NULL; } else { SysCom_DBG("SysComCreateMsg: malloc payload(addr: 0x%08X) successfully", (addP_t)payload); msg->sender = sender; msg->receiver = receiver; msg->msgType = msgtype; msg->transId = transid; msg->payload = payload; } } mico_rtos_unlock_mutex(&SysComMutex); return (SysComMsg*)msg; }
OSStatus MicoSpiTransfer( const mico_spi_device_t* spi, const mico_spi_message_segment_t* segments, uint16_t number_of_segments ) { platform_spi_config_t config; OSStatus err = kNoErr; if ( spi->port >= MICO_SPI_NONE ) return kUnsupportedErr; #ifdef MICO_WIFI_SHARE_SPI_BUS if( platform_spi_peripherals[spi->port].port == wifi_spi.port ) { return platform_wlan_spi_transfer( &platform_gpio_pins[spi->chip_select], segments, number_of_segments ); } #endif if( platform_spi_drivers[spi->port].spi_mutex == NULL) mico_rtos_init_mutex( &platform_spi_drivers[spi->port].spi_mutex ); config.chip_select = &platform_gpio_pins[spi->chip_select]; config.speed = spi->speed; config.mode = spi->mode; config.bits = spi->bits; mico_rtos_lock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); err = platform_spi_init( &platform_spi_drivers[spi->port], &platform_spi_peripherals[spi->port], &config ); err = platform_spi_transfer( &platform_spi_drivers[spi->port], &config, segments, number_of_segments ); mico_rtos_unlock_mutex( &platform_spi_drivers[spi->port].spi_mutex ); return err; }
OSStatus MicoFlashErase(mico_partition_t partition, uint32_t off_set, uint32_t size) { OSStatus err = kNoErr; uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + off_set; uint32_t end_addr = mico_partitions[ partition ].partition_start_addr + off_set + size - 1; if (size == 0) goto exit; require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr ); require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr ); require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr ); #ifndef BOOTLOADER require_action_quiet( ( mico_partitions[ partition ].partition_options & PAR_OPT_WRITE_MASK ) == PAR_OPT_WRITE_EN, exit, err = kPermissionErr ); #endif require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr ); require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length, exit, err = kParamErr ); if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false ) { err = MicoFlashInitialize( partition ); require_noerr_quiet( err, exit ); } mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); err = platform_flash_erase( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], start_addr, end_addr ); mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); exit: return err; }
// Create Object OSStatus ObjectCreate(const char* obj_name) { u8 index = 0; OSStatus ret; mico_rtos_lock_mutex(&ObjectMutex); do { if(object[index].used == false) { break; } index++; } while(index < MAX_OBJECT_NUM); if(index >= MAX_OBJECT_NUM) { Object_ERR("ObjectChild is fulled, %s create failed", obj_name); ret = kObjFullErr; goto ObjectCreate_RET; } object[index].used = true; strncpy(object[index].objName, obj_name, strlen(obj_name)); objectBackup[index].used = true; strncpy(objectBackup[index].objName, obj_name, strlen(obj_name)); Object_INF("Create ObjectChild %s Successfully", object[index].objName); ret = kNoErr; ObjectCreate_RET: mico_rtos_unlock_mutex(&ObjectMutex); return ret; }
// Data = AuthData#FTCServer(localIp/netMask/gateWay/dnsServer) void EasyLinkNotify_EasyLinkGetExtraDataHandler(int datalen, char* data, mico_Context_t * const inContext) { OSStatus err; int index ; char address[16]; easylink_log_trace(); uint32_t *ipInfo, ipInfoCount; require_action(inContext, exit, err = kParamErr); char *debugString; debugString = DataToHexStringWithSpaces( (const uint8_t *)data, datalen ); easylink_log("Get user info: %s", debugString); free(debugString); for(index = datalen - 1; index>=0; index-- ){ if(data[index] == '#' &&( (datalen - index) == 5 || (datalen - index) == 25 ) ) break; } require_action(index >= 0, exit, err = kParamErr); data[index++] = 0x0; ipInfo = (uint32_t *)&data[index]; ipInfoCount = (datalen - index)/sizeof(uint32_t); require_action(ipInfoCount >= 1, exit, err = kParamErr); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); inContext->flashContentInRam.micoSystemConfig.easylinkServerIP = *(uint32_t *)(ipInfo); if(ipInfoCount == 1){ inContext->flashContentInRam.micoSystemConfig.dhcpEnable = true; inet_ntoa( address, inContext->flashContentInRam.micoSystemConfig.easylinkServerIP); easylink_log("Get auth info: %s, EasyLink server ip address: %s", data, address); }else{ inContext->flashContentInRam.micoSystemConfig.dhcpEnable = false; ipInfo = (uint32_t *)&data[index]; inet_ntoa(inContext->flashContentInRam.micoSystemConfig.localIp, *(uint32_t *)(ipInfo+1)); inet_ntoa(inContext->flashContentInRam.micoSystemConfig.netMask, *(uint32_t *)(ipInfo+2)); inet_ntoa(inContext->flashContentInRam.micoSystemConfig.gateWay, *(uint32_t *)(ipInfo+3)); inet_ntoa(inContext->flashContentInRam.micoSystemConfig.dnsServer, *(uint32_t *)(ipInfo+4)); strcpy((char *)inContext->micoStatus.localIp, inContext->flashContentInRam.micoSystemConfig.localIp); strcpy((char *)inContext->micoStatus.netMask, inContext->flashContentInRam.micoSystemConfig.netMask); strcpy((char *)inContext->micoStatus.gateWay, inContext->flashContentInRam.micoSystemConfig.gateWay); strcpy((char *)inContext->micoStatus.dnsServer, inContext->flashContentInRam.micoSystemConfig.dnsServer); inet_ntoa( address, inContext->flashContentInRam.micoSystemConfig.easylinkServerIP); easylink_log("Get auth info: %s, EasyLink server ip address: %s, local IP info:%s %s %s %s ", data, address, inContext->flashContentInRam.micoSystemConfig.localIp,\ inContext->flashContentInRam.micoSystemConfig.netMask, inContext->flashContentInRam.micoSystemConfig.gateWay,inContext->flashContentInRam.micoSystemConfig.dnsServer); } mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); require_noerr(ConfigELRecvAuthData(data, inContext), exit); EasylinkFailed = false; mico_rtos_set_semaphore(&easylink_sem); ConfigEasyLinkIsSuccess(inContext); return; exit: easylink_log("ERROR, err: %d", err); ConfigWillStop(inContext); MicoSystemReboot(); }
// recv msg from queue OSStatus MicoFogCloudMsgRecv(mico_Context_t* const context, fogcloud_msg_t **msg, uint32_t timeout_ms) { fogcloud_log_trace(); OSStatus err = kUnknownErr; if(NULL == msg){ return kParamErr; } if(NULL != msg_recv_queue){ mico_rtos_lock_mutex(&msg_recv_queue_mutex); if(mico_rtos_is_queue_empty(&msg_recv_queue)){ mico_rtos_unlock_mutex(&msg_recv_queue_mutex); return kUnderrunErr; } err = mico_rtos_pop_from_queue(&msg_recv_queue, msg, timeout_ms); // just pop msg pointer from queue mico_rtos_unlock_mutex(&msg_recv_queue_mutex); if(kNoErr == err){ total_recv_buf_len -= (sizeof(fogcloud_msg_t) - 1 + (*msg)->topic_len + (*msg)->data_len); } } else{ err = kNotInitializedErr; } return err; }
OSStatus platform_flash_erase( platform_flash_driver_t *driver, uint32_t StartAddress, uint32_t EndAddress ) { OSStatus err = kNoErr; require_action_quiet( driver != NULL, exit, err = kParamErr); require_action_quiet( driver->initialized != false, exit, err = kNotInitializedErr); require_action( StartAddress >= driver->peripheral->flash_start_addr && EndAddress <= driver->peripheral->flash_start_addr + driver->peripheral->flash_length - 1, exit, err = kParamErr); #ifndef NO_MICO_RTOS mico_rtos_lock_mutex( &driver->flash_mutex ); #endif if( driver->peripheral->flash_type == FLASH_TYPE_INTERNAL ){ err = internalFlashErase(StartAddress, EndAddress); require_noerr(err, exit_with_mutex); } #ifdef USE_MICO_SPI_FLASH else if( driver->peripheral->flash_type == FLASH_TYPE_SPI ){ err = spiFlashErase(StartAddress, EndAddress); require_noerr(err, exit_with_mutex); } #endif else{ err = kTypeErr; goto exit_with_mutex; } exit_with_mutex: #ifndef NO_MICO_RTOS mico_rtos_unlock_mutex( &driver->flash_mutex ); #endif exit: return err; }
int socket_queue_delete(app_context_t * const inContext, mico_queue_t *queue) { int i; socket_msg_t *msg; int ret = -1; mico_rtos_lock_mutex(&inContext->appStatus.queue_mtx); // remove queue for(i=0; i < MAX_QUEUE_NUM; i++) { if (queue == inContext->appStatus.socket_out_queue[i]) { inContext->appStatus.socket_out_queue[i] = NULL; ret = 0; } } mico_rtos_unlock_mutex(&inContext->appStatus.queue_mtx); // free queue buffer while(kNoErr == mico_rtos_pop_from_queue( queue, &msg, 0)) { socket_msg_free(msg); } // deinit queue mico_rtos_deinit_queue(queue); return ret; }
OSStatus MicoFlashRead( mico_partition_t partition, volatile uint32_t* off_set, uint8_t* outBuffer ,uint32_t inBufferLength) { OSStatus err = kNoErr; uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + *off_set; uint32_t end_addr = mico_partitions[ partition ].partition_start_addr + *off_set + inBufferLength - 1; if (inBufferLength == 0) goto exit; require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr ); require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr ); require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr ); #ifndef BOOTLOADER require_action_quiet( ( mico_partitions[ partition ].partition_options & PAR_OPT_READ_MASK ) == PAR_OPT_READ_EN, exit, err = kPermissionErr ); #endif require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr ); require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length , exit, err = kParamErr ); if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false ) { err = MicoFlashInitialize( partition ); require_noerr_quiet( err, exit ); } mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); err = platform_flash_read( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], &start_addr, outBuffer, inBufferLength ); *off_set = start_addr - mico_partitions[ partition ].partition_start_addr; mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); exit: return err; }
OSStatus fogCloudResetCloudDevInfo(app_context_t* const inContext, MVDResetRequestData_t devResetRequestData) { OSStatus err = kUnknownErr; cloud_if_log("Delete device info from cloud..."); err = FogCloudDeviceReset(&easyCloudContext); require_noerr_action( err, exit, cloud_if_log("ERROR: FogCloudDeviceReset failed! err=%d", err) ); inContext->appStatus.fogcloudStatus.isActivated = false; mico_rtos_lock_mutex(&inContext->mico_context->flashContentInRam_mutex); inContext->appConfig->fogcloudConfig.isActivated = false; // need to reActivate inContext->appConfig->fogcloudConfig.owner_binding = false; // no owner binding sprintf(inContext->appConfig->fogcloudConfig.deviceId, DEFAULT_DEVICE_ID); sprintf(inContext->appConfig->fogcloudConfig.masterDeviceKey, DEFAULT_DEVICE_KEY); sprintf(inContext->appConfig->fogcloudConfig.loginId, DEFAULT_LOGIN_ID); sprintf(inContext->appConfig->fogcloudConfig.devPasswd, DEFAULT_DEV_PASSWD); inContext->appStatus.fogcloudStatus.isCloudConnected = false; err = mico_system_context_update(inContext->mico_context); mico_rtos_unlock_mutex(&inContext->mico_context->flashContentInRam_mutex); exit: return err; }
OSStatus SysComDestroyMsg(SysComMsg *msg) { OSStatus err = kUnknownErr; mico_rtos_lock_mutex(&SysComMutex); if(msg == NULL) { SysCom_ERR("SysComDestroyMsg: msg is NULL"); err = kParamErr; } else { if(msg->payload == NULL) { SysCom_ERR("SysComDestroyMsg: payload is NULL"); err = kParamErr; } else { MemFree(msg->payload); } MemFree(msg); SysCom_DBG("SysComDestroyMsg: msg destroyed"); err = kNoErr; } mico_rtos_unlock_mutex(&SysComMutex); return err; }
void _ConnectToAP( mico_Context_t * const inContext) { mico_log_trace(); network_InitTypeDef_adv_st wNetConfig; mico_log("connect to %s.....", inContext->flashContentInRam.micoSystemConfig.ssid); memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_adv_st)); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); strncpy((char*)wNetConfig.ap_info.ssid, inContext->flashContentInRam.micoSystemConfig.ssid, maxSsidLen); memcpy(wNetConfig.ap_info.bssid, inContext->flashContentInRam.micoSystemConfig.bssid, 6); wNetConfig.ap_info.channel = inContext->flashContentInRam.micoSystemConfig.channel; wNetConfig.ap_info.security = inContext->flashContentInRam.micoSystemConfig.security; memcpy(wNetConfig.key, inContext->flashContentInRam.micoSystemConfig.key, inContext->flashContentInRam.micoSystemConfig.keyLength); wNetConfig.key_len = inContext->flashContentInRam.micoSystemConfig.keyLength; if(inContext->flashContentInRam.micoSystemConfig.dhcpEnable == true) wNetConfig.dhcpMode = DHCP_Client; else wNetConfig.dhcpMode = DHCP_Disable; strncpy((char*)wNetConfig.local_ip_addr, inContext->flashContentInRam.micoSystemConfig.localIp, maxIpLen); strncpy((char*)wNetConfig.net_mask, inContext->flashContentInRam.micoSystemConfig.netMask, maxIpLen); strncpy((char*)wNetConfig.gateway_ip_addr, inContext->flashContentInRam.micoSystemConfig.gateWay, maxIpLen); strncpy((char*)wNetConfig.dnsServer_ip_addr, inContext->flashContentInRam.micoSystemConfig.dnsServer, maxIpLen); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); wNetConfig.wifi_retry_interval = 100; StartAdvNetwork(&wNetConfig); }
OSStatus ConfigIncommingJsonMessage( const char *input, mico_Context_t * const inContext ) { OSStatus err = kNoErr; json_object *new_obj; config_delegate_log_trace(); new_obj = json_tokener_parse(input); require_action(new_obj, exit, err = kUnknownErr); config_delegate_log("Recv config object=%s", json_object_to_json_string(new_obj)); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); json_object_object_foreach(new_obj, key, val) { if(!strcmp(key, "Device Name")){ strncpy(inContext->flashContentInRam.micoSystemConfig.name, json_object_get_string(val), maxNameLen); }else if(!strcmp(key, "RF power save")){ inContext->flashContentInRam.micoSystemConfig.rfPowerSaveEnable = json_object_get_boolean(val); }else if(!strcmp(key, "MCU power save")){ inContext->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable = json_object_get_boolean(val); }else if(!strcmp(key, "Bonjour")){ inContext->flashContentInRam.micoSystemConfig.bonjourEnable = json_object_get_boolean(val); }else if(!strcmp(key, "Wi-Fi")){ strncpy(inContext->flashContentInRam.micoSystemConfig.ssid, json_object_get_string(val), maxSsidLen); inContext->flashContentInRam.micoSystemConfig.channel = 0; memset(inContext->flashContentInRam.micoSystemConfig.bssid, 0x0, 6); inContext->flashContentInRam.micoSystemConfig.security = SECURITY_TYPE_AUTO; memcpy(inContext->flashContentInRam.micoSystemConfig.key, inContext->flashContentInRam.micoSystemConfig.user_key, maxKeyLen); inContext->flashContentInRam.micoSystemConfig.keyLength = inContext->flashContentInRam.micoSystemConfig.user_keyLength; }else if(!strcmp(key, "Password")){ inContext->flashContentInRam.micoSystemConfig.security = SECURITY_TYPE_AUTO; strncpy(inContext->flashContentInRam.micoSystemConfig.key, json_object_get_string(val), maxKeyLen); strncpy(inContext->flashContentInRam.micoSystemConfig.user_key, json_object_get_string(val), maxKeyLen); inContext->flashContentInRam.micoSystemConfig.keyLength = strlen(inContext->flashContentInRam.micoSystemConfig.key); inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen(inContext->flashContentInRam.micoSystemConfig.key); }else if(!strcmp(key, "DHCP")){ inContext->flashContentInRam.micoSystemConfig.dhcpEnable = json_object_get_boolean(val); }else if(!strcmp(key, "IP address")){ strncpy(inContext->flashContentInRam.micoSystemConfig.localIp, json_object_get_string(val), maxIpLen); }else if(!strcmp(key, "Net Mask")){ strncpy(inContext->flashContentInRam.micoSystemConfig.netMask, json_object_get_string(val), maxIpLen); }else if(!strcmp(key, "Gateway")){ strncpy(inContext->flashContentInRam.micoSystemConfig.gateWay, json_object_get_string(val), maxIpLen); }else if(!strcmp(key, "DNS Server")){ strncpy(inContext->flashContentInRam.micoSystemConfig.dnsServer, json_object_get_string(val), maxIpLen); }/*else if(!strcmp(key, "Baurdrate")){ inContext->flashContentInRam.appConfig.fogcloudConfig.USART_BaudRate = json_object_get_int(val); } */else if(!strcmp(key, "loginId")){ strncpy(inContext->flashContentInRam.appConfig.fogcloudConfig.loginId, json_object_get_string(val), MAX_SIZE_LOGIN_ID); } else if(!strcmp(key, "devpasswd")){ strncpy(inContext->flashContentInRam.appConfig.fogcloudConfig.devPasswd, json_object_get_string(val), MAX_SIZE_DEV_PASSWD); } else if(!strcmp(key, "userToken")){ strncpy(inContext->flashContentInRam.appConfig.fogcloudConfig.userToken, json_object_get_string(val), MAX_SIZE_USER_TOKEN); }else{ } } json_object_put(new_obj); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); exit: return err; }
void SysComSetReceiver(SysComMsg* msg, SthreadId receiver) { mico_rtos_lock_mutex(&SysComMutex); msg->receiver = receiver; SysCom_DBG("SysComSetReceiver: set msg(addr: 0x%08X) receiver(%d)", (addP_t)msg, msg->receiver); mico_rtos_unlock_mutex(&SysComMutex); }
void SysComSetSender(SysComMsg* msg, SthreadId sender) { mico_rtos_lock_mutex(&SysComMutex); msg->sender = sender; SysCom_DBG("SysComSetSender: set msg(addr: 0x%08X) sender(%d)", (addP_t)msg, msg->sender); mico_rtos_unlock_mutex(&SysComMutex); }
static void printlog(char *M, char *N) { if (mico_debug_enabled == 0) { return; } mico_rtos_lock_mutex( &stdio_tx_mutex ); printf("[%ld][%s] %s\r\n", mico_rtos_get_time(), M, N); mico_rtos_unlock_mutex( &stdio_tx_mutex ); }
// Add Dist into Object OSStatus ObjectAddDist(const char* obj_name, const char* dist_name, u8 ds) { u8 index; u8 dist_index; OSStatus ret; mico_rtos_lock_mutex(&ObjectMutex); index = 0; do { if((object[index].used == true) && (strncmp(object[index].objName, obj_name, strlen(obj_name)) == 0)) { break; } index++; } while(index < MAX_OBJECT_NUM); if(index >= MAX_OBJECT_NUM) { Object_ERR("ObjectChild %s isn't exist", obj_name); ret = kObjNotExistErr; goto ObjectAddDist_RET; } dist_index = 0; do { if(object[index].dist[dist_index].used == false) { break; } dist_index++; } while(dist_index < MAX_DIST_NUM_IN_OBJECT); if(dist_index >= MAX_DIST_NUM_IN_OBJECT) { Object_ERR("Dist is fulled, add %s is failed", dist_name); ret = kDistFullErr; goto ObjectAddDist_RET; } object[index].dist[dist_index].used = true; object[index].dist[dist_index].dsType = ds; strncpy(object[index].dist[dist_index].distName, dist_name, strlen(dist_name)); objectBackup[index].dist[dist_index].used = true; objectBackup[index].dist[dist_index].dsType = ds; strncpy(objectBackup[index].dist[dist_index].distName, dist_name, strlen(dist_name)); Object_INF("Add %s/%s Successfully", object[index].objName, object[index].dist[dist_index].distName); ret = kNoErr; ObjectAddDist_RET: mico_rtos_unlock_mutex(&ObjectMutex); return ret; }
/**************************************************************************** * Function : psZCB_FindNodeShortAddress * Description : Find Node * Input Para : * Output Para : * Return Value: ****************************************************************************/ tsZCB_Node *psZCB_FindNodeShortAddress(uint16_t u16ShortAddress) { tsZCB_Node *psZCBNode = &sZCB_Network.sNodes; mico_rtos_lock_mutex(&sZCB_Network.sLock); while (psZCBNode) { if (psZCBNode->u16ShortAddress == u16ShortAddress) { int iLockAttempts = 0; user_ZBNetwork_log("Short address 0x%04X found in network", u16ShortAddress); DBG_PrintNode(psZCBNode); while (++iLockAttempts < 5) { if (mico_rtos_lock_mutex(&psZCBNode->sLock) == kNoErr) { break; } else { mico_rtos_unlock_mutex(&sZCB_Network.sLock); if (iLockAttempts == 5) { user_ZBNetwork_log("\n\nError: Could not get lock on node!!"); return NULL; } mico_thread_msleep(1000); mico_rtos_lock_mutex(&sZCB_Network.sLock); } } break; } psZCBNode = psZCBNode->psNext; } mico_rtos_unlock_mutex(&sZCB_Network.sLock); return psZCBNode; }
void *SysComGetPayload(SysComMsg* msg) { mico_rtos_lock_mutex(&SysComMutex); SysCom_DBG("SysComGetPayload: get the msg(addr: 0x%08X) payload(addr: 0x%08X)", (addP_t)msg, (addP_t)msg->payload); mico_rtos_unlock_mutex(&SysComMutex); return msg->payload; }
/**************************************************************************** * Function : psZCB_FindNodeIEEEAddress * Description : Find Node * Input Para : * Output Para : * Return Value: ****************************************************************************/ tsZCB_Node *psZCB_FindNodeIEEEAddress(uint64_t u64IEEEAddress) { tsZCB_Node *psZCBNode = &sZCB_Network.sNodes; mico_rtos_lock_mutex(&sZCB_Network.sLock); while (psZCBNode) { if (psZCBNode->u64IEEEAddress == u64IEEEAddress) { int iLockAttempts = 0; user_ZBNetwork_log("IEEE address 0x%016llX found in network", (unsigned long long int)u64IEEEAddress); DBG_PrintNode(psZCBNode); while (++iLockAttempts < 5) { if (mico_rtos_lock_mutex(&psZCBNode->sLock) == kNoErr) { break; } else { mico_rtos_unlock_mutex(&sZCB_Network.sLock); if (iLockAttempts == 5) { user_ZBNetwork_log("\nError: Could not get lock on node!!"); return NULL; } mico_thread_msleep(1000); mico_rtos_lock_mutex(&sZCB_Network.sLock); } } break; } psZCBNode = psZCBNode->psNext; } mico_rtos_unlock_mutex(&sZCB_Network.sLock); return psZCBNode; }
SthreadId SysComGetReceiver(SysComMsg* msg) { mico_rtos_lock_mutex(&SysComMutex); SysCom_DBG("SysComGetReceiver: get msg(addr: 0x%08X) receiver(%d)", (addP_t)msg, msg->receiver); mico_rtos_unlock_mutex(&SysComMutex); return msg->receiver; }