2012-04-11 10:46:09 +09:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 2011, Robert Johansson, Raditex AB
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// rSCADA
|
|
|
|
// http://www.rSCADA.se
|
|
|
|
// info@rscada.se
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <mbus/mbus.h>
|
|
|
|
|
2012-05-29 21:34:20 +02:00
|
|
|
static int debug = 0;
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Execution starts here:
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2012-09-01 22:14:21 +02:00
|
|
|
char *host, *addr_mask = NULL;
|
2012-04-12 00:38:00 +02:00
|
|
|
int port;
|
2012-05-29 21:34:20 +02:00
|
|
|
mbus_handle *handle = NULL;
|
|
|
|
mbus_frame *frame = NULL, reply;
|
|
|
|
|
|
|
|
memset((void *)&reply, 0, sizeof(mbus_frame));
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
if (argc != 4 && argc != 3)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s host port [address-mask]\n", argv[0]);
|
|
|
|
fprintf(stderr, "\trestrict the search by supplying an optional address mask on the form\n");
|
|
|
|
fprintf(stderr, "\t'FFFFFFFFFFFFFFFF' where F is a wildcard character\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
host = argv[1];
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
if (argc == 4)
|
|
|
|
{
|
|
|
|
addr_mask = strdup(argv[3]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addr_mask = strdup("FFFFFFFFFFFFFFFF");
|
|
|
|
}
|
2012-09-01 22:14:21 +02:00
|
|
|
|
|
|
|
if (addr_mask == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to allocate address mask.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
if (strlen(addr_mask) != 16)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-04 19:49:54 +02:00
|
|
|
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2012-07-04 19:49:54 +02:00
|
|
|
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-09 12:33:32 +02:00
|
|
|
if (mbus_connect(handle) == -1)
|
2012-07-04 19:49:54 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
2012-05-29 21:34:20 +02:00
|
|
|
|
|
|
|
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
|
|
|
|
|
|
|
|
if (frame == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to allocate mbus frame.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// init slaves
|
|
|
|
//
|
|
|
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
2012-06-20 00:23:25 +02:00
|
|
|
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
|
2012-05-29 21:34:20 +02:00
|
|
|
|
|
|
|
if (mbus_send_frame(handle, frame) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to send SND_NKE #1.\n");
|
|
|
|
mbus_frame_free(frame);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) mbus_recv_frame(handle, &reply);
|
|
|
|
|
|
|
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
2012-06-20 00:23:25 +02:00
|
|
|
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
|
2012-05-29 21:34:20 +02:00
|
|
|
|
|
|
|
if (mbus_send_frame(handle, frame) == -1)
|
|
|
|
{
|
2012-09-01 22:14:21 +02:00
|
|
|
free(addr_mask);
|
2012-05-29 21:34:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) mbus_recv_frame(handle, &reply);
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
|
|
|
|
|
|
|
mbus_disconnect(handle);
|
2012-07-04 19:49:54 +02:00
|
|
|
mbus_context_free(handle);
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
//printf("Summary: Tried %ld address masks and found %d devices.\n", probe_count, match_count);
|
|
|
|
|
|
|
|
free(addr_mask);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|