![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Version FC
Dependencies: DmTftLibrary eeprom SX1280Lib filesystem mbed
Fork of MSNV2-Terminal_V1-5 by
ExtMemory.cpp
- Committer:
- patrick_duc
- Date:
- 2018-10-19
- Branch:
- Integration
- Revision:
- 40:b20720f79e0a
- Parent:
- 39:13e66d087ae9
File content as of revision 40:b20720f79e0a:
/* * MISNet * * ExtMemory: Accès EEPROM * * Created on: August 17, 2018 Author: Francis CHATAIN * */ // ===================================== Includes #include "ExtMemory.hpp" #ifdef TEST_ENVIRONMENT #include "ConcreteService.hpp" #endif #include "Component.hpp" #include "Payload.hpp" using namespace misnet; // ===================================== Methods of class void ExtMemory::read (DataBase* dataBase) { // read eeprom and initialize database //(Example) DEBUG("*** EXTMEM Reading external memory ***\n"); #ifndef TEST_ENVIRONMENT uint8_t componentID=0xff; int16_t servicesNbr=0, servicesTotal=0; int32_t pos=0; ServiceDefinition sd,prev_sd; Header hd; EEPROM ep(I2C_SDA, I2C_SCL, EEPROM_ADDR, EEPROM::T24C04); #endif #ifdef TEST_ENVIRONMENT /* ****************************************************************************** In this example, the terminal holds 3 components : * the first is a BME280 component, containing 3 sensors : * a presence sensor (IPSO code = 102) * a temperature sensor (IPSO code = 103) * a humidity sensor (IPSO code = 104) * the second is a LSM303A component, containing 2 sensors : * an accelerometer sensor (IPSO code = 113) * a barometer sensor (IPSO code = 115) * the third one is a DS18B20 containing an actuator (audio, IPCO code = 139). The terminal is of type IOT_PAYLOAD_3 (why not ?). ****************************************************************************** */ /* ************************************************************************************** Pour Hoel et Francis, de la part de Patrick : je me doute bien que les composants BME280, LSM303A et DS18B20 ne contiennent pas les devices décrits ici, c'est juste un exemple ;-) ************************************************************************************** */ Service::ACCESS_PIN access_pins[6] = {1, 2, 3, 4, 5, 6}; //Value::GENERIC_VALUE generic_value = { 0.0, Value::NOT_SET }; Value delta_threshold, low_threshold_limit, high_threshold_limit; std::vector<Service*> services = std::vector<Service*>(); // Construction du premier composant // --------------------------------- Component* component1 = new Component(Component::BME280, services); // Construction du premier service du premier composant // ---------------------------------------------------- delta_threshold.setUint16Value(100); low_threshold_limit.setUint16Value(10000); high_threshold_limit.setUint16Value(12000); Service* service1 = new ConcreteService(misnet::Service::SENSOR, 102, 1, misnet::Value::UINT16_T, Service::ENABLED, Service::UART_, Service::READ_BY_IRQ, Service::BY_DELTA, access_pins, 300, delta_threshold, low_threshold_limit, high_threshold_limit, Service::MESSAGE, Service::IO, "Premier service du composant BME280 sur cette payload", component1); // Construction du deuxieme service du premier composant // ----------------------------------------------------- delta_threshold.setFloatValue(1.0); low_threshold_limit.setFloatValue(100.0); high_threshold_limit.setFloatValue(110.0); Service* service2 = new ConcreteService(Service::SENSOR, 103, 2, misnet::Value::FLOAT, Service::ENABLED, Service::UART_, Service::READ_BY_IRQ, Service::BY_RANGE, access_pins, 120, delta_threshold, low_threshold_limit, high_threshold_limit, Service::MESSAGE, Service::IO, "Deuxieme service du composant BME280 sur cette payload", component1); // Construction du troisieme service du premier composant // ------------------------------------------------------ Service* service3 = new ConcreteService(Service::SENSOR, 104, 3, misnet::Value::FLOAT, Service::DISABLED, Service::UART_, Service::READ_BY_IRQ, Service::BY_DELTA, access_pins, 30, delta_threshold, low_threshold_limit, high_threshold_limit, Service::MESSAGE, Service::IO, "Troisieme service du composant BME280 sur cette payload", component1); // Insertion des services du premier composant // ------------------------------------------- component1->getServices().push_back(service1); component1->getServices().push_back(service2); component1->getServices().push_back(service3); // Construction du deuxieme composant // ---------------------------------- Component* component2 = new Component(Component::LSM303A, services); // Construction du premier service du deuxieme composant // ----------------------------------------------------- delta_threshold.setInt8Value(2); low_threshold_limit.setInt8Value(100); high_threshold_limit.setInt8Value(110); Service* service4 = new ConcreteService(Service::SENSOR, 113, 4, misnet::Value::INT8_T, Service::ENABLED, Service::UART_, Service::SYNCHRONOUS_READ, Service::BY_DELTA, access_pins, 60, delta_threshold, low_threshold_limit, high_threshold_limit, Service::MESSAGE, Service::IO, "Premier service du composant LSM303A sur cette payload", component2); // Construction du deuxieme service du deuxieme composant // ------------------------------------------------------ Service* service5 = new ConcreteService(Service::SENSOR, 115, 5, misnet::Value::INT8_T, Service::DISABLED, Service::UART_, Service::SYNCHRONOUS_READ, Service::BY_DELTA, access_pins, 80, delta_threshold, low_threshold_limit, high_threshold_limit, Service::MESSAGE, Service::IO, "Deuxieme service du composant LSM303A sur cette payload", component2); // Insertion des services du deuxieme composant // -------------------------------------------- component2->getServices().push_back(service4); component2->getServices().push_back(service5); // Construction du troisieme composant // ----------------------------------- Component* component3 = new Component(Component::DS18B20, services); // Construction du premier service du troisieme composant // ------------------------------------------------------ delta_threshold.setDoubleValue(1.0); low_threshold_limit.setDoubleValue(0.0); high_threshold_limit.setDoubleValue(1.0); Service* service6 = new ConcreteService(Service::ACTUATOR, 139, 6, misnet::Value::DOUBLE, Service::ENABLED, Service::UART_, Service::SYNCHRONOUS_READ, Service::BY_RANGE, access_pins, 50, delta_threshold, low_threshold_limit, high_threshold_limit, Service::MESSAGE, Service::IO, "Premier service du composant DS18B20 sur cette payload", component3); // Insertion des services du troisieme composant // --------------------------------------------- component3->getServices().push_back(service6); // Construction de la payload // -------------------------- std::vector<Component*> components = std::vector<Component*>(); components.push_back(component1); components.push_back(component2); components.push_back(component3); Payload * payload = new Payload(Payload::IOT_PAYLOAD_3, components, 10000); /* Payload sampling information, based on this example --------------------------------------------------- - Payload base period is 10000, which means 176 microseconds * 10000, i.e. 1.76 seconds - Service 1 : subsample rate is 300, so the service sampling period is 528 seconds - Service 2 : subsample rate is 120, so the service sampling period is 211.2 seconds - Service 3 : subsample rate is 30, so the service sampling period is 52.8 seconds - Service 4 : subsample rate is 60, so the service sampling period is 105.6 seconds - Service 5 : subsample rate is 80, so the service sampling period is 140.8 seconds - Service 6 : subsample rate is 50, so the service sampling period is 35.2 seconds - GCD (Greatest Common Divisor) of its services is 10, so the payload will be awaken every 17.6 seconds. */ // Enregistrement de la payload dans la database // --------------------------------------------- dataBase.setPayload(payload); #else dataBase->setRadioParameter ( (uint32_t)2400000000UL, LORA_BW_0400, LORA_SF7, (int8_t)-18, (uint8_t)100, (uint16_t)2000, (uint16_t)2000) ; // ---- read header block ep.read(pos, (void *) &hd, sizeof(hd)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(hd); DEBUG("*** [reading header] services:%d mode:0x%02x payloadID:0x%04x ***\n", hd.nb_of_services, hd.operating_mode, hd.payload_id); servicesTotal = hd.nb_of_services; //dataBase.getNbService() // ---- read services blocks std::vector<Component*> components = std::vector<Component*>(); std::vector<Service*> services = std::vector<Service*>(); Component* component; std::string name[6] = {"HTS221","LPS22HB","LSM303D","LSM6DSL","BME280","DS18B20"}; // debug purpose while(servicesNbr<servicesTotal){ while(ep.read(pos, (void *) &sd, sizeof(sd)),(servicesNbr<servicesTotal)){ pos += sizeof(sd); if (ep.getError() != 0) { error(ep.getErrorMessage()); continue;} // TODO STATE field is missing in ServiceDefinition // TODO access pins // Service::ACCESS_PIN access_pins[6] = { 1, 2, 3, 4, 5, 6 }; Service* service; // = new Service((misnet::Service::SERVICE_TYPE) sd.service_type, // sd.mistnet_code, // Service::ENABLED, // (misnet::Service::ACCESS_TYPE) sd.access_type, // (misnet::Service::REQUEST_MODE) sd.request_mode, // (misnet::Service::UP_MODE) sd.up_mode, // access_pins, // sd.subsample_rate, // (misnet::Service::ACTION) sd.action, // (misnet::Service::OUTPUT_MODE) sd.output, // sd.comment); if( componentID == 255 ) componentID = sd.component_id; if( (sd.component_id != componentID) || (servicesNbr == servicesTotal-1) ){ if(servicesNbr == servicesTotal-1) services.push_back(service); // component = new Component((misnet::Component::COMPONENT_ID) prev_sd.component_id, services); components.push_back(component); DEBUG("push component code %d [%s] to vector (%d services)\n",prev_sd.component_id,name[component->getId()-1].c_str(),services.size()); services.clear(); } services.push_back(service); prev_sd = sd; componentID = sd.component_id; servicesNbr += 1; } services.clear(); DEBUG("\n"); } Payload * payload = new Payload((misnet::Payload::PAYLOAD_ID) hd.payload_id, components, hd.payload_base_period); dataBase->setPayload(payload); #endif } void ExtMemory::populate (void) { int32_t pos=0; ServiceDefinition sd, sd_r; Header hd, hd_r; EEPROM ep(I2C_SDA, I2C_SCL, EEPROM_ADDR, EEPROM::T24C04); // -----------header---------- hd.nb_of_services = 5; hd.nb_of_sensors = 3; hd.operating_mode = 0x09; hd.payload_id = 0x1234; ep.write((uint32_t) (pos), (void *) &hd, sizeof(hd)); if (ep.getError() != 0) error(ep.getErrorMessage()); ep.read((uint32_t) (pos), (void *) &hd_r, sizeof(hd_r)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(hd); printf("*** [writing header] services:%d mode:0x%02x payloadID:0x%04x***\n", hd_r.nb_of_services, hd_r.operating_mode, hd_r.payload_id); // ---------services---------- sd.component_id = Component::DS18B20; sd.service_type = Service::SERVICE_TYPE::SENSOR; sd.mistnet_code = 0x5f; strcpy(sd.comment,"DS1_TEMP"); ep.write((uint32_t) (pos), (void *) &sd, sizeof(sd)); if (ep.getError() != 0) error(ep.getErrorMessage()); ep.read((uint32_t) (pos), (void *) &sd_r, sizeof(sd_r)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(sd); printf("service1 ID:0x%02x type:0x%02x code:0x%04x\n", sd_r.component_id, sd_r.service_type, sd_r.mistnet_code); sd.component_id = Component::LSM303D; sd.service_type = Service::SERVICE_TYPE::SENSOR; sd.mistnet_code = 0x2b; strcpy(sd.comment,"LSM_ACCEL"); ep.write((uint32_t) (pos), (void *) &sd, sizeof(sd)); if (ep.getError() != 0) error(ep.getErrorMessage()); ep.read((uint32_t) (pos), (void *) &sd_r, sizeof(sd_r)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(sd); printf("service2 ID:0x%02x type:0x%02x code:0x%04x\n", sd_r.component_id, sd_r.service_type, sd_r.mistnet_code); sd.component_id = Component::BME280; sd.service_type = Service::SERVICE_TYPE::SENSOR; sd.mistnet_code = 0x4a; strcpy(sd.comment,"BME_TEMP"); ep.write((uint32_t) (pos), (void *) &sd, sizeof(sd)); if (ep.getError() != 0) error(ep.getErrorMessage()); ep.read((uint32_t) (pos), (void *) &sd_r, sizeof(sd_r)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(sd); printf("service3 ID:0x%02x type:0x%02x code:0x%04x\n", sd_r.component_id, sd_r.service_type, sd_r.mistnet_code); sd.component_id = Component::BME280; sd.service_type = Service::SERVICE_TYPE::SENSOR; sd.mistnet_code = 0x4b; strcpy(sd.comment,"BME_HUMI"); ep.write((uint32_t) (pos), (void *) &sd, sizeof(sd)); if (ep.getError() != 0) error(ep.getErrorMessage()); ep.read((uint32_t) (pos), (void *) &sd_r, sizeof(sd_r)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(sd); printf("service4 ID:0x%02x type:0x%02x code:0x%04x\n", sd_r.component_id, sd_r.service_type, sd_r.mistnet_code); sd.component_id = Component::BME280; sd.service_type = Service::SERVICE_TYPE::SENSOR; sd.mistnet_code = 0x4c; strcpy(sd.comment,"BME_BARO"); ep.write((uint32_t) (pos), (void *) &sd, sizeof(sd)); if (ep.getError() != 0) error(ep.getErrorMessage()); ep.read((uint32_t) (pos), (void *) &sd_r, sizeof(sd_r)); if (ep.getError() != 0) error(ep.getErrorMessage()); pos += sizeof(sd); printf("service5 ID:0x%02x type:0x%02x code:0x%04x\n", sd_r.component_id, sd_r.service_type, sd_r.mistnet_code); }