Radiocraft Wireless M-Bus extension module  2.18
mod_mbus.c File Reference

Wireless M-Bus communication example module. More...

#include <module.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
#include <semaphore.h>

Go to the source code of this file.

Data Structures

struct  mbus_open
 FUNCTION mbusOpen
Defines the data structure of the VPL function mbusOpen. More...
 
struct  mbus_frame
 STRUCT_BLOCK mbusFrame
Defines the data structure of the VPL STRUCT_BLOCK mbusFrame. More...
 
struct  mbus_send
 FUNCTION mbusSend
Defines the data structure of the VPL function mbusSend. More...
 
struct  mbus_filter_receive
 FUNCTION mbusFilterReceive
Defines the data structure of the VPL function mbusFilterReceive. More...
 
struct  mbus_receive
 FUNCTION mbusReceive
Defines the data structure of the VPL function mbusReceive. More...
 
struct  mbus_reg_slave
 FUNCTION mbusRegisterSlave
Defines the data structure of the VPL function mbusRegisterSlave. More...
 
struct  mbus_set_filter
 FUNCTION mbusSetFilter
Defines the data structure of the VPL function mbusSetFilter. More...
 
struct  mbus_start_frame
 The first block of data when a new MBUS message is received. More...
 
struct  mbus_frame_list_elem
 The doubly-connected frame buffer list keeping received frames. More...
 
struct  mbus_frame_list
 The buffer list structure, specifying overwrite policy, buffer size and counters. More...
 
struct  mbus_slave_address
 Slave address entries. More...
 

Macros

#define MBUS_DATA_LENGTH   0xF6
 Maximum size of a MBUS message not including header information. More...
 
#define MAX_SLAVE_REGISTERS   64
 
#define MUTEX_LOCK(m)   pthread_mutex_lock(&m)
 
#define MUTEX_UNLOCK(m)   pthread_mutex_unlock(&m)
 
#define TH_SEM_RESET(s)   while (sem_trywait(&s) == 0)
 

Typedefs

typedef struct mbus_open tdef_mbus_open
 Typedef for mbus_open. More...
 
typedef struct mbus_frame tdef_mbus_frame
 Typedef for mbus_frame. More...
 
typedef struct mbus_send tdef_mbus_send
 Typedef for mbus_send. More...
 
typedef struct mbus_filter_receive tdef_mbus_filter_receive
 Typedef for mbus_filter_receive. More...
 
typedef struct mbus_receive tdef_mbus_receive
 Typedef for mbus_receive. More...
 
typedef struct mbus_reg_slave tdef_mbus_reg_slave
 Typedef for mbus_reg_slave. More...
 
typedef struct mbus_set_filter tdef_mbus_set_filter
 Typedef for mbus_set_filter. More...
 
typedef struct mbus_start_frame tdef_mbus_start_frame
 Typedef for mbus_start_frame. More...
 
typedef struct mbus_frame_list_elem tdef_mbus_frame_list_elem
 Typedef for mbus_frame_list_elem. More...
 
typedef struct mbus_frame_list tdef_mbus_frame_list
 Typedef for mbus_frame_list. More...
 
typedef struct mbus_slave_address tdef_mbus_slave_address
 Typedef for mbus_slave_address. More...
 

Functions

static void flist_append (tdef_mbus_frame_list_elem *e)
 Add element containing received frame data to list. More...
 
static void mbusDebug (const char *prefix, const char *format,...)
 Send mbus debug message to the device output in the format "<prefix>:<formated string>" This function is by default empty, but is active when compiling with DEBUG defined. More...
 
static void mbusPrintInfo (const char *prefix, const char *format,...)
 Send mbus info message to the device output in the format "<prefix>:<formated string>". More...
 
static int8 mbus_address_compare (tdef_mbus_slave_address *a, tdef_mbus_frame *f)
 Compare the address in a received frame to a given address. More...
 
static int mbusPower (uint8 power)
 Control power to module. More...
 
static int mbusConfigEnter (void)
 Entering configuration mode. More...
 
static int mbusConfigExit (void)
 Exit configuration mode. More...
 
static int mbusConfigTransact (uint8 cmd, uint8 *data, int d_size, uint8 *reply, int r_size)
 Send a single configuration command with additional data and receive the reply. More...
 
static int mbusConfigCmd (uint8 cmd, uint8 *reply, int size)
 Send a single command/argument. More...
 
static int mbusConfigData (uint8 cmd)
 Send configuration data. More...
 
static int mbus_read_reg (int8 reg, int size, const char *name)
 Reads the value of one or more registers in the M-Bus module and prints the value to the device output as hexadecimal values. More...
 
