C-API help

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
maros
Posts: 103
Joined: 05 Apr 2014 11:21

C-API help

Post by maros »

Hi,
I'm currently experimenting with the z-way C-API and somehow cannot manage to get any meaningful events. Since I'm a C-novice a little push into the right direction is very much appreciated :-) Basically I'm trying to get callbacks for key-push events from my z-wave.me FOB (deviceID=3,instance=0). I picked command class 45 (SceneController) and configured all group actions on the FOB to "send scenes".

Code: Select all

#include <stdio.h>
#include <ZWayLib.h>
#include <errno.h>
#include <signal.h>

typedef int bool;
enum { false, true };

volatile sig_atomic_t stop;

void dataChangeCallback(ZWay aZWay, ZWDataChangeType aType, ZDataHolder aData, void * apArg) {
    printf("Got callback\n");
}

void inthand(int signum) {
    stop = 1;
}

int main(int argc, char ** argv) {
      ZWay aZWay;
      ZWError result;
      int mDeviceId;

      memset(&aZWay, 0, sizeof(aZWay));
      result = zway_init(&aZWay, "/dev/ttyAMA0",
                "/opt/z-way-server/config",
                "/opt/z-way-server/translations",
                "/opt/z-way-server/ZDDX", stdout, Warning);

      if (result == NoError) {
           result = zway_start(aZWay,NULL);
           if (result == NoError) {
                printf("Discovering...\n");
                zway_data_acquire_lock(aZWay);
                for (mDeviceId = 0; mDeviceId <= 4; mDeviceId++) {
                    printf("Trying device %i",mDeviceId);
                    ZDataHolder dataHolder = zway_find_device_instance_cc_data(aZWay, mDeviceId, 0, 45, "");
                    if (dataHolder != NULL) {
                        zway_data_add_callback_ex(aZWay, dataHolder, &dataChangeCallback, TRUE, "");
                    } else {
                        printf("No data holder for deviceId=%i\n",mDeviceId);
                    }
                }
                zway_data_release_lock(aZWay);
                signal(SIGINT, inthand);
                while (!stop) pause();

                printf("Stopping ZWay...\n");
                result = zway_stop(aZWay);
           }
      }
      if (result != NoError) {
           printf("Error: %d\n", result);
      }
      zway_terminate(&aZWay);
      return 0;
 }
When i run this program and push some FOB-buttons i get the following output

Code: Select all

Discovering...
Trying device 0No data holder for deviceId=0
Trying device 1No data holder for deviceId=1
Trying device 2No data holder for deviceId=2
Trying device 3No data holder for deviceId=3
Trying device 4No data holder for deviceId=4
[2014-04-05 10:41:38.177] RECEIVED UNKNOWN PACKET TYPE: 0x00
[2014-04-05 10:41:38.188] RECEIVED UNKNOWN PACKET TYPE: 0x07
^CStopping ZWay...
[2014-04-05 10:41:42.908] SaveData will not save data since it wasn't loaded. This is to prevent data loss.
make: *** [run] Interrupt
Cheers
Maroš
Z-Way 2.2.4 on Raspi B | My Zway GitHub repos | Amazon Wishlist to support module development
Vincentoo
Posts: 18
Joined: 29 Aug 2013 09:57

Re: C-API help

Post by Vincentoo »

Hi,

I have been doing some similar experiments with the C API. I posted a few examples (using binary sensors) on my blog at http://yetanotherdeveloperblog.blogspot.be/.
The key is to figure out the right arguments (device ID, instance number, command class and path). Best check the ZWay logs while generating these events. That will give you all the information you need.

V
maros
Posts: 103
Joined: 05 Apr 2014 11:21

Re: C-API help

Post by maros »

Yes I know your blog and it has been a great help to understand how to create a callback (and also a big chunk of this program was copied from your blog). It would be great if you could publish some working examples at github. Mines are at https://github.com/maros/Homely/ (however at the current state this repo does not contain a single bit of z-wave code, but this should be added in the next couple of weeks)

I looked at the log as you recommended and it actually turned out that the events are called on the razberry (device=1) and the basic command class (32). Seems a little bit strange to me (especially the use of the generic command-class) but so be it.

Cheers
Maros
Z-Way 2.2.4 on Raspi B | My Zway GitHub repos | Amazon Wishlist to support module development
User avatar
PoltoS
Posts: 7624
Joined: 26 Jan 2011 19:36

Re: C-API help

Post by PoltoS »

SceneActivation CC is not supported by the Key Fob, but controlled. Don't mind what it means, but for you: it means it is not created until a command comes from the Fob. And this created Command Class (as not supported) was not saved. Now this was changed - we now save them too. This is changed in our developer tree and will become available in the next release.

Please check next changelog.
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: C-API help

Post by pofs »

zway_find_device_instance_cc_data() returns NULL because device tree is not rendered yet after zway_start().

You need to call zway_discover() after zway_start() to fetch controller information and devices list. Z-Way will load your saved device configuration only after successful discovery.
maros
Posts: 103
Joined: 05 Apr 2014 11:21

Re: C-API help

Post by maros »

thank you for your help so far, but unfortunately i still can't get any event callbacks from the zway api. Zway always fails with returnvalue -9 during discovery.

That's my code
https://github.com/maros/Homely/blob/master/play/test.c
And that's what i get on stdout:
https://github.com/maros/Homely/blob/ma ... ay/run.log

Even if i choose to ignore the returnvalue of zway_discover i don't get any data holders from zway_find_device_instance_cc_data (i have tried to call this function with multiple *path params such as "", "1.level", "level", "mylevel", ... ) and thus cannon add any callbacks

And idea what might be wrong here?

Cheers
Maroš
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: C-API help

Post by pofs »

You should stop z-way-server before running your custom program.
There's a known bug (scheduled to be fixed in the next version) that device is not opened in exclusive mode, so both your program and z-way-server struggle for data from device, and both read junk and corrupted packets.
Post Reply