Exemplo n.º 1
0
//------------------------------------------------------------------------------
// Execution starts here:
//------------------------------------------------------------------------------
int
main(int argc, char **argv)
{
    mbus_frame reply;
    mbus_frame_data reply_data;
    mbus_handle *handle = NULL;

    char *host, *addr_str, matching_addr[16], *xml_result;
    int port, address;
 
    memset((void *)&reply, 0, sizeof(mbus_frame));
    memset((void *)&reply_data, 0, sizeof(mbus_frame_data));
     
    if (argc == 4)
    {
        host = argv[1];   
        port = atoi(argv[2]);
        addr_str = argv[3];
        debug = 0;
    }
    else if (argc == 5 && strcmp(argv[1], "-d") == 0)
    {
        host = argv[2];   
        port = atoi(argv[3]);
        addr_str = argv[4];
        debug = 1;
    }
    else
    {
        fprintf(stderr, "usage: %s [-d] host port mbus-address\n", argv[0]);
        fprintf(stderr, "    optional flag -d for debug printout\n");
        return 0;
    }
    
    if (debug)
    {
        mbus_register_send_event(&mbus_dump_send_event);
        mbus_register_recv_event(&mbus_dump_recv_event);
    }
    
    if ((handle = mbus_context_tcp(host, port)) == NULL)
    {
        fprintf(stderr, "Could not initialize M-Bus context: %s\n",  mbus_error_str());
        return 1;
    }

    if (mbus_connect(handle) == -1)
    {
        fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
        return 1;
    }

    if (strlen(addr_str) == 16)
    {
        // secondary addressing

        int ret;

        ret = mbus_select_secondary_address(handle, addr_str);

        if (ret == MBUS_PROBE_COLLISION)
        {
            fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str);
            return 1;
        }
        else if (ret == MBUS_PROBE_NOTHING)
        {
            fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str);
            return 1;
        }
        else if (ret == MBUS_PROBE_ERROR)
        {
            fprintf(stderr, "%s: Error: Failed to select secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str);
            return 1;    
        }
        // else MBUS_PROBE_SINGLE

        if (mbus_send_request_frame(handle, 253) == -1)
        {
            fprintf(stderr, "Failed to send M-Bus request frame.\n");
            return 1;
        }
    } 
    else
    {
        // primary addressing

        address = atoi(addr_str);
        if (mbus_send_request_frame(handle, address) == -1)
        {
            fprintf(stderr, "Failed to send M-Bus request frame.\n");
            return 1;
        }
    }   

    if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
    {
        fprintf(stderr, "Failed to receive M-Bus response frame: %s\n", mbus_error_str());
        return 1;
    }

    //
    // parse data and print in XML format
    //
    if (debug)
    {
        mbus_frame_print(&reply);
    }

    if (mbus_frame_data_parse(&reply, &reply_data) == -1)
    {
        fprintf(stderr, "M-bus data parse error.\n");
        return 1;
    }
    
    if ((xml_result = mbus_frame_data_xml(&reply_data)) == NULL)
    {
        fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str());
        return 1;
    }
    
    printf("%s", xml_result);
    free(xml_result);

    // manual free
    if (reply_data.data_var.record)
    {
        mbus_data_record_free(reply_data.data_var.record); // free's up the whole list
    }

    mbus_disconnect(handle);
    mbus_context_free(handle);
    return 0;
}
//------------------------------------------------------------------------------
// Scan for devices using secondary addressing.
//------------------------------------------------------------------------------
int
main(int argc, char **argv)
{
    mbus_frame reply;
    mbus_frame_data reply_data;
    mbus_handle *handle = NULL;

    char *device, *addr_str, matching_addr[16];
    int address, baudrate = 9600;

    if (argc == 3)
    {
        device   = argv[1];   
        addr_str = argv[2];
    }
    else if (argc == 4 && strcmp(argv[1], "-d") == 0)
    {
        device   = argv[2];   
        addr_str = argv[3];
        debug = 1;
    }
    else if (argc == 5 && strcmp(argv[1], "-b") == 0)
    {
        baudrate = atoi(argv[2]);
        device   = argv[3];   
        addr_str = argv[4];
    }
    else if (argc == 6 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0)
    {
        baudrate = atoi(argv[3]);
        device   = argv[4];   
        addr_str = argv[5];
        debug = 1;
    }
    else 
    {
        fprintf(stderr, "usage: %s [-d] [-b BAUDRATE] device mbus-address\n", argv[0]);
        fprintf(stderr, "    optional flag -d for debug printout\n");
        fprintf(stderr, "    optional flag -b for selecting baudrate\n");
        return 0;
    }
 
    if ((handle = mbus_connect_serial(device)) == NULL)
    {
        fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
        return -1;
    }

    if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
    {
        printf("Failed to set baud rate.\n");
        return -1;
    }


    if (strlen(addr_str) == 16)
    {
        // secondary addressing

        int probe_ret;

        probe_ret = mbus_probe_secondary_address(handle, addr_str, matching_addr);

        if (probe_ret == MBUS_PROBE_COLLISION)
        {
            fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str);
            return -1;
        }
        else if (probe_ret == MBUS_PROBE_NOTHING)
        {
            fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str);
            return -1;
        }
        else if (probe_ret == MBUS_PROBE_ERROR)
        {
            fprintf(stderr, "%s: Error: Failed to probe secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str);
            return -1;    
        }
        // else MBUS_PROBE_SINGLE

        if (mbus_send_request_frame(handle, 253) == -1)
        {
            fprintf(stderr, "Failed to send M-Bus request frame.\n");
            return -1;
        }
    } 
    else
    {
        // primary addressing

        address = atoi(addr_str);
        if (mbus_send_request_frame(handle, address) == -1)
        {
            fprintf(stderr, "Failed to send M-Bus request frame.\n");
            return -1;
        }
    }   

    if (mbus_recv_frame(handle, &reply) == -1)
    {
        fprintf(stderr, "Failed to receive M-Bus response frame.\n");
        return -1;
    }    

    //
    // parse data and print in XML format
    //
    if (debug)
    {
        mbus_frame_print(&reply);
    }
    mbus_frame_data_parse(&reply, &reply_data);
    printf("%s", mbus_frame_data_xml(&reply_data));

    // manual free
    if (reply_data.data_var.record)
    {
        mbus_data_record_free(reply_data.data_var.record); // free's up the whole list
    }

    mbus_disconnect(handle);
    return 0;
}
Exemplo n.º 3
0
int
main(int argc, char *argv[])
{
    FILE *fp = NULL;
    size_t buff_len, len;
    int normalized = 0;
    unsigned char buf[1024];
    mbus_frame reply;
    mbus_frame_data frame_data;
    char *xml_result = NULL, *file = NULL;

    if (argc == 3 && strcmp(argv[1], "-n") == 0)
    {
        file = argv[2];
        normalized = 1;
    }
    else if (argc == 2)
    {
        file = argv[1];
    }
    else
    {
        fprintf(stderr, "usage: %s [-n] binary-file\n", argv[0]);
        fprintf(stderr, "    optional flag -n for normalized values\n");
        return 1;
    }

    if ((fp = fopen(file, "r")) == NULL)
    {
        fprintf(stderr, "%s: failed to open '%s'\n", argv[0], file);
        return 1;
    }

    memset(buf, 0, sizeof(buf));
    len = fread(buf, 1, sizeof(buf), fp);

    if (ferror(fp) != 0)
    {
        fprintf(stderr, "%s: failed to read '%s'\n", argv[0], file);
        return 1;
    }

    fclose(fp);

    memset(&reply, 0, sizeof(reply));
    memset(&frame_data, 0, sizeof(frame_data));
    mbus_parse(&reply, buf, len);
    mbus_frame_data_parse(&reply, &frame_data);
    mbus_frame_print(&reply);

    xml_result = normalized ? mbus_frame_data_xml_normalized(&frame_data) : mbus_frame_data_xml(&frame_data);

    if (xml_result == NULL)
    {
        fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str());
        return 1;
    }
    printf("%s", xml_result);
    free(xml_result);

    return 0;
}
Exemplo n.º 4
0
int
main(int argc, char *argv[])
{
    FILE *fp = NULL;
    size_t buff_len, len;
	int result;
	u_char raw_buff[4096], buff[4096];
	mbus_frame reply;
	mbus_frame_data frame_data;
	char *xml_result = NULL;

	if (argc != 2)
    {
        fprintf(stderr, "usage: %s hex-file\n", argv[0]);
        return 1;
    }
    
    if ((fp = fopen(argv[1], "r")) == NULL)
    {
        fprintf(stderr, "%s: failed to open '%s'\n", argv[0], argv[1]);
        return 1;
    }
    
    memset(raw_buff, 0, sizeof(raw_buff));
    len = fread(raw_buff, 1, sizeof(raw_buff), fp);
    fclose(fp);
    
    buff_len = mbus_hex2bin(buff,sizeof(buff),raw_buff,sizeof(raw_buff));

	memset(&reply, 0, sizeof(reply));
	memset(&frame_data, 0, sizeof(frame_data));
	
	//mbus_parse_set_debug(1);
	
	result = mbus_parse(&reply, buff, buff_len);

	if (result < 0)
	{
	    fprintf(stderr, "mbus_parse: %s\n", mbus_error_str());
        return 1;
    }
    else if (result > 0)
    {
        fprintf(stderr, "mbus_parse: need %d more bytes\n", result);
        return 1;
    }
    
    result = mbus_frame_data_parse(&reply, &frame_data);
	
	if (result != 0)
	{
	    mbus_frame_print(&reply);
	    fprintf(stderr, "mbus_frame_data_parse: %s\n", mbus_error_str());
        return 1;
    }
    
    //mbus_frame_print(&reply);
    //mbus_frame_data_print(&frame_data);
    
    if ((xml_result = mbus_frame_data_xml(&frame_data)) == NULL)
    {
        fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str());
        return 1;
    }
	printf("%s", xml_result);
	free(xml_result);

	
	return 0;
}
Exemplo n.º 5
0
int
main(int argc, char *argv[])
{
    FILE *fp = NULL;
    size_t buff_len, len;
	int result, normalized = 0;
	unsigned char raw_buff[4096], buff[4096];
	mbus_frame reply;
	mbus_frame_data frame_data;
	char *xml_result = NULL, *file = NULL;

	if (argc == 3 && strcmp(argv[1], "-n") == 0)
    {
        file = argv[2];
        normalized = 1;
    }
    else if (argc == 2)
    {
        file = argv[1];
    }
    else
    {
        fprintf(stderr, "usage: %s [-n] hex-file\n", argv[0]);
        fprintf(stderr, "    optional flag -n for normalized values\n");
        return 1;
    }

    if ((fp = fopen(file, "r")) == NULL)
    {
        fprintf(stderr, "%s: failed to open '%s'\n", argv[0], file);
        return 1;
    }

    memset(raw_buff, 0, sizeof(raw_buff));
    len = fread(raw_buff, 1, sizeof(raw_buff), fp);

    if (ferror(fp) != 0)
    {
        fprintf(stderr, "%s: failed to read '%s'\n", argv[0], file);
        return 1;
    }

    fclose(fp);

    buff_len = mbus_hex2bin(buff,sizeof(buff),raw_buff,sizeof(raw_buff));

	memset(&reply, 0, sizeof(reply));
	memset(&frame_data, 0, sizeof(frame_data));

	//mbus_parse_set_debug(1);

	result = mbus_parse(&reply, buff, buff_len);

	if (result < 0)
	{
	    fprintf(stderr, "mbus_parse: %s\n", mbus_error_str());
        return 1;
    }
    else if (result > 0)
    {
        fprintf(stderr, "mbus_parse: need %d more bytes\n", result);
        return 1;
    }

    result = mbus_frame_data_parse(&reply, &frame_data);

	if (result != 0)
	{
	    mbus_frame_print(&reply);
	    fprintf(stderr, "mbus_frame_data_parse: %s\n", mbus_error_str());
        return 1;
    }

    //mbus_frame_print(&reply);
    //mbus_frame_data_print(&frame_data);

    xml_result = normalized ? mbus_frame_data_xml_normalized(&frame_data) : mbus_frame_data_xml(&frame_data);

    if (xml_result == NULL)
    {
        fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str());
        return 1;
    }
	printf("%s", xml_result);
	free(xml_result);
	mbus_data_record_free(frame_data.data_var.record);

	return 0;
}
//------------------------------------------------------------------------------
// client_io:
//
//------------------------------------------------------------------------------
static gboolean
server_io(GIOChannel *gio, GIOCondition condition, gpointer data)
{
    static gchar *func_name = "server_io";
    gchar buff[PACKET_BUFF_SIZE];
    gint len, len_tot = 0;
    int fd;
    FsNetBuffer *nbdata;

    bzero((void *)buff, sizeof(buff));

    mbus_frame *frame;
    mbus_frame_data frame_data;

    //
    // init
    //

    if ((fd = g_io_channel_unix_get_fd(gio)) < 0)
    {
       g_warning("%s: Failed to aquire socket fd.", func_name);
       return FALSE;
    }

    if (condition & (G_IO_HUP|G_IO_NVAL|G_IO_ERR))
    {
        exit(0);
    }

    if ((nbdata  = fs_net_buffer_get(gio)) == NULL)
    {
        g_warning("%s: Serious error, couldn't retrieve data buffer for GIO Channel.", func_name);
        return FALSE;
    }

    //
    // start reading data
    //
    frame = mbus_frame_new(MBUS_FRAME_TYPE_ANY);
    len_tot = 1;
    
    while (len > 0)
    {
        if ((len = fs_net_read_data_nonblocking(gio, nbdata, len_tot)) < 0)
        {
            if (len == -2 && nbdata->offset == 0)
            {
                // Return value -2 is a special return value that we use to indicate
                // that the read operation was interrupted due to unavailability of 
                // data (so we got an try-again error, preventing a blocking read).
                //
                // Restart the read operation later.
                mbus_frame_free(frame);
                return TRUE;
            }        
	
	        if (len == -1)
	        {
                fs_net_buffer_free(gio);
	            fs_net_socket_close(fd);
                exit(0);
            }
        }
        
        nbdata->offset = len_tot; // start next read at the correct offset
        len = mbus_parse(frame, nbdata->buff, len_tot);
       	len_tot += len;
    }
  
    if (len == -1)
    {
        mbus_frame_free(frame);
        return TRUE;
    }
  
    //
    // parse data and print in XML format
    //
    mbus_frame_data_parse(frame, &frame_data);
    printf("%s", mbus_frame_data_xml(&frame_data));
    
        
    //
    // Free and exit
    //
    
    mbus_frame_free(frame);
    exit(0);	

    return TRUE;
}