static int mbus_read_regs (int8 reg, uint8 *buffer, int size)
 Reads the value of one or more registers in the M-Bus module and stores the value in the provided buffer. More...
 
static int mbus_config_mem_reg (uint8 reg, uint8 val)
 Change the value of a register in the M-Bus module. More...
 
static int mbus_config (int8 mode, int8 rssi, int8 only_installed)
 Configure a number of common settings. More...
 
static int mbus_receive (tdef_mbus_start_frame *start, int8 *data, int16 *rssi)
 Receive a new frame from the M-Bus module. More...
 
static void mbus_th_reader (void *arg)
 Thread function to recieve data and add to frame buffer list. More...
 
static int mbusConfigFactoryReset (void)
 This function performs a factory reset of the RF module. More...
 
static tdef_mbus_frame_list_elemflist_remove_first_element (void)
 Take out the oldest element in the buffer list and return a pointer to it. More...
 
static tdef_mbus_frame_list_elemflist_search_remove (tdef_mbus_slave_address *address)
 Search for and remove element from the buffer list, based on a given slave address, and return a pointer to it. More...
 
static int flist_clear (void)
 Clear the internal buffer list. More...
 
static int mbusBufferReceive (tdef_mbus_frame *rframe, tdef_mbus_slave_address *address, int32 timeout)
 Get element from buffer with timeout. More...
 
static int mbusClose (void)
 Closes the communication interface. More...
 
static int mbusOpen (int8 mode, int8 rssi)
 Opens the connection to the M-Bus module. More...
 
static int mbusSend (int16 c, int8 *data, int8 size)
 Send an M-BUS packet. More...
 
static int mbus_register_slave (int idx, int16 manufacturer, int32 id, int8 version, int8 type, uint8 *key)
 Registers/installs a slave device on the M-Bus module. More...
 
static int mbus_info (void)
 Print some information about the RF module to the device output. More...
 
static int mbusStartReader (uint16 buf_size, int8 overwrite)
 Start the receive thread. More...
 
static int mbusStopReader (void)
 Stop the receive thread and clean up. More...
 
static int mbusGetBufferLevel (void)
 Get the actual level of how full the buffer is in promille. More...
 
static int32 MODDECL mbusCallOpen (HANDLE *pCPU, void *pBase)
 Open communication interface to the module. More...
 
static int32 MODDECL mbusCallClose (HANDLE *pCPU, void *pBase)
 Close communication interface to the module. More...
 
static int32 MODDECL mbusCallFilterReceive (HANDLE *pCPU, void *pBase)
 Check if any MBUS packets has been received for a given filter. More...
 
static int32 MODDECL mbusCallReceive (HANDLE *pCPU, void *pBase)
 Check if any MBUS packets has been received. More...
 
static int32 MODDECL mbusCallSend (HANDLE *pCPU, void *pBase)
 Send a MBUS message. More...
 
static int32 MODDECL mbusCallInfo (HANDLE *pCPU, void *pBase)
 Perform a series of tests to validate communication. More...
 
static int32 MODDECL mbusCallFilterEnable (HANDLE *pCPU, void *pBase)
 Set mode. More...
 
static int32 MODDECL mbusCallRegisterSlave (HANDLE *pCPU, void *pBase)
 Register a slave device. More...
 
static int32 MODDECL mbusCallGetBufferLevel (HANDLE *pCPU, void *pBase)
 Get the actual level of how full the buffer is in promille. More...
 
int moduleInit (void)
 This function is called by the firmware when 'extModuleLoad' is called. More...
 
void moduleNotify (int event, int parm)
 This function is called by the firmware to notify the module about an event. More...
 

Variables

static int fd = -1
 serial port handle More...
 
static int cfg_active = 0
 Flag to monitor if we are in config mode. More...
 
static int cfg_activated = 0
 Flag to monitor if we have received the reply showing the we are in config mode. More...
 
static int rssi_included = 0
 Flag to keep track of whether the RSSI is included in each packet. More...
 
static tdef_mbus_frame_list mbus_flist = {0, 100, 0, 1, NULL, NULL}
 The frame list buffer. More...
 
static tdef_mbus_slave_address mbus_slave_register [MAX_SLAVE_REGISTERS]
 The stored addresses of registered slaves. More...
 
static pthread_mutex_t lock
 thread synchronization lock More...
 
static pthread_mutex_t list_lock
 list synchronization lock More...
 
static pthread_mutex_t rw_lock
 device read/write lock More...
 
static pthread_t reader
 thread configuration More...
 
