1 // *********************************************************************
    2 //   IQRF standards header file                                        *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: IQRFstandard.h,v $
    7 // Version: $Revision: 1.31 $
    8 // Date:    $Date: 2019/06/14 14:55:28 $
    9 //
   10 // Revision history:
   11 //   2019/06/12  Release for DPA 4.03
   12 //   2019/06/03  Release for DPA 4.02
   13 //   2019/03/07  Release for DPA 4.01
   14 //   2019/01/20  Release for DPA 4.00
   15 //   2018/10/25  Release for DPA 3.03
   16 //   2017/11/16  Release for DPA 3.02
   17 //   2017/08/14  Release for DPA 3.01
   18 //
   19 // *********************************************************************
   20 
   21 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   22 // IQRF Standards documentation https://www.iqrfalliance.org/techDocs/
   23 
   24 #ifndef _IQRFSTD_HEADER_
   25 #define _IQRFSTD_HEADER_
   26 
   27 //############################################################################################
   28 
   29 // Enumerate command valid for all standards
   30 #define PCMD_STD_ENUMERATE                      0x3E
   31 
   32 // -------------------------------------------------------------------------------------------
   33 // IQRF Sensors standard
   34 // -------------------------------------------------------------------------------------------
   35 // IQRF Sensors standard peripheral
   36 #define PNUM_STD_SENSORS                        0x5E
   37 // IQRF Sensors standard peripheral type
   38 #define PERIPHERAL_TYPE_STD_SENSORS             0x5E
   39 
   40 // IQRF Sensors standard peripheral - read sensor values command
   41 #define PCMD_STD_SENSORS_READ_VALUES            0x00
   42 // IQRF Sensors standard peripheral - read sensor types and values command
   43 #define PCMD_STD_SENSORS_READ_TYPES_AND_VALUES  0x01
   44 
   45 // IQRF Sensors standard peripheral - sensor types
   46 //  2 bytes
   47 #define STD_SENSOR_TYPE_TEMPERATURE             0x01
   48 #define STD_SENSOR_TYPE_CO2                     0x02
   49 #define STD_SENSOR_TYPE_VOC                     0x03
   50 #define STD_SENSOR_TYPE_EXTRA_LOW_VOLTAGE       0x04
   51 #define STD_SENSOR_TYPE_EARTHS_MAGNETIC_FIELD   0x05
   52 #define STD_SENSOR_TYPE_LOW_VOLTAGE             0x06
   53 #define STD_SENSOR_TYPE_CURRENT                 0x07
   54 #define STD_SENSOR_TYPE_POWER                   0x08
   55 #define STD_SENSOR_TYPE_MAINS_FREQUENCY         0x09
   56 #define STD_SENSOR_TYPE_TIMESPAN                0x0A
   57 #define STD_SENSOR_TYPE_ILLUMINANCE             0x0B
   58 #define STD_SENSOR_TYPE_NO2                     0x0C
   59 #define STD_SENSOR_TYPE_SO2                     0x0D
   60 #define STD_SENSOR_TYPE_CO                      0x0E
   61 #define STD_SENSOR_TYPE_O3                      0x0F
   62 #define STD_SENSOR_TYPE_ATMOSPHERIC_PRESSURE    0x10
   63 #define STD_SENSOR_TYPE_COLOR_TEMPERATURE       0x11
   64 #define STD_SENSOR_TYPE_PARTICULATES_PM25       0x12
   65 #define STD_SENSOR_TYPE_SOUND_PRESSURE_LEVEL    0x13
   66 #define STD_SENSOR_TYPE_ALTITUDE                0x14
   67 #define STD_SENSOR_TYPE_ACCELERATION            0x15
   68 #define STD_SENSOR_TYPE_NH3                     0x16
   69 #define STD_SENSOR_TYPE_METHANE                 0x17
   70 
   71 //  1 byte
   72 #define STD_SENSOR_TYPE_HUMIDITY                0x80
   73 #define STD_SENSOR_TYPE_BINARYDATA7             0x81
   74 #define STD_SENSOR_TYPE_POWER_FACTOR            0x82
   75 #define STD_SENSOR_TYPE_UV_INDEX                0x83
   76 #define STD_SENSOR_TYPE_PH                      0x84
   77 
   78 //  4 bytes
   79 #define STD_SENSOR_TYPE_BINARYDATA30            0xA0
   80 #define STD_SENSOR_TYPE_CONSUMPTION             0xA1
   81 #define STD_SENSOR_TYPE_DATETIME                0xA2
   82 #define STD_SENSOR_TYPE_TIMESPAN_LONG           0xA3
   83 #define STD_SENSOR_TYPE_LATITUDE                0xA4
   84 #define STD_SENSOR_TYPE_LONGITUDE               0xA5
   85 
   86 // Computes constant (otherwise not optimal) quantity size
   87 #define STD_SENSOR_TYPE_SIZE(q) ( ( ( (q) & 0x80 ) == 0x00 ) * 2 + ( ( (q) & 0xE0 ) == 0x80 ) * 1 + ( ( (q) & 0xE0 ) == 0xA0 ) * 4  + ( ( q & 0xC0 ) == 0xC0 || (q) == 0x00 ) * 0xFfFfFfFf /* hopefully causes overflow to signal error */ )
   88 
   89 //  Multiple bytes
   90 #define STD_SENSOR_TYPE_DATA_BLOCK              0xC0
   91 
   92 // IQRF Sensors standard peripheral - FRC commands
   93 #define FRC_STD_SENSORS_BIT                     0x10
   94 #define FRC_STD_SENSORS_1B                      0x90
   95 #define FRC_STD_SENSORS_2B                      0xE0
   96 #define FRC_STD_SENSORS_4B                      0xF9
   97 
   98 typedef struct
   99 {
  100   // Equals to PNUM_STD_SENSORS
  101   uns8  Header;
  102   // Sensor type STD_SENSOR_TYPE_???
  103   uns8  SensorType;
  104   // Sensor index (bits 0-4) + optional extended data (bits 5-7)
  105   uns8  SensorIndex;
  106   // Options
  107   // bit  0  : sleep
  108   // bits 1-7: reserved
  109   uns8  Options;
  110   // Optional sleep parameters
  111   TPerOSSleep_Request SleepParameters;
  112 } STRUCTATTR TPerStdSensorFrc;
  113 
  114 
  115 #ifdef __CC5X__
  116 // The above structure located at memory
  117 TPerStdSensorFrc PerStdSensorFrc @ DataOutBeforeResponseFRC;
  118 #endif  // #ifdef __CC5X__
  119 
  120 // Structure for PCMD_STD_SENSORS_READ[_TYPES_AND]_VALUES
  121 typedef struct
  122 {
  123   uns8  Bitmap[4];
  124   uns8  WrittenData[DPA_MAX_DATA_LENGTH - 4];
  125 } STRUCTATTR TPerStdSensorRead_Request;
  126 
  127 // IQRF Sensors standard peripheral - error values
  128 //  2 bytes
  129 #define STD_SENSOR_TYPE_TEMPERATURE_SET_ERROR(v)            v = 0x8000;
  130 #define STD_SENSOR_TYPE_TEMPERATURE_IS_ERROR(v)             ( (v) == 0x8000 )
  131 
  132 #define STD_SENSOR_TYPE_CO2_SET_ERROR(v)                    v = 0x8000;
  133 #define STD_SENSOR_TYPE_CO2_IS_ERROR(v)                     ( (v) == 0x8000 )
  134 
  135 #define STD_SENSOR_TYPE_VOC_SET_ERROR(v)                    v = 0x8000;
  136 #define STD_SENSOR_TYPE_VOC_IS_ERROR(v)                     ( (v) == 0x8000 )
  137 
  138 #define STD_SENSOR_TYPE_EXTRA_LOW_VOLTAGE_SET_ERROR(v)      v = 0x8000;
  139 #define STD_SENSOR_TYPE_EXTRA_LOW_VOLTAGE_IS_ERROR(v)       ( (v) == 0x8000 )
  140 
  141 #define STD_SENSOR_TYPE_EARTHS_MAGNETIC_FIELD_SET_ERROR(v)  v = 0x8000;
  142 #define STD_SENSOR_TYPE_EARTHS_MAGNETIC_FIELD_IS_ERROR(v)   ( (v) == 0x8000 )
  143 
  144 #define STD_SENSOR_TYPE_LOW_VOLTAGE_SET_ERROR(v)            v = 0x8000;
  145 #define STD_SENSOR_TYPE_LOW_VOLTAGE_IS_ERROR(v)             ( (v) == 0x8000 )
  146 
  147 #define STD_SENSOR_TYPE_CURRENT_SET_ERROR(v)                v = 0x8000;
  148 #define STD_SENSOR_TYPE_CURRENT_IS_ERROR(v)                 ( (v) == 0x8000 )
  149 
  150 #define STD_SENSOR_TYPE_POWER_SET_ERROR(v)                  v = 0xFfFf;
  151 #define STD_SENSOR_TYPE_POWER_IS_ERROR(v)                   ( (v) == 0xFfFf )
  152 
  153 #define STD_SENSOR_TYPE_MAINS_FREQUENCY_SET_ERROR(v)        v = 0xFfFf;
  154 #define STD_SENSOR_TYPE_MAINS_FREQUENCY_IS_ERROR(v)         ( (v) == 0xFfFf )
  155 
  156 #define STD_SENSOR_TYPE_TIMESPAN_SET_ERROR(v)               v = 0xFfFf;
  157 #define STD_SENSOR_TYPE_TIMESPAN_IS_ERROR(v)                ( (v) == 0xFfFf )
  158 
  159 #define STD_SENSOR_TYPE_ILLUMINANCE_SET_ERROR(v)            v = 0xFfFf;
  160 #define STD_SENSOR_TYPE_ILLUMINANCE_IS_ERROR(v)             ( (v) == 0xFfFf )
  161 
  162 #define STD_SENSOR_TYPE_NO2_SET_ERROR(v)                    v = 0xFfFf;
  163 #define STD_SENSOR_TYPE_NO2_IS_ERROR(v)                     ( (v) == 0xFfFf )
  164 
  165 #define STD_SENSOR_TYPE_SO2_SET_ERROR(v)                    v = 0xFfFf;
  166 #define STD_SENSOR_TYPE_SO2_IS_ERROR(v)                     ( (v) == 0xFfFf )
  167 
  168 #define STD_SENSOR_TYPE_CO_SET_ERROR(v)                     v = 0xFfFf;
  169 #define STD_SENSOR_TYPE_CO_IS_ERROR(v)                      ( (v) == 0xFfFf )
  170 
  171 #define STD_SENSOR_TYPE_O3_SET_ERROR(v)                     v = 0xFfFf;
  172 #define STD_SENSOR_TYPE_O3_IS_ERROR(v)                      ( (v) == 0xFfFf )
  173 
  174 #define STD_SENSOR_TYPE_ATMOSPHERIC_PRESSURE_SET_ERROR(v)   v = 0xFfFf;
  175 #define STD_SENSOR_TYPE_ATMOSPHERIC_PRESSURE_IS_ERROR(v)    ( (v) == 0xFfFf )
  176 
  177 #define STD_SENSOR_TYPE_COLOR_TEMPERATURE_SET_ERROR(v)      v = 0x8000;
  178 #define STD_SENSOR_TYPE_COLOR_TEMPERATURE_IS_ERROR(v)       ( (v) == 0x8000 )
  179 
  180 #define STD_SENSOR_TYPE_PARTICULATES_PM25_SET_ERROR(v)      v = 0x8000;
  181 #define STD_SENSOR_TYPE_PARTICULATES_PM25_IS_ERROR(v)       ( (v) == 0x8000 )
  182 
  183 #define STD_SENSOR_TYPE_SOUND_PRESSURE_LEVEL_SET_ERROR(v)   v = 0x8000;
  184 #define STD_SENSOR_TYPE_SOUND_PRESSURE_LEVEL_IS_ERROR(v)    ( (v) == 0x8000 )
  185 
  186 #define STD_SENSOR_TYPE_ALTITUDE_SET_ERROR(v)               v = 0xFfFf;
  187 #define STD_SENSOR_TYPE_ALTITUDE_IS_ERROR(v)                ( (v) == 0xFfFf )
  188 
  189 #define STD_SENSOR_TYPE_ACCELERATION_SET_ERROR(v)           v = 0x8000;
  190 #define STD_SENSOR_TYPE_ACCELERATION_IS_ERROR(v)            ( (v) == 0x8000 )
  191 
  192 #define STD_SENSOR_TYPE_NH3_SET_ERROR(v)                    v = 0xFfFf;
  193 #define STD_SENSOR_TYPE_NH3_IS_ERROR(v)                     ( (v) == 0xFfFf )
  194 
  195 
  196 //  1 byte
  197 #define STD_SENSOR_TYPE_HUMIDITY_SET_ERROR(v)               v = 0xEE;
  198 #define STD_SENSOR_TYPE_HUMIDITY_IS_ERROR(v)                ( (v) == 0xEE )
  199 
  200 #define STD_SENSOR_TYPE_BINARYDATA7_SET_ERROR(v)            v = 0x80;
  201 #define STD_SENSOR_TYPE_BINARYDATA7_IS_ERROR(v)             ( ( (v) & 0x80 ) != 0x00 )
  202 
  203 #define STD_SENSOR_TYPE_POWER_FACTOR_SET_ERROR(v)           v = 0xEE;
  204 #define STD_SENSOR_TYPE_POWER_FACTOR_IS_ERROR(v)            ( (v) == 0xEE )
  205 
  206 #define STD_SENSOR_TYPE_UV_INDEX_SET_ERROR(v)               v = 0xFF;
  207 #define STD_SENSOR_TYPE_UV_INDEX_IS_ERROR(v)                ( (v) == 0xFF )
  208 
  209 #define STD_SENSOR_TYPE_PH_INDEX_SET_ERROR(v)               v = 0xFF;
  210 #define STD_SENSOR_TYPE_PH_INDEX_IS_ERROR(v)                ( (v) == 0xFF )
  211 
  212 
  213 //  4 bytes
  214 #define STD_SENSOR_TYPE_BINARYDATA30_SET_ERROR(v)           v = 0x80000000;
  215 #define STD_SENSOR_TYPE_BINARYDATA30_IS_ERROR(v)            ( ( (v) & 0x80000000 ) != 0x00000000 )
  216 
  217 #define STD_SENSOR_TYPE_CONSUMPTION_SET_ERROR(v)            v = 0xFfFfFfFf;
  218 #define STD_SENSOR_TYPE_CONSUMPTION_IS_ERROR(v)             ( (v) == 0xFfFfFfFf )
  219 
  220 #define STD_SENSOR_TYPE_DATETIME_SET_ERROR(v)               v = 0xFfFfFfFf;
  221 #define STD_SENSOR_TYPE_DATETIME_IS_ERROR(v)                ( (v) == 0xFfFfFfFf )
  222 
  223 #define STD_SENSOR_TYPE_TIMESPAN_LONG_SET_ERROR(v)          v = 0xFfFfFfFf;
  224 #define STD_SENSOR_TYPE_TIMESPAN_LONG_IS_ERROR(v)           ( (v) == 0xFfFfFfFf )
  225 
  226 #define STD_SENSOR_TYPE_LATITUDE_SET_ERROR(v)               v = 0xFF000000;
  227 #define STD_SENSOR_TYPE_LATITUDE_IS_ERROR(v)                ( ( (v) & 0xFF000000 ) == 0xFF000000 )
  228 
  229 #define STD_SENSOR_TYPE_LONGITUDE_SET_ERROR(v)              v = 0xFF000000;
  230 #define STD_SENSOR_TYPE_LONGITUDE_IS_ERROR(v)               ( ( (v) & 0xFF000000 ) == 0xFF000000 )
  231 
  232 
  233 // -------------------------------------------------------------------------------------------
  234 // IQRF Binary Outputs standard
  235 // -------------------------------------------------------------------------------------------
  236 // IQRF Binary Outputs standard peripheral
  237 #define PNUM_STD_BINARY_OUTPUTS                 0x4B
  238 // IQRF Binary Outputs peripheral type
  239 #define PERIPHERAL_TYPE_STD_BINARY_OUTPUTS      0x4B
  240 
  241 // IQRF Binary Outputs standard peripheral - set outputs
  242 #define PCMD_STD_BINARY_OUTPUTS_SET             0x00
  243 
  244 // Structure returned by PCMD_STD_ENUMERATE
  245 typedef struct
  246 {
  247   uns8  Count;
  248 } STRUCTATTR TPerStdBinaryOutputEnumerate_Response;
  249 
  250 // Structure for PCMD_STD_BINARY_OUTPUTS_SET
  251 typedef struct
  252 {
  253   uns8  Bitmap[4];
  254   uns8  NewState[DPA_MAX_DATA_LENGTH - 4];
  255 } STRUCTATTR TPerStdBinaryOutputSet_Request;
  256 
  257 #define STD_BINARY_OUTPUT_STATE_OFF                    0x00
  258 #define STD_BINARY_OUTPUT_STATE_ON                     0x01
  259 
  260 #define STD_BINARY_OUTPUT_IS_STATE_OK(state)           ( (state) != 0x80 )
  261 #define STD_BINARY_OUTPUT_IS_STATE_OFF(state)          ( (state) == STD_BINARY_OUTPUT_STATE_OFF )
  262 #define STD_BINARY_OUTPUT_IS_STATE_ON(state)           ( (state) >= STD_BINARY_OUTPUT_STATE_ON )
  263 #define STD_BINARY_OUTPUT_STATE_HAS_TIME(state)        ( (state) > STD_BINARY_OUTPUT_STATE_ON )
  264 #define STD_BINARY_OUTPUT_STATE_TIME_IN_SECONDS(state) ( ( (state) & 0x80 ) != 0x00 )
  265 #define STD_BINARY_OUTPUT_STATE_GET_SECONDS(state)     ( (state) & 0x7F )
  266 #define STD_BINARY_OUTPUT_STATE_TIME_IN_MINUTES(state) ( (state) > STD_BINARY_OUTPUT_STATE_ON && (state) <= 0x7F )
  267 #define STD_BINARY_OUTPUT_STATE_GET_MINUTES(state)     (state)
  268 
  269 // Structure returned by PCMD_STD_BINARY_OUTPUTS_SET
  270 typedef struct
  271 {
  272   uns8  PreviousStates[4];
  273 } STRUCTATTR TPerStdBinaryOutputSet_Response;
  274 
  275 
  276 // -------------------------------------------------------------------------------------------
  277 // IQRF Light standard
  278 // -------------------------------------------------------------------------------------------
  279 // IQRF Light standard peripheral
  280 #define PNUM_STD_LIGHT                          0x71
  281 // IQRF Light standard peripheral type
  282 #define PERIPHERAL_TYPE_STD_LIGHT               0x71
  283 
  284 // IQRF Light standard peripheral - Set Power
  285 #define PCMD_STD_LIGHT_SET                      0x00
  286 // IQRF Light standard peripheral - Increment Power
  287 #define PCMD_STD_LIGHT_INC                      0x01
  288 // IQRF Light standard peripheral - Decrement Power
  289 #define PCMD_STD_LIGHT_DEC                      0x02
  290 
  291 // IQRF Light standard peripheral - FRC commands
  292 #define FRC_STD_LIGHT_ONOFF                     0x10
  293 #define FRC_STD_LIGHT_ALARM                     0x11
  294 
  295 // Structure returned by PCMD_STD_ENUMERATE
  296 typedef struct
  297 {
  298   uns8  Count;
  299 } STRUCTATTR TPerStdLightEnumerate_Response;
  300 
  301 // Structure for PCMD_STD_LIGHT_???
  302 typedef struct
  303 {
  304   uns8  Bitmap[4];
  305   uns8  Power[DPA_MAX_DATA_LENGTH - 4];
  306 } STRUCTATTR TPerStdLightSetIncDec_Request;
  307 
  308 #define STD_LIGHT_POWER_MIN                 0
  309 #define STD_LIGHT_POWER_MAX                 100
  310 #define STD_LIGHT_KEEP_POWER                127
  311 #define STD_LIGHT_IS_POWER_OK(power)        ( ( ( (power) & 0x7F ) <= STD_LIGHT_POWER_MAX ) || ( (power) & 0x7F ) == STD_LIGHT_KEEP_POWER )
  312 #define STD_LIGHT_TIME_FOLLOWS_POWER(power) ( ( (power) & 0x80 ) != 0x00 )
  313 
  314 #define STD_LIGHT_IS_TIME_OK(time)          ( ( (time) & 0x7F ) != 0x00 )
  315 #define STD_LIGHT_TIME_IN_SECONDS(time)     ( ( (time) & 0x80 ) != 0x00 )
  316 #define STD_LIGHT_TIME_GET_SECONDS(time)    ( (time) & 0x7F )
  317 #define STD_LIGHT_TIME_IN_MINUTES(time)     ( !STD_LIGHT_TIME_IN_SECONDS( time ) )
  318 #define STD_LIGHT_TIME_GET_MINUTES(time)    (time)
  319 
  320 // -------------------------------------------------------------------------------------------
  321 // IQRF DALI standard
  322 // -------------------------------------------------------------------------------------------
  323 // IQRF DALI standard peripheral
  324 #define PNUM_STD_DALI                           0x4A
  325 // IQRF DALI standard peripheral type
  326 #define PERIPHERAL_TYPE_STD_DALI                0x4A
  327 
  328 // IQRF DALI standard peripheral - Send DALI commands
  329 #define PCMD_STD_DALI_SEND_COMMANDS             0x00
  330 // IQRF DALI standard peripheral - Send DALI commands asynchronously
  331 #define PCMD_STD_DALI_SEND_COMMANDS_ASYNC       0x01
  332 
  333 // IQRF DALI standard peripheral - FRC commands
  334 #define FRC_STD_DALI_SEND                       0xE0
  335 
  336 // DALI answer byte #0
  337 typedef struct 
  338 {
  339   bit Answer;
  340   bit AnswerError;
  341   bit _bit2_reserved;
  342   bit _bit3_reserved;
  343   bit _bit4_reserved;
  344   bit _bit5_reserved;
  345   bit _bit6_reserved;
  346   bit Always1;
  347 } TSTD_DALI_Answer0;
  348 
  349 // -------------------------------------------------------------------------------------------
  350 
  351 
  352 // DPA Message data structure (packet w/o NADR, PNUM, PCMD, HWPID) for IQRF standards
  353 typedef union
  354 {
  355   // Structure for PCMD_STD_SENSORS_READ[_TYPES_AND]_VALUES
  356   TPerStdSensorRead_Request PerStdSensorRead_Request;
  357 
  358   // Structure returned by PCMD_STD_ENUMERATE for binary output
  359   TPerStdBinaryOutputEnumerate_Response PerStdBinaryOutputEnumerate_Response;
  360 
  361   // Structure for PCMD_STD_BINARY_OUTPUTS_SET
  362   TPerStdBinaryOutputSet_Request PerStdBinaryOutputSet_Request;
  363 
  364   // Structure returned by PCMD_STD_BINARY_OUTPUTS_SET
  365   TPerStdBinaryOutputSet_Response PerStdBinaryOutputSet_Response;
  366 
  367   // Structure returned by PCMD_STD_ENUMERATE for light
  368   TPerStdLightEnumerate_Response PerStdLightEnumerate_Response;
  369 } TDpaMessageIqrfStd;
  370 
  371 #ifdef __CC5X__
  372 // The structure above mapped at the DpaMessage
  373 TDpaMessageIqrfStd  _DpaMessageIqrfStd @ DpaRfMessage;
  374 #endif  // #ifdef __CC5X__
  375 
  376 // -------------------------------------------------------------------------------------------
  377 
  378 #endif  // _IQRFSTD_HEADER_
  379 
  380 //############################################################################################