| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #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 CMD_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_ECHO:
- {
- for(uint8_t i=3 ; i<nbus->rx_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 CMD_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;
- case CMD_INFO:
- {
- switch(nbus->rx_buffer[3]){
- case INFO_MODULE_NAME:
- nbus->tx_buffer[4] = MODULE_NAME[0];
- nbus->tx_buffer[5] = MODULE_NAME[1];
- nbus->tx_buffer[6] = MODULE_NAME[2];
- nbus->tx_buffer[7] = MODULE_NAME[3];
- nbus->tx_buffer[8] = MODULE_NAME[4];
- nbus->tx_buffer[9] = MODULE_NAME[5];
- nbus->tx_buffer[10] = MODULE_NAME[6];
- nbus->tx_buffer[11] = MODULE_NAME[7];
- nbus->tx_length += 8;
- break;
- case INFO_MODULE_TYPE:
- nbus->tx_buffer[4] = MODULE_TYPE[0];
- nbus->tx_buffer[5] = MODULE_TYPE[1];
- nbus->tx_buffer[6] = MODULE_TYPE[2];
- nbus->tx_length += 3;
- break;
- case INFO_MODULE_UUID:
- // Reference manual: Unique device ID registers
- uint32_t (*unique_id_3) = (uint32_t*)(0x1FF80064); // BASE address + 0x14 0ffset
- *(nbus->tx_buffer) = (uint32_t)unique_id_3;
- nbus->tx_length += 4;
- break;
- case INFO_MODULE_FW:
- nbus->tx_buffer[4] = VERSION_FW[0];
- nbus->tx_buffer[5] = '.';
- nbus->tx_buffer[6] = VERSION_FW[1];
- nbus->tx_length += 3;
- break;
- case INFO_MODULE_HW:
- nbus->tx_buffer[4] = VERSION_HW[0];
- nbus->tx_buffer[5] = '.';
- nbus->tx_buffer[6] = VERSION_HW[1];
- nbus->tx_length += 3;
- break;
- }
- }
- 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;
- }
|