?? bsp.c
字號:
/****************************************************************************** Product: QDPP example, 80x86, Win32, Visual C++ 6.0* Last Updated for Version: 3.4.00* Date of the Last Update: Sep 03, 2007** Q u a n t u m L e a P s* ---------------------------* innovating embedded systems** Copyright (C) 2002-2007 Quantum Leaps, LLC. All rights reserved.** This software may be distributed and modified under the terms of the GNU* General Public License version 2 (GPL) as published by the Free Software* Foundation and appearing in the file GPL.TXT included in the packaging of* this file. Please note that GPL Section 2[b] requires that all works based* on this software must also be made publicly available under the terms of* the GPL ("Copyleft").** Alternatively, this software may be distributed and modified under the* terms of Quantum Leaps commercial licenses, which expressly supersede* the GPL and are specifically designed for licensees interested in* retaining the proprietary status of their code.** Contact information:* Quantum Leaps Web site: http://www.quantum-leaps.com* e-mail: info@quantum-leaps.com*****************************************************************************/#include "qf_port.h"#include "qs_port.h"#include "qassert.h"#include "qdpp.h"#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/select.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <errno.h>#include <time.h>#include <termios.h>#include <unistd.h>Q_DEFINE_THIS_FILE/*--------------------------------------------------------------------------*/#ifdef Q_SPYenum QSQHsmTstRecords { QS_QDPP_DISPLAY = QS_USER};int l_sock = -1;/*..........................................................................*/uint8_t QS_init(void const *arg) { static uint8_t qsBuf[1024]; /* 1K buffer for Quantum Spy */ char host[64]; char const *src; char *dst; uint16_t port = 6601; /* default port */ struct sockaddr_in servAddr; struct hostent *server; QS_initBuf(qsBuf, sizeof(qsBuf)); src = (char const *)arg; dst = host; while ((*src != '\0') && (*src != ':') && (dst < &host[sizeof(host)])) { *dst++ = *src++; } *dst = '\0'; if (*src == ':') { port = (uint16_t)strtoul(src + 1, NULL, 10); } l_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* TCP socket */ if (l_sock == -1){ printf("Socket cannot be created.\n" "socket error 0x%08X.", errno); return (uint8_t)0; } server = gethostbyname(host); if (server == NULL) { printf("QSpy host name cannot be resolved.\n" "socket error 0x%08X.", errno); return (uint8_t)0; } memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; memcpy(&servAddr.sin_addr, server->h_addr, server->h_length); servAddr.sin_port = htons(port); if (connect(l_sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1) { printf("Socket cannot be connected to the QSpy server.\n" "socket error 0x%08X.", errno); QS_exit(); return (uint8_t)0; } return (uint8_t)1; /* success */}/*..........................................................................*/void QS_exit(void) { if (l_sock != -1) { close(l_sock); }}#define QS_DUMP() do { \ uint16_t nBytes = 1000; \ uint8_t const *block; \ QF_INT_LOCK(ignore); \ block = QS_getBlock(&nBytes); \ QF_INT_UNLOCK(ignore); \ if (block != (uint8_t *)0) { \ send(l_sock, (char const *)block, nBytes, 0); \ } \} while (0)/*..........................................................................*/void QS_flush(void) { uint16_t nBytes = 1000; uint8_t const *block; while ((block = QS_getBlock(&nBytes)) != (uint8_t *)0) { send(l_sock, (char const *)block, nBytes, 0); nBytes = 1000; }}/*..........................................................................*/QSTimeCtr QS_getTime(void) { return (QSTimeCtr)clock();}#endif /* Q_SPY *//*--------------------------------------------------------------------------*/static void *idleThread(void *me) { /* the expected P-Thread signature */ struct timeval timeout = { 0 }; /* timeout for select() */ struct termios tio; /* modified terminal attributes */ tcgetattr(0, &tio); /* obtain the current terminal attributes */ tio.c_lflag &= ~(ICANON | ECHO); /* disable the canonical mode & echo */ tcsetattr(0, TCSANOW, &tio); /* set the new attributes */ while (QF_running_) { fd_set con; /* FD set representing the console */ FD_ZERO(&con); FD_SET(0, &con); timeout.tv_usec = 8000; /* sleep for the full tick or until a console input arrives, NOTE01 */ if (0 != select(1, &con, 0, 0, &timeout)) { /* any descriptor set? */ char ch; read(0, &ch, 1); if (ch == '\33') { /* ESC pressed? */ QF_publish(Q_NEW(QEvent, TERMINATE_SIG)); } } QS_DUMP(); /* dump a chunk of QS trace data */ } return (void *)0; /* return success */}/*..........................................................................*/void BSP_init(int argc, char *argv[]) { pthread_attr_t attr; struct sched_param param; pthread_t idle; char const *hostAndPort = "localhost:6601"; if (argc > 1) { /* port specified? */ hostAndPort = argv[1]; } if (!QS_INIT(hostAndPort)) { printf("\nUnable to open QSpy socket\n"); exit(-1); } pthread_attr_init(&attr); /* SCHED_FIFO corresponds to real-time preemptive priority-based scheduler * NOTE: This scheduling policy requires the superuser priviledges */ pthread_attr_setschedpolicy(&attr, SCHED_FIFO); param.sched_priority = sched_get_priority_min(SCHED_FIFO); pthread_attr_setschedparam(&attr, ¶m); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (pthread_create(&idle, &attr, &idleThread, 0) != 0) { /* Creating the p-thread with the SCHED_FIFO policy failed. * Most probably this application has no superuser privileges, * so we just fall back to the default SCHED_OTHER policy * and priority 0. */ pthread_attr_setschedpolicy(&attr, SCHED_OTHER); param.sched_priority = 0; pthread_attr_setschedparam(&attr, ¶m); Q_ALLEGE(pthread_create(&idle, &attr, &idleThread, 0) == 0); } pthread_attr_destroy(&attr);}/*..........................................................................*/void displyPhilStat(uint8_t n, char const *stat) { printf("Philosopher %2d is %s\n", (int)n, stat); QS_BEGIN(QS_QDPP_DISPLAY, QS_apObj_); QS_U8(1, n); QS_STR(stat); QS_END();}/*..........................................................................*/void Q_assert_handler(char const Q_ROM * const Q_ROM_VAR file, int line) { fprintf(stderr, "Assertion failed in %s, line %d", file, line); QF_exit();}/******************************************************************************* NOTE01:* The select() system call seems to deliver the finest time granularity of* 1 clock tick. The timeout value passed to select() is rounded up to the* nearest tick (10 ms on desktop Linux). The timeout cannot be too short,* because the system might choose to busy-wait for very short timeouts.* An alternative, POSIX nanosleep() system call seems to deliver only 20ms* granularity.** Here the select() call is used not just as a fairly portable way to sleep* with subsecond precision. The select() call is also used to detect any* characters typed on the console.** Also according to man pages, on Linux, the function select() modifies* timeout to reflect the amount of time not slept; most other implementations* do not do this. This causes problems both when Linux code which reads* timeout is ported to other operating systems, and when code is ported to* Linux that reuses a struct timeval for multiple selects in a loop without* reinitializing it. Here the microsecond part of the structure is re-* initialized before each select() call.*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -