display.cpp 25.4 KB
Newer Older
D
Du Rendong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <map>

int Open_Port(char* comport)
{
    char port_tmp[20];
    sprintf(port_tmp, comport, comport);	

    int fd=0;
D
Du Rendong 已提交
18 19
    // O_NOCTTY If path refers to a terminal device, do not allocate the 
    // device as the controlling terminal for this process
D
Du Rendong 已提交
20
    // can use O_NONBLOCK instead of O_NDELAY
D
Du Rendong 已提交
21
    // fd=open(port_tmp,O_RDWR|O_NOCTTY|O_NDELAY);
D
Du Rendong 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    fd=open(port_tmp,O_RDWR);
    if(fd==-1){
        perror("Can not open serial port");
        return -1;
    }else{
        //printf("  -open %s ... done!\n", port_tmp);
    }

    // restore the serial port to block.
    if(fcntl(fd,F_SETFL,0)<0)
    {
        printf("fcntl failed!\n");
    }
    else
    {
        //printf("  -fcntl ... done!\n");
    }		
    if(isatty(STDIN_FILENO)==0)
    {
        printf("standard input is not a termined device\n");
    }
    else
    {
        //printf("  -istty ... done!\n");
    }		
    return fd;			
}


int Set_Opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio, oldtio;	
    if(tcgetattr(fd,&oldtio)!=0)
    {
        perror("Setup serial");
        return -1;
    }
    memset(&newtio, 0, sizeof(newtio));
    newtio.c_cflag|=CLOCAL|CREAD;
    newtio.c_cflag&=~CSIZE;	
    switch(nSpeed)
    {
        case 300:
            cfsetispeed(&newtio,B300);
            cfsetospeed(&newtio,B300);
            break;		
        case 600:
            cfsetispeed(&newtio,B600);
            cfsetospeed(&newtio,B600);
            break;			
        case 1200:
            cfsetispeed(&newtio,B1200);
            cfsetospeed(&newtio,B1200);
            break;			
        case 2400:
            cfsetispeed(&newtio,B2400);
            cfsetospeed(&newtio,B2400);
            break;		
        case 4800:
            cfsetispeed(&newtio,B4800);
            cfsetospeed(&newtio,B4800);
            break;			
        case 9600:
            cfsetispeed(&newtio,B9600);
            cfsetospeed(&newtio,B9600);
            break;			
        case 19200:
            cfsetispeed(&newtio,B19200);
            cfsetospeed(&newtio,B19200);
            break;			
        case 38400:
            cfsetispeed(&newtio,B38400);
            cfsetospeed(&newtio,B38400);
            break;			
        case 57600:
            cfsetispeed(&newtio,B57600);
            cfsetospeed(&newtio,B57600);
            break;		
        case 115200:
            cfsetispeed(&newtio,B115200);
            cfsetospeed(&newtio,B115200);
            break;		
        default:
            cfsetispeed(&newtio,B9600);
            cfsetospeed(&newtio,B9600);
            break;	
    }	
    switch(nBits)
    {
        case 7:
            newtio.c_cflag|=CS7;
            break;
        case 8:
            newtio.c_cflag|=CS8;
            break;			
        default:
            newtio.c_cflag|=CS8;
            break;			
    }  	
    switch(nEvent)
    {
        case 'O':
        case 'o':
            newtio.c_cflag|=PARENB;
            newtio.c_cflag|=PARODD;
            newtio.c_iflag|=(INPCK|ISTRIP);
            break;		
        case 'E':
        case 'e':
            newtio.c_iflag|=(INPCK|ISTRIP);
            newtio.c_cflag|=PARENB;
            newtio.c_cflag&=~PARODD;
            break;			
        case 'N':
        case 'n':
            newtio.c_cflag&=~PARENB;
            break;		
        default:
            newtio.c_cflag&=~PARENB;
            break;
    }  
    switch(nStop)
    {
        case 1:
            newtio.c_cflag&=~CSTOPB;
            break;			
        case 2:
            newtio.c_cflag|=CSTOPB;
            break;		
        default:
            newtio.c_cflag&=~CSTOPB;
            break; 			
    } 	
    newtio.c_cc[VTIME]=2;
    newtio.c_cc[VMIN]=0; // 0 non-block ; > 0 block
    tcflush(fd, TCIFLUSH);  