static int run_thread = 0
 thread running state More...
 
static sem_t read_sem
 semaphore to await data with timeout More...
 

Detailed Description

Wireless M-Bus communication example module.

This shows the framework necessary for communicating with the Radiocraft Wireless M-Bus RC1180-MBUS3 module.

Definition in file mod_mbus.c.

Macro Definition Documentation

◆ MAX_SLAVE_REGISTERS

#define MAX_SLAVE_REGISTERS   64

Definition at line 32 of file mod_mbus.c.

◆ MBUS_DATA_LENGTH

#define MBUS_DATA_LENGTH   0xF6

Maximum size of a MBUS message not including header information.

Definition at line 31 of file mod_mbus.c.

◆ MUTEX_LOCK

#define MUTEX_LOCK (   m)    pthread_mutex_lock(&m)

Definition at line 34 of file mod_mbus.c.

◆ MUTEX_UNLOCK

#define MUTEX_UNLOCK (   m)    pthread_mutex_unlock(&m)

Definition at line 35 of file mod_mbus.c.

◆ TH_SEM_RESET

#define TH_SEM_RESET (   s)    while (sem_trywait(&s) == 0)

Definition at line 36 of file mod_mbus.c.

Typedef Documentation

◆ tdef_mbus_frame_list

Typedef for mbus_frame_list.

Definition at line 1 of file mod_mbus.c.

◆ tdef_mbus_frame_list_elem

Typedef for mbus_frame_list_elem.

Definition at line 1 of file mod_mbus.c.

◆ tdef_mbus_slave_address

Typedef for mbus_slave_address.

Definition at line 1 of file mod_mbus.c.

◆ tdef_mbus_start_frame

Typedef for mbus_start_frame.

Definition at line 1 of file mod_mbus.c.

Variable Documentation

◆ cfg_activated

int cfg_activated = 0
static

Flag to monitor if we have received the reply showing the we are in config mode.

Definition at line 306 of file mod_mbus.c.

Referenced by mbusClose(), mbusConfigEnter(), mbusConfigExit(), mbusConfigFactoryReset(), mbusPower(), and moduleInit().

◆ cfg_active

int cfg_active = 0
static

Flag to monitor if we are in config mode.

Definition at line 304 of file mod_mbus.c.

Referenced by mbus_th_reader(), mbusClose(), mbusConfigEnter(), mbusConfigExit(), mbusConfigFactoryReset(), and moduleInit().

◆ fd

int fd = -1
static

◆ list_lock

pthread_mutex_t list_lock
static

list synchronization lock

Definition at line 319 of file mod_mbus.c.

Referenced by flist_append(), flist_clear(), and flist_search_remove().

◆ lock

◆ mbus_flist

tdef_mbus_frame_list mbus_flist = {0, 100, 0, 1, NULL, NULL}
static

The frame list buffer.

Definition at line 311 of file mod_mbus.c.

Referenced by flist_append(), flist_clear(), flist_remove_first_element(), flist_search_remove(), mbusGetBufferLevel(), and mbusStartReader().

◆ mbus_slave_register

tdef_mbus_slave_address mbus_slave_register[MAX_SLAVE_REGISTERS]
static

The stored addresses of registered slaves.

Definition at line 314 of file mod_mbus.c.

Referenced by mbus_register_slave(), mbusCallFilterReceive(), and moduleInit().

◆ read_sem

sem_t read_sem
static

semaphore to await data with timeout

Definition at line 327 of file mod_mbus.c.

Referenced by flist_append(), mbusBufferReceive(), mbusStartReader(), and mbusStopReader().

◆ reader

pthread_t reader
static

thread configuration

Definition at line 323 of file mod_mbus.c.

Referenced by mbusStartReader().

◆ rssi_included

int rssi_included = 0
static

Flag to keep track of whether the RSSI is included in each packet.

Definition at line 308 of file mod_mbus.c.

Referenced by mbus_config(), mbus_receive(), mbusCallFilterEnable(), mbusCallInfo(), mbusCallRegisterSlave(), and mbusCallSend().

◆ run_thread

int run_thread = 0
static

thread running state

Definition at line 325 of file mod_mbus.c.

Referenced by mbus_th_reader(), mbusBufferReceive(), mbusStartReader(), and mbusStopReader().

◆ rw_lock

pthread_mutex_t rw_lock
static

device read/write lock

Definition at line 321 of file mod_mbus.c.

Referenced by mbus_config(), mbus_info(), mbus_receive(), mbus_register_slave(), mbusClose(), mbusConfigFactoryReset(), mbusOpen(), and mbusSend().