Exemple #1
0
int create_socket(void)
{
        Msg msg_head;

        int server_sockfd;
        int len;
        int sin_size;
        char buf[SIZE];
        struct sockaddr_in server_addr;
        struct sockaddr_in client_addr;

        memset(&server_addr, 0, sizeof(server_addr));

        server_addr.sin_family      = AF_INET;  //设置为IP通信
        server_addr.sin_addr.s_addr = INADDR_ANY;   //无限制
        server_addr.sin_port        = htons(8000);

        if ((server_sockfd = socket(PF_INET,SOCK_DGRAM,0)) < 0) {
                ERROR("socket error\n");
                return -1;
        }

        if(bind(server_sockfd, (struct sockaddr *)&server_addr, 
                                sizeof(struct sockaddr)) < 0) {
                ERROR("bind error\n");
                return -1;
        }

        sin_size = sizeof(struct sockaddr_in);

        INFO("waiting for packet......\n");

        bzero(buf, SIZE);
        
        for(;;) {
                /* 接收消息*/
                if((len = recvfrom(server_sockfd, buf, SIZE, 0, 
                                                (struct sockaddr*)&client_addr, &sin_size)) < 0) {
                        ERROR("receive packet error\n");
                        return -1;
                }
                INFO("received packet from %s\n",inet_ntoa(client_addr.sin_addr));

                /* 消息处理
                 * 解析消息头
                 * 分发消息
                 * */
                memcpy(&msg_head, buf, sizeof(msg_head));
                switch(msg_head.MsgId) {
                        /* 板卡状态*/
                        case PC_ARM_CHECK_MACHINE_STAT_REQ:
                                get_machine_stat(buf);
                                break;

                                /* 寄存器操作请求*/
                        case PC_ARM_REGISTER_RW_REQ:
                                register_xfer(buf);
                                break;

                                /* 软件更新 */
                        case PC_ARM_SOFTWARE_UPDATE_REQ:
                                sw_update(buf);
                                break;

                                /* EEPROM */
                        case PC_ARM_EEPROM_RW_REQ:
                                eeprom_xfer(buf); 
                                break;

                                /* GPRS短信息*/
                        case PC_ARM_SEND_GPRS_MESSAGE_REQ:
                                send_gprs_message_req(buf);
                                break;
                
                                /* 发送ZIGBEE信息*/
                        case PC_ARM_SEND_ZIGBEE_MESSAGE_REQ:
                                send_zigbee_message_req(buf);
                                break;

                                /* 报警器控制*/
                        case PC_ARM_BEEP_CONTROL_REQ:
                                beep_control_req(buf);
                                break;

                                /* LED控制*/
                        case PC_ARM_LEDS_CONTROL_REQ:
                                led_control_req(buf);
                                break;

                                /* 硬件自检*/
                        case PC_ARM_HARDWARE_TESTSELF_REQ:
                                hardware_selftest_req(buf);
                                break;
                
                                /* 时间校准*/
                        case PC_ARM_TIME_ADJUST_REQ:
                                time_adjust(buf);
                                break;
                }

                /* 反馈消息*/
                if(sendto(server_sockfd, buf, len, 0, 
                                        (struct sockaddr *)&client_addr, sin_size) < 0) {
                        ERROR("send packet error\n");
                        return -1;
                }
        }
}
Exemple #2
0
/*-------------------------------------------------------------+
 | Display a table of Dawn and Dusk for the year.              |
 | Argument sunmode indicates the definition of Dawn and Dusk: |
 |   0  -> Rise and Set                                        |
 |   1  -> Civil Twilight                                      |
 |   2  -> Nautical Twilight                                   |
 |   3  -> Astronomical Twilight                               |
 +-------------------------------------------------------------*/