D
Du Rendong 已提交
158 159 160 161 162 163 164
    // This function returns OK if it was able to perform any of the 
    // requested actions, even if it couldn’tperform all the requested actions.
    // If the function returns OK, it is our responsibility to
    // see whether all the requested actions were performed. This means that 
    // after we call tcsetattr to set the desired attributes, we need to call 
    // tcgetattr and compare the actual terminal’s attributes to the desired a
    // ttributes to detect any differences.
D
Du Rendong 已提交
165 166 167 168 169
    if((tcsetattr(fd,TCSANOW,&newtio))!=0)
    {
        perror("Com set error");
        return -1;
    }  
D
Du Rendong 已提交
170
    // printf("  -serial set (%d,%d,%c,%d)... done!\n", nSpeed,nBits,nEvent,nStop);
D
Du Rendong 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    return 0;
}

int write_read_serial(char* msg, char* result)
{
    int fd = -1;
    char *comport = "/dev/ttyUSB2";
    int length = 0;
    if((fd = Open_Port(comport)) < 0)
    {
        printf("Open uart err \n");
        return -1;
    }

    if((Set_Opt(fd,115200,8,'N',1)) < 0)
    {
        perror("Set_Opt RS485 error");
        return -2;
    }

    tcflush(fd, TCIOFLUSH);  
    length = write(fd, msg, 128);
    while((length = read(fd, result, 512)) > 0) {
        tcflush(fd, TCIOFLUSH);  
        printf("%s\n", result);
    }

D
Du Rendong 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}

int read_gps(char* msg, char* result)
{
    write_read_serial("AT+GPSMODE=1\r", result);
    write_read_serial("AT+GPSSTART\r", result);

    // TODO check return result.
    
    int fd = -1;
    char *comport = "/dev/ttyUSB3";
    int length = 0;
    if((fd = Open_Port(comport)) < 0)
    {
        printf("Open uart err \n");
        return -1;
    }

    if((Set_Opt(fd,115200,8,'N',1)) < 0)
    {
        perror("Set_Opt RS485 error");
        return -2;
    }

    tcflush(fd, TCIOFLUSH);  
    memset(result, 0, 2048);
    while((length = read(fd, result, 2048)) > 0) {
        tcflush(fd, TCIOFLUSH);  
        printf("#### %s", result);
    }

D
Du Rendong 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
}
//#include <sys/types.h>
//#include <sys/stat.h>
//#include <fcntl.h>
//#include <unistd.h>
//
//int serial(int argc, char* argv[])
//{		
//    char *comport = "/dev/ttyUSB2";
//    int speed = 115200;
//    char snd_buf[128] = "";
//    char recv_buf[512] = "";
//    int fd = -1; 
//    int length = 0;
//
//    int type = 0; // 0 read 1 write 2 readwrite 3 writeread
//    int result = 0;
//    while( (result = getopt(argc, argv, "rw:a:v:t:s:")) != -1 )
//    {
//        switch(result)
//        {
//            case 'r': // read serail port
//                type = 0; 
//                break;
//            case 'w': // write to serial port
//                type = 1;
//                memcpy(snd_buf, optarg, strlen(optarg) + 1);
//                snd_buf[strlen(optarg)] = '\r';
//                break;
//            case 'a': // read serail port and write 
//                type = 2;
//                memcpy(snd_buf, optarg, strlen(optarg) + 1);
//                snd_buf[strlen(optarg)] = '\r';
//                break;
//            case 'v': // write serial port and read
//                type = 3;
//                memcpy(snd_buf, optarg, strlen(optarg) + 1);
//                snd_buf[strlen(optarg)] = '\r';
//                break;
//            case 't': // select device /dev/ttyUSB0
//                comport = optarg;
//                break;
//            case 's': // set speed 115200 
//                speed = atoi(optarg);
//                break;
//            default:
//                printf("Usage: %s [rwavts] r read w write a readwrite v writeread t device s speed\n", argv[0]);
//                break;
//        }
//    }
//
//    if((fd = Open_Port(comport)) < 0)
//    {
//        printf("Open uart err \n");
//        return -1;
//    }
//
//    if((Set_Opt(fd,speed,8,'N',1)) < 0)
//    {
//        perror("Set_Opt RS485 error");
//        return -2;
//    }
//
//    tcflush(fd, TCIOFLUSH);  
//    switch (type){
//        case 0:
//            while((length = read(fd, recv_buf, 512)) > 0) {
//                tcflush(fd, TCIOFLUSH);  
//                printf("%s\n", recv_buf);
//            }
//            break;
//        case 1:
//            length = write(fd, snd_buf, 128);
//            break;
//        case 2:
//            while((length = read(fd, recv_buf, 512)) > 0) {
//                tcflush(fd, TCIOFLUSH);  
//                printf("%s\n", recv_buf);
//            }
//            tcflush(fd, TCIOFLUSH);  
//            length = write(fd, snd_buf, 128);
//            break;
//        case 3:
//            length = write(fd, snd_buf, 128);
//            while((length = read(fd, recv_buf, 512)) > 0) {
//                tcflush(fd, TCIOFLUSH);  
//                printf("%s\n", recv_buf);
//            }
//            break;
//        default:
//            printf("Wrong type %d\n", type);
//            break;
//    }
//    close(fd);
//    return 0;
//}

