OpenCores
URL https://opencores.org/ocsvn/utosnet/utosnet/trunk

Subversion Repositories utosnet

[/] [utosnet/] [trunk/] [software/] [uTosNet_cmd/] [uTosNet_cmd.cpp] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sonicwave
/*
2
* uTosNet commandline utility
3
 
4
* uTosNet_cmd.cpp
5
* File created by:
6
*       Simon Falsig
7
*       University of Southern Denmark
8
*       Copyright 2010
9
*
10
* This file is part of the uTosnet commandline utility
11
*
12
*       The uTosnet commandline utility is free software: you can redistribute it
13
*       and/or modify it under the terms of the GNU Lesser General Public License as
14
*       published by the Free Software Foundation, either version 3 of the License,
15
*       or (at your option) any later version.
16
*
17
*       The uTosnet commandline utility is distributed in the hope that it will be
18
*       useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
20
*       General Public License for more details.
21
*
22
*       You should have received a copy of the GNU Lesser General Public License
23
*       along with the uTosnet commandline utility. If not, see
24
*       <http://www.gnu.org/licenses/>.
25
*/
26
 
27
 
28
#include "uTosNet_cmd.h"
29
 
30
#include <iostream>
31
 
32
using namespace std;
33
 
34
uTosNetCmdClass::uTosNetCmdClass()
35
{}
36
 
37
uTosNetCmdClass::~uTosNetCmdClass()
38
{}
39
 
40
void uTosNetCmdClass::run(int argc, char *argv[])
41
{
42
        int *sendBuffer;
43
        int *readBuffer;
44
 
45
        sendBuffer = new int[2];
46
        readBuffer = new int[1];
47
        sendBuffer[0] = 0;
48
        readBuffer[0] = 0;
49
 
50
        bool expectAnswer = false;
51
 
52
        if(argc >= 4)
53
        {
54
                bool convOk = false;
55
                int data = 0, size = 0, addr;
56
 
57
                addr = QString(argv[3]).toInt(&convOk, 16);                     //Read in the address to access
58
                if(!convOk)
59
                {
60
                        cout << "Invalid address specified: " << argv[3] << endl;
61
                        return;
62
                }
63
 
64
                switch(argv[2][0])                                                                       //Construct the packet depending on whether it is read or a write
65
                {
66
                        case 'w':
67
                        case 'W':
68
                                if(argc == 5)                                                                           //If data is specified,
69
                                {
70
                                        data = QString(argv[4]).toInt(&convOk, 16);             // - read it in
71
                                        if(!convOk)
72
                                        {
73
                                                cout << "Invalid data specified: " << argv[4] << endl;
74
                                                return;
75
                                        }
76
                                }
77
                                else
78
                                {
79
                                        cout << "No data specified for write operation!" << endl;
80
                                        return;
81
                                }
82
                                sendBuffer[1] = data;
83
                                ((char*)sendBuffer)[3] = 0;
84
                                ((char*)sendBuffer)[2] = 0;
85
                                ((char*)sendBuffer)[1] = (1<<3) + (1<<2) + (addr >> 8);
86
                                ((char*)sendBuffer)[0] = addr & 0xff;
87
                                size = 8;
88
                                break;
89
                        case 'r':
90
                        case 'R':
91
                                ((char*)sendBuffer)[3] = (1<<3) + (1<<2) + (addr >> 8);
92
                                ((char*)sendBuffer)[2] = addr & 0xff;
93
                                ((char*)sendBuffer)[1] = 0;
94
                                ((char*)sendBuffer)[0] = 0;
95
                                expectAnswer = true;
96
                                size = 4;
97
                                break;
98
                        default:
99
                                break;
100
                }
101
 
102
                socket.connectToHost(argv[1], 50000);
103
 
104
                if(socket.waitForConnected(10000))                      //Connect to specified host, times out if not connected within 10 seconds
105
                {
106
                        socket.write((char*)sendBuffer, size);  //Send data to host
107
 
108
                        if(expectAnswer)
109
                        {
110
                                if(socket.waitForReadyRead(10000))      //Wait for answer from host, times out if no data received within 10 seconds
111
                                {
112
                                        socket.read((char*)readBuffer, 4);
113
                                        cout << (readBuffer[0] & 0xffff) << endl;
114
                                }
115
                        }
116
                }
117
                else
118
                {
119
                        cout << "Could not connect to host: " << argv[1] << endl;
120
                }
121
        }
122
        else
123
        {
124
                cout << "uTosNet commandline utility" << endl <<
125
                                "Usage: utosnet_cmd host {W|R} address [data]" << endl << endl <<
126
                                "  host     The IP address of the host to connect to" << endl <<
127
                                "  W        Perform a write operation" << endl <<
128
                                "  R        Perform a read operation" << endl <<
129
                                "  address  The shared memory address to access" << endl <<
130
                                "  data     The data to write (only required when performing a write)" << endl << endl;
131
        }
132
 
133
        return;
134
}
135
 
136
int main(int argc, char *argv[])                        //Starts the application
137
{
138
        QApplication app(argc, argv);
139
 
140
        uTosNetCmdClass uTosNetCmd;
141
 
142
        uTosNetCmd.currentApp = &app;
143
 
144
        uTosNetCmd.run(argc, argv);
145
 
146
        return 0;
147
}
148
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.