Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

DataBase.hpp

Committer:
patrick_duc
Date:
2018-10-19
Branch:
Integration
Revision:
39:13e66d087ae9
Parent:
38:9b43b2415093

File content as of revision 39:13e66d087ae9:

/*
 * MISNet   
 *
 *  DataBase:   Contient les données du terminal 
 *
 *  Created on: August 17, 2018         Author: Francis CHATAIN
 *
 */

#ifndef __DATABASE_HPP__
#define __DATABASE_HPP__

#include <list>

#include "Context.h"

#ifndef TEST_ENVIRONMENT
#include "mbed.h"
#include "sx1280-hal.h"
#endif


#include "Service.hpp"
#include "Component.hpp"
#include "CandidateComponent.hpp"
#include "Payload.hpp"
#include "ClearMessagePart.hpp"
#include "ControlMessagePart.hpp"
#include "NormalIOTClearMessagePart.hpp"
#include "IOTConfigDataMessagePart.hpp"
#include "IOTValuesDataMessagePart.hpp"


// =======================================  Define 

#define  MODE_STO   0x0000      //  P2P Smart Tool 
#define  MODE_GEN   0x4000      //  P2P Generic
#define  MODE_IOT   0x8000      //  STAR IoT mode 
#define  MODE_BCA   0xC000      //  STAR Broacast mode 


namespace misnet {
    class DataBase;
}

// =======================================  Class content 

class misnet::DataBase {
public:

    enum TERMINAL_TYPE {
        SMART_TOOL = 0,         // Smart Tool
        GENERIC_PEER2PEER = 1,  // Generic tool
        NORMAL_IOT = 2,         // Normal IOT (the terminal is attached to a gateway)
        BROADCAST_IOT = 3       // Broadcast IOT (the terminal is not attached to a gateway)
    };

    DataBase() {}
    virtual ~DataBase() {}

    void init();

    void setTerminalType(TERMINAL_TYPE terminal_type) {
        this->_terminal_type = terminal_type;
    }

    TERMINAL_TYPE getTerminalType() {
        return this->_terminal_type;
    }

    uint16_t getTerminalId() {
        return this->_terminal_id;
    }

    void setTerminalId(uint16_t terminal_id) {
        this->_terminal_id = terminal_id;
    }

    uint8_t getGatewayId() {
        return this->_gateway_id;
    }

    void setGatewayId(uint8_t gateway_id) {
        this->_gateway_id = gateway_id;
    }

    uint8_t getApplicationId() {
        return this->_application_id;
    }

    void setApplicationId(uint8_t application_id) {
        this->_application_id = application_id;
    }

    void setPayloadModuleId(uint16_t payload_module_id) {
        this->_payload_module_id = payload_module_id;
    }

    uint16_t getPayloadModuleId() {
        return this->_payload_module_id;
    }

    void setPayloadHeartbeatPeriod(uint16_t payload_heartbeat_period) {
        this->_payload_heartbeat_period = payload_heartbeat_period;
    }

    void setPlatformHeartbeatPeriod(uint16_t platform_heartbeat_period) {
        this->_platform_heartbeat_period = platform_heartbeat_period;
    }

    void setClearMessagePart();

    misnet::ClearMessagePart* getClearMessagePart() {
        return _clear_message_part;
    }

    void setIOTConfigDataMessagePart();

    misnet::IOTConfigDataMessagePart* getIOTConfigDataMessagePart() {
        return _iot_config_data_message_part;
    }

    void setIOTValuesDataMessagePart(misnet::IOTValuesDataMessagePart* iotValuesDataMessagePart) {
        DEBUG("Setting IOT values data message part to %p\n", iotValuesDataMessagePart);
        _iot_values_data_message_part = iotValuesDataMessagePart;
    }

    misnet::IOTValuesDataMessagePart* getIOTValuesDataMessagePart() {
        return _iot_values_data_message_part;
    }

    void setPendingServices(std::vector<misnet::Service *>& pendingServices) {
        _pending_services = pendingServices;
    }

    std::vector<misnet::Service *>& getPendingServices() {
        return _pending_services;
    }

    short getNbServices(); // return the number of declared channels for the payload 
    short getNbService(misnet::Service::SERVICE_TYPE service); // return the number of declared channels for the payload 
    misnet::Service::SERVICE_TYPE getServiceType(short NumService); // Get Service type 


    void SetSensorState(short channel_rank, misnet::Service::STATE sensorState); // Set Sensor State  (Disable/Enable)  
    misnet::Service::STATE GetSensorState(short channel_rank); // Get Sensor State  (Disable/Enable) know if the device shall requested 