// ============================================================================
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

D
Du Rendong 已提交
334
#define DISPLAY_INTERVAL 5 // seconds
D
Du Rendong 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348
map<char, string>DLSX;

void initializeDLSX(){
    DLSX['A'] = "00"; DLSX['B'] = "01"; DLSX['C'] = "02"; DLSX['D'] = "03";
    DLSX['E'] = "04"; DLSX['F'] = "05"; DLSX['G'] = "06"; DLSX['H'] = "07";
    DLSX['I'] = "08"; DLSX['J'] = "09"; DLSX['K'] = "0a"; DLSX['L'] = "0b";
    DLSX['M'] = "0c"; DLSX['N'] = "0d"; DLSX['O'] = "0e"; DLSX['P'] = "0f";
    DLSX['Q'] = "10"; DLSX['R'] = "11"; DLSX['S'] = "12"; DLSX['T'] = "13";
    DLSX['U'] = "14"; DLSX['V'] = "15"; DLSX['W'] = "16"; DLSX['X'] = "17";
    DLSX['Y'] = "18"; DLSX['Z'] = "19"; DLSX['&'] = "1a"; DLSX[' '] = "1b";
    DLSX['?'] = "1c"; DLSX['!'] = "1d"; DLSX['1'] = "1e"; DLSX['2'] = "1f";
    DLSX['3'] = "20"; DLSX['4'] = "21"; DLSX['5'] = "22"; DLSX['6'] = "23";
    DLSX['7'] = "24"; DLSX['8'] = "25"; DLSX['9'] = "26"; DLSX['0'] = "27";
    DLSX['"'] = "49"; DLSX['*'] = "50"; DLSX['<'] = "51"; DLSX['>'] = "52";
D
Du Rendong 已提交
349 350
    /*DLSX['.'] = "b1"; DLSX['-'] = "c9"; DLSX['x'] = "d4"; DLSX['%'] = "d5";*/
    DLSX['.'] = "63"; DLSX['-'] = "c9"; DLSX['x'] = "d4"; DLSX['%'] = "d5";
D
Du Rendong 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    DLSX[','] = "1b"; // dlsx don't support ',' just work as space;
    DLSX['+'] = ""; // dlsx don't support ',' just work as space;
    DLSX[':'] = ""; // dlsx don't support ',' just work as space;
}
enum DisplayType
{
    DCSQ = 0,
    DPSART,
    DCOPS,
    DNETRATE,
    DGPS
};

