87 lines
2.6 KiB
C
87 lines
2.6 KiB
C
#include "nh_ctrl_api.h"
|
|
|
|
#include "esp_hosted_config.pb.h"
|
|
|
|
#define NH_XFER_BUF_SIZE 1600
|
|
|
|
#define NH_SEMAPHORE_EVENT_TIMEOUT 1000
|
|
#define NH_SEMAPHORE_REQ_TIMEOUT 5000
|
|
|
|
nh_ret_t nh_ctrl_api_init(nh_ctrl_api_t *api) {
|
|
nh_ret_t ret = NH_RET_SUCCESS;
|
|
|
|
/* Create semaphore for input events (Data Ready, Ctrl API call) */
|
|
ret = api->osa.sem_create(api->user_data, &api->p_semaphore_event);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
/* Create semaphore for control API requests, we can support maximum 1 control API request at any given time */
|
|
ret = api->osa.sem_create(api->user_data, &api->p_semaphore_request);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
/* Create semaphore for control API responses */
|
|
ret = api->osa.sem_create(api->user_data, &api->p_semaphore_response);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
/* TODO: Determine the initial value of each semaphores. */
|
|
|
|
/* Allocate TX and RX buffers, we should minimalize the malloc-free counts to reduce memory fragmentation */
|
|
ret = api->osa.buf_allocate(api->user_data, &api->p_buf_tx, NH_XFER_BUF_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
ret = api->osa.buf_allocate(api->user_data, &api->p_buf_rx, NH_XFER_BUF_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
return NH_RET_SUCCESS;
|
|
}
|
|
|
|
void nh_ctrl_api_task(nh_ctrl_api_t *api) {
|
|
nh_ret_t ret = NH_RET_SUCCESS;
|
|
|
|
ret = api->osa.sem_take(api->user_data, api->p_semaphore_event, NH_SEMAPHORE_EVENT_TIMEOUT);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
/* Failed to acquire semaphore due to error or timed out */
|
|
return;
|
|
}
|
|
|
|
/* Acquired semaphore, could be an event or a request, or both. */
|
|
}
|
|
|
|
nh_ret_t nh_ctrl_api_get_mac(nh_ctrl_api_t *api, uint8_t *mac) {
|
|
nh_ret_t ret;
|
|
|
|
ret = api->osa.sem_take(api->user_data, api->p_semaphore_request, NH_SEMAPHORE_REQ_TIMEOUT);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
/* Failed to take the request semaphore in time, maybe due to another request in under way */
|
|
return ret;
|
|
}
|
|
|
|
/* TODO: Encode the request */
|
|
CtrlMsg_Req_GetMacAddress req = CtrlMsg_Req_GetMacAddress_init_zero;
|
|
req.mode = Ctrl_WifiMode_STA;
|
|
|
|
api->p_flag_request = true;
|
|
ret = api->osa.sem_give(api->user_data, api->p_semaphore_event);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
goto give_sem_and_exit;
|
|
}
|
|
|
|
ret = api->osa.sem_take(api->user_data, api->p_semaphore_response, NH_SEMAPHORE_REQ_TIMEOUT);
|
|
if(ret != NH_RET_SUCCESS) {
|
|
goto give_sem_and_exit;
|
|
}
|
|
|
|
give_sem_and_exit:
|
|
api->osa.sem_give(api->user_data, api->p_semaphore_request);
|
|
|
|
return ret;
|
|
} |