    void SetSensorValue(uint8_t value); // Set Sensor Value + compute value before storage
    uint8_t GetSensorMessage(short channel_rank); // Get Sensor Message  (False si no change no threshold, response formated)

    short getNbActuator();
    void SetActuatorState();
    misnet::Service::STATE GetActuatorState(short actuator_rank);

    void setPayload(misnet::Payload* payload) {
        this->_payload = payload;
    }

    misnet::Payload* getPayload() {
        return this->_payload;
    }

    std::list<misnet::CandidateComponent*> * getCandidateSensorsList();
    std::list<misnet::Service*> * getCandidateServicesList();

    // Initialise database for a standard IOT terminal
    void initNormalIotTerminal(uint16_t terminal_id, uint8_t gateway_id, uint16_t payload_module_id,
            uint16_t platform_heartbeat_period, uint16_t payload_heartbeat_period) {
        this->setTerminalType(misnet::DataBase::NORMAL_IOT);
        this->setTerminalId(terminal_id);
        this->setGatewayId(gateway_id);
        this->setPayloadModuleId(payload_module_id);
        this->setPayloadHeartbeatPeriod(payload_heartbeat_period);
        this->setPlatformHeartbeatPeriod(platform_heartbeat_period);
        this->setClearMessagePart();
        this->setIOTConfigDataMessagePart();
        this->setIOTValuesDataMessagePart(NULL);
    }

    // Initialise database for a broadcast IOT terminal
    void initBroadcastIotTerminal(uint16_t terminal_id, uint8_t application_id, uint16_t payload_module_id,
            uint16_t platform_heartbeat_period, uint16_t payload_heartbeat_period) {
        this->setTerminalType(misnet::DataBase::BROADCAST_IOT);
        this->setTerminalId(terminal_id);
        this->setApplicationId(application_id);
        this->setPayloadModuleId(payload_module_id);
        this->setPayloadHeartbeatPeriod(payload_heartbeat_period);
        this->setPlatformHeartbeatPeriod(platform_heartbeat_period);
        this->setClearMessagePart();
        this->setIOTConfigDataMessagePart();
        this->setIOTValuesDataMessagePart(NULL);
    }

    // =============================================================== Radio Parameter (getter/setter) 


#ifndef TEST_ENVIRONMENT
    void getRadioParameter(uint32_t &rfFrequency,
            RadioLoRaBandwidths_t &loraBw,
            RadioLoRaSpreadingFactors_t &loraSf,
            int8_t &txOutputPower,
            uint8_t &bufferSizeMax,
            int16_t &terminal_heartbeat_period,
            int16_t &payload_heartbeat_period);

    void setRadioParameter(uint32_t rfFrequency,
            RadioLoRaBandwidths_t loraBw,
            RadioLoRaSpreadingFactors_t loraSf,
            int8_t txOutputPower,
            uint8_t ufferSizeMax,
            int16_t terminal_heartbeat_period,
            int16_t payload_heartbeat_period);
#endif


    // Private variables /methods
private:

#ifndef TEST_ENVIRONMENT
    uint32_t _rf_frequency; // HzNominal frequency
    RadioLoRaBandwidths_t _lora_bw; /* 200; 400; 800; 1600 */
    RadioLoRaSpreadingFactors_t _lora_sf; /* SF5; SF6=; SF7; SF8 ; SF9; SF10; SF11 ; SF12 */
    int8_t _tx_output_power; /* Output power in dBm [-18..+13] dBm */
    uint8_t _buffer_size_max; /* Payload size max */
#endif

    // Normal or broadcast IOT terminal
    uint16_t _terminal_id;
    uint8_t _gateway_id;
    uint8_t _application_id;
    uint16_t _payload_module_id;

    // Smart tool
    uint16_t _smart_tool_id;

    // Generic peer-to-peer terminal
    uint16_t _link_id;

    TERMINAL_TYPE _terminal_type;
    uint16_t _platform_heartbeat_period; // Platform heartbeat period
    uint16_t _payload_heartbeat_period; // Sensors' scan period
    misnet::Payload* _payload; // Payload definition
    misnet::ClearMessagePart* _clear_message_part;
    misnet::IOTConfigDataMessagePart* _iot_config_data_message_part;
    misnet::IOTValuesDataMessagePart* _iot_values_data_message_part;

    // Services whose value must be read (a payload heart beat is ongoing)
    // but their value requests an asynchronous read
    std::vector<misnet::Service *> _pending_services;
};

#endif  // __DATABASE_HPP__