enum DisplayDirection
{
    ToVideo = 0,
    ToLogFile,
    ToWebPage,
    ToStdout,
    None
};  
class Item
{
D
Du Rendong 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
public:
    Item(DisplayType type, DisplayDirection disp):m_type(type), m_disp(disp){}
    virtual int Display()=0;
    virtual void DisplayOnVideo(string &value, string &position)
    {
        cout << "value = " << value << endl;
        string cmd = "~/bit_dlsx 81 01 04 73 " + position;
        size_t value_size = value.size();
        int actual_count = 0;
        // only display 10 chars peer item.
        for(int i = 0, actual_count = 0; actual_count < 10; i++) {
            if(i < value_size){
                if(DLSX[value[i]] != "") {
                    cmd += " " + DLSX[value[i]];
                    actual_count++; 
D
Du Rendong 已提交
389
                }
D
Du Rendong 已提交
390 391 392
            } else {
                cmd += " 1b";
                actual_count++;
D
Du Rendong 已提交
393 394
            }
        }
D
Du Rendong 已提交
395 396 397 398 399 400
        cmd += " ff";
        system(cmd.c_str());
    }
    //    private:
    DisplayDirection m_disp;
    DisplayType m_type; 
D
Du Rendong 已提交
401 402
};

D
Du Rendong 已提交
403
#include <sstream>
D
Du Rendong 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
class CSQ : public Item
{
public:
    CSQ(DisplayType type, DisplayDirection disp) : Item(type, disp){}
    virtual int Display()
    { 
        char buf[512] = {0};
        write_read_serial("ATE\r", buf);
        memset(buf, 0, sizeof(buf));
        write_read_serial("AT+CSQ\r", buf);

        string tmp(buf);
        int pos = 0, start_pos = 0;
        while((pos = tmp.find("\r\n", start_pos)) != string::npos)
        {
            string sentence = tmp.substr(start_pos, pos);
            start_pos = pos + 2;
            if(sentence.find("+CSQ:") != string::npos)
            {
                string value = sentence.substr(0, sentence.find("\r\n"));
                switch(m_disp)
                {
                    case ToVideo: {
D
Du Rendong 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439
                        string dispvalue = value.substr(value.find(":") + 2, value.find(",") - value.find(":") - 2);
                        // convert string dispvalue into int disp.
                        int disp;
                        stringstream ss;
                        ss << dispvalue;
                        ss >> disp; 
                        string signal;
                        if (disp <= 6) signal = "";
                        else if (disp <= 13) signal = "I";
                        else if (disp <= 20) signal = "II";
                        else if (disp <= 27) signal = "III";
                        else if (disp <= 31) signal = "IIII";
                        else signal = "XXXX";
D
Du Rendong 已提交
440
                        string position("20");
D
Du Rendong 已提交
441
                        DisplayOnVideo(signal, position);
D
Du Rendong 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
                        break;
                    }
                    case ToStdout:
                        break;
                    case ToLogFile:
                        break;
                    case ToWebPage:
                        break;
                    default:
                        break;
                }
                break;
            }
        }
    }
};

class PSART : public Item
{
    public:
        PSART(DisplayType type, DisplayDirection disp) : Item(type, disp){}
        virtual int Display()
        {
            char buf[512] = {0};
            write_read_serial("ATE\r", buf);
            memset(buf, 0, sizeof(buf));
            write_read_serial("AT+PSRAT\r", buf);

            string tmp(buf);
            int pos = 0, start_pos = 0;
            while((pos = tmp.find("\r\n", start_pos)) != string::npos)
            {
                string sentence = tmp.substr(start_pos, pos);
                start_pos = pos + 2;
                if(sentence.find("+PSRAT:") != string::npos)
                {
                    string value = sentence.substr(0, sentence.find("\r\n"));
                    switch (m_disp) 
                    {
                        case ToVideo: {
D
Du Rendong 已提交
482 483 484 485 486
                                          string dispvalue = value.substr((value.find(":") + 2));
                                          string position("21");
                                          DisplayOnVideo(dispvalue, position);
                                          break;
                                      }
D
Du Rendong 已提交
487
                        case ToStdout:
D
Du Rendong 已提交
488
                                      break;
D
Du Rendong 已提交
489
                        case ToLogFile:
D
Du Rendong 已提交
490
                                      break;
D
Du Rendong 已提交
491
                        case ToWebPage:
D
Du Rendong 已提交
492
                                      break;
D
Du Rendong 已提交
493
                        default:
D
Du Rendong 已提交
494
                                      break;
D
Du Rendong 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                    }
                    break;
                }
            }
        }
};

