void cfgReadConfig(const char *filename) { assert(cfg); filenamesModified[filename] = 0; char *content = cfgReadEntireFile(filename); if(!content) return; cJSON *root = cJSON_Parse(content); if(!root) { printf("Parse error in config file %s: %s\n", filename, cJSON_GetErrorPtr()); return; } cJSON *ns = root->child; while(ns) { if(!strcmp(ns->string, "!includes")) { cJSON *file = ns->child; while(file) { cfgReadConfig(file->valuestring); file = file->next; } } else { cJSON *attr = ns->child; while(attr) { std::string fullname = std::string(ns->string) + "." + std::string(attr->string); if(cfg->keys[fullname]) { printf("%s = %s\n", fullname.c_str(), cJSON_Print(attr)); cfg->keys[fullname]->Parse(attr); } attr = attr->next; } } ns = ns->next; } free(content); }
void cfgUpdate() { extern int frame; if(frame % 5 != 0) return; for(std::map<std::string, time_t>::iterator i = filenamesModified.begin(); i != filenamesModified.end(); i++) { int fd; AioStat stat; if(Succeeded != aioOpen(i->first.c_str(), AioRead, &fd, 0)) continue; if(Succeeded != aioStat(fd, &stat)) continue; if(Succeeded != aioClose(fd)) continue; if(i->second < stat.modified) { cfgReadConfig(i->first.c_str()); filenamesModified[i->first] = stat.modified; } } }
/**@brief Function for application main entry. */ int main(void) { uint32_t err_code; bool erase_bonds; sensor_init(); app_trace_init(); // Initialize. timers_init(); #if SOFTDEVICE_7_1_0 == 1 application_timers_start(); #endif //buttons_leds_init(&erase_bonds); ble_stack_init(); device_manager_init(erase_bonds); gap_params_init(); advertising_init(); services_init(); conn_params_init(); sentek_init(); // Start execution. #if SOFTDEVICE_7_1_0 != 1 application_timers_start(); #endif APPL_LOG("Start Advertising \r\n"); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); //************* Config Section ******************* cfgResetConfig(); cfgReadConfig((void *)&cfg_test, &test, sizeof(test)); test = 4444; cfgWriteConfig(&cfg_test, &test, sizeof(test)); // cfgReadConfig(&cfg_test, &test, sizeof(test)); test = 1000; cfgReadConfig(&cfg_test, &test, sizeof(test)); // cfgReadConfigDirect(&test, &cfg_test, sizeof(test)); cfgReadConfigDirect(&cfg_test, &test, sizeof(test)); // cfgReadConfig(&cfg_test, &test, sizeof(test)); //************* Config Section ******************* initDone = true; // Enter main loop. for (;;) { #if (SOFTDEVICE_7_1_0 == 1) && (SDK_IS_9_0_0_OR_ABOVE == 1) if(!log_seconds()) { power_manage(); } #else log_seconds(); agsensor_update(); power_manage(); #endif if ((sentek_measurement_time_counter >= 250) && (initDone == true)) { sentek_measurement_time_counter = 0; sentek_measurement_loop(); } } }
void ble_agsensor_process_request(uint8_t data_len, uint8_t * p_data, uint16_t op_code, bool read_start, bool app_request, bool write) { APPL_LOG("ble_agsensor_process_request opcode %d \r\n", (int)op_code); if (isConfigSendWaiting == true && write == false && app_request == false) { APPL_LOG("ble_agsensor_process_request 1 \r\n"); return; } // APPL_LOG("ble_agsensor_process_request 2 \r\n"); uint8_t sensor_depth; uint32_t tempData32 = 0; uint16_t tempData16 = 0; // Asked for stop sending the Data for specific opcode if (app_request == true && read_start == false && opcode_inRead == op_code && write == false){ // Set to date time as they count as the ping message opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; APPL_LOG("ble_agsensor_process_request 2 \r\n"); return; } hvx_p_config_len = 0; (hvx_p_config_len) += uint16_encode(op_code, &hvx_encoded_buffer[hvx_p_config_len]); switch (op_code) { case BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME: if (write && data_len >=4 && p_data != NULL) { tempData32 = (((uint32_t)((uint8_t *)p_data)[0]) << 0) | (((uint32_t)((uint8_t *)p_data)[1]) << 8) | (((uint32_t)((uint8_t *)p_data)[2]) << 16) | (((uint32_t)((uint8_t *)p_data)[3]) << 24 ); setOffsetTime(tempData32); APPL_LOG("Date time offset update \r\n"); return; } else { // APPL_LOG("Date time msg created \r\n"); datetimeReadEnable = read_start; tempData32 = getTimeInSec(); // tempData32 = SwapByte4(tempData32); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); } break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_BATTERY_LEVEL: // read only batteryLevelReadEnable = read_start; break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_LONGITUDE_LATITUDE: break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_BONDED_DONGLE_RSSI: // read only bondedDongleRssiReadEnable = read_start; break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_BONDED_DONGLE_MAC_ADDRESS: // read only break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_TIME_SYNC_OFFSET: break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_UPDATE_RATE: break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_DATUM: break; /*Mosture */ case BLE_AGSENSOR_CONFIGURATION_OPCODE_MOSTURE_AIRWATER: if (write && data_len >= 5 && p_data != NULL) { APPL_LOG("Moisture sensor AirWater write request\r\n"); sensor_depth = p_data[0]; // Write the Air value in the config tempData16 = uint16_decode(&p_data[1]); cfgWriteConfig(&moisture_air[sensor_depth], &tempData16, sizeof(tempData16)); // Write the Water value in the config tempData16 = uint16_decode(&p_data[3]); cfgWriteConfig(&moisture_water[sensor_depth], &tempData16, sizeof(tempData16)); // Need to create response msg so application get the response to update the UI. ble_agsensor_create_temp_msg(op_code, p_data, data_len); return; } else { if(read_start) { msg_sensor_depth = MAX_SENSOR_COUNT; APPL_LOG("moisture sensor air Water read request\r\n"); // Job is done so need to return msg will be created on the ACK call back return; } else { msg_sensor_depth--; if (msg_sensor_depth == 0) { opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; } } // Add the sensor count in the sending msg hvx_encoded_buffer[hvx_p_config_len] = msg_sensor_depth; hvx_p_config_len ++; // Add the Air value in the sending msg cfgReadConfig(&moisture_air[msg_sensor_depth], &tempData16, sizeof(tempData16)); (hvx_p_config_len) += uint16_encode(tempData16, &hvx_encoded_buffer[hvx_p_config_len]); // Add the Water value in the sending msg cfgReadConfig(&moisture_water[msg_sensor_depth], &tempData16, sizeof(tempData16)); (hvx_p_config_len) += uint16_encode(tempData16, &hvx_encoded_buffer[hvx_p_config_len]); // If app have requested specific data which is not read start then need to create the temp msg if(app_request == true) { ble_agsensor_create_temp_msg(0, &hvx_encoded_buffer, hvx_p_config_len); } APPL_LOG("moisture sensor Air Water data created %d\r\n", msg_sensor_depth); } break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_MOSTURE_ABCD: if (write && data_len >=17 && p_data != NULL) { APPL_LOG("Moisture sensor ABCD write request\r\n"); sensor_depth = p_data[0]; // Write the A value in the config tempData32 = uint32_decode(&p_data[1]); cfgWriteConfig(&moisture_A[sensor_depth], &tempData32, sizeof(tempData32)); // Write the B value in the config tempData32 = uint32_decode(&p_data[5]); cfgWriteConfig(&moisture_B[sensor_depth], &tempData32, sizeof(tempData32)); // Write the B value in the config tempData32 = uint32_decode(&p_data[9]); cfgWriteConfig(&moisture_C[sensor_depth], &tempData32, sizeof(tempData32)); // Write the B value in the config tempData32 = uint32_decode(&p_data[13]); cfgWriteConfig(&moisture_D[sensor_depth], &tempData32, sizeof(tempData32)); // Need to create response msg so application get the response to update the UI. ble_agsensor_create_temp_msg(op_code, p_data, data_len); return; } else { if(read_start) { msg_sensor_depth = MAX_SENSOR_COUNT; APPL_LOG("moisture sensor ABCD read request\r\n"); // Job is done so need to return msg will be created on the ACK call back return; } else { msg_sensor_depth--; if (msg_sensor_depth == 0) { opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; } } // Add the sensor count in the sending msg hvx_encoded_buffer[hvx_p_config_len] = msg_sensor_depth; hvx_p_config_len ++; // Add the A value in the sending msg cfgReadConfig(&moisture_A[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // Add the B value in the sending msg cfgReadConfig(&moisture_B[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // Add the C value in the sending msg cfgReadConfig(&moisture_C[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // Add the D value in the sending msg cfgReadConfig(&moisture_D[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // If app have requested specific data which is not read start then need to create the temp msg if(app_request == true) { ble_agsensor_create_temp_msg(0, &hvx_encoded_buffer, hvx_p_config_len); } APPL_LOG("moisture sensor ABCD data created %d\r\n", msg_sensor_depth); } break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_TEMPERATURE_AB: if (write && data_len >=9 && p_data != NULL) { APPL_LOG("temperature sensor AB write request\r\n"); sensor_depth = p_data[0]; // Write the A value in the config tempData32 = uint32_decode(&p_data[1]); cfgWriteConfig(&temperature_A[sensor_depth], &tempData32, sizeof(tempData32)); // Write the B value in the config tempData32 = uint32_decode(&p_data[5]); cfgWriteConfig(&temperature_B[sensor_depth], &tempData32, sizeof(tempData32)); // Need to create response msg so application get the response to update the UI. ble_agsensor_create_temp_msg(op_code, p_data, data_len); return; } else { if(read_start) { msg_sensor_depth = MAX_SENSOR_COUNT; APPL_LOG("temperature sensor AB read request\r\n"); // Job is done so need to return msg will be created on the ACK call back return; } else { msg_sensor_depth--; if (msg_sensor_depth == 0) { opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; } } // Add the sensor count in the sending msg hvx_encoded_buffer[hvx_p_config_len] = msg_sensor_depth; hvx_p_config_len ++; // Add the A value in the sending msg cfgReadConfig(&temperature_A[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // Add the B value in the sending msg cfgReadConfig(&temperature_B[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // If app have requested specific data which is not read start then need to create the temp msg if(app_request == true) { ble_agsensor_create_temp_msg(0, &hvx_encoded_buffer, hvx_p_config_len); } APPL_LOG("temperature sensor AB data created %d\r\n", msg_sensor_depth); } break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_SALINITY_AIRWATER: if (write && data_len >=5 && p_data != NULL) { APPL_LOG("Salinity sensor AirWater write request\r\n"); sensor_depth = p_data[0]; // Write the Air value in the config tempData16 = uint16_decode(&p_data[1]); cfgWriteConfig(&salinity_air[sensor_depth], &tempData16, sizeof(tempData16)); // Write the Water value in the config tempData16 = uint16_decode(&p_data[3]); cfgWriteConfig(&salinity_water[sensor_depth], &tempData16, sizeof(tempData16)); // Need to create response msg so application get the response to update the UI. ble_agsensor_create_temp_msg(op_code, p_data, data_len); return; } else { if(read_start) { msg_sensor_depth = MAX_SENSOR_COUNT; APPL_LOG("Salinity sensor AirWater read request\r\n"); // Job is done so need to return msg will be created on the ACK call back return; } else { msg_sensor_depth--; if (msg_sensor_depth == 0) { opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; } } // Add the sensor count in the sending msg hvx_encoded_buffer[hvx_p_config_len] = msg_sensor_depth; hvx_p_config_len ++; // Add the Air value in the sending msg cfgReadConfig(&salinity_air[msg_sensor_depth], &tempData16, sizeof(tempData16)); (hvx_p_config_len) += uint16_encode(tempData16, &hvx_encoded_buffer[hvx_p_config_len]); // Add the Water value in the sending msg cfgReadConfig(&salinity_water[msg_sensor_depth], &tempData16, sizeof(tempData16)); (hvx_p_config_len) += uint16_encode(tempData16, &hvx_encoded_buffer[hvx_p_config_len]); // If app have requested specific data which is not read start then need to create the temp msg if(app_request == true) { ble_agsensor_create_temp_msg(0, &hvx_encoded_buffer, hvx_p_config_len); } APPL_LOG("Salinity sensor AirWater data created %d\r\n", msg_sensor_depth); } break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_SALINITY_D: if (write && data_len >=5 && p_data != NULL) { APPL_LOG("Salinity sensor D write request\r\n"); sensor_depth = p_data[0]; // Write the D value in the config tempData32 = uint32_decode(&p_data[1]); cfgWriteConfig(&salinity_D[sensor_depth], &tempData32, sizeof(tempData32)); // Need to create response msg so application get the response to update the UI. ble_agsensor_create_temp_msg(op_code, p_data, data_len); return; } else { if(read_start) { msg_sensor_depth = MAX_SENSOR_COUNT; APPL_LOG("Salinity sensor D read request\r\n"); // Job is done so need to return msg will be created on the ACK call back return; } else { msg_sensor_depth--; if (msg_sensor_depth == 0) { opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; } } // Add the sensor count in the sending msg hvx_encoded_buffer[hvx_p_config_len] = msg_sensor_depth; hvx_p_config_len ++; // Add the D value in the sending msg cfgReadConfig(&salinity_D[msg_sensor_depth], &tempData32, sizeof(tempData32)); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); // If app have requested specific data which is not read start then need to create the temp msg if(app_request == true) { ble_agsensor_create_temp_msg(0, &hvx_encoded_buffer, hvx_p_config_len); } APPL_LOG("Salinity sensor D data created %d\r\n", msg_sensor_depth); } break; case BLE_AGSENSOR_CONFIGURATION_OPCODE_START_MEASUREMENT: isMeasuringSensor = true; sentek_sensor_measurement_type_set(SENSOR_MEASUREMENT_CONTINUOUS); APPL_LOG("Start Measurement\r\n"); return; case BLE_AGSENSOR_CONFIGURATION_OPCODE_STOP_MEASUREMENT: isMeasuringSensor = false; sentek_sensor_measurement_type_set(SENSOR_MEASUREMENT_NONE); APPL_LOG("Stop doing Measurement\r\n"); return; case BLE_AGSENSOR_CONFIGURATION_OPCODE_AIR_NORMALIZE: sentek_sensor_measurement_type_set(SENSOR_MEASUREMENT_AIR_NORMALIZATION); airNormalizationInProgress = true; APPL_LOG("Air Normalization Request\r\n"); return; case BLE_AGSENSOR_CONFIGURATION_OPCODE_WATER_NORMALIZE: sentek_sensor_measurement_type_set(SENSOR_MEASUREMENT_WATER_NORMALIZATION); waterNormalizationInProgress = true; APPL_LOG("Water Normalization Request\r\n"); return; default: APPL_LOG("Default msg created %d\r\n", (int)op_code); datetimeReadEnable = read_start; opcode_inRead = BLE_AGSENSOR_CONFIGURATION_OPCODE_DATETIME; tempData32 = getTimeInSec(); // tempData32 = SwapByte4(tempData32); (hvx_p_config_len) += uint32_encode(tempData32, &hvx_encoded_buffer[hvx_p_config_len]); break; } if (isConnected == true && configIndicationEnable == true) { isConfigSendWaiting = true; return; } // APPL_LOG("read_request 6 \r\n"); return; }