#include "nbus_impl.h" #include "app_bridge.h" inline static void setErrorResponse(nBus_TypeDef *nbus, uint8_t code){ nbus->function_code.error = 1; nbus->tx_buffer[4] = code; nbus->tx_length += 1; } void nbus_unicastToSensorGet(nBus_TypeDef *nbus){ switch(nbus->function_code.function){ case SENSOR_TYPE: { nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address); if (t == TYPE_UNKNOWN) { setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS); break; } nbus->tx_buffer[4] = t; nbus->tx_length += 1; } break; case CMD_PARAM: { if (nbus->rx_length >= 5){ if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]) == 0) { setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED); break; } nbus->tx_buffer[4] = nbus->tx_buffer[3]; nbus->tx_buffer[5] = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]); nbus->tx_length += 2; } else { nBus_param_t* params = nbus_interface_allParams(); for(uint8_t i=0; i < nbus_interface_allParamsCount() ; i++){ if(nbus->interface->hasParam(i, params[i])){ nbus->tx_buffer[4+2*i] = params[i]; nbus->tx_buffer[5+2*i] = nbus->interface->getParam(i, params[i]); nbus->tx_length += 2; } } } } break; case CMD_DATA: { uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]); nbus->tx_length += cnt; } break; default: { setErrorResponse(nbus, ILLEGAL_FUNCTION); } } } void nbus_unicastToSensorSet(nBus_TypeDef *nbus){ switch(nbus->function_code.function){ case CMD_PARAM: { nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]); if (p == PARAM_NONE) { setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED); break; } nbus->tx_buffer[4] = OK_CODE; nbus->tx_length += 1; } break; case CMD_DATA: { nbus->interface->setData(&nbus->rx_buffer[3]); nbus->tx_buffer[4] = OK_CODE; nbus->tx_length += 1; } break; default: { setErrorResponse(nbus, ILLEGAL_FUNCTION); } } } void nbus_unicastToModuleGet(nBus_TypeDef *nbus){ switch(nbus->function_code.function){ case CMD_VERSION: { nbus->tx_buffer[4] = VERSION_MAJOR; nbus->tx_buffer[5] = '.'; nbus->tx_buffer[6] = VERSION_MINOR; nbus->tx_length += 3; } break; case CMD_ECHO: { for(uint8_t i=3 ; irx_length-1 ; i++){ nbus->tx_buffer[i+1] = nbus->rx_buffer[i]; } nbus->tx_length += (nbus->rx_length-4); } break; case CMD_SENSOR_CNT: { nbus->tx_buffer[4] = nbus->interface->getSensorCount(); nbus->tx_length += 1; } break; case SENSOR_TYPE: { //response: sensor1_index:sensor2_type | sensor1_index:sensor2_type | ... for(uint8_t i = 0; i < nbus->interface->getSensorCount() ; i++){ nbus->tx_buffer[4+2*i] = i; nbus->tx_buffer[5+2*i] = nbus->interface->getType(i); nbus->tx_length += 2; } } break; default: { setErrorResponse(nbus, ILLEGAL_FUNCTION); } } } void nbus_unicastToModuleSet(nBus_TypeDef *nbus){ switch(nbus->function_code.function){ case CMD_PARAM: { //same as nbus_unicastToSensorSet nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]); if (p == PARAM_NONE) { setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED); break; } nbus->tx_buffer[4] = OK_CODE; nbus->tx_length += 1; } break; case CMD_DATA: { nbus->interface->setData(&nbus->rx_buffer[3]); nbus->tx_buffer[4] = OK_CODE; nbus->tx_length += 1; } break; case CMD_SLEEP: { nbus->tx_buffer[4] = OK_CODE; nbus->tx_length += 1; } break; case CMD_WAKEUP: { nbus->tx_buffer[4] = OK_CODE; nbus->tx_length += 1; } break; default: { setErrorResponse(nbus, ILLEGAL_FUNCTION); } } } void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){ if(request_type == BROADCAST_SPECIFIC_SENSORS) { } if(request_type == BROADCAST_GLOBAL) { switch(nbus->function_code.function) { #if MODULE_MASTER case CMD_SYNC: { if(nbus->rx_length < 11){ return; } RTC_TimeTypeDef sTime = {0}; RTC_DateTypeDef sDate = {0}; sTime.Hours = nbus->rx_buffer[7]; sTime.Minutes = nbus->rx_buffer[8]; sTime.Seconds = nbus->rx_buffer[9]; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD); sDate.WeekDay = nbus->rx_buffer[6]; sDate.Date = nbus->rx_buffer[5]; sDate.Month = nbus->rx_buffer[4]; sDate.Year = nbus->rx_buffer[3]; HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD); } break; #endif case CMD_START: { nbus->measure_active = MEASURE_RUNNING; } break; case CMD_STOP: { nbus->measure_active = MEASURE_STOPPED; } break; default: ; // nothing } } nbus->send_response = NO_RESPONSE; }