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 <string.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <mbus/mbus.h>
|
|
|
|
|
2013-04-25 21:19:41 +02:00
|
|
|
static int debug = 0;
|
|
|
|
|
|
|
|
int ping_address(mbus_handle *handle, mbus_frame *reply, int address)
|
|
|
|
{
|
|
|
|
int i, ret = MBUS_RECV_RESULT_ERROR;
|
|
|
|
|
|
|
|
memset((void *)reply, 0, sizeof(mbus_frame));
|
|
|
|
|
|
|
|
for (i = 0; i <= handle->max_retry; i++)
|
|
|
|
{
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
printf("%d ", address);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbus_send_ping_frame(handle, address, 0) == -1)
|
|
|
|
{
|
|
|
|
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
|
|
|
|
return MBUS_RECV_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbus_recv_frame(handle, reply);
|
|
|
|
|
|
|
|
if (ret != MBUS_RECV_RESULT_TIMEOUT)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-11 10:46:09 +09:00
|
|
|
//------------------------------------------------------------------------------
|
2013-04-25 21:19:41 +02:00
|
|
|
// Primary addressing scanning of mbus devices.
|
2012-04-11 10:46:09 +09:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
mbus_handle *handle;
|
|
|
|
char *host;
|
2013-04-25 21:19:41 +02:00
|
|
|
int port, address, retries = 0;
|
2012-05-29 21:34:20 +02:00
|
|
|
int ret;
|
2012-04-11 10:46:09 +09:00
|
|
|
|
2012-05-13 16:34:34 +09:00
|
|
|
if (argc == 3)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2012-05-13 16:34:34 +09:00
|
|
|
host = argv[1];
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
else if (argc == 4 && strcmp(argv[1], "-d") == 0)
|
|
|
|
{
|
|
|
|
debug = 1;
|
|
|
|
host = argv[2];
|
|
|
|
port = atoi(argv[3]);
|
|
|
|
}
|
2013-04-25 21:19:41 +02:00
|
|
|
else if (argc == 5 && strcmp(argv[1], "-r") == 0)
|
|
|
|
{
|
|
|
|
retries = atoi(argv[2]);
|
|
|
|
host = argv[3];
|
|
|
|
port = atoi(argv[4]);
|
|
|
|
}
|
|
|
|
else if (argc == 6 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-r") == 0)
|
|
|
|
{
|
|
|
|
debug = 1;
|
|
|
|
retries = atoi(argv[3]);
|
|
|
|
host = argv[4];
|
|
|
|
port = atoi(argv[5]);
|
|
|
|
}
|
2012-05-13 16:34:34 +09:00
|
|
|
else
|
|
|
|
{
|
2013-04-25 21:19:41 +02:00
|
|
|
printf("usage: %s [-d] [-r RETRIES] host port\n", argv[0]);
|
2012-04-11 10:46:09 +09:00
|
|
|
return 0;
|
|
|
|
}
|
2012-05-15 22:57:03 +02:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
mbus_register_send_event(&mbus_dump_send_event);
|
|
|
|
mbus_register_recv_event(&mbus_dump_recv_event);
|
|
|
|
}
|
2012-05-13 16:34:34 +09:00
|
|
|
|
2012-07-04 19:49:54 +02:00
|
|
|
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
|
|
|
{
|
|
|
|
printf("Scan failed: 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-04-11 10:46:09 +09:00
|
|
|
{
|
2013-04-25 21:19:41 +02:00
|
|
|
printf("Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbus_context_set_option(handle, MBUS_OPTION_MAX_RETRY, retries) == -1)
|
|
|
|
{
|
|
|
|
printf("Failed to set retry count\n");
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-05-13 16:34:34 +09:00
|
|
|
if (debug)
|
|
|
|
printf("Scanning primary addresses:\n");
|
2012-05-29 21:34:20 +02:00
|
|
|
|
2013-03-02 18:03:31 +01:00
|
|
|
for (address = 0; address <= MBUS_MAX_PRIMARY_SLAVES; address++)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
|
|
|
mbus_frame reply;
|
|
|
|
|
2013-04-25 21:19:41 +02:00
|
|
|
ret = ping_address(handle, &reply, address);
|
2012-05-29 21:34:20 +02:00
|
|
|
|
2012-07-11 23:33:52 +02:00
|
|
|
if (ret == MBUS_RECV_RESULT_TIMEOUT)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
|
|
|
continue;
|
2012-05-29 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 23:33:52 +02:00
|
|
|
if (ret == MBUS_RECV_RESULT_INVALID)
|
2012-05-29 21:34:20 +02:00
|
|
|
{
|
|
|
|
/* check for more data (collision) */
|
2012-07-09 10:48:13 +02:00
|
|
|
mbus_purge_frames(handle);
|
2012-05-29 21:34:20 +02:00
|
|
|
printf("Collision at address %d\n", address);
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK)
|
|
|
|
{
|
2012-05-29 21:34:20 +02:00
|
|
|
/* check for more data (collision) */
|
2012-07-09 10:48:13 +02:00
|
|
|
if (mbus_purge_frames(handle))
|
2012-05-29 21:34:20 +02:00
|
|
|
{
|
|
|
|
printf("Collision at address %d\n", address);
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-03 14:35:07 +02:00
|
|
|
|
2012-04-11 10:46:09 +09:00
|
|
|
printf("Found a M-Bus device at address %d\n", address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbus_disconnect(handle);
|
2012-07-04 19:49:54 +02:00
|
|
|
mbus_context_free(handle);
|
2012-04-11 10:46:09 +09:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|