class COPS : public Item
{
    public:
        COPS(DisplayType type, DisplayDirection disp) : Item(type, disp){}
        virtual int Display()
        {
            char buf[512] = {0};
            write_read_serial("ATE\r", buf);
            memset(buf, 0, sizeof(buf));
D
Du Rendong 已提交
511 512
            // use short string for IPS. CMCC/CU-GSM
            write_read_serial("AT+COPS=3,1\r", buf);
D
Du Rendong 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526
            write_read_serial("AT+COPS?\r", buf);

            string tmp(buf);
            int pos = 0, start_pos = 0;
            while((pos = tmp.find("\r\n", start_pos)) != string::npos)
            {
                string sentence = tmp.substr(start_pos, pos);
                start_pos = pos + 2;
                if(sentence.find("+COPS:") != string::npos)
                {
                    string value = sentence.substr(0, sentence.find("\r\n"));
                    switch (m_disp) 
                    {
                        case ToVideo: {
D
Du Rendong 已提交
527 528 529 530 531
                                          string dispvalue = value.substr(value.find("\"") + 1, value.rfind("\"") - value.find("\"") - 1);
                                          string position("22");
                                          DisplayOnVideo(dispvalue, position);
                                          break;
                                      }
D
Du Rendong 已提交
532
                        case ToStdout:
D
Du Rendong 已提交
533
                                      break;
D
Du Rendong 已提交
534
                        case ToLogFile:
D
Du Rendong 已提交
535
                                      break;
D
Du Rendong 已提交
536
                        case ToWebPage:
D
Du Rendong 已提交
537
                                      break;
D
Du Rendong 已提交
538
                        default:
D
Du Rendong 已提交
539
                                      break;
D
Du Rendong 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553
                    }
                    break;
                }
            }

        }
};

