]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/qemu/mf624_interface/untitled/socket_thread.cpp
44246d9371040dede44cfaf8ae243b0de8fefe46
[mf6xx.git] / src / qemu / mf624_interface / untitled / socket_thread.cpp
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netdb.h>
8 #include <stdio.h>
9 #include "socket_thread.h"
10 #include "mf624_io_status.h"
11 #include "mainwindow.h"
12 #include "ui_mainwindow.h"
13 extern mf624_io_status_t mf624_io_status;
14
15
16 socket_thread::socket_thread(QObject *parent)
17 : QThread(parent)
18 {
19         sockfd = -1;
20 }
21
22 void socket_thread::socket_send(const char* str, int val)
23 {
24         int n;
25         char buffer[256];
26         sprintf(buffer, str, val);
27         //printf(str, val);
28
29         n = ::write(sockfd, buffer, strlen(buffer));
30         if (n < 0) {
31                 perror("write()");
32         }
33
34         //qDebug() << "MF624 in Qemu not runnig?";
35 }
36
37
38 void socket_thread::run()
39 {
40 #define STRING_BUFF_SIZE        256
41         qDebug() << "Executing in new independant thread, GUI is NOT blocked";
42
43         int portno;
44         int n;
45         int status;
46         struct sockaddr_in serv_addr;
47         struct hostent *server;
48         char read_buffer[STRING_BUFF_SIZE];
49         char reg[STRING_BUFF_SIZE+1];
50         int val;
51         char buffer[256];
52
53         //if (argc < 3) {
54         //   fprintf(stderr,"usage %s hostname port\n", argv[0]);
55         //   exit(0);
56         //}
57         //portno = atoi(argv[2]);
58         portno = atoi("55555");
59
60         sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
61         if (sockfd < 0) {
62                 perror("socket()");
63                 exit(0);
64         }
65
66         //server = gethostbyname(argv[1]);
67         server = ::gethostbyname("127.0.0.1");
68         if (server == NULL) {
69                 perror("gethostbyname()");
70                 exit(0);
71         }
72
73         memset((char *) &serv_addr, '\0', sizeof(serv_addr));
74         serv_addr.sin_family = AF_INET;
75         bcopy((char *)server->h_addr,
76              (char *)&serv_addr.sin_addr.s_addr,
77              server->h_length);
78
79         serv_addr.sin_port = htons(portno);
80         if (::connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
81                 perror("connect()");
82         }
83
84
85         while(1) {
86                 memset(buffer, '\0', 256);
87                 n = read(sockfd, buffer, 255);
88                 if (n < 0) {
89                         perror("read()");
90                         sleep(1);
91                 }
92
93                 status = sscanf(buffer, "%[A-Z0-9]=%u", reg, &val);
94                 if (status == 2) {
95                         if(!strcmp(reg, "DOUT")) {
96                                 mf624_io_status.DOUT = val;
97                         }
98                         if(!strcmp(reg, "DA0")) {
99                                 mf624_io_status.DA0 = val;
100                         }
101                         if(!strcmp(reg, "DA1")) {
102                                 mf624_io_status.DA1 = val;
103                         }
104                         if(!strcmp(reg, "DA2")) {
105                                 mf624_io_status.DA2 = val;
106                         }
107                         if(!strcmp(reg, "DA3")) {
108                                 mf624_io_status.DA3 = val;
109                         }
110                         if(!strcmp(reg, "DA4")) {
111                                 mf624_io_status.DA4 = val;
112                         }
113                         if(!strcmp(reg, "DA5")) {
114                                 mf624_io_status.DA5 = val;
115                         }
116                         if(!strcmp(reg, "DA6")) {
117                                 mf624_io_status.DA6 = val;
118                         }
119                         if(!strcmp(reg, "DA7")) {
120                                 mf624_io_status.DA7 = val;
121                         }
122                 }
123
124                 //Setting DOUT
125                 ((MainWindow*) parent())->ui->checkBox_9->setChecked(false);
126                 ((MainWindow*) parent())->ui->checkBox_10->setChecked(false);
127                 ((MainWindow*) parent())->ui->checkBox_11->setChecked(false);
128                 ((MainWindow*) parent())->ui->checkBox_12->setChecked(false);
129                 ((MainWindow*) parent())->ui->checkBox_13->setChecked(false);
130                 ((MainWindow*) parent())->ui->checkBox_14->setChecked(false);
131                 ((MainWindow*) parent())->ui->checkBox_15->setChecked(false);
132                 ((MainWindow*) parent())->ui->checkBox_16->setChecked(false);
133                 if (mf624_io_status.DOUT & (1 << 0)) {
134                         ((MainWindow*) parent())->ui->checkBox_9->setChecked(true);
135                 }
136                 if (mf624_io_status.DOUT & (1 << 1)) {
137                         ((MainWindow*) parent())->ui->checkBox_10->setChecked(true);
138                 }
139                 if (mf624_io_status.DOUT & (1 << 2)) {
140                         ((MainWindow*) parent())->ui->checkBox_11->setChecked(true);
141                 }
142                 if (mf624_io_status.DOUT & (1 << 3)) {
143                         ((MainWindow*) parent())->ui->checkBox_12->setChecked(true);
144                 }
145                 if (mf624_io_status.DOUT & (1 << 4)) {
146                         ((MainWindow*) parent())->ui->checkBox_13->setChecked(true);
147                 }
148                 if (mf624_io_status.DOUT & (1 << 5)) {
149                         ((MainWindow*) parent())->ui->checkBox_14->setChecked(true);
150                 }
151                 if (mf624_io_status.DOUT & (1 << 6)) {
152                         ((MainWindow*) parent())->ui->checkBox_15->setChecked(true);
153                 }
154                 if (mf624_io_status.DOUT & (1 << 7)) {
155                         ((MainWindow*) parent())->ui->checkBox_16->setChecked(true);
156                 }
157
158                 // Setting DAC
159                 ((MainWindow*) parent())->ui->dac0_slider->setValue(mf624_io_status.DA0);
160                 ((MainWindow*) parent())->ui->dac1_slider->setValue(mf624_io_status.DA1);
161                 ((MainWindow*) parent())->ui->dac2_slider->setValue(mf624_io_status.DA2);
162                 ((MainWindow*) parent())->ui->dac3_slider->setValue(mf624_io_status.DA3);
163                 ((MainWindow*) parent())->ui->dac4_slider->setValue(mf624_io_status.DA4);
164                 ((MainWindow*) parent())->ui->dac5_slider->setValue(mf624_io_status.DA5);
165                 ((MainWindow*) parent())->ui->dac6_slider->setValue(mf624_io_status.DA6);
166                 ((MainWindow*) parent())->ui->dac7_slider->setValue(mf624_io_status.DA7);
167
168                 //printf("%s\n", buffer);
169         }
170         //close(sockfd);
171
172         exec();
173 }
174