Skip to content

Coding examples

Dusuniot edited this page Aug 2, 2019 · 3 revisions

ZigBee Example

This section shows how to use the provided APIs to communicate with a ZigBee sensor. Please follow the steps to complete a program:

  1. Init the sdk by calling rbsdk_init(NULL, NULL);
  2. Write corresponding callback functions, and fill them in a stDevMsgCb struct which is used by rbsdk_dev_msgcb_set to set the callback functions.
  3. Write functions which call the send data API functions to send data to sensors.

When the callback functions are invoked, the data is reported from the sensors. For example, the rpt_door function can print the door status reported. If your sensor report data using a customized ZigBee cluster or customized data format, you should write the rpt_cmd and rpt_attr callback functions in which you can get the sensor data to be processed.

The code below shows how to use the provided APIs to get a Dusun door contact sensor data.

#include <stdio.h>
#include <stdlib.h>

#include "rbsdk.h"

int rpt_dev_added(char *mac, char *ModelStr, char *model, int type, int battery) {
	printf("[%s] - %s added\n", __func__, mac);
	return 0;
}
int rpt_dev_deled(char *mac) {
	printf("[%s] - %s deleted\n", __func__, mac);
	return 0;
}
int rpt_dev_online(char *mac, int online) {
	printf("[%s] - %s online : %d\n", __func__, mac, online);
	return 0;
}
int rpt_door(char *mac, int door) {
	printf("[%s] - %s door status:%d\n", __func__, mac, door);
	return 0;
}

int main(int argc, char *argv[]) {

rbsdk_init(NULL, NULL);
char sver[32];
rbsdk_vers(sver);
printf("rbsdk version : %s\n", sver);

stDevMsgCb_t dmc = {
	.rpt_dev_added			= rpt_dev_added, 
	.rpt_dev_deled			= rpt_dev_deled,
	.rpt_dev_online			= rpt_dev_online,
	.rpt_dev_battery		= NULL,
	.rpt_dev_lowpower		= NULL,
	.rpt_temp				= NULL,
	.rpt_humi				= NULL,
	.rpt_door				= rpt_door,
	.rpt_pir				= NULL,
	.rpt_leak				= NULL,
	.ret_lock_password_add	= NULL,
	.ret_lock_password_del	= NULL,
	.ret_lock_password_mod	= NULL,
	.ret_lock_password_clr	= NULL,
	.ret_lock_dynamic_seed	= NLL,
	.rpt_lock_checkrecord	= NULL,
	.rpt_lock_system_locked	= NULL,
	.rpt_smoke				= NULL,
	.rpt_light_onoff		= NULL,
	.rpt_winctr_status		= NULL,
	.rpt_winctr_percent		= NULL,
	.rpt_cmd                = NULL,
	.rpt_attr               = NULL,
};

rbsdk_dev_msgcb_set(&dmc);
while (1) {
	sleep (1);
}
return 0;
}

Z-Wave Example

This section shows how to use the provided APIs to communicate with a Z-Wave sensor. Please follow the steps to complete a program:

  1. Init the sdk by calling rbsdk_init(NULL, NULL);
  2. Write corresponding callback functions, and fill them in a stZwMsgCb_t struct which is used by rbsdk_zw_msgcb_set to set the callback functions. If your sensor report data using a customized ZigBee cluster or customized data format, you should write the rpt_cmd and rpt_attr callback functions in which you can get the sensor data to be processed.
  3. Write functions which call the send data API functions to send data to sensors. The rbsdk_zw_class_cmd function can be used to send messages or command to Z-Wave devices.

When the callback functions are invoked, the data is reported from the sensors. For example, the rpt_zwdev_cmd function can print the command details reported by a Z-Wave device. Cmd.c (in the code files) includes several command functions which show how to use the API functions. When the test program is running, if we type “include” in the console, the do_cmd_include function will be invoked and then the API function rbsdk_zw_include will be called to allow a Z-Wave device to pair with the Gateway.

The code below shows how to use the provided APIs to set or get a light switch Z-Wave contact sensor data.

#include <stdio.h>
#include <stdlib.h>
#include "rbsdk.h"
#include "simlog.h"

int rpt_cmd(char *mac, int ep, int cluster, int cmd, char *buf, int len) {
	log_info("[%s] - ep:%02X, cluster:%04X, cmd:%02X, buf:", mac, ep&0xff, cluster&0xffff, cmd&0xff);
	int i = 0;
	for (i = 0; i < len; i++) {
		printf("[%02X] ", buf[i]&0xff);
	}
	printf("\n");
	return 0;
}
int rpt_attr(char *mac, int ep, int cluster, int attr, char *buf, int len) {
	log_info("[%s] - ep:%02X, cluster:%04X, attr:%04X, buf:", mac, ep&0xff, cluster&0xffff, attr&0xffff);
	int i = 0;
	for (i = 0; i < len; i++) {
		printf("[%02X] ", buf[i]&0xff);
	}
	printf("\n");
	return 0;
}

int rpt_zwdev_added(char *mac, const char *epstr) {
	log_info("[%s] - epstr:%s", mac, epstr);
	return 0;
}

int rpt_zwdev_deled(char *mac) {
	log_info("[%s] - ", mac);
	return 0;
}

int rpt_zwdev_online(char *mac, int online) {
	log_info("[%s] - online:%d", mac, online);
	return 0;
}

int rpt_zwdev_cmd(char *mac, int ep, int class, int cmd, char *buf, int len) {
	log_info("[%s] - ep:%d, class:%02X, cmd:%02X", mac, ep&0xff, class&0xff, cmd&0xff);
	log_debug_hex("buf:", buf, len);
	return 0;
}

int main(int argc, char *argv[]) {

rbsdk_init(NULL, NULL);
char sver[32];
rbsdk_vers(sver);
log_info("rbsdk version : %s\n", sver);

stZwMsgCb_t zmc = {
	.rpt_zwdev_added = rpt_zwdev_added,
	.rpt_zwdev_deled = rpt_zwdev_deled,
	.rpt_zwdev_online= rpt_zwdev_online,
	.rpt_zwdev_cmd	 = rpt_zwdev_cmd,	
};
rbsdk_zw_msgcb_set(&zmc);

while (1) {
	fd_set	fds;
	FD_ZERO(&fds);
	FD_SET(0, &fds);

	struct timeval	tv;
	tv.tv_sec = 4;
	tv.tv_usec = 80;
	int ret = select(0 + 1, &fds, NULL, NULL, &tv);
	if (ret <= 0) {
		continue;
	}
	extern void cmd_in(void *arg, int fd);
	cmd_in(NULL, 0);
}
return 0;
}

Clone this wiki locally