/* * \internal Read data from usart interface * * \param[in] io_descr The pointer to an io descriptor * \param[in] buf A buffer to read data to * \param[in] length The size of a buffer * \param[in] timeout The millisecond of timeout * * \return The number of bytes user want to read. */ static int32_t usart_os_read(struct io_descriptor *const io_descr, uint8_t *const buf, const uint16_t length, uint32_t timeout) { uint16_t was_read = 0; struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io); ASSERT(buf); if (aos_mutex_lock(&descr->rx_mutex, AOS_WAIT_FOREVER) != 0) { return -1; } if (ringbuffer_num(&descr->rx) < length) { descr->rx_size = 0; descr->rx_length = length; descr->rx_buffer = buf; while (ringbuffer_num(&descr->rx) > 0) { ringbuffer_get(&descr->rx, &descr->rx_buffer[descr->rx_size++]); } if (sem_down(&descr->rx_sem, timeout) != 0) { aos_mutex_unlock(&descr->rx_mutex); return ERR_TIMEOUT; } } else { while (was_read < length) { ringbuffer_get(&descr->rx, &buf[was_read++]); } } aos_mutex_unlock(&descr->rx_mutex); return (int32_t)length; }
/** * This API is used, usually by athost, to send stream content without response * required. The content is usually status event, such as * YEVENT:MONITOR_UP/MONITOR_DOWN, etc. */ static int at_send_raw_no_rsp(const char *content) { int ret; aos_mutex_lock(&at.at_uart_send_mutex, AOS_WAIT_FOREVER); if (content) { #ifdef HDLC_UART if ((ret = hdlc_uart_send(&hdlc_encode_ctx, at._pstuart, (void *)content, strlen(content), at._timeout, true)) != 0) #else if ((ret = hal_uart_send(at._pstuart, (void *)content, strlen(content), at._timeout)) != 0) #endif { LOGE(MODULE_NAME, "uart send raw content (%s) failed", content); aos_mutex_unlock(&at.at_uart_send_mutex); assert(0); return -1; } LOGD(MODULE_NAME, "Raw content (%s) with no response required sent.", content); } aos_mutex_unlock(&at.at_uart_send_mutex); return 0; }
int aos_open(const char *path, int flags) { file_t *file; inode_t *node; size_t len = 0; int ret = VFS_SUCCESS; if (path == NULL) { return -EINVAL; } len = strlen(path); if (len > PATH_MAX) { return -ENAMETOOLONG; } if ((ret = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return ret; } node = inode_open(path); if (node == NULL) { aos_mutex_unlock(&g_vfs_mutex); return trap_open(path, flags); } node->i_flags = flags; file = new_file(node); aos_mutex_unlock(&g_vfs_mutex); if (file == NULL) { return -ENFILE; } if (INODE_IS_FS(node)) { if ((node->ops.i_fops->open) != NULL) { ret = (node->ops.i_fops->open)(file, path, flags); } } else { if ((node->ops.i_ops->open) != NULL) { ret = (node->ops.i_ops->open)(node, file); } } if (ret != VFS_SUCCESS) { del_file(file); return ret; } return get_fd(file); }
aos_dir_t *aos_opendir(const char *path) { file_t *file; inode_t *node; aos_dir_t *dp = NULL; if (path == NULL) { return NULL; } if (aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER) != 0) { return NULL; } node = inode_open(path); if (node == NULL) { aos_mutex_unlock(&g_vfs_mutex); return NULL; } file = new_file(node); aos_mutex_unlock(&g_vfs_mutex); if (file == NULL) { return NULL; } if (INODE_IS_FS(node)) { if ((node->ops.i_fops->opendir) != NULL) { dp = (node->ops.i_fops->opendir)(file, path); } } if (dp == NULL) { if (aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER) != 0) { return NULL; } del_file(file); aos_mutex_unlock(&g_vfs_mutex); return NULL; } dp->dd_vfs_fd = get_fd(file); return dp; }
int aos_closedir(aos_dir_t *dir) { file_t *f; inode_t *node; int err, ret = -ENOSYS; if (dir == NULL) { return -EINVAL; } f = get_file(dir->dd_vfs_fd); if (f == NULL) { return -ENOENT; } node = f->node; if (INODE_IS_FS(node)) { if ((node->ops.i_fops->closedir) != NULL) { ret = (node->ops.i_fops->closedir)(f, dir); } } if ((err = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return err; } del_file(f); aos_mutex_unlock(&g_vfs_mutex); return ret; }
ssize_t vfs_rtc_read(file_t *fp, void *buf, size_t nbytes) { int ret = -1; /* return value */ rtc_dev_t *rtc_dev = NULL; /* device pointer */ /* check empty pointer. */ if ((fp != NULL) && (fp->node != NULL) && (nbytes >= sizeof(rtc_time_t))) { /* get the device pointer. */ rtc_dev = (rtc_dev_t *)(fp->node->i_arg); /* lock the device. */ ret = aos_mutex_lock(&fp->node->mutex, AOS_WAIT_FOREVER); if (ret == 0) { /* get data from rtc. */ ret = hal_rtc_get_time(rtc_dev, (rtc_time_t *)buf); /* If the data is read correctly the return value is set to nbytes. */ if (ret == 0) { ret = nbytes; } } /* unlock the device. */ aos_mutex_unlock(&fp->node->mutex); } else { ret = -EINVAL; } return ret; }
ssize_t vfs_rtc_write(file_t *fp, const void *buf, size_t nbytes) { int ret = -1; /* return value */ rtc_dev_t *rtc_dev = NULL; /* device pointer */ /* check empty pointer. */ if ((fp != NULL) && (fp->node != NULL) && (nbytes >= sizeof(rtc_time_t))) { /* get the device pointer. */ rtc_dev = (rtc_dev_t *)(fp->node->i_arg); /* lock the device. */ ret = aos_mutex_lock(&fp->node->mutex, AOS_WAIT_FOREVER); if (ret == 0) { /* set rtc time. */ ret = hal_rtc_set_time(rtc_dev, (const rtc_time_t *)buf); /* If the time is set successfully, set the return value to nbytes. */ if (ret == 0) { ret = nbytes; } } /* unlock the device. */ aos_mutex_unlock(&fp->node->mutex); } else { ret = -EINVAL; } return ret; }
int vfs_rtc_close(file_t *fp) { int ret = -1; /* return value */ rtc_dev_t *rtc_dev = NULL; /* device pointer */ /* check empty pointer. */ if ((fp != NULL) && (fp->node != NULL)) { /* close device if the device is last closed. */ if (fp->node->refs == 1) { /* get the device pointer. */ rtc_dev = (rtc_dev_t *)(fp->node->i_arg); /* lock the device. */ ret = aos_mutex_lock(&fp->node->mutex, AOS_WAIT_FOREVER); if (ret == 0) { /* turns off hardware. */ ret = hal_rtc_finalize(rtc_dev); } /* unlock the device. */ aos_mutex_unlock(&fp->node->mutex); } else { ret = VFS_SUCCESS; } } else { ret = -EINVAL; } return ret; }
static int at_putc(char c) { int ret = 0; if (inited == 0) { LOGE(MODULE_NAME, "at have not init yet\r\n"); return -1; } if (at._mode != ASYN) { LOGE(MODULE_NAME, "AT mode is normal, can no use at_putc \r\n"); return -1; } LOGD(MODULE_NAME, "uart sending %c(0x%02x)\r\n", c, c); aos_mutex_lock(&at.at_uart_send_mutex, AOS_WAIT_FOREVER); #ifdef HDLC_UART ret = hdlc_uart_send(&hdlc_encode_ctx, at._pstuart, (void *)&c, 1, at._timeout, false); #else ret = hal_uart_send(at._pstuart, (void *)&c, 1, at._timeout); #endif aos_mutex_unlock(&at.at_uart_send_mutex); return ret; }
int aos_rename(const char *oldpath, const char *newpath) { file_t *f; inode_t *node; int err, ret = -ENOSYS; if (oldpath == NULL || newpath == NULL) { return -EINVAL; } if ((err = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return err; } node = inode_open(oldpath); if (node == NULL) { aos_mutex_unlock(&g_vfs_mutex); return -ENODEV; } f = new_file(node); aos_mutex_unlock(&g_vfs_mutex); if (f == NULL) { return -ENOENT; } if (INODE_IS_FS(node)) { if ((node->ops.i_fops->rename) != NULL) { ret = (node->ops.i_fops->rename)(f, oldpath, newpath); } } if ((err = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return err; } del_file(f); aos_mutex_unlock(&g_vfs_mutex); return ret; }
int aos_mkdir(const char *path) { file_t *file; inode_t *node; int err, ret = -ENOSYS; if (path == NULL) { return -EINVAL; } if ((err = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return err; } node = inode_open(path); if (node == NULL) { aos_mutex_unlock(&g_vfs_mutex); return -ENODEV; } file = new_file(node); aos_mutex_unlock(&g_vfs_mutex); if (file == NULL) { return -ENOENT; } if (INODE_IS_FS(node)) { if ((node->ops.i_fops->mkdir) != NULL) { ret = (node->ops.i_fops->mkdir)(file, path); } } if ((err = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return err; } del_file(file); aos_mutex_unlock(&g_vfs_mutex); return ret; }
void k_mutex_unlock(struct k_mutex *mutex) { if (NULL == mutex) { BT_ERR("mutex is NULL\n"); return; } aos_mutex_unlock(&mutex->mutex); return ; }
static void at_set_mode(at_mode_t m) { if (m == at._mode) { return; } /*at operate mode changed, it should wait the uart read operate finished in * the origin operate mode*/ aos_mutex_lock(&at.at_mutex, AOS_WAIT_FOREVER); at._mode = m; aos_mutex_unlock(&at.at_mutex); }
static int at_worker_task_add(at_task_t *tsk) { if (NULL == tsk) { LOGE(MODULE_NAME, "invalid input %s \r\n", __func__); return -1; } aos_mutex_lock(&at.task_mutex, AOS_WAIT_FOREVER); slist_add_tail(&tsk->next, &at.task_l); aos_mutex_unlock(&at.task_mutex); return 0; }
int aos_ioctl_in_loop(int cmd, unsigned long arg) { int err; int fd; for (fd = AOS_CONFIG_VFS_FD_OFFSET; fd < AOS_CONFIG_VFS_FD_OFFSET + AOS_CONFIG_VFS_DEV_NODES; fd++) { file_t *f; inode_t *node; if ((err = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return err; } f = get_file(fd); if (f == NULL) { aos_mutex_unlock(&g_vfs_mutex); return -ENOENT; } if ((err = aos_mutex_unlock(&g_vfs_mutex)) != 0) { return err; } node = f->node; if ((node->ops.i_ops->ioctl) != NULL) { err = (node->ops.i_ops->ioctl)(f, cmd, arg); if (err != VFS_SUCCESS) { return err; } } } return VFS_SUCCESS; }
static int at_send_raw_self_define_respone_formate(const char *command, char *rsp, uint32_t rsplen, char *rsp_prefix, char *rsp_success_postfix, char *rsp_fail_postfix) { int ret = 0; aos_mutex_lock(&at.at_uart_send_mutex, AOS_WAIT_FOREVER); ret = at_send_raw_self_define_respone_formate_internal( command, strlen(command), rsp, rsplen, rsp_prefix, rsp_success_postfix, rsp_fail_postfix); aos_mutex_unlock(&at.at_uart_send_mutex); return ret; }
static int sal_wifi_close(int fd, int32_t remote_port) { int link_id; char cmd[STOP_CMD_LEN] = {0}, out[64]; link_id = fd_to_linkid(fd); if (link_id >= LINK_ID_MAX) { LOGE(TAG, "No connection found for fd (%d) in %s", fd, __func__); return -1; } snprintf(cmd, STOP_CMD_LEN - 1, "%s=%d", STOP_CMD, link_id); LOGD(TAG, "%s %d - AT cmd to run: %s", __func__, __LINE__, cmd); at.send_raw(cmd, out, sizeof(out)); LOGD(TAG, "The AT response is: %s", out); if (strstr(out, CMD_FAIL_RSP) != NULL) { LOGE(TAG, "%s %d failed", __func__, __LINE__); goto err; } if (aos_sem_wait(&g_link[link_id].sem_close, AOS_WAIT_FOREVER) != 0) { LOGE(TAG, "%s sem_wait failed", __func__); goto err; } LOGD(TAG, "%s sem_wait succeed.", __func__); err: if (aos_mutex_lock(&g_link_mutex, AOS_WAIT_FOREVER) != 0) { LOGE(TAG, "Failed to lock mutex (%s).", __func__); return -1; } if (aos_sem_is_valid(&g_link[link_id].sem_start)) { aos_sem_free(&g_link[link_id].sem_start); } if (aos_sem_is_valid(&g_link[link_id].sem_close)) { aos_sem_free(&g_link[link_id].sem_close); } g_link[link_id].fd = -1; aos_mutex_unlock(&g_link_mutex); return -1; }
static int fd_to_linkid(int fd) { int link_id; if (aos_mutex_lock(&g_link_mutex, AOS_WAIT_FOREVER) != 0) { LOGE(TAG, "Failed to lock mutex (%s).", __func__); return -1; } for (link_id = 0; link_id < LINK_ID_MAX; link_id++) { if (g_link[link_id].fd == fd) { break; } } aos_mutex_unlock(&g_link_mutex); return link_id; }
static int at_worker_task_del(at_task_t *tsk) { if (NULL == tsk) { LOGE(MODULE_NAME, "invalid input %s \r\n", __func__); return -1; } aos_mutex_lock(&at.task_mutex, AOS_WAIT_FOREVER); slist_del(&tsk->next, &at.task_l); aos_mutex_unlock(&at.task_mutex); if (aos_sem_is_valid(&tsk->smpr)) { aos_sem_free(&tsk->smpr); } if (tsk) { aos_free(tsk); } return 0; }
/* * \internal Write the given data to usart interface * * \param[in] descr The pointer to an io descriptor * \param[in] buf Data to write to usart * \param[in] length The number of bytes to write * \param[in] timeout The millisecond of timeout * * \return The number of bytes written or <0 for timeout. */ static int32_t usart_os_write(struct io_descriptor *const io_descr, const uint8_t *const buf, const uint16_t length, uint32_t timeout) { int ret; struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io); if (aos_mutex_lock(&descr->tx_mutex, AOS_WAIT_FOREVER) != 0) { return -1; } descr->tx_buffer = (uint8_t *)buf; descr->tx_buffer_length = length; descr->tx_por = 0; _usart_async_enable_byte_sent_irq(&descr->device); ret = sem_down(&descr->tx_sem, timeout); aos_mutex_unlock(&descr->tx_mutex); return (ret == 0) ? length : ERR_TIMEOUT; }
static int at_getc(char *c) { int ret = 0; char data; uint32_t recv_size = 0; if (NULL == c) { return -1; } if (inited == 0) { LOGE(MODULE_NAME, "at have not init yet\r\n"); return -1; } #if 1 if (at._mode != ASYN) { return -1; } #endif aos_mutex_lock(&at.at_mutex, AOS_WAIT_FOREVER); #ifdef HDLC_UART ret = hdlc_uart_recv(&hdlc_decode_ctx, at._pstuart, (void *)&data, 1, &recv_size, at._timeout); #else ret = hal_uart_recv_II(at._pstuart, (void *)&data, 1, &recv_size, at._timeout); #endif aos_mutex_unlock(&at.at_mutex); if (ret != 0) { return -1; } if (recv_size == 1) { *c = data; return 0; } else { return -1; } }
static int at_read(char *data, int size) { int ret = 0; uint32_t recv_size, total_read = 0; if (inited == 0) { LOGE(MODULE_NAME, "at have not init yet\r\n"); return -1; } aos_mutex_lock(&at.at_mutex, AOS_WAIT_FOREVER); while (total_read < size) { #ifdef HDLC_UART ret = hdlc_uart_recv(&hdlc_decode_ctx, at._pstuart, (void *)(data + total_read), size - total_read, &recv_size, at._timeout); #else ret = hal_uart_recv_II(at._pstuart, (void *)(data + total_read), size - total_read, &recv_size, at._timeout); #endif if (ret != 0) { LOGE(MODULE_NAME, "at_read failed on uart_recv."); break; } if (recv_size <= 0) { continue; } total_read += recv_size; if (total_read >= size) { break; } } aos_mutex_unlock(&at.at_mutex); if (ret != 0) { return -1; } return total_read; }
/* * \internal Read data from usart interface * * \param[in] descr The pointer to an io descriptor * \param[in] buf A buffer to read data to * \param[in] length The size of a buffer * \param[in] timeout The millisecond of timeout * * \return The number of bytes read. */ static int32_t usart_async_read(struct io_descriptor *const io_descr, uint8_t *const buf, const uint16_t length, uint32_t timeout) { uint16_t was_read = 0; uint32_t num; size_t rev_size = 0; int32_t ret; struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io); ASSERT(descr && buf && length); if (aos_mutex_lock(&descr->rx_mutex, AOS_WAIT_FOREVER) != 0) { return -1; } while (was_read < length) { ret = krhino_buf_queue_recv(&descr->kbuf, timeout, &buf[was_read], &rev_size); if((ret == 0) && (rev_size == 1)) { was_read++; } else { break; } } aos_mutex_unlock(&descr->rx_mutex); return (int32_t)was_read; }
ssize_t vfs_i2c_read(file_t *fp, void *buf, size_t nbytes) { int ret = -1; /* return value */ i2c_dev_t *i2c_dev = NULL; /* device pointer */ uint16_t dev_addr = 0; /* dev address */ /* check empty pointer. */ if ((fp != NULL) && (fp->node != NULL)) { /* get the device pointer. */ i2c_dev = (i2c_dev_t *)(fp->node->i_arg); /* get the device address. */ dev_addr = i2c_dev->config.dev_addr; /* lock the device. */ ret = aos_mutex_lock(&fp->node->mutex, AOS_WAIT_FOREVER); if (ret == 0) { /* get data from i2c. */ ret = hal_i2c_master_recv(i2c_dev, dev_addr, (uint8_t *)buf, nbytes, HAL_WAIT_FOREVER); /* If the data is read correctly, the return value is set to read bytes. */ if (ret == 0) { ret = nbytes; } } /* unlock the device. */ aos_mutex_unlock(&fp->node->mutex); } else { ret = -EINVAL; } return ret; }
/* * \internal Write the given data to usart interface * * \param[in] descr The pointer to an io descriptor * \param[in] buf Data to write to usart * \param[in] length The number of bytes to write * \param[in] timeout The millisecond of timeout * * \return The number of bytes written. */ static int32_t usart_sync_write(struct io_descriptor *const io_descr, const uint8_t *const buf, const uint16_t length, uint32_t timeout) { ASSERT(io_descr && buf && length); uint32_t offset = 0; struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io); if (aos_mutex_lock(&descr->tx_mutex, AOS_WAIT_FOREVER) != 0) { return -1; } /* Flush unexpected data */ krhino_buf_queue_flush(&descr->kbuf); do { _usart_sync_write_byte(&descr->sync_device, buf[offset]); while (!_usart_sync_is_ready_to_send(&descr->sync_device)) { }; } while (++offset < length); while (!_usart_sync_is_transmit_done(&descr->sync_device)) { }; aos_mutex_unlock(&descr->tx_mutex); return offset; }
static int at_write(const char *data, int size) { int ret = 0; if (inited == 0) { LOGE(MODULE_NAME, "at have not init yet\r\n"); return -1; } aos_mutex_lock(&at.at_uart_send_mutex, AOS_WAIT_FOREVER); #ifdef HDLC_UART ret = hdlc_uart_send(&hdlc_encode_ctx, at._pstuart, (void *)data, size, AOS_WAIT_FOREVER, true); #else ret = hal_uart_send(at._pstuart, (void *)data, size, AOS_WAIT_FOREVER); #endif aos_mutex_unlock(&at.at_uart_send_mutex); if (ret != 0) { return -1; } return size; }
int aos_close(int fd) { int ret = VFS_SUCCESS; file_t *f; inode_t *node; f = get_file(fd); if (f == NULL) { return trap_close(fd); } node = f->node; if (INODE_IS_FS(node)) { if ((node->ops.i_fops->close) != NULL) { ret = (node->ops.i_fops->close)(f); } } else { if ((node->ops.i_ops->close) != NULL) { ret = (node->ops.i_ops->close)(f); } } if ((ret = aos_mutex_lock(&g_vfs_mutex, AOS_WAIT_FOREVER)) != 0) { return ret; } del_file(f); aos_mutex_unlock(&g_vfs_mutex); return ret; }
int playback_from_flash(void) { int ret = 0; int i; int pos_buff = 0; uint32_t pos_flash = 0; uint32_t part_len = DATA_BUFF_LEN / 2; uint32_t part_bytes = part_len * SAI_DATA_BYTES; uint32_t total_size = get_audio_part_len(); if (!aos_mutex_is_valid(&sai_mutex)) { KIDS_A10_PRT("aos_mutex_is_valid return false.\n"); return -1; } ret = aos_mutex_lock(&sai_mutex, SAI_WAIT_TIMEOUT); if (ret != 0) { KIDS_A10_PRT("SAI is very busy now.\n"); return -1; } if (!aos_sem_is_valid(&audio_sem)) { KIDS_A10_PRT("aos_sem_is_valid return false.\n"); ret = -1; goto PB_EXIT; } ret = reinit_sai_and_dma(SAI_dir_tx_m2p); if (ret != 0) { ret = -1; goto PB_EXIT; } printf("Playback time is %f seconds!\n", get_run_time()); #ifdef FLASH_MONO_DATA ret = hal_flash_read(PART_FOR_AUDIO, &pos_flash, &data_buff[pos_buff], part_bytes); if (ret != 0) { ret = -1; goto PB_EXIT; } ready_to_send(data_buff, part_len); ret = HAL_SAI_Transmit_DMA(&hsai_BlockA1, (uint8_t *)data_buff, DATA_BUFF_LEN); if (ret != 0) { KIDS_A10_PRT("HAL_SAI_Transmit_DMA return failed.\n"); ret = -1; goto PB_EXIT; } while (1) { /* Wait a callback event */ while (UpdatePointer == -1) { aos_sem_wait(&audio_sem, DMA_WAIT_TIMEOUT); if (ret != 0) KIDS_A10_PRT("DMA timeout.\n"); } pos_buff = UpdatePointer; UpdatePointer = -1; /* Upate the first or the second part of the buffer */ ret = hal_flash_read(PART_FOR_AUDIO, &pos_flash, &data_buff[pos_buff], part_bytes / 2); if (ret != 0) { ret = -1; break; } ready_to_send(&data_buff[pos_buff], part_len / 2); /* check the end of the file */ if ((pos_flash + part_bytes / 2) > total_size) { ret = HAL_SAI_DMAStop(&hsai_BlockA1); if (ret != 0) { KIDS_A10_PRT("HAL_SAI_DMAStop return failed.\n"); ret = -1; } break; } if (UpdatePointer != -1) { /* Buffer update time is too long compare to the data transfer time */ KIDS_A10_PRT("UpdatePointer error.\n"); ret = -1; break; } } #else ret = hal_flash_read(PART_FOR_AUDIO, &pos_flash, &data_buff[pos_buff], DATA_BUFF_LEN * SAI_DATA_BYTES); if (ret != 0) { ret = -1; goto PB_EXIT; } ret = HAL_SAI_Transmit_DMA(&hsai_BlockA1, (uint8_t *)data_buff, DATA_BUFF_LEN); if (ret != 0) { KIDS_A10_PRT("HAL_SAI_Transmit_DMA return failed.\n"); ret = -1; goto PB_EXIT; } while (1) { /* Wait a callback event */ while (UpdatePointer == -1) { aos_sem_wait(&audio_sem, DMA_WAIT_TIMEOUT); if (ret != 0) KIDS_A10_PRT("DMA timeout.\n"); } pos_buff = UpdatePointer; UpdatePointer = -1; /* Upate the first or the second part of the buffer */ ret = hal_flash_read(PART_FOR_AUDIO, &pos_flash, &data_buff[pos_buff], part_bytes); if (ret != 0) { ret = -1; break; } /* check the end of the file */ if ((pos_flash + part_bytes) > total_size) { ret = HAL_SAI_DMAStop(&hsai_BlockA1); if (ret != 0) { KIDS_A10_PRT("HAL_SAI_DMAStop return failed.\n"); ret = -1; } break; } if (UpdatePointer != -1) { /* Buffer update time is too long compare to the data transfer time */ KIDS_A10_PRT("UpdatePointer error.\n"); ret = -1; break; } } #endif PB_EXIT: ret = aos_mutex_unlock(&sai_mutex); if (ret != 0) { KIDS_A10_PRT("SAI release failed.\n"); } return ret; }
static int at_reset() { int ret = 0; char response[64] = {0}; char *commond = AT_RESET_CMD; if (at._mode != ASYN) { LOGE(MODULE_NAME, "Operation not supported in non asyn mode"); return -1; } at_task_t *tsk = (at_task_t *)aos_malloc(sizeof(at_task_t)); if (NULL == tsk) { LOGE(MODULE_NAME, "tsk buffer allocating failed"); return -1; } if ((ret = aos_sem_new(&tsk->smpr, 0)) != 0) { LOGE(MODULE_NAME, "failed to allocate semaphore"); goto end; } LOGD(MODULE_NAME, "at task created: %d, smpr: %d", (uint32_t)tsk, (uint32_t)&tsk->smpr); tsk->rsp = response; tsk->rsp_offset = 0; tsk->rsp_len = sizeof(response); aos_mutex_lock(&at._mutex, AOS_WAIT_FOREVER); slist_add_tail(&tsk->next, &at.task_l); // uart operation should be inside mutex lock if ((ret = hal_uart_send(&at._uart, (void *)commond, strlen(commond), at._timeout)) != 0) { aos_mutex_unlock(&at._mutex); LOGE(MODULE_NAME, "uart send command failed"); goto end; } LOGD(MODULE_NAME, "Sending command %s", commond); if ((ret = hal_uart_send(&at._uart, (void *)at._send_delimiter, strlen(at._send_delimiter), at._timeout)) != 0) { aos_mutex_unlock(&at._mutex); LOGE(MODULE_NAME, "uart send delimiter failed"); goto end; } LOGD(MODULE_NAME, "Sending delimiter %s", at._send_delimiter); aos_mutex_unlock(&at._mutex); if ((ret = aos_sem_wait(&tsk->smpr, AOS_WAIT_FOREVER)) != 0) { LOGE(MODULE_NAME, "sem_wait failed"); goto end; } LOGD(MODULE_NAME, "sem_wait succeed."); end: aos_mutex_lock(&at._mutex, AOS_WAIT_FOREVER); slist_del(&tsk->next, &at.task_l); aos_mutex_unlock(&at._mutex); if (aos_sem_is_valid(&tsk->smpr)) { aos_sem_free(&tsk->smpr); } if (tsk) { aos_free(tsk); } return ret; }
/** * Example: * AT+ENETRAWSEND=<len> * ><data> * OK * * Send data in 2 stages. These 2 stages must be finished inside * one mutex lock. * 1. Send 'fst' string (first stage); * 2. Receving prompt, usually "<" character; * 3. Send data (second stage) in 'len' length. */ static int at_send_data_2stage(const char *fst, const char *data, uint32_t len, char *rsp, uint32_t rsplen/*, at_send_t t*/) { int ret = 0; if (at._mode != ASYN) { LOGE(MODULE_NAME, "Operation not supported in non asyn mode"); return -1; } if ((ret = at_reset()) != 0){ LOGE(MODULE_NAME, "There is something wrong with atparser and reset fail\n"); return -1; } at_task_t *tsk = (at_task_t *)aos_malloc(sizeof(at_task_t)); if (NULL == tsk) { LOGE(MODULE_NAME, "tsk buffer allocating failed"); return -1; } if ((ret = aos_sem_new(&tsk->smpr, 0)) != 0) { LOGE(MODULE_NAME, "failed to allocate semaphore"); goto end; } LOGD(MODULE_NAME, "at 2stage task created: %d, smpr: %d", (uint32_t)tsk, (uint32_t)&tsk->smpr); tsk->rsp = rsp; tsk->rsp_offset = 0; tsk->rsp_len = rsplen; /* The 2 stages should be inside one mutex lock*/ /* Mutex context begin*/ aos_mutex_lock(&at._mutex, AOS_WAIT_FOREVER); LOGD(MODULE_NAME, "%s: at lock got", __func__); slist_add_tail(&tsk->next, &at.task_l); // uart operation should be inside mutex lock if ((ret = hal_uart_send(&at._uart, (void *)fst, strlen(fst), at._timeout)) != 0) { aos_mutex_unlock(&at._mutex); LOGE(MODULE_NAME, "uart send 2stage prefix failed"); goto end; } LOGD(MODULE_NAME, "Sending 2stage prefix %s", fst); if ((ret = hal_uart_send(&at._uart, (void *)at._send_delimiter, strlen(at._send_delimiter), at._timeout)) != 0) { aos_mutex_unlock(&at._mutex); LOGE(MODULE_NAME, "uart send delimiter failed"); goto end; } LOGD(MODULE_NAME, "Sending delimiter %s", at._send_delimiter); if ((ret = hal_uart_send(&at._uart, (void *)data, len, at._timeout)) != 0) { aos_mutex_unlock(&at._mutex); LOGE(MODULE_NAME, "uart send 2stage data failed"); goto end; } LOGD(MODULE_NAME, "Sending 2stage data %s", data); if ((ret = hal_uart_send(&at._uart, (void *)at._send_delimiter, strlen(at._send_delimiter), at._timeout)) != 0) { aos_mutex_unlock(&at._mutex); LOGE(MODULE_NAME, "uart send delimiter failed"); goto end; } LOGD(MODULE_NAME, "Sending delimiter %s", at._send_delimiter); aos_mutex_unlock(&at._mutex); LOGD(MODULE_NAME, "%s: at lock released", __func__); /* Mutex context end*/ if ((ret = aos_sem_wait(&tsk->smpr, AOS_WAIT_FOREVER)) != 0) { LOGE(MODULE_NAME, "sem_wait failed"); goto end; } LOGD(MODULE_NAME, "sem_wait succeed."); end: aos_mutex_lock(&at._mutex, AOS_WAIT_FOREVER); slist_del(&tsk->next, &at.task_l); aos_mutex_unlock(&at._mutex); if (aos_sem_is_valid(&tsk->smpr)) aos_sem_free(&tsk->smpr); if (tsk) aos_free(tsk); return ret; }