class NETRATE : public Item
{
    public:
        NETRATE(DisplayType type, DisplayDirection disp) : Item(type, disp){}
        virtual int Display()
        {
D
Du Rendong 已提交
554 555 556 557
            static unsigned long old_recv = 0;
            static unsigned long old_tras = 0;
            unsigned long current_recv = 0; 
            unsigned long current_tras = 0;
D
Du Rendong 已提交
558 559

            char tmp[64] = {0};
D
Du Rendong 已提交
560
            FILE* rp = popen("ifconfig eth0 | grep \"RX bytes\" | cut -d : -f 2 | cut - -d \" \" -f 1", "r");
D
Du Rendong 已提交
561 562 563 564 565
            while(fgets(tmp, sizeof(tmp), rp) != NULL)
            {
                if (tmp[strlen(tmp) - 1] == '\n') {
                    tmp[strlen(tmp) - 1] = '\0'; //去除换行符
                }
D
Du Rendong 已提交
566
                current_recv = atol(tmp);
D
Du Rendong 已提交
567 568 569
            }
            pclose(rp);

D
Du Rendong 已提交
570 571 572 573 574 575 576
            // check peer DISPLAY_INTERVAL seconds.
            double recv_rate = 0;

            // incase the byte up to 4294967295 then down to 0;
            if ((old_recv != 0) && (current_recv >= old_recv)) 
                recv_rate = ((double)(current_recv - old_recv)) / DISPLAY_INTERVAL;
            old_recv = current_recv;
D
Du Rendong 已提交
577

D
Du Rendong 已提交
578
            FILE* tp = popen("ifconfig eth0 | grep \"RX bytes\" | cut -d : -f 3 | cut - -d \" \" -f 1", "r");
D
Du Rendong 已提交
579 580 581 582 583
            while(fgets(tmp, sizeof(tmp), tp) != NULL)
            {
                if (tmp[strlen(tmp) - 1] == '\n') {
                    tmp[strlen(tmp) - 1] = '\0'; //去除换行符
                }
D
Du Rendong 已提交
584
                current_tras = atol(tmp);
D
Du Rendong 已提交
585 586
            }
            pclose(tp);
D
Du Rendong 已提交
587 588 589 590 591 592 593
            double tras_rate = 0;
            // incase the byte up to 4294967295 then down to 0;
            if ((old_tras != 0) && (current_tras >= old_tras))
            {
                tras_rate = ((double)(current_tras - old_tras)) / DISPLAY_INTERVAL;
            }
            old_tras = current_tras;
D
Du Rendong 已提交
594

D
Du Rendong 已提交
595
            // --------------------------------------------------------
D
Du Rendong 已提交
596
            char str[11] = {0};
D
Du Rendong 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
            double disp_recv_rate = recv_rate;
            if ((disp_recv_rate / 1024) >= 1)
            {
                disp_recv_rate = disp_recv_rate / 1024;
                if ((disp_recv_rate / 1024) >= 1)
                {
                    disp_recv_rate = disp_recv_rate / 1024;
                    sprintf(str, "RX%.1fMS", disp_recv_rate);
                }
                else
                {
                    sprintf(str, "RX%.1fKS", disp_recv_rate);
                }
            }
            else
            {
                sprintf(str, "RX%.1fBS", disp_recv_rate);
            }
D
Du Rendong 已提交
615 616
            string value(str);

D
Du Rendong 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
            // --------------------------------------------------------
            char str1[11] = {0};
            double disp_tras_rate = tras_rate;
            cout << "tras" << tras_rate << endl;
            if ((disp_tras_rate / 1024) >= 1)
            {
                disp_tras_rate = disp_tras_rate / 1024;
                if ((disp_tras_rate / 1024) >= 1)
                {
                    disp_tras_rate = disp_tras_rate / 1024;
                    sprintf(str1, "TX%.1fMS", disp_tras_rate);
                }
                else
                {
                    sprintf(str1, "TX%.1fKS", disp_tras_rate);
                }
            }
            else
            {
                sprintf(str1, "TX%.1fBS", disp_tras_rate);
            }
D
Du Rendong 已提交
638
            string value1(str1);
D
Du Rendong 已提交
639

D
Du Rendong 已提交
640 641 642
            switch (m_disp)
            {
                case ToVideo: {
D
Du Rendong 已提交
643 644 645 646 647 648
                                  string position("23");
                                  DisplayOnVideo(value, position);
                                  string position1("24");
                                  DisplayOnVideo(value1, position1);
                                  break;
                              }
D
Du Rendong 已提交
649
                case ToStdout:
D
Du Rendong 已提交
650
                              break;
D
Du Rendong 已提交
651
                case ToLogFile:
D
Du Rendong 已提交
652
                              break;
D
Du Rendong 已提交
653
                case ToWebPage:
D
Du Rendong 已提交
654
                              break;
D
Du Rendong 已提交
655
                default:
D
Du Rendong 已提交
656
                              break;
D
Du Rendong 已提交
657 658 659 660
            }
        }
};

D
Du Rendong 已提交
661
//#include <nmea.h>
D
Du Rendong 已提交
662 663 664 665 666 667
class GPS : public Item
{
    public:
        GPS(DisplayType type, DisplayDirection disp) : Item(type, disp){}
        virtual int Display()
        {
D
Du Rendong 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
            // get gps info
            char result[2048] = "";
            read_gps(NULL, result);

            cout << "=== GPS ===" << result << endl;
            for(int i = 0; i < 2048; i++)
            {
                printf("%x ", result[i]);
            }
            printf("\n");
            //int pos = 0, start_pos = 0;                                                                                                                                   
            //while((pos = tmp.find("\r\n", start_pos)) != string::npos)
            //{
            //}

D
Du Rendong 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

        }
};

class DisplayFactory
{
    public:
        static Item* createDisplay(DisplayType type, DisplayDirection direction)
        {
            Item* p = NULL;
            switch(type){
                case DCSQ:
                    p = new CSQ(type, direction);
                    break;
                case DPSART:
                    p = new PSART(type, direction);
                    break;
                case DCOPS:
                    p = new COPS(type, direction);
                    break;
                case DNETRATE:
                    p = new NETRATE(type, direction);
                    break;
                case DGPS:
                    p = new GPS(type, direction);
                    break;
                default:
                    break;
            }
            return p;
        }
};
// ============================================================================
#include <map>
bool parse_config(const string & filename, map<string, string> & m);

// ----------------------------------------------------------------------------
#define COMMENT_CHAR '#'