int display_sun_table ( FILE *fd_sun, int year, long timezone,
                        int sunmode, int offset, int timemode, int lat_d, int lat_m, int lon_d, int lon_m )
{
    static struct tzones {
        char *name;
        long seconds;
    } us_tzones[] = {
        {"Atlantic",         14400 },
        {"Eastern",          18000 },
        {"Central",          21600 },
        {"Mountain",         25200 },
        {"Pacific",          28800 },
        {"Alaska",           32400 },
        {"Hawaii-Aleutian",  36000 },
        {"Samoa",            39600 },
        {"Wake Island",     -43200 },
        {"Guam",            -39600 },
    };
    static int n_tzones = ( sizeof(us_tzones) / sizeof(struct tzones) );

    static int mdays[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    int time_adjust ( int, int, unsigned char );

    long   julianday, julianday0;

    int    j, month, day, yday, half ;
    int    rise, set, scode;
    int    retcode;
    char   tmark = ' ';
    char   minibuf[64];
    char   *timename;

    double latitude, longitude;

    get_dst_info(year);

    timename = ( timemode == TIMEMODE_CIVIL ) ? "Civil" : "Standard";

    latitude  = (lat_d < 0) ? (double)lat_d - (double)lat_m / 60.  :
                (double)lat_d + (double)lat_m / 60. ;
    longitude = (lon_d < 0) ? (double)lon_d - (double)lon_m / 60.  :
                (double)lon_d + (double)lon_m / 60. ;

    julianday0 = greg2jd(year, 1, 1);

    if ( greg2jd(year + 1, 1, 1) - julianday0 == 366 )
        mdays[2] = 29;
    else
        mdays[2] = 28;

    switch (sunmode) {
    case RiseSet :
        sprintf(minibuf, "Sunrise and Sunset for %d\n", year);
        break;
    case CivilTwi :
        sprintf(minibuf, "Civil Twilight for %d\n", year);
        break;
    case NautTwi :
        sprintf(minibuf, "Nautical Twilight for %d\n", year);
        break;
    case AstroTwi :
        sprintf(minibuf, "Astronomical Twilight for %d\n", year);
        break;
    case AngleOffset :
        sprintf(minibuf, "Sun centre at %d angle minutes below horizon for %d\n", offset, year);
        break;
    };
    fprintf(fd_sun, "%*s%s", (80 - (int)strlen(minibuf))/2, " ", minibuf);
    (void) fprintf(fd_sun, "\nLocation: ");
    (void) fprintf(fd_sun, "%s%03d:%02d,",
                   longitude < 0 ? "W" : "E", abs(lon_d), abs(lon_m));
    (void) fprintf(fd_sun, " %s%02d:%02d",
                   latitude  < 0 ? "S" : "N", abs(lat_d), abs(lat_m));

    for ( j = 0; j < n_tzones; j++ ) {
        if ( us_tzones[j].seconds == timezone )
            break;
    }
    if ( j < n_tzones )
        (void) fprintf(fd_sun, "   US/%s %s Time\n\n", us_tzones[j].name, timename);
    else
        (void) fprintf(fd_sun, "   Timezone: %.1fh %s of Greenwich - %s Time\n\n",
                       (double)abs(timezone)/3600., (timezone < 0 ? "East" : "West"), timename );

    for ( half = 0; half < 2; half++ ) {

        if ( half == 0 )
            (void) fprintf(fd_sun, "       Jan          Feb          Mar          Apr          May          Jun\n");
        else
            (void) fprintf(fd_sun, "\n\n       Jul          Aug          Sep          Oct          Nov          Dec\n");

        if ( sunmode == 0 ) {
            fprintf(fd_sun, "Day Rise   Set");
            for ( month = 2; month <= 6; month++ )
                fprintf(fd_sun, "   Rise   Set");
            fprintf(fd_sun, "\n  ");
        }
        else {
            fprintf(fd_sun, "Day Morn   Eve");
            for ( month = 2; month <= 6; month++ )
                fprintf(fd_sun, "   Morn   Eve");
            fprintf(fd_sun, "\n  ");
        }

        for ( month = 1; month <= 6; month++ )
            (void) fprintf(fd_sun, "  hh:mm hh:mm");

        scode = NORMAL_SUN;
        for ( day = 1; day < 32; day++ ) {
            (void) fprintf(fd_sun, "\n%02d", day);
            for ( month = 6 * half + 1; month <= 6 * half + 6; month++ ) {
                if ( day > mdays[month] ) {
                    (void) fprintf(fd_sun, "             ");
                    continue;
                }
                julianday = greg2jd( year, month, day );

                yday = (int)(julianday - julianday0);

                retcode = suntimes(latitude, longitude, timezone, julianday,
                                   sunmode, offset, &rise, &set, NULL, NULL );

                if ( timemode == TIMEMODE_CIVIL ) {
                    /* Adjust times for Daylight Time */
                    if ( retcode == NORMAL_SUN || retcode == NO_SUNSET ) {
                        rise += time_adjust(yday, rise, LGL2STD);
                        if ( rise < 0 || rise > 1439 )
                            retcode = NO_SUNRISE;
                    }
                    if ( retcode == NORMAL_SUN || retcode == NO_SUNRISE ) {
                        set += time_adjust(yday, set, LGL2STD);
                        if ( set < 0 || set > 1439 )
                            retcode = NO_SUNSET;
                    }

                    /* Mark day of time change */
                    tmark = (yday == 0 ||
                             time_adjust(yday, 720, LGL2STD) == time_adjust(yday - 1, 720, LGL2STD)) ? ' ' : '*';
                }

                scode |= retcode;

                switch ( retcode ) {
                case DOWN_ALL_DAY :
                    (void) fprintf(fd_sun, " %c----- -----", tmark);
                    break ;
                case UP_ALL_DAY :
                    (void) fprintf(fd_sun, " %c***** *****", tmark);
                    break ;
                case NO_SUNRISE :
                    (void) fprintf(fd_sun, " %c      %02d:%02d", tmark, set/60, set % 60);
                    break ;
                case NO_SUNSET :
                    (void) fprintf(fd_sun, " %c%02d:%02d      ", tmark, rise/60, rise%60);
                    break ;
                default :
                    (void) fprintf(fd_sun, " %c%02d:%02d %02d:%02d",
                                   tmark, rise/60, rise%60, set/60,set%60 );
                }
            }
        }
    }

    if ( timemode == TIMEMODE_CIVIL ) {
        (void) fprintf(fd_sun, "\n\n(*) Denotes time change");
    }
    else {
        (void) fprintf(fd_sun, "\n\nAdd offset for Daylight Time (usually +60 minutes) if and when in effect.");
    }

    if ( scode & (DOWN_ALL_DAY | UP_ALL_DAY) ) {
        (void) fprintf(fd_sun, "\n\n(*****) Sun continuously above horizon");
        (void) fprintf(fd_sun,  "%*s(-----) Sun continuously below horizon", 4, " ");
    }

    (void) fprintf(fd_sun, "\n");


    return 0;
}