bool IsSpace(char c)
{
    if (' ' == c || '\t' == c)
        return true;
    return false;
}

bool IsCommentChar(char c)
{
    switch(c) {
        case COMMENT_CHAR:
            return true;
        default:
            return false;
    }
}

void Trim(string & str)
{
    if (str.empty()) {
        return;
    }
    int i, start_pos, end_pos;
    for (i = 0; i < str.size(); ++i) {
        if (!IsSpace(str[i])) {
            break;
        }
    }
    if (i == str.size()) { // 全部是空白字符串
        str = "";
        return;
    }

    start_pos = i;

    for (i = str.size() - 1; i >= 0; --i) {
        if (!IsSpace(str[i])) {
            break;
        }
    }
    end_pos = i;

    str = str.substr(start_pos, end_pos - start_pos + 1);
}

bool AnalyseLine(const string & line, string & key, string & value)
{
    if (line.empty())
        return false;
    int start_pos = 0, end_pos = line.size() - 1, pos;
    if ((pos = line.find(COMMENT_CHAR)) != -1) {
        if (0 == pos) {  // 行的第一个字符就是注释字符
            return false;
        }
        end_pos = pos - 1;
    }
    // 预处理,删除注释部分
    string new_line = line.substr(start_pos, start_pos + 1 - end_pos);  
    if ((pos = new_line.find('=')) == -1)
        return false;  // 没有=号

    key = new_line.substr(0, pos);
    value = new_line.substr(pos + 1, end_pos + 1- (pos + 1));

    Trim(key);
    if (key.empty()) {
        return false;
    }
    Trim(value);
    return true;
}

bool parse_config(const string & filename, map<string, string> & m)
{
    m.clear();
    ifstream infile(filename.c_str());
    if (!infile) {
        cout << "file open error" << endl;
        return false;
    }
    string line, key, value;
    while (getline(infile, line)) {
        if (AnalyseLine(line, key, value)) {
            m[key] = value;
        }
    }
}

D
Du Rendong 已提交
810
// =============================================================================
D
Du Rendong 已提交
811 812
int main(int argc, char* argv[])
{
D
Du Rendong 已提交
813
    map<string, string> cfg; // key value for log config
D
Du Rendong 已提交
814

D
Du Rendong 已提交
815
    map<string, DisplayType> map_type; // map the string to enum display type
D
Du Rendong 已提交
816 817 818 819 820 821
    map_type["CSQ"] = DCSQ;
    map_type["PSART"] = DPSART;
    map_type["COPS"] = DCOPS;
    map_type["NETRATE"] = DNETRATE;
    map_type["GPS"] = DGPS;

D
Du Rendong 已提交
822
    map<string, DisplayDirection> map_direction; // string to enum direction
D
Du Rendong 已提交
823 824 825 826 827 828 829 830 831
    map_direction["ToLogFile"] = ToLogFile;
    map_direction["ToStdout"] = ToStdout;
    map_direction["ToVideo"] = ToVideo;
    map_direction["ToWebPage"] = ToWebPage;

    initializeDLSX();

    while(true) 
    {
D
Du Rendong 已提交
832 833 834 835
        // TODO now check the config file then start instance peer config, 
        // delete the instances everytime. 
        // enhance should be do to check the config file compare to the old 
        // one to determine which instance should add or delete.
D
Du Rendong 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
        parse_config("/mnt/nand/lte.conf", cfg);
        vector<Item*> display_list; 

        map<string, string>::iterator itc = cfg.begin();
        for(; itc != cfg.end(); ++itc)
        {
            Item* p = DisplayFactory::createDisplay(map_type[itc->first], 
                    map_direction[itc->second]);
            display_list.push_back(p);
        }

        vector<Item*>::iterator it;
        for(it = display_list.begin(); it != display_list.end(); ++it)
        {
            (*it)->Display();
        }
D
Du Rendong 已提交
852
        sleep(DISPLAY_INTERVAL);
D
Du Rendong 已提交
853 854 855 856 857 858 859 860

        for(it = display_list.begin(); it != display_list.end(); ++it)
        {
            delete (*it);
        }
    }
    return 0;
}